Skip to content
Merged
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
119 changes: 75 additions & 44 deletions compiler/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,53 @@ init_opcode_buf(OpcodeBuf *ob)
ob->line_number = NULL;
}

static void
fix_labels(OpcodeBuf *ob)
{
int i;
int j;
OpcodeInfo *info;
int label;
int address;

for (i = 0; i < ob->size; i++) {
if (ob->code[i] == JUMP
|| ob->code[i] == JUMPIF) {
label = (ob->code[i+1] << 8) + (ob->code[i+2]);
address = ob->label_table[label].label_address;
ob->code[i+1] = (SVM_Byte)(address >> 8);
ob->code[i+2] = (SVM_Byte)(address &0xff);
}
info = &svm_opcode_info[ob->code[i]];
for (j = 0; info->parameter[j] != '\0'; j++) {
switch (info->parameter[j]) {
case 'b':
i++;
break;
case 's':
case 'p':
i += 2;
break;
default:
printf("param..%s, j..%d", info->parameter, j);
exit(1);
}
}
}
}

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_global_variable(Compiler *compiler, SVM_Executable *exe)
{
Expand Down Expand Up @@ -133,48 +180,44 @@ generate_statement_list(SVM_Executable *exe, Block *current_block,
static void generate_expression(SVM_Executable *exe, Block *current_block,
Expression *expr, OpcodeBuf *ob);

static void
fix_labels(OpcodeBuf *ob)
/*
// TODO: LocalVariable for FuncDefinition
static SVM_Variable *
copy_local_variables(FuncDefinition *fd, int param_count)
{
int i;
int j;
OpcodeInfo *info;
int label;
int address;
int i;
int local_variable_count;
SVM_Variable *dest;

for (i = 0; i < ob->size; i++) {
if (ob->code[i] == JUMP
|| ob->code[i] == JUMPIF) {
label = (ob->code[i+1] << 8) + (ob->code[i+2]);
address = ob->label_table[label].label_address;
ob->code[i+1] = (SVM_Byte)(address >> 8);
ob->code[i+2] = (SVM_Byte)(address &0xff);
}
info = &svm_opcode_info[ob->code[i]];
for (j = 0; info->parameter[j] != '\0'; j++) {
switch (info->parameter[j]) {
case 'b':
i++;
break;
case 's':
case 'p':
i += 2;
break;
default:
printf("param..%s, j..%d", info->parameter, j);
exit(1);
}
}
local_variable_count = fd->local_variable_count - param_count;

dest = malloc(sizeof(SVM_LocalVariable) * local_variable_count);

for (i = 0; i < local_variable_count; i++) {
dest[i].name
= strdup(fd->local_variable[i+param_count]->name);
dest[i].type
= copy_type_specifier(fd->local_variable[i+param_count]->type);
}
}

return dest;
}
*/

static void
add_function(SVM_Executable *exe, FuncDefinition *src)
{
OpcodeBuf ob;
init_opcode_buf(&ob);
generate_statement_list(exe, src->block, src->block->statement_list, &ob);
fix_labels(&ob);
exe->function->code_block.code_size = ob.size;
exe->function->code_block.code = fix_opcode_buf(&ob);
exe->function->code_block.line_number_size = ob.line_number_size;
exe->function->code_block.line_number = ob.line_number;
//exe->function->local_variable
// = copy_local_variables(src, exe->function->parameter_count);
//exe->function->local_variable
// = src->local_variable_count - dest->parameter_count;
}

static void
Expand Down Expand Up @@ -494,18 +537,6 @@ generate_statement_list(SVM_Executable *exe, Block *current_block,
}
}

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)
{
Expand Down
14 changes: 12 additions & 2 deletions compiler/summoner.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ typedef struct
SVM_LineNumber *line_number;
} SVM_CodeBlock;

typedef struct {
TypeSpecifier *type;
char *name;
int parameter_count;
SVM_Variable *parameter;
int local_variable_count;
SVM_Variable *local_variable;
SVM_CodeBlock code_block;
} SVM_Function;

// TODO: perfect Compiler
typedef struct Compiler
{
Expand All @@ -311,8 +321,8 @@ typedef struct SVM_Executable
SVM_ConstantPool *constant_pool;
int global_variable_count;
SVM_Variable *global_variable;
//int *function_count;
//SVM_Function *function;
int function_count;
SVM_Function *function;
int type_specifier_count;
TypeSpecifier *type_specifier;
int constant_count;
Expand Down