diff --git a/src/intermediate_code.c b/src/intermediate_code.c index 24f370c..38cbe10 100644 --- a/src/intermediate_code.c +++ b/src/intermediate_code.c @@ -1,85 +1,113 @@ +#include #include "intermediate_code.h" +#include "symbol_table.h" - -start = current = NULL; - +Instruction * begin; +Instruction * current; // TODO: this is here to bring your attention to the comment bellow. // check if start is NULL if it is assign it to the start globle variable // otherwise make it next of current and set cur to your instruction. - +void emit_helper(void){ + Instruction * inst = calloc(1, sizeof(*inst)); + if(begin == NULL){ + begin = current = inst; + current->index = 1; + } else { + current->next = inst; + inst->prev = current; + inst->index = current->index++; + current = inst; + } +} void emit_binary_op(char* result, Op op, char* arg1, char* arg2){ - Instruction * inst = calloc(1, sizeof(*inst)); - if(start == NULL){ - start = current = inst; - current->index = 1; - } else { - current->next = inst; - inst->prev = current; - inst->index = current->index++; - current = inst; - } + emit_helper(); current->opcode = op; - current->result = look_up(result); - current->operand1 = look_up(arg1); - current->operand2 = look_up(arg2); + current->result = look_up(cur, result); + current->operand1 = look_up(cur, arg1); + current->operand2 = look_up(cur, arg2); return; } - void emit_unary_op(char* result, Oo op, char* arg){ - Instruction * inst = calloc(1, sizeof(*inst)); - if(start == NULL){ - start = current = inst; - current->index = 1; - } else { - current->next = inst; - inst->prev = current; - inst->index = current->index++; - current = inst; - } + + void emit_unary_op(char* result, Op op, char* arg){ + emit_helper(); current->opcode = op; - current->result = look_up(result); - current->operand1 = look_up(arg); + current->result = look_up(cur, result); + current->operand1 = look_up(cur, arg); return; } + void emit_assignment(char* target, char* source){ - return; + emit_helper(); + current->opcode = ADD; // TODO: replace with move + current->result = look_up(cur, target); + current->operand1 = look_up(cur, source); + return; } + void emit_as_file(FILE * out_file, Instruction * instr_arr){ + if(instr_arr == NULL){ + return; + } + + //fprintf(out_file, return; } void emit_label(char* label){ + emit_helper(); + current->opcode = LABEL; + current->label = label; return; } void emit_jump(char* label){ + emit_helper(); + current->opcode = GOTO; + current->label = label; return; } void emit_conditional_jump(char* condition, char* label){ + emit_helper(); + current->opcode = CGOTO; // I am positive this needs to be 2 instructions + current->label = label; return; } void emit_function_start(char* name){ + emit_helper(); // actualy what is this? + current->opcode = LABLE; // I think this is right TODO: ask + current->label = name; return; } void emit_parameter(char* param){ + emit_helper(); + current->opcode = PARAM; + current->operand1 = look_up(cur, param); return; } void emit_function_call(char* result, char* name){ + emit_helper(); + current->opcode = CALL; + current->operand1 = look_up(cur, name); + current->result = look_up(cur, result); return; } void emit_return(char* value){ + emit_helper(); return; } void emit_reserve(char* result, char* type_name, int size){ + emit_helper(); return; } void emit_release(char* pointer){ + emit_helper(); return; } diff --git a/src/intermediate_code.h b/src/intermediate_code.h index fdd0aff..464d331 100644 --- a/src/intermediate_code.h +++ b/src/intermediate_code.h @@ -12,24 +12,48 @@ // - Subtraction, multiplication, division, modulo #include "symbol_table.h" -typedef enum {ADD, SUB, MUL, DIV} Op; // TODO: add all the instructions -typedef struct { +// these are from page 364 +typedef enum { + LABEL, // this is not in the book + ADD, // 1 from the list + SUB, // 1 + MUL, // 1 + DIV, // 1 + MOD, // 1 + OR, // 1 + AND, // 1 + NEG, // 2 + NOT, // 2 + ASSIGN, // 3 + GOTO, // 4 + CGOTO, // 5 + LESSTHEN, // 6 rule 1 + 5 + EQUALTO, // 6 rule 1 + 5 + PARAM, // 7 + CALL // 7 + +} Op; +typedef struct Instruction { Op opcode; TableNode * result; TableNode * operand1; TableNode * operand2; - int label; + char * label; + + int index; + + Instruction * prev; Instruction * next; } Instruction; -Instruction * start; -Instruction * current; +extern Instruction * begin; +extern Instruction * current; -void emit_binary_op(char* result, char* op, char* arg1, char* arg2); -void emit_unary_op(char* result, char* op, char* arg); +void emit_binary_op(char* result, Op op, char* arg1, char* arg2); +void emit_unary_op(char* result, Op op, char* arg); void emit_assignment(char* target, char* 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