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