Commit df4eb677 authored by Kunshan Wang's avatar Kunshan Wang

Test new API in C.

parent 2e78a5d0
......@@ -26,6 +26,8 @@ int *muerrno;
exit(1); \
}} while(0)
#define SYM(i) MuID i = b->gen_sym(b, "@" #i)
int main(int argc, char** argv) {
MuVM *mvm = mu_refimpl2_new_ex(gc_conf);
......@@ -35,96 +37,141 @@ int main(int argc, char** argv) {
MUCHECKERR();
MuBundleNode b = ctx->new_bundle(ctx);
MuIRBuilder *b = ctx->new_ir_builder(ctx);
// primitive types
MuTypeNode i1 = ctx->new_type_int(ctx, b, 1);
MuTypeNode i8 = ctx->new_type_int(ctx, b, 8);
MuTypeNode i32 = ctx->new_type_int(ctx, b, 32);
MuTypeNode i64 = ctx->new_type_int(ctx, b, 64);
MuTypeNode pi8 = ctx->new_type_uptr(ctx, b);
MuTypeNode ppi8 = ctx->new_type_uptr(ctx, b);
ctx->set_type_uptr(ctx, pi8, i8);
ctx->set_type_uptr(ctx, ppi8, pi8);
MuID i32_id = ctx->get_id(ctx, b, i32);
MuID ppi8_id = ctx->get_id(ctx, b, ppi8);
MuID i1 = b->gen_sym(b, "@i1"); // The raw form
SYM(i8); // My convenient macro :)
SYM(i32);
SYM(i64);
SYM(pi8);
SYM(ppi8);
// typedef uint8_t i8;
// ...
b->new_type_int(b, i1, 1);
b->new_type_int(b, i8, 8);
b->new_type_int(b, i32, 32);
b->new_type_int(b, i64, 64);
b->new_type_uptr(b, pi8, i8);
b->new_type_uptr(b, ppi8, pi8);
// signature for the main function
// typedef void (main_sig)(i32, ppi8);
SYM(main_sig);
MuTypeNode main_paramtys[] = { i32, ppi8 };
MuFuncSigNode main_sig = ctx->new_funcsig(ctx, b, main_paramtys, 2,
NULL, 0);
b->new_funcsig(b, main_sig, main_paramtys, 2, NULL, 0);
// signature and ufuncptr for the write function
MuTypeNode hvoid = ctx->new_type_void(ctx, b);
MuTypeNode pvoid = ctx->new_type_uptr(ctx, b);
ctx->set_type_uptr(ctx, pvoid, hvoid);
// typedef void *pvoid;
SYM(muvoid);
SYM(pvoid);
b->new_type_void(b, muvoid);
b->new_type_uptr(b, pvoid, muvoid);
// typedef i64 (*write_fp)(i32, pvoid, i64);
SYM(write_sig);
SYM(write_fp);
MuTypeNode write_paramtys[] = { i32, pvoid, i64 };
MuTypeNode write_rettys[] = { i64 };
MuFuncSigNode write_sig = ctx->new_funcsig(ctx, b, write_paramtys, 3,
write_rettys, 1);
MuTypeNode write_fp = ctx->new_type_ufuncptr(ctx, b);
ctx->set_type_ufuncptr(ctx, write_fp, write_sig);
b->new_funcsig(b, write_sig, write_paramtys, 3, write_rettys, 1);
b->new_type_ufuncptr(b, write_fp, write_sig);
MuConstNode const_fp_write = ctx->new_const_int(ctx, b, write_fp, (uintptr_t)write);
// static write_fp const_fp_write = (uintptr_t)write;
SYM(const_fp_write);
b->new_const_int(b, const_fp_write, write_fp, (uintptr_t)write);
// constant for "Hello world!\n"
MuConstNode const_ptr_hw_string = ctx->new_const_int(ctx, b, pi8, (uintptr_t)hw_string);
// static pi8 const_ptr_hw_string = hw_string;
SYM(const_ptr_hw_string);
b->new_const_int(b, const_ptr_hw_string, pi8, (uintptr_t)hw_string);
// the exit state of the Mu function. It cannot directly return to C.
MuGlobalNode exit_status = ctx->new_global_cell(ctx, b, i32);
SYM(exit_status);
b->new_global_cell(b, exit_status, i32);
// the main function
MuFuncNode main_func = ctx->new_func(ctx, b, main_sig);
MuID main_func_id = ctx->get_id(ctx, b, main_func);
SYM(main_func);
b->new_func(b, main_func, main_sig);
MuFuncVerNode main_fv = ctx->new_func_ver(ctx, b, main_func);
// the main function body
MuBBNode entry = ctx->new_bb(ctx, main_fv);
MuNorParamNode hargc = ctx->new_nor_param(ctx, entry, i32);
MuNorParamNode hargv = ctx->new_nor_param(ctx, entry, ppi8);
// void entry(i32 p_argc, ppi8 p_argv) {
SYM(p_argc);
SYM(p_argv);
// args to write. fd=1, sz=size of hw_string
MuConstNode const_i32_1 = ctx->new_const_int(ctx, b, i32, 1L);
MuConstNode const_i64_hwsz = ctx->new_const_int(ctx, b, i64, strlen(hw_string)+1);
// convert the string pointer to void*
MuInstNode ptrcast = ctx->new_conv(ctx, entry, MU_CONV_PTRCAST,
// const i32 const_i32_1 = 1;
// const i64 const_i64_hwsz = strlen(hw_string)+1;
SYM(const_i32_1);
SYM(const_i64_hwsz);
b->new_const_int(b, const_i32_1, i32, 1L);
b->new_const_int(b, const_i64_hwsz, i64, strlen(hw_string)+1);
// var_x = (ppi8)const_ptr_hw_string; // inst_ptrcast
SYM(inst_ptrcast);
SYM(var_x);
b->new_conv(b, inst_ptrcast, var_x, MU_CONV_PTRCAST,
ppi8, pvoid, const_ptr_hw_string);
MuInstResNode ptrcast_r = ctx->get_inst_res(ctx, ptrcast, 0);
// call the write function
MuVarNode write_args[] = { const_i32_1, ptrcast_r, const_i64_hwsz };
MuInstNode write = ctx->new_ccall(ctx, entry, MU_CC_DEFAULT,
write_fp, write_sig, const_fp_write, write_args, 3);
MuInstResNode write_r0 = ctx->get_inst_res(ctx, write, 0);
// return 0
MuConstNode const_i32_0 = ctx->new_const_int(ctx, b, i32, 0L);
MuInstNode store = ctx->new_store(ctx, entry, 0, MU_ORD_NOT_ATOMIC, i32,
exit_status, const_i32_0);
// var_y = write(const_i32_1, var_x, const_i64_hwsz); // inst_write
SYM(var_y);
SYM(inst_write);
MuVarNode write_args[] = { const_i32_1, var_x, const_i64_hwsz };
MuVarNode write_rvs[] = { var_y };
b->new_ccall(b, inst_write, write_rvs, 1, MU_CC_DEFAULT,
write_fp, write_sig, const_fp_write, write_args, 3, MU_NO_ID, MU_NO_ID);
// const i32 const_i32_0 = 0;
// *exit_status = const_i32_0; // inst_store
SYM(const_i32_0);
SYM(inst_store);
b->new_const_int(b, const_i32_0, i32, 0L);
b->new_store(b, inst_store, 0, MU_ORD_NOT_ATOMIC, i32,
exit_status, const_i32_0, MU_NO_ID);
// thread_exit
MuInstNode threadexit = ctx->new_comminst(ctx, entry, MU_CI_UVM_THREAD_EXIT,
NULL, 0, NULL, 0, NULL, 0, NULL, 0);
// uvm.thread_exit(); // inst_threadexit
SYM(inst_threadexit);
b->new_comminst(b, inst_threadexit, NULL, 0, MU_CI_UVM_THREAD_EXIT,
NULL, 0, NULL, 0, NULL, 0, NULL, 0, MU_NO_ID, MU_NO_ID);
// Now construct the entry block
SYM(entry);
MuVarNode entry_args[] = { p_argc, p_argv };
MuTypeNode entry_argtys[] = { i32, ppi8 };
MuInstNode entry_insts[] = { inst_ptrcast, inst_write, inst_store,
inst_threadexit };
b->new_bb(b, entry, entry_args, entry_argtys, 2, MU_NO_ID, entry_insts, 4);
// and the function version (function body)
SYM(main_fv);
MuBBNode main_fv_bbs[] = { entry };
b->new_func_ver(b, main_fv, main_func, main_fv_bbs, 1);
ctx->load_bundle_from_node(ctx, b);
b->load(b);
MUCHECKERR();
MuIntValue v_argc = ctx->handle_from_sint32(ctx, i32_id, argc);
MuUPtrValue v_argv = ctx->handle_from_ptr(ctx, ppi8_id, (MuCPtr)argv);
MuIntValue v_argc = ctx->handle_from_sint32(ctx, i32, argc);
MuUPtrValue v_argv = ctx->handle_from_ptr(ctx, ppi8, (MuCPtr)argv);
MuValue v_main_args[] = {v_argc, v_argv};
MuFuncRefValue v_main_func = ctx->handle_from_func(ctx, main_func_id);
MuFuncRefValue v_main_func = ctx->handle_from_func(ctx, main_func);
MuStackRefValue v_main_stack = ctx->new_stack(ctx, v_main_func);
MuThreadRefValue v_main_thread = ctx->new_thread_nor(ctx, v_main_stack,
NULL, v_main_args, 2);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment