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

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