I added some packpatching
This commit is contained in:
@ -31,4 +31,6 @@ int offset;
|
||||
int currentsp;
|
||||
CGNode *cgList;
|
||||
|
||||
extern Stack* stack;
|
||||
extern Stack* s;
|
||||
Stack* TrueList;
|
||||
Stack* FalseList;
|
||||
|
@ -270,7 +270,7 @@ definition:
|
||||
}
|
||||
//printf("Ending ID: %s\n", $1);
|
||||
//printf("Ending Type: %s\n", getType(table_lookup(getAncestor(cur), $1)));
|
||||
}
|
||||
} idlist R_PAREN ASSIGN sblock
|
||||
|
||||
;
|
||||
|
||||
@ -434,8 +434,8 @@ dblock:
|
||||
|
||||
|
||||
declaration_list:
|
||||
declaration SEMI_COLON declaration_list
|
||||
| declaration
|
||||
declaration SEMI_COLON declaration_list
|
||||
| declaration
|
||||
| error SEMI_COLON { yyerrok; } declaration_list //only perform error recovery once we see semi-colon
|
||||
|
||||
;
|
||||
@ -552,24 +552,37 @@ compound_statement statement_list {
|
||||
|
||||
|
||||
compound_statement:
|
||||
WHILE L_PAREN expression R_PAREN sblock {
|
||||
$$ = $5;
|
||||
WHILE L_PAREN
|
||||
{
|
||||
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 ($6 == undefined && $8 != 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)){
|
||||
$$ = $8;
|
||||
}else if(((TableNode*)$6)==addr && (getAdInfoType((TableNode*)$8) == TYPE_RECORD_TYPE)){
|
||||
$$ = $8;
|
||||
| IF L_PAREN expression R_PAREN THEN {emit_label(label_gen());} sblock ELSE {emit_label(label_gen());} sblock {
|
||||
/*
|
||||
*/
|
||||
if ($7 == undefined && $10 != undefined) {
|
||||
$$ = $10;
|
||||
} else if ($7 != undefined && $10 == undefined) {
|
||||
$$ = $7;
|
||||
} else if ($7 == $10) {
|
||||
$$ = $7;
|
||||
}else if((getAdInfoType((TableNode*)$7) == TYPE_ARRAY_TYPE) && ((TableNode*)$10)==addr){
|
||||
$$ = $7;
|
||||
}else if((getAdInfoType((TableNode*)$7) == TYPE_RECORD_TYPE) && ((TableNode*)$10)==addr){
|
||||
$$ = $7;
|
||||
}else if(((TableNode*)$7)==addr && (getAdInfoType((TableNode*)$10) == TYPE_ARRAY_TYPE)){
|
||||
$$ = $10;
|
||||
}else if(((TableNode*)$7)==addr && (getAdInfoType((TableNode*)$10) == TYPE_RECORD_TYPE)){
|
||||
$$ = $10;
|
||||
} 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));
|
||||
@ -659,6 +672,7 @@ argument_list:
|
||||
//NEED TO EMIT PARAMETERS HERE. MAYBE USE STACK STRUCTURE
|
||||
expression{
|
||||
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));
|
||||
//S_Push(stack,current);
|
||||
//emit_detach();
|
||||
@ -825,11 +839,12 @@ 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");
|
||||
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 {
|
||||
$$=undefined;
|
||||
@ -843,7 +858,7 @@ 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));
|
||||
$$ = node;
|
||||
|
||||
} else {
|
||||
|
@ -69,6 +69,13 @@ void emit_detach(){
|
||||
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;
|
||||
}
|
||||
@ -480,21 +487,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:
|
||||
|
@ -108,13 +108,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;
|
||||
@ -159,3 +159,4 @@ 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);
|
||||
|
@ -102,7 +102,10 @@ void print_tok(int tok) {
|
||||
int run(FILE *alpha) {
|
||||
int token;
|
||||
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 (alpha == NULL) {
|
||||
|
Reference in New Issue
Block a user