source: dev/trunk/abdev/BasicCompiler32/Compile_CallProc.cpp@ 420

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

32bitコンパイラのリリース版で落ちてしまうバグを修正。スタック上の一時オブジェクトをクラス参照型パラメータに適用するのは危ない??

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