diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 31 | ||||
| -rw-r--r-- | basic.c | 178 | ||||
| -rw-r--r-- | basic.h | 1 | ||||
| -rw-r--r-- | common.c | 9 | ||||
| -rw-r--r-- | debug.h | 6 | ||||
| -rw-r--r-- | error.h | 17 | ||||
| -rw-r--r-- | lex.l | 20 | ||||
| -rw-r--r-- | parse.y | 18 | ||||
| -rw-r--r-- | simple.int | 6 |
10 files changed, 258 insertions, 29 deletions
@@ -7,5 +7,6 @@ parse.c parse.h basic +simple *.sw* @@ -1,15 +1,16 @@ -CC = c++ +CFLAGS = --std=c99 -Wpedantic -Wall -CFLAGS += -x c --std=c99 -Wpedantic -Wall -Werror - -CFLAGS += $(shell llvm-config --cppflags) -LDFLAGS += $(shell llvm-config --ldflags) -LDLIBS += $(shell llvm-config --libs core analysis bitwriter --system-libs) +CFLAGS += $(shell llvm-config --cflags) +CXXFLAGS += $(shell llvm-config --cppflags) +LDFLAGS += $(shell llvm-config --ldflags) +LDLIBS += $(shell llvm-config --libs core analysis --system-libs) .PHONY: default all clean default: all all: basic + basic: basic.o parse.o lex.o + $(LINK.cc) $(OUTPUT_OPTION) $^ $(LDLIBS) basic.o lex.o: parse.h basic.h parse.o: basic.h @@ -18,5 +19,21 @@ parse.h: parse.y $(YACC) -d -o /dev/null $< mv -f y.tab.h $@ +# test stuff (warning: influenced by compiler flags declared above!) + +simple: simple.o common.o + +%.o: %.c + $(COMPILE.c) $(OUTPUT_OPTION) $< + +%.o: %.ll + clang -c $(OUTPUT_OPTION) $< + +%.ll: %.int basic + < $< ./basic > $@ + +%.ll: %.c + $(COMPILE.c) -S -emit-llvm $(OUTPUT_OPTION) $< + clean: - $(RM) basic y.tab.* *.o lex.c parse.c parse.h + $(RM) basic simple y.tab.* *.ll *.o lex.c parse.c parse.h @@ -1,5 +1,7 @@ #include <stdlib.h> #include <stdio.h> +#include <assert.h> +#include <string.h> #include <llvm-c/Core.h> #include <llvm-c/Analysis.h> @@ -7,6 +9,8 @@ #include "basic.h" #include "parse.h" +#include "debug.h" +#include "error.h" int yyparse(); @@ -24,6 +28,12 @@ sexpr *new_int(int num) { return new_sexpr(INT, c); } +sexpr *new_str(char *str) { + contents c; + c.s = str; + return new_sexpr(STRING, c); +} + sexpr *new_float(float num) { contents c; c.f = num; @@ -54,6 +64,9 @@ void _print_sexpr(sexpr *c) { case FLOAT: printf("FLOAT %f", c->contents.f); break; + case STRING: + printf("STRING %s", c->contents.s); + break; case ID: printf("ID %s", c->contents.s); break; @@ -76,28 +89,163 @@ void print_sexpr(sexpr *c) { printf("\n"); } -int module_id = 0; +LLVMModuleRef module; +LLVMBuilderRef builder; + +LLVMTypeRef print_int_t; +LLVMValueRef print_int_p; + +LLVMTypeRef print_str_t; +LLVMValueRef print_str_p; + +LLVMTypeRef llvm_int_t; +LLVMTypeRef llvm_str_t; + +LLVMValueRef codegen_print_int(LLVMValueRef ref) { + LLVMValueRef args[] = { ref }; + LLVMValueRef ret = + LLVMBuildCall2(builder, print_int_t, print_int_p, args, 1, ""); + return ret; +} + +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 codegen_id(sexpr *sexpr) { + /* LLVMValueRef args[] = { */ + /* LLVMBuildGlobalStringPtr(builder, sexpr->contents.s, "") */ + /* }; */ + /* LLVMValueRef ret = */ + /* LLVMBuildCall2(builder, print_str_t, print_str_p, args, 1, ""); */ + + debug("codegenning '%s'\n", sexpr->contents.s); + LLVMValueRef ret = LLVMBuildGlobalStringPtr(builder, sexpr->contents.s, ""); + debug("codegenned '%s'\n", sexpr->contents.s); + + return ret; +} + +LLVMValueRef _codegen(sexpr *sexpr); + +int get_args_from_list(sexpr *se, LLVMValueRef *args_v, LLVMTypeRef *args_t) { + 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); +} + +LLVMValueRef codegen_inbuilt_functions(sexpr *se, int arg_c, LLVMValueRef + *args_v) { + + assert(se); + + sexpr *l = se->contents.n.l; + /* sexpr *r = se->contents.n.r; */ + + assert(l->type == ID); + + char *func_id = l->contents.s; + + LLVMValueRef res = 0; + + if (!strcmp(func_id, "+")) { + assert(arg_c == 2); + res = LLVMBuildAdd(builder, args_v[0], args_v[1], "add_out"); + } else if (!strcmp(func_id, "*")) { + assert(arg_c == 2); + res = LLVMBuildMul(builder, args_v[0], args_v[1], "mul_out"); + } + + 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; + + assert(l->type == ID); + + /* + * 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); + + LLVMValueRef res = codegen_inbuilt_functions(se, arg_c, args_v); + + if (!res) { + error(UNKNOWN_ID, "unrecognised ID %s", l->contents.s); + } + + debug("codegenned branch @%p\n", (void *) se); + return res; +} + +LLVMValueRef _codegen(sexpr *sexpr) { + switch (sexpr->type) { + case NIL: + case INT: + return codegen_int(sexpr); + case ID: + return codegen_id(sexpr); + case BRANCH: + return codegen_branch(sexpr); + default: + printf("erm\n"); + return 0; + } +} void codegen(sexpr *sexpr) { - char module_name[50]; - snprintf(module_name, 50, "module_%d", module_id++); - LLVMModuleRef module = LLVMModuleCreateWithName(module_name); + LLVMValueRef vr = _codegen(sexpr); + codegen_print_int(vr); +} + +void prologue() { + llvm_int_t = LLVMInt32Type(); + llvm_str_t = LLVMPointerType(LLVMInt8Type(), 0); + + module = LLVMModuleCreateWithName("main_module"); + builder = LLVMCreateBuilder(); - LLVMBuilderRef builder = LLVMCreateBuilder(); + /* 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); - LLVMTypeRef func_type = LLVMFunctionType(LLVMInt32Type(), 0, 0, 0); - LLVMValueRef new_func = LLVMAddFunction(module, "main", func_type); + /* 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); - LLVMBasicBlockRef entry = LLVMAppendBasicBlock(new_func, "entry"); + /* 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); - LLVMValueRef x = LLVMConstInt(LLVMInt32Type(), 4, 0); - LLVMValueRef add_output = LLVMBuildAdd(builder, x, x, "add_output"); - LLVMBuildRet(builder, add_output); +} + +void epilogue() { + LLVMValueRef main_ret = LLVMConstInt(llvm_int_t, 0, 0); + LLVMBuildRet(builder, main_ret); - char *err = 0; - LLVMVerifyModule(module, LLVMAbortProcessAction, &err); - LLVMDisposeMessage(err); + LLVMVerifyModule(module, LLVMAbortProcessAction, 0); char *module_status = LLVMPrintModuleToString(module); printf("%s\n", module_status); @@ -108,6 +256,8 @@ void codegen(sexpr *sexpr) { } int main() { + prologue(); yyparse(); + epilogue(); return 0; } @@ -21,6 +21,7 @@ sexpr *new_int(int num); sexpr *new_float(float num); sexpr *new_id(char *id); sexpr *new_node(sexpr *l, sexpr *r); +sexpr *new_str(char *str); void print_sexpr(sexpr *contents); void codegen(sexpr *sexpr); diff --git a/common.c b/common.c new file mode 100644 index 0000000..4d4966a --- /dev/null +++ b/common.c @@ -0,0 +1,9 @@ +#include <stdio.h> + +void print_int(int i) { + printf("%d\n", i); +} + +void print_str(char *s) { + printf("%s\n", s); +} @@ -0,0 +1,6 @@ +#ifndef _DEBUG_H +#define _DEBUG_H + +#define debug(...) fprintf(stderr, __VA_ARGS__) + +#endif /* _DEBUG_H */ @@ -0,0 +1,17 @@ +#ifndef _ERROR_H +#define _ERROR_H + +#include <stdio.h> +#include <stdlib.h> + +enum errcode { + OK = 0, + UNKNOWN_ID +}; + +#define error(v, ...) do { \ + fprintf(stderr, __VA_ARGS__); \ + exit(v); \ +} while (0) + +#endif /* _ERROR_H */ @@ -12,20 +12,34 @@ void yyerror(const char *s, ...); +char *str_value(char *s) { + s++; + /* TODO: strdup allocates memory! */ + char *ret = strdup(s); + ret[strlen(s) - 2] = 0; + + return ret; +} + %} -ID [-A-Za-z_+*!/][-A-Za-z0-9_+*!]* -FLOAT [+-]?([0-9]+[.][0-9]*([Ee][+-]?[0-9]+)?|[.][0-9]+([Ee][+-]?[0-9]+)?) -INT [+-]?[0-9]+ +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]+ %% "(" | ")" | +"[" | +"]" | ";" { return yytext[0]; } "()" { return NIL; } +{STRING} { yylval.s = str_value(yytext); return STRING; } + {ID} { /* TODO: strdup allocates memory! */ yylval.s = strdup(yytext); return ID; } @@ -23,21 +23,29 @@ void yyerror(char *s, ...); %token <f> FLOAT %token <i> INT %token <s> ID +%token <s> STRING %token BRANCH %type <u> sexpr +%type <u> lexpr %% cmds: - | cmds sexpr { print_sexpr($2); } + | cmds sexpr { codegen($2); } sexpr: '(' sexpr ';' sexpr ')' { $$ = new_node($2, $4); } | '(' sexpr sexpr ')' { $$ = new_node($2, $3); } - | INT { $$ = new_int($1); } - | FLOAT { $$ = new_float($1); } - | ID { $$ = new_id($1); } - | NIL { $$ = 0; } + | '[' lexpr { $$ = $2; } + | INT { $$ = new_int($1); } + | FLOAT { $$ = new_float($1); } + | ID { $$ = new_id($1); } + | NIL { $$ = 0; } + | STRING { $$ = new_str($1); } + +lexpr: ']' { $$ = 0; } + | sexpr ';' lexpr { $$ = new_node($1, $3); } + | sexpr lexpr { $$ = new_node($1, $2); } %% diff --git a/simple.int b/simple.int new file mode 100644 index 0000000..a58173f --- /dev/null +++ b/simple.int @@ -0,0 +1,6 @@ +1 +2 +3 +(+ [3 4]) +(+ [(+ [10 20]) (+ [1 2])]) +(+ [(* [10 20]) (+ [1 2])]) |
