From 85093de7415e9410dc5917f8ef6ac489b8198b42 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Mon, 28 Oct 2019 22:25:54 +0000 Subject: Add struct type definitions Struct-like types can now be created using the `type` builtin. This creates a constructor function as well as a function for accessing each of the struct's properties. LLVM's type checking makes using these structs a little awkward: they currently cannot be returned by any functions (except the constructor). The aim is to box everything, so that functions can return values they don't know much about. --- basic.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lex.l | 4 +-- scope.c | 6 ++-- simple.int | 6 ++-- 4 files changed, 118 insertions(+), 6 deletions(-) diff --git a/basic.c b/basic.c index 29ce672..1228fbb 100644 --- a/basic.c +++ b/basic.c @@ -184,6 +184,112 @@ int make_func_params(sexpr *params, char **ids, LLVMTypeRef *types) { return 1 + make_func_params(r, ids + 1, types + 1); } +LLVMTypeRef codegen_type_definition(sexpr *se) { + assert(se); + assert(se->type == BRANCH); + + sexpr *title = se->contents.n.l; + sexpr *info = se->contents.n.r; + + assert(title); + assert(info); + + /* + * TODO + * Decide how to do struct declarations. + * Would be nice to allow named and unnamed struct properties. + * + * What should accessing a struct's preoperties look like? + * + * Should it look like Haskell's property accessing? If it did, the struct + * type would also need to be mentioned, kinda like: + * ` ` + * Would require doing a special thing to get the property index from its + * name. + * + * Similar option is something like: + * `. ` + * Where a function is defined for each struct-property combo. + * Think I'll go with this option for now. + */ + + /* FIXME: magic sizes */ + char *prop_ids[50]; + LLVMTypeRef prop_t[50]; + int prop_c = make_func_params(info->contents.n.l, prop_ids, prop_t); + + char *struct_id = title->contents.s; + + debug("building struct %s\n", struct_id); + + /* last parameter determines if struct is packed */ + LLVMTypeRef struct_t = LLVMStructType(prop_t, prop_c, 0); + + /* + * TODO + * I'm not sure what the second parameter of LLVMPointerType (unsigned + * AddressSpace) is used for: + * https://llvm.org/doxygen/group__LLVMCCoreTypeSequential.html#ga299fe6147083678d0494b1b875f542fa + * I'm guessing it's for restricting the address space the pointer can + * point to, which would be a cool property to expose to front-end + * languages. + */ + LLVMTypeRef struct_ptr_t = LLVMPointerType(struct_t, 0); + + /* + * 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(); + + /* + * 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); + LLVMValueRef ctor_p = LLVMAddFunction(module, struct_id, ctor_t); + LLVMBasicBlockRef ctor_b = LLVMAppendBasicBlock(ctor_p, "entry"); + LLVMPositionBuilderAtEnd(util_builder, ctor_b); + /* FIXME: memory leak */ + LLVMValueRef src = LLVMBuildMalloc(util_builder, struct_t, "src"); + for (int i = 0; i < prop_c; i++) { + LLVMValueRef prop_pos = LLVMBuildStructGEP(util_builder, src, i, "pos"); + LLVMValueRef param = LLVMGetParam(ctor_p, i); + LLVMBuildStore(util_builder, param, prop_pos); + } + LLVMBuildRet(util_builder, src); + scope_add_entry(sc, struct_id, ctor_p, ctor_t); + + /* + * Make functions to retreive each property of the struct. + */ + /* FIXME: magic size */ + char prop_func_id[50]; + for (int i = 0; i < prop_c; i++) { + /* TODO: maybe choose a different separator character? */ + 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); + 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); + LLVMValueRef param = LLVMGetParam(prop_func_p, 0); + LLVMValueRef ptr = + LLVMBuildStructGEP(util_builder, param, i, "part"); + LLVMValueRef ret = LLVMBuildLoad(util_builder, ptr, "deref"); + LLVMBuildRet(util_builder, ret); + + 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); + + return struct_t; +} + LLVMValueRef codegen_definition(sexpr *se) { assert(se); assert(se->type == BRANCH); @@ -371,6 +477,8 @@ LLVMValueRef codegen_branch(sexpr *se) { if (l->type == ID) { if (!strcmp(l->contents.s, "def")) { res = codegen_definition(r); + } else if (!strcmp(l->contents.s, "type")) { + codegen_type_definition(r); } else if (!strcmp(l->contents.s, "if")) { res = codegen_conditional(r); } else { diff --git a/lex.l b/lex.l index 432ad53..62080b3 100644 --- a/lex.l +++ b/lex.l @@ -23,8 +23,8 @@ char *str_value(char *s) { %} -ID [-A-Za-z_+*!/][-A-Za-z0-9_+*!]* -STRING ["][^"]*["] +ID [-A-Za-z_+*!/.][-A-Za-z0-9_+*!.]* +STRING ["][^"]*["]|['][^']*['] FLOAT [+-]?([0-9]+[.][0-9]*([Ee][+-]?[0-9]+)?|[.][0-9]+([Ee][+-]?[0-9]+)?) INT [+-]?[0-9]+ diff --git a/scope.c b/scope.c index 249439d..7936f55 100644 --- a/scope.c +++ b/scope.c @@ -13,8 +13,10 @@ void free_layer(scope_layer *layer) { for (scope_entry *entry = layer->head; entry;) { + scope_entry *x = entry; entry = entry->prev; - free(entry); + free(x->id); + free(x); } free(layer); @@ -40,7 +42,7 @@ void scope_add_entry(scope sc, char *id, LLVMValueRef val, LLVMTypeRef type) { scope_entry *new_entry = malloc(sizeof (*new_entry)); if (!new_entry) error(GENERAL_ERROR, "cannot malloc?"); - new_entry->id = id; + new_entry->id = strdup(id); new_entry->value = val; new_entry->type = type; diff --git a/simple.int b/simple.int index fe34eab..d06bbcc 100644 --- a/simple.int +++ b/simple.int @@ -1,3 +1,5 @@ -[def (INT fact) [(INT n)] [if n [* n [fact [- n 1]]] 1]] +[type test [(INT x) (INT y)]] +[def (INT testest) [] [test.y [test 3 4]]] +[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 10]]] +[def (INT main) [] [debug_int [fact [debug_int [testest]]]]] -- cgit v1.2.1