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

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

[415]を64bit版にマージ。

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