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

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

関数の戻り値の構造体など、一時メモリに保持された構造体のメンバに直接アクセスした場合、その一時メモリの解放が正常に行われないバグを修正(まずは32bit版のみ)

File size: 15.2 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, isNeedHeapFreeStructure = false;
292 Type baseType( DEF_OBJECT, *pUserProc->GetParentClassPtr() ) , resultType;
293 if( !TermOpe( ObjectName, baseType, resultType, isLiteral, isNeedHeapFreeStructure, NULL, false, !pMethod->IsConst() ) )
294 {
295 return false;
296 }
297 if( !resultType.IsObject() )
298 {
299 SetError();
300 }
301
302 // 実態ポインタをeaxにコピー
303 compiler.codeGenerator.op_mov_RR( REG_ECX, REG_EAX );
304 }
305 }
306 else{
307InClassMember:
308 if(dwFlags&PROCFLAG_NEW){
309 //New演算子によるコンストラクタ呼び出しの場合
310
311 //mov ecx,dword ptr[esp+ParmSize]
312 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_ESP, ParmSize + tempSize, MOD_BASE_DISP32 );
313 }
314 else{
315 //Thisポインタをecxにコピー
316 SetThisPtrToReg(REG_ECX);
317 }
318 }
319 }
320
321 if( pUserProc->IsVirtual() && !isFixedClass )
322 {
323 int vtblIndex;
324 if( pobj_c->IsInterface() )
325 {
326 // インターフェイス メソッド呼び出し
327
328 int offset_vtbl = compiler.GetObjectModule().meta.GetClasses().GetInterfaceInfoClassPtr()->GetMemberOffset( "__vtbl" );
329
330
331 // vtblのポインタを取得
332 //mov edx,dword ptr[ecx+offset_vtbl]
333 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, offset_vtbl, MOD_BASE_DISP8 );
334
335 int offset_this = compiler.GetObjectModule().meta.GetClasses().GetInterfaceInfoClassPtr()->GetMemberOffset( "__this" );
336
337
338
339 // インターフェイスの場合は更に__thisを取得する
340 //mov ecx,qword ptr[ecx+offset_this]
341 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_ECX, offset_this, MOD_BASE_DISP8 );
342
343 int vtblMasterListIndex;
344 pobj_c->GetVtblMasterListIndexAndVtblIndex( pUserProc, vtblMasterListIndex, vtblIndex );
345 if( vtblMasterListIndex != 0 )
346 {
347 SetError();
348 }
349 }
350 else if( pobj_c->IsComInterface() )
351 {
352 // COMインターフェイス メソッド呼び出し
353
354 //仮想関数(オブジェクトメソッド)呼び出し
355 // pObj -> vtbl1 -> func1
356 // -> func2
357 // -> func3
358
359 int vtblMasterListIndex;
360 pobj_c->GetVtblMasterListIndexAndVtblIndex( pUserProc, vtblMasterListIndex, vtblIndex );
361
362 // vtblのポインタを取得
363 //mov edx,dword ptr[ecx]
364 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, 0, MOD_BASE );
365 }
366 else
367 {
368 //仮想関数(オブジェクトメソッド)呼び出し
369 // pObj -> vtbl_master_list -> vtbl1 -> func1
370 // -> func2
371 // -> func3
372 // -> vtbl2 -> func1
373 // -> func2
374 // -> func3
375
376 int vtblMasterListIndex;
377 pobj_c->GetVtblMasterListIndexAndVtblIndex( pUserProc, vtblMasterListIndex, vtblIndex );
378
379 // vtblマスターリストのポインタを取得
380 //mov edx,dword ptr[ecx+sizeof(com_vtbl)]
381 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, PTR_SIZE, MOD_BASE_DISP8 );
382
383 // vtblのポインタを取得
384 //mov edx,dword ptr[edx+vtblMasterListIndex]
385 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_EDX, vtblMasterListIndex*PTR_SIZE, MOD_BASE_DISP32 );
386 }
387
388 //push ecx
389 compiler.codeGenerator.op_push(REG_ECX);
390
391 //call dword ptr[edx+func_index]
392 if( vtblIndex * PTR_SIZE <= 0x7F )
393 {
394 compiler.codeGenerator.PutOld(
395 (char)0xFF,
396 (char)0x52,
397 (char)(vtblIndex*PTR_SIZE)
398 );
399 }
400 else{
401 compiler.codeGenerator.PutOld(
402 (char)0xFF,
403 (char)0x92
404 );
405 compiler.codeGenerator.PutOld( (long)(vtblIndex*PTR_SIZE), Schedule::None );
406 }
407 }
408 else{
409 //通常呼び出し
410
411 if( pUserProc->GetParentClassPtr() && isStatic == false )
412 {
413 //push ecx
414 compiler.codeGenerator.op_push(REG_ECX);
415 }
416
417 //call ProcAddr
418 compiler.codeGenerator.op_call(pUserProc);
419 }
420
421 if(pUserProc->IsCdecl()){
422 //add esp,ParmSize
423 compiler.codeGenerator.op_add_esp(ParmSize);
424 }
425
426 //一時オブジェクトを破棄
427 pobj_parameter->DeleteTempParameters();
428
429 //パラメータオブジェクトを破棄
430 delete pobj_parameter;
431
432 return true;
433}
434
435bool Opcode_CallDllProc( const char *lpszParms, const DllProc *pDllProc ){
436
437 extern BOOL bDebugCompile;
438 extern BOOL bDebugSupportProc;
439 if(bDebugCompile&&bDebugSupportProc==0&& pDllProc->IsEqualSymbol( "DebugBreak" ) ){
440 Call_DebugSys_SaveContext();
441 }
442
443
444 ////////////////////////
445 // パラメータのセット
446 ////////////////////////
447
448 //パラメータオブジェクトを生成
449 ParamImpl *pobj_parameter=0;
450 pobj_parameter=new ParamImpl(lpszParms);
451
452 // デフォルト引数を適用
453 pobj_parameter->ApplyDefaultParameters( pDllProc->Params() );
454
455 //エラーチェック
456 if( !pobj_parameter->ErrorCheck( pDllProc->GetName(), pDllProc->Params() ) ){
457 //パラメータにエラーがあるときは処理を終える
458 return false;
459 }
460
461 //一時オブジェクトを生成
462 pobj_parameter->NewTempParameters( pDllProc->GetName(), pDllProc->Params() );
463
464 //レジスタ、スタックフレームにセット
465 int ParmSize = pobj_parameter->SetParameter(pDllProc->GetName(), pDllProc->Params() );
466
467
468 //動的リンクされたプロシージャの呼び出し
469
470 //call dword ptr[LookupTable]
471 compiler.codeGenerator.op_call( pDllProc );
472
473 if(pDllProc->IsCdecl()){
474 //add esp,ParmSize
475 compiler.codeGenerator.op_add_esp(ParmSize);
476 }
477
478 //一時オブジェクトを破棄
479 pobj_parameter->DeleteTempParameters();
480
481 //パラメータオブジェクトを破棄
482 delete pobj_parameter;
483
484 return true;
485}
486
487void Opcode_CallDelegate( const Delegate &dg, const char *methodPtrValueStr, const char *objPtrValueStr, const char *params )
488{
489 extern BOOL bDebugCompile;
490 extern BOOL bDebugSupportProc;
491 if(bDebugCompile&&bDebugSupportProc==0)
492 Call_DebugSys_SaveContext();
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.