Skip to content

Commit 0a8b8a6

Browse files
committed
web49_error more
1 parent 91aabaf commit 0a8b8a6

File tree

10 files changed

+30
-40
lines changed

10 files changed

+30
-40
lines changed

src/api/wasi.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ static web49_interp_data_t web49_api_wasi_fd_seek(void *wasi_untyped, web49_inte
2727
whence = SEEK_SET;
2828
break;
2929
default:
30-
fprintf(stderr, "bad wasi whence: %" PRIu32 "\n", interp.locals[2].i32_u);
31-
__builtin_trap();
30+
web49_error("bad wasi whence: %" PRIu32 "\n", interp.locals[2].i32_u);
3231
}
3332
if (interp.locals[0].i32_u <= 2 && whence == SEEK_SET) {
3433
WEB49_INTERP_WRITE(int64_t, interp, interp.locals[3].i32_u, interp.locals[1].i64_s);
@@ -180,8 +179,7 @@ static web49_interp_data_t web49_api_wasi_path_open(void *wasi_untyped, web49_in
180179
dirfd = open("./", O_RDONLY);
181180
break;
182181
default:
183-
fprintf(stderr, "unknown dirfd: %i\n", wdirfd);
184-
__builtin_trap();
182+
web49_error("unknown dirfd: %i\n", wdirfd);
185183
}
186184
#if defined(__WIN32__)
187185
int hostfd = open(host_path, flags, 0644);

src/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
#define WEB49_PRINT_INSTR 0
1010
#define WEB49_PRINT_INSTR_DEPTH 0
1111
#define WEB49_CHECK_BOUNDS 0
12+
#define WEB49_DEBUG_DIE 1
1213

1314
#endif

src/interp/interp.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#if WEB49_USE_SWTICH
66
#define OPCODE(n) (n)
77
#else
8-
#define OPCODE(n) ({size_t x = (n); if (ptrs[x] == NULL) {__builtin_trap();} ptrs[x]; })
8+
#define OPCODE(n) ({size_t x = (n); if (ptrs[x] == NULL) {web49_die();} ptrs[x]; })
99
#endif
1010

1111
web49_env_t *web49_env_new(void *state, web49_env_func_t func) {
@@ -375,10 +375,7 @@ uint32_t web49_interp_read_instr(web49_read_block_state_t *state, web49_instr_t
375375
args[i] = n;
376376
uint32_t end = state->depth;
377377
if (begin + 1 != end) {
378-
fprintf(stderr, "error: the below instruction (%s ...) did not yield exactly 1 value (debug: %" PRIu32 "+1 != %" PRIu32 ")\n", web49_opcode_to_name(cur.args[i].opcode), begin, end);
379-
web49_debug_print_instr(stderr, cur.args[i]);
380-
__builtin_trap();
381-
// state->depth = begin + 1;
378+
web49_error("error: the instruction (%s ...) did not yield exactly 1 value (debug: %" PRIu32 "+1 != %" PRIu32 ")\n", web49_opcode_to_name(cur.args[i].opcode), begin, end);
382379
}
383380
}
384381
// puts(web49_opcode_to_name(cur.opcode));
@@ -388,12 +385,10 @@ uint32_t web49_interp_read_instr(web49_read_block_state_t *state, web49_instr_t
388385
}
389386
uint32_t outer_end = state->depth;
390387
if (outer_begin + cur.nargs != outer_end) {
391-
fprintf(stderr, "error: stack should have been at %zu, but was at %zu\n", (size_t)(outer_begin + cur.nargs), (size_t)outer_end);
392-
web49_debug_print_instr(stderr, cur);
393-
__builtin_trap();
388+
web49_error("error: stack should have been at %zu, but was at %zu\n", (size_t)(outer_begin + cur.nargs), (size_t)outer_end);
394389
}
395390
if (state->depth < cur.nargs) {
396-
__builtin_trap();
391+
web49_error("error: bad number of arguments");
397392
}
398393
state->depth -= cur.nargs;
399394
if (cur.opcode == WEB49_OPCODE_NOP || cur.opcode == WEB49_OPCODE_UNREACHABLE) {
@@ -516,8 +511,7 @@ uint32_t web49_interp_read_instr(web49_read_block_state_t *state, web49_instr_t
516511
build->code[build->ncode++].data.v128 = WEB49_INTERP_V128(cur.immediate.u128);
517512
break;
518513
default:
519-
fprintf(stderr, "bad immediate: %zu\n", (size_t)cur.immediate.id);
520-
__builtin_trap();
514+
web49_error("bad immediate: %zu\n", (size_t)cur.immediate.id);
521515
}
522516
return ret;
523517
}
@@ -579,8 +573,7 @@ static void web49_interp_block_run_comp(web49_interp_block_t *block, void **ptrs
579573
} else if (block->nreturns == 1) {
580574
instrs[0].opcode = OPCODE(WEB49_OPCODE_FFI_CALL1);
581575
} else {
582-
fprintf(stderr, "error: import returns too many things\n");
583-
__builtin_trap();
576+
web49_error("error: import returns too many things\n");
584577
}
585578
web49_env_t *env = NULL;
586579
for (size_t nimport = 0; nimport < interp.num_env; nimport++) {
@@ -593,8 +586,7 @@ static void web49_interp_block_run_comp(web49_interp_block_t *block, void **ptrs
593586
}
594587
}
595588
if (env == NULL) {
596-
fprintf(stderr, "not implemented: %s.%s\n", block->module_str, block->field_str);
597-
__builtin_trap();
589+
web49_error("not implemented: %s.%s\n", block->module_str, block->field_str);
598590
}
599591
instrs[1].func = env;
600592
web49_free(block->module_str);

src/interp/interp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ uint32_t web49_interp_read_instr(web49_read_block_state_t *state, web49_instr_t
187187
void web49_free_interp(web49_interp_t interp);
188188

189189
#if WEB49_CHECK_BOUNDS
190-
#define WEB49_INTERP_BOUNDS(low, add) ({ fprintf(stderr, "memmory access 0x%zx of size 0x%zx out of bounds\n", (size_t) (low), (size_t) (add)); __builtin_trap(); })
190+
#define WEB49_INTERP_BOUNDS(low, add) ({ web49_error(stderr, "memmory access 0x%zx of size 0x%zx out of bounds\n", (size_t) (low), (size_t) (add)); })
191191
#else
192192
#define WEB49_INTERP_BOUNDS(low, add) (__builtin_unreachable())
193193
#endif

src/interp/interp0.inc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
NAME(WEB49_OPCODE_MEMORY_INIT) {
2-
fprintf(stderr, "memory init?\n");
3-
__builtin_trap();
2+
web49_error("memory init?\n");
43
NEXT();
54
}
65
NAME(WEB49_OPCODE_DATA_DROP) {
6+
web49_error("data drop? \n");
77
NEXT();
88
}
99
NAME(WEB49_OPCODE_TABLE_INIT) {
10-
fprintf(stderr, "table init? \n");
11-
__builtin_trap();
10+
web49_error("table init? \n");
1211
NEXT();
1312
}
1413
NAME(WEB49_OPCODE_ELEM_DROP) {
15-
fprintf(stderr, "elem drop?\n");
16-
__builtin_trap();
14+
web49_error("elem drop?\n");
1715
NEXT();
1816
}
1917
NAME(WEB49_OPCODE_TABLE_COPY) {
20-
fprintf(stderr, "table copy?\n");
21-
__builtin_trap();
18+
web49_error("table copy?\n");
2219
NEXT();
2320
}
2421
NAME(WEB49_OPCODE_FFI_CALL1) {
@@ -223,7 +220,7 @@ NAME(WEB49_OPCODE_CALL) {
223220
} else if (fblock->nlocals <= 1024) {
224221
head[-1].opcode = OPCODE(WEB49_OPCODE_CALL_DONE1024);
225222
} else {
226-
fprintf(stderr, "too many locals: %zu\n", (size_t)fblock->nlocals);
223+
web49_error("too many locals: %zu\n", (size_t)fblock->nlocals);
227224
__builtin_trap();
228225
}
229226
head[0].opcodes = fblock->code;

src/lib.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,16 @@ extern size_t web49_total_alloc;
3939
#define web49_realloc(ptr, size) (realloc(ptr, size))
4040
#define web49_free(ptr) (free((void *)ptr))
4141

42+
#define web49_unreachable() __builtin_unreachable()
43+
44+
#if WEB49_DEBUG_DIE
45+
#define web49_die() __builtin_trap()
46+
#else
47+
#define web49_die() exit(1)
48+
#endif
49+
4250
#define web49_error(...) \
4351
fprintf(stderr, __VA_ARGS__); \
44-
__builtin_trap()
52+
web49_die()
4553

4654
#endif

src/opt/tree.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ static void web49_opt_untree_emit_counting(web49_opcode_t op, uint32_t num, size
214214
static void web49_opt_untree(web49_module_t *mod, uint32_t func_nreturns, web49_block_list_t **pblocks, web49_instr_t cur, size_t *len, web49_instr_t **out, size_t *alloc) {
215215
size_t nargs = cur.nargs;
216216
cur.nargs = 0;
217-
printf("*pblocks = %p\n", *pblocks);
218217
if (cur.opcode == WEB49_OPCODE_IF) {
219218
web49_block_list_t list = (web49_block_list_t){
220219
.ret = false,
@@ -358,14 +357,13 @@ static void web49_opt_untree(web49_module_t *mod, uint32_t func_nreturns, web49_
358357
c += 1;
359358
}
360359
}
361-
fprintf(stderr, "could not find (func %zu)\n", (size_t)cur.immediate.varuint32);
362-
__builtin_trap();
360+
web49_error("could not find (func %zu)\n", (size_t)cur.immediate.varuint32);
363361
} else {
364362
web49_section_function_t func = web49_module_get_section(*mod, WEB49_SECTION_ID_FUNCTION).function_section;
365363
ent = type.entries[func.entries[cur.immediate.varuint32 - thresh]];
366364
}
367365
} else {
368-
__builtin_trap();
366+
web49_error("internal error: trying to handle an unhandled case");
369367
}
370368
found_call_ent:;
371369
if (cur.opcode == WEB49_OPCODE_CALL) {
@@ -424,7 +422,6 @@ static void web49_opt_tree_code(web49_module_t *mod, web49_section_code_entry_t
424422
};
425423
blocks += 1;
426424
for (size_t i = 0; i < entry->num_instrs; i++) {
427-
web49_debug_print_instr(stdout, entry->instrs[i]);
428425
web49_opt_untree(mod, type.num_returns, &blocks, entry->instrs[i], &len, head, alloc);
429426
}
430427
web49_instr_t bump = (*head)[--len];

src/read_bin.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,7 @@ web49_instr_immediate_t web49_readbin_instr_immediate(web49_io_input_t *in, web4
602602
}
603603
return ret;
604604
}
605-
fprintf(stderr, "unknown internal immediate id %zu\n", (size_t)id);
606-
__builtin_trap();
605+
web49_error("unknown internal immediate id %zu\n", (size_t)id);
607606
}
608607

609608
web49_instr_t web49_readbin_init_expr(web49_io_input_t *in) {

src/read_wat.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "./ast.h"
44
#include "./tables.h"
55

6-
#define exit(n) __builtin_trap()
7-
86
web49_instr_t web49_readwat_instr_buf(web49_readwat_state_t *out, web49_opcode_t opcode, size_t nargs, web49_readwat_expr_t *args, bool is_in_parens);
97

108
static void web49_readwat_table_set(web49_readwat_table_t *restrict table, const char *key, uint64_t value) {

src/tables.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2196,7 +2196,7 @@ web49_opcode_t web49_bytes_to_opcode(uint8_t *bytes) {
21962196
case 0xFF:
21972197
web49_error("unknown opcode: staring with byte 0xFF\n");
21982198
}
2199-
__builtin_trap();
2199+
web49_unreachable();
22002200
}
22012201

22022202
void web49_opcode_to_bytes(web49_opcode_t opcode, size_t *len, uint8_t *buf) {

0 commit comments

Comments
 (0)