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

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

関数の戻り値の構造体など、一時メモリに保持された構造体のメンバに直接アクセスした場合、その一時メモリの解放が正常に行われないバグを修正(まずは32bit版のみ)

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
[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{
[435]291 bool isLiteral, isNeedHeapFreeStructure = false;
[420]292 Type baseType( DEF_OBJECT, *pUserProc->GetParentClassPtr() ) , resultType;
[435]293 if( !TermOpe( ObjectName, baseType, resultType, isLiteral, isNeedHeapFreeStructure, NULL, false, !pMethod->IsConst() ) )
[415]294 {
295 return false;
[18]296 }
[435]297 if( !resultType.IsObject() )
298 {
299 SetError();
300 }
[3]301
[415]302 // 実態ポインタをeaxにコピー
303 compiler.codeGenerator.op_mov_RR( REG_ECX, REG_EAX );
[3]304 }
305 }
306 else{
307InClassMember:
308 if(dwFlags&PROCFLAG_NEW){
309 //New演算子によるコンストラクタ呼び出しの場合
[64]310
[3]311 //mov ecx,dword ptr[esp+ParmSize]
[225]312 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_ESP, ParmSize + tempSize, MOD_BASE_DISP32 );
[3]313 }
314 else{
315 //Thisポインタをecxにコピー
316 SetThisPtrToReg(REG_ECX);
317 }
318 }
319 }
320
[350]321 if( pUserProc->IsVirtual() && !isFixedClass )
322 {
[349]323 int vtblIndex;
324 if( pobj_c->IsInterface() )
325 {
[370]326 // インターフェイス メソッド呼び出し
[348]327
[349]328 int offset_vtbl = compiler.GetObjectModule().meta.GetClasses().GetInterfaceInfoClassPtr()->GetMemberOffset( "__vtbl" );
[3]329
[349]330
331 // vtblのポインタを取得
332 //mov edx,dword ptr[ecx+offset_vtbl]
333 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, offset_vtbl, MOD_BASE_DISP8 );
334
335 int offset_this = compiler.GetObjectModule().meta.GetClasses().GetInterfaceInfoClassPtr()->GetMemberOffset( "__this" );
336
337
338
339 // インターフェイスの場合は更に__thisを取得する
[350]340 //mov ecx,qword ptr[ecx+offset_this]
[349]341 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_ECX, offset_this, MOD_BASE_DISP8 );
342
343 int vtblMasterListIndex;
344 pobj_c->GetVtblMasterListIndexAndVtblIndex( pUserProc, vtblMasterListIndex, vtblIndex );
345 if( vtblMasterListIndex != 0 )
346 {
347 SetError();
348 }
349 }
[370]350 else if( pobj_c->IsComInterface() )
351 {
352 // COMインターフェイス メソッド呼び出し
353
354 //仮想関数(オブジェクトメソッド)呼び出し
355 // pObj -> vtbl1 -> func1
356 // -> func2
357 // -> func3
358
359 int vtblMasterListIndex;
360 pobj_c->GetVtblMasterListIndexAndVtblIndex( pUserProc, vtblMasterListIndex, vtblIndex );
361
362 // vtblのポインタを取得
363 //mov edx,dword ptr[ecx]
364 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, 0, MOD_BASE );
365 }
[349]366 else
367 {
368 //仮想関数(オブジェクトメソッド)呼び出し
369 // pObj -> vtbl_master_list -> vtbl1 -> func1
370 // -> func2
371 // -> func3
372 // -> vtbl2 -> func1
373 // -> func2
374 // -> func3
375
376 int vtblMasterListIndex;
377 pobj_c->GetVtblMasterListIndexAndVtblIndex( pUserProc, vtblMasterListIndex, vtblIndex );
378
379 // vtblマスターリストのポインタを取得
[370]380 //mov edx,dword ptr[ecx+sizeof(com_vtbl)]
381 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, PTR_SIZE, MOD_BASE_DISP8 );
[349]382
383 // vtblのポインタを取得
384 //mov edx,dword ptr[edx+vtblMasterListIndex]
[350]385 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_EDX, vtblMasterListIndex*PTR_SIZE, MOD_BASE_DISP32 );
[349]386 }
387
[350]388 //push ecx
389 compiler.codeGenerator.op_push(REG_ECX);
390
[3]391 //call dword ptr[edx+func_index]
[342]392 if( vtblIndex * PTR_SIZE <= 0x7F )
393 {
[250]394 compiler.codeGenerator.PutOld(
395 (char)0xFF,
396 (char)0x52,
[342]397 (char)(vtblIndex*PTR_SIZE)
[250]398 );
[3]399 }
400 else{
[250]401 compiler.codeGenerator.PutOld(
402 (char)0xFF,
403 (char)0x92
404 );
[342]405 compiler.codeGenerator.PutOld( (long)(vtblIndex*PTR_SIZE), Schedule::None );
[3]406 }
407 }
408 else{
409 //通常呼び出し
410
[350]411 if( pUserProc->GetParentClassPtr() && isStatic == false )
412 {
413 //push ecx
414 compiler.codeGenerator.op_push(REG_ECX);
415 }
416
[3]417 //call ProcAddr
[225]418 compiler.codeGenerator.op_call(pUserProc);
[3]419 }
420
[75]421 if(pUserProc->IsCdecl()){
[3]422 //add esp,ParmSize
[225]423 compiler.codeGenerator.op_add_esp(ParmSize);
[3]424 }
425
[20]426 //一時オブジェクトを破棄
427 pobj_parameter->DeleteTempParameters();
428
429 //パラメータオブジェクトを破棄
430 delete pobj_parameter;
[76]431
432 return true;
[3]433}
434
[250]435bool Opcode_CallDllProc( const char *lpszParms, const DllProc *pDllProc ){
[3]436
437 extern BOOL bDebugCompile;
438 extern BOOL bDebugSupportProc;
[113]439 if(bDebugCompile&&bDebugSupportProc==0&& pDllProc->IsEqualSymbol( "DebugBreak" ) ){
[3]440 Call_DebugSys_SaveContext();
[75]441 }
[3]442
443
444 ////////////////////////
445 // パラメータのセット
446 ////////////////////////
447
448 //パラメータオブジェクトを生成
[71]449 ParamImpl *pobj_parameter=0;
[76]450 pobj_parameter=new ParamImpl(lpszParms);
[3]451
[77]452 // デフォルト引数を適用
453 pobj_parameter->ApplyDefaultParameters( pDllProc->Params() );
454
[3]455 //エラーチェック
[75]456 if( !pobj_parameter->ErrorCheck( pDllProc->GetName(), pDllProc->Params() ) ){
[31]457 //パラメータにエラーがあるときは処理を終える
[76]458 return false;
[31]459 }
[3]460
[45]461 //一時オブジェクトを生成
[75]462 pobj_parameter->NewTempParameters( pDllProc->GetName(), pDllProc->Params() );
[45]463
[3]464 //レジスタ、スタックフレームにセット
[75]465 int ParmSize = pobj_parameter->SetParameter(pDllProc->GetName(), pDllProc->Params() );
[3]466
467
468 //動的リンクされたプロシージャの呼び出し
469
470 //call dword ptr[LookupTable]
[250]471 compiler.codeGenerator.op_call( pDllProc );
[3]472
[75]473 if(pDllProc->IsCdecl()){
[3]474 //add esp,ParmSize
[225]475 compiler.codeGenerator.op_add_esp(ParmSize);
[3]476 }
477
[45]478 //一時オブジェクトを破棄
479 pobj_parameter->DeleteTempParameters();
480
481 //パラメータオブジェクトを破棄
482 delete pobj_parameter;
483
[76]484 return true;
[3]485}
[325]486
487void Opcode_CallDelegate( const Delegate &dg, const char *methodPtrValueStr, const char *objPtrValueStr, const char *params )
488{
[332]489 extern BOOL bDebugCompile;
490 extern BOOL bDebugSupportProc;
491 if(bDebugCompile&&bDebugSupportProc==0)
492 Call_DebugSys_SaveContext();
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.