From 80d5e9144815ac5b4e741109fdcb8c6e4d933499 Mon Sep 17 00:00:00 2001 From: hyl <563807243@qq.com> Date: Thu, 22 Jul 2021 22:25:31 +0800 Subject: [PATCH 1/3] code gen init --- Makefile | 2 +- src/codegen.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/summoner.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 src/codegen.c diff --git a/Makefile b/Makefile index 1c70498..ab12081 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ comp: lex src/lex.l yacc -d src/grammar.y - gcc -o bin/main y.tab.c lex.yy.c src/create.c src/interpreter.c src/compiler.c main/main.c + gcc -o bin/main y.tab.c lex.yy.c src/create.c src/interpreter.c src/compiler.c src/codegen.c main/main.c diff --git a/src/codegen.c b/src/codegen.c new file mode 100644 index 0000000..8458cb0 --- /dev/null +++ b/src/codegen.c @@ -0,0 +1,59 @@ +#include "summoner.h" +#include + +static SVM_Executable * +alloc_executable() +{ + SVM_Executable *exe; + + exe = malloc(sizeof(SVM_Executable)); + + exe->constant_pool_count = 0; + exe->constant_pool = NULL; + exe->global_variable_count = 0; + exe->global_variable = NULL; + exe->constant_count = 0; + exe->constant_definition = NULL; + exe->type_specifier_count = 0; + exe->type_specifier = NULL; + + return exe; +} + +static void +add_global_variable(Compiler *compiler, SVM_Executable *exe) +{ +} + +static void +add_functions(Compiler *compiler, SVM_Executable *exe) +{ +} + +static void +add_top_level(Compiler *compiler, SVM_Executable *exe) +{ +} +static void +generate_constant_initializer(Compiler *compiler, SVM_Executable *exe) +{ +} + +SVM_Executable * +smc_code_gen(Compiler *compiler) +{ + SVM_Executable *exe; + + exe = alloc_executable(); + + exe->constant_count = compiler->svm_constant_count; + exe->constant_definition = compiler->svm_constant; + + add_global_variable(compiler, exe); + add_functions(compiler, exe); + add_top_level(compiler, exe); + + generate_constant_initializer(compiler, exe); + + return exe; +} diff --git a/src/summoner.h b/src/summoner.h index bcc267a..77fcff3 100644 --- a/src/summoner.h +++ b/src/summoner.h @@ -2,6 +2,7 @@ #define _SUMMONER_H_ #include +#include typedef enum { @@ -170,12 +171,68 @@ Block *alloc_block(StatementList *list); TypeSpecifier *alloc_type_specifier(BasicType type, char *identifier); +typedef wchar_t SVM_Char; +typedef unsigned char SVM_Byte; + +typedef enum { + SVM_FALSE = 0, + SVM_TRUE = 1 +} SVM_Boolean; + +typedef struct { + TypeSpecifier *type; + char *package_name; + char *name; + SVM_Boolean is_defined; +} SVM_Constant; + +typedef enum { + DVM_CONSTANT_INT, + DVM_CONSTANT_DOUBLE, + DVM_CONSTANT_STRING +} SVM_ConstantPoolTag; + +typedef struct { + SVM_ConstantPoolTag tag; + union { + int c_int; + double c_double; + SVM_Char *c_string; + } u; +} SVM_ConstantPool; + +typedef struct { + char *name; + TypeSpecifier *type; +} SVM_Variable; + +typedef struct { + int line_number; + int start_pc; + int pc_count; +} SVM_LineNumber; + typedef struct Compiler { - Block *current_block; + int svm_constant_count; + SVM_Constant *svm_constant; + Block *current_block; } Compiler; Compiler *get_current_compiler(); void set_current_compiler(Compiler *compiler); +typedef struct SVM_Executable { + int constant_pool_count; + SVM_ConstantPool *constant_pool; + int global_variable_count; + SVM_Variable *global_variable; + //int *function_count; + //SVM_Function *function; + int type_specifier_count; + TypeSpecifier *type_specifier; + int constant_count; + SVM_Constant *constant_definition; +} SVM_Executable; + #endif From d9376ba500d32d4f3599b9d2fc3eda42f25d2006 Mon Sep 17 00:00:00 2001 From: hyl <563807243@qq.com> Date: Fri, 23 Jul 2021 09:58:11 +0800 Subject: [PATCH 2/3] add generate_statement_list --- src/codegen.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/summoner.h | 15 +++++++++--- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index 8458cb0..429c515 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -1,6 +1,21 @@ #include "summoner.h" #include +typedef struct { + int label_address; +} LabelTable; + +typedef struct { + int size; + int alloc_size; + SVM_Byte *code; + int label_table_size; + int label_table_alloc_size; + LabelTable *label_table; + int line_number_size; + SVM_LineNumber *line_number; +} OpcodeBuf; + static SVM_Executable * alloc_executable() { @@ -20,6 +35,19 @@ alloc_executable() return exe; } +static void +init_opcode_buf(OpcodeBuf *ob) +{ + ob->size = 0; + ob->alloc_size = 0; + ob->code = NULL; + ob->label_table_size = 0; + ob->label_table_alloc_size = 0; + ob->label_table = NULL; + ob->line_number_size = 0; + ob->line_number = NULL; +} + static void add_global_variable(Compiler *compiler, SVM_Executable *exe) { @@ -30,9 +58,45 @@ add_functions(Compiler *compiler, SVM_Executable *exe) { } +// TODO: huge swith-case +static void +generate_statement_list(SVM_Executable *exe, Block *current_block, + StatementList *statement_list, + OpcodeBuf *ob) +{ +} + +static void +fix_labels(OpcodeBuf *ob) +{ +} + +static SVM_Byte * +fix_opcode_buf(OpcodeBuf *ob) +{ + SVM_Byte *ret; + + fix_labels(ob); + ret = realloc(ob->code, ob->size); + free(ob->label_table); + + return ret; +} + + static void add_top_level(Compiler *compiler, SVM_Executable *exe) { + OpcodeBuf ob; + + init_opcode_buf(&ob); + generate_statement_list(exe, NULL, compiler->statement_list, + &ob); + + exe->top_level.code_size = ob.size; + exe->top_level.code = fix_opcode_buf(&ob); + exe->top_level.line_number_size = ob.line_number_size; + exe->top_level.line_number = ob.line_number; } static void generate_constant_initializer(Compiler *compiler, SVM_Executable *exe) diff --git a/src/summoner.h b/src/summoner.h index 77fcff3..b7ce668 100644 --- a/src/summoner.h +++ b/src/summoner.h @@ -187,9 +187,9 @@ typedef struct { } SVM_Constant; typedef enum { - DVM_CONSTANT_INT, - DVM_CONSTANT_DOUBLE, - DVM_CONSTANT_STRING + SVM_CONSTANT_INT, + SVM_CONSTANT_DOUBLE, + SVM_CONSTANT_STRING } SVM_ConstantPoolTag; typedef struct { @@ -212,10 +212,18 @@ typedef struct { int pc_count; } SVM_LineNumber; +typedef struct { + int code_size; + SVM_Byte *code; + int line_number_size; + SVM_LineNumber *line_number; +} SVM_CodeBlock; + typedef struct Compiler { int svm_constant_count; SVM_Constant *svm_constant; + StatementList *statement_list; Block *current_block; } Compiler; @@ -233,6 +241,7 @@ typedef struct SVM_Executable { TypeSpecifier *type_specifier; int constant_count; SVM_Constant *constant_definition; + SVM_CodeBlock top_level; } SVM_Executable; #endif From a26568a3cda01bd45062f2599d71a13295089076 Mon Sep 17 00:00:00 2001 From: hyl <563807243@qq.com> Date: Fri, 23 Jul 2021 10:33:28 +0800 Subject: [PATCH 3/3] local statementlist --- src/codegen.c | 3 ++- src/summoner.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/codegen.c b/src/codegen.c index 429c515..4a8071f 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -90,7 +90,8 @@ add_top_level(Compiler *compiler, SVM_Executable *exe) OpcodeBuf ob; init_opcode_buf(&ob); - generate_statement_list(exe, NULL, compiler->statement_list, + //TODO: local statement list use - functionDecl -> Block -> statementList + generate_statement_list(exe, NULL, NULL, &ob); exe->top_level.code_size = ob.size; diff --git a/src/summoner.h b/src/summoner.h index b7ce668..f6bc9f6 100644 --- a/src/summoner.h +++ b/src/summoner.h @@ -219,11 +219,11 @@ typedef struct { SVM_LineNumber *line_number; } SVM_CodeBlock; +// TODO: perfect Compiler typedef struct Compiler { int svm_constant_count; SVM_Constant *svm_constant; - StatementList *statement_list; Block *current_block; } Compiler;