From 647e8039ae08479a1f7a7d80fc96170bfbb7efc2 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Thu, 15 Sep 2016 17:59:49 +0900 Subject: [PATCH 1/7] More portable set_yyin_file --- src/lexer.l | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lexer.l b/src/lexer.l index 5813707d..dbd8a5e1 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -156,5 +156,5 @@ void set_yyin_string(const char *code) { } void set_yyin_file(FILE* fp) { - yyin = fp; + yyset_in(fp); } From 7086ec324699b86446c8b5e99a8690d045bec112 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Thu, 30 Jun 2016 10:37:01 +0900 Subject: [PATCH 2/7] Add global pointer to ast root --- src/parser.y | 1 + 1 file changed, 1 insertion(+) diff --git a/src/parser.y b/src/parser.y index 52aae5b1..e685bcce 100644 --- a/src/parser.y +++ b/src/parser.y @@ -7,6 +7,7 @@ YYERROR; \ } while (false) +static YYSTYPE g_ast_root; void yyerror(const char *); } From 2cda12a660b06019d4cc7623b0226d9e8fcc615c Mon Sep 17 00:00:00 2001 From: hatsusato Date: Thu, 30 Jun 2016 10:37:37 +0900 Subject: [PATCH 3/7] Add entry-point and its action --- src/parser.y | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/parser.y b/src/parser.y index e685bcce..d0025ac6 100644 --- a/src/parser.y +++ b/src/parser.y @@ -84,7 +84,7 @@ void set_yyin_string(const char *code); %token VOLATILE "volatile" %token WHILE "while" -%start translation-unit +%start entry-point %% @@ -282,6 +282,11 @@ function-definition : declaration-specifiers function-definition-declarator compound-statement ; +entry-point +: translation-unit { + g_ast_root = $[translation-unit]; +} + %% void yyerror(const char* s) { From e47ff36fc83c3b7eb43ac0a6eced31213a446b7a Mon Sep 17 00:00:00 2001 From: hatsusato Date: Thu, 30 Jun 2016 10:38:32 +0900 Subject: [PATCH 4/7] Implement parse --- src/parser.y | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/parser.y b/src/parser.y index d0025ac6..7ffcaf0e 100644 --- a/src/parser.y +++ b/src/parser.y @@ -15,6 +15,7 @@ void yyerror(const char *); int yylex(void); void set_yyin_file(FILE* fp); void set_yyin_string(const char *code); +YYSTYPE parse(FILE* fp); } %code requires { @@ -292,3 +293,21 @@ entry-point void yyerror(const char* s) { fprintf(stderr, "%s\n", s); } + +YYSTYPE parse(FILE* fp) { + set_yyin_file(fp); + switch (yyparse()) { + case 0: + return g_ast_root; + case 1: + yyerror("invalid input in yyparse"); + break; + case 2: + yyerror("memory exhaustion in yyparse"); + break; + default: + yyerror("unknown error in yyparse"); + break; + } + return NULL; +} From 81121e183c4df7ff3842cd1f2b4bdb05bba46b21 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Thu, 30 Jun 2016 18:09:45 +0900 Subject: [PATCH 5/7] Use parse --- src/main.c | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/main.c b/src/main.c index d5862c48..bd6c59fd 100644 --- a/src/main.c +++ b/src/main.c @@ -1,30 +1,17 @@ #include -#include -#include -#include +#include "parser.tab.h" +#include "sexpr_pool.h" #include "utility.h" int main(int argc, char *argv[]) { - LLVMModuleRef module = LLVMModuleCreateWithName("kmc90_module"); - /* int main() { return 0; } */ - LLVMTypeRef main_type = LLVMFunctionType(LLVMInt32Type(), NULL, 0, false); - LLVMValueRef main_func = LLVMAddFunction(module, "main", main_type); - LLVMBasicBlockRef main_entry = LLVMAppendBasicBlock(main_func, "main_entry"); - LLVMValueRef return_value = LLVMConstInt(LLVMInt32Type(), 0, false); - LLVMBuilderRef builder = LLVMCreateBuilder(); - LLVMPositionBuilderAtEnd(builder, main_entry); - LLVMBuildRet(builder, return_value); - { - char *error = NULL; - LLVMVerifyModule(module, LLVMAbortProcessAction, &error); - LLVMDisposeMessage(error); + if (1 < argc) { + FILE* fp = fopen(argv[1], "r"); + if (fp) { + sexpr_initialize_pool(); + parse(fp); + sexpr_finalize_pool(); + fclose(fp); + } } - if (LLVMWriteBitcodeToFile(module, "main.bc") != 0) { - fputs("failed to write bitcode to file\n", stderr); - } - LLVMDisposeBuilder(builder); - LLVMDisposeModule(module); return 0; - UNUSED(argc); - UNUSED(argv); } From 3a9b7dcb3ecf0d3c5f829f0525a2362bef8b6227 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Thu, 15 Sep 2016 20:44:43 +0900 Subject: [PATCH 6/7] Enable yacc debug --- src/Makefile | 2 +- src/main.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index 512ae58c..ef31e0e4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -24,7 +24,7 @@ YACC_TARGET := parser.tab.o LEX := flex LEX_OPTION := YACC := bison -YACC_OPTION := -d +YACC_OPTION := --debug -d TARGET := kmc90 OBJS += $(AST_OBJS) OBJS += $(CODEGEN_OBJS) diff --git a/src/main.c b/src/main.c index bd6c59fd..f94434fa 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,10 @@ #include "sexpr_pool.h" #include "utility.h" +extern int yydebug; + int main(int argc, char *argv[]) { + yydebug = 1; if (1 < argc) { FILE* fp = fopen(argv[1], "r"); if (fp) { From 18cf4b2b43efa29a4057609b761edff871dbfc40 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Thu, 30 Jun 2016 18:10:40 +0900 Subject: [PATCH 7/7] Set dependency about main --- src/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Makefile b/src/Makefile index ef31e0e4..e13e0aa4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -45,3 +45,5 @@ $(LEX_INTERMEDIATE): $(LEX_SRC) $(YACC_INTERMEDIATE) $(LEX_TARGET): CFLAGS += -Wno-unused-function .PHONY: build + +main.c: $(YACC_INTERMEDIATE)