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

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

SplitMemberNameの依存関係を排除。

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