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

Last change on this file since 730 was 730, checked in by dai, 16 years ago

外部からprivateメソッドを呼び出してしまった場合において、エラーを吐いてからも構文解析を継続するようにした。

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