void AbstractByteCode::GenSendByteCode( fint offset, fint length, stringOop selector, bool isSelfImplicit, bool isUndirectedResend, stringOop resendTarget) { if (resendTarget != NULL) { GenDelegateeByteCode(offset, length, resendTarget); } else if ( isUndirectedResend ) GenUndirectedResendByteCode(offset, length); fint argc = selector->arg_count(); if (GenArgCountBytecode && argc != 0) GenCode(offset, length, BuildCode( ARGUMENT_COUNT_CODE, GenIndex(offset, length, argc))); GenCode(offset, length, BuildCode( isSelfImplicit ? IMPLICIT_SEND_CODE : SEND_CODE, GenIndex(offset, length, GenLiteral(selector)))); if (!isSelfImplicit) --stack_depth; stack_depth -= argc; assert(stack_depth >= 0, "negative stack?"); ++stack_depth; }
void HandleCaseStmt(TreeNode* pnode){ char caseEd[100]; strcpy(caseEd, GetLabel()); GenCode(pnode->child[0]); EMITCODE("pushl %eax\n"); GenCode(pnode->child[1]); }
void HandleForStmt(TreeNode* pnode){ char forSt[100], forEd[100]; int i; strcpy(forSt, GetLabel()); strcpy(forEd, GetLabel()); GenCode(pnode->child[1]); EMITCODE("pushl %eax\n"); GenCode(pnode->child[0]); EMITCODE("popl %eax\n"); EMITCODE("movl %eax, -0(%esi) \n"); if(pnode->attr.op == TOKEN_TO){ sprintf(tmp,"%s:\n",forSt); EMITCODE(tmp); GenCode(pnode->child[2]); EMITCODE("pushl %eax\n"); GenCode(pnode->child[0]); EMITCODE("popl %ebx\n"); EMITCODE("cmpl %ebx, %eax\n"); sprintf(tmp, "ja %s\n", forEd); EMITCODE(tmp); GenCode(pnode->child[3]); GenCode(pnode->child[0]); EMITCODE("inc %eax\n"); EMITCODE("movl %eax, -0(%esi)\n"); sprintf(tmp,"jmp %s\n",forSt); EMITCODE(tmp); sprintf(tmp,"%s:\n",forEd); EMITCODE(tmp); } else { sprintf(tmp,"%s:\n",forSt); EMITCODE(tmp); GenCode(pnode->child[2]); EMITCODE("pushl %eax\n"); GenCode(pnode->child[0]); EMITCODE("popl %ebx\n"); EMITCODE("cmpl %ebx, %eax\n"); sprintf(tmp, "jb %s\n", forEd); EMITCODE(tmp); GenCode(pnode->child[3]); GenCode(pnode->child[0]); EMITCODE("dec %eax\n"); EMITCODE("movl %eax, -0(%esi)\n"); sprintf(tmp,"jmp %s\n",forSt); EMITCODE(tmp); sprintf(tmp,"%s:\n",forEd); EMITCODE(tmp); } }
void HandleRepeatStmt(TreeNode* pnode){ char repeat_start[100]; strcpy(repeat_start, GetLabel()); sprintf(tmp,"%s:\n",repeat_start); EMITCODE(tmp); GenCode(pnode->child[0]); GenCode(pnode->child[1]); EMITCODE("cmpl $0, %eax\n"); sprintf(tmp,"je %s\n",repeat_start); EMITCODE(tmp); }
void HandleCaseExp(TreeNode* pnode){ char case_end[100]; strcpy(case_end, GetLabel()); GenCode(pnode->child[0]); EMITCODE("popl %ebx\n"); EMITCODE("cmpl %ebx,%eax\n"); sprintf(tmp, "jne %s\n", case_end); EMITCODE(tmp); GenCode(pnode->child[1]); sprintf(tmp, "%s:\n", case_end); EMITCODE(tmp); if (pnode->sibling!=NULL) EMITCODE("pushl %ebx\n"); }
void HandleAssignStmt(TreeNode* pnode){ FuncList r_ssfuc=NULL; VariableList l_ssvar=varListLookup((pnode->child[0])->attr.name); if ((pnode->child[1])->nodekind==2&&(pnode->child[1])->kind>4) r_ssfuc=funcListLookup((pnode->child[1])->attr.name); GenCode(pnode->child[1]); EMITCODE("pushl %eax\n"); if (l_ssvar==NULL){ if (!pnode->child[1]->ERROR_STATE){ ErrorHandler(ERROR_VAR_MISS, (pnode->child[0])); pnode->child[0]->ERROR_STATE=ERROR_VAR_MISS; pnode->ERROR_STATE=ERROR_VAR_MISS; } return; } else if (l_ssvar->isConst){ if (!(pnode->child[0]->ERROR_STATE||pnode->child[1]->ERROR_STATE)){ ErrorHandler(ERROR_VAR_MODIFYCONST, (pnode->child[0])); pnode->ERROR_STATE=ERROR_VAR_MODIFYCONST; } return; } if (r_ssfuc&&l_ssvar->type<r_ssfuc->retType){ if (!(pnode->child[0]->ERROR_STATE||pnode->child[1]->ERROR_STATE)){ ErrorHandler(ERROR_TYPE_MISMATCH,pnode); pnode->ERROR_STATE=ERROR_TYPE_MISMATCH; } return; } else if (!r_ssfuc&&(l_ssvar->type<5)&&l_ssvar->type<pnode->child[1]->RuningType){ if (!(pnode->child[0]->ERROR_STATE||pnode->child[1]->ERROR_STATE)){ ErrorHandler(ERROR_TYPE_MISMATCH,pnode); pnode->ERROR_STATE=ERROR_TYPE_MISMATCH; } return; } else if (l_ssvar->type==EXPTYPE_REAL&&(pnode->child[1]->RuningType!=EXPTYPE_REAL)){ GenCode(pnode->child[0]); EMITCODE("fildl 0(%esp)\n"); EMITCODE("fstps 0(%esp)\n"); // make integer into float EMITCODE("popl %eax\n"); EMITCODE("movl %eax, -0(%esi)\t# assign\n"); } else { GenCode(pnode->child[0]); EMITCODE("popl %eax\n"); EMITCODE("movl %eax, -0(%esi)\t# assign\n"); } }
void HandleWhileStmt(TreeNode* pnode){ char whileSt[100], whileEd[100]; strcpy(whileSt, GetLabel()); strcpy(whileEd, GetLabel()); sprintf(tmp, "%s:\n", whileSt); EMITCODE(tmp); GenCode(pnode->child[0]); EMITCODE("cmpl $0, %eax\n"); sprintf(tmp,"je %s\n",whileEd); EMITCODE(tmp); GenCode(pnode->child[1]); sprintf(tmp,"jmp %s\n",whileSt); EMITCODE(tmp); sprintf(tmp, "%s:\n", whileEd); EMITCODE(tmp); }
void AbstractByteCode::GenLiteralByteCode(fint offset, fint length, oop literal) { GenCode(offset, length, BuildCode(LITERAL_CODE, GenIndex(offset, length, GenLiteral(literal)))); ++stack_depth; }
int Compile(unsigned char* string, int len, unsigned char *fileName, wchar_t *fileNameW) { Token *tokens; int numTokens; int numErrors = Tokenize(string, len, tokens, numTokens, fileNameW); if (numErrors) { CleanupTokens(tokens, numTokens); return 0; } /* Token test[100]; test[0].type = ONE; test[1].type = PLUS; test[2].type = ONE; test[3].type = DOLLAR; // TreeNode *t = ParseScript(test, 4); //*/ ParseTree *tree = ParseScript(tokens, numTokens); if (!tree) { return 0; } TempCode * tempCode = GenTempCode(tree); CleanupTree(tree); if (!tempCode) { return 0; } if (!GenCode(tempCode, string, len, fileName)) { return 0; } return 1; }
void HandleLabelStmt(TreeNode* pnode){ char sysLabel[100]; strcpy(sysLabel, GetSysLabel(pnode->attr.val)); sprintf(tmp, "%s:\n", sysLabel); EMITCODE(tmp); GenCode(pnode->child[0]); }
void AbstractByteCode::GenRWLocalByteCode( fint offset, fint length, bool isRead, int32 lexicalLevel, int32 index) { assert(lexicalLevel >= 0 && index >= 0, "just checkin'"); if ( lexicalLevel > 0 ) GenCode(offset, length, BuildCode(LEXICAL_LEVEL_CODE, GenIndex( offset, length, lexicalLevel ))); GenCode(offset, length, BuildCode(isRead ? READ_LOCAL_CODE : WRITE_LOCAL_CODE, GenIndex( offset, length, index ))); if ( isRead ) ++stack_depth; }
void TreeNode::CodeGeneration(CodeEmitter &e) { GenProlog(jumpMain, e); GenIOFunctions(e); // Traverse the tree GenCode(e, true, 0, 0); }
int CG_main(TreeNode* pnode,char * ffname){ int paraSize; char buf[10000]; codename = fopen("code_part.asm", "w"); dataname = fopen("data_part.asm", "w"); EMITDATA(".INOUT_I:\n"); EMITDATA("\t.string \"%i\"\n"); EMITDATA(".OUTPUT_I_N:\n"); EMITDATA("\t.string \"%i\\n\"\n"); EMITDATA(".INOUT_C:\n"); EMITDATA("\t.string \"%c\"\n"); EMITDATA(".OUTPUT_C_N:\n"); EMITDATA("\t.string \"%c\\n\"\n"); EMITDATA(".INOUT_F:\n"); EMITDATA("\t.string \"%f\"\n"); EMITDATA(".OUTPUT_F_N:\n"); EMITDATA("\t.string \"%f\\n\"\n"); EMITDATA(".INOUT_S:\n"); EMITDATA("\t.string \"%s\"\n"); EMITDATA(".OUTPUT_S_N:\n"); EMITDATA("\t.string \"%s\\n\"\n"); EMITCODE(".text\n"); initScope(); pnode->attr.name="main"; GenCode(pnode); paraSize=leaveScope(); sprintf(tmp, "addl $%d, %%esp\n", paraSize); EMITCODE(tmp); EMITCODE("leave\n"); EMITCODE("ret\n"); fclose(codename); fclose(dataname); if (!COMPILE_ERROR){ ff = fopen(ffname, "w"); codename = fopen("code_part.asm", "r"); dataname = fopen("data_part.asm", "r"); while(fgets(buf,BUFSIZ,dataname)!=NULL){ fputs(buf,ff); } while(fgets(buf,BUFSIZ,codename)!=NULL){ fputs(buf,ff); } fclose(codename); fclose(dataname); fclose(ff); } }
void ParserGen::GenProductions() { Symbol *sym; for (int i=0; i<tab->nonterminals->Count; i++) { sym = (Symbol*)((*(tab->nonterminals))[i]); curSy = sym; fwprintf(gen, L"void Parser::%ls(", sym->name); CopySourcePart(sym->attrPos, 0); fwprintf(gen, L") {\n"); CopySourcePart(sym->semPos, 2); GenCode(sym->graph, 2, new BitArray(tab->terminals->Count)); fwprintf(gen, L"}\n"); fwprintf(gen, L"\n"); } }
void HandleIfStmt(TreeNode* pnode){ char ifLabel[100], elseLabel[100], exitLabel[100]; strcpy(ifLabel, GetLabel()); strcpy(elseLabel, GetLabel()); strcpy(exitLabel, GetLabel()); GenCode(pnode->child[0]); EMITCODE("cmpl $1, %eax\n"); sprintf(tmp, "je %s \njmp %s\n", ifLabel, elseLabel); EMITCODE(tmp); sprintf(tmp,"%s:\n",ifLabel); EMITCODE(tmp); GenCode(pnode->child[1]); sprintf(tmp,"jmp %s\n",exitLabel); EMITCODE(tmp); sprintf(tmp, "%s: \n", elseLabel); EMITCODE(tmp); if (pnode->child[2]!=NULL) GenCode(pnode->child[2]); sprintf(tmp, "%s: \n", exitLabel); EMITCODE(tmp); }
bool AbstractByteCode::GenSimpleBranchByteCode( fint offset, fint length, oop label, ByteCodeKind op ) { int32 literalIndex = labelSet->RecordLabelInfo( this, label, stack_depth, simpleBranch, bci() ); if ( literalIndex == -1 ) return false; branchSet->RecordBranch( false, literalIndex, label ); GenCode(offset, length, BuildCode(op, GenIndex( offset, length, literalIndex))); return true; }
bool AbstractByteCode::GenBranchIndexedByteCode( fint offset, fint length, objVectorOop labels) { --stack_depth; assert(stack_depth >= 0, "negative stack?"); int32 literalIndex = labelSet->RecordLabelVectorInfo( this, labels, stack_depth, bci() ); if ( literalIndex == -1 ) return false; branchSet->RecordBranch( true, literalIndex, literals->obj_at(literalIndex) ); GenCode(offset, length, BuildCode(BRANCH_INDEXED_CODE, GenIndex( offset, length, literalIndex))); return true; }
cEqDirecteDistorsion:: cEqDirecteDistorsion ( cParamIntrinsequeFormel & aPIF, eTypeEqDisDirecre Usage, bool Code2Gen ) : mUsage (Usage), mSet (*aPIF.Set()), mPIF (aPIF), mP1 ("PIm1"), mP2 ("PIm2"), mResidu (P1_EDD(mPIF,mP1.PtF(),Usage) - mP2.PtF()), mNameType ( "cEqDirectDist" + mPIF.NameType() + (Usage2Str(Usage)) +"_CodGen" ) { mPIF.IncInterv().SetName("Calib"); mLInterv.AddInterv(mPIF.IncInterv()); mFoncEqResidu = cElCompiledFonc::AllocFromName(mNameType); if (Code2Gen) { GenCode(); return; } if (mFoncEqResidu==0) { std::cout << "Name = " << mNameType << "\n"; ELISE_ASSERT(false,"Can Get Code Comp for cEqCalibCroisee"); mFoncEqResidu = cElCompiledFonc::DynamicAlloc(mLInterv,Fonc_Num(0)); } mFoncEqResidu->SetMappingCur(mLInterv,&mSet); mP1.InitAdr(*mFoncEqResidu); mP2.InitAdr(*mFoncEqResidu); aPIF.InitStateOfFoncteur(mFoncEqResidu,0); mSet.AddFonct(mFoncEqResidu); }
cEqOffsetGPS::cEqOffsetGPS(cRotationFormelle & aRF,cBaseGPS & aBase,bool doGenCode) : mSet (aRF.Set()), mRot (&aRF), mBase (&aBase), mGPS ("GPS"), mNameType ("cEqObsBaseGPS" + std::string(aRF.IsGL() ? "_GL" : "")), mResidu (mRot->C2M(mBase->BaseInc())- mGPS.PtF()), mFoncEqResidu (0) { /* ELISE_ASSERT ( (! aRF.IsGL()), "cEqOffsetGPS to complete in Gimbal Lock Mode" ); */ AllowUnsortedVarIn_SetMappingCur = true; ELISE_ASSERT ( mRot->Set()==mBase->Set(), "cEqOffsetGPS Rotation & Base do no belong to same set of unknown" ); mRot->IncInterv().SetName("Orient"); mBase->IncInterv().SetName("Base"); mLInterv.AddInterv(mRot->IncInterv()); mLInterv.AddInterv(mBase->IncInterv()); if (doGenCode) { GenCode(); return; } mFoncEqResidu = cElCompiledFonc::AllocFromName(mNameType); ELISE_ASSERT(mFoncEqResidu!=0,"Cannot allocate cEqObsBaseGPS"); mFoncEqResidu->SetMappingCur(mLInterv,mSet); // GL mGPS.InitAdr(*mFoncEqResidu); mSet->AddFonct(mFoncEqResidu); }
bool CheckUserCode(char* sUsername, char* code) { if (sUsername == NULL || code == NULL) { printf("CheckUserCode: input username or code is null\n"); return false; } char *check_code = GenCode(sUsername); if (check_code == NULL) { printf("CheckUserCode: Generate check code failed\n"); return false; } if (strcmp(check_code, code)) return false; return true; }
cEqPlanInconnuFormel::cEqPlanInconnuFormel ( cTFI_Triangle * aTri, bool Code2DGen ) : cSurfInconnueFormelle(aTri->Set()), mTri (aTri), mPlanCur (mTri->CalcPlancCurValAsZ()), mNameType ("cCodeGenEqPlanInconnuFormel") { cMatr_Etat_PhgrF aMatEtat("CoefBar",3,3); ELISE_ASSERT(aTri->Dim()==1,"Bad dim in cEqPlanInconnuFormel"); mLInterv.AddInterv(aTri->IntervA1()); mLInterv.AddInterv(aTri->IntervA2()); mLInterv.AddInterv(aTri->IntervA3()); mLInterv.AddInterv(mEqP3I->IncInterv()); mFoncEqResidu = cElCompiledFonc::AllocFromName(mNameType); if (Code2DGen) { GenCode(aMatEtat); return; } if (mFoncEqResidu==0) { ELISE_ASSERT(false,"Can Get Code Comp for cCameraFormelle::cEqAppui"); } mFoncEqResidu->SetMappingCur(mLInterv,&mSet); aMatEtat.InitAdr(*mFoncEqResidu); aMatEtat.SetEtat ( mTri->TriGeom().MatCoeffBarry()); mSet.AddFonct(mFoncEqResidu); }
void AbstractByteCode::GenPopByteCode(fint offset, fint length) { GenCode(offset, length, BuildCode(NO_OPERAND_CODE, POP_CODE)); --stack_depth; assert(stack_depth >= 0, "negative stack?"); }
void AbstractByteCode::GenSelfByteCode(fint offset, fint length) { GenCode(offset, length, BuildCode(NO_OPERAND_CODE, SELF_CODE)); ++stack_depth; }
void GenCode(TreeNode* pnode){ int paraSize; switch (pnode->nodekind){ case NODE_STATEMENT: switch (pnode->kind+(NODE_STATEMENT<<4)){ case STMT_ASSIGN: HandleAssignStmt(pnode); break; case STMT_CASE: HandleCaseStmt(pnode); break; case STMT_FOR: HandleForStmt(pnode); break; case STMT_GOTO: HandleGotoStmt(pnode); break; case STMT_IF: HandleIfStmt(pnode); break; case STMT_LABEL: HandleLabelStmt(pnode); break; case STMT_REPEAT: HandleRepeatStmt(pnode); break; case STMT_WHILE: HandleWhileStmt(pnode); break; case STMT_PROC_ID: HandleProcExc(pnode); break; case STMT_PROC_SYS: switch(pnode->attr.op) { case TOKEN_WRITE: HandleOutStmt(pnode); break; case TOKEN_WRITELN: HandleOutStmt(pnode); break; case TOKEN_READ: HandleInStmt(pnode); break; default: break; } break; } break; case NODE_EXPRESSION: HandleNodeExp(pnode); break; case NODE_DECLARE: switch (pnode->kind+(NODE_DECLARE<<4)){ case DECL_ROUTINEHEAD: paraSize=enterNewScope(pnode); if (pnode->child[3]!=NULL) GenCode(pnode->child[3]); if (strcmp(pnode->attr.name,"main")){ sprintf(tmp, "%s: \n",pnode->attr.name); EMITCODE(tmp); } else { EMITCODE(".globl main\n"); EMITCODE("\t.type main, @function\n"); EMITCODE("main:\n"); EMITCODE("pushl %ebp\n"); EMITCODE("movl %esp, %ebp\n"); EMITCODE("movl %esp, %ecx\n"); } sprintf(tmp, "subl $%d, %%esp\n", paraSize); EMITCODE(tmp); break; case DECL_FUNCTION: funcListInsert(pnode->child[0]); (pnode->child[1])->attr.name=(pnode->child[0])->attr.name; GenCode(pnode->child[1]); paraSize=leaveScope(); sprintf(tmp, "addl $%d, %%esp\n", paraSize); EMITCODE(tmp); EMITCODE("ret\n"); break; case DECL_PROCEDURE: fflush(stdout); procListInsert(pnode->child[0]); (pnode->child[1])->attr.name=(pnode->child[0])->attr.name; GenCode(pnode->child[1]); paraSize=leaveScope(); sprintf(tmp, "addl $%d, %%esp\n", paraSize); EMITCODE(tmp); EMITCODE("ret\n"); break; } break; case NODE_TYPE: break; default: printf("pnode->nodekind Default"); } if(pnode->sibling!=NULL){ GenCode(pnode->sibling); } }
void AbstractByteCode::GenDelegateeByteCode(fint offset, fint length, stringOop delegatee) { GenCode(offset, length, BuildCode(DELEGATEE_CODE, GenIndex(offset, length, GenLiteral(delegatee)))); }
void AbstractByteCode::GenUndirectedResendByteCode(fint offset, fint length) { GenCode(offset, length, BuildCode( NO_OPERAND_CODE, UNDIRECTED_RESEND_CODE)); }
void HandleExpOp(TreeNode* pnode){ VariableList lsvar, rsvar; int bothConst; char const_real_data[100]; if (pnode->child[0]!=NULL && pnode->child[1]!=NULL){ // puts("11112222"); // lsvar = varListLookup(pnode->child[0]->attr.name); // puts("2233"); // rsvar = varListLookup(pnode->child[1]->attr.name); // puts("2323"); // if (lsvar->isConst == 1 && rsvar->isConst == 1){ // pnode->kind+(NODE_EXPRESSION<<4) if ((pnode->child[0])->kind+(NODE_EXPRESSION<<4) == EXP_CONST && (pnode->child[0])->kind+(NODE_EXPRESSION<<4) == EXP_CONST){ bothConst = 1; puts("22223"); } else { GenCode(pnode->child[0]); EMITCODE("pushl %eax\n"); GenCode(pnode->child[1]); EMITCODE("pushl %eax\n"); if ((pnode->child[0]->RuningType>EXPTYPE_REAL||pnode->child[0]->RuningType>EXPTYPE_REAL)&&(pnode->child[0]->RuningType != (pnode->child[1])->RuningType)){ if (!(pnode->child[0]->ERROR_STATE||pnode->child[1]->ERROR_STATE)){ ErrorHandler(ERROR_TYPE_MISMATCH, pnode); } pnode->ERROR_STATE=ERROR_TYPE_MISMATCH; return; } else if (pnode->child[0]->ERROR_STATE||pnode->child[1]->ERROR_STATE){ pnode->ERROR_STATE=ERROR_TYPE_MISMATCH; return; } if ((pnode->child[0])->RuningType==EXPTYPE_REAL && (pnode->child[1])->RuningType==EXPTYPE_REAL) pnode->RuningType=EXPTYPE_REAL; if ((pnode->child[0])->RuningType==EXPTYPE_INT && (pnode->child[1])->RuningType==EXPTYPE_INT) pnode->RuningType=EXPTYPE_INT; } } else { EMITCODE("pushl $0\n"); GenCode(pnode->child[0]); EMITCODE("pushl %eax\n"); pnode->RuningType=(pnode->child[0])->RuningType; } if (pnode->child[0]!=NULL && pnode->child[1]!=NULL && bothConst == 1){ if ((pnode->child[0])->type == EXPTYPE_INT){ if ((pnode->child[1])->type == EXPTYPE_INT){ switch(pnode->attr.op){ case TOKEN_AND: pnode->attr.val = (pnode->child[0])->attr.val & (pnode->child[1])->attr.val; sprintf(tmp, "movl $%d, %%eax\t \n", pnode->attr.val); EMITCODE(tmp); break; case TOKEN_PLUS: pnode->attr.val = (pnode->child[0])->attr.val + (pnode->child[1])->attr.val; sprintf(tmp, "movl $%d, %%eax\t \n", pnode->attr.val); EMITCODE(tmp); break; case TOKEN_MINUS: pnode->attr.val = (pnode->child[0])->attr.val - (pnode->child[1])->attr.val; sprintf(tmp, "movl $%d, %%eax\t \n", pnode->attr.val); EMITCODE(tmp); break; case TOKEN_MUL: pnode->attr.val = (pnode->child[0])->attr.val * (pnode->child[1])->attr.val; sprintf(tmp, "movl $%d, %%eax\t \n", pnode->attr.val); EMITCODE(tmp); break; case TOKEN_DIV: pnode->attr.val = (pnode->child[0])->attr.val / (pnode->child[1])->attr.val; sprintf(tmp, "movl $%d, %%eax\t \n", pnode->attr.val); EMITCODE(tmp); break; default: printf("compare between const\n"); break; } } else if ((pnode->child[1])->type == EXPTYPE_REAL){ switch(pnode->attr.op){ case TOKEN_PLUS: pnode->attr.real_val = (pnode->child[0])->attr.val + (pnode->child[1])->attr.real_val; break; case TOKEN_MINUS: pnode->attr.real_val = (pnode->child[0])->attr.val - (pnode->child[1])->attr.real_val; break; case TOKEN_MUL: pnode->attr.real_val = (pnode->child[0])->attr.val * (pnode->child[1])->attr.real_val; break; case TOKEN_DIV: pnode->attr.real_val = (pnode->child[0])->attr.val / (pnode->child[1])->attr.real_val; break; default: printf("compare between const\n"); break; } } } else if ((pnode->child[0])->type == EXPTYPE_REAL){ if ((pnode->child[1])->type == EXPTYPE_INT){ switch(pnode->attr.op){ case TOKEN_PLUS: pnode->attr.real_val = (pnode->child[0])->attr.real_val + (pnode->child[1])->attr.val; break; case TOKEN_MINUS: pnode->attr.real_val = (pnode->child[0])->attr.real_val - (pnode->child[1])->attr.val; break; case TOKEN_MUL: pnode->attr.real_val = (pnode->child[0])->attr.real_val * (pnode->child[1])->attr.val; break; case TOKEN_DIV: pnode->attr.real_val = (pnode->child[0])->attr.real_val / (pnode->child[1])->attr.val; break; default: printf("compare between const\n"); break; } } else if ((pnode->child[1])->type == EXPTYPE_REAL){ switch(pnode->attr.op){ case TOKEN_PLUS: pnode->attr.real_val = (pnode->child[0])->attr.real_val + (pnode->child[1])->attr.real_val; break; case TOKEN_MINUS: pnode->attr.real_val = (pnode->child[0])->attr.real_val - (pnode->child[1])->attr.real_val; break; case TOKEN_MUL: pnode->attr.real_val = (pnode->child[0])->attr.real_val * (pnode->child[1])->attr.real_val; break; case TOKEN_DIV: pnode->attr.real_val = (pnode->child[0])->attr.real_val / (pnode->child[1])->attr.real_val; break; default: printf("compare between const\n"); break; } } } if ((pnode->child[0])->type != EXPTYPE_INT || (pnode->child[1])->type != EXPTYPE_INT){ strcpy(const_real_data, GetDataLabel()); sprintf(tmp, ".%s:\n\t.float %lf\n", const_real_data, pnode->attr.real_val); EMITDATA(tmp); sprintf(tmp,"movl $.%s, %%ebx\t# calculate real ExpConst \n", const_real_data); EMITCODE(tmp); EMITCODE("movl (%ebx), %eax\n"); } } else if (pnode->RuningType==EXPTYPE_INT){ EMITCODE("popl %ebx\n"); EMITCODE("popl %eax\n"); switch(pnode->attr.op) { case TOKEN_AND: EMITCODE("andl %ebx, %eax\n"); break; case TOKEN_PLUS: EMITCODE("addl %ebx, %eax\n"); break; case TOKEN_MINUS: EMITCODE("subl %ebx, %eax\n"); break; case TOKEN_MUL: EMITCODE("xorl %edx, %edx\nimull %ebx\n"); break; case TOKEN_DIV: EMITCODE("xorl %edx, %edx\nidivl %ebx\n"); break; case TOKEN_MOD: EMITCODE("xorl %edx, %edx\nidivl %ebx\n"); EMITCODE("movl %edx, %eax\n"); break; case TOKEN_LT: EMITCODE("cmpl %ebx, %eax\n"); EMITCODE("movl $0, %eax\n"); EMITCODE("setlb %al\n"); break; case TOKEN_LE: EMITCODE("cmpl %ebx, %eax\n"); EMITCODE("movl $0, %eax\n"); EMITCODE("setngb %al\n"); break; case TOKEN_GT: EMITCODE("cmpl %ebx, %eax\n"); EMITCODE("movl $0, %eax\n"); EMITCODE("setgb %al\n"); break; case TOKEN_GE: EMITCODE("cmpl %ebx, %eax\n"); EMITCODE("movl $0, %eax\n"); EMITCODE("setnlb %al\n"); break; case TOKEN_EQUAL: EMITCODE("cmpl %ebx, %eax\n"); EMITCODE("movl $0, %eax\n"); EMITCODE("seteb %al\n"); break; case TOKEN_UNEQUAL: EMITCODE("cmpl %ebx, %eax\n"); EMITCODE("movl $0, %eax\n"); EMITCODE("setneb %al\n"); break; } } else { // EMITCODE("flds (%esp)\n"); // to be decided //char fcom_lable_false[100], fcom_lable_end[100]; if (pnode->child[1]!=NULL&&((pnode->child[1])->RuningType == EXPTYPE_INT)){ EMITCODE("filds (%esp)\n"); } else EMITCODE("flds (%esp)\n"); if ((pnode->child[0])->RuningType == EXPTYPE_INT){ EMITCODE("filds 4(%esp)\n"); } else EMITCODE("flds 4(%esp)\n"); EMITCODE("popl %eax\n"); EMITCODE("popl %eax\n"); switch(pnode->attr.op){ case TOKEN_PLUS: EMITCODE("faddp\n"); break; case TOKEN_MINUS: EMITCODE("fsubp\n"); break; case TOKEN_MUL: EMITCODE("fmulp\n"); break; case TOKEN_DIV: EMITCODE("fdivp\n"); break; case TOKEN_LT: case TOKEN_LE: case TOKEN_GT: case TOKEN_GE: case TOKEN_EQUAL: // EMITCODE("fxch %st(1)\n"); EMITCODE("fucomip %st(1),%st\n"); EMITCODE("fstp %st(0)\n"); EMITCODE("movl $0x0, %eax\n"); break; } switch(pnode->attr.op){ case TOKEN_PLUS: case TOKEN_MINUS: case TOKEN_MUL: case TOKEN_DIV: EMITCODE("subl $4, %esp\n"); EMITCODE("fstps (%esp)\n"); EMITCODE("popl %eax\n"); break; case TOKEN_LT: // if (pnode->attr.op == TOKEN_LT){ // strcpy(fcom_lable_false, GetLabel()); // strcpy(fcom_lable_end, GetLabel()); // sprintf(tmp, "jnb %s\n", fcom_lable_false); // EMITCODE(tmp); // } EMITCODE("setb %al\n"); break; case TOKEN_LE: // if (pnode->attr.op == TOKEN_LE){ // strcpy(fcom_lable_false, GetLabel()); // strcpy(fcom_lable_end, GetLabel()); // sprintf(tmp, "jnbe %s\n", fcom_lable_false); // EMITCODE(tmp); // } EMITCODE("setbe %al\n"); break; case TOKEN_GT: // if (pnode->attr.op == TOKEN_GT){ // strcpy(fcom_lable_false, GetLabel()); // strcpy(fcom_lable_end, GetLabel()); // sprintf(tmp, "jna %s\n", fcom_lable_false); // EMITCODE(tmp); // } EMITCODE("seta %al\n"); break; case TOKEN_GE: // if (pnode->attr.op == TOKEN_GE){ // strcpy(fcom_lable_false, GetLabel()); // strcpy(fcom_lable_end, GetLabel()); // sprintf(tmp, "jnae %s\n", fcom_lable_false); // EMITCODE(tmp); // } EMITCODE("setae %al\n"); break; case TOKEN_EQUAL: // if (pnode->attr.op == TOKEN_EQUAL){ // strcpy(fcom_lable_false, GetLabel()); // strcpy(fcom_lable_end, GetLabel()); // sprintf(tmp, "jne %s\n", fcom_lable_false); // EMITCODE(tmp); // } // EMITCODE("movl $1, (%esp)\n"); // sprintf(tmp, "jmp %s\n", fcom_lable_end); // EMITCODE(tmp); // sprintf(tmp, "%s:\n", fcom_lable_false); // EMITCODE(tmp); // EMITCODE("movl $0, (%esp)\n"); // sprintf(tmp, "%s:\n", fcom_lable_end); // EMITCODE(tmp); EMITCODE("sete %al\n"); break; default: break; } // EMITCODE("subl $4, %esp\n"); // EMITCODE("fstps (%esp)\n"); // EMITCODE("popl %eax\n"); } }
void AbstractByteCode::GenInstructionSetSelectionByteCode( fint offset, fint length, InstructionSetKind k) { assert(0 <= k && k <= LAST_INSTRUCTION_SET, "bad instruction set"); GenCode(offset, length, BuildCode(INSTRUCTION_SET_SELECTION_CODE, k)); }
void ParserGen::GenCode (Node *p, int indent, BitArray *isChecked) { Node *p2; BitArray *s1, *s2; while (p != NULL) { if (p->typ == Node::nt) { Indent(indent); fwprintf(gen, L"%ls(", p->sym->name); CopySourcePart(p->pos, 0); fwprintf(gen, L");\n"); } else if (p->typ == Node::t) { Indent(indent); // assert: if isChecked[p->sym->n] is true, then isChecked contains only p->sym->n if ((*isChecked)[p->sym->n]) fwprintf(gen, L"Get();\n"); else { fwprintf(gen, L"Expect("); WriteSymbolOrCode(gen, p->sym); fwprintf(gen, L");\n"); } } if (p->typ == Node::wt) { Indent(indent); s1 = tab->Expected(p->next, curSy); s1->Or(tab->allSyncSets); fwprintf(gen, L"ExpectWeak("); WriteSymbolOrCode(gen, p->sym); fwprintf(gen, L", %d);\n", NewCondSet(s1)); } if (p->typ == Node::any) { Indent(indent); int acc = Sets::Elements(p->set); if (tab->terminals->Count == (acc + 1) || (acc > 0 && Sets::Equals(p->set, isChecked))) { // either this ANY accepts any terminal (the + 1 = end of file), or exactly what's allowed here fwprintf(gen, L"Get();\n"); } else { GenErrorMsg(altErr, curSy); if (acc > 0) { fwprintf(gen, L"if ("); GenCond(p->set, p); fwprintf(gen, L") Get(); else SynErr(%d);\n", errorNr); } else fwprintf(gen, L"SynErr(%d); // ANY node that matches no symbol\n", errorNr); } } if (p->typ == Node::eps) { // nothing } if (p->typ == Node::rslv) { // nothing } if (p->typ == Node::sem) { CopySourcePart(p->pos, indent); } if (p->typ == Node::sync) { Indent(indent); GenErrorMsg(syncErr, curSy); s1 = p->set->Clone(); fwprintf(gen, L"while (!("); GenCond(s1, p); fwprintf(gen, L")) {"); fwprintf(gen, L"SynErr(%d); Get();", errorNr); fwprintf(gen, L"}\n"); } if (p->typ == Node::alt) { s1 = tab->First(p); bool equal = Sets::Equals(s1, isChecked); bool useSwitch = UseSwitch(p); if (useSwitch) { Indent(indent); fwprintf(gen, L"switch (la->kind) {\n"); } p2 = p; while (p2 != NULL) { s1 = tab->Expected(p2->sub, curSy); Indent(indent); if (useSwitch) { PutCaseLabels(s1); fwprintf(gen, L"{\n"); } else if (p2 == p) { fwprintf(gen, L"if ("); GenCond(s1, p2->sub); fwprintf(gen, L") {\n"); } else if (p2->down == NULL && equal) { fwprintf(gen, L"} else {\n"); } else { fwprintf(gen, L"} else if ("); GenCond(s1, p2->sub); fwprintf(gen, L") {\n"); } GenCode(p2->sub, indent + 1, s1); if (useSwitch) { Indent(indent); fwprintf(gen, L"\tbreak;\n"); Indent(indent); fwprintf(gen, L"}\n"); } p2 = p2->down; } Indent(indent); if (equal) { fwprintf(gen, L"}\n"); } else { GenErrorMsg(altErr, curSy); if (useSwitch) { fwprintf(gen, L"default: SynErr(%d); break;\n", errorNr); Indent(indent); fwprintf(gen, L"}\n"); } else { fwprintf(gen, L"} "); fwprintf(gen, L"else SynErr(%d);\n", errorNr); } } } if (p->typ == Node::iter) { Indent(indent); p2 = p->sub; fwprintf(gen, L"while ("); if (p2->typ == Node::wt) { s1 = tab->Expected(p2->next, curSy); s2 = tab->Expected(p->next, curSy); fwprintf(gen, L"WeakSeparator("); WriteSymbolOrCode(gen, p2->sym); fwprintf(gen, L",%d,%d) ", NewCondSet(s1), NewCondSet(s2)); s1 = new BitArray(tab->terminals->Count); // for inner structure if (p2->up || p2->next == NULL) p2 = NULL; else p2 = p2->next; } else { s1 = tab->First(p2); GenCond(s1, p2); } fwprintf(gen, L") {\n"); GenCode(p2, indent + 1, s1); Indent(indent); fwprintf(gen, L"}\n"); } if (p->typ == Node::opt) { s1 = tab->First(p->sub); Indent(indent); fwprintf(gen, L"if ("); GenCond(s1, p->sub); fwprintf(gen, L") {\n"); GenCode(p->sub, indent + 1, s1); Indent(indent); fwprintf(gen, L"}\n"); } if (p->typ != Node::eps && p->typ != Node::sem && p->typ != Node::sync) isChecked->SetAll(false); // = new BitArray(Symbol.terminals.Count); if (p->up) break; p = p->next; } }
void AbstractByteCode::GenNonLocalReturnByteCode(fint offset, fint length) { GenCode(offset, length, BuildCode(NO_OPERAND_CODE, NONLOCAL_RETURN_CODE)); }