source: dev/BasicCompiler64/Compile_ProcOp.cpp@ 109

Last change on this file since 109 was 108, checked in by dai_9181, 17 years ago

関数、クラスメソッドにImports機構を適用。

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