source: dev/trunk/ab5.0/abdev/BasicCompiler32/Compile_CallProc.cpp@ 465

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

Messenger/ErrorMessengerクラスを導入。SetError関数によるエラー生成を廃止した。

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