source: dev/trunk/abdev/BasicCompiler32/Compile_CallProc.cpp@ 342

Last change on this file since 342 was 342, checked in by dai_9181, 17 years ago

vtblの構造を変更。vtblMasterListをはさんでvtblを表現した。
その他メンバ名変更。
ClassPrototypeクラスを追加。

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