I added some packpatching

This commit is contained in:
Meyer Simon
2025-04-30 08:20:11 -04:00
parent 0189c2d937
commit cd4393d052
6 changed files with 138 additions and 110 deletions

View File

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

View File

@ -270,7 +270,7 @@ definition:
} }
//printf("Ending ID: %s\n", $1); //printf("Ending ID: %s\n", $1);
//printf("Ending Type: %s\n", getType(table_lookup(getAncestor(cur), $1))); //printf("Ending Type: %s\n", getType(table_lookup(getAncestor(cur), $1)));
} } idlist R_PAREN ASSIGN sblock
; ;
@ -552,24 +552,37 @@ compound_statement statement_list {
compound_statement: compound_statement:
WHILE L_PAREN expression R_PAREN sblock { WHILE L_PAREN
$$ = $5; {
if (!TrueList) {TrueList = S_Init();}
if (!FalseList) {FalseList = S_Init();}
S_Push(TrueList, S_Init());
S_Push(FalseList, S_Init());
} expression R_PAREN {
emit_label(label_gen());
backpatch(S_Pop(TrueList), getLabel(current));
} sblock {
$$ = $7;
emit_label(label_gen());
backpatch(S_Pop(FalseList), getLabel(current));
} }
| IF L_PAREN expression R_PAREN THEN sblock ELSE sblock { | IF L_PAREN expression R_PAREN THEN {emit_label(label_gen());} sblock ELSE {emit_label(label_gen());} sblock {
if ($6 == undefined && $8 != undefined) { /*
$$ = $8; */
} else if ($6 != undefined && $8 == undefined) { if ($7 == undefined && $10 != undefined) {
$$ = $6; $$ = $10;
} else if ($6 == $8) { } else if ($7 != undefined && $10 == undefined) {
$$ = $6; $$ = $7;
}else if((getAdInfoType((TableNode*)$6) == TYPE_ARRAY_TYPE) && ((TableNode*)$8)==addr){ } else if ($7 == $10) {
$$ = $6; $$ = $7;
}else if((getAdInfoType((TableNode*)$6) == TYPE_RECORD_TYPE) && ((TableNode*)$8)==addr){ }else if((getAdInfoType((TableNode*)$7) == TYPE_ARRAY_TYPE) && ((TableNode*)$10)==addr){
$$ = $6; $$ = $7;
}else if(((TableNode*)$6)==addr && (getAdInfoType((TableNode*)$8) == TYPE_ARRAY_TYPE)){ }else if((getAdInfoType((TableNode*)$7) == TYPE_RECORD_TYPE) && ((TableNode*)$10)==addr){
$$ = $8; $$ = $7;
}else if(((TableNode*)$6)==addr && (getAdInfoType((TableNode*)$8) == TYPE_RECORD_TYPE)){ }else if(((TableNode*)$7)==addr && (getAdInfoType((TableNode*)$10) == TYPE_ARRAY_TYPE)){
$$ = $8; $$ = $10;
}else if(((TableNode*)$7)==addr && (getAdInfoType((TableNode*)$10) == TYPE_RECORD_TYPE)){
$$ = $10;
} else { } else {
printdebug("3 differing return types within same function at line %d, column %d", @1.first_line, @1.first_column); 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)); //printf("%s\n", getName((TableNode*)$6));
@ -659,6 +672,7 @@ argument_list:
//NEED TO EMIT PARAMETERS HERE. MAYBE USE STACK STRUCTURE //NEED TO EMIT PARAMETERS HERE. MAYBE USE STACK STRUCTURE
expression{ expression{
TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL); TableNode* arg = CreateEntry(cur, getAdInfoType((TableNode*)$1), getTypeEntry((TableNode*)$1), arg_var_gen(), NULL);
// this emits params for function and arrays TODO: fix
emit_parameter(tn_or_const(NODE,arg)); emit_parameter(tn_or_const(NODE,arg));
//S_Push(stack,current); //S_Push(stack,current);
//emit_detach(); //emit_detach();
@ -825,11 +839,12 @@ expression:
| expression LESS_THAN expression | expression LESS_THAN expression
{ {
emit_conditional_jump(E_LESS_THAN, 0, tn_or_const(NODE,$1), tn_or_const(NODE,$3));
S_Push(S_Peek(TrueList), current);
printdebug("less than expression"); printdebug("less than expression");
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==integ) { if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1)==integ) {
char* temp = temp_var_gen(); char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); 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; $$ = node;
} else { } else {
$$=undefined; $$=undefined;
@ -843,7 +858,7 @@ expression:
if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) { if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) {
char* temp = temp_var_gen(); char* temp = temp_var_gen();
TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); 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));
$$ = node; $$ = node;
} else { } else {

View File

@ -69,6 +69,13 @@ void emit_detach(){
current->next = NULL; 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){ TNodeOrConst * getOperand1(Instruction * i){
return i->operand1; return i->operand1;
} }
@ -480,21 +487,21 @@ void emit_as_file(FILE * out_file, Instruction * i){
case E_LESS_THAN: case E_LESS_THAN:
// this feels wrong I need to TODO: this // this feels wrong I need to TODO: this
fprintf(out_file, fprintf(out_file,
"%4.d: %s = %s < %s\n", "%4.d: if ( %s < %s ) GOTO %d\n",
i->index, i->index,
getName(i->result),
get_string(i->operand1), get_string(i->operand1),
get_string(i->operand2) get_string(i->operand2),
i->label
); );
break; break;
case E_EQUAL_TO: case E_EQUAL_TO:
// this feels wrong I need to TODO: this // this feels wrong I need to TODO: this
fprintf(out_file, fprintf(out_file,
"%4.d: %s = %s == %s\n", "%4.d: if ( %s = %s ) GOTO %d\n",
i->index, i->index,
getName(i->result),
get_string(i->operand1), get_string(i->operand1),
get_string(i->operand2) get_string(i->operand2),
i->label
); );
break; break;
case E_CALL: case E_CALL:

View File

@ -108,13 +108,13 @@ typedef struct TFList {
TFList * next; TFList * next;
} TFList; } TFList;
TFList * make_list(Instruction * i); // TFList * make_list(Instruction * i);
// - makelist(i) function to create instruction lists // - 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 // - 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 // - backpatch(p,i) function to fill in jump targets
void bp_temp(int n); // void bp_temp(int n);
extern Instruction * begin; extern Instruction * begin;
@ -159,3 +159,4 @@ int get_index(Instruction * i);
void set_label(Instruction * i, int label); void set_label(Instruction * i, int label);
bool isConst(TNodeOrConst * tnc); bool isConst(TNodeOrConst * tnc);
int label_gen(); int label_gen();
void backpatch(Stack *s, int l);

View File

@ -102,7 +102,10 @@ void print_tok(int tok) {
int run(FILE *alpha) { int run(FILE *alpha) {
int token; int token;
top = cur = init(CreateScope(NULL, 1, 1)); top = cur = init(CreateScope(NULL, 1, 1));
Stack *s = S_Init(); Stack *stack = S_Init();
Stack *TrueList = S_Init();
Stack *FalseList = S_Init();
// If file is not found // If file is not found
if (alpha == NULL) { if (alpha == NULL) {