IR stuff
This commit is contained in:
@ -3,43 +3,104 @@
|
|||||||
|
|
||||||
#include "intermediate_code.h"
|
#include "intermediate_code.h"
|
||||||
|
|
||||||
// TODO: this is here to bring your attention to the comment bellow.
|
Stack * S_Init(){
|
||||||
// check if start is NULL if it is assign it to the start globle variable
|
Stack * s = calloc(1, sizeof(*s));
|
||||||
// otherwise make it next of current and set cur to your instruction.
|
return s;
|
||||||
TNodeOrConst* getOperand1(Instruction* i) {
|
}
|
||||||
|
|
||||||
|
void S_Free(Stack *s){
|
||||||
|
// since we are not responsible for the values we can just pop until
|
||||||
|
// NULL
|
||||||
|
for (void * p = S_Pop(s); p != NULL; p = S_Pop(s));
|
||||||
|
free(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void S_Push(Stack * s, void *v) {
|
||||||
|
__Node * n = calloc(1, sizeof(*n));
|
||||||
|
n->v = v
|
||||||
|
n->next = s->n;
|
||||||
|
s->n = n;
|
||||||
|
s->size = s->size + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void * S_Pop(Stack *s) {
|
||||||
|
if (s->size == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
__Node * node = s->n;
|
||||||
|
s->n = node->next;
|
||||||
|
s->size = s->size - 1;
|
||||||
|
void * r = node->v;
|
||||||
|
free(node);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void * S_Peek(Stack *s){
|
||||||
|
if (!S_IsEmpty(s)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return s->n->v;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool S_IsEmpty(Stack *s){
|
||||||
|
if(!s->size) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int S_Size(Stack *s){
|
||||||
|
return s->size;
|
||||||
|
}
|
||||||
|
//_______________________________________________________________________
|
||||||
|
|
||||||
|
Instruction * begin;
|
||||||
|
Instruction * current;
|
||||||
|
char * temp = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
TNodeOrConst * getOperand1(Instruction * i){
|
||||||
return i->operand1;
|
return i->operand1;
|
||||||
}
|
}
|
||||||
|
|
||||||
TNodeOrConst* getOperand2(Instruction* i) {
|
TNodeOrConst * getOperand2(Instruction * i){
|
||||||
return i->operand2;
|
return i->operand2;
|
||||||
}
|
}
|
||||||
|
|
||||||
TableNode* getResult(Instruction* i) {
|
TableNode * getResult(Instruction * i){
|
||||||
return i->result;
|
return i->result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Op getOp(Instruction* i) {
|
Op getOp(Instruction * i){
|
||||||
return i->opcode;
|
return i->opcode;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getLabel(Instruction* i) {
|
int getLabel(Instruction * i){
|
||||||
return i->label;
|
return i->label;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_index(Instruction* i) {
|
int get_index(Instruction * i){
|
||||||
return i->index;
|
return i->index;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_label(Instruction* i, int label) {
|
void set_label(Instruction * i, int label){
|
||||||
i->label = label;
|
i->label = label;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isConst(TNodeOrConst* tnc) {
|
bool isConst(TNodeOrConst * tnc) {
|
||||||
return tnc->d != NODE;
|
return tnc->d != NODE;
|
||||||
}
|
}
|
||||||
|
|
||||||
TNodeOrConst* tn_or_const(Discriminant d, void* tnc) {
|
TNodeOrConst * tn_or_const(Discriminant d, void * tnc) {
|
||||||
TNodeOrConst* count = calloc(1, sizeof(*count));
|
TNodeOrConst * count = calloc(1, sizeof(*count));
|
||||||
count->d = d;
|
count->d = d;
|
||||||
count->tnc_union = calloc(1, sizeof(*count->tnc_union));
|
count->tnc_union = calloc(1, sizeof(*count->tnc_union));
|
||||||
switch (d) {
|
switch (d) {
|
||||||
@ -65,9 +126,9 @@ TNodeOrConst* tn_or_const(Discriminant d, void* tnc) {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void emit_helper(void) {
|
static void emit_helper(void){
|
||||||
Instruction* inst = calloc(1, sizeof(*inst));
|
Instruction * inst = calloc(1, sizeof(*inst));
|
||||||
if (begin == NULL) {
|
if(begin == NULL){
|
||||||
begin = current = inst;
|
begin = current = inst;
|
||||||
current->index = 1;
|
current->index = 1;
|
||||||
} else {
|
} else {
|
||||||
@ -78,31 +139,36 @@ static void emit_helper(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_binary_op(Op op, TableNode* result, TNodeOrConst* arg1, TNodeOrConst* arg2) {
|
void emit_binary_op(
|
||||||
|
Op op,
|
||||||
|
TableNode * result,
|
||||||
|
TNodeOrConst * arg1,
|
||||||
|
TNodeOrConst * arg2
|
||||||
|
){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode = op;
|
current->opcode = op;
|
||||||
// TODO: create temp and remove result from param list
|
// TODO: create temp and remove result from param list
|
||||||
current->result = result;
|
current->result = result;
|
||||||
current->operand1 = arg1;
|
current->operand1 = arg1;
|
||||||
current->operand2 = arg2;
|
current->operand2 = arg2;
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_unary_op(Op op, TableNode* result, TNodeOrConst* arg) {
|
void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode = op;
|
current->opcode = op;
|
||||||
current->result = result;
|
current->result = result;
|
||||||
current->operand1 = arg;
|
current->operand1 = arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_assignment(TableNode* target, TNodeOrConst* source) {
|
void emit_assignment(TableNode * target, TNodeOrConst * source){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode = E_ASSIGN;
|
current->opcode = E_ASSIGN;
|
||||||
current->result = target;
|
current->result = target;
|
||||||
current->operand1 = source;
|
current->operand1 = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* get_string(TNodeOrConst* tc) {
|
char * get_string(TNodeOrConst * tc){
|
||||||
char* s;
|
char * s;
|
||||||
switch (tc->d) {
|
switch (tc->d) {
|
||||||
case NODE:
|
case NODE:
|
||||||
return getName(tc->tnc_union->node);
|
return getName(tc->tnc_union->node);
|
||||||
@ -119,139 +185,26 @@ char* get_string(TNodeOrConst* tc) {
|
|||||||
sprintf(s, "%c", tc->tnc_union->character);
|
sprintf(s, "%c", tc->tnc_union->character);
|
||||||
return s;
|
return s;
|
||||||
case BOOLEAN:
|
case BOOLEAN:
|
||||||
if (tc->tnc_union->Boolean) {
|
if(tc->tnc_union->Boolean){
|
||||||
return strdup("true");
|
return strdup("true");
|
||||||
}
|
}
|
||||||
return strdup("false");
|
return strdup("false");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_as_file(FILE* out_file, Instruction* i) {
|
void emit_label(int label){
|
||||||
if (i == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
switch (i->opcode) {
|
|
||||||
case E_LABEL:
|
|
||||||
break;
|
|
||||||
// this is a terrible one to start with
|
|
||||||
// fprintf(out_file, "%04.d: %d ", i->index, i->label);
|
|
||||||
case E_ADD:
|
|
||||||
fprintf(out_file, "%4.d: %s = %s + %s\n",
|
|
||||||
i->index, getName(i->result),
|
|
||||||
get_string(i->operand1),
|
|
||||||
get_string(i->operand2));
|
|
||||||
break;
|
|
||||||
case E_SUB:
|
|
||||||
fprintf(out_file, "%4.d: %s = %s - %s\n",
|
|
||||||
i->index, getName(i->result),
|
|
||||||
get_string(i->operand1),
|
|
||||||
get_string(i->operand2));
|
|
||||||
break;
|
|
||||||
case E_MUL:
|
|
||||||
fprintf(out_file, "%4.d: %s = %s * %s\n",
|
|
||||||
i->index, getName(i->result),
|
|
||||||
get_string(i->operand1),
|
|
||||||
get_string(i->operand2));
|
|
||||||
break;
|
|
||||||
case E_DIV:
|
|
||||||
fprintf(out_file, "%4.d: %s = %s / %s\n",
|
|
||||||
i->index, getName(i->result),
|
|
||||||
get_string(i->operand1),
|
|
||||||
get_string(i->operand2));
|
|
||||||
break;
|
|
||||||
case E_MOD:
|
|
||||||
fprintf(out_file, "%4.d: %s = %s %% %s\n",
|
|
||||||
i->index, getName(i->result),
|
|
||||||
get_string(i->operand1),
|
|
||||||
get_string(i->operand2));
|
|
||||||
break;
|
|
||||||
case E_OR:
|
|
||||||
fprintf(out_file, "%4.d: %s = %s | %s\n",
|
|
||||||
i->index, getName(i->result),
|
|
||||||
get_string(i->operand1),
|
|
||||||
get_string(i->operand2));
|
|
||||||
break;
|
|
||||||
case E_AND:
|
|
||||||
fprintf(out_file, "%4.d: %s = %s & %s\n",
|
|
||||||
i->index, getName(i->result),
|
|
||||||
get_string(i->operand1),
|
|
||||||
get_string(i->operand2));
|
|
||||||
break;
|
|
||||||
case E_NEG:
|
|
||||||
fprintf(out_file, "%4.d: %s = -%s\n",
|
|
||||||
i->index, getName(i->result),
|
|
||||||
get_string(i->operand1));
|
|
||||||
break;
|
|
||||||
case E_NOT:
|
|
||||||
fprintf(out_file, "%4.d: %s = !%s\n",
|
|
||||||
i->index, getName(i->result),
|
|
||||||
get_string(i->operand1));
|
|
||||||
break;
|
|
||||||
case E_ASSIGN:
|
|
||||||
fprintf(out_file, "%4.d: %s = %s\n",
|
|
||||||
i->index, getName(i->result),
|
|
||||||
get_string(i->operand1));
|
|
||||||
break;
|
|
||||||
case E_GOTO:
|
|
||||||
// are we ever going to use this?
|
|
||||||
// yes we do look at bounds checking
|
|
||||||
case E_IF_X_TRUE:
|
|
||||||
fprintf(out_file, "%4.d: if %s goto %d\n",
|
|
||||||
i->index, get_string(i->operand1),
|
|
||||||
i->label);
|
|
||||||
break;
|
|
||||||
case E_IF_X_FALSE:
|
|
||||||
fprintf(out_file, "%4.d: if %s false goto %d\n",
|
|
||||||
i->index, get_string(i->operand1),
|
|
||||||
i->label);
|
|
||||||
break;
|
|
||||||
case E_LESS_THAN:
|
|
||||||
fprintf(out_file, "%4.d: if %s < %s goto %d\n",
|
|
||||||
i->index, get_string(i->operand1),
|
|
||||||
get_string(i->operand2), i->label);
|
|
||||||
break;
|
|
||||||
case E_EQUAL_TO:
|
|
||||||
fprintf(out_file, "%4.d: if %s = %s goto %d\n",
|
|
||||||
i->index, get_string(i->operand1),
|
|
||||||
get_string(i->operand2), i->label);
|
|
||||||
break;
|
|
||||||
case E_CALL:
|
|
||||||
fprintf(out_file, "%4.d: call %s %s\n",
|
|
||||||
i->index, get_string(i->operand1),
|
|
||||||
get_string(i->operand2));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case E_PARAM:
|
|
||||||
fprintf(out_file, "%4.d: param %s \n",
|
|
||||||
i->index, get_string(i->operand1));
|
|
||||||
break;
|
|
||||||
case E_RETURN:
|
|
||||||
|
|
||||||
case E_INDEX_COPY_RIGHT:
|
|
||||||
case E_INDEX_COPY_LEFT:
|
|
||||||
|
|
||||||
case E_ADDRESS_OF:
|
|
||||||
|
|
||||||
case E_DEREF_RIGHT:
|
|
||||||
case E_DEREF_LEFT:
|
|
||||||
}
|
|
||||||
|
|
||||||
emit_as_file(out_file, i->next);
|
|
||||||
}
|
|
||||||
|
|
||||||
void emit_label(int label) {
|
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode = E_LABEL;
|
current->opcode = E_LABEL;
|
||||||
current->label = label;
|
current->label = label;
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_jump(int label) {
|
void emit_jump(int label){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode = E_GOTO;
|
current->opcode = E_GOTO;
|
||||||
current->label = label;
|
current->label = label;
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_conditional_jump(Op condition, int label, ...) {
|
void emit_conditional_jump(Op condition, int label, ...){
|
||||||
// when this instruction is a conditional jump then the imput looks like (Op, int, TNodeOrConst *).
|
// when this instruction is a conditional jump then the imput looks like (Op, int, TNodeOrConst *).
|
||||||
// when the inst is a cond with a Relational operation then the input looks like (Op, int, TNodeOrConst *, TNodeOrConst *)
|
// when the inst is a cond with a Relational operation then the input looks like (Op, int, TNodeOrConst *, TNodeOrConst *)
|
||||||
emit_helper();
|
emit_helper();
|
||||||
@ -259,18 +212,16 @@ void emit_conditional_jump(Op condition, int label, ...) {
|
|||||||
va_start(argptr, label);
|
va_start(argptr, label);
|
||||||
current->opcode = condition;
|
current->opcode = condition;
|
||||||
current->label = label;
|
current->label = label;
|
||||||
TNodeOrConst* n1;
|
TNodeOrConst * n1;
|
||||||
TNodeOrConst* n2;
|
TNodeOrConst * n2;
|
||||||
switch (condition) {
|
switch (condition) {
|
||||||
case E_IF_X_TRUE:
|
case E_IF_X_TRUE: case E_IF_X_FALSE:
|
||||||
case E_IF_X_FALSE:
|
n1 = va_arg(argptr, TNodeOrConst *);
|
||||||
n1 = va_arg(argptr, TNodeOrConst*);
|
|
||||||
current->operand1 = n1;
|
current->operand1 = n1;
|
||||||
break;
|
break;
|
||||||
case E_LESS_THAN:
|
case E_LESS_THAN: case E_EQUAL_TO:
|
||||||
case E_EQUAL_TO:
|
n1 = va_arg(argptr, TNodeOrConst *);
|
||||||
n1 = va_arg(argptr, TNodeOrConst*);
|
n2 = va_arg(argptr, TNodeOrConst *);
|
||||||
n2 = va_arg(argptr, TNodeOrConst*);
|
|
||||||
current->operand1 = n1;
|
current->operand1 = n1;
|
||||||
current->operand2 = n2;
|
current->operand2 = n2;
|
||||||
break;
|
break;
|
||||||
@ -278,20 +229,23 @@ void emit_conditional_jump(Op condition, int label, ...) {
|
|||||||
va_end(argptr);
|
va_end(argptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_function_start(TNodeOrConst * name) {
|
void emit_function_start(TableNode * name){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode = E_LABEL; // I think this is right TODO: ask
|
current->opcode = E_FUNC_START;
|
||||||
current->operand1 = name;
|
current->result = name;
|
||||||
// this is probabaly a func declaration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_parameter(TNodeOrConst* param) {
|
void emit_parameter(TNodeOrConst * param){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode = E_PARAM;
|
current->opcode = E_PARAM;
|
||||||
current->operand1 = param;
|
current->operand1 = param;
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_function_call(TableNode* result, int param_count, TNodeOrConst* name) {
|
void emit_function_call(
|
||||||
|
TableNode * result,
|
||||||
|
int param_count,
|
||||||
|
TNodeOrConst * name
|
||||||
|
){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode = E_CALL;
|
current->opcode = E_CALL;
|
||||||
current->operand1 = tn_or_const(INTEGER, ¶m_count);
|
current->operand1 = tn_or_const(INTEGER, ¶m_count);
|
||||||
@ -299,44 +253,57 @@ void emit_function_call(TableNode* result, int param_count, TNodeOrConst* name)
|
|||||||
current->result = result;
|
current->result = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_return(TNodeOrConst* value) {
|
void emit_return(TNodeOrConst * value){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode = E_RETURN;
|
current->opcode = E_RETURN;
|
||||||
current->operand1 = value;
|
current->operand1 = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_reserve(TableNode* result, TNodeOrConst* size) {
|
void emit_reserve(TableNode * result, TNodeOrConst * size){
|
||||||
emit_parameter(size);
|
emit_parameter(size);
|
||||||
emit_function_call(result, 1, tn_or_const(NODE, look_up(cur, "reserve")));
|
emit_function_call(result, 1, tn_or_const(NODE, look_up(cur, "reserve")));
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_release(TableNode* pointer) {
|
void emit_release(TableNode * pointer){
|
||||||
emit_parameter(tn_or_const(NODE, pointer));
|
emit_parameter(tn_or_const(NODE, pointer));
|
||||||
emit_function_call(pointer, 1, tn_or_const(NODE, look_up(cur, "release")));
|
emit_function_call(pointer, 1, tn_or_const(NODE, look_up(cur, "release")));
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_deref_right() {
|
void emit_deref_right(TableNode * x, TNodeOrConst * y){
|
||||||
return;
|
emit_helper();
|
||||||
|
current->opcode = E_DEREF_RIGHT;
|
||||||
|
current->result = x;
|
||||||
|
current->operand1 = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_deref_left() {
|
void emit_deref_left(TableNode * x, TNodeOrConst * y){
|
||||||
return;
|
emit_helper();
|
||||||
|
current->opcode = E_DEREF_LEFT;
|
||||||
|
current->result = x;
|
||||||
|
current->operand1 = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_field_access(char* result, char* record, char* field) {
|
void emit_address_of(TableNode * x, TNodeOrConst * y){
|
||||||
|
emit_helper();
|
||||||
|
current->opcode = E_ADDRESS_OF;
|
||||||
|
current->result = x;
|
||||||
|
current->opernad1 = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void emit_field_access(char* result, char* record, char* field){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_array_access(Op op, TableNode* result, TNodeOrConst* array, TNodeOrConst* index) {
|
void emit_array_access(Op op, TableNode * result, TNodeOrConst * array, TNodeOrConst * index){
|
||||||
emit_helper();
|
emit_helper();
|
||||||
current->opcode;
|
current->opcode = op;
|
||||||
current->result = result;
|
current->result = result;
|
||||||
current->operand1 = array;
|
current->operand1 = array;
|
||||||
current->operand2 = index;
|
current->operand2 = index;
|
||||||
// TODO: Still don't know what to do with the dimentions
|
// TODO: Still don't know what to do with the dimentions
|
||||||
}
|
}
|
||||||
|
|
||||||
void emit_bounds_check(TNodeOrConst* index, TNodeOrConst* arr) {
|
void emit_bounds_check(TNodeOrConst * index, TNodeOrConst * arr){
|
||||||
/*
|
/*
|
||||||
{[string: 5]
|
{[string: 5]
|
||||||
.
|
.
|
||||||
@ -365,36 +332,241 @@ void emit_bounds_check(TNodeOrConst* index, TNodeOrConst* arr) {
|
|||||||
if t_0 < s._1 GOTO access array
|
if t_0 < s._1 GOTO access array
|
||||||
GOTO ERROR
|
GOTO ERROR
|
||||||
*/
|
*/
|
||||||
//emit_conditional_jump(E_LESS_THAN, );
|
|
||||||
//emit_conditional_jump(E_LESS_THAN, );
|
|
||||||
//emit_jump();
|
|
||||||
/* We need a label ERROR to jump to
|
/* We need a label ERROR to jump to
|
||||||
|
emit_conditional_jump(E_LESS_THAN, );
|
||||||
|
emit_conditional_jump(E_LESS_THAN, );
|
||||||
|
emit_jump();
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/*// * Implement temp variable generator function that produces unique names (t1, t2, etc.)
|
// * Implement temp variable generator function that produces unique names (t1, t2, etc.)
|
||||||
char * temp_var_gen(){
|
char * temp_var_gen(){
|
||||||
char * ret = calloc(9, sizeof(*ret));
|
char * ret = calloc(9, sizeof(*ret));
|
||||||
sprintf(ret, "$t%d", temp_count);
|
sprintf(ret, "$t%d", temp_count);
|
||||||
temp_count++;
|
temp_count++;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
int label_gen(){
|
char * label_gen(){
|
||||||
|
char * ret = calloc( 9, sizeof(*ret));
|
||||||
|
sprintf(ret, "L_%d", label_count);
|
||||||
label_count++;
|
label_count++;
|
||||||
return label_count;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
TableNode* getTN(TNodeOrConst* tnc) {
|
void emit_as_file(FILE * out_file, Instruction * i){
|
||||||
if (tnc->d == NODE) {
|
if(i == NULL){
|
||||||
return tnc->tnc_union->node;
|
return;
|
||||||
}
|
}
|
||||||
return NULL;
|
switch(i->opcode){
|
||||||
}
|
case E_FUNC_START:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: func : %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_LABEL:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: Label : %d\n",
|
||||||
|
i->index,
|
||||||
|
i->label
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_ADD:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = %s + %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1),
|
||||||
|
get_string(i->operand2)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_SUB:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = %s - %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1),
|
||||||
|
get_string(i->operand2)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_MUL:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = %s * %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1),
|
||||||
|
get_string(i->operand2)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_DIV:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = %s / %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1),
|
||||||
|
get_string(i->operand2)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_MOD:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = %s %% %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1),
|
||||||
|
get_string(i->operand2)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_OR:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = %s | %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1),
|
||||||
|
get_string(i->operand2)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_AND:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = %s & %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1),
|
||||||
|
get_string(i->operand2)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_NEG:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = -%s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_NOT:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = !%s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_ASSIGN:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_GOTO:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: GOTO : %d\n",
|
||||||
|
i->index,
|
||||||
|
i->label
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_IF_X_TRUE:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: if %s goto %d\n",
|
||||||
|
i->index,
|
||||||
|
get_string(i->operand1),
|
||||||
|
i->label
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_IF_X_FALSE:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: if %s false goto %d\n",
|
||||||
|
i->index,
|
||||||
|
get_string(i->operand1),
|
||||||
|
i->label
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_LESS_THAN:
|
||||||
|
// this feels wrong I need to TODO: this
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = %s < %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1),
|
||||||
|
get_string(i->operand2)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_EQUAL_TO:
|
||||||
|
// this feels wrong I need to TODO: this
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = %s == %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1),
|
||||||
|
get_string(i->operand2)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_CALL:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: call : %s %s\n",
|
||||||
|
i->index,
|
||||||
|
get_string(i->operand1),
|
||||||
|
get_string(i->operand2)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_PARAM:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: param %s \n",
|
||||||
|
i->index,
|
||||||
|
get_string(i->operand1)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_RETURN:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: return : %s\n",
|
||||||
|
i->index,
|
||||||
|
get_string(i->operand1)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_INDEX_COPY_RIGHT:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = %s[ %s ]\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1),
|
||||||
|
get_string(i->operand2)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case E_INDEX_COPY_LEFT:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s[ %s ] = %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand2),
|
||||||
|
get_string(i->operand1));
|
||||||
|
break;
|
||||||
|
case E_ADDRESS_OF:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = &%s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
int getConst(TNodeOrConst* tnc) {
|
|
||||||
if (tnc->d == INTEGER) {
|
case E_DEREF_RIGHT:
|
||||||
return tnc->tnc_union->integer;
|
fprintf(out_file,
|
||||||
|
"%4.d: %s = *%s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1)
|
||||||
|
);
|
||||||
|
case E_DEREF_LEFT:
|
||||||
|
fprintf(out_file,
|
||||||
|
"%4.d: *%s = %s\n",
|
||||||
|
i->index,
|
||||||
|
getName(i->result),
|
||||||
|
get_string(i->operand1)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return -1;
|
|
||||||
|
emit_as_file(out_file, i->next);
|
||||||
}
|
}
|
@ -12,14 +12,32 @@
|
|||||||
|
|
||||||
#include "symbol_table.h"
|
#include "symbol_table.h"
|
||||||
|
|
||||||
// these are from page 364
|
typedef struct Stack Stack;
|
||||||
typedef enum {
|
typedef struct __Node __Node;
|
||||||
|
|
||||||
|
typedef struct __Node {
|
||||||
|
void * v;
|
||||||
|
__Node * next;
|
||||||
|
} __Node;
|
||||||
|
|
||||||
|
typedef struct Stack {
|
||||||
|
__Node * n;
|
||||||
|
int size;
|
||||||
|
} Stack;
|
||||||
|
//______________________________________________________________________________________________
|
||||||
|
|
||||||
|
typedef union TNConstUnion TNConstUnion;
|
||||||
|
typedef struct Instruction Instruction;
|
||||||
|
typedef struct TNodeOrConst TNodeOrConst;
|
||||||
|
|
||||||
|
typedef enum { // these are from page 364
|
||||||
E_LABEL = 10000, // this is not in the book
|
E_LABEL = 10000, // this is not in the book
|
||||||
|
E_FUNC_START,
|
||||||
E_ADD, // 1 from the list
|
E_ADD, // 1 from the list
|
||||||
E_SUB, // 1
|
E_SUB, // 1
|
||||||
E_MUL, // 1
|
E_MUL, // 1
|
||||||
E_DIV, // 1
|
E_DIV, // 1
|
||||||
E_MOD, // 1
|
E_MOD, // 1 TODO: Please change to REM
|
||||||
E_OR, // 1
|
E_OR, // 1
|
||||||
E_AND, // 1
|
E_AND, // 1
|
||||||
E_NEG, // 2
|
E_NEG, // 2
|
||||||
@ -50,75 +68,71 @@ typedef enum {
|
|||||||
BOOLEAN // bool
|
BOOLEAN // bool
|
||||||
} Discriminant;
|
} Discriminant;
|
||||||
|
|
||||||
typedef union {
|
typedef union TNConstUnion {
|
||||||
TableNode* node;
|
TableNode * node;
|
||||||
int integer;
|
int integer;
|
||||||
char* string;
|
char * string;
|
||||||
char character;
|
char character;
|
||||||
void* address;
|
void * address;
|
||||||
bool Boolean;
|
bool Boolean;
|
||||||
} TNConstUnion;
|
} TNConstUnion;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct TNodeOrConst {
|
||||||
Discriminant d;
|
Discriminant d;
|
||||||
TNConstUnion* tnc_union;
|
TNConstUnion * tnc_union;
|
||||||
} TNodeOrConst;
|
} TNodeOrConst;
|
||||||
|
|
||||||
typedef struct Instruction Instruction;
|
|
||||||
typedef struct Instruction {
|
typedef struct Instruction {
|
||||||
Op opcode;
|
Op opcode;
|
||||||
TableNode* result;
|
TableNode * result;
|
||||||
TNodeOrConst* operand1;
|
TNodeOrConst * operand1;
|
||||||
TNodeOrConst* operand2;
|
TNodeOrConst * operand2;
|
||||||
int label;
|
int label;
|
||||||
int index;
|
int index;
|
||||||
|
|
||||||
Instruction* prev;
|
Instruction * prev;
|
||||||
Instruction* next;
|
Instruction * next;
|
||||||
} Instruction;
|
} Instruction;
|
||||||
|
|
||||||
|
|
||||||
|
// NOTE We are not using this We are using the Stack api
|
||||||
typedef struct TFList {
|
typedef struct TFList {
|
||||||
Instruction* i;
|
Instruction * i;
|
||||||
TFList* next;
|
TFList * next;
|
||||||
} TFList;
|
} TFList;
|
||||||
|
|
||||||
TNodeOrConst* getOperand1(Instruction* i);
|
TFList * make_list(Instruction * i);
|
||||||
TNodeOrConst* getOperand2(Instruction* i);
|
// - makelist(i) function to create instruction lists
|
||||||
TableNode* getResult(Instruction* i);
|
void merge(TFList * l1, TFList * l2);
|
||||||
Op getOp(Instruction* i);
|
// - merge(p1,p2) function to concatenate lists
|
||||||
int getLabel(Instruction* i);
|
void backpatch(TFList * l, int label);
|
||||||
int get_index(Instruction* i);
|
// - backpatch(p,i) function to fill in jump targets
|
||||||
void set_label(Instruction* i, int label);
|
void bp_temp(int n);
|
||||||
bool isConst(TNodeOrConst* tnc);
|
|
||||||
TNodeOrConst* tn_or_const(Discriminant d, void* tnc);
|
|
||||||
static void emit_helper(void);
|
extern Instruction * begin;
|
||||||
void emit_binary_op(Op op, TableNode* result, TNodeOrConst* arg1, TNodeOrConst* arg2);
|
extern Instruction * current;
|
||||||
void emit_unary_op(Op op, TableNode* result, TNodeOrConst* arg);
|
int temp_count = 0;
|
||||||
void emit_assignment(TableNode* target, TNodeOrConst* source);
|
int label_count = 0;
|
||||||
char* get_string(TNodeOrConst* tc);
|
bool code_gen = true;
|
||||||
void emit_as_file(FILE* out_file, Instruction* i);
|
|
||||||
|
|
||||||
|
TNodeOrConst * tn_or_const(Discriminant , void * );
|
||||||
|
void emit_binary_op(Op op, TableNode * result, TNodeOrConst * arg1, TNodeOrConst * arg2);
|
||||||
|
void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg);
|
||||||
|
void emit_assignment(TableNode * target, TNodeOrConst * source);
|
||||||
|
void emit_as_file(FILE * out_file, Instruction * instr_arr);
|
||||||
void emit_label(int label);
|
void emit_label(int label);
|
||||||
void emit_jump(int label);
|
void emit_jump(int label);
|
||||||
|
|
||||||
void emit_conditional_jump(Op condition, int label, ...);
|
void emit_conditional_jump(Op condition, int label, ...);
|
||||||
void emit_function_start(TNodeOrConst * name);
|
|
||||||
void emit_parameter(TNodeOrConst* param);
|
void emit_function_start(int name);
|
||||||
void emit_function_call(TableNode* result, int param_count, TNodeOrConst* name);
|
void emit_parameter(TNodeOrConst * param);
|
||||||
void emit_return(TNodeOrConst* value);
|
void emit_function_call(TableNode * result, int param_count, TNodeOrConst * name);
|
||||||
void emit_reserve(TableNode* result, TNodeOrConst* size);
|
void emit_return(TNodeOrConst * value);
|
||||||
void emit_release(TableNode* pointer);
|
void emit_reserve(TableNode * result, TNodeOrConst * size);
|
||||||
void emit_deref_right();
|
void emit_release(TableNode * pointer);
|
||||||
void emit_deref_left();
|
|
||||||
void emit_field_access(char* result, char* record, char* field);
|
void emit_field_access(char* result, char* record, char* field);
|
||||||
void emit_array_access(Op op, TableNode* result, TNodeOrConst* array, TNodeOrConst* index);
|
void emit_array_access(Op op, TableNode * result, TNodeOrConst * array, TNodeOrConst * index);
|
||||||
void emit_bounds_check(TNodeOrConst* index, TNodeOrConst* arr);
|
void emit_bounds_check(TNodeOrConst * index, TNodeOrConst * arr, int error_label);
|
||||||
int label_gen();
|
|
||||||
TableNode* getTN(TNodeOrConst* tnc);
|
|
||||||
int getConst(TNodeOrConst* tnc);
|
|
||||||
|
|
||||||
extern int label_count;
|
|
||||||
extern Instruction* begin;
|
|
||||||
extern Instruction* current;
|
|
||||||
|
|
||||||
extern int offset;
|
|
||||||
extern int currentsp;
|
|
||||||
extern CGNode* cgList;
|
|
Reference in New Issue
Block a user