Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -45,3 +45,5 @@ $(LEX_INTERMEDIATE): $(LEX_SRC) $(YACC_INTERMEDIATE)
$(LEX_TARGET): CFLAGS += -Wno-unused-function

.PHONY: build

main.c: $(YACC_INTERMEDIATE)
2 changes: 1 addition & 1 deletion src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,5 @@ void set_yyin_string(const char *code) {
}

void set_yyin_file(FILE* fp) {
yyin = fp;
yyset_in(fp);
}
36 changes: 13 additions & 23 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
#include <stdio.h>
#include <llvm-c/Analysis.h>
#include <llvm-c/BitWriter.h>
#include <llvm-c/Core.h>
#include "parser.tab.h"
#include "sexpr_pool.h"
#include "utility.h"

extern int yydebug;

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 (LLVMWriteBitcodeToFile(module, "main.bc") != 0) {
fputs("failed to write bitcode to file\n", stderr);
yydebug = 1;
if (1 < argc) {
FILE* fp = fopen(argv[1], "r");
if (fp) {
sexpr_initialize_pool();
parse(fp);
sexpr_finalize_pool();
fclose(fp);
}
}
LLVMDisposeBuilder(builder);
LLVMDisposeModule(module);
return 0;
UNUSED(argc);
UNUSED(argv);
}
27 changes: 26 additions & 1 deletion src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
YYERROR; \
} while (false)

static YYSTYPE g_ast_root;
void yyerror(const char *);
}

%code provides {
int yylex(void);
void set_yyin_file(FILE* fp);
void set_yyin_string(const char *code);
YYSTYPE parse(FILE* fp);
}

%code requires {
Expand Down Expand Up @@ -83,7 +85,7 @@ void set_yyin_string(const char *code);
%token VOLATILE "volatile"
%token WHILE "while"

%start translation-unit
%start entry-point

%%

Expand Down Expand Up @@ -281,8 +283,31 @@ function-definition
: declaration-specifiers function-definition-declarator compound-statement
;

entry-point
: translation-unit {
g_ast_root = $[translation-unit];
}

%%

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;
}