From 311010f4e6ca8f6ef0b7b615344dc8be75beee04 Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 15 May 2025 22:25:07 +0200 Subject: [PATCH] refactor: get rid of warnings --- wai.c | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/wai.c b/wai.c index 9a5ed3a..8bf4cc2 100644 --- a/wai.c +++ b/wai.c @@ -156,7 +156,7 @@ struct value_t { } value; }; -#define incr(i, len) i++; if (i >= len) {return -1;} +#define incr(i, len) i++; if (i >= len) {return 0;} void print_value(struct value_t *value) { void *number = &value->value; @@ -174,7 +174,7 @@ void print_value(struct value_t *value) { printf("%f", *(double*)number); break; case TYPE_V128: - printf("%ld", *(__int128*)number); + printf("%f", (double) *(__int128*)number); break; case TYPE_FUNCREF: printf("%ld", *(int64_t*)number); @@ -188,7 +188,7 @@ void print_value(struct value_t *value) { void stack_peak(struct stack *s, struct value_t *value, size_t nth, size_t from) { int byte_i = from - 1; - for (int element_i = 0; element_i < nth && byte_i > 0; element_i++) { + for (size_t element_i = 0; element_i < nth && byte_i > 0; element_i++) { byte_i -= TYPE_SIZE[s->items[byte_i]]; } value->type = s->items[byte_i]; @@ -203,9 +203,6 @@ void stack_push(struct stack *s, const struct value_t *value) { printf("stack: "); for (int i = s->bytes - 1; i > 0; i -= TYPE_SIZE[s->items[i]] + 1) { - enum TYPE t = s->items[i]; - size_t type_size = TYPE_SIZE[t]; - void *number = &s->items[i - type_size]; struct value_t stack_value = {0}; stack_peak(s, &stack_value, 0, i + 1); print_value(&stack_value); @@ -224,9 +221,9 @@ void stack_pop(struct stack *s, struct value_t *value) { s->bytes -= TYPE_SIZE[value->type] + 1; } -int parse_function(struct module *module, size_t func_i, int len); -int parse_instruction(struct module *module, u_char *binary, size_t func_i, size_t func_stack_begin, int len) { - int i = 0; +int parse_function(struct module *module, size_t func_i, size_t len); +int parse_instruction(struct module *module, u_char *binary, size_t func_i, size_t func_stack_begin, size_t len) { + size_t i = 0; enum INSTRUCTION instr = binary[i]; u_char *instr_addr = &binary[i]; struct value_t a = {0}; @@ -329,13 +326,13 @@ int parse_instruction(struct module *module, u_char *binary, size_t func_i, size return i; } -int parse_function(struct module *module, size_t func_i, int len) { - int i = 0; +int parse_function(struct module *module, size_t func_i, size_t len) { + size_t i = 0; struct func_t *func = &module->func[func_i]; u_char *binary = func->addr; size_t func_type_i = func->func_type_index; struct func_type_t *func_type = &module->func_types[func_type_i]; - int body_size = binary[i]; + // int body_size = binary[i]; size_t func_stack_begin = module->stack.bytes; size_t func_stack_end; struct value_t result = {0}; @@ -343,8 +340,8 @@ int parse_function(struct module *module, size_t func_i, int len) { incr(i, len); func->num_local_vars = binary[i]; incr(i, len); - for (int local_var_i = 0; local_var_i < func->num_local_vars; local_var_i++) { - stack_push(&module->stack, &(struct value_t) {.type = binary[i], .value = 0}); + for (size_t local_var_i = 0; local_var_i < func->num_local_vars; local_var_i++) { + stack_push(&module->stack, &(struct value_t) {.type = binary[i], .value = {0}}); incr(i, len); } while (binary[i] != INSTR_END) { @@ -364,8 +361,8 @@ int parse_function(struct module *module, size_t func_i, int len) { return i; } -int parse_section(struct module *module, u_char *binary, int len) { - int i = 0; +int parse_section(struct module *module, u_char *binary, size_t len) { + size_t i = 0; enum section type = binary[i]; incr(i, len); int size = binary[i]; @@ -383,7 +380,7 @@ int parse_section(struct module *module, u_char *binary, int len) { for (size_t type_i = 0; type_i < num_types; type_i++) { if (binary[i] != 0x60) { printf("expected function type, found %x\n", binary[i]); - return -1; + return 0; } incr(i, len); func_type = &module->func_types[type_i]; @@ -424,7 +421,7 @@ int parse_section(struct module *module, u_char *binary, int len) { if(module->num_exports > MAX_FUNCTIONS) { printf("Number of exports exceeds maximum number of functions in a module (%d)", MAX_FUNCTIONS); - return -1; + return 0; } incr(i, len); @@ -495,7 +492,7 @@ int parse_section(struct module *module, u_char *binary, int len) { } int parse_module(struct module *module, u_char *binary, size_t len) { - int i = 0; + size_t i = 0; char *magic = "\0asm"; while (i < 4) { if ((char) binary[i] != magic[i]) { @@ -507,7 +504,7 @@ int parse_module(struct module *module, u_char *binary, size_t len) { printf("magic found\n"); printf("wasm version: %d\n", le32toh(*(int*)&binary[i])); i += 4; - printf("addr %d\n", i); + printf("addr %zu\n", i); module->binary = binary; @@ -557,7 +554,7 @@ int main(int argc, char **argv) { size_t function_search_i = module.exports[export_search_i].func_index; size_t function_search_type_index = module.func[function_search_i].func_type_index; struct func_type_t *func_type_search = &module.func_types[function_search_type_index]; - if (func_type_search->num_params != argc - 3) { + if (func_type_search->num_params != (size_t) (argc - 3)) { printf("Not enough function arguments provided. Got %d expected %zu. \n", argc - 3, func_type_search->num_params); exit(1); }