I merged IR-Linting to Dev

This commit is contained in:
Meyer Simon
2025-05-02 13:39:58 -04:00
12 changed files with 407 additions and 156 deletions

View File

@ -31,4 +31,6 @@ int offset;
int currentsp;
CGNode *cgList;
extern Stack* stack;
Stack* stack;
Stack* TrueList;
Stack* FalseList;

View File

@ -130,20 +130,11 @@ prototype:
definition:
TYPE ID COLON
{
printdebug("Currently see a record definition for %s", $2);
tn = CreateEntry(getAncestor(cur),TYPE_RECORD_TYPE, recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0)));
printdebug("Created a new scope");
//if (look_up(cur, $2) == undefined) {
// printdebug("rec not found");
//}
}
dblock
{
//We are scanning through the dblock scope to get the length of the dblock (num of elements) from getRecSize
//and then putting it in the entry that we created above.
} dblock {
setRecSize(look_up(getParent(cur), $2), getRecSize(cur));
//putting in all the offsets
setRecOffsetInfo(cur, look_up(getParent(cur),$2));
printdebug("Moving up a scope after seeing a record definition");
cur = getParent(cur);
@ -155,9 +146,7 @@ definition:
CreateEntry(cur,TYPE_ARRAY_TYPE, arrayprim, $2, CreateArrayInfo($4, (TableNode*)$6));
printdebug("%sID: %s, dimensions: %d, typeOfArray: %s", COLOR_GREEN, $2, $4, getName((TableNode*)$6));
}
| function_declaration
| TYPE ID COLON id_or_types ARROW id_or_types
{
printdebug("Currently see a function type definition of name %s,parameter type %s, of return type %s", $2, getName((TableNode*)$4), getName((TableNode*)$6));
@ -270,7 +259,7 @@ definition:
}
//printf("Ending ID: %s\n", $1);
//printf("Ending Type: %s\n", getType(table_lookup(getAncestor(cur), $1)));
}
}
;
@ -367,6 +356,7 @@ idlist:
sblock:
L_BRACE
{
// emit_label(label_gen());
if (getLine(cur) != 0) {
cur = CreateScope(cur,@1.first_line,@1.first_column);
printdebug("Created a new scope");
@ -552,24 +542,55 @@ compound_statement statement_list {
compound_statement:
WHILE L_PAREN expression R_PAREN sblock {
$$ = $5;
}
| IF L_PAREN expression R_PAREN THEN sblock ELSE sblock {
if ($6 == undefined && $8 != undefined) {
WHILE L_PAREN {
S_Push(TrueList, S_Init(), 0);
S_Push(FalseList, S_Init(), 0);
int *l = calloc(1, sizeof(int));
*l = label_gen();
emit_label(*l);
S_Push(stack, l, 2);
} expression R_PAREN {
emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current));
} sblock {
$$ = $7;
int l = label_gen();
emit_backpatch(S_Pop(FalseList), l);
emit_goto(*(int*)(S_Pop(stack)));
emit_label(l);
}
| IF L_PAREN {
S_Push(TrueList, S_Init(), 0);
S_Push(FalseList, S_Init(), 0);
}expression R_PAREN THEN {
emit_label(label_gen());
emit_backpatch(S_Pop(TrueList), getLabel(current));
} sblock ELSE {
// NOTE we are not going back to
int l = label_gen();
emit_backpatch(S_Pop(FalseList), l);
S_Push(FalseList, S_Init(), 0);
emit_goto(0);
S_Push(S_Peek(FalseList), current, 1);
emit_label(l);
} sblock {
int l = label_gen();
emit_backpatch(S_Pop(FalseList), l);
emit_label(l);
if ($8 == undefined && $11 != undefined) {
$$ = $11;
} else if ($8 != undefined && $11 == undefined) {
$$ = $8;
} else if ($6 != undefined && $8 == undefined) {
$$ = $6;
} else if ($6 == $8) {
$$ = $6;
}else if((getAdInfoType((TableNode*)$6) == TYPE_ARRAY_TYPE) && ((TableNode*)$8)==addr){
$$ = $6;
}else if((getAdInfoType((TableNode*)$6) == TYPE_RECORD_TYPE) && ((TableNode*)$8)==addr){
$$ = $6;
}else if(((TableNode*)$6)==addr && (getAdInfoType((TableNode*)$8) == TYPE_ARRAY_TYPE)){
} else if ($8 == $11) {
$$ = $8;
}else if(((TableNode*)$6)==addr && (getAdInfoType((TableNode*)$8) == TYPE_RECORD_TYPE)){
}else if((getAdInfoType((TableNode*)$8) == TYPE_ARRAY_TYPE) && ((TableNode*)$11)==addr){
$$ = $8;
}else if((getAdInfoType((TableNode*)$8) == TYPE_RECORD_TYPE) && ((TableNode*)$11)==addr){
$$ = $8;
}else if(((TableNode*)$8)==addr && (getAdInfoType((TableNode*)$11) == TYPE_ARRAY_TYPE)){
$$ = $11;
}else if(((TableNode*)$8)==addr && (getAdInfoType((TableNode*)$11) == TYPE_RECORD_TYPE)){
$$ = $11;
} else {
printdebug("3 differing return types within same function at line %d, column %d", @1.first_line, @1.first_column);
//printf("%s\n", getName((TableNode*)$6));
@ -648,9 +669,18 @@ rec_op:
ablock:
L_PAREN argument_list R_PAREN
L_PAREN{
if (stack == NULL){
stack = S_Init();
}
Stack * t = S_Init();
S_Push(stack, t, 0);
}
argument_list {
} R_PAREN
{
$$ = $2;
// here
$$ = $3;
printdebug("ablock is %d", $$);
}
@ -660,14 +690,20 @@ ablock:
argument_list:
//NEED TO EMIT PARAMETERS HERE. MAYBE USE STACK STRUCTURE
expression{
char* name = arg_var_gen();
TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), name, NULL);
emit_parameter(tn_or_const(NODE,arg));
//S_Push(stack,current);
//emit_detach();
//printdebug("[ARGUMENT_LIST] argument list is %d", $$);
TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL);
// ----------------------------------------------------------------------------
// this is emitting the param withthe wrong TableNode
// We need to fiture out how to get the right one.
Stack * t = S_Peek(stack);
if(t==NULL){
t = S_Init();
S_Push(stack, t, 1);
}
emit_parameter(tn_or_const(NODE,$1));
S_Push(t, current, 1);
emit_detach();
// ----------------------------------------------------------------------------
}
COMMA argument_list
{$$ = $4 + 1;}
@ -675,11 +711,8 @@ argument_list:
| expression
{
char* name = arg_var_gen();
TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), name, NULL);
emit_parameter(tn_or_const(NODE,arg));
//S_Push(stack,current);
//emit_detach();
TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL);
emit_parameter(tn_or_const(NODE,$1));
$$ = 1;
printdebug("[ARGUMENT_LIST] argument list is %d", $$);
}
@ -831,11 +864,24 @@ expression:
| expression LESS_THAN expression
{
emit_conditional_jump(E_LESS_THAN, 0, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
Stack * t = S_Peek(TrueList);
if(t==NULL){
t = S_Init();
S_Push(TrueList, t, 1);
}
S_Push(t, current, 1);
emit_goto(0);
t = S_Peek(FalseList);
if(t==NULL){
t = S_Init();
S_Push(FalseList, t, 1);
}
S_Push(t, current, 1);
printdebug("less than expression");
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==integ) {
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
emit_binary_op(E_LESS_THAN,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
$$ = node;
} else if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==boo){
char* temp = temp_var_gen();
@ -854,7 +900,20 @@ expression:
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) {
char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL);
emit_binary_op(E_EQUAL_TO,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3));
emit_conditional_jump(E_EQUAL_TO, 0, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
Stack * t = S_Peek(TrueList);
if(t==NULL){
t = S_Init();
S_Push(TrueList, t, 1);
}
S_Push(t, current, 1);
emit_goto(0);
t = S_Peek(FalseList);
if(t==NULL){
t = S_Init();
S_Push(FalseList, t, 1);
}
S_Push(t, current, 1);
$$ = node;
} else {
@ -874,7 +933,11 @@ expression:
$$=$2;
}
| RESERVE assignable
// TODO: We need to type check this.
| RESERVE ID {$$ = undefined; }
| RELEASE ID {$$ = undefined; }
| RESERVE ID L_PAREN argument_list R_PAREN {$$ = undefined; }
| RELEASE ID L_PAREN argument_list R_PAREN
{
int d = getAdInfoType((TableNode*)$2);
//commenting out type checks for now since assignable is being resolved to something before reserve is being applied which is tricky
@ -888,6 +951,7 @@ expression:
// $$=undefined;
// }
}
/*
| RELEASE assignable
{
int d = getAdInfoType((TableNode*)$2);
@ -902,6 +966,7 @@ expression:
// $$=undefined;
// }
}
*/
;
@ -920,7 +985,6 @@ assignable:
$$ = pass;
printdebug("[ASSIGNABLE - RULE 1] assignable = type: %s | ID = %s", getType(pass), getName(pass));
}
| assignable
{
printdebug("%sBeginning rule 2 of assignable.", COLOR_CYAN);
@ -1007,6 +1071,14 @@ assignable:
throw_error(ERROR_TYPE, "Undefined type returned by function.");
}
TableNode* node = CreateEntry(cur,t, typeNode2, temp, NULL);
//-----------------------------------------------------------------------------
// Please don't touch
// the + 1 is here because I don't detach the last param
int a = S_Size(S_Peek(stack)) + 1;
emit_push_all(S_Peek(stack));
S_Pop(stack);
emit_function_call(node, a, tn_or_const(NODE, $1));
//-----------------------------------------------------------------------------
$$ = node;
//NOTE ADD ASSIGNMENT EMIT HERE (MIGHT NEED TO PUSH TO STACK for function call)
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getName(typeNode2), getName((TableNode*)$1));
@ -1035,8 +1107,8 @@ assignable:
throw_error(ERROR_TYPE, "Undefined type stored in array.");
}
TableNode* node = CreateEntry(cur,t, typeNode2, temp, NULL);
//emit assign here
//emit_array_access(char* node, char* array, ...)
//TODO: emit assign here
//TODO: emit_array_access(char* node, char* array, ...)
$$ = node;
printdebug("[ASSIGNABLE - RULE 2] assignable = type: %s | name_func = %s", getType((TableNode*)$1), getName((TableNode*)$1));
}

View File

@ -15,16 +15,17 @@ void S_Free(Stack *s){
free(s);
}
void S_Push(Stack * s, void *v) {
void S_Push(Stack * s, void *v, int i) {
__Node * n = calloc(1, sizeof(*n));
n->v = v;
n->next = s->n;
s->n = n;
s->w = i;
s->size = s->size + 1;
}
void * S_Pop(Stack *s) {
if (s->size == 0) {
if (s == NULL || S_IsEmpty(s)) {
return NULL;
}
__Node * node = s->n;
@ -37,22 +38,31 @@ void * S_Pop(Stack *s) {
void * S_Peek(Stack *s){
if (!S_IsEmpty(s)) {
if (s == NULL || S_IsEmpty(s)) {
return NULL;
}
return s->n->v;
}
bool S_IsEmpty(Stack *s){
if(!s->size) {
if(s == NULL || s->size == 0) {
return true;
}
return false;
}
int S_Size(Stack *s){
if (s == NULL || S_IsEmpty(s)) {
return 0;
}
return s->size;
}
void emit_backpatch(Stack * s, int l){
for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){
i->label = l;
}
}
//_______________________________________________________________________
char * temp = NULL;
@ -64,11 +74,28 @@ char * temp = NULL;
otherwise make it next of current and set cur to your instruction.
*/
void emit_push_all(Stack * s){
for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){
current->next = i;
i->prev = current;
i->index = current->index + 1;
current = i;
current->next = NULL;
}
}
void emit_detach(){
current = current->prev;
current->next = NULL;
}
void backpatch(Stack *s, int l){
while (!S_IsEmpty(s)){
Instruction * i = S_Pop(s);
set_label(i, l);
}
}
TNodeOrConst * getOperand1(Instruction * i){
return i->operand1;
}
@ -129,10 +156,6 @@ TNodeOrConst * tn_or_const(Discriminant d, void * tnc) {
}
static void emit_helper(void){
if (!ir_flag) {
begin = NULL;
return;
}
Instruction * inst = calloc(1, sizeof(*inst));
if(begin == NULL){
begin = current = inst;
@ -159,6 +182,12 @@ void emit_binary_op(
current->operand2 = arg2;
}
void emit_goto(int i){
emit_helper();
current->opcode = E_GOTO;
current->label = i;
}
void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg){
emit_helper();
current->opcode = op;
@ -254,8 +283,8 @@ void emit_function_call(
){
emit_helper();
current->opcode = E_CALL;
current->operand1 = tn_or_const(INTEGER, &param_count);
current->operand2 = name;
current->operand1 = name;
current->operand2 = tn_or_const(INTEGER, &param_count);
current->result = result;
}
@ -472,7 +501,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
break;
case E_IF_X_TRUE:
fprintf(out_file,
"%4.d: if %s goto %d\n",
"%4.d: if %s GOTO %d\n",
i->index,
get_string(i->operand1),
i->label
@ -480,7 +509,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
break;
case E_IF_X_FALSE:
fprintf(out_file,
"%4.d: if %s false goto %d\n",
"%4.d: if %s false GOTO %d\n",
i->index,
get_string(i->operand1),
i->label
@ -489,21 +518,21 @@ void emit_as_file(FILE * out_file, Instruction * i){
case E_LESS_THAN:
// this feels wrong I need to TODO: this
fprintf(out_file,
"%4.d: %s = %s < %s\n",
"%4.d: if ( %s < %s ) GOTO %d\n",
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
get_string(i->operand2),
i->label
);
break;
case E_EQUAL_TO:
// this feels wrong I need to TODO: this
fprintf(out_file,
"%4.d: %s = %s == %s\n",
"%4.d: if ( %s = %s ) GOTO %d\n",
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
get_string(i->operand2),
i->label
);
break;
case E_CALL:

View File

@ -22,11 +22,12 @@ typedef struct __Node {
typedef struct Stack {
__Node * n;
int w;
int size;
} Stack;
Stack * S_Init();
void S_Free(Stack *s);
void S_Push(Stack * s, void *v);
void S_Push(Stack * s, void *v, int i);
void * S_Pop(Stack *s);
void * S_Peek(Stack *s);
bool S_IsEmpty(Stack *s);
@ -108,13 +109,13 @@ typedef struct TFList {
TFList * next;
} TFList;
TFList * make_list(Instruction * i);
// TFList * make_list(Instruction * i);
// - makelist(i) function to create instruction lists
void merge(TFList * l1, TFList * l2);
// void merge(TFList * l1, TFList * l2);
// - merge(p1,p2) function to concatenate lists
void backpatch(TFList * l, int label);
// void backpatch(TFList * l, int label);
// - backpatch(p,i) function to fill in jump targets
void bp_temp(int n);
// void bp_temp(int n);
extern Instruction * begin;
@ -143,6 +144,9 @@ void emit_release(TableNode * pointer);
void emit_field_access(char* result, char* record, char* field);
void emit_array_access(Op op, TableNode * result, TNodeOrConst * array, TNodeOrConst * index);
void emit_bounds_check(TNodeOrConst * index, TNodeOrConst * arr);
void emit_goto(int i);
void emit_detach();
void emit_push_all(Stack * s);
int getLabel(Instruction * i);
@ -160,3 +164,5 @@ int get_index(Instruction * i);
void set_label(Instruction * i, int label);
bool isConst(TNodeOrConst * tnc);
int label_gen();
void backpatch(Stack *s, int l);
void emit_backpatch(Stack *s, int l);

View File

@ -102,7 +102,7 @@ void print_tok(int tok) {
int run(FILE *alpha) {
int token;
top = cur = init(CreateScope(NULL, 1, 1));
Stack *s = S_Init();
// If file is not found
if (alpha == NULL) {
@ -127,6 +127,9 @@ int run(FILE *alpha) {
fseek(alpha, 0, SEEK_SET);
yyin = alpha;
stack = S_Init();
TrueList = S_Init();
FalseList = S_Init();
yyparse();
if (tok_flag != NULL) {

View File

@ -70,6 +70,9 @@ TableNode *recprime;
TableNode *funtypeprime;
TableNode *undefined;
extern Instruction *begin;
extern Stack* stack;
extern Stack* TrueList;
extern Stack* FalseList;
int main(int argc, char *argv[]);
int check_flag(char *arg, char *alpha);

View File

@ -15,7 +15,6 @@ function make_list : list
make_list (a) := {
[integer:orig_a; llnode: ret; llnode: curr; llnode: temp]
if (a < 0 | a = 0) then {
return null;
} else {
@ -73,7 +72,7 @@ entry (arg) := {
w := reserve w;
w.x := 5;
w.y := 7;
result := bar1(w);
(* result := bar1(w); *)
result := bar2(5,7);
return 0;

View File

@ -0,0 +1,80 @@
type main: string -> integer
function entry: main
type rec: [integer: x; integer: y]
type T1: integer -> integer
type T2: rec -> integer
type llnode: [llnode: prev; integer: val; llnode: next]
type list: integer -> llnode
function foo : T1
function bar1 : T2
function bar2 : T2
function make_list : list
make_list (a) := {
[integer:orig_a; llnode: ret; llnode: curr; llnode: temp]
if (a < 0 | a = 0) then {
return null;
} else {
ret := reserve ret;
ret.prev := null;
ret.next := null;
ret.val := a;
while (0 < a) {
temp := reserve temp;
temp.prev := null;
temp.next := null;
temp.val := ret.val;
if (a = orig_a) then {
ret.next := temp;
temp.prev := ret;
curr := temp;
} else {
curr.next := temp;
temp.prev := curr;
curr := temp;
}
a := a - 1;
}
return ret;
}
}
foo (x) := {
return x * x;
}
bar1(a,b) := {
return a * b;
}
bar2(r,s) := {
if (r < s) then {
while (!(r < s)) {
r := r + 1;
}
} else {
[integer: x]
x := 0;
while (x < 10) {
r := r + s;
}
}
return r * s;
}
entry (arg) := {
[ integer: result ; rec: w; llnode: li; boolean: b]
li := make_list(6, 7);
result := foo(5);
w := reserve w;
w.x := 5;
w.y := 7;
result := bar1(w);
result := bar2(5,7);
return 'a';
}

View File

@ -0,0 +1,25 @@
type rec: [character: x; integer: y]
type T2: rec -> integer
type main: string -> integer
function entry: main
function bar: T2
bar (r,s) := {
return 0;
}
entry (arg) := {
[ integer: result ; rec: w]
if ( result = result ) then {
if ( result < w.y ) then {
result := 8;
} else {
result := 9;
}(* *)
} else {
result := bar('c', 7);
}
return 0;
}

View File

@ -0,0 +1,27 @@
type rec: [character: x; integer: y]
type T2: rec -> integer
type main: string -> integer
function entry: main
function bar: T2
bar (r,s) := {
return 0;
}
entry (arg) := {
[ integer: result ; rec: w]
while ( result = result ) {
result := result + 8;
if ( result < w.y ) then {
while (true) {
result := 8;
}
} else {
result := 9;
}
result := bar('c', 7);
}
return 0;
}

View File

@ -12,7 +12,12 @@ bar (r,s) := {
entry (arg) := {
[ integer: result ; rec: w]
result := bar(1,2);
while ( result = result ) {
while ( result < w.y ) {
result := 8;
}
result := 9;
}
result := bar('c', 7);
return 0;
}