void nt_var_def (token tok) { ///////////////////////////////////////////////////////////////////////RULE6 if (tok -> type == t_var_id) { /* Pripravím si key pre ukladanie */ char * key = createKey ("V", tok -> val_str); /* Rozhodnem sa s ktorou tabulkou idem pracovať */ tNodePtr * currTS = NULL; currTS = (searchGlobalOnly == true) ? &rootTS : &localTS; /* Uložím premennú do odpovedajúcej tabulky symbolov */ saveSymbol (&*currTS, key, tok -> val_str, t_var_id, 0, true); /* Porovnanie terminálov */ match (tok, t_var_id); match (tok, t_colon); /* Priradenie typu práve uloženej premennej */ /* searchType=1 hovorí, že ide o premennú */ nt_type (tok, key); /* Porovnanie zvyšného terminálu */ match (tok, t_semicolon); //Sem pride instrukcia tData currentVar = searchSymbol(&*currTS, key)->data; if (localIL==NULL){ insertInst (&IL, I_VAR, currentVar, NULL, NULL); printf("GLOBAL\n");printf("Vlozil som instrukciu I_VAR s ukazatelom %u do IL %u\n", ¤tVar,&IL); } else{ insertInst (localIL, I_VAR, currentVar, NULL, NULL); printf("LOCAL\n");printf("Vlozil som instrukciu I_VAR s ukazatelom %u do IL %u\n", ¤tVar,localIL); } free (key); } else { printf ("nt_var_def\n"); errorHandler (errSyn); } }
SsaOpnd* OSR::apply(Type* type, Opcode opcode, Operation op, SsaOpnd* opnd1, SsaOpnd* opnd2) { SsaOpnd* opnd2Leader = getLeadingOperand(opnd2); if (opnd2Leader!= 0) { opnd2 = opnd2Leader; } opnd1 = (SsaOpnd*) DeadCodeEliminator::copyPropagate(opnd1); if (Log::isEnabled()) { Log::out() << "Applying: "; opnd1->print(Log::out()); Log::out() << std::endl; opnd2->print(Log::out()); Log::out() << std::endl; } Inst* newinst = hashTable->lookup(op.encodeForHashing(), opnd1->getId(), opnd2->getId()); SsaOpnd* result = 0; if (newinst) { result = newinst->getDst()->asSsaOpnd(); } else { if (getLeadingOperand(opnd1) && isLoopInvariant(opnd2)) { result = reduce(type, opcode, op, opnd1, opnd2); } else if (getLeadingOperand(opnd2) && isLoopInvariant(opnd1)) { result = reduce(type, opcode, op, opnd2, opnd1); } else { OpndManager & opndManager = irManager.getOpndManager(); result = opndManager.createSsaTmpOpnd(type); InstFactory & instFactory = irManager.getInstFactory(); opnd1 = makeTmp(opnd1, opnd1->getInst()); opnd2 = makeTmp(opnd2, opnd2->getInst()); Inst* newInst = instFactory.makeInst(opcode, op.getModifier(), type->tag, result, opnd1, opnd2); Inst* instLoc = findInsertionPlace(opnd1, opnd2); insertInst(newInst, instLoc); writeLeadingOperand(result, 0); hashTable->insert(op.encodeForHashing(), opnd1->getId(), opnd2->getId(), newInst); writeLFTR(opnd1, op, opnd2, newInst->getDst()->asSsaOpnd()); } } return result; }
SsaTmpOpnd* OSR::makeTmp(SsaOpnd* inOpnd, Inst* place) { if (inOpnd->isSsaVarOpnd()) { SsaVarOpnd* inSsaVarOpnd = inOpnd->asSsaVarOpnd(); Inst* inst = inSsaVarOpnd->getInst(); if (inst->getOpcode() == Op_StVar) { SsaTmpOpnd* res = inst->getSrc(0)->asSsaTmpOpnd(); return res; } else { OpndManager& opndManager = irManager.getOpndManager(); InstFactory& instFactory = irManager.getInstFactory(); SsaTmpOpnd* res = opndManager.createSsaTmpOpnd(inOpnd->getType()); Inst* ldVarInst = instFactory.makeLdVar(res, inSsaVarOpnd); Inst* where = chooseLocationForConvert(inst, place); insertInst(ldVarInst, where); writeLeadingOperand(res, getLeadingOperand(inOpnd)); return res; } } else { return inOpnd->asSsaTmpOpnd(); } }
int nt_assign (token tok) { if (tok->type == t_expr_int || tok->type == t_expr_str || tok->type == t_expr_dou || tok->type == t_fun_id || tok->type == t_var_id || tok->type == t_l_parrent) { ///////////////////////////////////////////////////////////////////////RULE23 if (tok->type == t_expr_int || tok->type == t_expr_str || tok->type == t_expr_dou || tok->type == t_var_id || tok->type == t_l_parrent) { //printf("TYPE JE %d\n",tok->type); //printf("--idem priradenie\n"); //printf("--idem skusit precedenceParser s tokentom %d \n",tok->type); //printf("localTS je momentalne %d\n",&*localTS ); int kokot = precedenceParser(NULL); //printf("--precedenceParser presiel a vratil: %d\n",kokot ); //printf("--stav tok je %d\n",tok->type ); //int typ=tok->type; //match(tok,tok->type); terminalis(kokot,NULL); switch (kokot) { case 41: return sym_var_int;break; case 42: return sym_var_str;break; case 43: return sym_var_rea;break; case 44: return sym_var_boo;break; default: errorHandler(errSemDef);return 0; } } ////////////////////////////////////////////////////////////////////////RULE24 else { char * key = createKey ("F",tok->val_str); tNodePtr hledam = searchSymbol(&rootTS, key); //printf("IDEM HLADAT %s\n",key ); if (hledam==0) { printf("Hladal som key %s\n",key ); errorHandler(errSemDef); } //printf("\n funkcia %s ma %d argumentu \n",key,hledam->data->argCount ); match(tok,t_fun_id); match(tok,t_l_parrent); //printf("idem vytvorit pole\n"); tContent **contentArr=malloc(sizeof(struct tContent*)*100); printf("vytvoril som doublepole %u\n____________________\n",&contentArr); //globalArr=contentArr; contentArr[0]=&hledam->data->nextArg->data->content; //elegeblege[0]=; printf("KOKOOOOOOOOOOOOOOOOT %d\n",contentArr[0]->integer); /*for (int i = 0; i<100; i++) { contentArr[i]=malloc(sizeof(struct tContent)); }*/ //struct tContent *(*contentArrPtr)[] = &contentArr; nt_term_list(tok, key, contentArr); pocetArg = 0; //printf("factorial ma content %d\n", hledam->data->content.integer); //termy su overene idem ich nahradit printf("\nhledam->data->argCount je %d\n",hledam->data->argCount); j=0; //povodne argsRead=0; match(tok,t_r_parrent); //treba ale este predat premenne if (localIL==NULL) { insertInst (&IL, I_FCE, hledam->data, contentArr, NULL); printf("GLOBAL\n");printf("Vlozil som instrukciu I_FCE s ukazatelom %u do IL %u\n", &hledam->data,&IL); } else { insertInst (localIL, I_FCE, hledam->data, contentArr, NULL); printf("GLOBAL\n");printf("Vlozil som instrukciu I_FCE s ukazatelom %u do IL %u\n", &hledam->data,localIL); } free(key); return hledam->data->type; } } else { printf("syn error in nt_assign\n"); errorHandler(errSyn); } return -1; }
void nt_stmt (token tok) { if (tok->type == t_if || tok->type == t_begin || tok->type == t_write || tok->type == t_while || tok->type == t_readln || tok->type == t_var_id ) { tNodePtr hledam = NULL; tNodePtr *currTS = NULL; char * key = NULL; int precedenceResult = 0; switch (tok->type) { ////////////////////////////////////////////////////////////////////////////////RULE17 case 8: nt_body (tok); break; ////////////////////////////////////////////////////////////////////////////////RULE18 case 20: key = createKey ("V", tok->val_str); currTS = (searchGlobalOnly == true) ? &rootTS : &localTS; hledam = searchSymbol (&*currTS, key); if (hledam == 0 && searchGlobalOnly == false) hledam = searchSymbol(&rootTS, key); if (hledam!=0) if (hledam->data->type==t_var_id || (hledam->data->type >= 1 && hledam->data->type <= 4)) { //printf("\n\nSnazim sa priradit do premennej typu %d\n\n",hledam->data->type); match (tok,t_var_id); } else { printf ("nedefnovana premenna\n"); errorHandler (errSemDef); return; } match (tok,t_assign); //SEMANTICKA KONTROLA int semControlVar = nt_assign (tok); //printf("semkontrol je %d\n",semControlVar ); //printf("skontroloval som\n"); if (semControlVar != hledam->data->type+4 && semControlVar != hledam->data->type+12 && semControlVar != hledam->data->type) { printf("nesedeli typy\n"); errorHandler (errSemTypArg); } //else printf("ok\n"); //Tu je rozhodovanie ktoru instrukciu mam zavolat podla typu precedencie int intype=0; switch (semControlVar) { case 1: intype=I_ASGNI;break; case 3: intype=I_ASGNS;break; case 2: intype=I_ASGNR;break; case 4: intype=I_ASGNB;break; } if (localIL==NULL) insertInst (&IL, intype, NULL, NULL, &hledam); else insertInst (&IL, intype, NULL, NULL, &hledam); break; free (key); break; ////////////////////////////////////////////////////////////////////////////////RULE19 case 13: match (tok,t_if); //printf("local ts je %d\n",&localTS ); precedenceResult = precedenceParser(); if (precedenceResult!=t_expr_boo)//docasne sem dam integer lebo bool este neni hotovy v precedenci ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ errorHandler(errSemTypArg); //Volanie instrukcie na bool spravil filip terminalis(precedenceResult,NULL); match (tok,t_then); //Interpret musi tieto dve tabulky potom uvolnit //Tabulka inst pre then tInsList thenIL;//vytvorim novu tabulku tInsList *revert=&*localIL;//odpamatam su aktualnu lokalnu InitList (&thenIL);//inicializujem novu localIL=&thenIL;//nova sa stane aktivnou lokalnou printf("localIL je teraz %u\n",&*localIL ); nt_body (tok);//nt body donej nahadze instrukcie z tela thenu localIL=revert;//obnovi sa povodna lokalna printf("localIL je teraz %u\n",&*localIL ); match (tok,t_else); //Tabulka inst pre else tInsList elseIL; //vytvorim novu tabulku revert=&*localIL;//odpamatam su aktualnu lokalnu InitList (&elseIL);//inicializujem novu localIL=&elseIL; //nova sa stane aktivnou lokalnou printf("localIL je teraz %u\n",&*localIL ); nt_body (tok); //nt body donej nahadze instrukcie z tela elsu localIL=revert; //obnovi sa povodna lokalna printf("localIL je teraz %u\n",&*localIL ); //volanie instrukcie if (localIL==NULL) insertInst (&IL, I_IF, &thenIL, &elseIL, NULL); else insertInst (&*localIL, I_IF, &thenIL, &elseIL, NULL); break; ////////////////////////////////////////////////////////////////////////////////RULE20 case 16: match (tok,t_while); precedenceResult = precedenceParser(); if (precedenceResult!=t_expr_boo)//docasne sem dam integer lebo bool este neni hotovy v precedenci ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ errorHandler(errSyn); terminalis(precedenceResult,NULL); match (tok,t_do); nt_body (tok); break; ////////////////////////////////////////////////////////////////////////////////RULE21 case 18: match (tok,t_readln); match (tok,t_l_parrent); //Kontrola či premenná už je v tabulke key = createKey ("V", tok->val_str); currTS = (searchGlobalOnly == true) ? &rootTS : &localTS; hledam = searchSymbol (&*currTS, key); if (hledam == 0 && searchGlobalOnly == false) hledam = searchSymbol(&rootTS, key); if (hledam!=0) if (hledam->data->type==t_var_id || (hledam->data->type >= 1 && hledam->data->type <= 4))//toto reaguje len na typove premenne { match (tok,t_var_id); } else { printf ("nedefnovana premenna pre readln\n"); errorHandler (errSemDef); return; } //SEMANTICKA KONTROLA if (hledam->data->type < 1 || hledam->data->type > 3) { printf("tuna nemoze byt bool\n"); errorHandler (errSemTypArg); } match (tok,t_r_parrent); free (key); break; ////////////////////////////////////////////////////////////////////////////////RULE22 case 19: match (tok,t_write); match (tok,t_l_parrent); tContent **doublePointer=malloc(sizeof(struct tContent*)*100); nt_term_list (tok, "Fwrite", doublePointer); /*bolo tu povodne argc*/ pocetArg = 0; match (tok,t_r_parrent); break; } } else { printf("syntax error in nt_stmt\n"); errorHandler(errSyn); } }
Inst* OSR::insertNewDef(Type* type, SsaOpnd* iv, SsaOpnd* rc) { Inst* inst = copyInst(type, iv->getInst(), rc, Operation(iv->getInst()->getOpcode())); insertInst(inst, iv->getInst()); return inst; }