source: dev/BasicCompiler64/Compile_ProcOp.cpp@ 122

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

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

File size: 27.0 KB
Line 
1#include "../BasicCompiler_Common/common.h"
2#include "Opcode.h"
3
4void SystemProc( const UserProc &userProc ){
5 if( userProc.GetName() == "_System_GetEip" ){
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 }
12 else if( userProc.GetName() == "_System_InitDllGlobalVariables" ){
13 ////////////////////////////////////////
14 // DLLのグローバル領域をコンパイル
15 ////////////////////////////////////////
16 extern BOOL bDll;
17 if(!bDll){
18 //ret
19 OpBuffer[obp++]=(char)0xC3;
20
21 return;
22 }
23
24 UserProc *pBackUserProc;
25 pBackUserProc = &UserProc::CompilingUserProc();
26 UserProc::CompileStartForGlobalArea();
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
46 //クラスに属する静的メンバを定義
47 CMember::InitStaticMember();
48
49 GetGlobalDataForDll();
50
51 //add rsp,スタックフレームサイズ
52 op_add_RV(REG_RSP,pobj_sf->GetFrameSize());
53
54 //スタックフレームスケジュール(subコマンドに渡す値)
55 *((long *)(OpBuffer+StackFrameSchedule))=pobj_sf->GetFrameSize();
56
57 UserProc::CompileStartForUserProc( pBackUserProc );
58 cp=BackCp;
59
60 //ret
61 OpBuffer[obp++]=(char)0xC3;
62 }
63 else if( userProc.GetName() == "_System_InitStaticLocalVariables" ){
64 //静的ローカルオブジェクトのコンストラクタ呼び出し
65
66 //sub rsp,スタックフレームサイズ
67 int StackFrameSchedule;
68 op_sub_rsp(0xFFFFFFFF);
69 StackFrameSchedule=obp-sizeof(long);
70
71 foreach( Variable *pVar, globalVars ){
72 if(memicmp(pVar->GetName().c_str(),"Static%",7)==0){
73 //コンストラクタ呼び出し
74 if( pVar->IsObject() ){
75
76 //エラー用
77 cp=pVar->source_code_address;
78
79 CallConstructor(
80 pVar->GetName().c_str(),
81 pVar->GetSubScriptsPtr(),
82 *pVar,
83 pVar->paramStrForConstructor.c_str());
84 }
85 }
86 }
87
88 //add rsp,スタックフレームサイズ
89 op_add_RV(REG_RSP,pobj_sf->GetFrameSize());
90
91 //スタックフレームスケジュール(subコマンドに渡す値)
92 *((long *)(OpBuffer+StackFrameSchedule))=pobj_sf->GetFrameSize();
93
94 //ret
95 OpBuffer[obp++]=(char)0xC3;
96 }
97 else if( userProc.GetName() == "_System_Call_Destructor_of_GlobalObject" ){
98 //sub rsp,8(※RSPを16バイト境界にあわせるため)
99 op_sub_rsp(0x8);
100
101
102 UserProc *pBackUserProc;
103 pBackUserProc = &UserProc::CompilingUserProc();
104 UserProc::CompileStartForGlobalArea();
105
106 obj_LexScopes.CallDestructorsOfScopeEnd();
107
108 UserProc::CompileStartForUserProc( pBackUserProc );
109
110
111 //add rsp,8
112 op_add_RV(REG_RSP,0x8);
113
114 //ret
115 OpBuffer[obp++]=(char)0xC3;
116 }
117 else if( userProc.GetName() == "_System_GetSp" ){
118 //mov rax,rsp
119 op_mov_RR(REG_RAX,REG_RSP);
120
121 //add rax,PTR_SIZE
122 op_add_RV(REG_RAX,PTR_SIZE);
123
124 //ret
125 OpBuffer[obp++]=(char)0xC3;
126 }
127 else if( userProc.GetName() == "_allrem" ){
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 }
136 else if( userProc.GetName() == "_allmul" ){
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 }
145 else if( userProc.GetName() == "_alldiv" ){
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 }
154 else if( userProc.GetName() == "_allshl" ){
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 }
163 else if( userProc.GetName() == "_allshr" ){
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 }
172 else if( userProc.GetName() == "_aullshr" ){
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 }
197 else{
198 SetError();
199 }
200}
201void AutoGeneration(UserProc &userProc){
202 if( userProc.GetName() == "InitializeUserTypes"
203 && userProc.HasParentClass()
204 && (string)userProc.GetParentClass().name == "_System_TypeBase" ){
205
206 pobj_DBClass->Compile_System_InitializeUserTypes();
207 }
208 else if( userProc.GetName() == "RegisterGlobalRoots"
209 && userProc.HasParentClass()
210 && (string)userProc.GetParentClass().name == "_System_CGarbageCollection" ){
211
212 Compile_AddGlobalRootsForGc();
213 }
214 else{
215 SetError();
216 }
217}
218void _compile_proc(UserProc *pUserProc){
219 extern char *basbuf;
220 extern HANDLE hHeap;
221 extern GlobalProc **ppSubHash;
222 extern BOOL bDebugCompile;
223 int i3,i4;
224 char temporary[VN_SIZE];
225
226 if( pUserProc->IsUsing() == false || pUserProc->IsCompiled() ) return;
227
228 if( pUserProc->localVars.size() ){
229 SetError();
230 return;
231 }
232
233 pUserProc->CompleteCompile();
234
235 extern BOOL bSystemProc;
236 if(memcmp(pUserProc->GetName().c_str(),"_System_",8)==0) bSystemProc=1;
237 else bSystemProc=0;
238
239 extern BOOL bDebugSupportProc;
240 if(memcmp(pUserProc->GetName().c_str(),"_DebugSys_",10)==0){
241 if(!bDebugCompile){
242 return;
243 }
244 bDebugSupportProc=1;
245 }
246 else bDebugSupportProc=0;
247
248 pUserProc->beginOpAddress=obp;
249
250 //コンパイル中の関数が属するクラス
251 pobj_CompilingClass=pUserProc->GetParentClassPtr();
252
253 //コンパイルスタートをクラス管理クラスに追加
254 pobj_DBClass->StartCompile( pUserProc );
255
256 //コンパイル中の関数
257 UserProc::CompileStartForUserProc( pUserProc );
258
259 // コンパイル中の関数が属する名前空間
260 Smoothie::Lexical::liveingNamespaceScopes = pUserProc->GetNamespaceScopes();
261
262 // コンパイル中の関数でImportsされている名前空間
263 Smoothie::Meta::importedNamespaces = pUserProc->GetImportedNamespaces();
264
265 if(pUserProc->IsSystem()){
266 ////////////////////
267 // 特殊関数
268 ////////////////////
269
270 extern int AllLocalVarSize;
271 AllLocalVarSize=0;
272
273 //スタックフレーム管理用オブジェクトを初期化
274 extern CStackFrame *pobj_sf;
275 pobj_sf=new CStackFrame();
276
277 SystemProc(*pUserProc);
278
279 //スタックフレーム管理用オブジェクトを破棄
280 delete pobj_sf;
281 pobj_sf=0;
282
283 pUserProc->endOpAddress=obp;
284 return;
285 }
286
287 cp=pUserProc->GetCodePos();
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
345 //パラメータ用の変数データを考慮
346 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
347 Parameter &param = *pUserProc->RealParams()[i3];
348
349 Variable *pVar = new Variable( param.GetVarName(), param, false, param.IsRef() );
350
351 if( param.IsArray() ){
352 pVar->SetArray( param.GetSubScriptsPtr() );
353 }
354
355 int varSize;
356 if( param.IsRef() == false && param.IsStruct() ){
357 //構造体のByValパラメータ
358 pVar->ThisIsParameter();
359 varSize=PTR_SIZE;
360 }
361 else{
362 if( param.IsArray() == false ){
363 varSize = pVar->GetMemorySize();
364 }
365 else{
366 varSize=PTR_SIZE;
367 }
368 }
369 AllLocalVarSize+=varSize;
370 pVar->offset=AllLocalVarSize;
371
372 //レキシカルスコープ情報
373 pVar->ScopeLevel=obj_LexScopes.GetNowLevel();
374 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();
375 pVar->bLiving=TRUE;
376
377 pUserProc->localVars.push_back( pVar );
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
392 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
393 Parameter &param = *pUserProc->RealParams()[i3];
394 if(i3==3){
395 if(param.IsReal()&&param.IsRef() == false){
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){
405 if(param.IsReal()&&param.IsRef() == false){
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){
415 if(param.IsReal()&&param.IsRef() == false){
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){
425 if(param.IsReal()&&param.IsRef() == false){
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
457 if( !pUserProc->ReturnType().IsNull() ){
458 //戻り値が存在するとき
459
460 const char *temp = pUserProc->GetName().c_str();
461 if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
462 temp = "_System_ReturnValue";
463 }
464
465 if( pUserProc->ReturnType().IsStruct() ){
466 //戻り値用の構造体(値型)はパラメータで引き渡される
467 }
468 else{
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 }
476
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
489 op_mov_RR(REG_RCX,REG_RSP);
490
491 //add rcx,スタックフレームサイズ+sizeof(_int64) ※ret用のサイズを考慮
492 op_add_RV(REG_RCX,0);
493 RspOffsetSchedule2=obp-sizeof(long);
494
495 //call _DebugSys_StartProc
496 extern UserProc *pSub_DebugSys_StartProc;
497 op_call(pSub_DebugSys_StartProc);
498 }
499
500 if(pobj_CompilingClass){
501 if( pUserProc->GetName() == pobj_CompilingClass->name ){
502 ////////////////////////////////////
503 // コンストラクタをコンパイルするとき
504 ////////////////////////////////////
505
506 //コンストラクタのコンパイル開始を通知
507 pobj_CompilingClass->NotifyStartConstructorCompile();
508
509 //基底クラスかどうかの識別
510 //(継承元がインターフェイスの場合も基底クラスと見なす)
511 BOOL bThisIsSuperClass;
512 if(pobj_CompilingClass->pobj_InheritsClass==0) bThisIsSuperClass=1;
513 else if( pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod() == NULL ){
514 //インターフェイスを継承したときはコンストラクタを持たない
515 bThisIsSuperClass=1;
516 }
517 else bThisIsSuperClass=0;
518
519 if(!bThisIsSuperClass){
520 /* サブクラスコンストラクタをコンパイルしているときは、
521 基底クラスのコンストラクタを呼び出す */
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){
534 //基底クラスのコンストラクタを呼び出す
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
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 );
554 }
555 else{
556 //基底クラスのコンストラクタを暗黙的に呼び出す
557 Opcode_CallProc("",
558 pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod()->pUserProc,
559 0,
560 "",
561 0);
562 }
563 }
564
565 //新しいオブジェクト領域は0で初期化されているため、Nothingを明示的に代入する必要はない
566/*
567 //実体クラスを持つメンバのコンストラクタ(引数有りを除く)を呼び出す
568 for(i3=0;i3<pobj_CompilingClass->iMemberNum;i3++){
569 CMember *pMember = pobj_CompilingClass->ppobj_Member[i3];
570 if(pMember->IsObject()){
571 // オブジェクトメンバを発見したとき
572
573 sprintf(temporary, "This.%s=Nothing",
574 pMember->name );
575 OpcodeCalc( temporary );
576 }
577 }
578*/
579
580 //仮想関数テーブルを初期化
581 if(pobj_CompilingClass->vtbl_num&&
582 pobj_CompilingClass->IsAbstract()==false){
583 //関数テーブルに値をセット
584 int offset = (int)pobj_CompilingClass->GetVtblGlobalOffset();
585
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);
591
592 //Thisポインタをrcxにコピー
593 SetThisPtrToReg(REG_RCX);
594
595 //mov qword ptr[rcx],rax
596 op_mov_MR(sizeof(_int64),REG_RAX,REG_RCX,0,MOD_BASE);
597 }
598 }
599 else if( pUserProc->IsDestructor() ){
600 //デストラクタをコンパイルしたとき
601
602 //デストラクタのコンパイル開始を通知
603 pobj_CompilingClass->NotifyStartDestructorCompile();
604 }
605 }
606
607 //////////////////////////////////////////
608 //////////////////////////////////////////
609 ////// プロシージャ内をコンパイル ////////
610 if( pUserProc->IsAutoGeneration() ){
611 AutoGeneration( *pUserProc );
612 }
613 else{
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 }
625 }
626 //////////////////////////////////////////
627 //////////////////////////////////////////
628
629 if( pobj_CompilingClass ){
630
631 if( pobj_CompilingClass->IsCompilingConstructor() ){
632 // コンストラクタをコンパイルしていたとき
633
634 // コンストラクタのコンパイルが完了したことを通知
635 pobj_CompilingClass->NotifyFinishConstructorCompile();
636 }
637 else if( pUserProc->IsDestructor() ){
638 ////////////////////////////////////
639 //デストラクタをコンパイルしたとき
640 ////////////////////////////////////
641
642 // デストラクタのコンパイルが完了したことを通知
643 pobj_CompilingClass->NotifyFinishDestructorCompile();
644
645 if(pobj_CompilingClass->pobj_InheritsClass){
646 /* サブクラスのデストラクタをコンパイルしているときは、
647 基底クラスのデストラクタを呼び出す */
648
649 CMethod *method = pobj_CompilingClass->pobj_InheritsClass->GetDestructorMethod();
650 if( method ){
651 Opcode_CallProc("",
652 method->pUserProc,
653 0,
654 "",
655 0);
656 }
657 }
658
659 //実体クラスを持つメンバのデストラクタ呼び出しはGCに任せる
660 /*
661 //※コンストラクタと逆順序で呼び出す
662 for(i3=pobj_CompilingClass->iMemberNum-1;i3>=0;i3--){
663 CMember *pMember = pobj_CompilingClass->ppobj_Member[i3];
664 int MemberTypeSize=
665 GetTypeSize(pMember->TypeInfo.type,
666 pMember->TypeInfo.u.lpIndex);
667
668 int MemberObjectNum=
669 JumpSubScripts(pMember->SubScripts);
670
671 int offset = pobj_CompilingClass->GetMemberOffset( pMember->name, NULL );
672
673 if(pMember->IsObject()){
674 CMethod *method = pMember->GetClass().GetDestructorMethod();
675 if( method ){
676 for(i4=MemberObjectNum-1;i4>=0;i4--){
677 //Thisポインタをrcxにコピー
678 SetThisPtrToReg(REG_RCX);
679
680 //add rcx,offset
681 op_add_RV(REG_RCX,offset+i4*MemberTypeSize);
682
683 //call destructor
684 op_call(method->pUserProc);
685 }
686 }
687 }
688 }*/
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)の解放処理
720 obj_LexScopes.CallDestructorsOfScopeEnd();
721
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
730 extern UserProc *pSub_DebugSys_EndProc;
731 op_call(pSub_DebugSys_EndProc);
732 }
733
734 if( !pUserProc->ReturnType().IsNull() ){
735 //////////////////////////////////
736 // 戻り値をraxまたはxmm0に設定
737 //////////////////////////////////
738
739 RELATIVE_VAR RelativeVar;
740
741 const char *temp = pUserProc->GetName().c_str();
742 if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
743 temp="_System_ReturnValue";
744 }
745 GetVarOffsetReadWrite(temp,&RelativeVar,Type());
746
747 i3=pUserProc->ReturnType().GetBasicType();
748
749 if(i3==DEF_OBJECT || i3==DEF_STRUCT){
750 SetVarPtrToReg(REG_RAX,&RelativeVar);
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 }
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);
781 foreach( Variable *pVar, pUserProc->localVars ){
782 //後にデバッグで利用する
783 pVar->offset = AllLocalVarSize + pobj_sf->GetFrameSize() - pVar->offset;
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
821 pUserProc->endOpAddress=obp;
822
823
824 //重複エラー情報管理のメモリを解放
825 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
826 HeapDefaultFree(SynonymErrorWords);
827}
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" );
838 sprintf( temporary + lstrlen(temporary), "【 %s のコード情報】\n", userProc.GetName().c_str() );
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}
844void CompileLocal(){
845 extern GlobalProc **ppSubHash;
846 int i2;
847
848 extern BOOL bDll;
849 if(bDll){
850 //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
851 UserProc *pUserProc=GetSubHash("_System_InitDllGlobalVariables");
852 if(pUserProc){
853 CompileBufferInProcedure( *pUserProc );
854 }
855 else SetError(300,NULL,cp);
856 }
857
858 //_System_TypeBase_InitializeUserTypesは一番最後にコンパイル
859 extern UserProc *pSubStaticMethod_System_TypeBase_InitializeUserTypes;
860 pSubStaticMethod_System_TypeBase_InitializeUserTypes->CompleteCompile();
861
862 //_System_InitStaticLocalVariablesは一番最後にコンパイル
863 //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
864 extern UserProc *pSub_System_InitStaticLocalVariables;
865 pSub_System_InitStaticLocalVariables->CompleteCompile();
866
867 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
868 extern UserProc *pSub_System_Call_Destructor_of_GlobalObject;
869 pSub_System_Call_Destructor_of_GlobalObject->CompleteCompile();
870
871repeat:
872 GlobalProc *pGlobalProc;
873 for(i2=0;i2<MAX_HASH;i2++){
874 pGlobalProc=ppSubHash[i2];
875 while(pGlobalProc){
876 CompileBufferInProcedure( *pGlobalProc );
877 pGlobalProc=pGlobalProc->pNextData;
878 }
879 }
880
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++){
893 pGlobalProc=ppSubHash[i2];
894 while(pGlobalProc){
895 CompileBufferInProcedure( *pGlobalProc );
896 pGlobalProc=pGlobalProc->pNextData;
897 }
898 }
899 }
900
901 //_System_InitStaticLocalVariablesは一番最後にコンパイル
902 pSub_System_InitStaticLocalVariables->KillCompileStatus();
903 CompileBufferInProcedure( *pSub_System_InitStaticLocalVariables );
904
905 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
906 pSub_System_Call_Destructor_of_GlobalObject->KillCompileStatus();
907 CompileBufferInProcedure( *pSub_System_Call_Destructor_of_GlobalObject );
908}
Note: See TracBrowser for help on using the repository browser.