diff --git a/src/intermediate_code.c b/src/intermediate_code.c index 2d50a34..24f370c 100644 --- a/src/intermediate_code.c +++ b/src/intermediate_code.c @@ -1,3 +1,7 @@ +#include "intermediate_code.h" + + +start = current = NULL; @@ -7,11 +11,38 @@ - void emit_binary_op(char* result, char* op, char* arg1, char* arg2){ - return; + 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; + } + current->opcode = op; + current->result = look_up(result); + current->operand1 = look_up(arg1); + current->operand2 = look_up(arg2); + return; } - void emit_unary_op(char* result, char* op, char* arg){ - 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; + } + current->opcode = op; + current->result = look_up(result); + current->operand1 = look_up(arg); + return; } void emit_assignment(char* target, char* source){ return; diff --git a/src/intermediate_code.h b/src/intermediate_code.h index 1ccd389..fdd0aff 100644 --- a/src/intermediate_code.h +++ b/src/intermediate_code.h @@ -19,13 +19,14 @@ typedef struct { TableNode * operand1; TableNode * operand2; int label; - int instruction; + int index; Instruction * prev; Instruction * next; } Instruction; -Instruction * start = NULL; -Instruction * current = NULL; +Instruction * start; +Instruction * current; + void emit_binary_op(char* result, char* op, char* arg1, char* arg2); void emit_unary_op(char* result, char* op, char* arg);