source: dev/trunk/ab5.0/abdev/compiler_x86/Compile_CallProc.cpp@ 829

Last change on this file since 829 was 829, checked in by イグトランス (egtra), 12 years ago

svn:eol-styleとsvn:mime-type(文字コード指定含む)の設定

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