Added body of some of the functions (not compiling) #t51
This commit is contained in:
@ -1,85 +1,113 @@
|
|||||||
|
#include <stdio.h>
|
||||||
#include "intermediate_code.h"
|
#include "intermediate_code.h"
|
||||||
|
#include "symbol_table.h"
|
||||||
|
|
||||||
|
Instruction * begin;
|
||||||
start = current = NULL;
|
Instruction * current;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: this is here to bring your attention to the comment bellow.
|
// 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
|
// 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.
|
// 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){
|
void emit_binary_op(char* result, Op op, char* arg1, char* arg2){
|
||||||
Instruction * inst = calloc(1, sizeof(*inst));
|
emit_helper();
|
||||||
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->opcode = op;
|
||||||
current->result = look_up(result);
|
current->result = look_up(cur, result);
|
||||||
current->operand1 = look_up(arg1);
|
current->operand1 = look_up(cur, arg1);
|
||||||
current->operand2 = look_up(arg2);
|
current->operand2 = look_up(cur, arg2);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void emit_unary_op(char* result, Oo op, char* arg){
|
|
||||||
Instruction * inst = calloc(1, sizeof(*inst));
|
void emit_unary_op(char* result, Op op, char* arg){
|
||||||
if(start == NULL){
|
emit_helper();
|
||||||
start = current = inst;
|
|
||||||
current->index = 1;
|
|
||||||
} else {
|
|
||||||
current->next = inst;
|
|
||||||
inst->prev = current;
|
|
||||||
inst->index = current->index++;
|
|
||||||
current = inst;
|
|
||||||
}
|
|
||||||
current->opcode = op;
|
current->opcode = op;
|
||||||
current->result = look_up(result);
|
current->result = look_up(cur, result);
|
||||||
current->operand1 = look_up(arg);
|
current->operand1 = look_up(cur, arg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_assignment(char* target, char* source){
|
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){
|
void emit_as_file(FILE * out_file, Instruction * instr_arr){
|
||||||
|
if(instr_arr == NULL){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//fprintf(out_file,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void emit_label(char* label){
|
void emit_label(char* label){
|
||||||
|
emit_helper();
|
||||||
|
current->opcode = LABEL;
|
||||||
|
current->label = label;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void emit_jump(char* label){
|
void emit_jump(char* label){
|
||||||
|
emit_helper();
|
||||||
|
current->opcode = GOTO;
|
||||||
|
current->label = label;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void emit_conditional_jump(char* condition, char* label){
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void emit_function_start(char* name){
|
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;
|
return;
|
||||||
}
|
}
|
||||||
void emit_parameter(char* param){
|
void emit_parameter(char* param){
|
||||||
|
emit_helper();
|
||||||
|
current->opcode = PARAM;
|
||||||
|
current->operand1 = look_up(cur, param);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void emit_function_call(char* result, char* name){
|
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;
|
return;
|
||||||
}
|
}
|
||||||
void emit_return(char* value){
|
void emit_return(char* value){
|
||||||
|
emit_helper();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void emit_reserve(char* result, char* type_name, int size){
|
void emit_reserve(char* result, char* type_name, int size){
|
||||||
|
emit_helper();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void emit_release(char* pointer){
|
void emit_release(char* pointer){
|
||||||
|
emit_helper();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,24 +12,48 @@
|
|||||||
// - Subtraction, multiplication, division, modulo
|
// - Subtraction, multiplication, division, modulo
|
||||||
#include "symbol_table.h"
|
#include "symbol_table.h"
|
||||||
|
|
||||||
typedef enum {ADD, SUB, MUL, DIV} Op; // TODO: add all the instructions
|
// these are from page 364
|
||||||
typedef struct {
|
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;
|
Op opcode;
|
||||||
TableNode * result;
|
TableNode * result;
|
||||||
TableNode * operand1;
|
TableNode * operand1;
|
||||||
TableNode * operand2;
|
TableNode * operand2;
|
||||||
int label;
|
char * label;
|
||||||
|
|
||||||
|
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
|
|
||||||
Instruction * prev;
|
Instruction * prev;
|
||||||
Instruction * next;
|
Instruction * next;
|
||||||
} Instruction;
|
} Instruction;
|
||||||
|
|
||||||
Instruction * start;
|
extern Instruction * begin;
|
||||||
Instruction * current;
|
extern Instruction * current;
|
||||||
|
|
||||||
|
|
||||||
void emit_binary_op(char* result, char* op, char* arg1, char* arg2);
|
void emit_binary_op(char* result, Op op, char* arg1, char* arg2);
|
||||||
void emit_unary_op(char* result, char* op, char* arg);
|
void emit_unary_op(char* result, Op op, char* arg);
|
||||||
void emit_assignment(char* target, char* source);
|
void emit_assignment(char* target, char* source);
|
||||||
// TODO: Find out what these are suposed to do. Guess is create an entry in
|
// 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
|
// the list of instructions. Guess is that its suposed to ret a struct ptr
|
||||||
|
Reference in New Issue
Block a user