Changeset 254 in dev for trunk/abdev/BasicCompiler64/Compile_Statement.cpp
- Timestamp:
- Jul 31, 2007, 4:30:31 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/abdev/BasicCompiler64/Compile_Statement.cpp
r243 r254 3 3 #include <jenga/include/smoothie/LexicalAnalysis.h> 4 4 5 #include <LexicalScopingImpl.h>6 5 #include <Compiler.h> 7 6 … … 192 191 193 192 //je (endif、または else まで条件ジャンプ) 194 CodeGenerator::PertialSchedule *pIfPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true );193 const PertialSchedule *pIfPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true ); 195 194 196 195 … … 200 199 201 200 //レキシカルスコープをレベルアップ 202 GetLexicalScopes().Start( obp,SCOPE_TYPE_IF );201 compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_IF ); 203 202 204 203 int i2=CompileBuffer(ESC_ENDIF,0); 205 204 206 205 //レキシカルスコープをレベルダウン 207 GetLexicalScopes().End();206 compiler.codeGenerator.lexicalScopes.End(); 208 207 209 208 210 209 if(i2==ESC_ELSE){ 211 210 //jmp (endifまで) 212 CodeGenerator::PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );211 const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true ); 213 212 214 213 compiler.codeGenerator.opfix_JmpPertialSchedule( pIfPertialSchedule ); … … 221 220 222 221 //レキシカルスコープをレベルアップ 223 GetLexicalScopes().Start( obp,SCOPE_TYPE_IF );222 compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_IF ); 224 223 225 224 CompileBuffer(ESC_ENDIF,0); 226 225 227 226 //レキシカルスコープをレベルダウン 228 GetLexicalScopes().End();227 compiler.codeGenerator.lexicalScopes.End(); 229 228 230 229 … … 259 258 void OpcodeGoto(char *Parameter){ 260 259 extern HANDLE hHeap; 261 extern GOTOLABELSCHEDULE *pGotoLabelSchedule;262 extern int GotoLabelScheduleNum;263 260 int i,LineNum; 264 261 … … 266 263 i=GetLabelAddress(Parameter+1,0); 267 264 268 //jmp ...269 OpBuffer[obp++]=(char)0xE9;270 if(i==-1){271 pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapReAlloc(hHeap,0,pGotoLabelSchedule,(GotoLabelScheduleNum+1)*sizeof(GOTOLABELSCHEDULE));272 pGotoLabelSchedule[GotoLabelScheduleNum].pName=(char *)HeapAlloc(hHeap,0,lstrlen(Parameter+1)+1);273 lstrcpy(pGotoLabelSchedule[GotoLabelScheduleNum].pName,Parameter+1);274 pGotoLabelSchedule[GotoLabelScheduleNum].pos=obp;275 pGotoLabelSchedule[GotoLabelScheduleNum].now_cp=cp;276 GotoLabelScheduleNum++;277 }278 *((long *)(OpBuffer+obp))=i-(obp+sizeof(long));279 obp+=sizeof(long);265 if( i == -1 ) 266 { 267 //jmp ...(schedule) 268 extern int obp; 269 compiler.codeGenerator.op_jmp_goto_schedule( (const std::string)(Parameter + 1), 0, cp ); 270 } 271 else 272 { 273 //jmp ... 274 extern int obp; 275 compiler.codeGenerator.op_jmp( i-obp, sizeof(long), false, true ); 276 } 280 277 } 281 278 else{ … … 283 280 i=GetLabelAddress(0,LineNum); 284 281 285 //jmp ...286 OpBuffer[obp++]=(char)0xE9;287 if(i==-1){288 pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapReAlloc(hHeap,0,pGotoLabelSchedule,(GotoLabelScheduleNum+1)*sizeof(GOTOLABELSCHEDULE));289 pGotoLabelSchedule[GotoLabelScheduleNum].pName=0;290 pGotoLabelSchedule[GotoLabelScheduleNum].line=LineNum;291 pGotoLabelSchedule[GotoLabelScheduleNum].pos=obp;292 pGotoLabelSchedule[GotoLabelScheduleNum].now_cp=cp;293 GotoLabelScheduleNum++;294 }295 *((long *)(OpBuffer+obp))=i-(obp+sizeof(long));296 obp+=sizeof(long);282 if( i == -1 ) 283 { 284 //jmp ...(schedule) 285 extern int obp; 286 compiler.codeGenerator.op_jmp_goto_schedule( "", LineNum, cp ); 287 } 288 else 289 { 290 //jmp ... 291 extern int obp; 292 compiler.codeGenerator.op_jmp( i-obp, sizeof(long), false, true ); 293 } 297 294 } 298 295 } … … 309 306 310 307 //je (Wend まで) 311 CodeGenerator::PertialSchedule *pWhilePertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true );308 const PertialSchedule *pWhilePertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true ); 312 309 313 310 //レキシカルスコープをレベルアップ 314 GetLexicalScopes().Start( obp,SCOPE_TYPE_WHILE );311 compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_WHILE ); 315 312 316 313 //While内をコンパイル 317 314 CompileBuffer(0,COM_WEND); 318 315 319 GetLexicalScopes().CallDestructorsOfScopeEnd();316 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd(); 320 317 321 318 //jmp ... … … 323 320 324 321 //レキシカルスコープをレベルダウン 325 GetLexicalScopes().End();322 compiler.codeGenerator.lexicalScopes.End(); 326 323 327 324 compiler.codeGenerator.opfix_JmpPertialSchedule( pWhilePertialSchedule ); … … 364 361 365 362 //jmp ... 366 CodeGenerator::PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );363 const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true ); 367 364 368 365 //Continueアドレスのバックアップとセット … … 407 404 408 405 //jmp [カウンタ減少の場合の判定を飛び越す] 409 CodeGenerator::PertialSchedule *pTempPertialSchedule2 = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );406 const PertialSchedule *pTempPertialSchedule2 = compiler.codeGenerator.op_jmp( 0, sizeof(long), true ); 410 407 411 408 //jeジャンプ先のオフセット値 … … 430 427 431 428 //レキシカルスコープをレベルアップ 432 GetLexicalScopes().Start( obp,SCOPE_TYPE_FOR );429 compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_FOR ); 433 430 434 431 //For内をコンパイル 435 432 CompileBuffer(0,COM_NEXT); 436 433 437 GetLexicalScopes().CallDestructorsOfScopeEnd();434 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd(); 438 435 439 436 if(szNextVariable[0]){ … … 447 444 448 445 //レキシカルスコープをレベルダウン 449 GetLexicalScopes().End();446 compiler.codeGenerator.lexicalScopes.End(); 450 447 451 448 //jeジャンプ先のオフセット値 … … 466 463 467 464 //レキシカルスコープをレベルアップ 468 GetLexicalScopes().Start( obp,SCOPE_TYPE_DO );465 compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_DO ); 469 466 470 467 //Do内をコンパイル 471 468 CompileBuffer(0,COM_LOOP); 472 469 473 GetLexicalScopes().CallDestructorsOfScopeEnd();474 475 CodeGenerator::PertialSchedule *pDoPertialSchedule = NULL;470 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd(); 471 472 const PertialSchedule *pDoPertialSchedule = NULL; 476 473 477 474 extern char *basbuf; … … 522 519 523 520 //jmp ... 524 CodeGenerator::PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );521 const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true ); 525 522 526 523 //レキシカルスコープをレベルダウン 527 GetLexicalScopes().End();524 compiler.codeGenerator.lexicalScopes.End(); 528 525 529 526 //jmpジャンプ先のオフセット値 … … 539 536 540 537 void OpcodeExitSub(void){ 541 extern DWORD *pExitSubSchedule;542 extern int ExitSubScheduleNum;543 extern HANDLE hHeap;544 545 538 if( UserProc::IsGlobalAreaCompiling() ){ 546 539 SetError(12,"Exit Sub/Function",cp); … … 549 542 550 543 //未解放のローカルオブジェクトのデストラクタを呼び出す 551 GetLexicalScopes().CallDestructorsOfReturn();544 compiler.codeGenerator.lexicalScopes.CallDestructorsOfReturn(); 552 545 553 546 //jmp ...(End Sub/Function) 554 OpBuffer[obp++]=(char)0xE9; 555 556 pExitSubSchedule=(DWORD *)HeapReAlloc(hHeap,0,pExitSubSchedule,(ExitSubScheduleNum+1)*sizeof(DWORD)); 557 pExitSubSchedule[ExitSubScheduleNum]=obp; 558 ExitSubScheduleNum++; 559 560 obp+=sizeof(long); 561 } 562 563 void AddCaseSchedule(void){ 564 extern DWORD *pCaseSchedule; 565 extern int CaseScheduleNum; 566 extern HANDLE hHeap; 567 568 pCaseSchedule=(DWORD *)HeapReAlloc(hHeap,0,pCaseSchedule,(CaseScheduleNum+1)*sizeof(DWORD)); 569 pCaseSchedule[CaseScheduleNum]=obp; 570 CaseScheduleNum++; 571 } 547 compiler.codeGenerator.op_jmp_exitsub(); 548 } 549 550 //Caseスケジュール 551 class SelectSchedule 552 { 553 public: 554 SelectSchedule( int typeSize ) 555 : typeSize( typeSize ) 556 , nowCaseSchedule( 0 ) 557 { 558 } 559 560 PertialSchedules casePertialSchedules; 561 int typeSize; 562 int nowCaseSchedule; 563 }; 564 std::vector<SelectSchedule> selectSchedules; 565 572 566 void OpcodeSelect( const char *lpszParms ){ 573 extern DWORD *pCaseSchedule;574 extern int CaseScheduleNum;575 extern int NowCaseSchedule;576 567 extern HANDLE hHeap; 577 568 extern char *basbuf; 578 569 int i,i2,i3,NowCaseCp; 579 570 char temporary[VN_SIZE]; 580 581 DWORD *temp_pCaseSchedule; 582 int temp_CaseScheduleNum; 583 int temp_NowCaseSchedule; 584 585 temp_pCaseSchedule=pCaseSchedule; 586 temp_CaseScheduleNum=CaseScheduleNum; 587 temp_NowCaseSchedule=NowCaseSchedule; 588 pCaseSchedule=(DWORD *)HeapAlloc(hHeap,0,1); 589 CaseScheduleNum=0; 590 NowCaseSchedule=0; 591 571 592 572 int reg1=REG_RAX; 593 573 Type type1; … … 596 576 } 597 577 578 selectSchedules.push_back( SelectSchedule( type1.GetSize() ) ); 579 580 if( selectSchedules.back().typeSize < sizeof(long) ){ 581 selectSchedules.back().typeSize = sizeof(long); 582 } 583 598 584 if(type1.IsDouble()){ 599 585 //movsd qword ptr[rsp+offset],xmm_reg ※スタックフレームを利用 … … 613 599 for(i=cp;;i++){ 614 600 if(basbuf[i]=='\0'){ 615 HeapDefaultFree(pCaseSchedule); 616 pCaseSchedule=temp_pCaseSchedule; 617 CaseScheduleNum=temp_CaseScheduleNum; 618 NowCaseSchedule=temp_NowCaseSchedule; 601 selectSchedules.pop_back(); 619 602 SetError(22,"Select",cp); 620 603 return; … … 718 701 719 702 //jne ... 720 OpBuffer[obp++]=(char)0x0F; 721 OpBuffer[obp++]=(char)0x85; 703 selectSchedules.back().casePertialSchedules.push_back( 704 compiler.codeGenerator.op_jne( 0, sizeof(long), true ) 705 ); 722 706 } 723 707 else{ … … 759 743 760 744 //je ... 761 OpBuffer[obp++]=(char)0x0F; 762 OpBuffer[obp++]=(char)0x84; 745 selectSchedules.back().casePertialSchedules.push_back( 746 compiler.codeGenerator.op_je( 0, sizeof(long), true ) 747 ); 763 748 } 764 AddCaseSchedule();765 obp+=sizeof(long);766 749 767 750 if(basbuf[i]!=',') break; … … 770 753 if(basbuf[i]==1&&basbuf[i+1]==ESC_CASEELSE){ 771 754 //jmp ... 772 OpBuffer[obp++]=(char)0xE9;773 AddCaseSchedule();774 obp+=sizeof(long);755 selectSchedules.back().casePertialSchedules.push_back( 756 compiler.codeGenerator.op_jmp( 0, sizeof(long), true ) 757 ); 775 758 } 776 759 } … … 780 763 781 764 //レキシカルスコープをレベルアップ 782 GetLexicalScopes().Start( obp,SCOPE_TYPE_SELECT );765 compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_SELECT ); 783 766 784 767 //Select Case内をコンパイル … … 786 769 787 770 //jmp EndSelect 788 OpBuffer[obp++]=(char)0xE9;789 AddCaseSchedule();790 obp+=sizeof(long);771 selectSchedules.back().casePertialSchedules.push_back( 772 compiler.codeGenerator.op_jmp( 0, sizeof(long), true ) 773 ); 791 774 792 775 //最終スケジュール 793 for(i=NowCaseSchedule;i<CaseScheduleNum;i++){ 794 *(long *)(OpBuffer+pCaseSchedule[i])=obp-(pCaseSchedule[i]+sizeof(long)); 795 } 796 HeapDefaultFree(pCaseSchedule); 776 for(i=selectSchedules.back().nowCaseSchedule;i<(int)selectSchedules.back().casePertialSchedules.size();i++){ 777 compiler.codeGenerator.opfix_JmpPertialSchedule( selectSchedules.back().casePertialSchedules[i] ); 778 } 797 779 798 780 //レキシカルスコープをレベルダウン 799 GetLexicalScopes().End(); 800 801 pCaseSchedule=temp_pCaseSchedule; 802 CaseScheduleNum=temp_CaseScheduleNum; 803 NowCaseSchedule=temp_NowCaseSchedule; 781 compiler.codeGenerator.lexicalScopes.End(); 782 783 selectSchedules.pop_back(); 804 784 } 805 785 void OpcodeCase(char *Parameter){ 806 extern DWORD *pCaseSchedule;807 extern int NowCaseSchedule;808 786 int i; 809 787 810 if( !pCaseSchedule){788 if(selectSchedules.back().typeSize==-1){ 811 789 SetError(30,"Case",cp); 812 790 return; … … 814 792 815 793 //jmp EndSelect 816 OpBuffer[obp++]=(char)0xE9;817 AddCaseSchedule();818 obp+=sizeof(long);794 selectSchedules.back().casePertialSchedules.push_back( 795 compiler.codeGenerator.op_jmp( 0, sizeof(long), true ) 796 ); 819 797 820 798 i=0; 821 799 while(1){ 822 800 //Caseスケジュール 823 *(long *)(OpBuffer+pCaseSchedule[NowCaseSchedule])=obp-(pCaseSchedule[NowCaseSchedule]+sizeof(long));824 NowCaseSchedule++;801 compiler.codeGenerator.opfix_JmpPertialSchedule( selectSchedules.back().casePertialSchedules[selectSchedules.back().nowCaseSchedule] ); 802 selectSchedules.back().nowCaseSchedule++; 825 803 826 804 i=JumpOneParameter(Parameter,i); … … 830 808 831 809 void OpcodeGosub(char *Parameter){ 832 extern HANDLE hHeap;833 extern GOTOLABELSCHEDULE *pGotoLabelSchedule;834 extern int GotoLabelScheduleNum;835 int i,LineNum;836 837 //call _System_GetEip838 extern const UserProc *pSub_System_GetEip;839 compiler.codeGenerator.op_call(pSub_System_GetEip);840 841 //add rax,offset(Gosubステートメントの最終ポイント)842 int schedule=obp,schedule2;843 compiler.codeGenerator.op_add_RV(REG_RAX,0);844 schedule2=obp-sizeof(long);845 846 //※戻り先用のrip847 //mov qword ptr[rsp+offset],rax ※スタックフレームを利用848 pobj_sf->push(REG_RAX);849 850 851 if(Parameter[0]=='*'){852 i=GetLabelAddress(Parameter+1,0);853 854 //jmp ...855 OpBuffer[obp++]=(char)0xE9;856 if(i==-1){857 pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapReAlloc(hHeap,0,pGotoLabelSchedule,(GotoLabelScheduleNum+1)*sizeof(GOTOLABELSCHEDULE));858 pGotoLabelSchedule[GotoLabelScheduleNum].pName=(char *)HeapAlloc(hHeap,0,lstrlen(Parameter+1)+1);859 lstrcpy(pGotoLabelSchedule[GotoLabelScheduleNum].pName,Parameter+1);860 pGotoLabelSchedule[GotoLabelScheduleNum].pos=obp;861 pGotoLabelSchedule[GotoLabelScheduleNum].now_cp=cp;862 GotoLabelScheduleNum++;863 }864 *((long *)(OpBuffer+obp))=i-(obp+sizeof(long));865 obp+=sizeof(long);866 }867 else{868 LineNum=atoi(Parameter);869 i=GetLabelAddress(0,LineNum);870 871 //jmp ...872 OpBuffer[obp++]=(char)0xE9;873 if(i==-1){874 pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapReAlloc(hHeap,0,pGotoLabelSchedule,(GotoLabelScheduleNum+1)*sizeof(GOTOLABELSCHEDULE));875 pGotoLabelSchedule[GotoLabelScheduleNum].pName=0;876 pGotoLabelSchedule[GotoLabelScheduleNum].line=LineNum;877 pGotoLabelSchedule[GotoLabelScheduleNum].pos=obp;878 pGotoLabelSchedule[GotoLabelScheduleNum].now_cp=cp;879 GotoLabelScheduleNum++;880 }881 *((long *)(OpBuffer+obp))=i-(obp+sizeof(long));882 obp+=sizeof(long);883 }884 885 *((long *)(OpBuffer+schedule2))=obp-schedule;886 887 //※スタックフレームを元に戻す888 pobj_sf->pop(REG_NON);889 890 810 SetError(-1,"Gosub ~ Returnステートメントは64ビットコンパイラで利用することはできません。",cp); 891 811 }
Note:
See TracChangeset
for help on using the changeset viewer.