F diff --git a/src/backend/asm/intel/intel_compile.c b/src/backend/asm/intel/intel_compile.c
--- a/src/backend/asm/intel/intel_compile.c
+++ b/src/backend/asm/intel/intel_compile.c
void compile_compound_statement_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Compound_Statement *statement)
{
struct Queue_Node *it;
+ static const _Bool is_expression[AST_TYPE_END]
+ =
+ {
+ [OP_COMMA]=1,
+ [OP_ADDITION]=1,
+ [OP_POINTER_ADDITION]=1,
+ [OP_SUBTRACTION]=1,
+ [OP_MUL]=1,
+ [OP_DIV]=1,
+ [OP_REMAINDER]=1,
+ [OP_COND]=1,
+ [OP_FUNCTION]=1,
+ [OP_ASSIGN]=1,
+ [OP_ADD_ASSIGN]=1,
+ [OP_SUBTRACT_ASSIGN]=1,
+ [OP_MULTIPLY_ASSIGN]=1,
+ [OP_REMAINDER_ASSIGN]=1,
+ [OP_DIV_ASSIGN]=1,
+ [OP_SHIFT_LEFT_ASSIGN]=1,
+ [OP_SHIFT_RIGHT_ASSIGN]=1,
+ [OP_AND_ASSIGN]=1,
+ [OP_XOR_ASSIGN]=1,
+ [OP_PIPE_ASSIGN]=1,
+ [OP_LOGICAL_OR]=1,
+ [OP_LOGICAL_AND]=1,
+ [OP_LOGICAL_NOT]=1,
+ [OP_BITWISE_OR]=1,
+ [OP_BITWISE_AND]=1,
+ [OP_BITWISE_XOR]=1,
+ [OP_BITWISE_NOT]=1,
+ [OP_ADDR_OF]=1,
+ [OP_DEREFERENCE]=1,
+ [OP_MEMBER_TROUGH_PTR]=1,
+ [OP_MEMBER]=1,
+ [OP_ARR_SUBSCRIPT]=1,
+ [OP_POSTFIX_INC]=1,
+ [OP_POSTFIX_DEC]=1,
+ [OP_PREFIX_INC]=1,
+ [OP_PREFIX_DEC]=1,
+ [OP_UNARY_PLUS]=1,
+ [OP_UNARY_MINUS]=1,
+ [OP_CAST]=1,
+ [OP_SHIFT_LEFT]=1,
+ [OP_SHIFT_RIGHT]=1,
+ [OP_LESS_EQ]=1,
+ [OP_GREATER_EQ]=1,
+ [OP_LESS]=1,
+ [OP_GREATER]=1,
+ [OP_EQUAL]=1,
+ [OP_NOT_EQUAL]=1,
+ [OP_DESIGNATOR]=1,
+ [OP_CONSTANT]=1,
+ [OP_STRING_LITERAL]=1,
+ [ST_COMPOUND]=1,
+ };
for(it=statement->components->first;it!=NULL;it=it->prev)
+ {
compile_ast_to_intel_asm(compile_data,it->data);
+ if(is_expression[((struct AST*)it->data)->type])
+ push_intel_asm_instruction(compile_data,intel_asm_get_pop(INTEL_ASM_REGISTER_RAX));
+ }
}
void compile_for_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_For_Statement *statement)
{
}
void compile_while_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_While_Statement *wh)
{
- push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));
+ struct Intel_Asm_Label *loop;
+ struct Intel_Asm_Label *end;
+
+ loop=(struct Intel_Asm_Label*)get_intel_asm_new_unique_label(compile_data);
+ end=(struct Intel_Asm_Label*)get_intel_asm_new_unique_label(compile_data);
+
+ push_intel_asm_instruction(compile_data,(struct Intel_Asm_Instruction*)loop);
+
+
+ compile_ast_to_intel_asm(compile_data,wh->condition);
+ push_intel_asm_instruction(compile_data,intel_asm_get_pop(INTEL_ASM_REGISTER_RAX));
+
+ push_intel_asm_instruction(compile_data,get_intel_asm_binary_instruction(
+ get_intel_asm_register(INTEL_ASM_REGISTER_RAX),
+ get_intel_asm_register(INTEL_ASM_REGISTER_RAX),
+ INTEL_ASM_OP_TEST));
+ push_intel_asm_instruction(compile_data,get_intel_asm_jump_instruction(end,INTEL_ASM_OP_JZ));
+
+ compile_ast_to_intel_asm(compile_data,wh->body_statement);
+ push_intel_asm_instruction(compile_data,get_intel_asm_jump_instruction(loop,INTEL_ASM_OP_JMP));
+
+
+ push_intel_asm_instruction(compile_data,(struct Intel_Asm_Instruction*)end);
}
void compile_do_while_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Do_While_Statement *do_while)
{
- push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));
+ struct Intel_Asm_Label *loop;
+
+ loop=(struct Intel_Asm_Label*)get_intel_asm_new_unique_label(compile_data);
+
+ push_intel_asm_instruction(compile_data,(struct Intel_Asm_Instruction*)loop);
+ compile_ast_to_intel_asm(compile_data,do_while->body_statement);
+
+ compile_ast_to_intel_asm(compile_data,do_while->condition);
+ push_intel_asm_instruction(compile_data,intel_asm_get_pop(INTEL_ASM_REGISTER_RAX));
+ push_intel_asm_instruction(compile_data,get_intel_asm_binary_instruction(
+ get_intel_asm_register(INTEL_ASM_REGISTER_RAX),
+ get_intel_asm_register(INTEL_ASM_REGISTER_RAX),
+ INTEL_ASM_OP_TEST));
+
+ push_intel_asm_instruction(compile_data,get_intel_asm_jump_instruction(loop,INTEL_ASM_OP_JNZ));
}
void compile_if_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_If_Statement *statement)
{
- push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));
+ struct Intel_Asm_Label *if_false;
+ struct Intel_Asm_Label *end;
+
+ end=(struct Intel_Asm_Label*)get_intel_asm_new_unique_label(compile_data);
+
+ if(statement->else_statement==NULL)
+ if_false=end;
+ else
+ if_false=(struct Intel_Asm_Label*)get_intel_asm_new_unique_label(compile_data);
+
+
+ compile_ast_to_intel_asm(compile_data,statement->condition);
+
+ push_intel_asm_instruction(compile_data,intel_asm_get_pop(INTEL_ASM_REGISTER_RAX));
+ push_intel_asm_instruction(compile_data,get_intel_asm_binary_instruction(
+ get_intel_asm_register(INTEL_ASM_REGISTER_RAX),
+ get_intel_asm_register(INTEL_ASM_REGISTER_RAX),
+ INTEL_ASM_OP_TEST));
+
+ push_intel_asm_instruction(compile_data,get_intel_asm_jump_instruction(if_false,INTEL_ASM_OP_JZ));
+
+ compile_ast_to_intel_asm(compile_data,statement->body_statement);
+ push_intel_asm_instruction(compile_data,get_intel_asm_jump_instruction(end,INTEL_ASM_OP_JMP));
+
+ push_intel_asm_instruction(compile_data,(struct Intel_Asm_Instruction*)if_false);
+
+ if(statement->else_statement!=NULL)
+ compile_ast_to_intel_asm(compile_data,statement->else_statement);
+
+ push_intel_asm_instruction(compile_data,(struct Intel_Asm_Instruction*)end);
}
void compile_goto_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Goto_Statement *jump)
{
F diff --git a/src/backend/asm/intel/intel_instruction.c b/src/backend/asm/intel/intel_instruction.c
--- a/src/backend/asm/intel/intel_instruction.c
+++ b/src/backend/asm/intel/intel_instruction.c
{
struct Intel_Asm_Instruction_Jump *ret;
ret=wonky_malloc(sizeof(struct Intel_Asm_Instruction_Jump));
- ret->type=INTEL_ASM_OP_JMP;
+ ret->type=type;
ret->where_to=where_to;
[INTEL_ASM_OP_JZ]="JZ",
};
wonky_assert(map[jmp->type]!=NULL);
- fprintf(out,"%s ",map[jmp->type]);
- save_intel_asm_label(jmp->where_to,out);
+ fprintf(out,"%s %s",map[jmp->type],jmp->where_to->label_name);
fprintf(out,"\n");
}
void save_intel_asm_simple_instruction(struct Intel_Asm_Instruction *instruction,FILE *out)