source: dev/trunk/abdev/BasicCompiler32/Compile_CallProc.cpp@ 307

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

構造体をクラスメソッドの戻り値にしたときにThisポインタが正常に引き渡されないバグを修正。

File size: 10.8 KB
RevLine 
[206]1#include "stdafx.h"
2
[183]3#include <jenga/include/smoothie/Smoothie.h>
4
[193]5#include <Compiler.h>
6
[3]7#include "../BasicCompiler_Common/common.h"
8#include "Opcode.h"
9
10void Call_DebugSys_SaveContext(){
11 //call _System_GetEip
[206]12 extern const UserProc *pSub_System_GetEip;
[225]13 compiler.codeGenerator.op_call(pSub_System_GetEip);
[3]14
15 //push eax
[225]16 compiler.codeGenerator.op_push(REG_EAX);
[3]17
18 //push ebp
[225]19 compiler.codeGenerator.op_push(REG_EBP);
[3]20
21 //call _DebugSys_SaveContext
[206]22 extern const UserProc *pSub_DebugSys_SaveContext;
[225]23 compiler.codeGenerator.op_call(pSub_DebugSys_SaveContext);
[3]24}
25
[76]26bool Opcode_CallProcPtr( const char *variable, const char *lpszParms,ProcPointer *pProcPointer){
[3]27
28 extern BOOL bDebugCompile;
29 extern BOOL bDebugSupportProc;
30 if(bDebugCompile&&bDebugSupportProc==0)
31 Call_DebugSys_SaveContext();
32
33
34 ////////////////////////
35 // パラメータのセット
36 ////////////////////////
37
38 //パラメータオブジェクトを生成
[71]39 ParamImpl *pobj_parameter=0;
[76]40 pobj_parameter=new ParamImpl(lpszParms);
[3]41
[77]42 // デフォルト引数を適用
43 pobj_parameter->ApplyDefaultParameters( pProcPointer->Params() );
44
[3]45 //エラーチェック
[75]46 if( !pobj_parameter->ErrorCheck(variable,pProcPointer->Params() ) ){
[31]47 //パラメータにエラーがあるときは処理を終える
[76]48 return false;
[31]49 }
[3]50
[20]51 //一時オブジェクトを生成
[75]52 pobj_parameter->NewTempParameters( variable,pProcPointer->Params() );
[20]53
[3]54 //レジスタ、スタックフレームにセット
[75]55 pobj_parameter->SetParameter(variable,pProcPointer->Params() );
[3]56
57
58
[20]59 ////////////////////////
60 // call
61 ////////////////////////
[3]62 RELATIVE_VAR RelativeVar;
[76]63 GetVarOffsetReadOnly(variable,&RelativeVar,Type());
[3]64 SetVarPtrToEax(&RelativeVar);
65
66 //mov eax,dword ptr[eax]
[235]67 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
[3]68
69 //call eax
[235]70 compiler.codeGenerator.op_call_R( REG_EAX );
[3]71
[20]72
73
74 //一時オブジェクトを破棄
75 pobj_parameter->DeleteTempParameters();
76
77 //パラメータオブジェクトを破棄
78 delete pobj_parameter;
79
[76]80 return true;
[3]81}
82
[290]83bool Opcode_CallProc(const char *Parameter,const UserProc *pUserProc,DWORD dwFlags,const char *ObjectName ){
[51]84 int i2;
[3]85
[75]86 if( pUserProc->IsMacro() ){
87 if( lstrcmpi( pUserProc->GetName().c_str(), "Print" ) == 0 ){
[3]88 Opcode_Print(Parameter,0);
[76]89 return true;
[3]90 }
[75]91 if( lstrcmpi( pUserProc->GetName().c_str(), "Input" ) == 0 ){
[3]92 Opcode_Input(Parameter);
[76]93 return true;
[3]94 }
[75]95 if( lstrcmpi( pUserProc->GetName().c_str(), "Write" ) == 0 ){
[3]96 Opcode_Print(Parameter,1);
[76]97 return true;
[3]98 }
99 }
100
[75]101 pUserProc->Using();
[3]102
[47]103 bool isStatic = false;
[76]104 const CClass *pobj_c = NULL;
[135]105 const CMethod *pMethod = NULL;
[292]106 Type leftType;
[304]107 bool isFixedClass = false;
[75]108 if( pUserProc->GetParentClassPtr() ){
[3]109 //クラスのメンバ関数を呼び出す場合はアクセスチェックを行う
[304]110 if(ObjectName[0] && (dwFlags&PROCFLAG_NEW)==0)
111 {
112 if(lstrcmpi(ObjectName,"Super")==0)
113 {
[27]114 //クラスメンバ関数内から基底クラスの呼び出し
[304]115 pobj_c=&compiler.pCompilingClass->GetSuperClass();
116
117 isFixedClass = true;
[3]118 }
[304]119 else
120 {
[47]121 //"->"によってオブジェクトを指定する通常のメンバ関数呼び出し
[76]122 Type varType;
123 GetVarType( ObjectName, varType, false );
[290]124 if( NATURAL_TYPE( varType.GetBasicType() ) == DEF_OBJECT )
125 {
126 pobj_c = &varType.GetClass();
[292]127 leftType = varType;
[290]128 }
129 else
130 {
[265]131 pobj_c=compiler.GetObjectModule().meta.GetClasses().Find(ObjectName);
[47]132 if( pobj_c ){
133 isStatic = true;
134 }
135 else{
136 SetError(300,NULL,cp);
137 }
[3]138 }
139 }
140 }
141 else{
142 if(dwFlags&PROCFLAG_NEW){
[294]143 GetVarType( ObjectName, leftType, false );
144
[3]145 //New演算子によるコンストラクタ呼び出し
[75]146 pobj_c=pUserProc->GetParentClassPtr();
[3]147 }
148 else{
149 //クラスメンバ関数内から同一クラスのメンバ関数の呼び出し
[206]150 pobj_c=compiler.pCompilingClass;
[3]151 }
152 }
153
154
[18]155 /////////////////////////////////
156 // メソッド情報を取得
157 /////////////////////////////////
[27]158 pMethod = NULL;
[135]159 if( ! isStatic ) pMethod = pobj_c->GetMethods().GetMethodPtr( pUserProc );
[27]160 if( ! pMethod ){
[18]161 //動的メソッドが取得できなかったときは静的メソッドを当たる
[135]162 pMethod = pobj_c->GetStaticMethods().GetMethodPtr( pUserProc );
[18]163 if( !pMethod ){
164 SetError(300,NULL,cp);
[76]165 return false;
[3]166 }
[26]167
168 //静的メンバ
[47]169 isStatic = true;
[3]170 }
171
172
173 //////////////////////////////
174 // アクセスエラーチェック
175 //////////////////////////////
176
177 if(ObjectName[0]){
178 //外部からの呼び出し
[206]179 if(pobj_c==compiler.pCompilingClass){
[3]180 //同一クラスオブジェクトの場合はプライベートアクセスを容認する
[137]181 if( pMethod->IsNoneAccess() ){
[75]182 SetError(109,pUserProc->GetName(),cp);
[76]183 return false;
[3]184 }
185 }
186 else{
[137]187 if( pMethod->IsPrivate()
188 || pMethod->IsNoneAccess() ){
[75]189 SetError(109,pUserProc->GetName(),cp);
[76]190 return false;
[3]191 }
[306]192 if( !pMethod->GetUserProc().GetParentClass().IsEqualsOrSubClass( pobj_c ) && pMethod->IsProtected() ){
[75]193 SetError(110,pUserProc->GetName(),cp);
[76]194 return false;
[3]195 }
196 }
197 }
198 else{
199 //クラス内部からの呼び出し(継承によるACCESS_NONのみをエラーとする)
[137]200 if( pMethod->IsNoneAccess() ){
[75]201 SetError(109,pUserProc->GetName(),cp);
[76]202 return false;
[3]203 }
204 }
205 }
206
207
208 ///////////////////////////////////////////////////////////////
[64]209 // _System_LocalThisのダミーをセット
[3]210 ///////////////////////////////////////////////////////////////
211
212 char temporary[VN_SIZE]={0};
[75]213 if( pUserProc->GetParentClassPtr() && isStatic == false ){
[3]214 //_System_LocalThis(第一パラメータ)のダミーを作成
215 lstrcpy(temporary,"0,");
216 }
217
218 if(Parameter[0]=='\0'&&temporary[0])
219 temporary[lstrlen(temporary)-1]=0;
220 else lstrcat(temporary,Parameter);
221
222
223 ////////////////////////
224 // パラメータをセット
225 ////////////////////////
226
227 //パラメータオブジェクトを生成
[71]228 ParamImpl *pobj_parameter=0;
229 pobj_parameter=new ParamImpl(temporary);
[3]230
[77]231 // デフォルト引数を適用
232 pobj_parameter->ApplyDefaultParameters( pUserProc->RealParams() );
233
[292]234 // 型パラメータを適用
235 pobj_parameter->SetLeftType( leftType );
236
[3]237 //エラーチェック
[75]238 if( !pobj_parameter->ErrorCheck(pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetSecondParmNum() ) ){
[31]239 //パラメータにエラーがあるときは処理を終える
[76]240 return false;
[31]241 }
[3]242
[75]243 if(pUserProc->IsMacro()){
[3]244 //マクロ関数の場合は、パラメータ省略を考慮する
[75]245 pobj_parameter->MacroParameterSupport( pUserProc->RealParams() );
[3]246 }
247
[20]248 //一時オブジェクトを生成
[75]249 int tempSize = pobj_parameter->NewTempParameters( pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetRealSecondParmNum() );
[20]250
[3]251 //レジスタ、スタックフレームにセット
[301]252 int ParmSize = pobj_parameter->SetParameter(pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetRealSecondParmNum(), pUserProc );
[3]253
[75]254 if(pUserProc->ReturnType().IsStruct() ){
[3]255 //////////////////////////////////////////////////////
[64]256 // 戻り値に構造体インスタンスを持つ場合
257 // ※ByRef _System_ReturnValue パラメータをセット
[3]258 //////////////////////////////////////////////////////
259
[75]260 int object_size = pUserProc->ReturnType().GetClass().GetSize();
[3]261
262 //push object_size
[225]263 compiler.codeGenerator.op_push_V(object_size);
[3]264
265 //call calloc
[206]266 extern const UserProc *pSub_calloc;
[225]267 compiler.codeGenerator.op_call(pSub_calloc);
[3]268
269 //push eax
[225]270 compiler.codeGenerator.op_push(REG_EAX);
[307]271
272 ParmSize += PTR_SIZE;
[3]273 }
274
275
[75]276 if( pUserProc->GetParentClassPtr() && isStatic == false ){
[3]277 //////////////////////////////////////////////////////
278 // メンバ関数の場合
279 // ※_System_LocalThis パラメータをセット
280 //////////////////////////////////////////////////////
281
[97]282 if(ObjectName[0] && (dwFlags&PROCFLAG_NEW)==0){
[3]283 if(lstrcmpi(ObjectName,"Super")==0) goto InClassMember;
[18]284 else{
285 RELATIVE_VAR RelativeVar;
[135]286 if( pMethod->IsConst() ){
[18]287 //Constアクセスが可能なメソッドの場合
[76]288 if( !GetVarOffsetReadOnly( ObjectName, &RelativeVar, Type() ) ){
289 return false;
290 }
[18]291 }
292 else{
293 //Constアクセスが不可能なメソッドの場合
[76]294 if( !GetVarOffsetReadWrite( ObjectName, &RelativeVar, Type() ) ){
295 return false;
296 }
[18]297 }
[3]298
[18]299 SetVarPtrToEax(&RelativeVar);
[3]300
[64]301 // 参照を実体ポインタにする
[225]302 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_EAX, 0, MOD_BASE );
[3]303 }
304 }
305 else{
306InClassMember:
307 if(dwFlags&PROCFLAG_NEW){
308 //New演算子によるコンストラクタ呼び出しの場合
[64]309
[3]310 //mov ecx,dword ptr[esp+ParmSize]
[225]311 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_ESP, ParmSize + tempSize, MOD_BASE_DISP32 );
[3]312 }
313 else{
314 //Thisポインタをecxにコピー
315 SetThisPtrToReg(REG_ECX);
316 }
317 }
318
319 //push ecx
[225]320 compiler.codeGenerator.op_push(REG_ECX);
[3]321 }
322
[304]323 if( pUserProc->IsVirtual() && !isFixedClass ){
[3]324 //仮想関数(オブジェクトメソッド)呼び出し
325 //pObj->func_table->func1
326 // ->func2
327 // ->func3
328
329 //mov edx,dword ptr[ecx]
[235]330 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, 0, MOD_BASE );
[3]331
[75]332 i2 = pobj_c->GetFuncNumInVtbl( pUserProc );
[3]333
334 //call dword ptr[edx+func_index]
335 if(i2*PTR_SIZE<=0x7F){
[250]336 compiler.codeGenerator.PutOld(
337 (char)0xFF,
338 (char)0x52,
339 (char)(i2*PTR_SIZE)
340 );
[3]341 }
342 else{
[250]343 compiler.codeGenerator.PutOld(
344 (char)0xFF,
345 (char)0x92
346 );
347 compiler.codeGenerator.PutOld( (long)(i2*PTR_SIZE), Schedule::None );
[3]348 }
349 }
350 else{
351 //通常呼び出し
352
353 //call ProcAddr
[225]354 compiler.codeGenerator.op_call(pUserProc);
[3]355 }
356
[75]357 if(pUserProc->IsCdecl()){
[3]358 //add esp,ParmSize
[225]359 compiler.codeGenerator.op_add_esp(ParmSize);
[3]360 }
361
[20]362 //一時オブジェクトを破棄
363 pobj_parameter->DeleteTempParameters();
364
365 //パラメータオブジェクトを破棄
366 delete pobj_parameter;
[76]367
368 return true;
[3]369}
370
[250]371bool Opcode_CallDllProc( const char *lpszParms, const DllProc *pDllProc ){
[3]372
373 extern BOOL bDebugCompile;
374 extern BOOL bDebugSupportProc;
[113]375 if(bDebugCompile&&bDebugSupportProc==0&& pDllProc->IsEqualSymbol( "DebugBreak" ) ){
[3]376 Call_DebugSys_SaveContext();
[75]377 }
[3]378
379
380 ////////////////////////
381 // パラメータのセット
382 ////////////////////////
383
384 //パラメータオブジェクトを生成
[71]385 ParamImpl *pobj_parameter=0;
[76]386 pobj_parameter=new ParamImpl(lpszParms);
[3]387
[77]388 // デフォルト引数を適用
389 pobj_parameter->ApplyDefaultParameters( pDllProc->Params() );
390
[3]391 //エラーチェック
[75]392 if( !pobj_parameter->ErrorCheck( pDllProc->GetName(), pDllProc->Params() ) ){
[31]393 //パラメータにエラーがあるときは処理を終える
[76]394 return false;
[31]395 }
[3]396
[45]397 //一時オブジェクトを生成
[75]398 pobj_parameter->NewTempParameters( pDllProc->GetName(), pDllProc->Params() );
[45]399
[3]400 //レジスタ、スタックフレームにセット
[75]401 int ParmSize = pobj_parameter->SetParameter(pDllProc->GetName(), pDllProc->Params() );
[3]402
403
404 //動的リンクされたプロシージャの呼び出し
405
406 //call dword ptr[LookupTable]
[250]407 compiler.codeGenerator.op_call( pDllProc );
[3]408
[75]409 if(pDllProc->IsCdecl()){
[3]410 //add esp,ParmSize
[225]411 compiler.codeGenerator.op_add_esp(ParmSize);
[3]412 }
413
[45]414 //一時オブジェクトを破棄
415 pobj_parameter->DeleteTempParameters();
416
417 //パラメータオブジェクトを破棄
418 delete pobj_parameter;
419
[76]420 return true;
[3]421}
Note: See TracBrowser for help on using the repository browser.