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

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