Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
123 changes: 123 additions & 0 deletions src/codegen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include "summoner.h"
#include <stdlib.h>

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()
{
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
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)
{
}

static void
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)
{
}

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;
}
68 changes: 67 additions & 1 deletion src/summoner.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _SUMMONER_H_

#include <stdbool.h>
#include <wchar.h>

typedef enum
{
Expand Down Expand Up @@ -170,12 +171,77 @@ 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 {
SVM_CONSTANT_INT,
SVM_CONSTANT_DOUBLE,
SVM_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 {
int code_size;
SVM_Byte *code;
int line_number_size;
SVM_LineNumber *line_number;
} SVM_CodeBlock;

typedef struct Compiler
{
Block *current_block;
int svm_constant_count;
SVM_Constant *svm_constant;
StatementList *statement_list;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statement_list去掉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK了

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_CodeBlock top_level;
} SVM_Executable;

#endif