// Track 1: Core Infrastructure & Basic Expressions // * Add Bison actions for arithmetic expressions: // - Addition: $$ = new_temp(); emit_binary_op($$, "ADD", $1, $3); // - Subtraction, multiplication, division, modulo #include "symbol_table.h" #include // these are from page 364 typedef enum { E_LABEL = 10000, // this is not in the book E_ADD, // 1 from the list E_SUB, // 1 E_MUL, // 1 E_DIV, // 1 E_MOD, // 1 E_OR, // 1 E_AND, // 1 E_NEG, // 2 E_NOT, // 2 E_ASSIGN, // 3 E_GOTO, // 4 E_COND_GOTO, // 5 I don't thik I need this because we could just follow the < or the = and just assume that it's a cond got E_IF_X_TRUE, // 5 E_IF_X_FALSE, // 5 E_LESSTHEN, // 6 rule 1 + 5 E_EQUALTO, // 6 rule 1 + 5 E_CALL, // 7 E_PARAM, // 7 E_RETURN, // 7 E_INDEX_COPY_RIGHT, // 8 E_INDEX_COPY_LEFT, // 8 E_ADDRESS_OF // 9 /* for x = *y and *y = x we can just use index copy right and left with index 0*/ } Op; typedef enum { NODE = 11000, // TableNode INTEGER, // int STRING, // char * CHARACTER, // char ADDRESS, // void * BOOLEAN // bool } Discriminant; typedef union { TableNode * node; int integer; char * string; char character; void * address; bool Boolean; } RegConstUnion; typedef struct { Discriminant d; RegConstUnion * rc_union; } TNodeOrConst; typedef struct Instruction Instruction; typedef struct Instruction { Op opcode; TableNode * result; TNodeOrConst * operand1; TableNode * operand2; char * label; int index; Instruction * prev; Instruction * next; } Instruction; extern Instruction * begin; extern Instruction * current; int temp_count = 0; int label_count = 0; bool code_gen = true; void emit_binary_op(Op op, TableNode * result, TNodeOrConst * arg1, TableNode * arg2); void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg); void emit_assignment(TableNode * target, TNodeOrConst * source); // TODO: Find out what these are suposed to do. Guess is create an entry in // the list of instructions. Guess is that its suposed to ret a struct ptr // * Implement integer/boolean/character specific operation handling // TODO: Find out what this means. // * Create output function to write instructions to file with line formatting void emit_as_file(FILE * out_file, Instruction * instr_arr); // * Implement instruction array storage for backpatching void emit_label(char* label); void emit_jump(char* label); void emit_conditional_jump(int count, ...); void emit_function_start(char* name); void emit_parameter(TNodeOrConst * param); void emit_function_call(TNodeOrConst * result, TNodeOrConst * name); void emit_return(TNodeOrConst * value); void emit_reserve(char* result, char* type_name, int size); void emit_release(char* pointer); void emit_field_access(char* result, char* record, char* field); void emit_array_access(char* result, char* array, char* index, char* dimension); void emit_bounds_check(char* index, char* size, char* error_label);