style: formatting

This commit is contained in:
Orangerot 2025-05-13 03:40:22 +02:00
parent bce3e3bf25
commit 906b2d03e3

47
wai.c
View file

@ -24,12 +24,12 @@
*/ */
#include <endian.h> #include <endian.h>
#include <inttypes.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <inttypes.h>
enum section { enum section {
Section_Custom, Section_Custom,
@ -107,14 +107,8 @@ enum TYPE {
}; };
static const int TYPE_SIZE[] = { static const int TYPE_SIZE[] = {
[TYPE_I32] = 4, [TYPE_I32] = 4, [TYPE_I64] = 8, [TYPE_F32] = 4, [TYPE_F64] = 8,
[TYPE_I64] = 8, [TYPE_V128] = 16, [TYPE_FUNCREF] = 16, [TYPE_EXTERNREF] = 16};
[TYPE_F32] = 4,
[TYPE_F64] = 8,
[TYPE_V128] = 16,
[TYPE_FUNCREF] = 16,
[TYPE_EXTERNREF] = 16
};
struct value_t { struct value_t {
enum TYPE type; enum TYPE type;
@ -129,7 +123,11 @@ struct value_t {
} value; } value;
}; };
#define incr(i, len) i++; if (i >= len) {return -1;} #define incr(i, len) \
i++; \
if (i >= len) { \
return -1; \
}
void stack_push(struct stack *s, const struct value_t *value) { void stack_push(struct stack *s, const struct value_t *value) {
size_t type_size = TYPE_SIZE[value->type]; size_t type_size = TYPE_SIZE[value->type];
@ -173,7 +171,8 @@ void stack_push(struct stack *s, const struct value_t *value) {
void stack_top(struct stack *s, struct value_t *value) { void stack_top(struct stack *s, struct value_t *value) {
value->type = s->items[s->bytes - 1]; value->type = s->items[s->bytes - 1];
memcpy(&value->value, &(s->items[s->bytes - 1 - TYPE_SIZE[value->type]]), TYPE_SIZE[value->type]); memcpy(&value->value, &(s->items[s->bytes - 1 - TYPE_SIZE[value->type]]),
TYPE_SIZE[value->type]);
} }
void stack_pop(struct stack *s, struct value_t *value) { void stack_pop(struct stack *s, struct value_t *value) {
@ -201,8 +200,10 @@ int parse_type(u_char *binary, int len) {
return i; return i;
} }
int parse_function(struct module *module, u_char *binary, double param, int len); int parse_function(struct module *module, u_char *binary, double param,
int parse_instruction(struct module *module, u_char *binary, double param, int len) { int len);
int parse_instruction(struct module *module, u_char *binary, double param,
int len) {
int i = 0; int i = 0;
enum INSTRUCTION instr = (u_char)binary[i]; enum INSTRUCTION instr = (u_char)binary[i];
u_char *instr_addr = &binary[i]; u_char *instr_addr = &binary[i];
@ -290,17 +291,20 @@ int parse_instruction(struct module *module, u_char *binary, double param, int l
case INSTR_LOCAL_GET: { case INSTR_LOCAL_GET: {
int local_index = binary[i]; int local_index = binary[i];
incr(i, len); incr(i, len);
stack_push(&module->stack, &(struct value_t) {.value.f64 = param, .type = TYPE_F64}); stack_push(&module->stack,
&(struct value_t){.value.f64 = param, .type = TYPE_F64});
break; break;
} }
default: default:
printf("unknown instruction! %x at %lx\n", instr, instr_addr - module->binary); printf("unknown instruction! %x at %lx\n", instr,
instr_addr - module->binary);
exit(1); exit(1);
} }
return i; return i;
} }
int parse_function(struct module *module, u_char *binary, double param, int len) { int parse_function(struct module *module, u_char *binary, double param,
int len) {
int i = 0; int i = 0;
int body_size = binary[i]; int body_size = binary[i];
incr(i, len); incr(i, len);
@ -368,7 +372,9 @@ int parse_section(struct module *module, u_char *binary, int len) {
int num_exports = binary[i]; int num_exports = binary[i];
if (num_exports > MAX_FUNCTIONS) { if (num_exports > MAX_FUNCTIONS) {
printf("Number of exports exceeds maximum number of functions in a module (%d)", MAX_FUNCTIONS); printf("Number of exports exceeds maximum number of functions in a "
"module (%d)",
MAX_FUNCTIONS);
return -1; return -1;
} }
@ -379,7 +385,7 @@ int parse_section(struct module *module, u_char *binary, int len) {
export->name_length = binary[i]; export->name_length = binary[i];
incr(i, len); incr(i, len);
for (int si = 0; si < export->name_length; si++) { for (size_t si = 0; si < export->name_length; si++) {
export->name[si] = binary[i]; export->name[si] = binary[i];
incr(i, len); incr(i, len);
} }
@ -413,7 +419,9 @@ int parse_section(struct module *module, u_char *binary, int len) {
exit(1); exit(1);
} }
if (size == 0x0) {incr(i, len);} if (size == 0x0) {
incr(i, len);
}
return i; return i;
} }
@ -468,4 +476,3 @@ int main(int argc, char **argv) {
fclose(file); fclose(file);
return 0; return 0;
} }