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

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

インターフェイス実装中…

File size: 14.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
[342]83bool Opcode_CallProc(const char *Parameter,const UserProc *pUserProc,DWORD dwFlags,const char *ObjectName )
84{
[75]85 if( pUserProc->IsMacro() ){
86 if( lstrcmpi( pUserProc->GetName().c_str(), "Print" ) == 0 ){
[3]87 Opcode_Print(Parameter,0);
[76]88 return true;
[3]89 }
[75]90 if( lstrcmpi( pUserProc->GetName().c_str(), "Input" ) == 0 ){
[3]91 Opcode_Input(Parameter);
[76]92 return true;
[3]93 }
[75]94 if( lstrcmpi( pUserProc->GetName().c_str(), "Write" ) == 0 ){
[3]95 Opcode_Print(Parameter,1);
[76]96 return true;
[3]97 }
98 }
99
[75]100 pUserProc->Using();
[3]101
[47]102 bool isStatic = false;
[76]103 const CClass *pobj_c = NULL;
[135]104 const CMethod *pMethod = NULL;
[292]105 Type leftType;
[304]106 bool isFixedClass = false;
[75]107 if( pUserProc->GetParentClassPtr() ){
[3]108 //クラスのメンバ関数を呼び出す場合はアクセスチェックを行う
[304]109 if(ObjectName[0] && (dwFlags&PROCFLAG_NEW)==0)
110 {
111 if(lstrcmpi(ObjectName,"Super")==0)
112 {
[27]113 //クラスメンバ関数内から基底クラスの呼び出し
[304]114 pobj_c=&compiler.pCompilingClass->GetSuperClass();
115
116 isFixedClass = true;
[3]117 }
[304]118 else
119 {
[47]120 //"->"によってオブジェクトを指定する通常のメンバ関数呼び出し
[76]121 Type varType;
122 GetVarType( ObjectName, varType, false );
[290]123 if( NATURAL_TYPE( varType.GetBasicType() ) == DEF_OBJECT )
124 {
125 pobj_c = &varType.GetClass();
[292]126 leftType = varType;
[290]127 }
128 else
129 {
[265]130 pobj_c=compiler.GetObjectModule().meta.GetClasses().Find(ObjectName);
[47]131 if( pobj_c ){
132 isStatic = true;
133 }
134 else{
135 SetError(300,NULL,cp);
136 }
[3]137 }
138 }
139 }
140 else{
141 if(dwFlags&PROCFLAG_NEW){
[294]142 GetVarType( ObjectName, leftType, false );
143
[3]144 //New演算子によるコンストラクタ呼び出し
[75]145 pobj_c=pUserProc->GetParentClassPtr();
[3]146 }
147 else{
148 //クラスメンバ関数内から同一クラスのメンバ関数の呼び出し
[206]149 pobj_c=compiler.pCompilingClass;
[3]150 }
151 }
152
153
[18]154 /////////////////////////////////
155 // メソッド情報を取得
156 /////////////////////////////////
[27]157 pMethod = NULL;
[347]158 if( ! isStatic ) pMethod = pobj_c->GetDynamicMethodOfInterfaceMethod( pUserProc );
[27]159 if( ! pMethod ){
[18]160 //動的メソッドが取得できなかったときは静的メソッドを当たる
[135]161 pMethod = pobj_c->GetStaticMethods().GetMethodPtr( pUserProc );
[18]162 if( !pMethod ){
163 SetError(300,NULL,cp);
[76]164 return false;
[3]165 }
[26]166
167 //静的メンバ
[47]168 isStatic = true;
[3]169 }
170
171
172 //////////////////////////////
173 // アクセスエラーチェック
174 //////////////////////////////
175
176 if(ObjectName[0]){
177 //外部からの呼び出し
[206]178 if(pobj_c==compiler.pCompilingClass){
[3]179 //同一クラスオブジェクトの場合はプライベートアクセスを容認する
[137]180 if( pMethod->IsNoneAccess() ){
[75]181 SetError(109,pUserProc->GetName(),cp);
[76]182 return false;
[3]183 }
184 }
185 else{
[137]186 if( pMethod->IsPrivate()
187 || pMethod->IsNoneAccess() ){
[75]188 SetError(109,pUserProc->GetName(),cp);
[76]189 return false;
[3]190 }
[306]191 if( !pMethod->GetUserProc().GetParentClass().IsEqualsOrSubClass( pobj_c ) && pMethod->IsProtected() ){
[75]192 SetError(110,pUserProc->GetName(),cp);
[76]193 return false;
[3]194 }
195 }
196 }
197 else{
198 //クラス内部からの呼び出し(継承によるACCESS_NONのみをエラーとする)
[137]199 if( pMethod->IsNoneAccess() ){
[75]200 SetError(109,pUserProc->GetName(),cp);
[76]201 return false;
[3]202 }
203 }
204 }
205
206
207 ///////////////////////////////////////////////////////////////
[64]208 // _System_LocalThisのダミーをセット
[3]209 ///////////////////////////////////////////////////////////////
210
211 char temporary[VN_SIZE]={0};
[75]212 if( pUserProc->GetParentClassPtr() && isStatic == false ){
[3]213 //_System_LocalThis(第一パラメータ)のダミーを作成
214 lstrcpy(temporary,"0,");
215 }
[320]216 if( pUserProc->ReturnType().IsStruct() ){
217 // ※ByRef _System_ReturnValue パラメータのダミーをセット
218 lstrcat(temporary,"0,");
219 }
[3]220
221 if(Parameter[0]=='\0'&&temporary[0])
222 temporary[lstrlen(temporary)-1]=0;
223 else lstrcat(temporary,Parameter);
224
225
226 ////////////////////////
227 // パラメータをセット
228 ////////////////////////
229
230 //パラメータオブジェクトを生成
[71]231 ParamImpl *pobj_parameter=0;
232 pobj_parameter=new ParamImpl(temporary);
[3]233
[77]234 // デフォルト引数を適用
235 pobj_parameter->ApplyDefaultParameters( pUserProc->RealParams() );
236
[292]237 // 型パラメータを適用
238 pobj_parameter->SetLeftType( leftType );
239
[3]240 //エラーチェック
[75]241 if( !pobj_parameter->ErrorCheck(pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetSecondParmNum() ) ){
[31]242 //パラメータにエラーがあるときは処理を終える
[76]243 return false;
[31]244 }
[3]245
[75]246 if(pUserProc->IsMacro()){
[3]247 //マクロ関数の場合は、パラメータ省略を考慮する
[75]248 pobj_parameter->MacroParameterSupport( pUserProc->RealParams() );
[3]249 }
250
[20]251 //一時オブジェクトを生成
[75]252 int tempSize = pobj_parameter->NewTempParameters( pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetRealSecondParmNum() );
[20]253
[3]254 //レジスタ、スタックフレームにセット
[301]255 int ParmSize = pobj_parameter->SetParameter(pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetRealSecondParmNum(), pUserProc );
[3]256
[75]257 if(pUserProc->ReturnType().IsStruct() ){
[3]258 //////////////////////////////////////////////////////
[64]259 // 戻り値に構造体インスタンスを持つ場合
260 // ※ByRef _System_ReturnValue パラメータをセット
[3]261 //////////////////////////////////////////////////////
262
[75]263 int object_size = pUserProc->ReturnType().GetClass().GetSize();
[3]264
265 //push object_size
[225]266 compiler.codeGenerator.op_push_V(object_size);
[3]267
268 //call calloc
[206]269 extern const UserProc *pSub_calloc;
[225]270 compiler.codeGenerator.op_call(pSub_calloc);
[3]271
272 //push eax
[225]273 compiler.codeGenerator.op_push(REG_EAX);
[307]274
275 ParmSize += PTR_SIZE;
[3]276 }
277
278
[75]279 if( pUserProc->GetParentClassPtr() && isStatic == false ){
[3]280 //////////////////////////////////////////////////////
281 // メンバ関数の場合
282 // ※_System_LocalThis パラメータをセット
283 //////////////////////////////////////////////////////
284
[97]285 if(ObjectName[0] && (dwFlags&PROCFLAG_NEW)==0){
[3]286 if(lstrcmpi(ObjectName,"Super")==0) goto InClassMember;
[18]287 else{
288 RELATIVE_VAR RelativeVar;
[135]289 if( pMethod->IsConst() ){
[18]290 //Constアクセスが可能なメソッドの場合
[76]291 if( !GetVarOffsetReadOnly( ObjectName, &RelativeVar, Type() ) ){
292 return false;
293 }
[18]294 }
295 else{
296 //Constアクセスが不可能なメソッドの場合
[76]297 if( !GetVarOffsetReadWrite( ObjectName, &RelativeVar, Type() ) ){
298 return false;
299 }
[18]300 }
[3]301
[18]302 SetVarPtrToEax(&RelativeVar);
[3]303
[64]304 // 参照を実体ポインタにする
[225]305 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_EAX, 0, MOD_BASE );
[3]306 }
307 }
308 else{
309InClassMember:
310 if(dwFlags&PROCFLAG_NEW){
311 //New演算子によるコンストラクタ呼び出しの場合
[64]312
[3]313 //mov ecx,dword ptr[esp+ParmSize]
[225]314 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_ESP, ParmSize + tempSize, MOD_BASE_DISP32 );
[3]315 }
316 else{
317 //Thisポインタをecxにコピー
318 SetThisPtrToReg(REG_ECX);
319 }
320 }
321
322 //push ecx
[225]323 compiler.codeGenerator.op_push(REG_ECX);
[3]324 }
325
[304]326 if( pUserProc->IsVirtual() && !isFixedClass ){
[3]327 //仮想関数(オブジェクトメソッド)呼び出し
328 //pObj->func_table->func1
329 // ->func2
330 // ->func3
331
[349]332 int vtblIndex;
333 if( pobj_c->IsInterface() )
334 {
335 // インターフェイスメソッド呼び出し
[348]336
[349]337 int offset_vtbl = compiler.GetObjectModule().meta.GetClasses().GetInterfaceInfoClassPtr()->GetMemberOffset( "__vtbl" );
[3]338
[349]339
340 // vtblのポインタを取得
341 //mov edx,dword ptr[ecx+offset_vtbl]
342 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, offset_vtbl, MOD_BASE_DISP8 );
343
344 int offset_this = compiler.GetObjectModule().meta.GetClasses().GetInterfaceInfoClassPtr()->GetMemberOffset( "__this" );
345
346
347
348 // インターフェイスの場合は更に__thisを取得する
349 //mov rcx,qword ptr[rcx+offset_this]
350 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_ECX, offset_this, MOD_BASE_DISP8 );
351
352 int vtblMasterListIndex;
353 pobj_c->GetVtblMasterListIndexAndVtblIndex( pUserProc, vtblMasterListIndex, vtblIndex );
354 if( vtblMasterListIndex != 0 )
355 {
356 SetError();
357 }
358 }
359 else
360 {
361 //仮想関数(オブジェクトメソッド)呼び出し
362 // pObj -> vtbl_master_list -> vtbl1 -> func1
363 // -> func2
364 // -> func3
365 // -> vtbl2 -> func1
366 // -> func2
367 // -> func3
368
369 int vtblMasterListIndex;
370 pobj_c->GetVtblMasterListIndexAndVtblIndex( pUserProc, vtblMasterListIndex, vtblIndex );
371
372 // vtblマスターリストのポインタを取得
373 //mov edx,dword ptr[ecx]
374 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, 0, MOD_BASE );
375
376 // vtblのポインタを取得
377 //mov edx,dword ptr[edx+vtblMasterListIndex]
378 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_EDX, vtblMasterListIndex, MOD_BASE_DISP32 );
379 }
380
[3]381 //call dword ptr[edx+func_index]
[342]382 if( vtblIndex * PTR_SIZE <= 0x7F )
383 {
[250]384 compiler.codeGenerator.PutOld(
385 (char)0xFF,
386 (char)0x52,
[342]387 (char)(vtblIndex*PTR_SIZE)
[250]388 );
[3]389 }
390 else{
[250]391 compiler.codeGenerator.PutOld(
392 (char)0xFF,
393 (char)0x92
394 );
[342]395 compiler.codeGenerator.PutOld( (long)(vtblIndex*PTR_SIZE), Schedule::None );
[3]396 }
397 }
398 else{
399 //通常呼び出し
400
401 //call ProcAddr
[225]402 compiler.codeGenerator.op_call(pUserProc);
[3]403 }
404
[75]405 if(pUserProc->IsCdecl()){
[3]406 //add esp,ParmSize
[225]407 compiler.codeGenerator.op_add_esp(ParmSize);
[3]408 }
409
[20]410 //一時オブジェクトを破棄
411 pobj_parameter->DeleteTempParameters();
412
413 //パラメータオブジェクトを破棄
414 delete pobj_parameter;
[76]415
416 return true;
[3]417}
418
[250]419bool Opcode_CallDllProc( const char *lpszParms, const DllProc *pDllProc ){
[3]420
421 extern BOOL bDebugCompile;
422 extern BOOL bDebugSupportProc;
[113]423 if(bDebugCompile&&bDebugSupportProc==0&& pDllProc->IsEqualSymbol( "DebugBreak" ) ){
[3]424 Call_DebugSys_SaveContext();
[75]425 }
[3]426
427
428 ////////////////////////
429 // パラメータのセット
430 ////////////////////////
431
432 //パラメータオブジェクトを生成
[71]433 ParamImpl *pobj_parameter=0;
[76]434 pobj_parameter=new ParamImpl(lpszParms);
[3]435
[77]436 // デフォルト引数を適用
437 pobj_parameter->ApplyDefaultParameters( pDllProc->Params() );
438
[3]439 //エラーチェック
[75]440 if( !pobj_parameter->ErrorCheck( pDllProc->GetName(), pDllProc->Params() ) ){
[31]441 //パラメータにエラーがあるときは処理を終える
[76]442 return false;
[31]443 }
[3]444
[45]445 //一時オブジェクトを生成
[75]446 pobj_parameter->NewTempParameters( pDllProc->GetName(), pDllProc->Params() );
[45]447
[3]448 //レジスタ、スタックフレームにセット
[75]449 int ParmSize = pobj_parameter->SetParameter(pDllProc->GetName(), pDllProc->Params() );
[3]450
451
452 //動的リンクされたプロシージャの呼び出し
453
454 //call dword ptr[LookupTable]
[250]455 compiler.codeGenerator.op_call( pDllProc );
[3]456
[75]457 if(pDllProc->IsCdecl()){
[3]458 //add esp,ParmSize
[225]459 compiler.codeGenerator.op_add_esp(ParmSize);
[3]460 }
461
[45]462 //一時オブジェクトを破棄
463 pobj_parameter->DeleteTempParameters();
464
465 //パラメータオブジェクトを破棄
466 delete pobj_parameter;
467
[76]468 return true;
[3]469}
[325]470
471void Opcode_CallDelegate( const Delegate &dg, const char *methodPtrValueStr, const char *objPtrValueStr, const char *params )
472{
[332]473 extern BOOL bDebugCompile;
474 extern BOOL bDebugSupportProc;
475 if(bDebugCompile&&bDebugSupportProc==0)
476 Call_DebugSys_SaveContext();
477
478
[325]479 ///////////////////////////////////////////////////////////////
480 // _System_LocalThisのダミーをセット
481 ///////////////////////////////////////////////////////////////
482
483 char temporary[VN_SIZE]={0};
[339]484 bool isDynamicCall = false;
[325]485 if( objPtrValueStr && objPtrValueStr[0] ){
486 //_System_LocalThis(第一パラメータ)のダミーを作成
487 lstrcpy(temporary,"0,");
[339]488
489 isDynamicCall = true;
[325]490 }
491 if( dg.ReturnType().IsStruct() ){
492 // ※ByRef _System_ReturnValue パラメータのダミーをセット
493 lstrcat(temporary,"0,");
494 }
495
496 if(params[0]=='\0'&&temporary[0])
497 temporary[lstrlen(temporary)-1]=0;
498 else lstrcat(temporary,params);
499
500
[339]501 const Parameters *pParams = &dg.Params();
502 if( isDynamicCall )
503 {
504 pParams = &dg.GetDynamicParams();
505 }
[325]506
[339]507
508 ParamImpl *pobj_parameter = new ParamImpl( temporary );
509
[325]510 //一時オブジェクトを生成
[339]511 pobj_parameter->NewTempParameters( dg.GetName(), *pParams );
[325]512
513 //レジスタ、スタックフレームにセット
[339]514 int ParmSize = pobj_parameter->SetParameter( dg.GetName(), *pParams );
[325]515
516
517 if( objPtrValueStr && objPtrValueStr[0] )
518 {
519 RELATIVE_VAR RelativeVar;
520 //Constアクセスが不可能なメソッドの場合
521 if( !GetVarOffsetReadWrite( objPtrValueStr, &RelativeVar, Type() ) ){
522 Jenga::Throw( "Opcode_CallDelegate関数内で呼ばれるGetVarOffsetReadWrite関数に失敗" );
523 return;
524 }
525
526 SetVarPtrToEax(&RelativeVar);
527
528 // 参照を実体ポインタにする
529 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_EAX, 0, MOD_BASE );
530
531 //push ecx
532 compiler.codeGenerator.op_push(REG_ECX);
533 }
534
535
536 {
537 ////////////////////////
538 // call
539 ////////////////////////
540 RELATIVE_VAR RelativeVar;
541 GetVarOffsetReadOnly( methodPtrValueStr, &RelativeVar, Type() );
542 SetVarPtrToEax( &RelativeVar );
543
544 //mov eax,dword ptr[eax]
545 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
546
547 //call eax
548 compiler.codeGenerator.op_call_R( REG_EAX );
549 }
550
551
552 //一時オブジェクトを破棄
553 pobj_parameter->DeleteTempParameters();
554
555 //パラメータオブジェクトを破棄
556 delete pobj_parameter;
557}
Note: See TracBrowser for help on using the repository browser.