source: dev/trunk/ab5.0/abdev/BasicCompiler32/Compile_ProcOp.cpp@ 459

Last change on this file since 459 was 459, checked in by dai_9181, 16 years ago

・Compiler::IsDebugメソッドを導入した(bDebugCompileグローバル変数は廃止)。
・bStrictグローバル変数は意味を成さないので廃止した。

File size: 25.3 KB
RevLine 
[206]1#include "stdafx.h"
2
[183]3#include <jenga/include/smoothie/Smoothie.h>
4
5#include <Program.h>
[193]6#include <Compiler.h>
[248]7#include <LexicalScope.h>
[206]8#include <Class.h>
9#include <Variable.h>
[195]10#include <NamespaceSupporter.h>
[183]11
[3]12#include "../BasicCompiler_Common/common.h"
13#include "Opcode.h"
14
15
[86]16void SystemProc( const UserProc &userProc ){
17 if( userProc.GetName() == "_System_GetEip" ){
[3]18 //mov eax,dword ptr[esp]
[235]19 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_ESP, 0, MOD_BASE );
[3]20
21 //ret
[225]22 compiler.codeGenerator.op_ret();
[3]23 }
[86]24 else if( userProc.GetName() == "_System_InitDllGlobalVariables" ){
[3]25 ////////////////////////////////////////
26 // DLLのグローバル領域をコンパイル
27 ////////////////////////////////////////
28
[266]29 if( !compiler.IsDll() ){
[3]30 //ret
[225]31 compiler.codeGenerator.op_ret();
[3]32
33 return;
34 }
35
[206]36 const UserProc *pBackUserProc;
[76]37 pBackUserProc = &UserProc::CompilingUserProc();
38 UserProc::CompileStartForGlobalArea();
[3]39
40 int BackCp;
41 BackCp=cp;
42 cp=-1;
43
[459]44 if( compiler.IsDebug() )
45 {
[3]46 //デバッグ用の変数を定義
47 DebugVariable();
48 }
49
50 //GC用の変数を定義
51 InitGCVariables();
52
[129]53 //_System_StartupProgramの呼び出し
[206]54 extern const UserProc *pSub_System_StartupProgram;
[225]55 compiler.codeGenerator.op_call(pSub_System_StartupProgram);
[129]56
[42]57 //クラスに属する静的メンバを定義
[265]58 compiler.GetObjectModule().meta.GetClasses().InitStaticMember();
[42]59
[3]60 GetGlobalDataForDll();
61
[76]62 UserProc::CompileStartForUserProc( pBackUserProc );
[3]63 cp=BackCp;
64
65 //ret
[225]66 compiler.codeGenerator.op_ret();
[3]67 }
[86]68 else if( userProc.GetName() == "_System_InitStaticLocalVariables" ){
[3]69 //静的ローカルオブジェクトのコンストラクタ呼び出し
70
[265]71 BOOST_FOREACH( Variable *pVar, compiler.GetObjectModule().meta.GetGlobalVars() ){
[76]72 if(memicmp(pVar->GetName().c_str(),"Static%",7)==0){
[3]73 //コンストラクタ呼び出し
[206]74 if( pVar->GetType().IsObject() ){
[3]75
76 //エラー用
[76]77 cp=pVar->source_code_address;
[3]78
[50]79 CallConstructor(
[76]80 pVar->GetName().c_str(),
[206]81 pVar->GetSubscripts(),
82 pVar->GetType(),
83 pVar->GetParamStrForConstructor().c_str());
[3]84 }
85 }
86 }
87
88 //ret
[225]89 compiler.codeGenerator.op_ret();
[3]90 }
[86]91 else if( userProc.GetName() == "_System_Call_Destructor_of_GlobalObject" ){
[3]92
[206]93 const UserProc *pBackUserProc;
[76]94 pBackUserProc = &UserProc::CompilingUserProc();
95 UserProc::CompileStartForGlobalArea();
[3]96
[248]97 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
[3]98
[76]99 UserProc::CompileStartForUserProc( pBackUserProc );
[3]100
101
102 //ret
[225]103 compiler.codeGenerator.op_ret();
[3]104 }
[86]105 else if( userProc.GetName() == "_allrem" ){
[3]106 //乗除演算用の特殊関数(64ビット整数対応)
107 BYTE Buffer_allrem[]={
108 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
109 };
110
[276]111 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allrem, 178 ) );
[3]112 }
[86]113 else if( userProc.GetName() == "_aullrem" ){
[3]114 //乗除演算用の特殊関数(64ビット整数対応)
115 BYTE Buffer_aullrem[]={
116 0x53,0x8B,0x44,0x24,0x14,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x10,0x8B,
117 0x44,0x24,0x0C,0x33,0xD2,0xF7,0xF1,0x8B,0x44,0x24,0x08,0xF7,0xF1,0x8B,
118 0xC2,0x33,0xD2,0xEB,0x50,0x8B,0xC8,0x8B,0x5C,0x24,0x10,0x8B,0x54,0x24,
119 0x0C,0x8B,0x44,0x24,0x08,0xD1,0xE9,0xD1,0xDB,0xD1,0xEA,0xD1,0xD8,0x0B,
120 0xC9,0x75,0xF4,0xF7,0xF3,0x8B,0xC8,0xF7,0x64,0x24,0x14,0x91,0xF7,0x64,
121 0x24,0x10,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x0C,0x77,0x08,0x72,0x0E,
122 0x3B,0x44,0x24,0x08,0x76,0x08,0x2B,0x44,0x24,0x10,0x1B,0x54,0x24,0x14,
123 0x2B,0x44,0x24,0x08,0x1B,0x54,0x24,0x0C,0xF7,0xDA,0xF7,0xD8,0x83,0xDA,
124 0x00,0x5B,0xC2,0x10,0x00
125 };
126
[276]127 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aullrem, 117 ) );
[3]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
[276]135 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allmul, 52 ) );
[3]136 }
[86]137 else if( userProc.GetName() == "_alldiv" ){
[3]138 //除算用の特殊関数(64ビット整数対応)
139 BYTE Buffer_alldiv[]={
140 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
141 };
142
[276]143 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_alldiv, 170 ) );
[3]144 }
[86]145 else if( userProc.GetName() == "_aulldiv" ){
[3]146 //整数除算用の特殊関数(64ビット整数対応)
147 BYTE Buffer_aulldiv[]={
148 0x53,0x56,0x8B,0x44,0x24,0x18,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x14,
149 0x8B,0x44,0x24,0x10,0x33,0xD2,0xF7,0xF1,0x8B,0xD8,0x8B,0x44,0x24,0x0C,
150 0xF7,0xF1,0x8B,0xD3,0xEB,0x41,0x8B,0xC8,0x8B,0x5C,0x24,0x14,0x8B,0x54,
151 0x24,0x10,0x8B,0x44,0x24,0x0C,0xD1,0xE9,0xD1,0xDB,0xD1,0xEA,0xD1,0xD8,
152 0x0B,0xC9,0x75,0xF4,0xF7,0xF3,0x8B,0xF0,0xF7,0x64,0x24,0x18,0x8B,0xC8,
153 0x8B,0x44,0x24,0x14,0xF7,0xE6,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x10,
154 0x77,0x08,0x72,0x07,0x3B,0x44,0x24,0x0C,0x76,0x01,0x4E,0x33,0xD2,0x8B,
155 0xC6,0x5E,0x5B,0xC2,0x10,0x00
156 };
157
[276]158 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aulldiv, 104 ) );
[3]159 }
[86]160 else if( userProc.GetName() == "_allshl" ){
[3]161 //符号あり左ビットシフト用の特殊関数(64ビット整数対応)
162 BYTE Buffer_allshl[]={
163 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
164 };
165
[276]166 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allshl, 31 ) );
[3]167 }
[86]168 else if( userProc.GetName() == "_allshr" ){
[3]169 //符号あり右ビットシフト用の特殊関数(64ビット整数対応)
170 BYTE Buffer_allshr[]={
171 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
172 };
173
[276]174 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_allshr, 33 ) );
[3]175 }
[86]176 else if( userProc.GetName() == "_aullshr" ){
[3]177 //符号なし右ビットシフト用の特殊関数(64ビット整数対応)
178 BYTE Buffer_aullshr[]={
179 0x80,0xF9,0x40, //cmp cl,40h
180 0x73,0x15, //jae RETZERO (0040d71a)
181 0x80,0xF9,0x20, //cmp cl,20h
182 0x73,0x06, //jae MORE32 (0040d710)
183 0x0F,0xAD,0xD0, //shrd eax,edx,cl
184 0xD3,0xEA, //shr edx,cl
185 0xC3, //ret
186 //MORE32:
187 0x8B,0xC2, //mov eax,edx
188 0x33,0xD2, //xor edx,edx
189 0x80,0xE1,0x1F, //and cl,1Fh
190 0xD3,0xE8, //shr eax,cl
191 0xC3, //ret
192 //RETZERO:
193 0x33,0xC0, //xor eax,eax
194 0x33,0xD2, //xor edx,edx
195 0xC3 //ret
196 };
197
[276]198 compiler.codeGenerator.PutOld( NativeCode( (const char *)Buffer_aullshr, 31 ) );
[3]199 }
[90]200 else{
201 SetError();
202 }
203}
[206]204void AutoGeneration( const UserProc &userProc){
[90]205 if( userProc.GetName() == "InitializeUserTypes"
[89]206 && userProc.HasParentClass()
[131]207 && userProc.GetParentClass().GetName() == "_System_TypeBase" ){
[89]208
[265]209 compiler.GetObjectModule().meta.GetClasses().Compile_System_InitializeUserTypes();
[86]210 }
[355]211 else if( userProc.GetName() == "InitializeUserTypesForBaseType"
212 && userProc.HasParentClass()
213 && userProc.GetParentClass().GetName() == "_System_TypeBase" )
214 {
215 compiler.GetObjectModule().meta.GetClasses().Compile_System_InitializeUserTypesForBaseType();
216 }
[95]217 else if( userProc.GetName() == "RegisterGlobalRoots"
218 && userProc.HasParentClass()
[131]219 && userProc.GetParentClass().GetName() == "_System_CGarbageCollection" ){
[95]220
221 Compile_AddGlobalRootsForGc();
222 }
[308]223 else if( userProc.GetName() == compiler.globalAreaProcName ){
224 ////////////////////////////////////////
225 // グローバル領域をコンパイル
226 ////////////////////////////////////////
227
[365]228 UserProc::pGlobalProc = &userProc;
229
[308]230 const UserProc *pBackUserProc = &UserProc::CompilingUserProc();
231 UserProc::CompileStartForGlobalArea();
232
233 int BackCp = cp;
234 cp=-1;
235
236 //クラスに属する静的メンバを定義
237 compiler.GetObjectModule().meta.GetClasses().InitStaticMember();
238
239 //グローバル実行領域をコンパイル開始
240 CompileBuffer(0,0);
241
242 //Goto未知ラベルスケジュールが存在したらエラーにする
243 BOOST_FOREACH( const GotoLabelSchedule *pGotoLabelSchedule, compiler.codeGenerator.gotoLabelSchedules )
244 {
245 if(pGotoLabelSchedule->GetName().size()>0){
246 SetError(6,pGotoLabelSchedule->GetName(),pGotoLabelSchedule->GetSourceCodePos());
247 }
248 else{
249 char temporary[255];
250 sprintf(temporary,"%d",pGotoLabelSchedule->GetLineNum());
251 SetError(6,temporary,pGotoLabelSchedule->GetSourceCodePos());
252 }
253 }
254
255 UserProc::CompileStartForUserProc( pBackUserProc );
256 cp=BackCp;
257 }
[350]258 else if( userProc.HasParentClass()
259 && userProc.IsCastOperator()
260 && userProc.ReturnType().IsInterface() )
261 {
262 // インターフェイス型にキャストするためのメソッド
263
264 int vtblMasterListIndex = userProc.GetParentClass().GetVtblMasterListIndex( &userProc.ReturnType().GetClass() );
265
266 char temporary[1024];
267 sprintf( temporary,
[370]268 "Return New %s(ObjPtr( This ),Get_LONG_PTR( (Get_LONG_PTR( ObjPtr(This)+SizeOf(VoidPtr) ) + SizeOf(LONG_PTR)*%d) As VoidPtr ) As VoidPtr )",
[350]269 userProc.ReturnType().GetClass().GetName().c_str(),
270 vtblMasterListIndex
271 );
272 MakeMiddleCode( temporary );
273
274 ChangeOpcode( temporary );
275 }
[78]276 else{
277 SetError();
278 }
[3]279}
280
[316]281void _compile_proc(const UserProc *pUserProc)
282{
[3]283 extern char *basbuf;
284 extern HANDLE hHeap;
[253]285 int i3,i4,BaseOffset;
[3]286 char temporary[VN_SIZE];
287
[206]288 if( pUserProc->GetLocalVars().size() ){
[76]289 SetError();
290 return;
291 }
292
[206]293 trace_for_sourcecodestep( "★★★ " << pUserProc->GetFullName() << "のコンパイルを開始" );
294
[75]295 pUserProc->CompleteCompile();
[3]296
297 extern BOOL bSystemProc;
[75]298 if(memcmp(pUserProc->GetName().c_str(),"_System_",8)==0) bSystemProc=1;
[3]299 else bSystemProc=0;
300
301 extern BOOL bDebugSupportProc;
[459]302 if(memcmp(pUserProc->GetName().c_str(),"_DebugSys_",10)==0)
303 {
304 if( !compiler.IsDebug() )
305 {
[3]306 return;
307 }
308 bDebugSupportProc=1;
309 }
310 else bDebugSupportProc=0;
311
[278]312 if( pUserProc->GetCodeSize() != 0 || pUserProc->GetNativeCode().GetSize() != 0 )
[276]313 {
314 // 既にコード生成が行われている場合はエラー
315 SetError();
316 }
[3]317
[89]318 //コンパイル中の関数が属するクラス
[290]319 compiler.pCompilingClass = pUserProc->GetParentClassPtr();
[89]320
321 //コンパイルスタートをクラス管理クラスに追加
[265]322 compiler.GetObjectModule().meta.GetClasses().StartCompile( pUserProc );
[89]323
324 //コンパイル中の関数
325 UserProc::CompileStartForUserProc( pUserProc );
326
[101]327 // コンパイル中の関数が属する名前空間
[199]328 compiler.GetNamespaceSupporter().SetLivingNamespaceScopes( pUserProc->GetNamespaceScopes() );
[101]329
[108]330 // コンパイル中の関数でImportsされている名前空間
[199]331 compiler.GetNamespaceSupporter().SetImportedNamespaces( pUserProc->GetImportedNamespaces() );
[108]332
[225]333 // コード生成対象を選択
334 compiler.codeGenerator.Select( (const_cast<UserProc *>(pUserProc))->GetNativeCode() );
335
[75]336 if(pUserProc->IsSystem()){
[89]337 ////////////////////
338 // 特殊関数
339 ////////////////////
340
[3]341 extern int AllLocalVarSize;
342 AllLocalVarSize=0;
343
[86]344 SystemProc(*pUserProc);
[3]345 return;
346 }
347
[285]348 if( !pUserProc->IsAutoGeneration() )
349 {
350 cp=pUserProc->GetCodePos();
351 for(;;cp++){
352 if(IsCommandDelimitation(basbuf[cp])) break;
353 }
354 cp--;
[3]355 }
356
357 //ローカル変数に関する情報
358 extern int AllLocalVarSize;
359 AllLocalVarSize=0;
360
361 //パラメータ用の変数データを考慮
[75]362 for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
363 Parameter &param = *pUserProc->RealParams()[i3];
[73]364
[275]365 Variable *pVar = new Variable( param.GetVarName(), param, false, param.IsRef(), "", false );
[3]366
[76]367 if( param.IsArray() ){
[206]368 pVar->SetArray( param.GetSubscripts() );
[3]369 }
370
[76]371 int varSize;
[73]372 if( param.IsRef() == false && param.IsStruct() ){
[64]373 //構造体のByValパラメータ
[76]374 pVar->ThisIsParameter();
375 varSize=PTR_SIZE;
[3]376 }
377 else{
[76]378 if( param.IsArray() == false ){
379 varSize = pVar->GetMemorySize();
[3]380 }
381 else{
[76]382 varSize=PTR_SIZE;
[3]383 }
384 }
[76]385 AllLocalVarSize+=varSize;
[206]386 pVar->SetOffsetAddress( AllLocalVarSize );
[3]387
388 //レキシカルスコープ情報
[248]389 pVar->SetScopeLevel( compiler.codeGenerator.lexicalScopes.GetNowLevel() );
390 pVar->SetScopeStartAddress( compiler.codeGenerator.lexicalScopes.GetStartAddress() );
[392]391 pVar->isLiving = true;
[3]392
[206]393 pUserProc->GetLocalVars().push_back( pVar );
[3]394 }
395
396 //Thisポインタを示すローカルオフセット値をセット
397 extern int LocalVar_ThisPtrOffset;
398 LocalVar_ThisPtrOffset=AllLocalVarSize;
399
400 BaseOffset=AllLocalVarSize;
401
402 //ret用のアドレスを考慮
403 AllLocalVarSize+=sizeof(long);
404
405
406 ///////////////////////
407 // ここからコード生成
408
409 //sub esp,AllLocalVarSize(スケジュール)
[253]410 const PertialSchedule *pAllLocalVarPertialSchedule = compiler.codeGenerator.op_sub_esp( 0, true );
[3]411
412 //push ebp
[225]413 compiler.codeGenerator.op_push(REG_EBP);
[3]414
415 //mov ebp,esp
[235]416 compiler.codeGenerator.op_mov_RR( REG_EBP, REG_ESP );
[3]417
418 //push ebx
[225]419 compiler.codeGenerator.op_push(REG_EBX);
[3]420
421 //push esi
[235]422 compiler.codeGenerator.op_push( REG_ESI );
[3]423
424 //push edi
[235]425 compiler.codeGenerator.op_push( REG_EDI );
[3]426
[75]427 if( !pUserProc->ReturnType().IsNull() ){
[3]428 //戻り値が存在するとき
429
[75]430 const char *temp = pUserProc->GetName().c_str();
431 if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
432 temp = "_System_ReturnValue";
433 }
[3]434
[75]435 if( pUserProc->ReturnType().IsStruct() ){
[64]436 //戻り値用の構造体(値型)はパラメータで引き渡される
[3]437 }
438 else{
[89]439 if( pUserProc->ReturnType().IsObject() ){
[299]440 sprintf(temporary,"%s=Nothing%c%c%s",temp,1,ESC_AS, compiler.TypeToString( pUserProc->ReturnType() ).c_str() );
[89]441 }
442 else{
443 //戻り値用の変数の定義
[299]444 sprintf(temporary,"%s%c%c%s",temp,1,ESC_AS, compiler.TypeToString( pUserProc->ReturnType() ).c_str() );
[89]445 }
[40]446
[3]447 OpcodeDim(temporary,0);
448 }
449 }
450
451 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
[247]452 compiler.codeGenerator.exitSubCodePositions.clear();
[3]453
[260]454 //ラベル管理オブジェクトを初期化
[261]455 compiler.codeGenerator.gotoLabels.clear();
[3]456
457 //Gotoラベルスケジュール
[246]458 compiler.codeGenerator.gotoLabelSchedules.clear();
[3]459
460 //With情報のメモリを確保
461 extern WITHINFO WithInfo;
462 WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
463 WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
464 WithInfo.num=0;
465
466 //重複エラー情報管理のメモリを確保
467 extern char **SynonymErrorWords;
468 extern int SynonymErrorNum;
469 SynonymErrorNum=0;
470 SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
471
472 //Continueアドレスを初期化
[241]473 compiler.codeGenerator.ClearContinueArea();
[3]474
[308]475 //レキシカルスコープ情報を初期化
476 compiler.codeGenerator.lexicalScopes.Init( compiler.codeGenerator.GetNativeCodeSize() );
477
[251]478 const PertialSchedule *pEspOffsetPertialSchedule = NULL;
[459]479 if( compiler.IsDebug() && bDebugSupportProc == 0 )
480 {
[3]481 //push dword ptr[ebp+(AllLocalVarSize-BaseOffset)](スケジュール)
[251]482 pEspOffsetPertialSchedule = compiler.codeGenerator.op_push_M( REG_EBP, 0, Schedule::None, true );
[3]483
484 //push dword ptr[ebp](以前のebp)
[235]485 compiler.codeGenerator.op_push_M( REG_EBP );
[3]486
487 //call _DebugSys_StartProc
[206]488 extern const UserProc *pSub_DebugSys_StartProc;
[225]489 compiler.codeGenerator.op_call(pSub_DebugSys_StartProc);
[3]490 }
491
[206]492 if(compiler.pCompilingClass){
493 if( pUserProc->GetName() == compiler.pCompilingClass->GetName() ){
[3]494 ////////////////////////////////////
495 // コンストラクタをコンパイルするとき
496 ////////////////////////////////////
497
[17]498 //コンストラクタのコンパイル開始を通知
[206]499 compiler.pCompilingClass->NotifyStartConstructorCompile();
[17]500
[27]501 //基底クラスかどうかの識別
502 //(継承元がインターフェイスの場合も基底クラスと見なす)
[3]503 BOOL bThisIsSuperClass;
[206]504 if( !compiler.pCompilingClass->HasSuperClass() ) bThisIsSuperClass=1;
505 else if( compiler.pCompilingClass->GetSuperClass().GetConstructorMethod() == NULL ){
[3]506 //インターフェイスを継承したときはコンストラクタを持たない
507 bThisIsSuperClass=1;
508 }
509 else bThisIsSuperClass=0;
510
511 if(!bThisIsSuperClass){
512 /* サブクラスコンストラクタをコンパイルしているときは、
[27]513 基底クラスのコンストラクタを呼び出す */
[3]514
515 i3=cp+1;
516 while(IsCommandDelimitation(basbuf[i3])) i3++;
517 for(i4=0;;i3++,i4++){
518 if(!IsVariableChar(basbuf[i3])){
519 temporary[i4]=0;
520 break;
521 }
522 temporary[i4]=basbuf[i3];
523 }
[206]524 if( compiler.pCompilingClass->GetSuperClass().GetName() == temporary ){
[27]525 //基底クラスのコンストラクタを呼び出す
[3]526 cp=i3;
527 for(i4=0;;cp++,i4++){
528 if(IsCommandDelimitation(basbuf[cp])){
529 temporary[i4]=0;
530 break;
531 }
532 temporary[i4]=basbuf[cp];
533 }
534 if(!(temporary[0]=='('&&temporary[lstrlen(temporary)-1]==')')){
535 SetError(1,NULL,cp);
536 }
537 RemoveStringPare(temporary);
538
[89]539 Type dummyType;
540 CallProc( PROC_DEFAULT
[206]541 , &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc()
542 , compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc().GetName().c_str()
[89]543 , temporary
[331]544 , Type() // baseTypeはなし
545 , dummyType
546 );
[3]547 }
548 else{
[27]549 //基底クラスのコンストラクタを暗黙的に呼び出す
[3]550 Opcode_CallProc("",
[206]551 &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc(),
[3]552 0,
[290]553 ""
554 );
[3]555 }
556 }
557 }
[75]558 else if( pUserProc->IsDestructor() ){
[18]559 //デストラクタをコンパイルしたとき
560
561 //デストラクタのコンパイル開始を通知
[206]562 compiler.pCompilingClass->NotifyStartDestructorCompile();
[18]563 }
[3]564 }
565
566 //////////////////////////////////////////
567 //////////////////////////////////////////
568 ////// プロシージャ内をコンパイル ////////
[90]569 if( pUserProc->IsAutoGeneration() ){
570 AutoGeneration( *pUserProc );
571 }
[75]572 else{
[90]573 if(pUserProc->IsMacro()){
574 CompileBuffer(ESC_ENDMACRO,0);
575 }
576 else{
577 if(pUserProc->IsSub()){
578 CompileBuffer(ESC_ENDSUB,0);
579 }
580 else if(pUserProc->IsFunction()){
581 CompileBuffer(ESC_ENDFUNCTION,0);
582 }
583 }
[75]584 }
[3]585 //////////////////////////////////////////
586 //////////////////////////////////////////
587
[206]588 if( compiler.pCompilingClass ){
[17]589
[206]590 if( compiler.pCompilingClass->IsCompilingConstructor() ){
[17]591 // コンストラクタをコンパイルしていたとき
[18]592
[17]593 // コンストラクタのコンパイルが完了したことを通知
[206]594 compiler.pCompilingClass->NotifyFinishConstructorCompile();
[17]595 }
[75]596 else if( pUserProc->IsDestructor() ){
[3]597 ////////////////////////////////////
598 //デストラクタをコンパイルしたとき
599 ////////////////////////////////////
600
[18]601 // デストラクタのコンパイルが完了したことを通知
[206]602 compiler.pCompilingClass->NotifyFinishDestructorCompile();
[18]603
[206]604 if( compiler.pCompilingClass->HasSuperClass() ){
[3]605 /* サブクラスのデストラクタをコンパイルしているときは、
[27]606 基底クラスのデストラクタを呼び出す */
[3]607
[206]608 const CMethod *method = compiler.pCompilingClass->GetSuperClass().GetDestructorMethod();
[51]609 if( method ){
[3]610 Opcode_CallProc("",
[206]611 &method->GetUserProc(),
[3]612 0,
[290]613 ""
614 );
[3]615 }
616 }
617 }
618 }
619
620 //With情報のメモリを解放
621 for(i3=0;i3<WithInfo.num;i3++){
622 SetError(22,"With",WithInfo.pWithCp[i3]);
623 HeapDefaultFree(WithInfo.ppName[i3]);
624 }
625 HeapDefaultFree(WithInfo.ppName);
626 HeapDefaultFree(WithInfo.pWithCp);
627
628 //push ebp
629 AllLocalVarSize+=sizeof(long);
630
631 //ローカルオブジェクトの解放処理
[248]632 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
[3]633
[34]634 //プロシージャ抜け出しスケジュール(Exit Sub/Function)
[247]635 compiler.codeGenerator.ResolveExitSubSchedule();
[34]636
[459]637 if( compiler.IsDebug() && bDebugSupportProc == 0 )
638 {
[251]639 compiler.codeGenerator.opfix( pEspOffsetPertialSchedule, AllLocalVarSize-BaseOffset-sizeof(long) );
[34]640
641 //call _DebugSys_EndProc
[206]642 extern const UserProc *pSub_DebugSys_EndProc;
[225]643 compiler.codeGenerator.op_call(pSub_DebugSys_EndProc);
[34]644 }
645
[75]646 if( !pUserProc->ReturnType().IsNull() ){
[3]647 //戻り値をeax、edxに設定
648 RELATIVE_VAR RelativeVar;
649
[75]650 const char *temp = pUserProc->GetName().c_str();
651 if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
[3]652 temp="_System_ReturnValue";
[75]653 }
[76]654 GetVarOffsetReadWrite(temp,&RelativeVar,Type());
[3]655
[290]656 const Type &returnType = pUserProc->ReturnType();
657 if( returnType.IsObject() || returnType.IsStruct() )
658 {
[3]659 SetVarPtrToEax(&RelativeVar);
[290]660 if( returnType.IsObject() )
661 {
[64]662 //mov eax,dword ptr[eax]
[225]663 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
[64]664 }
[3]665 }
[290]666 else if( returnType.IsReal() )
[231]667 {
[3]668 //fld qword ptr[ebp+offset]
[253]669 compiler.codeGenerator.localVarPertialSchedules.push_back(
[290]670 compiler.codeGenerator.op_fld_base_offset( returnType.GetBasicType(), REG_EBP, RelativeVar.offset, Schedule::None, true )
[253]671 );
[3]672 }
[290]673 else if( returnType.Is64() )
674 {
[3]675 //mov eax,dword ptr[ebp+offset]
[253]676 compiler.codeGenerator.localVarPertialSchedules.push_back(
677 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
678 );
[3]679
680 //mov edx,dword ptr[ebp+offset+sizeof(long)]
[253]681 compiler.codeGenerator.localVarPertialSchedules.push_back(
682 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_EBP, RelativeVar.offset+sizeof(long), MOD_BASE_DISP32, Schedule::None, true )
683 );
[3]684 }
[290]685 else if( returnType.GetSize() == sizeof(long) )
[253]686 {
[3]687 //mov eax,dword ptr[ebp+offset]
[253]688 compiler.codeGenerator.localVarPertialSchedules.push_back(
689 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
690 );
[3]691 }
[290]692 else if( returnType.GetSize() == sizeof(short) )
693 {
[3]694 //xor eax,eax(eaxを0に初期化する)
[225]695 compiler.codeGenerator.op_zero_reg(REG_EAX);
[3]696
697 //mov ax,word ptr[ebp+offset]
[253]698 compiler.codeGenerator.localVarPertialSchedules.push_back(
699 compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
700 );
[3]701 }
[290]702 else if( returnType.GetSize() == sizeof(char) )
703 {
[3]704 //xor eax,eax(eaxを0に初期化する)
[225]705 compiler.codeGenerator.op_zero_reg(REG_EAX);
[3]706
707 //mov al,byte ptr[ebp+offset]
[253]708 compiler.codeGenerator.localVarPertialSchedules.push_back(
709 compiler.codeGenerator.op_mov_RM( sizeof(char), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::None, true )
710 );
[3]711 }
[290]712 else
713 {
714 SetError();
715 }
[3]716 }
717
718 //ローカル変数アドレススケジュール
[253]719 BOOST_FOREACH( const PertialSchedule *pPertialSchedule, compiler.codeGenerator.localVarPertialSchedules )
720 {
721 compiler.codeGenerator.opfix_offset( pPertialSchedule, AllLocalVarSize );
[3]722 }
[253]723 compiler.codeGenerator.localVarPertialSchedules.clear();
[206]724 BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
[76]725 //後にデバッグで利用する
[206]726 pVar->SetOffsetAddress( AllLocalVarSize - pVar->GetOffsetAddress() );
[3]727 }
728
729 //push ebp、ret用のアドレスを考慮
730 AllLocalVarSize-=sizeof(long)*2;
731
732 //ローカル変数用メモリを確保するためのスケジュール(subコマンド)
[253]733 compiler.codeGenerator.opfix( pAllLocalVarPertialSchedule, AllLocalVarSize - BaseOffset );
[3]734
735 //pop edi
[235]736 compiler.codeGenerator.op_pop( REG_EDI );
[3]737
738 //pop esi
[235]739 compiler.codeGenerator.op_pop( REG_ESI );
[3]740
741 //pop ebx
[225]742 compiler.codeGenerator.op_pop(REG_EBX);
[3]743
[459]744 if( compiler.IsDebug() )
745 {
[64]746 //cmp esp,ebp
[225]747 compiler.codeGenerator.op_cmp_RR( REG_ESP, REG_EBP );
[64]748
[240]749 //je 6(次のcallとbreakpointを飛び越す)
750 compiler.codeGenerator.op_je( 6 );
[64]751
752 //call _esp_error
[206]753 extern const UserProc *pSub_esp_error;
[225]754 compiler.codeGenerator.op_call( pSub_esp_error );
[64]755
756 breakpoint;
757 }
758
[3]759 //mov esp,ebp
[235]760 compiler.codeGenerator.op_mov_RR( REG_ESP, REG_EBP );
[3]761
762 //pop ebp
[225]763 compiler.codeGenerator.op_pop(REG_EBP);
[3]764
765 //add esp AllLocalVarSize
[225]766 compiler.codeGenerator.op_add_esp(AllLocalVarSize-BaseOffset);
[3]767
[75]768 if( BaseOffset==0 || pUserProc->IsCdecl() ){
[142]769 //ret
[225]770 compiler.codeGenerator.op_ret();
[3]771 }
772 else{
773 //ret BaseOffset(パラメータ分のスタック領域を解放)
[240]774 compiler.codeGenerator.op_ret( (_int16)BaseOffset );
[3]775 }
776
[76]777
[75]778 //重複エラー情報管理のメモリを解放
779 for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
780 HeapDefaultFree(SynonymErrorWords);
781 SynonymErrorWords=0;
782
783
[3]784 //ローカル変数のネーム情報は後に解放する
785}
Note: See TracBrowser for help on using the repository browser.