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

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

SplitMemberNameの依存関係を排除。

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