F diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txtinclude_directories(src/backend/js)include_directories(src/backend/text/print)include_directories(src/backend/text)+ include_directories(src/backend/asm)+ include_directories(src/backend/asm/intel)include_directories(src/backend)include_directories(src/frontend)set(SOURCESsrc/backend/text/print/print.csrc/backend/text/lines.c+ src/backend/asm/intel/intel_asm.c+ src/backend/asm/intel/intel_instruction.c+ src/backend/asm/intel/intel_location.c+ src/backend/asm/intel/intel_compile.csrc/backend/compile.csrc/debug/debug_ast.csrc/debug/debug_denoted.cF diff --git a/src/backend/asm/intel/intel_asm.c b/src/backend/asm/intel/intel_asm.c new file mode 100644 --- /dev/null +++ b/src/backend/asm/intel/intel_asm.c+ #ifndef WONKY_INTEL_ASM_C+ #define WONKY_INTEL_ASM_C WONKY_INTEL_ASM_C+ #include <intel_asm.h>++ struct Compiled_Object_Intel_Asm* compile_program_to_intel_asm(struct Program *program)+ {+ struct Compile_Data_Intel_Asm *data;+ struct Compiled_Object_Intel_Asm *ret;+ struct Queue_Node *it;++ data=get_compile_data_for_intel_asm();++ for(it=program->translation_units->first;it!=NULL;it=it->prev)+ compile_translation_unit_to_intel_asm(data,it->data);++ ret=malloc(sizeof(struct Compiled_Object_Intel_Asm));+ ret->type=COMPILE_TO_INTEL_ASM;+ ret->errors=data->errors;+ ret->instructions=data->instructions;++ return ret;+ }+ void save_compiled_intel_asm(struct Compiled_Object_Intel_Asm *object,FILE *out)+ {+ struct Queue_Node *it;+ for(it=object->instructions->first;it!=NULL;it=it->prev)+ save_intel_asm_instruction(it->data,out);+ }+ struct Compile_Data_Intel_Asm* get_compile_data_for_intel_asm()+ {+ struct Compile_Data_Intel_Asm *ret;++ ret=malloc(sizeof(struct Compile_Data_Intel_Asm));+ ret->type=COMPILE_TO_INTEL_ASM;++ ret->errors=malloc(sizeof(struct Queue));+ Queue_Init(ret->errors);++ ret->instructions=malloc(sizeof(struct Queue));+ Queue_Init(ret->instructions);++ ret->number_of_anon_labels=0;+ return ret;++ }+++++ struct Intel_Asm_Instruction* intel_asm_get_push_ax()+ {+ return get_intel_asm_unary_instruction(intel_asm_get_ax_register(),INTEL_ASM_OP_PUSH);+ }+ struct Intel_Asm_Instruction* intel_asm_get_pop_ax()+ {+ return get_intel_asm_unary_instruction(intel_asm_get_ax_register(),INTEL_ASM_OP_POP);+ }+ struct Intel_Asm_Instruction* intel_asm_get_pop_dx()+ {+ return get_intel_asm_unary_instruction(intel_asm_get_dx_register(),INTEL_ASM_OP_POP);+ }++ void push_intel_asm_instruction(struct Compile_Data_Intel_Asm *compile_data,struct Intel_Asm_Instruction *instruction)+ {+ Queue_Push(compile_data->instructions,instruction);+ }+ #endifF diff --git a/src/backend/asm/intel/intel_asm.h b/src/backend/asm/intel/intel_asm.h new file mode 100644 --- /dev/null +++ b/src/backend/asm/intel/intel_asm.h+ #ifndef WONKY_INTEL_ASM_H+ #define WONKY_INTEL_ASM_H WONKY_INTEL_ASM_H+ #include <intel_asm.hh>++ #include <intel_instruction.h>+ #include <intel_location.h>+ #include <intel_compile.h>++ #include <stdio.h>+ #include <stdlib.h>+ #include <compile.h>+ #include <program.h>+ #include <queue.h>+ #include <ast.h>+++ struct Compile_Data_Intel_Asm+ {+ enum Compilation_Type type;+ struct Queue *errors;++ struct Queue *instructions;++ int number_of_anon_labels;+ };+ struct Compiled_Object_Intel_Asm+ {+ enum Compilation_Type type;+ struct Queue *errors;++ struct Queue *instructions;+ };+++++ struct Compiled_Object_Intel_Asm* compile_program_to_intel_asm(struct Program *program);+ void save_compiled_intel_asm(struct Compiled_Object_Intel_Asm *object,FILE *out);+ struct Compile_Data_Intel_Asm* get_compile_data_for_intel_asm();+++ void push_intel_asm_instruction(struct Compile_Data_Intel_Asm *compile_data,struct Intel_Asm_Instruction *instruction);++ #endifF diff --git a/src/backend/asm/intel/intel_asm.hh b/src/backend/asm/intel/intel_asm.hh new file mode 100644 --- /dev/null +++ b/src/backend/asm/intel/intel_asm.hh+ #ifndef WONKY_INTEL_ASM_HH+ #define WONKY_INTEL_ASM_HH WONKY_INTEL_ASM_HH++ struct Compile_Data_Intel_Asm;+ struct Compiled_Object_Intel_Asm;+++ #endifF diff --git a/src/backend/asm/intel/intel_compile.c b/src/backend/asm/intel/intel_compile.c new file mode 100644 --- /dev/null +++ b/src/backend/asm/intel/intel_compile.c+ #ifndef WONKY_INTEL_COMPILE_C+ #define WONKY_INTEL_COMPILE_C WONKY_INTEL_COMPILE_C+ #include <intel_compile.h>++ void compile_ast_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST *ast)+ {+ typedef void (*map_entry)(struct Compile_Data_Intel_Asm*,void*);+ static map_entry map[AST_TYPE_END]+ =+ {+ [OP_COMMA]=(map_entry)compile_comma_expression_to_intel_asm,+ [OP_ADDITION]=(map_entry)compile_addition_to_intel_asm,+ [OP_POINTER_ADDITION]=(map_entry)compile_pointer_addition_to_intel_asm,+ [OP_SUBTRACTION]=(map_entry)compile_subscript_to_intel_asm,+ [OP_MUL]=(map_entry)compile_mul_assign_to_intel_asm,+ [OP_DIV]=(map_entry)compile_div_assign_to_intel_asm,+ [OP_REMAINDER]=(map_entry)compile_remainder_to_intel_asm,+ [OP_COND]=(map_entry)compile_conditional_expression_to_intel_asm,+ [OP_FUNCTION]=(map_entry)compile_function_expression_to_intel_asm,+ [OP_ASSIGN]=(map_entry)compile_assign_to_intel_asm,+ [OP_ADD_ASSIGN]=(map_entry)compile_add_assign_to_intel_asm,+ [OP_SUBTRACT_ASSIGN]=(map_entry)compile_sub_assign_to_intel_asm,+ [OP_MULTIPLY_ASSIGN]=(map_entry)compile_mul_assign_to_intel_asm,+ [OP_REMAINDER_ASSIGN]=(map_entry)compile_rem_assign_to_intel_asm,+ [OP_DIV_ASSIGN]=(map_entry)compile_div_assign_to_intel_asm,+ [OP_SHIFT_LEFT_ASSIGN]=(map_entry)compile_shift_left_to_intel_asm,+ [OP_SHIFT_RIGHT_ASSIGN]=(map_entry)compile_shift_right_to_intel_asm,+ [OP_AND_ASSIGN]=(map_entry)compile_and_assign_to_intel_asm,+ [OP_XOR_ASSIGN]=(map_entry)compile_xor_assign_to_intel_asm,+ [OP_PIPE_ASSIGN]=(map_entry)compile_pipe_assign_to_intel_asm,+ [OP_NOP]=(map_entry)compile_nop_to_intel_asm,+ [OP_LOGICAL_OR]=(map_entry)compile_logical_or_to_intel_asm,+ [OP_LOGICAL_AND]=(map_entry)compile_logical_and_to_intel_asm,+ [OP_LOGICAL_NOT]=(map_entry)compile_logical_not_to_intel_asm,+ [OP_BITWISE_OR]=(map_entry)compile_logical_or_to_intel_asm,+ [OP_BITWISE_AND]=(map_entry)compile_bitwise_and_to_intel_asm,+ [OP_BITWISE_XOR]=(map_entry)compile_bitwise_xor_to_intel_asm,+ [OP_BITWISE_NOT]=(map_entry)compile_bitwise_not_to_intel_asm,+ [OP_ADDR_OF]=(map_entry)compile_get_address_to_intel_asm,+ [OP_DEREFERENCE]=(map_entry)compile_dereference_to_intel_asm,+ [OP_MEMBER_TROUGH_PTR]=(map_entry)compile_member_from_ptr_to_intel_asm,+ [OP_MEMBER]=(map_entry)compile_member_to_intel_asm,+ [OP_ARR_SUBSCRIPT]=(map_entry)compile_subscript_to_intel_asm,+ [OP_POSTFIX_INC]=(map_entry)compile_postfix_inc_to_intel_asm,+ [OP_POSTFIX_DEC]=(map_entry)compile_postfix_dec_to_intel_asm,+ [OP_PREFIX_INC]=(map_entry)compile_prefix_inc_to_intel_asm,+ [OP_PREFIX_DEC]=(map_entry)compile_prefix_dec_to_intel_asm,+ [OP_UNARY_PLUS]=(map_entry)compile_unary_plus_to_intel_asm,+ [OP_UNARY_MINUS]=(map_entry)compile_unary_minus_to_intel_asm,+ [OP_CAST]=(map_entry)compile_cast_to_intel_asm,+ [OP_SHIFT_LEFT]=(map_entry)compile_shift_left_to_intel_asm,+ [OP_SHIFT_RIGHT]=(map_entry)compile_shift_right_to_intel_asm,+ [OP_LESS_EQ]=(map_entry)compile_less_eq_to_intel_asm,+ [OP_GREATER_EQ]=(map_entry)compile_greater_eq_to_intel_asm,+ [OP_LESS]=(map_entry)compile_less_to_intel_asm,+ [OP_GREATER]=(map_entry)compile_greater_to_intel_asm,+ [OP_EQUAL]=(map_entry)compile_equal_to_intel_asm,+ [OP_NOT_EQUAL]=(map_entry)compile_not_equal_to_intel_asm,+ [OP_DESIGNATOR]=(map_entry)compile_designator_to_intel_asm,+ [OP_CONSTANT]=(map_entry)compile_constant_to_intel_asm,+ [OP_STRING_LITERAL]=(map_entry)compile_string_literal_to_intel_asm,+ [ST_COMPOUND]=(map_entry)compile_compound_statement_to_intel_asm,+ [ST_EXPRESSION]=(map_entry)compile_expression_statement,+ [ST_SWITCH]=(map_entry)compile_switch_to_intel_asm,+ [ST_IF]=(map_entry)compile_if_to_intel_asm,+ [ST_WHILE]=(map_entry)compile_while_to_intel_asm,+ [ST_DO_WHILE]=(map_entry)compile_do_while_to_intel_asm,+ [ST_GOTO]=(map_entry)compile_goto_to_intel_asm,+ [ST_LABEL]=(map_entry)compile_labeled_statement_to_intel_asm,+ [ST_CASE]=(map_entry)compile_case_to_intel_asm,+ [ST_DEFAULT]=(map_entry)compile_default_to_intel_asm,+ [ST_CONTINUE]=(map_entry)compile_break_continue_statement_to_intel_asm,+ [ST_BREAK]=(map_entry)compile_break_continue_statement_to_intel_asm,+ [ST_RETURN]=(map_entry)compile_return_to_intel_asm,+ [ST_FOR]=(map_entry)compile_for_to_intel_asm,+ [ST_OBJECT_DECLARATION]=(map_entry)compile_object_declaration_to_intel_asm,+ [ST_FUNCTION_DEFINITION]=(map_entry)compile_function_definition_to_intel_asm,+ };++ wonky_assert(is_valid_ast(ast));+ wonky_assert(map[ast->type]!=NULL);++ map[ast->type](compile_data,ast);+ }+ void compile_conditional_expression_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Conditional_Expression *expression)+ {+ struct Intel_Asm_Label *end;+ struct Intel_Asm_Label *second;+ struct Intel_Asm_Memory_Location *ax_register;++ end=(struct Intel_Asm_Label*)get_intel_asm_new_unique_label(compile_data);+ second=(struct Intel_Asm_Label*)get_intel_asm_new_unique_label(compile_data);++ compile_ast_to_intel_asm(compile_data,(struct AST*)expression->left);++ push_intel_asm_instruction(compile_data,intel_asm_get_pop_ax());+ push_intel_asm_instruction(compile_data,get_intel_asm_binary_instruction(ax_register,ax_register,INTEL_ASM_OP_TEST));+ push_intel_asm_instruction(compile_data,get_intel_asm_jump_instruction(second,INTEL_ASM_OP_JZ));++ compile_ast_to_intel_asm(compile_data,(struct AST*)expression->center);+ 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*)second);+ compile_ast_to_intel_asm(compile_data,(struct AST*)expression->right);++ push_intel_asm_instruction(compile_data,(struct Intel_Asm_Instruction*)end);+ }+ void compile_function_expression_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Function_Expression *call)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_constant_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Constant *constant)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_string_literal_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_String_Literal *literal)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_designator_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Designator *designator)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_labeled_statement_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Labeled_Statement *labeled)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_break_continue_statement_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Break_Continue_Statement *br)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_compound_statement_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Compound_Statement *statement)+ {+ struct Queue_Node *it;++ for(it=statement->components.first;it!=NULL;it=it->prev)+ compile_ast_to_intel_asm(compile_data,it->data);+ }+ void compile_for_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_For_Statement *statement)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ 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));+ }+ 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));+ }+ 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));+ }+ void compile_goto_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Goto_Statement *jump)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_switch_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Switch_Statement *sw)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_default_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Default_Statement *def)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_case_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Case_Statement *cs)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_return_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Return_Statement *ret)+ {+ push_intel_asm_instruction(compile_data,intel_asm_get_pop_ax());+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_RET));+ }+ void compile_object_declaration_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Object_Declaration *objd)+ {+ push_intel_asm_instruction(compile_data,+ get_intel_asm_binary_instruction(get_intel_asm_register(INTEL_ASM_REGISTER_SP),+ get_intel_asm_in_instruction_number(4),+ INTEL_ASM_OP_SUB));++ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_function_definition_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Function_Definition *def)+ {+ struct token *id;+ id=def->function->id;+ push_intel_asm_instruction(compile_data,get_intel_asm_label(gstr_dup(id->data,id->data+id->data_size,1024)));+ compile_compound_statement_to_intel_asm(compile_data,def->body);++ /*if there are no returns we return jiberrish*/+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_RET));++ }+ void compile_translation_unit_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Translation_Unit *unit)+ {+ struct Queue_Node *it;+ for(it=unit->components->first;it!=NULL;it=it->prev)+ compile_ast_to_intel_asm(compile_data,it->data);+ }+ void compile_expression_statement(struct Compile_Data_Intel_Asm *compile_data,struct AST_Expression *expression)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }++ void compile_comma_expression_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_addition_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ compile_ast_to_intel_asm(compile_data,(struct AST*)bin->left);+ compile_ast_to_intel_asm(compile_data,(struct AST*)bin->right);+ push_intel_asm_instruction(compile_data,intel_asm_get_pop_ax());+ push_intel_asm_instruction(compile_data,intel_asm_get_pop_dx());+ push_intel_asm_instruction(compile_data,get_intel_asm_binary_instruction(intel_asm_get_ax_register(),intel_asm_get_dx_register(),INTEL_ASM_OP_ADD));+ push_intel_asm_instruction(compile_data,intel_asm_get_push_ax());+ }+ void compile_pointer_addition_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_multiplication_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_division_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_remainder_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_add_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_sub_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_mul_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_rem_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_div_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_shift_left_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_shift_right_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_and_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_xor_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_pipe_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_nop_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,void *dummy)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_logical_or_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_logical_and_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_logical_not_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_bitwise_or_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_bitwise_and_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_bitwise_xor_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_bitwise_not_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_get_address_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_dereference_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_member_from_ptr_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_member_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_subscript_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_postfix_inc_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_postfix_dec_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_prefix_inc_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_prefix_dec_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_unary_plus_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_unary_minus_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_cast_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_shift_left_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_shift_right_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_less_eq_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_greater_eq_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_less_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_greater_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_equal_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+ void compile_not_equal_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin)+ {+ push_intel_asm_instruction(compile_data,get_intel_asm_simple_instruction(INTEL_ASM_OP_NOP));+ }+++ #endifF diff --git a/src/backend/asm/intel/intel_compile.h b/src/backend/asm/intel/intel_compile.h new file mode 100644 --- /dev/null +++ b/src/backend/asm/intel/intel_compile.h+ #ifndef WONKY_INTEL_COMPILE_H+ #define WONKY_INTEL_COMPILE_H WONKY_INTEL_COMPILE_H++ #include <intel_asm.h>+ #include <ast.h>++ void compile_binary_expression(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_unary_expression(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);++ void compile_ast_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST *ast);+ void compile_conditional_expression_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Conditional_Expression *expression);+ void compile_function_expression_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Function_Expression *call);+ void compile_constant_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Constant *constant);+ void compile_string_literal_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_String_Literal *literal);+ void compile_designator_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Designator *designator);+ void compile_labeled_statement_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Labeled_Statement *labeled);+ void compile_break_continue_statement_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Break_Continue_Statement *br);+ void compile_compound_statement_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Compound_Statement *statement);+ 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);+ void compile_do_while_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Do_While_Statement *do_while);+ void compile_if_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_If_Statement *statement);+ void compile_goto_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Goto_Statement *jump);+ void compile_switch_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Switch_Statement *sw);+ void compile_default_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Default_Statement *def);+ void compile_case_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Case_Statement *cs);+ void compile_return_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Return_Statement *ret);+ void compile_object_declaration_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Object_Declaration *objd);+ void compile_function_definition_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Function_Definition *def);+ void compile_translation_unit_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Translation_Unit *unit);+ void compile_expression_statement(struct Compile_Data_Intel_Asm *compile_data,struct AST_Expression *expression);++ void compile_comma_expression_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_addition_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_pointer_addition_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_multiplication_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_division_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_remainder_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_add_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_sub_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_mul_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_rem_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_div_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_shift_left_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_shift_right_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_and_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_xor_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_pipe_assign_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_nop_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,void *dummy);+ void compile_logical_or_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data);+ void compile_logical_and_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_logical_not_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_bitwise_or_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);+ void compile_bitwise_and_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_bitwise_xor_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_bitwise_not_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);+ void compile_get_address_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);+ void compile_dereference_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);+ void compile_member_from_ptr_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_member_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_subscript_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_postfix_inc_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);+ void compile_postfix_dec_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);+ void compile_prefix_inc_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);+ void compile_prefix_dec_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);+ void compile_unary_plus_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);+ void compile_unary_minus_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);+ void compile_cast_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Unary_Expression *unary);+ void compile_shift_left_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_shift_right_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_less_eq_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_greater_eq_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_less_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_greater_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_equal_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+ void compile_not_equal_to_intel_asm(struct Compile_Data_Intel_Asm *compile_data,struct AST_Binary_Expression *bin);+++ #endifF diff --git a/src/backend/asm/intel/intel_instruction.c b/src/backend/asm/intel/intel_instruction.c new file mode 100644 --- /dev/null +++ b/src/backend/asm/intel/intel_instruction.c+ #ifndef WONKY_INTEL_ASM_INSTRUCTION_C+ #define WONKY_INTEL_ASM_INSTRUCTION_C WONKY_INTEL_ASM_INSTRUCTION_C+ #include <intel_instruction.h>++ struct Intel_Asm_Instruction* get_intel_asm_label(char *label)+ {+ struct Intel_Asm_Label *ret;++ ret=malloc(sizeof(struct Intel_Asm_Label));+ ret->type=INTEL_ASM_OP_LABEL;+ ret->label_name=label;++ return (struct Intel_Asm_Instruction*)ret;+ }++ struct Intel_Asm_Instruction* get_intel_asm_new_unique_label(struct Compile_Data_Intel_Asm *compile_data)+ {+ struct Intel_Asm_Label *ret;+ char *label;+ label=calloc(1,1024);++ if(snprintf(label,1024,"_label_%d",compile_data->number_of_anon_labels)!=1024)+ label[1023]='\0';/*TODO throw an error*/++ ret=malloc(sizeof(struct Intel_Asm_Label));+ ret->type=INTEL_ASM_OP_LABEL;+ ret->label_name=label;++ return (struct Intel_Asm_Instruction*)ret;+ }+ struct Intel_Asm_Instruction* get_intel_asm_binary_instruction(struct Intel_Asm_Memory_Location *left,struct Intel_Asm_Memory_Location *right,enum Intel_Asm_Instruction_Type type)+ {+ struct Intel_Asm_Instruction_Binary *ret;+ ret=malloc(sizeof(struct Intel_Asm_Instruction_Binary));+ ret->type=type;+ ret->left=left;+ ret->right=right;++ return (struct Intel_Asm_Instruction*)ret;+ }+ struct Intel_Asm_Instruction* get_intel_asm_unary_instruction(struct Intel_Asm_Memory_Location *operand,enum Intel_Asm_Instruction_Type type)+ {+ struct Intel_Asm_Instruction_Unary *ret;+ ret=malloc(sizeof(struct Intel_Asm_Instruction_Unary));+ ret->type=type;+ ret->operand=operand;++ return (struct Intel_Asm_Instruction*)ret;+ }+ struct Intel_Asm_Instruction* get_intel_asm_jump_instruction(struct Intel_Asm_Label *where_to,enum Intel_Asm_Instruction_Type type)+ {+ struct Intel_Asm_Instruction_Jump *ret;+ ret=malloc(sizeof(struct Intel_Asm_Instruction_Jump));+ ret->type=INTEL_ASM_OP_JMP;+ ret->where_to=where_to;+++ return (struct Intel_Asm_Instruction*)ret;+ }++ struct Intel_Asm_Instruction* get_intel_asm_simple_instruction(enum Intel_Asm_Instruction_Type type)+ {+ struct Intel_Asm_Instruction *ret;+ ret=malloc(sizeof(struct Intel_Asm_Instruction));+ ret->type=type;++ return ret;+ }+ void save_intel_asm_instruction(struct Intel_Asm_Instruction *instruction,FILE *out)+ {+ typedef void (*map_entry)(void *,FILE *out);+ static const map_entry map[INTEL_ASM_OP_END]+ =+ {+ [INTEL_ASM_OP_ADD]=(map_entry)save_intel_asm_binary_instruction,+ [INTEL_ASM_OP_SUB]=(map_entry)save_intel_asm_binary_instruction,+ [INTEL_ASM_OP_MOV]=(map_entry)save_intel_asm_binary_instruction,+ [INTEL_ASM_OP_MUL]=(map_entry)save_intel_asm_binary_instruction,+ [INTEL_ASM_OP_JMP]=(map_entry)save_intel_asm_jump_instruction,+ [INTEL_ASM_OP_TEST]=(map_entry)save_intel_asm_binary_instruction,+ [INTEL_ASM_OP_JE]=(map_entry)save_intel_asm_jump_instruction,+ [INTEL_ASM_OP_JL]=(map_entry)save_intel_asm_jump_instruction,+ [INTEL_ASM_OP_JNZ]=(map_entry)save_intel_asm_jump_instruction,+ [INTEL_ASM_OP_JZ]=(map_entry)save_intel_asm_jump_instruction,+ [INTEL_ASM_OP_RET]=(map_entry)save_intel_asm_simple_instruction,+ [INTEL_ASM_OP_CALL]=(map_entry)save_intel_asm_jump_instruction,+ [INTEL_ASM_OP_AND]=(map_entry)save_intel_asm_binary_instruction,+ [INTEL_ASM_OP_XOR]=(map_entry)save_intel_asm_binary_instruction,+ [INTEL_ASM_OP_OR]=(map_entry)save_intel_asm_binary_instruction,+ [INTEL_ASM_OP_NOP]=(map_entry)save_intel_asm_simple_instruction,+ [INTEL_ASM_OP_LABEL]=(map_entry)save_intel_asm_label,+ [INTEL_ASM_OP_POP]=(map_entry)save_intel_asm_unary_instruction,+ [INTEL_ASM_OP_PUSH]=(map_entry)save_intel_asm_unary_instruction,+ };+ wonky_assert(map[instruction->type]!=NULL);+ map[instruction->type](instruction,out);+ }+ void save_intel_asm_label(struct Intel_Asm_Label *label,FILE *out)+ {+ fprintf(out,"%s:\n",label->label_name);+ }+ void save_intel_asm_binary_instruction(struct Intel_Asm_Instruction_Binary *bin,FILE *out)+ {+ const char *map[INTEL_ASM_OP_END]+ =+ {+ [INTEL_ASM_OP_ADD]="ADD",+ [INTEL_ASM_OP_SUB]="SUB",+ [INTEL_ASM_OP_MOV]="MOV",+ [INTEL_ASM_OP_MUL]="MOV",+ [INTEL_ASM_OP_TEST]="TEST",+ [INTEL_ASM_OP_AND]="AND",+ [INTEL_ASM_OP_XOR]="XOR",+ [INTEL_ASM_OP_OR]="OR",+ };+ wonky_assert(map[bin->type]!=NULL);+ fprintf(out,"%s ",map[bin->type]);+ save_intel_asm_location(bin->left,out);+ fprintf(out,", ");+ save_intel_asm_location(bin->right,out);+ fprintf(out,"\n");+++ }+ void save_intel_asm_unary_instruction(struct Intel_Asm_Instruction_Unary *unary,FILE *out)+ {+ const char *map[INTEL_ASM_OP_END]+ =+ {+ [INTEL_ASM_OP_POP]="POP",+ [INTEL_ASM_OP_PUSH]="PUSH",+ };+ wonky_assert(map[unary->type]!=NULL);+ fprintf(out,"%s ",map[unary->type]);+ save_intel_asm_location(unary->operand,out);+ fprintf(out,"\n");+ }+ void save_intel_asm_jump_instruction(struct Intel_Asm_Instruction_Jump *jmp,FILE *out)+ {+ const char *map[INTEL_ASM_OP_END]+ =+ {+ [INTEL_ASM_OP_JMP]="JMP",+ [INTEL_ASM_OP_JE]="JE",+ [INTEL_ASM_OP_JL]="JL",+ [INTEL_ASM_OP_JNZ]="JNZ",+ [INTEL_ASM_OP_JZ]="JZ",+ [INTEL_ASM_OP_CALL]="CALL",+ };+ wonky_assert(map[jmp->type]!=NULL);+ fprintf(out,"%s ",map[jmp->type]);+ save_intel_asm_label(jmp->where_to,out);+ fprintf(out,"\n");+ }+ void save_intel_asm_simple_instruction(struct Intel_Asm_Instruction *instruction,FILE *out)+ {+ static const char *map[INTEL_ASM_OP_END]+ =+ {+ [INTEL_ASM_OP_RET]="RET",+ [INTEL_ASM_OP_NOP]="NOP",+ };+ wonky_assert(map[instruction->type]!=NULL);+ fprintf(out,"%s\n",map[instruction->type]);++ }+ #endifF diff --git a/src/backend/asm/intel/intel_instruction.h b/src/backend/asm/intel/intel_instruction.h new file mode 100644 --- /dev/null +++ b/src/backend/asm/intel/intel_instruction.h+ #ifndef WONKY_INTEL_ASM_INSTRUCTION_H+ #define WONKY_INTEL_ASM_INSTRUCTION_H WONKY_INTEL_ASM_INSTRUCTION_H+ #include <intel_instruction.hh>++ #include <intel_asm.h>+ #include <stdio.h>+ #include <stdlib.h>++ struct Intel_Asm_Instruction+ {+ enum Intel_Asm_Instruction_Type type;+ };+ struct Intel_Asm_Label+ {+ enum Intel_Asm_Instruction_Type type;+ char *label_name;+ };+ struct Intel_Asm_Instruction_Binary+ {+ enum Intel_Asm_Instruction_Type type;++ struct Intel_Asm_Memory_Location *left;+ struct Intel_Asm_Memory_Location *right;+ };+ struct Intel_Asm_Instruction_Unary+ {+ enum Intel_Asm_Instruction_Type type;++ struct Intel_Asm_Memory_Location *operand;+ };+ struct Intel_Asm_Instruction_Jump+ {+ enum Intel_Asm_Instruction_Type type;++ struct Intel_Asm_Label *where_to;+ };++ struct Intel_Asm_Instruction* get_intel_asm_label(char *label);+ struct Intel_Asm_Instruction* get_intel_asm_new_unique_label(struct Compile_Data_Intel_Asm *compile_data);+ struct Intel_Asm_Instruction* get_intel_asm_binary_instruction(struct Intel_Asm_Memory_Location *left,struct Intel_Asm_Memory_Location *right,enum Intel_Asm_Instruction_Type type);+ struct Intel_Asm_Instruction* get_intel_asm_unary_instruction(struct Intel_Asm_Memory_Location *operand,enum Intel_Asm_Instruction_Type type);+ struct Intel_Asm_Instruction* get_intel_asm_jump_instruction(struct Intel_Asm_Label *where_to,enum Intel_Asm_Instruction_Type type);+ struct Intel_Asm_Instruction* get_intel_asm_simple_instruction(enum Intel_Asm_Instruction_Type type);++ struct Intel_Asm_Instruction* intel_asm_get_push_ax();+ struct Intel_Asm_Instruction* intel_asm_get_pop_ax();+ struct Intel_Asm_Instruction* intel_asm_get_pop_dx();++ void save_intel_asm_instruction(struct Intel_Asm_Instruction *instruction,FILE *out);+ void save_intel_asm_label(struct Intel_Asm_Label *label,FILE *out);+ void save_intel_asm_binary_instruction(struct Intel_Asm_Instruction_Binary *bin,FILE *out);+ void save_intel_asm_unary_instruction(struct Intel_Asm_Instruction_Unary *unary,FILE *out);+ void save_intel_asm_jump_instruction(struct Intel_Asm_Instruction_Jump *jmp,FILE *out);+ void save_intel_asm_simple_instruction(struct Intel_Asm_Instruction *instruction,FILE *out);++ #endifF diff --git a/src/backend/asm/intel/intel_instruction.hh b/src/backend/asm/intel/intel_instruction.hh new file mode 100644 --- /dev/null +++ b/src/backend/asm/intel/intel_instruction.hh+ #ifndef WONKY_INTEL_ASM_INSTRUCTION_HH+ #define WONKY_INTEL_ASM_INSTRUCTION_HH WONKY_INTEL_ASM_INSTRUCTION_HH++ enum Intel_Asm_Instruction_Type+ {+ INTEL_ASM_OP_ADD,+ INTEL_ASM_OP_SUB,+ INTEL_ASM_OP_MOV,+ INTEL_ASM_OP_MUL,+ INTEL_ASM_OP_JMP,+ INTEL_ASM_OP_TEST,+ INTEL_ASM_OP_JE,+ INTEL_ASM_OP_JL,+ INTEL_ASM_OP_JNZ,+ INTEL_ASM_OP_JZ,+ INTEL_ASM_OP_RET,+ INTEL_ASM_OP_CALL,+ INTEL_ASM_OP_AND,+ INTEL_ASM_OP_XOR,+ INTEL_ASM_OP_OR,+ INTEL_ASM_OP_NOP,+ INTEL_ASM_OP_LABEL,+ INTEL_ASM_OP_POP,+ INTEL_ASM_OP_PUSH,+ INTEL_ASM_OP_END+ };++ struct Intel_Asm_Instruction;+ struct Intel_Asm_Label;+ struct Intel_Asm_Instruction_Binary;+ struct Intel_Asm_Instruction_Unary;+ struct Intel_Asm_Instruction_Jump;+++ #endifF diff --git a/src/backend/asm/intel/intel_location.c b/src/backend/asm/intel/intel_location.c new file mode 100644 --- /dev/null +++ b/src/backend/asm/intel/intel_location.c+ #ifndef WONKY_INTEL_ASM_LOCATION_C+ #define WONKY_INTEL_ASM_LOCATION_C WONKY_INTEL_ASM_LOCATION_C+ #include <intel_location.h>++ struct Intel_Asm_Memory_Location* get_intel_by_asm_register(enum Intel_Asm_Registers reg)+ {+ struct Intel_Asm_Memory_Location_By_Register *ret;+ ret=malloc(sizeof(struct Intel_Asm_Memory_Location_By_Register));+ ret->type=INTEL_ASM_MEMORY_LOCATION_BY_REGISTER;+ ret->reg=reg;++ return (struct Intel_Asm_Memory_Location*)ret;+ }+ struct Intel_Asm_Memory_Location* get_intel_asm_register(enum Intel_Asm_Registers reg)+ {+ struct Intel_Asm_Memory_Location_Register *ret;+ ret=malloc(sizeof(struct Intel_Asm_Memory_Location_Register));+ ret->type=INTEL_ASM_MEMORY_LOCATION_REGISTER;+ ret->reg=reg;++ return (struct Intel_Asm_Memory_Location*)ret;+ }+ struct Intel_Asm_Memory_Location* get_intel_asm_label_location(struct Intel_Asm_Label *label)+ {+ struct Intel_Asm_Memory_Location_By_Label *ret;++ ret=malloc(sizeof(struct Intel_Asm_Memory_Location_By_Label));+ ret->type=INTEL_ASM_MEMORY_LOCATION_BY_LABEL;+ ret->label=label;++ return (struct Intel_Asm_Memory_Location*)ret;++ }+ struct Intel_Asm_Memory_Location* get_intel_asm_stack_offset(int offset)+ {+ struct Intel_Asm_Memory_Location_By_Stack_Offset *ret;+ ret=malloc(sizeof(struct Intel_Asm_Memory_Location_By_Stack_Offset));+ ret->type=INTEL_ASM_MEMORY_LOCATION_BY_STACK_OFFSET;+ ret->offset=offset;++ return (struct Intel_Asm_Memory_Location*)ret;+ }+ struct Intel_Asm_Memory_Location* get_intel_asm_in_instruction_number(int number)+ {++ struct Intel_Asm_Memory_Location_In_Instruction_Number *ret;+ ret=malloc(sizeof(struct Intel_Asm_Memory_Location_In_Instruction_Number));+ ret->type=INTEL_ASM_MEMORY_LOCATION_IN_INSTRUCTION_NUMBER;+ ret->number=number;++ return (struct Intel_Asm_Memory_Location*)ret;++ }++ struct Intel_Asm_Memory_Location* intel_asm_get_ax_register()+ {+ return get_intel_asm_register(INTEL_ASM_REGISTER_AX);+ }+ struct Intel_Asm_Memory_Location* intel_asm_get_dx_register()+ {+ return get_intel_asm_register(INTEL_ASM_REGISTER_DX);+ }+ void save_intel_asm_label_location(struct Intel_Asm_Memory_Location_By_Label *label,FILE *out)+ {+ fprintf(out,"%s",label->label->label_name);+ }+ void save_intel_asm_stack_offset_location(struct Intel_Asm_Memory_Location_By_Stack_Offset *sp,FILE *out)+ {+ fprintf(out,"[SP+%d]",sp->offset);+ }+ void save_intel_asm_by_register_location(struct Intel_Asm_Memory_Location_By_Register *reg,FILE *out)+ {+ static const char *map[INTEL_ASM_REGISTER_END]+ =+ {+ [INTEL_ASM_REGISTER_AX]="AX",+ [INTEL_ASM_REGISTER_BX]="BX",+ [INTEL_ASM_REGISTER_DX]="DX",+ [INTEL_ASM_REGISTER_DI]="DI",+ [INTEL_ASM_REGISTER_SI]="SI",+ [INTEL_ASM_REGISTER_CX]="CX",+ [INTEL_ASM_REGISTER_SP]="SP",+ };++ wonky_assert(map[reg->reg]!=NULL);+ fprintf(out,"[%s]",map[reg->reg]);+ }+ void save_intel_asm_register_location(struct Intel_Asm_Memory_Location_Register *reg,FILE *out)+ {++ static const char *map[INTEL_ASM_REGISTER_END]+ =+ {+ [INTEL_ASM_REGISTER_AX]="AX",+ [INTEL_ASM_REGISTER_BX]="BX",+ [INTEL_ASM_REGISTER_DX]="DX",+ [INTEL_ASM_REGISTER_DI]="DI",+ [INTEL_ASM_REGISTER_SI]="SI",+ [INTEL_ASM_REGISTER_CX]="CX",+ [INTEL_ASM_REGISTER_SP]="SP",+ };++ wonky_assert(map[reg->reg]!=NULL);+ fprintf(out,"%s",map[reg->reg]);+ }+ void save_intel_asm_location(struct Intel_Asm_Memory_Location *location,FILE *out)+ {+ typedef void (*map_entry)(void *,FILE *out);+ static const map_entry map[INTEL_ASM_MEMORY_LOCATION_END]+ =+ {+ [INTEL_ASM_MEMORY_LOCATION_BY_REGISTER]=(map_entry)save_intel_asm_by_register_location,+ [INTEL_ASM_MEMORY_LOCATION_REGISTER]=(map_entry)save_intel_asm_register_location,+ [INTEL_ASM_MEMORY_LOCATION_BY_LABEL]=(map_entry)save_intel_asm_label_location,+ [INTEL_ASM_MEMORY_LOCATION_BY_STACK_OFFSET]=(map_entry)save_intel_asm_stack_offset_location,+ [INTEL_ASM_MEMORY_LOCATION_IN_INSTRUCTION_NUMBER]=(map_entry)save_intel_asm_number_in_instruction,+ };+ wonky_assert(map[location->type]!=NULL);+ map[location->type](location,out);+ }+ void save_intel_asm_number_in_instruction(struct Intel_Asm_Memory_Location_In_Instruction_Number *num,FILE *out)+ {+ fprintf(out,"%d",num->number);+ }+ #endifF diff --git a/src/backend/asm/intel/intel_location.h b/src/backend/asm/intel/intel_location.h new file mode 100644 --- /dev/null +++ b/src/backend/asm/intel/intel_location.h+ #ifndef WONKY_INTEL_ASM_LOCATION_H+ #define WONKY_INTEL_ASM_LOCATION_H WONKY_INTEL_ASM_LOCATION_H+ #include <intel_location.hh>+ #include <intel_asm.h>+ #include <stdio.h>+ #include <stdlib.h>+++++ struct Intel_Asm_Memory_Location+ {+ enum Intel_Asm_Memory_Location_Type type;+ };+ struct Intel_Asm_Memory_Location_By_Register+ {+ enum Intel_Asm_Memory_Location_Type type;+ enum Intel_Asm_Registers reg;+ };+ struct Intel_Asm_Memory_Location_Register+ {+ enum Intel_Asm_Memory_Location_Type type;+ enum Intel_Asm_Registers reg;+ };+ struct Intel_Asm_Memory_Location_By_Label+ {+ enum Intel_Asm_Memory_Location_Type type;+ struct Intel_Asm_Label *label;+ };+ struct Intel_Asm_Memory_Location_By_Stack_Offset+ {+ enum Intel_Asm_Memory_Location_Type type;+ int offset;+ };+ struct Intel_Asm_Memory_Location_In_Instruction_Number+ {+ enum Intel_Asm_Memory_Location_Type type;+ int number;+ };++ void save_intel_asm_location(struct Intel_Asm_Memory_Location *location,FILE *out);+ void save_intel_asm_label_location(struct Intel_Asm_Memory_Location_By_Label *label,FILE *out);+ void save_intel_asm_stack_offset_location(struct Intel_Asm_Memory_Location_By_Stack_Offset *sp,FILE *out);+ void save_intel_asm_by_register_location(struct Intel_Asm_Memory_Location_By_Register *reg,FILE *out);+ void save_intel_asm_register_location(struct Intel_Asm_Memory_Location_Register *reg,FILE *out);+ void save_intel_asm_number_in_instruction(struct Intel_Asm_Memory_Location_In_Instruction_Number *num,FILE *out);++ struct Intel_Asm_Memory_Location* get_intel_asm_register(enum Intel_Asm_Registers reg);+ struct Intel_Asm_Memory_Location* get_intel_asm_by_register(enum Intel_Asm_Registers reg);+ struct Intel_Asm_Memory_Location* get_intel_asm_label_location(struct Intel_Asm_Label *label);+ struct Intel_Asm_Memory_Location* get_intel_asm_stack_offset(int offset);+ struct Intel_Asm_Memory_Location* get_intel_asm_in_instruction_number(int number);+ struct Intel_Asm_Memory_Location* intel_asm_get_ax_register();+ struct Intel_Asm_Memory_Location* intel_asm_get_dx_register();+ #endifF diff --git a/src/backend/asm/intel/intel_location.hh b/src/backend/asm/intel/intel_location.hh new file mode 100644 --- /dev/null +++ b/src/backend/asm/intel/intel_location.hh+ #ifndef WONKY_INTEL_ASM_LOCATION_HH+ #define WONKY_INTEL_ASM_LOCATION_HH WONKY_INTEL_ASM_LOCATION_HH++ enum Intel_Asm_Registers+ {+ INTEL_ASM_REGISTER_AX,+ INTEL_ASM_REGISTER_BX,+ INTEL_ASM_REGISTER_DX,+ INTEL_ASM_REGISTER_DI,+ INTEL_ASM_REGISTER_SI,+ INTEL_ASM_REGISTER_CX,+ INTEL_ASM_REGISTER_SP,+ INTEL_ASM_REGISTER_END+ };++ enum Intel_Asm_Memory_Location_Type+ {+ INTEL_ASM_MEMORY_LOCATION_BY_REGISTER,+ INTEL_ASM_MEMORY_LOCATION_REGISTER,+ INTEL_ASM_MEMORY_LOCATION_BY_LABEL,+ INTEL_ASM_MEMORY_LOCATION_BY_STACK_OFFSET,+ INTEL_ASM_MEMORY_LOCATION_IN_INSTRUCTION_NUMBER,+ INTEL_ASM_MEMORY_LOCATION_END+ };++ struct Intel_Asm_Memory_Location;+ struct Intel_Asm_Memory_Location_By_Register;+ struct Intel_Asm_Memory_Location_Register;+ struct Intel_Asm_Memory_Location_By_Label;+ struct Intel_Asm_Memory_Location_By_Stack_Offset;+ struct Intel_Asm_Memory_Location_In_Instruction_Number;++ struct Intel_Asm_Memory_Location;+ #endifF diff --git a/src/backend/compile.h b/src/backend/compile.h --- a/src/backend/compile.h +++ b/src/backend/compile.h#define WONKY_COMPILE_H WONKY_COMPILE_H#include <compile.hh>#include <program.h>+ #include <intel_asm.h>#include <print.h>#include <stdio.h>+ #include <queue.h>struct Compile_Data{enum Compilation_Type type;+ struct Queue *errors;};struct Compiled_Object{enum Compilation_Type type;+ struct Queue *errors;};struct Compiled_Object* compile_program(struct Program *program,enum Compilation_Type compile_to_what);void save_compiled_object(struct Compiled_Object *object,FILE *out);-#endifF diff --git a/src/backend/compile.hh b/src/backend/compile.hh --- a/src/backend/compile.hh +++ b/src/backend/compile.hhenum Compilation_Type{COMPILE_TO_PRINT,+ COMPILE_TO_INTEL_ASM,};struct Compile_Data;- struct Compile_Data_Print;struct Compiled_Object;- struct Compiled_Object_Print;#endifF diff --git a/src/backend/text/print/print.c b/src/backend/text/print/print.c --- a/src/backend/text/print/print.c +++ b/src/backend/text/print/print.cret->type=COMPILE_TO_PRINT;ret->indent=0;- ret->lines=malloc(sizeof(struct Queue*));+ ret->lines=malloc(sizeof(struct Queue));Queue_Init(ret->lines);+ ret->errors=malloc(sizeof(struct Queue));+ Queue_Init(ret->errors);+return ret;}F diff --git a/src/backend/text/print/print.h b/src/backend/text/print/print.h --- a/src/backend/text/print/print.h +++ b/src/backend/text/print/print.h#ifndef WONKY_PRINT_H#define WONKY_PRINT_H WONKY_PRINT_H+#include <print.hh>#include <stdio.h>#include <wonky_assert.h>#include <common.h>#include <compile.h>#include <lines.h>+ #include <queue.h>+ #include <object.h>+ #include <value.h>#define ASTPTR(s) ((struct AST*)(s))struct Compile_Data_Print{enum Compilation_Type type;+ struct Queue *errors;+struct Queue *lines;/*queue of struct Lines*/int indent;};struct Compiled_Object_Print{enum Compilation_Type type;+ struct Queue *errors;+struct Queue *lines;+++};struct Compiled_Object_Print* compile_program_for_print(struct Program *program);F diff --git a/src/debug/debug_ast.c b/src/debug/debug_ast.c --- a/src/debug/debug_ast.c +++ b/src/debug/debug_ast.c}_Bool is_valid_ast_expression_enum(enum AST_Type ast_type){- return ast_type>AST_TYPE_EXPRESSION_START && ast_type<AST_TYPE_EXPRESSION_END;+ return (ast_type>AST_TYPE_EXPRESSION_START && ast_type<AST_TYPE_EXPRESSION_END) || ast_type==ERROR;}_Bool is_valid_ast(struct AST *ast){F diff --git a/src/debug/wobler/wobler_tests.h b/src/debug/wobler/wobler_tests.h --- a/src/debug/wobler/wobler_tests.h +++ b/src/debug/wobler/wobler_tests.h.how_much_time_should_execution_take=0.01,},{+ .filenames={"test_undeclared_error.c"},+ .test_function=should_not_compile,+ .how_much_time_should_execution_take=0.01,+ },+ {.filenames={"test_declaration.c"},.test_function=should_compile,.how_much_time_should_execution_take=0.01,F diff --git a/src/program/gcc_arguments.c b/src/program/gcc_arguments.c --- a/src/program/gcc_arguments.c +++ b/src/program/gcc_arguments.c#ifndef WONKY_ARGUMENTS_C#define WONKY_ARGUMENTS_C WONKY_ARGUMENTS_C- #include<gcc_arguments.h>+ #include <gcc_arguments.h>struct Queue *source_names;ret=malloc(sizeof(struct Command_Arguments));- ret->print_ast=ret->print_tokens=ret->transpile_to_js=ret->is_quiet=0;+ ret->print_ast=ret->print_tokens=ret->transpile_to_js=ret->is_quiet=ret->compile_to_intel_asm=0;ret->output_file=ret->javascript_extern_file=NULL;source_names=malloc(sizeof(struct Queue));}else if(gstr_cmp(*argv,"--transpile_to_js") || gstr_cmp(*argv,"-js")){ret->transpile_to_js=1;+ }else if(gstr_cmp(*argv,"--compile-asm") || gstr_cmp(*argv,"-asm"))+ {+ ret->compile_to_intel_asm=1;}else if(gstr_cmp(*argv,"-o")){++argv;F diff --git a/src/program/gcc_arguments.h b/src/program/gcc_arguments.h --- a/src/program/gcc_arguments.h +++ b/src/program/gcc_arguments.hchar is_quiet:1;char transpile_to_js:1;char insert_html:1;+ char compile_to_intel_asm:1;FILE* output_file;F diff --git a/src/program/gcc_error.c b/src/program/gcc_error.c --- a/src/program/gcc_error.c +++ b/src/program/gcc_error.cQueue_Push(translation_data->errors,hold_message);}+ void push_compile_error(const char *message,struct Compile_Data *compile_data)+ {+ Queue_Push(compile_data->errors,gstr_to_heap(message));+ }+ void push_compile_note(const char *message,struct Compile_Data *compile_data)+ {+ Queue_Push(compile_data->errors,gstr_to_heap(message));+ }char* get_string_for_type_error(struct Type *type,struct Translation_Data *translation_data){char *ret;{free(translation_error);}+ void print_compile_errors(FILE *out,struct Queue *errors)+ {+ while(errors->size>0)+ fputs(Queue_Pop(errors),out);+ }#endifF diff --git a/src/program/gcc_error.h b/src/program/gcc_error.h --- a/src/program/gcc_error.h +++ b/src/program/gcc_error.h#include <queue.h>#include <gcc_string.h>#include <common.h>+ #include <compile.h>struct Translation_Messagevoid push_raw_translation_error(const char *error_message,size_t line,size_t column,const char *filename,struct Translation_Data *translation_data);+ void push_compile_error(const char *message,struct Compile_Data *compile_data);+ void push_compile_note(const char *message,struct Compile_Data *compile_data);+char* get_string_for_type_error(struct Type *type,struct Translation_Data *translation_data);char* get_string_for_denoted_error(struct Denoted *denoted,struct Translation_Data *translation_data);char* get_string_for_token_error(struct token *token,struct Translation_Data *translation_data);void print_translation_error(FILE *out,struct Translation_Message *message);void delete_translation_error(struct Translation_Message *message);++ void print_compile_errors(FILE *out,struct Queue *errors);+++/*=====================================================================================================================F diff --git a/src/semantics/ast.c b/src/semantics/ast.c --- a/src/semantics/ast.c +++ b/src/semantics/ast.chold_denoted=check_ordinary(scope,id);if(hold_denoted==NULL){- push_translation_error("using undeclared id in expression",translation_data);+ push_translation_error("using undeclared id - %t, in expression",translation_data,id);free(ret);return (struct AST_Designator*)get_error_tree(NULL);}elseF diff --git a/src/wonky.c b/src/wonky.c --- a/src/wonky.c +++ b/src/wonky.c{//transpile_to_javascript(command_arguments->output_file,program,command_arguments);fprintf(stderr,"JS transpilation is not currently supported\n");+ }else if(command_arguments->compile_to_intel_asm)+ {+ struct Compiled_Object_Intel_Asm *object;+ object=compile_program_to_intel_asm(program);+ save_compiled_intel_asm(object,stdout);}}F diff --git a/tests/test_undeclared_error.c b/tests/test_undeclared_error.c new file mode 100644 --- /dev/null +++ b/tests/test_undeclared_error.c+ int main()+ {+ x+a+b;+ }