source: dev/BasicCompiler64/Compile_ProcOp.cpp@ 95

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

RegisterGlobalRootsの自動生成に対応

File size: 26.7 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 UserProc **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 if(pUserProc->IsSystem()){
260 ////////////////////
261 // 特殊関数
262 ////////////////////
263
264 extern int AllLocalVarSize;
265 AllLocalVarSize=0;
266
267 //スタックフレーム管理用オブジェクトを初期化
268 extern CStackFrame *pobj_sf;
269 pobj_sf=new CStackFrame();
270
271 SystemProc(*pUserProc);
272
273 //スタックフレーム管理用オブジェクトを破棄
274 delete pobj_sf;
275 pobj_sf=0;
276
277 pUserProc->endOpAddress=obp;
278 return;
279 }
280
281 cp=pUserProc->GetCodePos();
282 for(;;cp++){
283 if(IsCommandDelimitation(basbuf[cp])) break;
284 }
285 cp--;
286
287 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
288 extern DWORD *pExitSubSchedule;
289 extern int ExitSubScheduleNum;
290 pExitSubSchedule=(DWORD *)HeapAlloc(hHeap,0,1);
291 ExitSubScheduleNum=0;
292
293 //ラベル用のメモリを確保
294 extern LABEL *pLabelNames;
295 extern int MaxLabelNum;
296 pLabelNames=(LABEL *)HeapAlloc(hHeap,0,1);
297 MaxLabelNum=0;
298
299 //Gotoラベルスケジュール
300 extern GOTOLABELSCHEDULE *pGotoLabelSchedule;
301 extern int GotoLabelScheduleNum;
302 pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapAlloc(hHeap,0,1);
303 GotoLabelScheduleNum=0;
304
305 //With情報のメモリを確保
306 extern WITHINFO WithInfo;
307 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
308 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
309 WithInfo.num=0;
310
311 //重複エラー情報管理のメモリを確保
312 extern char **SynonymErrorWords;
313 extern int SynonymErrorNum;
314 SynonymErrorNum=0;
315 SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
316
317 //Continueアドレスを初期化
318 extern DWORD dwContinueAddress;
319 dwContinueAddress=-1;
320
321 //ローカル変数に関する情報
322 extern int AllLocalVarSize;
323 AllLocalVarSize=0;
324
325 //ローカル変数アドレススケジュール
326 extern DWORD *pLocalVarAddrSchedule;
327 extern int LocalVarAddrScheduleNum;
328 pLocalVarAddrSchedule=(DWORD *)HeapAlloc(hHeap,0,1);
329 LocalVarAddrScheduleNum=0;
330
331 //レキシカルスコープ情報を初期化
332 obj_LexScopes.Init(obp);
333
334
335 /////////////////////////////////////
336 // パラメータ用の変数データを考慮
337 /////////////////////////////////////
338
339 //パラメータ用の変数データを考慮
340 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
341 Parameter &param = *pUserProc->RealParams()[i3];
342
343 Variable *pVar = new Variable( param.GetVarName(), param, false, param.IsRef() );
344
345 if( param.IsArray() ){
346 pVar->SetArray( param.GetSubScriptsPtr() );
347 }
348
349 int varSize;
350 if( param.IsRef() == false && param.IsStruct() ){
351 //構造体のByValパラメータ
352 pVar->ThisIsParameter();
353 varSize=PTR_SIZE;
354 }
355 else{
356 if( param.IsArray() == false ){
357 varSize = pVar->GetMemorySize();
358 }
359 else{
360 varSize=PTR_SIZE;
361 }
362 }
363 AllLocalVarSize+=varSize;
364 pVar->offset=AllLocalVarSize;
365
366 //レキシカルスコープ情報
367 pVar->ScopeLevel=obj_LexScopes.GetNowLevel();
368 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();
369 pVar->bLiving=TRUE;
370
371 pUserProc->localVars.push_back( pVar );
372 }
373
374 //Thisポインタを示すローカルオフセット値をセット
375 extern int LocalVar_ThisPtrOffset;
376 LocalVar_ThisPtrOffset=AllLocalVarSize;
377
378 //スタックフレーム管理用クラスを初期化
379 extern CStackFrame *pobj_sf;
380 pobj_sf=new CStackFrame();
381
382
383 ///////////////////////
384 // ここからコード生成
385
386 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
387 Parameter &param = *pUserProc->RealParams()[i3];
388 if(i3==3){
389 if(param.IsReal()&&param.IsRef() == false){
390 //movsd qword ptr[rsp+0x20],xmm3
391 op_movsd_MR(REG_XMM3,REG_RSP,0x20,MOD_BASE_DISP32);
392 }
393 else{
394 //mov qword ptr[rsp+0x20],r9
395 op_mov_MR(sizeof(_int64),REG_R9,REG_RSP,0x20,MOD_BASE_DISP32);
396 }
397 }
398 if(i3==2){
399 if(param.IsReal()&&param.IsRef() == false){
400 //movsd qword ptr[rsp+0x18],xmm2
401 op_movsd_MR(REG_XMM2,REG_RSP,0x18,MOD_BASE_DISP32);
402 }
403 else{
404 //mov qword ptr[rsp+0x18],r8
405 op_mov_MR(sizeof(_int64),REG_R8,REG_RSP,0x18,MOD_BASE_DISP32);
406 }
407 }
408 if(i3==1){
409 if(param.IsReal()&&param.IsRef() == false){
410 //movsd qword ptr[rsp+0x10],xmm1
411 op_movsd_MR(REG_XMM1,REG_RSP,0x10,MOD_BASE_DISP32);
412 }
413 else{
414 //mov qword ptr[rsp+0x10],rdx
415 op_mov_MR(sizeof(_int64),REG_RDX,REG_RSP,0x10,MOD_BASE_DISP32);
416 }
417 }
418 if(i3==0){
419 if(param.IsReal()&&param.IsRef() == false){
420 //movsd qword ptr[rsp+0x8],xmm0
421 op_movsd_MR(REG_XMM0,REG_RSP,0x8,MOD_BASE_DISP32);
422 }
423 else{
424 //mov qword ptr[rsp+0x8],rcx
425 op_mov_MR(sizeof(_int64),REG_RCX,REG_RSP,0x8,MOD_BASE_DISP32);
426 }
427 }
428 }
429
430 //ret用のアドレスを考慮
431 AllLocalVarSize+=sizeof(_int64);
432
433 //sub rsp,スタックフレームサイズ
434 int StackFrameSchedule;
435 op_sub_rsp(0xFFFFFFFF);
436 StackFrameSchedule=obp-sizeof(long);
437
438 //mov qword ptr[rsp+offset],reg ※スタックフレームを利用
439 pobj_sf->push(REG_RBX);
440 pobj_sf->push(REG_RSI);
441 pobj_sf->push(REG_RDI);
442 pobj_sf->push(REG_R12);
443 pobj_sf->push(REG_R13);
444 pobj_sf->push(REG_R14);
445 pobj_sf->push(REG_R15);
446
447 //ローカル変数のベース値
448 int BaseLocalVar;
449 BaseLocalVar=AllLocalVarSize;
450
451 if( !pUserProc->ReturnType().IsNull() ){
452 //戻り値が存在するとき
453
454 const char *temp = pUserProc->GetName().c_str();
455 if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
456 temp = "_System_ReturnValue";
457 }
458
459 if( pUserProc->ReturnType().IsStruct() ){
460 //戻り値用の構造体(値型)はパラメータで引き渡される
461 }
462 else{
463 if( pUserProc->ReturnType().IsObject() ){
464 sprintf(temporary,"%s=Nothing%c%c%s",temp,1,ESC_AS, pUserProc->ReturnType().ToString().c_str() );
465 }
466 else{
467 //戻り値用の変数の定義
468 sprintf(temporary,"%s%c%c%s",temp,1,ESC_AS, pUserProc->ReturnType().ToString().c_str() );
469 }
470
471 OpcodeDim(temporary,0);
472 }
473 }
474
475 int RspOffsetSchedule=0;
476 int RspOffsetSchedule2;
477 if(bDebugCompile&&bDebugSupportProc==0){
478 //mov rdx, qword ptr[rsp+スタックフレームサイズ]
479 op_mov_RM(sizeof(_int64),REG_RDX,REG_RSP,0,MOD_BASE_DISP32);
480 RspOffsetSchedule=obp-sizeof(long);
481
482 //mov rcx,rsp
483 op_mov_RR(REG_RCX,REG_RSP);
484
485 //add rcx,スタックフレームサイズ+sizeof(_int64) ※ret用のサイズを考慮
486 op_add_RV(REG_RCX,0);
487 RspOffsetSchedule2=obp-sizeof(long);
488
489 //call _DebugSys_StartProc
490 extern UserProc *pSub_DebugSys_StartProc;
491 op_call(pSub_DebugSys_StartProc);
492 }
493
494 if(pobj_CompilingClass){
495 if( pUserProc->GetName() == pobj_CompilingClass->name ){
496 ////////////////////////////////////
497 // コンストラクタをコンパイルするとき
498 ////////////////////////////////////
499
500 //コンストラクタのコンパイル開始を通知
501 pobj_CompilingClass->NotifyStartConstructorCompile();
502
503 //基底クラスかどうかの識別
504 //(継承元がインターフェイスの場合も基底クラスと見なす)
505 BOOL bThisIsSuperClass;
506 if(pobj_CompilingClass->pobj_InheritsClass==0) bThisIsSuperClass=1;
507 else if( pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod() == NULL ){
508 //インターフェイスを継承したときはコンストラクタを持たない
509 bThisIsSuperClass=1;
510 }
511 else bThisIsSuperClass=0;
512
513 if(!bThisIsSuperClass){
514 /* サブクラスコンストラクタをコンパイルしているときは、
515 基底クラスのコンストラクタを呼び出す */
516
517 i3=cp+1;
518 while(IsCommandDelimitation(basbuf[i3])) i3++;
519 for(i4=0;;i3++,i4++){
520 if(!IsVariableChar(basbuf[i3])){
521 temporary[i4]=0;
522 break;
523 }
524 temporary[i4]=basbuf[i3];
525 }
526 if(lstrcmp(temporary,
527 pobj_CompilingClass->pobj_InheritsClass->name)==0){
528 //基底クラスのコンストラクタを呼び出す
529 cp=i3;
530 for(i4=0;;cp++,i4++){
531 if(IsCommandDelimitation(basbuf[cp])){
532 temporary[i4]=0;
533 break;
534 }
535 temporary[i4]=basbuf[cp];
536 }
537 if(!(temporary[0]=='('&&temporary[lstrlen(temporary)-1]==')')){
538 SetError(1,NULL,cp);
539 }
540 RemoveStringPare(temporary);
541
542 Type dummyType;
543 CallProc( PROC_DEFAULT
544 , pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod()->pUserProc
545 , pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod()->pUserProc->GetName().c_str()
546 , temporary
547 , dummyType );
548 }
549 else{
550 //基底クラスのコンストラクタを暗黙的に呼び出す
551 Opcode_CallProc("",
552 pobj_CompilingClass->pobj_InheritsClass->GetConstructorMethod()->pUserProc,
553 0,
554 "",
555 0);
556 }
557 }
558
559 //新しいオブジェクト領域は0で初期化されているため、Nothingを明示的に代入する必要はない
560/*
561 //実体クラスを持つメンバのコンストラクタ(引数有りを除く)を呼び出す
562 for(i3=0;i3<pobj_CompilingClass->iMemberNum;i3++){
563 CMember *pMember = pobj_CompilingClass->ppobj_Member[i3];
564 if(pMember->IsObject()){
565 // オブジェクトメンバを発見したとき
566
567 sprintf(temporary, "This.%s=Nothing",
568 pMember->name );
569 OpcodeCalc( temporary );
570 }
571 }
572*/
573
574 //仮想関数テーブルを初期化
575 if(pobj_CompilingClass->vtbl_num&&
576 pobj_CompilingClass->IsAbstract()==false){
577 //関数テーブルに値をセット
578 int offset = (int)pobj_CompilingClass->GetVtblGlobalOffset();
579
580 //mov rax,offset
581 op_mov_RV(sizeof(_int64),REG_RAX,offset);
582 obp-=sizeof(long);
583 pobj_DataTableSchedule->add();
584 obp+=sizeof(long);
585
586 //Thisポインタをrcxにコピー
587 SetThisPtrToReg(REG_RCX);
588
589 //mov qword ptr[rcx],rax
590 op_mov_MR(sizeof(_int64),REG_RAX,REG_RCX,0,MOD_BASE);
591 }
592 }
593 else if( pUserProc->IsDestructor() ){
594 //デストラクタをコンパイルしたとき
595
596 //デストラクタのコンパイル開始を通知
597 pobj_CompilingClass->NotifyStartDestructorCompile();
598 }
599 }
600
601 //////////////////////////////////////////
602 //////////////////////////////////////////
603 ////// プロシージャ内をコンパイル ////////
604 if( pUserProc->IsAutoGeneration() ){
605 AutoGeneration( *pUserProc );
606 }
607 else{
608 if(pUserProc->IsMacro()){
609 CompileBuffer(ESC_ENDMACRO,0);
610 }
611 else{
612 if(pUserProc->IsSub()){
613 CompileBuffer(ESC_ENDSUB,0);
614 }
615 else if(pUserProc->IsFunction()){
616 CompileBuffer(ESC_ENDFUNCTION,0);
617 }
618 }
619 }
620 //////////////////////////////////////////
621 //////////////////////////////////////////
622
623 if( pobj_CompilingClass ){
624
625 if( pobj_CompilingClass->IsCompilingConstructor() ){
626 // コンストラクタをコンパイルしていたとき
627
628 // コンストラクタのコンパイルが完了したことを通知
629 pobj_CompilingClass->NotifyFinishConstructorCompile();
630 }
631 else if( pUserProc->IsDestructor() ){
632 ////////////////////////////////////
633 //デストラクタをコンパイルしたとき
634 ////////////////////////////////////
635
636 // デストラクタのコンパイルが完了したことを通知
637 pobj_CompilingClass->NotifyFinishDestructorCompile();
638
639 if(pobj_CompilingClass->pobj_InheritsClass){
640 /* サブクラスのデストラクタをコンパイルしているときは、
641 基底クラスのデストラクタを呼び出す */
642
643 CMethod *method = pobj_CompilingClass->pobj_InheritsClass->GetDestructorMethod();
644 if( method ){
645 Opcode_CallProc("",
646 method->pUserProc,
647 0,
648 "",
649 0);
650 }
651 }
652
653 //実体クラスを持つメンバのデストラクタ呼び出しはGCに任せる
654 /*
655 //※コンストラクタと逆順序で呼び出す
656 for(i3=pobj_CompilingClass->iMemberNum-1;i3>=0;i3--){
657 CMember *pMember = pobj_CompilingClass->ppobj_Member[i3];
658 int MemberTypeSize=
659 GetTypeSize(pMember->TypeInfo.type,
660 pMember->TypeInfo.u.lpIndex);
661
662 int MemberObjectNum=
663 JumpSubScripts(pMember->SubScripts);
664
665 int offset = pobj_CompilingClass->GetMemberOffset( pMember->name, NULL );
666
667 if(pMember->IsObject()){
668 CMethod *method = pMember->GetClass().GetDestructorMethod();
669 if( method ){
670 for(i4=MemberObjectNum-1;i4>=0;i4--){
671 //Thisポインタをrcxにコピー
672 SetThisPtrToReg(REG_RCX);
673
674 //add rcx,offset
675 op_add_RV(REG_RCX,offset+i4*MemberTypeSize);
676
677 //call destructor
678 op_call(method->pUserProc);
679 }
680 }
681 }
682 }*/
683 }
684 }
685
686 //ラベル用のメモリを解放
687 for(i3=0;i3<MaxLabelNum;i3++){
688 if(pLabelNames[i3].pName) HeapDefaultFree(pLabelNames[i3].pName);
689 }
690 HeapDefaultFree(pLabelNames);
691
692 //Goto未知ラベルスケジュールを解放
693 for(i3=0;i3<GotoLabelScheduleNum;i3++){
694 if(pGotoLabelSchedule[i3].pName){
695 SetError(6,pGotoLabelSchedule[i3].pName,pGotoLabelSchedule[i3].now_cp);
696 HeapDefaultFree(pGotoLabelSchedule[i3].pName);
697 }
698 else{
699 sprintf(temporary,"%d",pGotoLabelSchedule[i3].line);
700 SetError(6,temporary,pGotoLabelSchedule[i3].now_cp);
701 }
702 }
703 HeapDefaultFree(pGotoLabelSchedule);
704
705 //With情報のメモリを解放
706 for(i3=0;i3<WithInfo.num;i3++){
707 SetError(22,"With",WithInfo.pWithCp[i3]);
708 HeapDefaultFree(WithInfo.ppName[i3]);
709 }
710 HeapDefaultFree(WithInfo.ppName);
711 HeapDefaultFree(WithInfo.pWithCp);
712
713 //ローカルオブジェクト(レキシカルスコープレベル=0)の解放処理
714 obj_LexScopes.CallDestructorsOfScopeEnd();
715
716 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
717 for(i3=0;i3<ExitSubScheduleNum;i3++){
718 *((long *)(OpBuffer+pExitSubSchedule[i3]))=obp-(pExitSubSchedule[i3]+sizeof(long));
719 }
720 HeapDefaultFree(pExitSubSchedule);
721
722 if(bDebugCompile&&bDebugSupportProc==0){
723 //call _DebugSys_EndProc
724 extern UserProc *pSub_DebugSys_EndProc;
725 op_call(pSub_DebugSys_EndProc);
726 }
727
728 if( !pUserProc->ReturnType().IsNull() ){
729 //////////////////////////////////
730 // 戻り値をraxまたはxmm0に設定
731 //////////////////////////////////
732
733 RELATIVE_VAR RelativeVar;
734
735 const char *temp = pUserProc->GetName().c_str();
736 if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
737 temp="_System_ReturnValue";
738 }
739 GetVarOffsetReadWrite(temp,&RelativeVar,Type());
740
741 i3=pUserProc->ReturnType().GetBasicType();
742
743 if(i3==DEF_OBJECT || i3==DEF_STRUCT){
744 SetVarPtrToReg(REG_RAX,&RelativeVar);
745 if( i3==DEF_OBJECT ){
746 //mov rax,qword ptr[rax]
747 op_mov_RM( sizeof(_int64), REG_RAX, REG_RAX, 0, MOD_BASE );
748 }
749 }
750 else if(i3==DEF_DOUBLE){
751 //64ビット実数型
752 SetXmmReg_DoubleVariable(&RelativeVar,REG_XMM0);
753 }
754 else if(i3==DEF_SINGLE){
755 //32ビット実数型
756 SetXmmReg_SingleVariable(&RelativeVar,REG_XMM0);
757 }
758 else if(IsWholeNumberType(i3)){
759 //整数型
760 SetReg_WholeVariable(i3,&RelativeVar,REG_RAX);
761 }
762 else SetError(300,NULL,cp);
763 }
764
765 //ローカル変数領域のサイズをスタックフレームに通知
766 int iLocalParmSize;
767 iLocalParmSize=AllLocalVarSize-BaseLocalVar;
768 pobj_sf->SetLocalParmSize(iLocalParmSize);
769
770 //ローカル変数アドレススケジュール
771 for(i3=0;i3<LocalVarAddrScheduleNum;i3++){
772 *((long *)(OpBuffer+pLocalVarAddrSchedule[i3]))+=AllLocalVarSize+pobj_sf->GetFrameSize();
773 }
774 HeapDefaultFree(pLocalVarAddrSchedule);
775 foreach( Variable *pVar, pUserProc->localVars ){
776 //後にデバッグで利用する
777 pVar->offset = AllLocalVarSize + pobj_sf->GetFrameSize() - pVar->offset;
778 }
779
780 //mov reg,qword ptr[rsp+offset] ※スタックフレームを利用
781 pobj_sf->pop(REG_R15);
782 pobj_sf->pop(REG_R14);
783 pobj_sf->pop(REG_R13);
784 pobj_sf->pop(REG_R12);
785 pobj_sf->pop(REG_RDI);
786 pobj_sf->pop(REG_RSI);
787 pobj_sf->pop(REG_RBX);
788
789 int iStackFrameSize;
790 iStackFrameSize=iLocalParmSize + pobj_sf->GetFrameSize();
791
792 //add rsp,スタックフレームサイズ
793 op_add_rsp(iStackFrameSize);
794
795 //ret 0
796 OpBuffer[obp++]=(char)0xC3;
797
798
799 //デバッグ用
800 if(RspOffsetSchedule){
801 *((long *)(OpBuffer+RspOffsetSchedule))=iStackFrameSize;
802 *((long *)(OpBuffer+RspOffsetSchedule2))=iStackFrameSize+sizeof(_int64);
803 }
804
805
806 //スタックフレームスケジュール(subコマンド)
807 *((long *)(OpBuffer+StackFrameSchedule))=iStackFrameSize;
808
809 //スタックフレームスケジュールを実行
810 pobj_sf->RunningSchedule();
811 delete pobj_sf;
812 pobj_sf=0;
813
814
815 pUserProc->endOpAddress=obp;
816
817
818 //重複エラー情報管理のメモリを解放
819 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
820 HeapDefaultFree(SynonymErrorWords);
821}
822
823void CompileBufferInProcedure( UserProc &userProc ){
824 if( userProc.IsUsing() == false || userProc.IsCompiled() ) return;
825
826 _compile_proc( &userProc );
827
828 // ログを履く
829 char temporary[8192];
830 temporary[0]=0;
831 lstrcat( temporary, "------------------------------------------------------------------\n" );
832 sprintf( temporary + lstrlen(temporary), "【 %s のコード情報】\n", userProc.GetFullName().c_str() );
833 sprintf( temporary + lstrlen(temporary), "code size: %d bytes\n", userProc.GetCodeSize() );
834 lstrcat( temporary, "------------------------------------------------------------------\n" );
835 lstrcat( temporary, "\n" );
836 Smoothie::Logger::Put( temporary );
837}
838void CompileLocal(){
839 extern UserProc **ppSubHash;
840 int i2;
841 UserProc *pUserProc;
842
843 extern BOOL bDll;
844 if(bDll){
845 //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
846 pUserProc=GetSubHash("_System_InitDllGlobalVariables");
847 if(pUserProc){
848 CompileBufferInProcedure( *pUserProc );
849 }
850 else SetError(300,NULL,cp);
851 }
852
853 //_System_TypeBase_InitializeUserTypesは一番最後にコンパイル
854 extern UserProc *pSubStaticMethod_System_TypeBase_InitializeUserTypes;
855 pSubStaticMethod_System_TypeBase_InitializeUserTypes->CompleteCompile();
856
857 //_System_InitStaticLocalVariablesは一番最後にコンパイル
858 //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
859 extern UserProc *pSub_System_InitStaticLocalVariables;
860 pSub_System_InitStaticLocalVariables->CompleteCompile();
861
862 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
863 extern UserProc *pSub_System_Call_Destructor_of_GlobalObject;
864 pSub_System_Call_Destructor_of_GlobalObject->CompleteCompile();
865
866repeat:
867 for(i2=0;i2<MAX_HASH;i2++){
868 pUserProc=ppSubHash[i2];
869 while(pUserProc){
870 CompileBufferInProcedure( *pUserProc );
871 pUserProc=pUserProc->pNextData;
872 }
873 }
874
875 if( IsNeedProcCompile() ){
876 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
877 goto repeat;
878 }
879
880 //_System_TypeBase_InitializeUserTypesは最後のほうでコンパイル
881 pSubStaticMethod_System_TypeBase_InitializeUserTypes->KillCompileStatus();
882 CompileBufferInProcedure( *pSubStaticMethod_System_TypeBase_InitializeUserTypes );
883
884 if( IsNeedProcCompile() ){
885 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
886 for(i2=0;i2<MAX_HASH;i2++){
887 pUserProc=ppSubHash[i2];
888 while(pUserProc){
889 CompileBufferInProcedure( *pUserProc );
890 pUserProc=pUserProc->pNextData;
891 }
892 }
893 }
894
895 //_System_InitStaticLocalVariablesは一番最後にコンパイル
896 pSub_System_InitStaticLocalVariables->KillCompileStatus();
897 CompileBufferInProcedure( *pSub_System_InitStaticLocalVariables );
898
899 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
900 pSub_System_Call_Destructor_of_GlobalObject->KillCompileStatus();
901 CompileBufferInProcedure( *pSub_System_Call_Destructor_of_GlobalObject );
902}
Note: See TracBrowser for help on using the repository browser.