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

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