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
32 changes: 24 additions & 8 deletions compiler/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef struct
int size;
int alloc_size;
SVM_Byte *code;
int pc;
int label_table_size;
int label_table_alloc_size;
LabelTable *label_table;
Expand Down Expand Up @@ -49,6 +50,7 @@ init_opcode_buf(OpcodeBuf *ob)
ob->size = 0;
ob->alloc_size = 0;
ob->code = NULL;
ob->pc = 0;
ob->label_table_size = 0;
ob->label_table_alloc_size = 0;
ob->label_table = NULL;
Expand All @@ -70,6 +72,7 @@ fix_labels(OpcodeBuf *ob)
if (ob->code[i] >= OP_DATA_1 && ob->code[i] <= OP_DATA_75) {
int len = ob->code[i] - OP_DATA_1 + 1;
i += len;
ob->pc++;
continue;
}

Expand All @@ -83,6 +86,7 @@ fix_labels(OpcodeBuf *ob)
ob->code[ob->size++] = SHIFT_OP(address, shift);
}
}
ob->pc++;
}
}

Expand Down Expand Up @@ -137,7 +141,7 @@ add_global_variable(Compiler *compiler, SVM_Executable *exe)

for (dl = compiler->declaration_list, i = 0; dl; dl = dl->next, i++)
{
strcpy(exe->global_variable[i].name, dl->name);
exe->global_variable[i].name = dl->name;
exe->global_variable[i].type = dl->type;
}
// TODO: derive type: FUNCTION_DERIVE/ARRAY_DERIVE
Expand All @@ -148,6 +152,7 @@ generate_builtin_code(OpcodeBuf *ob, SVM_Opcode *ops, int op_cnt)
for(int i = 0; i< op_cnt; i++) {
ob->code[ob->size] = ops[i];
ob->size++;
ob->pc++;
}
}

Expand All @@ -167,6 +172,7 @@ generate_code(OpcodeBuf *ob, SVM_Opcode code, ...)

ob->code[ob->size] = code;
ob->size++;
ob->pc++;
}

static void
Expand Down Expand Up @@ -195,6 +201,7 @@ generate_label_code(OpcodeBuf *ob, int value)
int64_t shift = SHIFT_SIZE * (JUMP_TARGET_SIZE - (i + 1));
ob->code[ob->size++] = SHIFT_OP(value, shift);
}
ob->pc++;
}

static void
Expand Down Expand Up @@ -243,7 +250,7 @@ count_parameter(ParameterList *src)
}

static SVM_Variable *
copy_parameter_list(ParameterList *src, int *param_count_p)
copy_parameter_list(ParameterList *src, int *param_count_p, OpcodeBuf *ob)
{
ParameterList *param;
SVM_Variable *dest;
Expand All @@ -258,6 +265,7 @@ copy_parameter_list(ParameterList *src, int *param_count_p)
for (param = src, i = 0; param; param = param->next, i++) {
dest[i].name = strdup(param->name);
dest[i].type = alloc_type_specifier(param->type->basic_type);
ob->pc++;
}

return dest;
Expand All @@ -271,7 +279,7 @@ add_function(SVM_Executable *exe, FuncDefinition *src, SVM_Function *dest)

dest->type = alloc_type_specifier(src->return_type->basic_type);
dest->parameter = copy_parameter_list(src->parameters,
&dest->parameter_count);
&dest->parameter_count, &ob);

generate_statement_list(exe, src->code_block, src->code_block->statement_list, &ob);
dest->code_block.code_size = ob.size;
Expand All @@ -294,21 +302,28 @@ add_functions(Compiler *compiler, SVM_Executable *exe)
}
}

static void
generate_int_expression(SVM_Executable *cf, int64_t value,
OpcodeBuf *ob);

static void
generate_pop_to_identifier(Declaration *decl, OpcodeBuf *ob)
{
switch (decl->variable_index) {
int depth = ob->pc - decl->pc - 1;
switch (depth) {
case 0:
generate_code(ob, OP_DUP);
break;
case 1:
generate_code(ob, OP_DUP);
generate_code(ob, OP_OVER);
break;
default:
generate_code(ob, OP_1 + decl->variable_index - 1);
generate_code(ob, OP_ROLL);
generate_int_expression(NULL, depth, ob);
generate_code(ob, OP_PICK);
break;
}
generate_code(ob, OP_DROP);

decl->pc = ob->pc;
}

static void
Expand Down Expand Up @@ -452,6 +467,7 @@ generate_string_expression(SVM_Executable *cf, Expression *expr,
ob->code[ob->size] = str[i];
ob->size++;
}
ob->pc++;
}

// NOTE: no bvm ops for double type
Expand Down
11 changes: 10 additions & 1 deletion compiler/disassemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,16 @@ assemble_Opcode(int code_size, SVM_Byte *code)
{
printf("*** assemble opcodes ***\n");
for (int index = 0; index < code_size; index++) {
fprintf(stdout, "%x", code[index]);
fprintf(stdout, "%02x", code[index]);

if (code[index] == OP_DATA_4 || code[index] == OP_DATA_8) {
int size = (code[index] == OP_DATA_4 ? sizeof(int) : sizeof(int64_t));
for(int off = size; off > 0; off--) {
fprintf(stdout, "%02x", code[index+off]);
}
index += size;
continue;
}
}
printf("\n");
}
Expand Down
1 change: 1 addition & 0 deletions compiler/summoner.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ typedef struct Declaration
Expression *initializer;
bool is_local;
int variable_index;
int pc;
struct Declaration *next;
} Declaration;

Expand Down