source: dev/trunk/ab5.0/abdev/compiler_x86/Compile_CallProc.cpp@ 536

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

Compiler::pCompilingClassメンバをprivateにし、setter/getterにあたるメソッドを用意した。

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