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

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

構造体をクラスメソッドの戻り値にしたときにThisポインタが正常に引き渡されないバグを修正。

File size: 10.8 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
218 if(Parameter[0]=='\0'&&temporary[0])
219 temporary[lstrlen(temporary)-1]=0;
220 else lstrcat(temporary,Parameter);
221
222
223 ////////////////////////
224 // パラメータをセット
225 ////////////////////////
226
227 //パラメータオブジェクトを生成
228 ParamImpl *pobj_parameter=0;
229 pobj_parameter=new ParamImpl(temporary);
230
231 // デフォルト引数を適用
232 pobj_parameter->ApplyDefaultParameters( pUserProc->RealParams() );
233
234 // 型パラメータを適用
235 pobj_parameter->SetLeftType( leftType );
236
237 //エラーチェック
238 if( !pobj_parameter->ErrorCheck(pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetSecondParmNum() ) ){
239 //パラメータにエラーがあるときは処理を終える
240 return false;
241 }
242
243 if(pUserProc->IsMacro()){
244 //マクロ関数の場合は、パラメータ省略を考慮する
245 pobj_parameter->MacroParameterSupport( pUserProc->RealParams() );
246 }
247
248 //一時オブジェクトを生成
249 int tempSize = pobj_parameter->NewTempParameters( pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetRealSecondParmNum() );
250
251 //レジスタ、スタックフレームにセット
252 int ParmSize = pobj_parameter->SetParameter(pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetRealSecondParmNum(), pUserProc );
253
254 if(pUserProc->ReturnType().IsStruct() ){
255 //////////////////////////////////////////////////////
256 // 戻り値に構造体インスタンスを持つ場合
257 // ※ByRef _System_ReturnValue パラメータをセット
258 //////////////////////////////////////////////////////
259
260 int object_size = pUserProc->ReturnType().GetClass().GetSize();
261
262 //push object_size
263 compiler.codeGenerator.op_push_V(object_size);
264
265 //call calloc
266 extern const UserProc *pSub_calloc;
267 compiler.codeGenerator.op_call(pSub_calloc);
268
269 //push eax
270 compiler.codeGenerator.op_push(REG_EAX);
271
272 ParmSize += PTR_SIZE;
273 }
274
275
276 if( pUserProc->GetParentClassPtr() && isStatic == false ){
277 //////////////////////////////////////////////////////
278 // メンバ関数の場合
279 // ※_System_LocalThis パラメータをセット
280 //////////////////////////////////////////////////////
281
282 if(ObjectName[0] && (dwFlags&PROCFLAG_NEW)==0){
283 if(lstrcmpi(ObjectName,"Super")==0) goto InClassMember;
284 else{
285 RELATIVE_VAR RelativeVar;
286 if( pMethod->IsConst() ){
287 //Constアクセスが可能なメソッドの場合
288 if( !GetVarOffsetReadOnly( ObjectName, &RelativeVar, Type() ) ){
289 return false;
290 }
291 }
292 else{
293 //Constアクセスが不可能なメソッドの場合
294 if( !GetVarOffsetReadWrite( ObjectName, &RelativeVar, Type() ) ){
295 return false;
296 }
297 }
298
299 SetVarPtrToEax(&RelativeVar);
300
301 // 参照を実体ポインタにする
302 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_EAX, 0, MOD_BASE );
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 //push ecx
320 compiler.codeGenerator.op_push(REG_ECX);
321 }
322
323 if( pUserProc->IsVirtual() && !isFixedClass ){
324 //仮想関数(オブジェクトメソッド)呼び出し
325 //pObj->func_table->func1
326 // ->func2
327 // ->func3
328
329 //mov edx,dword ptr[ecx]
330 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, 0, MOD_BASE );
331
332 i2 = pobj_c->GetFuncNumInVtbl( pUserProc );
333
334 //call dword ptr[edx+func_index]
335 if(i2*PTR_SIZE<=0x7F){
336 compiler.codeGenerator.PutOld(
337 (char)0xFF,
338 (char)0x52,
339 (char)(i2*PTR_SIZE)
340 );
341 }
342 else{
343 compiler.codeGenerator.PutOld(
344 (char)0xFF,
345 (char)0x92
346 );
347 compiler.codeGenerator.PutOld( (long)(i2*PTR_SIZE), Schedule::None );
348 }
349 }
350 else{
351 //通常呼び出し
352
353 //call ProcAddr
354 compiler.codeGenerator.op_call(pUserProc);
355 }
356
357 if(pUserProc->IsCdecl()){
358 //add esp,ParmSize
359 compiler.codeGenerator.op_add_esp(ParmSize);
360 }
361
362 //一時オブジェクトを破棄
363 pobj_parameter->DeleteTempParameters();
364
365 //パラメータオブジェクトを破棄
366 delete pobj_parameter;
367
368 return true;
369}
370
371bool Opcode_CallDllProc( const char *lpszParms, const DllProc *pDllProc ){
372
373 extern BOOL bDebugCompile;
374 extern BOOL bDebugSupportProc;
375 if(bDebugCompile&&bDebugSupportProc==0&& pDllProc->IsEqualSymbol( "DebugBreak" ) ){
376 Call_DebugSys_SaveContext();
377 }
378
379
380 ////////////////////////
381 // パラメータのセット
382 ////////////////////////
383
384 //パラメータオブジェクトを生成
385 ParamImpl *pobj_parameter=0;
386 pobj_parameter=new ParamImpl(lpszParms);
387
388 // デフォルト引数を適用
389 pobj_parameter->ApplyDefaultParameters( pDllProc->Params() );
390
391 //エラーチェック
392 if( !pobj_parameter->ErrorCheck( pDllProc->GetName(), pDllProc->Params() ) ){
393 //パラメータにエラーがあるときは処理を終える
394 return false;
395 }
396
397 //一時オブジェクトを生成
398 pobj_parameter->NewTempParameters( pDllProc->GetName(), pDllProc->Params() );
399
400 //レジスタ、スタックフレームにセット
401 int ParmSize = pobj_parameter->SetParameter(pDllProc->GetName(), pDllProc->Params() );
402
403
404 //動的リンクされたプロシージャの呼び出し
405
406 //call dword ptr[LookupTable]
407 compiler.codeGenerator.op_call( pDllProc );
408
409 if(pDllProc->IsCdecl()){
410 //add esp,ParmSize
411 compiler.codeGenerator.op_add_esp(ParmSize);
412 }
413
414 //一時オブジェクトを破棄
415 pobj_parameter->DeleteTempParameters();
416
417 //パラメータオブジェクトを破棄
418 delete pobj_parameter;
419
420 return true;
421}
Note: See TracBrowser for help on using the repository browser.