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

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

Symbolクラスをab_commonプロジェクトに移動した。

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