source: dev/trunk/abdev/BasicCompiler64/Compile_ProcOp.cpp@ 255

Last change on this file since 255 was 255, checked in by dai_9181, 17 years ago
File size: 22.5 KB
Line 
1#include "stdafx.h"
2
3#include <jenga/include/smoothie/Smoothie.h>
4#include <jenga/include/smoothie/LexicalAnalysis.h>
5
6#include <Program.h>
7#include <Compiler.h>
8#include <Class.h>
9
10#include "../BasicCompiler_Common/common.h"
11#include "Opcode.h"
12
13void SystemProc( const UserProc &userProc ){
14 if( userProc.GetName() == "_System_GetEip" ){
15 //mov rax,qword ptr[rsp]
16 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_RAX,REG_RSP,0,MOD_BASE);
17
18 //ret
19 compiler.codeGenerator.op_ret();
20 }
21 else if( userProc.GetName() == "_System_InitDllGlobalVariables" ){
22 ////////////////////////////////////////
23 // DLLのグローバル領域をコンパイル
24 ////////////////////////////////////////
25 extern BOOL bDll;
26 if(!bDll){
27 //ret
28 compiler.codeGenerator.op_ret();
29
30 return;
31 }
32
33 const UserProc *pBackUserProc = &UserProc::CompilingUserProc();
34 UserProc::CompileStartForGlobalArea();
35
36 int BackCp;
37 BackCp=cp;
38 cp=-1;
39
40 //sub rsp,スタックフレームサイズ
41 const PertialSchedule *pStackFramePertialSchedule = compiler.codeGenerator.op_sub_rsp( 0, true );
42
43 extern BOOL bDebugCompile;
44 if(bDebugCompile){
45 //デバッグ用の変数を定義
46 DebugVariable();
47 }
48
49 //GC用の変数を定義
50 InitGCVariables();
51
52 //_System_StartupProgramの呼び出し
53 extern const UserProc *pSub_System_StartupProgram;
54 compiler.codeGenerator.op_call(pSub_System_StartupProgram);
55
56 //クラスに属する静的メンバを定義
57 compiler.GetMeta().GetClasses().InitStaticMember();
58
59 GetGlobalDataForDll();
60
61 //add rsp,スタックフレームサイズ
62 compiler.codeGenerator.op_add_RV(REG_RSP,pobj_sf->GetFrameSize(0));
63
64 //スタックフレームスケジュール(subコマンドに渡す値)
65 compiler.codeGenerator.opfix( pStackFramePertialSchedule, pobj_sf->GetFrameSize(0) );
66
67 UserProc::CompileStartForUserProc( pBackUserProc );
68 cp=BackCp;
69
70 //ret
71 compiler.codeGenerator.op_ret();
72 }
73 else if( userProc.GetName() == "_System_InitStaticLocalVariables" ){
74 //静的ローカルオブジェクトのコンストラクタ呼び出し
75
76 //sub rsp,スタックフレームサイズ
77 const PertialSchedule *pStackFramePertialSchedule = compiler.codeGenerator.op_sub_rsp( 0, true );
78
79 BOOST_FOREACH( Variable *pVar, compiler.GetMeta().GetGlobalVars() ){
80 if(memicmp(pVar->GetName().c_str(),"Static%",7)==0){
81 //コンストラクタ呼び出し
82 if( pVar->GetType().IsObject() ){
83
84 //エラー用
85 cp=pVar->source_code_address;
86
87 CallConstructor(
88 pVar->GetName().c_str(),
89 pVar->GetSubscripts(),
90 pVar->GetType(),
91 pVar->GetParamStrForConstructor().c_str());
92 }
93 }
94 }
95
96 //add rsp,スタックフレームサイズ
97 compiler.codeGenerator.op_add_RV(REG_RSP,pobj_sf->GetFrameSize(0));
98
99 //スタックフレームスケジュール(subコマンドに渡す値)
100 compiler.codeGenerator.opfix( pStackFramePertialSchedule, pobj_sf->GetFrameSize(0) );
101
102 //ret
103 compiler.codeGenerator.op_ret();
104 }
105 else if( userProc.GetName() == "_System_Call_Destructor_of_GlobalObject" ){
106 //sub rsp,8(※RSPを16バイト境界にあわせるため)
107 compiler.codeGenerator.op_sub_rsp(0x8);
108
109
110 const UserProc *pBackUserProc = &UserProc::CompilingUserProc();
111 UserProc::CompileStartForGlobalArea();
112
113 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
114
115 UserProc::CompileStartForUserProc( pBackUserProc );
116
117
118 //add rsp,8
119 compiler.codeGenerator.op_add_RV(REG_RSP,0x8);
120
121 //ret
122 compiler.codeGenerator.op_ret();
123 }
124 else if( userProc.GetName() == "_System_GetSp" ){
125 //mov rax,rsp
126 compiler.codeGenerator.op_mov_RR(REG_RAX,REG_RSP);
127
128 //add rax,PTR_SIZE
129 compiler.codeGenerator.op_add_RV(REG_RAX,PTR_SIZE);
130
131 //ret
132 compiler.codeGenerator.op_ret();
133 }
134 else{
135 SetError();
136 }
137}
138void AutoGeneration(const UserProc &userProc){
139 if( userProc.GetName() == "InitializeUserTypes"
140 && userProc.HasParentClass()
141 && userProc.GetParentClass().GetName() == "_System_TypeBase" ){
142
143 compiler.GetMeta().GetClasses().Compile_System_InitializeUserTypes();
144 }
145 else if( userProc.GetName() == "RegisterGlobalRoots"
146 && userProc.HasParentClass()
147 && userProc.GetParentClass().GetName() == "_System_CGarbageCollection" ){
148
149 Compile_AddGlobalRootsForGc();
150 }
151 else{
152 SetError();
153 }
154}
155void _compile_proc(const UserProc *pUserProc){
156 extern char *basbuf;
157 extern HANDLE hHeap;
158 extern BOOL bDebugCompile;
159 int i3,i4;
160 char temporary[VN_SIZE];
161
162 if( pUserProc->IsUsing() == false || pUserProc->IsCompiled() ) return;
163
164 if( pUserProc->GetLocalVars().size() ){
165 SetError();
166 return;
167 }
168
169 trace_for_sourcecodestep( "★★★ " << pUserProc->GetFullName() << "のコンパイルを開始" );
170
171 pUserProc->CompleteCompile();
172
173 extern BOOL bSystemProc;
174 if(memcmp(pUserProc->GetName().c_str(),"_System_",8)==0) bSystemProc=1;
175 else bSystemProc=0;
176
177 extern BOOL bDebugSupportProc;
178 if(memcmp(pUserProc->GetName().c_str(),"_DebugSys_",10)==0){
179 if(!bDebugCompile){
180 return;
181 }
182 bDebugSupportProc=1;
183 }
184 else bDebugSupportProc=0;
185
186 pUserProc->SetBeginOpAddress( obp );
187
188 //コンパイル中の関数が属するクラス
189 compiler.pCompilingClass=pUserProc->GetParentClassPtr();
190
191 //コンパイルスタートをクラス管理クラスに追加
192 compiler.GetMeta().GetClasses().StartCompile( pUserProc );
193
194 //コンパイル中の関数
195 UserProc::CompileStartForUserProc( pUserProc );
196
197 // コンパイル中の関数が属する名前空間
198 compiler.GetNamespaceSupporter().SetLivingNamespaceScopes( pUserProc->GetNamespaceScopes() );
199
200 // コンパイル中の関数でImportsされている名前空間
201 compiler.GetNamespaceSupporter().SetImportedNamespaces( pUserProc->GetImportedNamespaces() );
202
203 // コード生成対象を選択
204 compiler.codeGenerator.Select( (const_cast<UserProc *>(pUserProc))->GetNativeCode() );
205
206 if(pUserProc->IsSystem()){
207 ////////////////////
208 // 特殊関数
209 ////////////////////
210
211 extern int AllLocalVarSize;
212 AllLocalVarSize=0;
213
214 //スタックフレーム管理用オブジェクトを初期化
215 extern CStackFrame *pobj_sf;
216 pobj_sf=new CStackFrame();
217
218 SystemProc(*pUserProc);
219
220 //スタックフレーム管理用オブジェクトを破棄
221 delete pobj_sf;
222 pobj_sf=0;
223
224 pUserProc->SetEndOpAddress( obp );
225 return;
226 }
227
228 cp=pUserProc->GetCodePos();
229 for(;;cp++){
230 if(IsCommandDelimitation(basbuf[cp])) break;
231 }
232 cp--;
233
234 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
235 compiler.codeGenerator.exitSubCodePositions.clear();
236 compiler.codeGenerator._exitSubCodePositions_ObpOld.clear();
237
238 //ラベル用のメモリを確保
239 extern LABEL *pLabelNames;
240 extern int MaxLabelNum;
241 pLabelNames=(LABEL *)HeapAlloc(hHeap,0,1);
242 MaxLabelNum=0;
243
244 //Gotoラベルスケジュール
245 compiler.codeGenerator.gotoLabelSchedules.clear();
246
247 //With情報のメモリを確保
248 extern WITHINFO WithInfo;
249 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
250 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
251 WithInfo.num=0;
252
253 //重複エラー情報管理のメモリを確保
254 extern char **SynonymErrorWords;
255 extern int SynonymErrorNum;
256 SynonymErrorNum=0;
257 SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
258
259 //Continueアドレスを初期化
260 compiler.codeGenerator.ClearContinueArea();
261
262 //ローカル変数に関する情報
263 extern int AllLocalVarSize;
264 AllLocalVarSize=0;
265
266 //レキシカルスコープ情報を初期化
267 compiler.codeGenerator.lexicalScopes.Init(obp);
268
269
270 /////////////////////////////////////
271 // パラメータ用の変数データを考慮
272 /////////////////////////////////////
273
274 //パラメータ用の変数データを考慮
275 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
276 Parameter &param = *pUserProc->RealParams()[i3];
277
278 Variable *pVar = new Variable( param.GetVarName(), param, false, param.IsRef(), "" );
279
280 if( param.IsArray() ){
281 pVar->SetArray( param.GetSubscripts() );
282 }
283
284 int varSize;
285 if( param.IsRef() == false && param.IsStruct() ){
286 //構造体のByValパラメータ
287 pVar->ThisIsParameter();
288 varSize=PTR_SIZE;
289 }
290 else{
291 if( param.IsArray() == false ){
292 varSize = pVar->GetMemorySize();
293 }
294 else{
295 varSize=PTR_SIZE;
296 }
297 }
298 AllLocalVarSize+=varSize;
299 pVar->SetOffsetAddress( AllLocalVarSize );
300
301 //レキシカルスコープ情報
302 pVar->SetScopeLevel( compiler.codeGenerator.lexicalScopes.GetNowLevel() );
303 pVar->SetScopeStartAddress( compiler.codeGenerator.lexicalScopes.GetStartAddress() );
304 pVar->bLiving=TRUE;
305
306 pUserProc->GetLocalVars().push_back( pVar );
307 }
308
309 //Thisポインタを示すローカルオフセット値をセット
310 extern int LocalVar_ThisPtrOffset;
311 LocalVar_ThisPtrOffset=AllLocalVarSize;
312
313 //スタックフレーム管理用クラスを初期化
314 extern CStackFrame *pobj_sf;
315 pobj_sf=new CStackFrame();
316
317
318 ///////////////////////
319 // ここからコード生成
320
321 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
322 Parameter &param = *pUserProc->RealParams()[i3];
323 if(i3==3){
324 if(param.IsReal()&&param.IsRef() == false){
325 //movsd qword ptr[rsp+0x20],xmm3
326 compiler.codeGenerator.op_movsd_MR(REG_XMM3,REG_RSP,0x20,MOD_BASE_DISP32);
327 }
328 else{
329 //mov qword ptr[rsp+0x20],r9
330 compiler.codeGenerator.op_mov_MR(sizeof(_int64),REG_R9,REG_RSP,0x20,MOD_BASE_DISP32);
331 }
332 }
333 if(i3==2){
334 if(param.IsReal()&&param.IsRef() == false){
335 //movsd qword ptr[rsp+0x18],xmm2
336 compiler.codeGenerator.op_movsd_MR(REG_XMM2,REG_RSP,0x18,MOD_BASE_DISP32);
337 }
338 else{
339 //mov qword ptr[rsp+0x18],r8
340 compiler.codeGenerator.op_mov_MR(sizeof(_int64),REG_R8,REG_RSP,0x18,MOD_BASE_DISP32);
341 }
342 }
343 if(i3==1){
344 if(param.IsReal()&&param.IsRef() == false){
345 //movsd qword ptr[rsp+0x10],xmm1
346 compiler.codeGenerator.op_movsd_MR(REG_XMM1,REG_RSP,0x10,MOD_BASE_DISP32);
347 }
348 else{
349 //mov qword ptr[rsp+0x10],rdx
350 compiler.codeGenerator.op_mov_MR(sizeof(_int64),REG_RDX,REG_RSP,0x10,MOD_BASE_DISP32);
351 }
352 }
353 if(i3==0){
354 if(param.IsReal()&&param.IsRef() == false){
355 //movsd qword ptr[rsp+0x8],xmm0
356 compiler.codeGenerator.op_movsd_MR(REG_XMM0,REG_RSP,0x8,MOD_BASE_DISP32);
357 }
358 else{
359 //mov qword ptr[rsp+0x8],rcx
360 compiler.codeGenerator.op_mov_MR(sizeof(_int64),REG_RCX,REG_RSP,0x8,MOD_BASE_DISP32);
361 }
362 }
363 }
364
365 //ret用のアドレスを考慮
366 AllLocalVarSize+=sizeof(_int64);
367
368 //sub rsp,スタックフレームサイズ
369 const PertialSchedule *pStackFramePertialSchedule = compiler.codeGenerator.op_sub_rsp( 0, true );
370
371 //mov qword ptr[rsp+offset],reg ※スタックフレームを利用
372 pobj_sf->push(REG_RBX);
373 pobj_sf->push(REG_RSI);
374 pobj_sf->push(REG_RDI);
375 pobj_sf->push(REG_R12);
376 pobj_sf->push(REG_R13);
377 pobj_sf->push(REG_R14);
378 pobj_sf->push(REG_R15);
379
380 //ローカル変数のベース値
381 int BaseLocalVar;
382 BaseLocalVar=AllLocalVarSize;
383
384 if( !pUserProc->ReturnType().IsNull() ){
385 //戻り値が存在するとき
386
387 const char *temp = pUserProc->GetName().c_str();
388 if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
389 temp = "_System_ReturnValue";
390 }
391
392 if( pUserProc->ReturnType().IsStruct() ){
393 //戻り値用の構造体(値型)はパラメータで引き渡される
394 }
395 else{
396 if( pUserProc->ReturnType().IsObject() ){
397 sprintf(temporary,"%s=Nothing%c%c%s",temp,1,ESC_AS, Compiler::TypeToString( pUserProc->ReturnType() ).c_str() );
398 }
399 else{
400 //戻り値用の変数の定義
401 sprintf(temporary,"%s%c%c%s",temp,1,ESC_AS, Compiler::TypeToString( pUserProc->ReturnType() ).c_str() );
402 }
403
404 OpcodeDim(temporary,0);
405 }
406 }
407
408 const PertialSchedule *pRspOffsetPertialSchedule1 = NULL;
409 const PertialSchedule *pRspOffsetPertialSchedule2 = NULL;
410 if(bDebugCompile&&bDebugSupportProc==0){
411 //mov rdx, qword ptr[rsp+スタックフレームサイズ]
412 pRspOffsetPertialSchedule1 = compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_RDX,REG_RSP,0,MOD_BASE_DISP32, Schedule::None, true );
413
414 //mov rcx,rsp
415 compiler.codeGenerator.op_mov_RR(REG_RCX,REG_RSP);
416
417 //add rcx,スタックフレームサイズ+sizeof(_int64) ※ret用のサイズを考慮
418 pRspOffsetPertialSchedule2 = compiler.codeGenerator.op_add_RV(REG_RCX,0, Schedule::None, true );
419
420 //call _DebugSys_StartProc
421 extern const UserProc *pSub_DebugSys_StartProc;
422 compiler.codeGenerator.op_call(pSub_DebugSys_StartProc);
423 }
424
425 if(compiler.pCompilingClass){
426 if( pUserProc->GetName() == compiler.pCompilingClass->GetName() ){
427 ////////////////////////////////////
428 // コンストラクタをコンパイルするとき
429 ////////////////////////////////////
430
431 //コンストラクタのコンパイル開始を通知
432 compiler.pCompilingClass->NotifyStartConstructorCompile();
433
434 //基底クラスかどうかの識別
435 //(継承元がインターフェイスの場合も基底クラスと見なす)
436 BOOL bThisIsSuperClass;
437 if( !compiler.pCompilingClass->HasSuperClass() ) bThisIsSuperClass=1;
438 else if( compiler.pCompilingClass->GetSuperClass().GetConstructorMethod() == NULL ){
439 //インターフェイスを継承したときはコンストラクタを持たない
440 bThisIsSuperClass=1;
441 }
442 else bThisIsSuperClass=0;
443
444 if(!bThisIsSuperClass){
445 /* サブクラスコンストラクタをコンパイルしているときは、
446 基底クラスのコンストラクタを呼び出す */
447
448 i3=cp+1;
449 while(IsCommandDelimitation(basbuf[i3])) i3++;
450 for(i4=0;;i3++,i4++){
451 if(!IsVariableChar(basbuf[i3])){
452 temporary[i4]=0;
453 break;
454 }
455 temporary[i4]=basbuf[i3];
456 }
457 if( compiler.pCompilingClass->GetSuperClass().GetName() == temporary ){
458 //基底クラスのコンストラクタを呼び出す
459 cp=i3;
460 for(i4=0;;cp++,i4++){
461 if(IsCommandDelimitation(basbuf[cp])){
462 temporary[i4]=0;
463 break;
464 }
465 temporary[i4]=basbuf[cp];
466 }
467 if(!(temporary[0]=='('&&temporary[lstrlen(temporary)-1]==')')){
468 SetError(1,NULL,cp);
469 }
470 RemoveStringPare(temporary);
471
472 Type dummyType;
473 CallProc( PROC_DEFAULT
474 , &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc()
475 , compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc().GetName().c_str()
476 , temporary
477 , dummyType );
478 }
479 else{
480 //基底クラスのコンストラクタを暗黙的に呼び出す
481 Opcode_CallProc("",
482 &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc(),
483 0,
484 "",
485 0);
486 }
487 }
488
489 //仮想関数テーブルを初期化
490 if( compiler.pCompilingClass->IsExistVirtualFunctions()
491 && !compiler.pCompilingClass->IsAbstract() ){
492 //関数テーブルに値をセット
493 int offset = (int)compiler.pCompilingClass->GetVtblGlobalOffset();
494
495 //mov rax,offset
496 compiler.codeGenerator.op_mov_RV(sizeof(_int64),REG_RAX,offset, Schedule::DataTable );
497
498 //Thisポインタをrcxにコピー
499 SetThisPtrToReg(REG_RCX);
500
501 //mov qword ptr[rcx],rax
502 compiler.codeGenerator.op_mov_MR(sizeof(_int64),REG_RAX,REG_RCX,0,MOD_BASE);
503 }
504 }
505 else if( pUserProc->IsDestructor() ){
506 //デストラクタをコンパイルしたとき
507
508 //デストラクタのコンパイル開始を通知
509 compiler.pCompilingClass->NotifyStartDestructorCompile();
510 }
511 }
512
513 //////////////////////////////////////////
514 //////////////////////////////////////////
515 ////// プロシージャ内をコンパイル ////////
516 if( pUserProc->IsAutoGeneration() ){
517 AutoGeneration( *pUserProc );
518 }
519 else{
520 if(pUserProc->IsMacro()){
521 CompileBuffer(ESC_ENDMACRO,0);
522 }
523 else{
524 if(pUserProc->IsSub()){
525 CompileBuffer(ESC_ENDSUB,0);
526 }
527 else if(pUserProc->IsFunction()){
528 CompileBuffer(ESC_ENDFUNCTION,0);
529 }
530 }
531 }
532 //////////////////////////////////////////
533 //////////////////////////////////////////
534
535 if( compiler.pCompilingClass ){
536
537 if( compiler.pCompilingClass->IsCompilingConstructor() ){
538 // コンストラクタをコンパイルしていたとき
539
540 // コンストラクタのコンパイルが完了したことを通知
541 compiler.pCompilingClass->NotifyFinishConstructorCompile();
542 }
543 else if( pUserProc->IsDestructor() ){
544 ////////////////////////////////////
545 //デストラクタをコンパイルしたとき
546 ////////////////////////////////////
547
548 // デストラクタのコンパイルが完了したことを通知
549 compiler.pCompilingClass->NotifyFinishDestructorCompile();
550
551 if( compiler.pCompilingClass->HasSuperClass() ){
552 /* サブクラスのデストラクタをコンパイルしているときは、
553 基底クラスのデストラクタを呼び出す */
554
555 const CMethod *method = compiler.pCompilingClass->GetSuperClass().GetDestructorMethod();
556 if( method ){
557 Opcode_CallProc("",
558 &method->GetUserProc(),
559 0,
560 "",
561 0);
562 }
563 }
564 }
565 }
566
567 //ラベル用のメモリを解放
568 for(i3=0;i3<MaxLabelNum;i3++){
569 if(pLabelNames[i3].pName) HeapDefaultFree(pLabelNames[i3].pName);
570 }
571 HeapDefaultFree(pLabelNames);
572
573 //With情報のメモリを解放
574 for(i3=0;i3<WithInfo.num;i3++){
575 SetError(22,"With",WithInfo.pWithCp[i3]);
576 HeapDefaultFree(WithInfo.ppName[i3]);
577 }
578 HeapDefaultFree(WithInfo.ppName);
579 HeapDefaultFree(WithInfo.pWithCp);
580
581 //ローカルオブジェクト(レキシカルスコープレベル=0)の解放処理
582 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
583
584 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
585 compiler.codeGenerator.ResolveExitSubSchedule();
586
587 if(bDebugCompile&&bDebugSupportProc==0){
588 //call _DebugSys_EndProc
589 extern const UserProc *pSub_DebugSys_EndProc;
590 compiler.codeGenerator.op_call(pSub_DebugSys_EndProc);
591 }
592
593 if( !pUserProc->ReturnType().IsNull() ){
594 //////////////////////////////////
595 // 戻り値をraxまたはxmm0に設定
596 //////////////////////////////////
597
598 RELATIVE_VAR RelativeVar;
599
600 const char *temp = pUserProc->GetName().c_str();
601 if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
602 temp="_System_ReturnValue";
603 }
604 GetVarOffsetReadWrite(temp,&RelativeVar,Type());
605
606 i3=pUserProc->ReturnType().GetBasicType();
607
608 if(i3==DEF_OBJECT || i3==DEF_STRUCT){
609 SetVarPtrToReg(REG_RAX,&RelativeVar);
610 if( i3==DEF_OBJECT ){
611 //mov rax,qword ptr[rax]
612 compiler.codeGenerator.op_mov_RM( sizeof(_int64), REG_RAX, REG_RAX, 0, MOD_BASE );
613 }
614 }
615 else if(i3==DEF_DOUBLE){
616 //64ビット実数型
617 SetXmmReg_DoubleVariable(&RelativeVar,REG_XMM0);
618 }
619 else if(i3==DEF_SINGLE){
620 //32ビット実数型
621 SetXmmReg_SingleVariable(&RelativeVar,REG_XMM0);
622 }
623 else if(IsWholeNumberType(i3)){
624 //整数型
625 SetReg_WholeVariable(i3,&RelativeVar,REG_RAX);
626 }
627 else SetError(300,NULL,cp);
628 }
629
630 //ローカル変数領域のサイズをスタックフレームに通知
631 int localParmSize = AllLocalVarSize - BaseLocalVar;
632 int stackFrameSize = pobj_sf->GetFrameSize( localParmSize );
633
634 //ローカル変数アドレススケジュール
635 BOOST_FOREACH( const PertialSchedule *pPertialSchedule, compiler.codeGenerator.localVarPertialSchedules )
636 {
637 compiler.codeGenerator.opfix_offset( pPertialSchedule, AllLocalVarSize + stackFrameSize );
638 }
639 compiler.codeGenerator.localVarPertialSchedules.clear();
640 BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
641 //後にデバッグで利用する
642 pVar->SetOffsetAddress(
643 AllLocalVarSize + stackFrameSize - pVar->GetOffsetAddress()
644 );
645 }
646
647 //mov reg,qword ptr[rsp+offset] ※スタックフレームを利用
648 pobj_sf->pop(REG_R15);
649 pobj_sf->pop(REG_R14);
650 pobj_sf->pop(REG_R13);
651 pobj_sf->pop(REG_R12);
652 pobj_sf->pop(REG_RDI);
653 pobj_sf->pop(REG_RSI);
654 pobj_sf->pop(REG_RBX);
655
656 int stackFrameAndLocalParamSize = localParmSize + stackFrameSize;
657
658 //add rsp,スタックフレームサイズ
659 compiler.codeGenerator.op_add_rsp(stackFrameAndLocalParamSize);
660
661 //ret
662 compiler.codeGenerator.op_ret();
663
664
665 //デバッグ用
666 if( pRspOffsetPertialSchedule1 ){
667 compiler.codeGenerator.opfix( pRspOffsetPertialSchedule1, stackFrameAndLocalParamSize );
668 compiler.codeGenerator.opfix( pRspOffsetPertialSchedule2, stackFrameAndLocalParamSize + sizeof(_int64) );
669 }
670
671
672 //スタックフレームスケジュール(subコマンド)
673 compiler.codeGenerator.opfix( pStackFramePertialSchedule, stackFrameAndLocalParamSize );
674
675 //スタックフレームスケジュールを実行
676 pobj_sf->RunningSchedule( stackFrameSize );
677 delete pobj_sf;
678 pobj_sf=0;
679
680
681 pUserProc->SetEndOpAddress( obp );
682
683
684 //重複エラー情報管理のメモリを解放
685 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
686 HeapDefaultFree(SynonymErrorWords);
687}
688
689void CompileBufferInProcedure( const UserProc &userProc ){
690 if( userProc.IsUsing() == false || userProc.IsCompiled() ) return;
691
692 _compile_proc( &userProc );
693
694 // ログを履く
695 char temporary[8192];
696 temporary[0]=0;
697 lstrcat( temporary, "------------------------------------------------------------------\n" );
698 sprintf( temporary + lstrlen(temporary), "【 %s のコード情報】\n", userProc.GetName().c_str() );
699 sprintf( temporary + lstrlen(temporary), "code size: %d bytes\n", userProc.GetCodeSize() );
700 lstrcat( temporary, "------------------------------------------------------------------\n" );
701 lstrcat( temporary, "\n" );
702 trace_for_size( temporary );
703}
704void CompileLocal(){
705 extern BOOL bDll;
706 if(bDll){
707 //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
708 const UserProc *pUserProc = GetSubHash("_System_InitDllGlobalVariables");
709 if(pUserProc){
710 CompileBufferInProcedure( *pUserProc );
711 }
712 else SetError(300,NULL,cp);
713 }
714
715 //_System_TypeBase_InitializeUserTypesは一番最後にコンパイル
716 extern const UserProc *pSubStaticMethod_System_TypeBase_InitializeUserTypes;
717 pSubStaticMethod_System_TypeBase_InitializeUserTypes->CompleteCompile();
718
719 //_System_InitStaticLocalVariablesは一番最後にコンパイル
720 //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
721 extern const UserProc *pSub_System_InitStaticLocalVariables;
722 pSub_System_InitStaticLocalVariables->CompleteCompile();
723
724 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
725 extern const UserProc *pSub_System_Call_Destructor_of_GlobalObject;
726 pSub_System_Call_Destructor_of_GlobalObject->CompleteCompile();
727
728repeat:
729 compiler.GetMeta().GetUserProcs().Iterator_Reset();
730 while( compiler.GetMeta().GetUserProcs().Iterator_HasNext() )
731 {
732 UserProc *pUserProc = compiler.GetMeta().GetUserProcs().Iterator_GetNext();
733 CompileBufferInProcedure( *pUserProc );
734 }
735
736 if( IsNeedProcCompile() ){
737 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
738 goto repeat;
739 }
740
741 //_System_TypeBase_InitializeUserTypesは最後のほうでコンパイル
742 pSubStaticMethod_System_TypeBase_InitializeUserTypes->KillCompileStatus();
743 CompileBufferInProcedure( *pSubStaticMethod_System_TypeBase_InitializeUserTypes );
744
745 if( IsNeedProcCompile() ){
746 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
747
748 compiler.GetMeta().GetUserProcs().Iterator_Reset();
749 while( compiler.GetMeta().GetUserProcs().Iterator_HasNext() )
750 {
751 UserProc *pUserProc = compiler.GetMeta().GetUserProcs().Iterator_GetNext();
752 CompileBufferInProcedure( *pUserProc );
753 }
754 }
755
756 //_System_InitStaticLocalVariablesは一番最後にコンパイル
757 pSub_System_InitStaticLocalVariables->KillCompileStatus();
758 CompileBufferInProcedure( *pSub_System_InitStaticLocalVariables );
759
760 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
761 pSub_System_Call_Destructor_of_GlobalObject->KillCompileStatus();
762 CompileBufferInProcedure( *pSub_System_Call_Destructor_of_GlobalObject );
763}
Note: See TracBrowser for help on using the repository browser.