source: dev/trunk/ab5.0/abdev/BasicCompiler32/Compile_CallProc.cpp@ 461

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

smoothieプロジェクトが不要になったため、破棄。

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