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

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

Compiler::pCompilingClassメンバをprivateにし、setter/getterにあたるメソッドを用意した。

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