| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <stdlib.h> | ||
| 2 | #include <string.h> | ||
| 3 | #include <stdarg.h> | ||
| 4 | #include "gwion_util.h" | ||
| 5 | #include "gwion_ast.h" | ||
| 6 | #include "gwion_env.h" | ||
| 7 | #include "vm.h" | ||
| 8 | #include "instr.h" | ||
| 9 | #include "gwion.h" | ||
| 10 | #include "object.h" | ||
| 11 | #include "operator.h" | ||
| 12 | #include "import.h" | ||
| 13 | #include "gack.h" | ||
| 14 | |||
| 15 | 1609 | ANN2(1) static int fmtlen(const char *fmt, va_list args) { | |
| 16 | va_list tmpa; | ||
| 17 | 1609 | va_copy(tmpa, args); | |
| 18 | 1609 | const int size = vsnprintf(NULL, 0, fmt, tmpa); | |
| 19 | 1609 | va_end(tmpa); | |
| 20 | 1609 | return size; | |
| 21 | } | ||
| 22 | |||
| 23 | ANN2(2) | ||
| 24 | 1609 | static int gw_vasprintf(MemPool mp, char **str, const char *fmt, va_list args) { | |
| 25 | 1609 | char * base = *str; | |
| 26 |
2/2✓ Branch 0 taken 501 times.
✓ Branch 1 taken 1108 times.
|
1609 | const size_t base_len = base ? strlen(base) : 0; |
| 27 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1609 times.
|
1609 | DECL_BB(const int, size, = fmtlen(fmt, args)); |
| 28 | 1609 | char *ret = mp_malloc2(mp, base_len + size + 1); | |
| 29 |
2/2✓ Branch 0 taken 501 times.
✓ Branch 1 taken 1108 times.
|
1609 | if (base) strcpy(ret, base); |
| 30 | 1609 | const int final_len = vsprintf(ret + base_len, fmt, args); | |
| 31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1609 times.
|
1609 | if (final_len < 0) { |
| 32 | ✗ | mp_free2(mp, base_len + size + 1, ret); | |
| 33 | ✗ | return -1; | |
| 34 | } | ||
| 35 |
2/2✓ Branch 0 taken 501 times.
✓ Branch 1 taken 1108 times.
|
1609 | if (base) mp_free2(mp, strlen(base), base); |
| 36 | 1609 | *str = ret; | |
| 37 | 1609 | return final_len; | |
| 38 | } | ||
| 39 | |||
| 40 | 1609 | ANN2(2) int gw_asprintf(MemPool mp, char **str, const char *fmt, ...) { | |
| 41 | va_list args; | ||
| 42 | 1609 | va_start(args, fmt); | |
| 43 | 1609 | const int ret = gw_vasprintf(mp, str, fmt, args); | |
| 44 | 1609 | va_end(args); | |
| 45 | 1609 | return ret; | |
| 46 | } | ||
| 47 | |||
| 48 | 4 | ANN static void prepare_call(const VM_Shred shred, const m_uint offset) { | |
| 49 | 4 | const m_uint push = offset + SZ_INT + sizeof(struct frame_t); | |
| 50 | 4 | shred->mem += push; | |
| 51 | 4 | struct frame_t *frame = | |
| 52 | 4 | (struct frame_t *)(shred->mem - sizeof(struct frame_t)); | |
| 53 | 4 | frame->push = push; | |
| 54 | 4 | frame->code = shred->code; | |
| 55 | 4 | frame->pc = shred->pc; | |
| 56 | 4 | shred->pc = 0; | |
| 57 | 4 | } | |
| 58 | |||
| 59 | 1606 | ANN void gack(const VM_Shred shred, const m_uint offset) { | |
| 60 | 1606 | const Type t = *(Type *)shred->reg; | |
| 61 | 1606 | const VM_Code code = get_gack(t)->info->gack; | |
| 62 |
2/2✓ Branch 0 taken 1602 times.
✓ Branch 1 taken 4 times.
|
1606 | if (code->builtin) { |
| 63 | 1602 | const m_uint sz = *(m_uint *)(shred->reg + SZ_INT); | |
| 64 | 1602 | ((f_gack)code->native_func)(t, (shred->reg - sz), shred); | |
| 65 | 1602 | POP_REG(shred, sz); | |
| 66 | } else { | ||
| 67 | 4 | prepare_call(shred, offset); | |
| 68 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
|
4 | if (tflag(t, tflag_struct)) |
| 69 | 2 | *(void **)(shred->mem) = (void *)(shred->reg - t->size); | |
| 70 | else | ||
| 71 | 2 | *(M_Object *)(shred->mem) = *(M_Object *)(shred->reg - SZ_INT); | |
| 72 | 4 | shred->code = code; | |
| 73 | } | ||
| 74 | 1606 | return; | |
| 75 | } | ||
| 76 |