70 likes | 163 Views
Parsing and Code Generation: Procedures. System Software Fall 2012. New dynamics. Adding procedures allows for new levels of complexity that must now be accounted for.
E N D
Parsing and Code Generation:Procedures System Software Fall 2012
New dynamics • Adding procedures allows for new levels of complexity that must now be accounted for. • The lexical level must be maintained to correctly identify the scope of identifiers and make the correct calls from the symbol table. • The code will now call BLOCK at multiple lexical levels.
Procedure • Procedures are declared inside BLOCK • Must be accounted for in the symbol table • Procedure calls create new calls to BLOCK • Variables calls should be checked from the current lexical level first, down to the older level of the stack.
Parsing a Procedure Decleration while (token->tokenType == procsym){ token = getNextToken (token); if (token->tokenType != identsym){ error(4); printf("ERROR identifier should follow procedure"); } else{ makeSymbol(token, procsym,cx+1) lev++; token = getNextToken(token); if ( token->tokenType != semicolonsym){ printf("ERROR ; or , missing"); } Continue on next slide
Parsing a Procedure Decleration Cont. else{ inttempCX = cx; emit(JMP,0,0); //Move past the procedure if you didn’t call it token = getNextToken(token); token = block (token); //Parse and generate code inside procedure emit(OPR,0,0); //Emit return from procedure call codeStack[tempCX].m = cx; //Now that procedure is parsed save current stack value lev--; to address of jump if (token->tokenType != semicolonsym){ error(5); printf("ERROR ; or , missing"); } else token = getNextToken(token); }
Encountering a Procedure Call Statement{ … else if (token->tokenType == callsym){ token = getNextToken(token); struct token *temp = token; //TA note – This is not necessary //Maybe they are afraid of losing //the head pointer if (token->tokenType != identsym){ error(14); printf("ERROR Missing Indentifier"); } else{ emit(CAL, getLevel(temp) , getAddress(temp)); token = getNextToken(token); } }
Supporting Function Rolls • Get level is being called on a token that is guaranteed to be an identifier • It is searching the symbol table and returning return global - symTable[i].level; • Get address: return symTable[i].addr;