style: formatting
This commit is contained in:
parent
bce3e3bf25
commit
906b2d03e3
81
wai.c
81
wai.c
|
@ -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,
|
||||||
|
@ -101,20 +101,14 @@ enum TYPE {
|
||||||
TYPE_I64 = 0x7E,
|
TYPE_I64 = 0x7E,
|
||||||
TYPE_F32 = 0x7D,
|
TYPE_F32 = 0x7D,
|
||||||
TYPE_F64 = 0x7C,
|
TYPE_F64 = 0x7C,
|
||||||
TYPE_V128= 0x7B,
|
TYPE_V128 = 0x7B,
|
||||||
TYPE_FUNCREF = 0x70,
|
TYPE_FUNCREF = 0x70,
|
||||||
TYPE_EXTERNREF = 0x6F
|
TYPE_EXTERNREF = 0x6F
|
||||||
};
|
};
|
||||||
|
|
||||||
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];
|
||||||
|
@ -145,25 +143,25 @@ void stack_push(struct stack *s, const struct value_t *value) {
|
||||||
|
|
||||||
switch (t) {
|
switch (t) {
|
||||||
case TYPE_I32:
|
case TYPE_I32:
|
||||||
printf("%d (I32)", *(int32_t*)value);
|
printf("%d (I32)", *(int32_t *)value);
|
||||||
break;
|
break;
|
||||||
case TYPE_I64:
|
case TYPE_I64:
|
||||||
printf("%ld (I32)", *(int64_t*)value);
|
printf("%ld (I32)", *(int64_t *)value);
|
||||||
break;
|
break;
|
||||||
case TYPE_F32:
|
case TYPE_F32:
|
||||||
printf("%f (F32)", *(float*)value);
|
printf("%f (F32)", *(float *)value);
|
||||||
break;
|
break;
|
||||||
case TYPE_F64:
|
case TYPE_F64:
|
||||||
printf("%f (F64)", *(double*)value);
|
printf("%f (F64)", *(double *)value);
|
||||||
break;
|
break;
|
||||||
case TYPE_V128:
|
case TYPE_V128:
|
||||||
printf("%ld (V128)", *(__int128*)value);
|
printf("%ld (V128)", *(__int128 *)value);
|
||||||
break;
|
break;
|
||||||
case TYPE_FUNCREF:
|
case TYPE_FUNCREF:
|
||||||
printf("%ld (EREF)", *(int64_t*)value);
|
printf("%ld (EREF)", *(int64_t *)value);
|
||||||
break;
|
break;
|
||||||
case TYPE_EXTERNREF:
|
case TYPE_EXTERNREF:
|
||||||
printf("%ld (EREF)", *(int64_t*)value);
|
printf("%ld (EREF)", *(int64_t *)value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
printf(", ");
|
printf(", ");
|
||||||
|
@ -172,8 +170,9 @@ 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,10 +200,12 @@ 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];
|
||||||
struct value_t a = {0};
|
struct value_t a = {0};
|
||||||
struct value_t b = {0};
|
struct value_t b = {0};
|
||||||
|
@ -226,7 +227,7 @@ int parse_instruction(struct module *module, u_char *binary, double param, int l
|
||||||
break;
|
break;
|
||||||
case INSTR_F64_CONST:
|
case INSTR_F64_CONST:
|
||||||
result.type = TYPE_F64;
|
result.type = TYPE_F64;
|
||||||
result.value.f64 = *(double*)&binary[i];
|
result.value.f64 = *(double *)&binary[i];
|
||||||
i += 8;
|
i += 8;
|
||||||
stack_push(&module->stack, &result);
|
stack_push(&module->stack, &result);
|
||||||
break;
|
break;
|
||||||
|
@ -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);
|
||||||
|
@ -322,7 +326,7 @@ int parse_section(struct module *module, u_char *binary, int len) {
|
||||||
incr(i, len);
|
incr(i, len);
|
||||||
printf("section %x with size %d\n", type, size);
|
printf("section %x with size %d\n", type, size);
|
||||||
|
|
||||||
switch ((enum section) type) {
|
switch ((enum section)type) {
|
||||||
case Section_Custom:
|
case Section_Custom:
|
||||||
break;
|
break;
|
||||||
case Section_Type:
|
case Section_Type:
|
||||||
|
@ -367,8 +371,10 @@ int parse_section(struct module *module, u_char *binary, int len) {
|
||||||
printf("section: exports\n");
|
printf("section: exports\n");
|
||||||
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,13 +385,13 @@ 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);
|
||||||
}
|
}
|
||||||
export->description = (int) binary[i];
|
export->description = (int)binary[i];
|
||||||
incr(i, len);
|
incr(i, len);
|
||||||
export->index = (uint32_t) binary[i];
|
export->index = (uint32_t)binary[i];
|
||||||
printf("export name: %s of type %d\n", export->name, export->description);
|
printf("export name: %s of type %d\n", export->name, export->description);
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -421,14 +429,14 @@ int parse_module(u_char *binary, size_t len) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
char *magic = "\0asm";
|
char *magic = "\0asm";
|
||||||
while (i < 4) {
|
while (i < 4) {
|
||||||
if ((char) binary[i] != magic[i]) {
|
if ((char)binary[i] != magic[i]) {
|
||||||
fprintf(stderr, "no wasm magic\n");
|
fprintf(stderr, "no wasm magic\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
incr(i, len);
|
incr(i, len);
|
||||||
}
|
}
|
||||||
printf("magic found\n");
|
printf("magic found\n");
|
||||||
printf("wasm version: %d\n", le32toh(*(int*)&binary[i]));
|
printf("wasm version: %d\n", le32toh(*(int *)&binary[i]));
|
||||||
i += 4;
|
i += 4;
|
||||||
printf("addr %d\n", i);
|
printf("addr %d\n", i);
|
||||||
|
|
||||||
|
@ -468,4 +476,3 @@ int main(int argc, char **argv) {
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue