varaq-wasm-c/compile_test.c

145 lines
4.2 KiB
C
Raw Normal View History

2023-03-04 13:20:59 -05:00
#include "compiler.h"
#include "common.h"
#include "tokenizer.h"
Code *
demo_function_compile ()
{
Code *add_args_code = (Code *)calloc (1, sizeof (Code));
append_byte (add_args_code, f32);
append_byte (add_args_code, f32);
Code *add_return_code = (Code *)calloc (1, sizeof (Code));
append_byte (add_return_code, f32);
Code *add_function_type = (Code *)calloc (1, sizeof (Code));
append_byte (add_function_type, FUNCTION);
append (add_function_type, encodeVector (add_args_code));
append (add_function_type, encodeVector (add_return_code));
add_function_type->override = true;
add_function_type->override_count = 1;
Code *type_section = createSection (TYPE, encodeVector (add_function_type));
Code *return_type_code = (Code *)calloc (1, sizeof (Code));
append_byte (return_type_code, 0x00);
return_type_code->override = true;
return_type_code->override_count = 1;
Code *func_section = createSection (FUNC, encodeVector (return_type_code));
Code *exp = encodeString ("main");
append_byte (exp, EXPORT_FUNC);
append_byte (exp, 0x00);
exp->override = true;
exp->override_count = 1;
Code *export_section = createSection (EXPORT, encodeVector (exp));
Code *code = (Code *)calloc (1, sizeof (Code));
append_byte (code, LOCAL_GET);
append (code, unsignedLEB128 (0));
append_byte (code, LOCAL_GET);
append (code, unsignedLEB128 (1));
append_byte (code, F32_ADD);
Code *body = (Code *)calloc (1, sizeof (Code));
append_byte (body, EMPTY_ARRAY);
append (body, code);
append_byte (body, END);
Code *function_body = (Code *)calloc (1, sizeof (Code));
append (function_body, encodeVector (body));
function_body->override = true;
function_body->override_count = 1;
Code *code_section = createSection (CODE, encodeVector (function_body));
Code *tape = (Code *)malloc (sizeof (Code));
tape->cells = calloc (8, sizeof (uint8_t));
tape->cells[0] = 0;
tape->cells[1] = 'a';
tape->cells[2] = 's';
tape->cells[3] = 'm';
tape->cells[4] = 1;
tape->cells[5] = 0;
tape->cells[6] = 0;
tape->cells[7] = 0;
tape->count = 8;
append (tape, type_section);
append (tape, func_section);
append (tape, export_section);
append (tape, code_section);
return tape;
}
Code *
demo_add_compile ()
{
Code *add_args_code = (Code *)calloc (1, sizeof (Code));
Code *add_return_code = (Code *)calloc (1, sizeof (Code));
append_byte (add_return_code, f64);
Code *add_function_type = (Code *)calloc (1, sizeof (Code));
append_byte (add_function_type, FUNCTION);
append (add_function_type, encodeVector (add_args_code));
append (add_function_type, encodeVector (add_return_code));
add_function_type->override = true;
add_function_type->override_count = 1;
Code *type_section = createSection (TYPE, encodeVector (add_function_type));
Code *return_type_code = (Code *)calloc (1, sizeof (Code));
append_byte (return_type_code, 0x00);
return_type_code->override = true;
return_type_code->override_count = 1;
Code *func_section = createSection (FUNC, encodeVector (return_type_code));
Code *exp = encodeString ("main");
append_byte (exp, EXPORT_FUNC);
append_byte (exp, 0x00);
exp->override = true;
exp->override_count = 1;
Code *export_section = createSection (EXPORT, encodeVector (exp));
Code *code = (Code *)calloc (1, sizeof (Code));
append_byte (code, F64_CONST);
append_f64 (code, 6.7);
append_byte (code, F64_CONST);
append_f64 (code, 8.5);
append_byte (code, F64_ADD);
Code *body = (Code *)calloc (1, sizeof (Code));
append_byte (body, EMPTY_ARRAY);
append (body, code);
append_byte (body, END);
Code *function_body = (Code *)calloc (1, sizeof (Code));
append (function_body, encodeVector (body));
function_body->override = true;
function_body->override_count = 1;
Code *code_section = createSection (CODE, encodeVector (function_body));
Code *tape = (Code *)malloc (sizeof (Code));
tape->cells = calloc (8, sizeof (uint8_t));
tape->cells[0] = 0;
tape->cells[1] = 'a';
tape->cells[2] = 's';
tape->cells[3] = 'm';
tape->cells[4] = 1;
tape->cells[5] = 0;
tape->cells[6] = 0;
tape->cells[7] = 0;
tape->count = 8;
append (tape, type_section);
append (tape, func_section);
append (tape, export_section);
append (tape, code_section);
return tape;
}