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