From 31bc69d52be7e2748155bf68e3357cefd80718a7 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Sat, 3 May 2025 19:29:38 -0400 Subject: [PATCH] It seems to be working but I need to ckeck again --- src/grammar.y | 114 +++++++++++++++++----- src/intermediate_code.c | 11 ++- src/intermediate_code.h | 1 + tests/sprint3/test/sp3_if_else_sse.alpha | 26 +++++ tests/sprint3/test/sp3_if_else_sse2.alpha | 29 ++++++ tests/sprint3/test/sp3_while_sse.alpha | 27 +++++ 6 files changed, 181 insertions(+), 27 deletions(-) create mode 100644 tests/sprint3/test/sp3_if_else_sse.alpha create mode 100644 tests/sprint3/test/sp3_if_else_sse2.alpha create mode 100644 tests/sprint3/test/sp3_while_sse.alpha diff --git a/src/grammar.y b/src/grammar.y index 7de731e..b2cc77b 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -578,23 +578,8 @@ compound_statement: S_Push(TrueList, S_Init(), 0); S_Push(FalseList, S_Init(), 0); }expression R_PAREN THEN { - emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $4)); - 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); emit_label(label_gen()); emit_backpatch(S_Pop(TrueList), getLabel(current)); - } sblock ELSE { // NOTE we are not going back to int l = label_gen(); @@ -868,11 +853,47 @@ expression: | expression AND expression { +// ---------------------------------------------------------------------------- + emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $1)); + Stack * t1 = S_Peek(TrueList); + if(t1==NULL){ + t1 = S_Init(); + S_Push(TrueList, t1, 1); + } + S_Push(t1, current, 1); + emit_goto(0); + t1 = S_Peek(FalseList); + if(t1==NULL){ + t1 = S_Init(); + S_Push(FalseList, t1, 1); + } + S_Push(t1, current, 1); + emit_label(label_gen()); + emit_backpatch(S_Pop(TrueList), getLabel(current)); + + emit_conditional_jump(E_IF_X_TRUE, 0, 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); + S_Merge(FalseList); + emit_label(label_gen()); + emit_backpatch(S_Pop(TrueList), getLabel(current)); +// ---------------------------------------------------------------------------- printdebug("AND"); + char* temp = temp_var_gen(); + TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); if((getTypeEntry((TableNode*)$1)==getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == boo) { - char* temp = temp_var_gen(); - TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); - emit_binary_op(E_AND,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3)); + //emit_binary_op(E_AND,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3)); $$ = node; } else { $$=undefined; @@ -882,11 +903,47 @@ expression: | expression OR expression { +// ---------------------------------------------------------------------------- + emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, $1)); + Stack * t1 = S_Peek(TrueList); + if(t1==NULL){ + t1 = S_Init(); + S_Push(TrueList, t1, 1); + } + S_Push(t1, current, 1); + emit_goto(0); + t1 = S_Peek(FalseList); + if(t1==NULL){ + t1 = S_Init(); + S_Push(FalseList, t1, 1); + } + S_Push(t1, current, 1); + emit_label(label_gen()); + emit_backpatch(S_Pop(FalseList), getLabel(current)); + + emit_conditional_jump(E_IF_X_TRUE, 0, 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); + S_Merge(TrueList); + // This is very inportant. + // With OR W DO NOT BP THE TRUELIST Only with AND +// ---------------------------------------------------------------------------- printdebug("OR"); if((getTypeEntry((TableNode*)$1)==getTypeEntry((TableNode*)$3)) && getTypeEntry((TableNode*)$1) == boo) { char* temp = temp_var_gen(); TableNode* node = CreateEntry(cur,TYPE_PRIMITIVE, boo, temp, NULL); - emit_binary_op(E_OR,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3)); + // emit_binary_op(E_OR,node,tn_or_const(NODE,$1),tn_or_const(NODE,$3)); $$ = node; } else { $$=undefined; @@ -896,11 +953,10 @@ expression: | expression LESS_THAN expression { -// ---------------------------------------------------------------------------- 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)); - /* emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, node)); Stack * t = S_Peek(TrueList); if(t==NULL){ @@ -915,6 +971,9 @@ expression: S_Push(FalseList, t, 1); } S_Push(t, current, 1); + // emit_label(label_gen()); + // emit_backpatch(S_Pop(TrueList), getLabel(current)); + /* */ // ---------------------------------------------------------------------------- printdebug("less than expression"); @@ -930,13 +989,11 @@ expression: | expression EQUAL_TO expression { - printdebug("equals check 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)); + emit_conditional_jump(E_IF_X_TRUE, 0, tn_or_const(NODE, node)); Stack * t = S_Peek(TrueList); if(t==NULL){ t = S_Init(); @@ -950,7 +1007,12 @@ expression: S_Push(FalseList, t, 1); } S_Push(t, current, 1); - */ + // emit_label(label_gen()); + // emit_backpatch(S_Pop(TrueList), getLabel(current)); +// ---------------------------------------------------------------------------- + printdebug("equals check expression"); + if(getTypeEntry((TableNode*)$1) == getTypeEntry((TableNode*)$3) && getTypeEntry((TableNode*)$1) != undefined) { + // emit_binary_op(E_EQUAL_TO, node, tn_or_const(NODE,$1), tn_or_const(NODE,$3)); $$ = node; } else { diff --git a/src/intermediate_code.c b/src/intermediate_code.c index c807096..62fb563 100644 --- a/src/intermediate_code.c +++ b/src/intermediate_code.c @@ -58,7 +58,16 @@ int S_Size(Stack *s){ return s->size; } -void S_Merge(Stack *s1, Stack *s2){ +void S_Merge(Stack *list){ + Stack* s1 = S_Pop(list); + Stack* s2 = S_Peek(list); + if(s1 == NULL){ + return; + } + if(s2 == NULL){ + S_Push(list, s1, 0); + return; + } for (Instruction * i = S_Pop(s1); i; i = S_Pop(s1)){ S_Push(s2, i, 1); } diff --git a/src/intermediate_code.h b/src/intermediate_code.h index 0906cb1..4e241fe 100644 --- a/src/intermediate_code.h +++ b/src/intermediate_code.h @@ -32,6 +32,7 @@ void * S_Pop(Stack *s); void * S_Peek(Stack *s); bool S_IsEmpty(Stack *s); int S_Size(Stack *s); +void S_Merge(Stack *list); //______________________________________________________________________________________________ typedef union TNConstUnion TNConstUnion; diff --git a/tests/sprint3/test/sp3_if_else_sse.alpha b/tests/sprint3/test/sp3_if_else_sse.alpha new file mode 100644 index 0000000..24ec9ee --- /dev/null +++ b/tests/sprint3/test/sp3_if_else_sse.alpha @@ -0,0 +1,26 @@ +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) := { + [ Boolean:x ; Boolean:y ; Boolean:z ; Boolean:t] + if ( !( x < y ) & ( z & t ) ) then { + (* if ( result < w.y ) then { + result := 8; + } else { + result := 9; + } *) + z := t; + } else { + y := true; (* bar('c', 7); *) + } + return 0; +} diff --git a/tests/sprint3/test/sp3_if_else_sse2.alpha b/tests/sprint3/test/sp3_if_else_sse2.alpha new file mode 100644 index 0000000..c26ce11 --- /dev/null +++ b/tests/sprint3/test/sp3_if_else_sse2.alpha @@ -0,0 +1,29 @@ +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) := { + [ Boolean:x ; Boolean:y ; Boolean:z ; Boolean:t] + if ( y & z | t ) then { + (* if ( ( x < y ) & ( z = t ) ) then { + y := z < t; + t:= 0; + } else { + t := z = t; + z := 1; + } *) + t := true; + } else { + y := true; (* bar('c', 7); *) + } + (* x := x & y; *) + return 0; +} diff --git a/tests/sprint3/test/sp3_while_sse.alpha b/tests/sprint3/test/sp3_while_sse.alpha new file mode 100644 index 0000000..89fa23f --- /dev/null +++ b/tests/sprint3/test/sp3_while_sse.alpha @@ -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) := { + [ Boolean:x ; Boolean:y ; Boolean:z ; Boolean:t] + while ( !( x & y ) (* | ( z | t ) *) ) { + (* + if ( ( x < y ) & ( z = t ) ) then { + y := z < t; + } else { + t := z = t; + } + *) + y := true; (* bar('c', 7); *) + } + (* x := x & y; *) + return 0; +}