Skip to content

Commit

Permalink
basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawSumma committed Oct 15, 2023
1 parent edfdcba commit 6eeb0f3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 52 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ libminivm.lib
*.bin
*.core
.minivm-history
out.*
out.*
*.rdbg
*.stackdump
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 2 additions & 0 deletions test/return.paka
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

return 4984
4 changes: 0 additions & 4 deletions vm/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 1 addition & 46 deletions vm/jit/tb.c
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down
60 changes: 60 additions & 0 deletions vm/jit/test.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 6eeb0f3

Please sign in to comment.