1 / 7

Parsing and Code Generation: Procedures

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.

weston
Download Presentation

Parsing and Code Generation: Procedures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parsing and Code Generation:Procedures System Software Fall 2012

  2. 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.

  3. 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.

  4. 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

  5. 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); }

  6. 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); } }

  7. 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;

More Related