source: dev/BasicCompiler32/Compile_ProcOp.cpp@ 101

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

名前空間機能をグローバル関数に適用(作業完了)。

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