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

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

・Compiler::IsDebugメソッドを導入した(bDebugCompileグローバル変数は廃止)。
・bStrictグローバル変数は意味を成さないので廃止した。

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