Compare commits

..

1 commit

Author SHA1 Message Date
Orangerot 3f13610b96 feat: use types of values in stack and instructions 2025-05-12 00:52:51 +02:00
2 changed files with 14 additions and 34 deletions

View file

@ -3,9 +3,10 @@
# SPDX-License-Identifier: GPL-3.0
wai: wai.c
gcc -ggdb -Wall -Wextra -o wai wai.c
gcc -Wall -Wextra -o wai wai.c
# tests/factorial.wasm: tests/factorial.wat
.PHONY: tests
tests:
wat2wasm -o tests/factorial.wasm tests/factorial.wat

45
wai.c
View file

@ -30,6 +30,7 @@
#include <string.h>
#include <sys/stat.h>
#include <inttypes.h>
#include <sys/types.h>
enum section {
Section_Custom,
@ -53,24 +54,9 @@ struct stack {
size_t bytes;
};
#define MAX_FUNCTIONS 128
enum export_desc {
Export_Func,
Export_Table,
Export_Mem,
Export_Global,
};
struct export_t {
u_char name[128];
size_t name_length;
uint32_t index;
enum export_desc description;
};
struct module {
struct type_t *types;
u_char *funcs[MAX_FUNCTIONS];
u_char *funcs[128];
struct table_t *tables;
struct mem_t *mems;
struct global_t *globals;
@ -78,7 +64,7 @@ struct module {
struct data_t *datas;
struct start_t *start;
struct import_t *imports;
struct export_t exports[MAX_FUNCTIONS];
struct export_t *exports;
u_char *binary;
struct stack stack;
int scope;
@ -366,27 +352,19 @@ int parse_section(struct module *module, u_char *binary, int len) {
case Section_Export:
printf("section: exports\n");
int num_exports = binary[i];
if(num_exports > MAX_FUNCTIONS) {
printf("Number of exports exceeds maximum number of functions in a module (%d)", MAX_FUNCTIONS);
return -1;
}
incr(i, len);
for (int exports_i = 0; exports_i < num_exports; exports_i++) {
struct export_t *export = &module->exports[i];
export->name_length = binary[i];
int string_len = binary[i];
incr(i, len);
for (int si = 0; si < export->name_length; si++) {
export->name[si] = binary[i];
printf("export name: ");
for (int si = 0; si < string_len; si++) {
putchar(binary[i]);
incr(i, len);
}
export->description = (int) binary[i];
putchar('\n');
// export kind
incr(i, len);
export->index = (uint32_t) binary[i];
printf("export name: %s of type %d\n", export->name, export->description);
// export func index
incr(i, len);
}
break;
@ -441,6 +419,7 @@ int parse_module(u_char *binary, size_t len) {
return i;
}
int main(int argc, char **argv) {
if (argc != 2) {
@ -458,7 +437,7 @@ int main(int argc, char **argv) {
printf("size: %ld\n", st.st_size);
u_char *binary = malloc(st.st_size);
fread(binary, st.st_size, 1, file);
fread(binary, st.st_size, st.st_size, file);
if (parse_module(binary, st.st_size) == -1) {
printf("error :(\n");