From 6eeb0f352152c80ca5d51753651df5183990be4e Mon Sep 17 00:00:00 2001 From: Shaw Summa Date: Sun, 15 Oct 2023 04:33:55 -0400 Subject: [PATCH] basic example --- .gitignore | 4 ++- .vscode/settings.json | 5 +++- test/return.paka | 2 ++ vm/ir.h | 4 --- vm/jit/tb.c | 47 +-------------------------------- vm/jit/test.c | 60 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 52 deletions(-) create mode 100644 test/return.paka create mode 100644 vm/jit/test.c diff --git a/.gitignore b/.gitignore index 342e36c1..b9ac5ff3 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,6 @@ libminivm.lib *.bin *.core .minivm-history -out.* \ No newline at end of file +out.* +*.rdbg +*.stackdump \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 296fbd14..64f6c6c5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,6 +19,9 @@ "gc_priv.h": "c", "windows.h": "c", "winapifamily.h": "c", - "gc.h": "c" + "gc.h": "c", + "tb.h": "c", + "obj.h": "c", + "type.h": "c" } } \ No newline at end of file diff --git a/test/return.paka b/test/return.paka new file mode 100644 index 00000000..d879e4f2 --- /dev/null +++ b/test/return.paka @@ -0,0 +1,2 @@ + +return 4984 diff --git a/vm/ir.h b/vm/ir.h index 9c12eefc..a9401b2f 100644 --- a/vm/ir.h +++ b/vm/ir.h @@ -152,10 +152,6 @@ struct vm_block_t { bool mark : 1; }; -void vm_instr_free(vm_instr_t *instr); -void vm_block_free(vm_block_t *block); -void vm_blocks_free(size_t nblocks, vm_block_t *blocks); - void vm_block_realloc(vm_block_t *block, vm_instr_t instr); void vm_print_arg(FILE *out, vm_arg_t val); diff --git a/vm/jit/tb.c b/vm/jit/tb.c index 92214ded..2219ddb1 100644 --- a/vm/jit/tb.c +++ b/vm/jit/tb.c @@ -1,58 +1,13 @@ - #include "../obj.h" #include "../std/std.h" #include "../type.h" #include "../../tb/include/tb.h" -typedef double callable_t(double, double); +typedef void __attribute__((cdecl)) callable_t(double, double); vm_std_value_t vm_x64_run(vm_block_t *block, vm_table_t *std, vm_std_value_t *args) { - TB_FeatureSet features = (TB_FeatureSet) {0}; - TB_Module* module = tb_module_create_for_host(&features, true); - TB_Function* function = tb_function_create( module, -1, "test", TB_LINKAGE_PUBLIC); - TB_PrototypeParam proto_params[2] = { - { TB_TYPE_F64 }, - { TB_TYPE_F64 }, - }; - TB_PrototypeParam proto_return[1] = { - { TB_TYPE_F64 }, - }; - TB_FunctionPrototype *proto = tb_prototype_create(module, TB_CDECL, 2, proto_params, 1, proto_return, false); - tb_function_set_prototype( - function, - -1, proto, - NULL - ); - - // TB_Node *a0 = tb_inst_local(function, sizeof(double), _Alignof(double)); - // TB_Node *a1 = tb_inst_local(function, sizeof(double), _Alignof(double)); - - // tb_inst_store(function, TB_TYPE_F64, a0, tb_inst_param(function, 0), _Alignof(double), false); - // tb_inst_store(function, TB_TYPE_F64, a1, tb_inst_param(function, 1), _Alignof(double), false); - - TB_Node *a0 = tb_inst_param(function, 0); - TB_Node *a1 = tb_inst_param(function, 1); - - TB_Node *add_result = tb_inst_add( - function, - a0, - a1, - 0 - ); - tb_inst_ret(function, 1, &add_result); - - TB_Passes *passes = tb_pass_enter(function, tb_function_get_arena(function)); - TB_FunctionOutput *out = tb_pass_codegen(passes, true); - tb_output_print_asm(out, stdout); - - TB_JIT *jit = tb_jit_begin(module, 0); - callable_t *the_func = (callable_t *) tb_jit_place_function(jit, function); - - printf("%f\n", the_func(10, 20)); - tb_jit_end(jit); - return (vm_std_value_t) { .tag = VM_TAG_NIL, }; diff --git a/vm/jit/test.c b/vm/jit/test.c new file mode 100644 index 00000000..b4b568a7 --- /dev/null +++ b/vm/jit/test.c @@ -0,0 +1,60 @@ + +#include "../lib.h" +#include "../../tb/include/tb.h" + +typedef double __attribute__((cdecl)) callable_t(double, double); + +int main() { + TB_FeatureSet features = (TB_FeatureSet) {0}; + TB_Module* module = tb_module_create_for_host(&features, true); + TB_Function* function = tb_function_create( module, -1, "test", TB_LINKAGE_PUBLIC); + TB_PrototypeParam proto_params[2] = { + { TB_TYPE_F64 }, + { TB_TYPE_F64 }, + }; + TB_PrototypeParam proto_return[1] = { + { TB_TYPE_F64 }, + }; + TB_FunctionPrototype *proto = tb_prototype_create(module, TB_CDECL, 2, proto_params, 1, proto_return, false); + tb_function_set_prototype( + function, + -1, proto, + NULL + ); + + TB_Node *a0 = tb_inst_param(function, 0); + TB_Node *a1 = tb_inst_param(function, 1); + + TB_Node *add_result = tb_inst_fadd( + function, + a0, + a1 + ); + + tb_inst_ret(function, 1, &add_result); + + TB_Passes *passes = tb_pass_enter(function, tb_function_get_arena(function)); + tb_pass_print(passes); + + TB_FunctionOutput *out = tb_pass_codegen(passes, true); + tb_output_print_asm(out, stdout); + + tb_pass_exit(passes); + + TB_JIT *jit = tb_jit_begin(module, 1 << 16); + callable_t *the_func1 = (callable_t *) tb_jit_place_function(jit, function); + + callable_t *the_func2 = (callable_t *) tb_jit_get_code_ptr(function); + + printf("%p %p\n", the_func1, the_func2); + + double x = 12.3; + double y = 23.4; + + double res = the_func2(x, y); + + printf("%f = %f + %f\n", res, x, y); + tb_jit_end(jit); + + return 0; +} \ No newline at end of file