source: dev/trunk/ab5.0/abdev/BasicCompiler64/Compile_CallProc.cpp@ 452

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

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

File size: 16.9 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, isNeedHeapFreeStructure = false;
325 Type baseType( DEF_OBJECT, *pUserProc->GetParentClassPtr() ) , resultType;
326 if( !TermOpe( ObjectName, baseType, resultType, isLiteral, isNeedHeapFreeStructure, NULL, false, !pMethod->IsConst() ) )
327 {
328 return false;
329 }
330 if( !resultType.IsObject() )
331 {
332 SetError();
333 }
334
335 // 実態ポインタをraxにコピー
336 compiler.codeGenerator.op_mov_RR( REG_RCX, REG_RAX );
337 }
338 }
339 else{
340InClassMember:
341 if(dwFlags&PROCFLAG_NEW){
342 //New演算子によるコンストラクタ呼び出しの場合
343
344 //mov rcx,qword ptr[rsp+offset] ※スタックフレームを利用
345 pobj_sf->ref_offset_data(REG_RCX, this_sp_offset);
346 }
347 else{
348 //自身のオブジェクトのThisポインタをrcxにコピー
349 SetThisPtrToReg(REG_RCX);
350 }
351 }
352 }
353
354 if( pUserProc->IsVirtual() && !isFixedClass ){
355 int vtblIndex;
356 if( pobj_c->IsInterface() )
357 {
358 // インターフェイス メソッド呼び出し
359
360 int offset_vtbl = compiler.GetObjectModule().meta.GetClasses().GetInterfaceInfoClassPtr()->GetMemberOffset( "__vtbl" );
361
362 // vtblのポインタを取得
363 //mov r11,qword ptr[rcx+offset_vtbl]
364 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RCX,offset_vtbl,MOD_BASE_DISP8);
365
366 int offset_this = compiler.GetObjectModule().meta.GetClasses().GetInterfaceInfoClassPtr()->GetMemberOffset( "__this" );
367
368 // インターフェイスの場合は更に__thisを取得する
369 //mov rcx,qword ptr[rcx+offset_this]
370 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_RCX,REG_RCX,offset_this,MOD_BASE_DISP8);
371
372 int vtblMasterListIndex;
373 pobj_c->GetVtblMasterListIndexAndVtblIndex( pUserProc, vtblMasterListIndex, vtblIndex );
374 if( vtblMasterListIndex != 0 )
375 {
376 SetError();
377 }
378 }
379 else if( pobj_c->IsComInterface() )
380 {
381 // COMインターフェイス メソッド呼び出し
382
383 //仮想関数(オブジェクトメソッド)呼び出し
384 // pObj -> vtbl1 -> func1
385 // -> func2
386 // -> func3
387
388 int vtblMasterListIndex;
389 pobj_c->GetVtblMasterListIndexAndVtblIndex( pUserProc, vtblMasterListIndex, vtblIndex );
390
391 // vtblのポインタを取得
392 //mov r11,qword ptr[rcx]
393 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RCX,0,MOD_BASE); }
394 else
395 {
396 //仮想関数(オブジェクトメソッド)呼び出し
397 // pObj -> vtbl_master_list -> vtbl1 -> func1
398 // -> func2
399 // -> func3
400 // -> vtbl2 -> func1
401 // -> func2
402 // -> func3
403
404 int vtblMasterListIndex;
405 pobj_c->GetVtblMasterListIndexAndVtblIndex( pUserProc, vtblMasterListIndex, vtblIndex );
406
407 // vtblマスターリストのポインタを取得
408 //mov r11,qword ptr[rcx+sizeof(com_vtbl)]
409 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RCX,PTR_SIZE,MOD_BASE_DISP8);
410
411 // vtblのポインタを取得
412 //mov r11,dword ptr[r11+vtblMasterListIndex]
413 compiler.codeGenerator.op_mov_RM( sizeof(_int64), REG_R11, REG_R11, vtblMasterListIndex*PTR_SIZE, MOD_BASE_DISP32 );
414 }
415
416 //call qword ptr[r11+func_index]
417 if( vtblIndex * PTR_SIZE <= 0x7F ){
418 compiler.codeGenerator.PutOld(
419 (char)0x41,
420 (char)0xFF,
421 (char)0x53,
422 (char)(vtblIndex*PTR_SIZE)
423 );
424 }
425 else{
426 compiler.codeGenerator.PutOld(
427 (char)0x41,
428 (char)0xFF,
429 (char)0x93,
430 (long)(vtblIndex*PTR_SIZE)
431 );
432 }
433 }
434 else{
435 //通常呼び出し
436
437 //call ProcAddr
438 compiler.codeGenerator.op_call(pUserProc);
439 }
440
441 /* 64コンパイラでは不要
442 if(pDllProc->bCdecl){
443 //add esp,ParmSize
444 }*/
445
446
447 //レジスタのブロッキングを解除 ※パラメータセット時にロックされたレジスタ
448 pobj_BlockReg->clear();
449
450 //一時オブジェクトを破棄
451 pobj_parameter->DeleteTempParameters();
452
453 //スタックフレームに存在する既存のパラメータを復元
454 pobj_parameter->RestoreParameter( (int)pUserProc->RealParams().size() );
455
456 //パラメータオブジェクトを破棄
457 delete pobj_parameter;
458
459 return true;
460}
461
462bool Opcode_CallDllProc( const char *lpszParms, DllProc *pDllProc ){
463
464 extern BOOL bDebugCompile;
465 extern BOOL bDebugSupportProc;
466 if(bDebugCompile&&bDebugSupportProc==0&& pDllProc->GetName() != "DebugBreak" ){
467 Call_DebugSys_SaveContext();
468 }
469
470
471 ////////////////////////
472 // パラメータのセット
473 ////////////////////////
474
475 //パラメータオブジェクトを生成
476 ParamImpl *pobj_parameter=0;
477 pobj_parameter=new ParamImpl(lpszParms);
478
479 // デフォルト引数を適用
480 pobj_parameter->ApplyDefaultParameters( pDllProc->Params() );
481
482 //エラーチェック
483 if( !pobj_parameter->ErrorCheck( pDllProc->GetName(), pDllProc->Params() ) ){
484 //パラメータにエラーがあるときは処理を終える
485 return false;
486 }
487
488 //スタックフレームに存在する既存のパラメータをバックアップ
489 pobj_parameter->BackupParameter( (int)pDllProc->Params().size() );
490
491 //一時オブジェクトを生成
492 pobj_parameter->NewTempParameters( pDllProc->GetName(), pDllProc->Params() );
493
494 //レジスタ、スタックフレームにセット
495 pobj_parameter->SetParameter(pDllProc->GetName(), pDllProc->Params() );
496
497
498 //レジスタのブロッキングを解除 ※パラメータセット時にロックされたレジスタ
499 pobj_BlockReg->clear();
500
501
502 //動的リンクされたプロシージャの呼び出し
503
504 //call dword ptr[ImportTable]
505 compiler.codeGenerator.op_call( pDllProc );
506
507 /* 64コンパイラでは不要
508 if(pDllProc->bCdecl){
509 //add esp,ParmSize
510 }*/
511
512 //一時オブジェクトを破棄
513 pobj_parameter->DeleteTempParameters();
514
515 //スタックフレームに存在する既存のパラメータを復元
516 pobj_parameter->RestoreParameter( (int)pDllProc->Params().size() );
517
518 //パラメータオブジェクトを破棄
519 delete pobj_parameter;
520
521 return true;
522}
523
524void Opcode_CallDelegate( const Delegate &dg, const char *methodPtrValueStr, const char *objPtrValueStr, const char *params )
525{
526 extern BOOL bDebugCompile;
527 extern BOOL bDebugSupportProc;
528 if(bDebugCompile&&bDebugSupportProc==0)
529 Call_DebugSys_SaveContext();
530
531
532 ///////////////////////////////////////////////////////////////
533 // _System_LocalThisのダミーをセット
534 ///////////////////////////////////////////////////////////////
535
536 char temporary[VN_SIZE]={0};
537 bool isDynamicCall = false;
538 if( objPtrValueStr && objPtrValueStr[0] ){
539 //_System_LocalThis(第一パラメータ)のダミーを作成
540 lstrcpy(temporary,"0,");
541
542 isDynamicCall = true;
543 }
544 if( dg.ReturnType().IsStruct() ){
545 // ※ByRef _System_ReturnValue パラメータのダミーをセット
546 lstrcat(temporary,"0,");
547 }
548
549 if(params[0]=='\0'&&temporary[0])
550 temporary[lstrlen(temporary)-1]=0;
551 else lstrcat(temporary,params);
552
553 const Parameters *pParams = &dg.Params();
554 if( isDynamicCall )
555 {
556 pParams = &dg.GetDynamicParams();
557 }
558
559
560 ParamImpl *pobj_parameter = new ParamImpl( temporary );
561
562 //スタックフレームに存在する既存のパラメータをバックアップ
563 pobj_parameter->BackupParameter( (int)pParams->size() );
564
565 //一時オブジェクトを生成
566 pobj_parameter->NewTempParameters( dg.GetName(), *pParams );
567
568 //レジスタ、スタックフレームにセット
569 pobj_parameter->SetParameter( dg.GetName(), *pParams );
570
571
572 if( objPtrValueStr && objPtrValueStr[0] )
573 {
574 RELATIVE_VAR RelativeVar;
575 //Constアクセスが不可能なメソッドの場合
576 if( !GetVarOffsetReadWrite( objPtrValueStr, &RelativeVar, Type() ) ){
577 Jenga::Throw( "Opcode_CallDelegate関数内で呼ばれるGetVarOffsetReadWrite関数に失敗" );
578 return;
579 }
580
581 SetVarPtrToReg(REG_RCX,&RelativeVar);
582
583 // 参照を実体ポインタにする
584 //mov rcx,qword ptr[rcx]
585 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_RCX,REG_RCX,0,MOD_BASE);
586 }
587
588
589 {
590 ////////////////////////
591 // call
592 ////////////////////////
593 RELATIVE_VAR RelativeVar;
594 GetVarOffsetReadOnly( methodPtrValueStr, &RelativeVar, Type() );
595 SetVarPtrToReg(REG_RAX,&RelativeVar);
596
597 //mov rax,qword ptr[rax]
598 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_RAX,REG_RAX,0,MOD_BASE);
599
600 //call rax
601 compiler.codeGenerator.PutOld(
602 (char)0xFF,
603 (char)0xD0
604 );
605 }
606
607
608 //レジスタのブロッキングを解除 ※パラメータセット時にロックされたレジスタ
609 pobj_BlockReg->clear();
610
611 //一時オブジェクトを破棄
612 pobj_parameter->DeleteTempParameters();
613
614 //スタックフレームに存在する既存のパラメータを復元
615 pobj_parameter->RestoreParameter( (int)pParams->size() );
616
617 //パラメータオブジェクトを破棄
618 delete pobj_parameter;
619}
Note: See TracBrowser for help on using the repository browser.