source: dev/trunk/ab5.0/abdev/compiler_x64/Compile_CallProc.cpp@ 598

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

SplitMemberNameの依存関係を排除。

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