/* * allocate a quad5 function $$.quad = new_quad5( IFTRUE, $3.quad, $5.quad, 0); // if $$.quad = new_quad5( IFTRUE, $3.quad, $5.quad, $7.quad); // if else */ QUAD *new_quad5( int operator, QUAD *q1, QUAD *q2, QUAD *q3) { QUAD *me; QUAD *last1 = end_quad_list( q1); QUAD *last2 = end_quad_list( q2); QUAD *label1 = new_quad( LABEL, TYPE_LABEL, next_label(), 0,0, 0,0); QUAD *last3; QUAD *label2; QUAD *theGoto; if ( q3 == 0){ me = new_quad( operator, last1->dst_type, last1->dst_index, label1->dst_type, label1->dst_index, 0, 0); } else { last3 = end_quad_list( q3); label2 = new_quad( LABEL, TYPE_LABEL, next_label(), 0,0, 0,0); theGoto = new_quad( GOTO, TYPE_LABEL, label2->dst_index, 0,0, 0,0); me = new_quad( operator, last1->dst_type, last1->dst_index, label1->dst_type, label1->dst_index, label2->dst_type, label2->dst_index); } last1->next = me; me->next = q2; if ( q3 != 0){ last2->next = theGoto; theGoto->next= label1; label1->next = q3; last3->next = label2; } else { last2->next = label1; } return q1; }
/* * allocate a quad8 function $$.quad = new_quad8( ']', $1.index, $3.quad, 0); */ QUAD *new_quad8( int operator, int index, QUAD *q1, QUAD *q2) { // declare variables QUAD *newQuad; QUAD *multQuad; QUAD *last1 = end_quad_list( q1); char size[2]; int multIndex; // make new quad(s) if ( q2 == 0){ size[0] = (char)data.st[ index].specifier+48; size[1] = 0; multIndex= install( TYPE_CONSTANT, size, 2, FORMAT_DECIMAL); multQuad = new_quad( '*', TYPE_TEMPORARY, next_temp(), last1->dst_type, last1->dst_index, TYPE_CONSTANT, multIndex); newQuad = new_quad( operator, TYPE_TEMPORARY, next_temp(), data.st[ index].type, index, multQuad->dst_type, multQuad->dst_index); } else { } // link up new quad and old quads correctly last1->next = multQuad; multQuad->next = newQuad; // return the head quad in the linked list return q1; }
/* * allocate a quad3 function $$.quad = new_quad3( '=', $1.index, $3.quad); */ QUAD *new_quad3( int operator, int index, QUAD *q1) { QUAD *quad; if ( q1 == 0){ quad = new_quad(operator, TYPE_TEMPORARY, next_temp(), data.st[ index].type, index, 0, 0); return quad; } else { QUAD *quad1 = end_quad_list( q1); // optimization attempt if (quad1->dst_type == TYPE_TEMPORARY){ data.temp--; quad1->dst_type = data.st[ index].type; quad1->dst_index = index; return q1; } // normal quad-making quad = new_quad( operator, data.st[ index].type, index, quad1->dst_type, quad1->dst_index, 0, 0); quad1->next = quad; } return q1; }
// operator = IF // q1 = if (bla) // q2 = then bla QUAD *new_quad5(int operator, QUAD *q1, QUAD *q2) { int goto_label = next_label(); QUAD *ifstatement, *thenstatement; ifstatement = end_quad_list(q1); ifstatement->next = new_quad(operator, ifstatement->dst_type, ifstatement->dst_index, TYPE_LABEL, goto_label, 0, 0); ifstatement->next->next = q2; thenstatement = end_quad_list(ifstatement); thenstatement->next = new_quad(LABEL, TYPE_LABEL, goto_label, 0, 0, 0, 0); return q1; }
/* * allocate a quad1 function $$.quad = new_quad1( '+', $1.quad, $3.quad); */ QUAD *new_quad1( int operator, QUAD *q1, QUAD *q2) { QUAD *quad; // optimized attempt for both passed quads as ident/numbers if ( q1->operator == '=' && q1->dst_type == TYPE_TEMPORARY && ( q1->op1_type == TYPE_IDENTIFIER || q1->op1_type == TYPE_CONSTANT) && q2->operator == '=' && q2->dst_type == TYPE_TEMPORARY && ( q2->op1_type == TYPE_IDENTIFIER || q2->op1_type == TYPE_CONSTANT)) { data.temp--; data.temp--; quad = new_quad( operator, TYPE_TEMPORARY, next_temp(), q1->op1_type, q1->op1_index, q2->op1_type, q2->op1_index); free_quad( q1); free_quad( q2); return quad; } // another optimization attempt, for single passed quad as ident/number (q2) if ( q2->operator == '=' && q2->dst_type == TYPE_TEMPORARY && ( q2->op1_type == TYPE_IDENTIFIER || q2->op1_type == TYPE_CONSTANT)) { data.temp--; QUAD *l1 = end_quad_list( q1); quad = new_quad( operator, TYPE_TEMPORARY, data.temp, l1->dst_type, l1->dst_index, q2->op1_type, q2->op1_index); free_quad( q2); l1->next = quad; return q1; } // another optimization attempt, for single passed quad as ident/number (q1) if ( q1->operator == '=' && q1->dst_type == TYPE_TEMPORARY && ( q1->op1_type == TYPE_IDENTIFIER || q1->op1_type == TYPE_CONSTANT)) { data.temp--; QUAD *l2 = end_quad_list( q2); quad = new_quad( operator, TYPE_TEMPORARY, data.temp, q1->op1_type, q1->op1_index, l2->dst_type, l2->dst_index); free_quad( q1); l2->next = quad; return q2; } // previous, inefficient thing QUAD *last1 = end_quad_list( q1); QUAD *last2 = end_quad_list( q2); quad = new_quad( operator, TYPE_TEMPORARY, next_temp(), last1->dst_type, last1->dst_index, last2->dst_type, last2->dst_index); last1->next = q2; last2->next = quad; return q1; }
// operator = IF // q1 = if (bla) // q2 = then bla // q3 = else bla QUAD *new_quad5E(int operator, QUAD *q1, QUAD *q2, QUAD *q3) { int if_goto = next_label(); int then_goto = next_label(); QUAD *ifstatement, *thenstatement, *elsestatement; ifstatement = end_quad_list(q1); ifstatement->next = new_quad(operator, ifstatement->dst_type, ifstatement->dst_index, TYPE_LABEL, if_goto, 0, 0); ifstatement->next->next = q2; thenstatement = end_quad_list(ifstatement); thenstatement->next = new_quad(GOTO, TYPE_LABEL, then_goto, 0, 0, 0, 0); thenstatement->next->next = new_quad(LABEL, TYPE_LABEL, if_goto, 0, 0, 0, 0); thenstatement->next->next->next = q3; end_quad_list(q3)->next = new_quad(LABEL, TYPE_LABEL, then_goto, 0, 0, 0, 0); return q1; }
/* * allocate a quad3 function $$.quad = new_quad3( '=', $1.index, $3.quad); */ QUAD *new_quad3( int operator, int index, QUAD *q1) { QUAD *quad; if ( q1 == 0){ quad = new_quad(operator, TYPE_TEMPORARY, next_temp(), data.st[ index].type, index, 0, 0); return quad; } else { QUAD *quad1 = end_quad_list( q1); quad = new_quad( operator, data.st[ index].type, index, quad1->dst_type, quad1->dst_index, 0, 0); quad1->next = quad; } return q1; }
/* * allocate a quad3 function $$.quad = new_quad3( '=', $1.index); */ QUAD *new_quad3( int operator, int index) { QUAD *quad; quad = new_quad( operator, TYPE_TEMPORARY, next_temp(), find_symbol_table_index(index)->type, index, 0, 0); return quad; }
/* * allocate a quad2 function $$.quad = new_quad2( '~', $1.quad); */ QUAD *new_quad2( int operator, QUAD *q1) { QUAD *quad1; quad1 = end_quad_list( q1); quad1->next = new_quad( operator, TYPE_TEMPORARY, next_temp(), quad1->dst_type, quad1->dst_index, 0, 0); return q1; }
/* * allocate a quad2 function $$.quad = new_quad2( '~', $1.quad); */ QUAD *new_quad2( int operator, QUAD *q1) { QUAD *quad; QUAD *quad1 = end_quad_list( q1); quad = new_quad( operator, quad1->dst_type, quad1->dst_index, 0, 0, 0, 0); q1->next = quad; return q1; }
/* * allocate a quad1 function $$.quad = new_quad1( '+', $1.quad, $3.quad); */ QUAD *new_quad1( int operator, QUAD *q1, QUAD *q2) { QUAD *quad; QUAD *last1 = end_quad_list( q1); QUAD *last2 = end_quad_list( q2); quad = new_quad( operator, TYPE_TEMPORARY, next_temp(), last1->dst_type, last1->dst_index, last2->dst_type, last2->dst_index); last1->next = q2; last2->next = quad; return q1; }
/* * allocate a quad1 function $$.quad = new_quad1( '+', $1.quad, $3.quad); */ QUAD *new_quad1( int operator, QUAD *q1, QUAD *q2) { QUAD *quad1; QUAD *quad2; quad1 = end_quad_list( q1); quad1->next = q2; quad2 = end_quad_list( q2); quad2->next = new_quad( operator, TYPE_TEMPORARY, next_temp(), quad1->dst_type, quad1->dst_index, quad2->dst_type, quad2->dst_index); return q1; }
QUAD *new_quad3A(int operator, int index, QUAD *q1) { QUAD *quad1; quad1 = end_quad_list(q1); quad1->next = new_quad(operator, TYPE_IDENTIFIER, index, quad1->dst_type, quad1->dst_index, 0, 0); return q1; }