diff options
| -rw-r--r-- | Makefile | 14 | ||||
| -rw-r--r-- | basic.c | 251 | ||||
| -rw-r--r-- | basic.h | 4 | ||||
| -rw-r--r-- | common.c | 16 | ||||
| -rw-r--r-- | debug.h | 2 | ||||
| -rw-r--r-- | error.h | 7 | ||||
| -rw-r--r-- | lex.l | 4 | ||||
| -rw-r--r-- | parse.y | 2 | ||||
| -rw-r--r-- | scope.c | 62 | ||||
| -rw-r--r-- | scope.h | 26 | ||||
| -rw-r--r-- | simple.int | 8 |
11 files changed, 317 insertions, 79 deletions
@@ -9,17 +9,21 @@ LDLIBS += $(shell llvm-config --libs core analysis --system-libs) default: all all: basic -basic: basic.o parse.o lex.o - $(LINK.cc) $(OUTPUT_OPTION) $^ $(LDLIBS) - -basic.o lex.o: parse.h basic.h +basic.o: basic.h parse.h debug.h error.h scope.h parse.o: basic.h +lex.o: basic.h parse.h +scope.o: scope.h error.h + +basic: basic.o parse.o lex.o scope.o + $(LINK.cc) $(OUTPUT_OPTION) $^ $(LDLIBS) parse.h: parse.y $(YACC) -d -o /dev/null $< mv -f y.tab.h $@ # test stuff (warning: influenced by compiler flags declared above!) +# TODO should probably rearrange the system so tests don't use the same build +# configs as the compiler simple: simple.o common.o @@ -33,7 +37,7 @@ simple: simple.o common.o < $< ./basic > $@ %.ll: %.c - $(COMPILE.c) -S -emit-llvm $(OUTPUT_OPTION) $< + clang -S -emit-llvm $(OUTPUT_OPTION) $< clean: $(RM) basic simple y.tab.* *.ll *.o lex.c parse.c parse.h @@ -11,11 +11,12 @@ #include "parse.h" #include "debug.h" #include "error.h" +#include "scope.h" int yyparse(); sexpr *new_sexpr(int type, contents val) { - /* TODO: unchecked memory allocation! */ + /* FIXME: unchecked memory allocation! */ sexpr *ret = malloc(sizeof (*ret)); ret->type = type; ret->contents = val; @@ -53,40 +54,44 @@ sexpr *new_node(sexpr *l, sexpr *r) { return new_sexpr(BRANCH, c); } -void _print_sexpr(sexpr *c) { +void _fprint_sexpr(FILE *fd, sexpr *c) { if (!c) { - printf("NIL"); + fprintf(fd, "NIL"); } else { switch (c->type) { case INT: - printf("INT %i", c->contents.i); + fprintf(fd, "INT %i", c->contents.i); break; case FLOAT: - printf("FLOAT %f", c->contents.f); + fprintf(fd, "FLOAT %f", c->contents.f); break; case STRING: - printf("STRING %s", c->contents.s); + fprintf(fd, "STRING %s", c->contents.s); break; case ID: - printf("ID %s", c->contents.s); + fprintf(fd, "ID %s", c->contents.s); break; case BRANCH: - printf("("); - _print_sexpr(c->contents.n.l); - printf(";"); - _print_sexpr(c->contents.n.r); - printf(")"); + fprintf(fd, "("); + _fprint_sexpr(fd, c->contents.n.l); + fprintf(fd, ";"); + _fprint_sexpr(fd, c->contents.n.r); + fprintf(fd, ")"); break; case NIL: default: - printf("ERROR"); + fprintf(fd, "ERROR"); } } } -void print_sexpr(sexpr *c) { - _print_sexpr(c); - printf("\n"); +void fprint_sexpr(FILE *fd, sexpr *c) { + _fprint_sexpr(fd, c); + fprintf(fd, "\n"); +} + +void print_sexpr(sexpr *se) { + fprint_sexpr(stdin, se); } LLVMModuleRef module; @@ -101,6 +106,8 @@ LLVMValueRef print_str_p; LLVMTypeRef llvm_int_t; LLVMTypeRef llvm_str_t; +scope sc; + LLVMValueRef codegen_print_int(LLVMValueRef ref) { LLVMValueRef args[] = { ref }; LLVMValueRef ret = @@ -116,29 +123,125 @@ LLVMValueRef codegen_int(sexpr *sexpr) { return ret; } -LLVMValueRef codegen_id(sexpr *sexpr) { - /* LLVMValueRef args[] = { */ - /* LLVMBuildGlobalStringPtr(builder, sexpr->contents.s, "") */ - /* }; */ - /* LLVMValueRef ret = */ - /* LLVMBuildCall2(builder, print_str_t, print_str_p, args, 1, ""); */ +LLVMValueRef codegen_id(char *id) { + scope_entry *entry = scope_find(sc, id); + if (!entry) return 0; - debug("codegenning '%s'\n", sexpr->contents.s); - LLVMValueRef ret = LLVMBuildGlobalStringPtr(builder, sexpr->contents.s, ""); - debug("codegenned '%s'\n", sexpr->contents.s); - - return ret; + return entry->value; } LLVMValueRef _codegen(sexpr *sexpr); -int get_args_from_list(sexpr *se, LLVMValueRef *args_v, LLVMTypeRef *args_t) { +int get_args_from_list(sexpr *se, LLVMValueRef *args) { debug("getting args from list @%p\n", (void *) se); if (!se) return 0; assert(se->type == BRANCH); - *args_v = _codegen(se->contents.n.l); - return 1 + get_args_from_list(se->contents.n.r, args_v + 1, args_t + 1); + *args = _codegen(se->contents.n.l); + return 1 + get_args_from_list(se->contents.n.r, args + 1); +} + +void get_param_info(sexpr* param_def, char **id, LLVMTypeRef *type) { + assert(param_def); + assert(param_def->type == BRANCH); + + sexpr *var_type_node = param_def->contents.n.l; + sexpr *var_name_node = param_def->contents.n.r; + + assert(var_type_node); + assert(var_type_node->type == ID); + assert(var_name_node); + assert(var_name_node->type == ID); + + char *var_type_str = var_type_node->contents.s; + assert(var_type_str); + + *id = var_name_node->contents.s; + + if (!strcmp(var_type_str, "INT")) { + *type = llvm_int_t; + } else if (!strcmp(var_type_str, "STR")) { + *type = llvm_str_t; + } else { + error(UNKNOWN_ID, "Unknown parameter type %s", var_type_str); + } +} + +int make_func_params(sexpr *params, char **ids, LLVMTypeRef *types) { + if (!params) return 0; + + assert(params->type == BRANCH); + + sexpr *l = params->contents.n.l; + sexpr *r = params->contents.n.r; + + assert(l); + assert(l->type == BRANCH); + + get_param_info(l, ids, types); + + return 1 + make_func_params(r, ids + 1, types + 1); +} + +LLVMValueRef codegen_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 I'm finding it really hard to figure out how I should do this, I + // want to add this function's argument names to the scope table, but also I + // need the function's types first + // The function's arg types will be declared with the args themselves, like + // [(int x) (str y) (int z)] + // For now don't make a general thing, just get definitions to work + + char *func_id; + LLVMTypeRef ret_type; + get_param_info(title, &func_id, &ret_type); + + /* FIXME: magic sizes */ + char *ids[50]; + LLVMTypeRef param_t[50]; + int param_c = make_func_params(info->contents.n.l, ids, param_t); + + debug("building function %s\n", func_id); + + LLVMTypeRef func_t = LLVMFunctionType(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); + + scope_add_entry(sc, func_id, func_p, func_t); + + scope_push_layer(&sc); + for (int i = 0; i < param_c; i++) { + scope_add_entry(sc, ids[i], LLVMGetParam(func_p, i), param_t[i]); + } + + assert(!info->contents.n.r->contents.n.r); + LLVMValueRef ret = _codegen(info->contents.n.r->contents.n.l); + LLVMBuildRet(builder, ret); + + scope_pop_layer(&sc); + + debug("built function %s\n", func_id); + + return func_p; +} + +LLVMValueRef codegen_from_scope(char *func_id, LLVMValueRef *args, int arg_c) { + scope_entry *entry = scope_find(sc, func_id); + if (!entry) return 0; + + LLVMValueRef res = + LLVMBuildCall2(builder, entry->type, entry->value, args, arg_c, ""); + + return res; } LLVMValueRef codegen_inbuilt_functions(sexpr *se, int arg_c, LLVMValueRef @@ -161,14 +264,21 @@ LLVMValueRef codegen_inbuilt_functions(sexpr *se, int arg_c, LLVMValueRef } else if (!strcmp(func_id, "*")) { assert(arg_c == 2); res = LLVMBuildMul(builder, args_v[0], args_v[1], "mul_out"); + } else if (!strcmp(func_id, "-")) { + assert(arg_c == 2); + res = LLVMBuildSub(builder, args_v[0], args_v[1], "sub_out"); } return res; } -LLVMValueRef codegen_branch(sexpr *se) { - debug("codegenning branch @%p\n", (void *) se); +/* + * To implement lazy evaluation, I could implement thunks + * Closures would require using a struct to keep track of variable's values from + * when the closure was created + */ +LLVMValueRef codegen_invocation(sexpr *se) { assert(se); sexpr *l = se->contents.n.l; @@ -176,19 +286,45 @@ LLVMValueRef codegen_branch(sexpr *se) { assert(l->type == ID); + char *func_id = l->contents.s; + /* * args_t may not be needed as we can just get the types of values from the * function declaration. Who knows, really. */ - // TODO: magic values - LLVMValueRef args_v[50]; - LLVMTypeRef args_t[50]; - int arg_c = get_args_from_list(r, args_v, args_t); + /* FIXME: magic values */ + LLVMValueRef args[50]; + int arg_c = get_args_from_list(r, args); + + LLVMValueRef res = codegen_from_scope(func_id, args, arg_c); + if (!res) res = codegen_inbuilt_functions(se, arg_c, args); - LLVMValueRef res = codegen_inbuilt_functions(se, arg_c, args_v); + if (!res) error(UNKNOWN_ID, "unrecognised ID %s", func_id); + + + return res; +} + +LLVMValueRef codegen_branch(sexpr *se) { + debug("codegenning branch @%p\n", (void *) se); + + assert(se); + + sexpr *l = se->contents.n.l; + sexpr *r = se->contents.n.r; + + LLVMValueRef res; + + if (l->type == ID) { + if (!strcmp(l->contents.s, "def")) { + res = codegen_definition(r); + } else { + res = codegen_invocation(se); + } + } else { + fprint_sexpr(stderr, se); + error(SYNTAX_ERROR, "don't know what to do with above AST"); - if (!res) { - error(UNKNOWN_ID, "unrecognised ID %s", l->contents.s); } debug("codegenned branch @%p\n", (void *) se); @@ -201,7 +337,7 @@ LLVMValueRef _codegen(sexpr *sexpr) { case INT: return codegen_int(sexpr); case ID: - return codegen_id(sexpr); + return codegen_id(sexpr->contents.s); case BRANCH: return codegen_branch(sexpr); default: @@ -211,11 +347,13 @@ LLVMValueRef _codegen(sexpr *sexpr) { } void codegen(sexpr *sexpr) { - LLVMValueRef vr = _codegen(sexpr); - codegen_print_int(vr); + _codegen(sexpr); } void prologue() { + sc = 0; + scope_push_layer(&sc); + llvm_int_t = LLVMInt32Type(); llvm_str_t = LLVMPointerType(LLVMInt8Type(), 0); @@ -224,28 +362,25 @@ void prologue() { /* add print_int */ LLVMTypeRef print_int_params[] = { llvm_int_t }; - print_int_t = LLVMFunctionType(LLVMVoidType(), print_int_params, 1, 0); - print_int_p = LLVMAddFunction(module, "print_int", print_int_t); + print_int_t = LLVMFunctionType(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); /* add print_str */ LLVMTypeRef print_str_params[] = { llvm_str_t }; - print_str_t = LLVMFunctionType(LLVMVoidType(), print_str_params, 1, 0); - print_str_p = LLVMAddFunction(module, "print_str", print_str_t); - - /* add main */ - LLVMTypeRef main_type = LLVMFunctionType(llvm_int_t, 0, 0, 0); - LLVMValueRef main_prototype = LLVMAddFunction(module, "main", main_type); - - LLVMBasicBlockRef entry = LLVMAppendBasicBlock(main_prototype, "entry"); - - LLVMPositionBuilderAtEnd(builder, entry); + print_str_t = LLVMFunctionType(LLVMInt32Type(), print_str_params, 1, 0); + print_str_p = LLVMAddFunction(module, "debug_str", print_str_t); + scope_add_entry(sc, "debug_str", print_str_p, print_str_t); + + /* add int_if */ + LLVMTypeRef int_if_params[] = { llvm_int_t, llvm_int_t, llvm_int_t }; + LLVMTypeRef int_if_t = LLVMFunctionType(llvm_int_t, int_if_params, 3, 0); + LLVMValueRef int_if_p = LLVMAddFunction(module, "int_if", int_if_t); + scope_add_entry(sc, "int_if", int_if_p, int_if_t); } void epilogue() { - LLVMValueRef main_ret = LLVMConstInt(llvm_int_t, 0, 0); - LLVMBuildRet(builder, main_ret); - - LLVMVerifyModule(module, LLVMAbortProcessAction, 0); + /* LLVMVerifyModule(module, LLVMAbortProcessAction, 0); */ char *module_status = LLVMPrintModuleToString(module); printf("%s\n", module_status); @@ -23,7 +23,9 @@ sexpr *new_id(char *id); sexpr *new_node(sexpr *l, sexpr *r); sexpr *new_str(char *str); -void print_sexpr(sexpr *contents); +void print_sexpr(sexpr *se); +void fprint_sexpr(FILE *f, sexpr *se); + void codegen(sexpr *sexpr); #endif /* _BASIC_H */ @@ -1,9 +1,15 @@ -#include <stdio.h> +#include "debug.h" -void print_int(int i) { - printf("%d\n", i); +int debug_int(int i) { + debug("%d\n", i); + return i; } -void print_str(char *s) { - printf("%s\n", s); +char *debug_str(char *s) { + debug("%s\n", s); + return s; +} + +int int_if(int p, int a, int b) { + return p ? a : b; } @@ -1,6 +1,8 @@ #ifndef _DEBUG_H #define _DEBUG_H +#include <stdio.h> + #define debug(...) fprintf(stderr, __VA_ARGS__) #endif /* _DEBUG_H */ @@ -6,11 +6,16 @@ enum errcode { OK = 0, - UNKNOWN_ID + GENERAL_ERROR = 1, + COMMAND_NOT_FOUND = 2, + UNKNOWN_ID, + SYNTAX_ERROR }; +/* FIXME: double fprintf is ugly */ #define error(v, ...) do { \ fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, "\n"); \ exit(v); \ } while (0) @@ -14,7 +14,7 @@ void yyerror(const char *s, ...); char *str_value(char *s) { s++; - /* TODO: strdup allocates memory! */ + /* FIXME: strdup allocates memory! */ char *ret = strdup(s); ret[strlen(s) - 2] = 0; @@ -40,7 +40,7 @@ INT [+-]?[0-9]+ {STRING} { yylval.s = str_value(yytext); return STRING; } -{ID} { /* TODO: strdup allocates memory! */ +{ID} { /* FIXME: strdup allocates memory! */ yylval.s = strdup(yytext); return ID; } {INT} { yylval.i = atoi(yytext); return INT; } @@ -32,7 +32,7 @@ void yyerror(char *s, ...); %% cmds: - | cmds sexpr { codegen($2); } + | cmds sexpr { fprint_sexpr(stderr, $2); codegen($2); } sexpr: '(' sexpr ';' sexpr ')' { $$ = new_node($2, $4); } | '(' sexpr sexpr ')' { $$ = new_node($2, $3); } @@ -0,0 +1,62 @@ +#include <stdlib.h> +#include <string.h> + +#include "scope.h" +#include "error.h" + +/* + * Guess we'll use a linked list of linked lists for now, though that certainly + * isn't the best option and should at some point be replaced by a hash table or + * something. + * TODO + */ + +void free_layer(scope_layer *layer) { + for (scope_entry *entry = layer->head; entry;) { + entry = entry->prev; + free(entry); + } + + free(layer); +} + +void scope_push_layer(scope *sc) { + scope_layer *new_layer = malloc(sizeof (*new_layer)); + if (!new_layer) error(GENERAL_ERROR, "cannot malloc?"); + + new_layer->prev = *sc; + new_layer->head = 0; + + *sc = new_layer; +} + +void scope_pop_layer(scope *sc) { + scope new_sc = (**sc).prev; + free_layer(*sc); + *sc = new_sc; +} + +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->value = val; + new_entry->type = type; + + scope_layer *layer = sc; + new_entry->prev = layer->head; + layer->head = new_entry; +} + +scope_entry *scope_find(scope sc, char *id) { + for (scope_layer *layer = sc; layer; layer = layer->prev) { + for (scope_entry *entry = layer->head; entry; entry = entry->prev) { + if (!strcmp(entry->id, id)) { + return entry; + } + } + } + + return 0; +} @@ -0,0 +1,26 @@ +#ifndef _SCOPE_H +#define _SCOPE_H + +#include <llvm-c/Core.h> + +typedef struct scope_entry { + char *id; + LLVMValueRef value; + LLVMTypeRef type; + struct scope_entry *prev; +} scope_entry; + +typedef struct scope_layer { + struct scope_entry *head; + struct scope_layer *prev; +} scope_layer; + +typedef scope_layer *scope; + +void scope_push_layer(scope *sc); +void scope_pop_layer(scope *sc); + +void scope_add_entry(scope sc, char *id, LLVMValueRef val, LLVMTypeRef type); +scope_entry *scope_find(scope sc, char *id); + +#endif /* _SCOPE_H */ @@ -1,6 +1,2 @@ -1 -2 -3 -(+ [3 4]) -(+ [(+ [10 20]) (+ [1 2])]) -(+ [(* [10 20]) (+ [1 2])]) +[def (INT double) [(INT x)] [* 2 x]] +[def (INT main) [] [debug_int [+ 3 [double 4]]]] |
