source: dev/trunk/abdev/BasicCompiler32/Compile_ProcOp.cpp@ 198

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