| author | Tom Harley | 2019-10-29 17:14:56 +0000 |
|---|---|---|
| committer | Tom Harley | 2019-10-29 17:14:56 +0000 |
| commit | 42c26919532146b887b134430c4713709b7a4d75 (patch) | |
| tree | df339b5af52f009dfec9b0fb39be0b4fe768f254 | |
| parent | Add struct type definitions (diff) | |
| download | sinter-42c26919532146b887b134430c4713709b7a4d75.tar.gz sinter-42c26919532146b887b134430c4713709b7a4d75.zip | |
Box everything
Everything now gets reduced to an i8*, either through placing on the heap (in the case of primitives) or just through a simple pointer cast.
In the case of primitives, boxing might be avoided by casting the primitive to an i8*, however this would only work for primitives with sizes less than or equal to the size of a pointer on the system.
Boxing increases the generated LLVMIR size dramatically, so it is probably worth looking into ways to reduce the frequency of boxing. One option is to keep track of whether or not a value has been boxed, and only boxing/unboxing it when necessary instead of all the time.
The current implementation uses LLVMBuildMalloc, which allocates memory, but does not ever free this memory. Freeing boxes once its value is extracted may be safe, though I’m not certain. It may be worth looking into LLVM’s garbage collection support at this point.
| -rw-r--r-- | basic.c | 105 | ||||
| -rw-r--r-- | common.c | 5 | ||||
| -rw-r--r-- | simple.int | 6 |
@@ -106,9 +106,25 @@ LLVMValueRef print_str_p; LLVMTypeRef llvm_int_t; LLVMTypeRef llvm_str_t; +LLVMTypeRef boxed_t; scope sc; +LLVMValueRef box_val(LLVMBuilderRef b, LLVMValueRef val, LLVMTypeRef type) { + /* FIXME: memory leak */ + LLVMValueRef box = LLVMBuildMalloc(b, type, "box"); + LLVMBuildStore(b, val, box); + LLVMValueRef cast = LLVMBuildPointerCast(b, box, boxed_t, "cast"); + return cast; +} + +LLVMValueRef unbox_val(LLVMBuilderRef b, LLVMValueRef box, LLVMTypeRef type) { + LLVMTypeRef ptr_type = LLVMPointerType(type, 0); + LLVMValueRef uncast = LLVMBuildPointerCast(b, box, ptr_type, "uncast"); + LLVMValueRef val = LLVMBuildLoad2(b, type, uncast, "unboxed"); + return val; +} + LLVMValueRef codegen_print_int(LLVMValueRef ref) { LLVMValueRef args[] = { ref }; LLVMValueRef ret = @@ -117,11 +133,9 @@ LLVMValueRef codegen_print_int(LLVMValueRef ref) { } LLVMValueRef codegen_int(sexpr *sexpr) { - debug("codegenning %d\n", sexpr->contents.i); - LLVMValueRef ret = LLVMConstInt(llvm_int_t, sexpr->contents.i, 0); - debug("codegenned %d\n", sexpr->contents.i); - - return ret; + LLVMValueRef val = LLVMConstInt(llvm_int_t, sexpr->contents.i, 0); + LLVMValueRef box = box_val(builder, val, llvm_int_t); + return box; } LLVMValueRef codegen_id(char *id) { @@ -133,6 +147,18 @@ LLVMValueRef codegen_id(char *id) { LLVMValueRef _codegen(sexpr *sexpr); +LLVMTypeRef make_function_type(LLVMTypeRef ret_t, LLVMTypeRef *param_t, + int param_c, LLVMBool vararg) { + + /* FIXME: memory leak */ + LLVMTypeRef *boxed_params = malloc((sizeof (LLVMTypeRef)) * param_c); + for (int i = 0; i < param_c; i++) { + boxed_params[i] = boxed_t; + } + + return LLVMFunctionType(boxed_t, boxed_params, param_c, vararg); +} + int get_args_from_list(sexpr *se, LLVMValueRef *args) { debug("getting args from list @%p\n", (void *) se); if (!se) return 0; @@ -240,24 +266,26 @@ LLVMTypeRef codegen_type_definition(sexpr *se) { * Create a temporary builder for building some extra functions so that the * main builder doesn't lose its place. Disposed of at end of scope. */ - LLVMBuilderRef util_builder = LLVMCreateBuilder(); + LLVMBuilderRef util_b = LLVMCreateBuilder(); /* * Make a function for constructing an instance of this struct type. Takes * each of the struct's properties as an individual argument. */ - LLVMTypeRef ctor_t = LLVMFunctionType(struct_ptr_t, prop_t, prop_c, 0); + LLVMTypeRef ctor_t = make_function_type(struct_ptr_t, prop_t, prop_c, 0); LLVMValueRef ctor_p = LLVMAddFunction(module, struct_id, ctor_t); LLVMBasicBlockRef ctor_b = LLVMAppendBasicBlock(ctor_p, "entry"); - LLVMPositionBuilderAtEnd(util_builder, ctor_b); + LLVMPositionBuilderAtEnd(util_b, ctor_b); /* FIXME: memory leak */ - LLVMValueRef src = LLVMBuildMalloc(util_builder, struct_t, "src"); + LLVMValueRef src = LLVMBuildMalloc(util_b, struct_t, "src"); for (int i = 0; i < prop_c; i++) { - LLVMValueRef prop_pos = LLVMBuildStructGEP(util_builder, src, i, "pos"); + LLVMValueRef prop_pos = LLVMBuildStructGEP(util_b, src, i, "pos"); LLVMValueRef param = LLVMGetParam(ctor_p, i); - LLVMBuildStore(util_builder, param, prop_pos); + LLVMValueRef val = unbox_val(util_b, param, prop_t[i]); + LLVMBuildStore(util_b, val, prop_pos); } - LLVMBuildRet(util_builder, src); + LLVMValueRef cast = LLVMBuildPointerCast(util_b, src, boxed_t, "cast"); + LLVMBuildRet(util_b, cast); scope_add_entry(sc, struct_id, ctor_p, ctor_t); /* @@ -270,22 +298,25 @@ LLVMTypeRef codegen_type_definition(sexpr *se) { snprintf(prop_func_id, 50, "%s.%s", struct_id, prop_ids[i]); LLVMTypeRef param_t[] = { struct_ptr_t }; - LLVMTypeRef prop_func_t = LLVMFunctionType(prop_t[i], param_t, 1, 0); + LLVMTypeRef prop_func_t = make_function_type(prop_t[i], param_t, 1, 0); LLVMValueRef prop_func_p = LLVMAddFunction(module, prop_func_id, prop_func_t); LLVMBasicBlockRef prop_func_b = LLVMAppendBasicBlock(prop_func_p, "entry"); - LLVMPositionBuilderAtEnd(util_builder, prop_func_b); + LLVMPositionBuilderAtEnd(util_b, prop_func_b); LLVMValueRef param = LLVMGetParam(prop_func_p, 0); + LLVMValueRef uncast = + LLVMBuildPointerCast(util_b, param, struct_ptr_t, "uncast"); LLVMValueRef ptr = - LLVMBuildStructGEP(util_builder, param, i, "part"); - LLVMValueRef ret = LLVMBuildLoad(util_builder, ptr, "deref"); - LLVMBuildRet(util_builder, ret); + LLVMBuildStructGEP2(util_b, struct_t, uncast, i, "part"); + LLVMValueRef val = LLVMBuildLoad(util_b, ptr, "deref"); + LLVMValueRef box = box_val(util_b, val, prop_t[i]); + LLVMBuildRet(util_b, box); debug("adding %s to scope\n", prop_func_id); scope_add_entry(sc, prop_func_id, prop_func_p, prop_func_t); } - LLVMDisposeBuilder(util_builder); + LLVMDisposeBuilder(util_b); return struct_t; } @@ -318,7 +349,7 @@ LLVMValueRef codegen_definition(sexpr *se) { debug("building function %s\n", func_id); - LLVMTypeRef func_t = LLVMFunctionType(ret_type, param_t, param_c, 0); + LLVMTypeRef func_t = make_function_type(ret_type, param_t, param_c, 0); LLVMValueRef func_p = LLVMAddFunction(module, func_id, func_t); LLVMBasicBlockRef func_b = LLVMAppendBasicBlock(func_p, "entry"); LLVMPositionBuilderAtEnd(builder, func_b); @@ -353,6 +384,23 @@ LLVMValueRef codegen_from_scope(char *func_id, LLVMValueRef *args, int arg_c) { return res; } +/* typedef LLVMValueRef llvm_binop(LLVMBuilderRef, LLVMValueRef, LLVMValueRef, */ +/* const char*); */ + +/* LLVMValueRef codegen_binop(LLVMBuilderRef b, LLVMValueRef x, LLVMValueRef y, */ +/* llvm_binop *op) { */ +/* } */ + +/* TODO: maybe make a function? */ +#define codegen_binop(b, x, y, op) do { \ + assert(arg_c == 2); \ + LLVMValueRef deref_x = unbox_val(b, x, llvm_int_t); \ + LLVMValueRef deref_y = unbox_val(b, y, llvm_int_t); \ + LLVMValueRef op_res = op(b, deref_x, deref_y, "op_res"); \ + LLVMValueRef res_box = box_val(b, op_res, llvm_int_t); \ + res = res_box; \ +} while (0) + LLVMValueRef codegen_inbuilt_functions(sexpr *se, int arg_c, LLVMValueRef *args_v) { @@ -368,14 +416,11 @@ LLVMValueRef codegen_inbuilt_functions(sexpr *se, int arg_c, LLVMValueRef LLVMValueRef res = 0; if (!strcmp(func_id, "+")) { - assert(arg_c == 2); - res = LLVMBuildAdd(builder, args_v[0], args_v[1], "add_out"); + codegen_binop(builder, args_v[0], args_v[1], LLVMBuildAdd); } else if (!strcmp(func_id, "*")) { - assert(arg_c == 2); - res = LLVMBuildMul(builder, args_v[0], args_v[1], "mul_out"); + codegen_binop(builder, args_v[0], args_v[1], LLVMBuildMul); } else if (!strcmp(func_id, "-")) { - assert(arg_c == 2); - res = LLVMBuildSub(builder, args_v[0], args_v[1], "sub_out"); + codegen_binop(builder, args_v[0], args_v[1], LLVMBuildSub); } return res; @@ -443,8 +488,9 @@ LLVMValueRef codegen_conditional(sexpr *se) { LLVMValueRef llvm_zero_v = LLVMConstInt(llvm_int_t, 0, 0); LLVMValueRef cond_res = _codegen(cond_ast); - LLVMValueRef cond_v = - LLVMBuildICmp(builder, LLVMIntNE, cond_res, llvm_zero_v, "neq_0"); + LLVMValueRef unboxed_cond_res = unbox_val(builder, cond_res, llvm_int_t); + LLVMValueRef cond_v = LLVMBuildICmp(builder, LLVMIntNE, unboxed_cond_res, + llvm_zero_v, "neq_0"); LLVMBuildCondBr(builder, cond_v, then_b, else_b); LLVMPositionBuilderAtEnd(builder, then_b); @@ -456,7 +502,7 @@ LLVMValueRef codegen_conditional(sexpr *se) { LLVMBuildBr(builder, done_b); LLVMPositionBuilderAtEnd(builder, done_b); - LLVMValueRef phi_p = LLVMBuildPhi(builder, llvm_int_t, "if_res"); + LLVMValueRef phi_p = LLVMBuildPhi(builder, boxed_t, "if_res"); LLVMBasicBlockRef phi_bs[] = { then_b, else_b }; LLVMValueRef phi_vs[] = { then_v, else_v }; LLVMAddIncoming(phi_p, phi_vs, phi_bs, 2); @@ -519,13 +565,14 @@ void prologue() { llvm_int_t = LLVMInt32Type(); llvm_str_t = LLVMPointerType(LLVMInt8Type(), 0); + boxed_t = LLVMPointerType(LLVMInt8Type(), 0); module = LLVMModuleCreateWithName("main_module"); builder = LLVMCreateBuilder(); /* add print_int */ LLVMTypeRef print_int_params[] = { llvm_int_t }; - print_int_t = LLVMFunctionType(LLVMInt32Type(), print_int_params, 1, 0); + print_int_t = make_function_type(LLVMInt32Type(), print_int_params, 1, 0); print_int_p = LLVMAddFunction(module, "debug_int", print_int_t); scope_add_entry(sc, "debug_int", print_int_p, print_int_t); @@ -1,7 +1,8 @@ #include "debug.h" -int debug_int(int i) { - debug("%d\n", i); +char *debug_int(char *i) { + int *p = (int *) i; + debug("%d\n", *p); return i; } @@ -1,5 +1,7 @@ [type test [(INT x) (INT y)]] -[def (INT testest) [] [test.y [test 3 4]]] +[def (INT testest) [] [test.y [test 3 10]]] [def (INT fact) [(INT n)] [if [- n 1] [* n [fact [- n 1]]] 1]] [def (INT double) [(INT x)] [* 2 x]] -[def (INT main) [] [debug_int [fact [debug_int [testest]]]]] +[def (INT _main) [] [debug_int [fact [debug_int [testest]]]]] +[def (INT ttt) [(INT a) (INT b)] [test b a]] +[def (INT main) [] [debug_int [test.y [ttt 5 10]]]] |
