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

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

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

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