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

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

32bitコンパイラのリリース版で落ちてしまうバグを修正。スタック上の一時オブジェクトをクラス参照型パラメータに適用するのは危ない??

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