source: dev/trunk/abdev/BasicCompiler32/CParameter.cpp@ 301

Last change on this file since 301 was 301, checked in by dai_9181, 17 years ago
File size: 9.6 KB
Line 
1#include "stdafx.h"
2
3#include <Compiler.h>
4
5#include "../BasicCompiler_Common/common.h"
6#include "opcode.h"
7
8int ParamImpl::NewTempParameters( const string &procName, const Parameters &params, int SecondParmNum ){
9 if( SecondParmNum == -1 ) SecondParmNum = (int)params.size();
10
11 ///////////////////////////////////////////////////////
12 // 一時オブジェクトをあらかじめスタックに積んでおく
13 ///////////////////////////////////////////////////////
14
15 useTempObject = false;
16
17 //一時参照の数
18 nCountOfTempObjects = 0;
19
20 BOOL bEllipse;
21 if(params.size()){
22 if(params[params.size()-1]->GetBasicType()==DEF_ELLIPSE) bEllipse=1;
23 else bEllipse=0;
24 }
25 else bEllipse=0;
26
27 for(int i2=ParmsNum-1;i2>=0;i2--){
28 useTempParameters[i2] = false;
29
30 if(bEllipse&&i2<=(int)params.size()-2) bEllipse=0;
31
32 if(i2==0){
33 if( params[i2]->GetVarName() == "_System_LocalThis" ){
34 //オブジェクトメンバの第一パラメータのThisポインタ
35 continue;
36 }
37 }
38 if( i2==0||i2==1 ){
39 if( params[i2]->GetVarName() == procName ){
40 //オブジェクトメンバの第一または第二パラメータの戻り値用オブジェクト
41 continue;
42 }
43 }
44
45 Type dummyType;
46 BOOL bByVal;
47 if(bEllipse){
48 NumOpe_GetType( Parms[i2], Type(), dummyType );
49 bByVal=1;
50 }
51 else{
52 dummyType = *params[i2];
53 bByVal = ( params[i2]->IsRef() == false ) ? TRUE:FALSE;
54 }
55
56
57 if( !bByVal ){
58 //ポインタ参照
59 if(Parms[i2][0]==1&&Parms[i2][1]==ESC_BYVAL){
60 //ポインタ指定
61 continue;
62 }
63
64 if( !GetVarType( Parms[i2], Type(), FALSE ) ){
65 //変数ではないとき
66 Type calcType;
67 NumOpe( Parms[i2], dummyType, calcType );
68 //↑ここでスタックに積む
69
70 nCountOfTempObjects++;
71
72 if( !calcType.IsStruct() ){
73 //一時参照を作成
74
75 //push esp
76 compiler.codeGenerator.op_push( REG_ESP );
77
78 nCountOfTempObjects++;
79 }
80
81 bool result = CheckDifferentType(
82 dummyType,
83 calcType,
84 procName.c_str(),
85 i2);
86
87 if( result ){
88 useTempParameters[i2] = true;
89 useTempObject = true;
90
91 types[i2] = calcType;
92 }
93 }
94 }
95 }
96
97 return nCountOfTempObjects * PTR_SIZE;
98}
99
100void ParamImpl::DeleteTempParameters(){
101 ///////////////////////////////////////////////////////
102 // 一時オブジェクトを破棄
103 ///////////////////////////////////////////////////////
104 if( !useTempObject ) return;
105
106 for(int i2=ParmsNum-1;i2>=0;i2--){
107 if( useTempParameters[i2] ){
108 if( types[i2].IsStruct() ){
109 // 構造体の一時メモリ
110
111 //メモリを解放する
112
113 //call free
114 extern const UserProc *pSub_free;
115 compiler.codeGenerator.op_call(pSub_free);
116 }
117 else{
118 if( types[i2].Is64() ){
119 //pop ... 参照を消す
120 //pop ... 上位32ビット
121 //pop ... 下位32ビット
122 compiler.codeGenerator.op_add_esp( PTR_SIZE * 3 );
123 }
124 else{
125 //pop ... 参照を消す
126 //pop ... 値を消す
127 compiler.codeGenerator.op_add_esp( PTR_SIZE * 2 );
128 }
129 }
130 }
131 }
132}
133
134void ParamImpl::SetStructParameter( const Type &baseType, const char *expression ){
135 int object_size = baseType.GetClass().GetSize();
136
137 //push object_size
138 compiler.codeGenerator.op_push_V(object_size);
139
140 //call calloc
141 extern const UserProc *pSub_calloc;
142 compiler.codeGenerator.op_call(pSub_calloc);
143
144 //push eax(ここでプッシュされた値が実際にパラメータとして引き渡される)
145 compiler.codeGenerator.op_push(REG_EAX);
146
147 //push eax
148 compiler.codeGenerator.op_push(REG_EAX);
149
150 Type calcType;
151 BOOL bUseHeap;
152 NumOpe( expression,
153 baseType,
154 calcType,
155 &bUseHeap );
156
157 // ※スタックにある二つのデータ(コピー先、コピー元)の値を必要とする
158 SetStructVariable( baseType, calcType, bUseHeap );
159}
160
161int ParamImpl::SetParameter( const string &procName, const Parameters &params, int SecondParmNum, const UserProc *pUserProc ){
162 if( SecondParmNum == -1 ) SecondParmNum = (int)params.size();
163
164 ///////////////////////////////////////////////////////////
165 // パラメータをレジスタ及びスタックフレームにセット
166 ///////////////////////////////////////////////////////////
167 int i2,i3;
168
169 BOOL bEllipse;
170 if( params.size() ){
171 if(params[params.size()-1]->GetBasicType()==DEF_ELLIPSE) bEllipse=1;
172 else bEllipse=0;
173 }
174 else bEllipse=0;
175
176 BOOL bHas_System_LocalThis=0;
177 if(ParmsNum>=1){
178 if( params[0]->GetVarName() == "_System_LocalThis" ){
179 bHas_System_LocalThis=1;
180 }
181 }
182
183 //戻り値用の変数名を取得
184 const char *lpszVarNameToReturn = (procName[0]==1&&procName[1]==ESC_OPERATOR)?"_System_ReturnValue":procName.c_str();
185
186 //パラメータをレジスタとスタックに格納
187 int ParmSize=0;
188 RELATIVE_VAR RelativeVar;
189 int nCountOfNowTempObjects = 0;
190 for(i2=ParmsNum-1;i2>=0;i2--){
191 if(bEllipse&&i2<=(int)params.size()-2) bEllipse=0;
192
193 if(i2==0){
194 if( params[i2]->GetVarName() == "_System_LocalThis" ){
195 //オブジェクトメンバの第一パラメータのThisポインタ
196 continue;
197 }
198 }
199 if(i2==0||i2==1){
200 if( params[i2]->GetVarName() == lpszVarNameToReturn ){
201 //オブジェクトメンバの第一または第二パラメータの戻り値用オブジェクト
202 continue;
203 }
204 }
205
206 Type dummyType;
207 BOOL bByVal;
208 if(bEllipse){
209 NumOpe_GetType( Parms[i2], Type(), dummyType );
210 bByVal=1;
211 }
212 else{
213 dummyType = *params[i2];
214 bByVal = ( params[i2]->IsRef() == false ) ? TRUE:FALSE;
215
216 // 型パラメータを解決
217 ResolveFormalGenericTypeParameter( dummyType, leftType, pUserProc );
218 }
219
220 if(bByVal==1){
221 //値参照
222/*
223 if(Parms[i2][0]==1&&Parms[i2][1]==ESC_BYVAL){
224 char temp2[255];
225 sprintf(temp2,"%s関数の第%dパラメータ",procName.c_str(),i2+1);
226 SetError(19,temp2,cp);
227 continue;
228 }
229*/
230 if( dummyType.IsStruct() ){
231 SetStructParameter( dummyType, Parms[i2] );
232 goto next;
233 }
234
235 extern LONG_PTR ProcPtr_BaseIndex;
236 LONG_PTR back_ProcPtr_BaseIndex = ProcPtr_BaseIndex;
237 if( dummyType.IsProcPtr() ){
238 ProcPtr_BaseIndex = dummyType.GetIndex();
239 }
240 else{
241 ProcPtr_BaseIndex=-1;
242 }
243
244 BOOL bCalcUseHeap;
245 Type calcType;
246 if( !NumOpe( Parms[i2], dummyType, calcType, &bCalcUseHeap ) ){
247 break;
248 }
249
250 ProcPtr_BaseIndex=back_ProcPtr_BaseIndex;
251
252 if( calcType.IsObject() ){
253 if( !dummyType.IsObject()
254 ||
255 dummyType.IsObject() &&
256 !dummyType.GetClass().IsEqualsOrSubClass( &calcType.GetClass() ) ){
257 //キャスト演算子のオーバーロードに対応する
258 CallCastOperatorProc( calcType, bCalcUseHeap,dummyType );
259 }
260 }
261
262 if(!bEllipse){
263 //型チェック
264 // TODO: _System_ReturnValueが考慮されていない?
265 if(bHas_System_LocalThis) i3=i2-1;
266 else i3=i2;
267 CheckDifferentType(
268 dummyType,
269 calcType,
270 procName.c_str(),
271 i3);
272 }
273
274 if( dummyType.IsDouble() ){
275 ChangeTypeToDouble( calcType.GetBasicType() );
276 ParmSize+=sizeof(long)*2;
277 }
278 else if( dummyType.IsSingle() ){
279 ChangeTypeToSingle( calcType.GetBasicType() );
280 ParmSize+=sizeof(long);
281 }
282 else if( dummyType.Is64() ){
283 ChangeTypeToInt64( calcType.GetBasicType() );
284 ParmSize+=sizeof(long)*2;
285 }
286 else if( dummyType.IsLong() || dummyType.IsDWord()
287 || dummyType.IsPointer()
288 || dummyType.IsObject() || dummyType.IsStruct() ){
289 ChangeTypeToLong( calcType.GetBasicType() );
290 ParmSize+=sizeof(long);
291 }
292 else if( dummyType.IsInteger() || dummyType.IsWord() ){
293 ChangeTypeToInteger( calcType.GetBasicType() );
294 ParmSize+=sizeof(long);
295 }
296 else if( dummyType.IsSByte() || dummyType.IsByte() || dummyType.IsBoolean() ){
297 ChangeTypeToByte( calcType.GetBasicType() );
298 ParmSize+=sizeof(long);
299 }
300 else{
301 SetError(300,NULL,cp);
302 }
303 }
304 else{
305 //ポインタ参照
306 if(Parms[i2][0]==1&&Parms[i2][1]==ESC_BYVAL){
307 //ポインタ指定
308
309 Type calcType;
310 if( !NumOpe( Parms[i2]+2, dummyType, calcType) ){
311 break;
312 }
313
314 ChangeTypeToLong( calcType.GetBasicType() );
315
316 dummyType.PtrLevelUp();
317
318 //型チェック
319 if(bHas_System_LocalThis) i3=i2-1;
320 else i3=i2;
321 CheckDifferentType(
322 dummyType,
323 calcType,
324 procName.c_str(),
325 i3);
326 }
327 else{
328 if( useTempParameters[i2] ){
329 //一時オブジェクトをコピー
330
331 if( !types[i2].IsStruct() ){
332 // 一時参照のための領域を考慮する
333 nCountOfNowTempObjects++;
334 }
335
336 nCountOfNowTempObjects++;
337
338 //mov eax, dword ptr[esp+offset]
339 compiler.codeGenerator.op_mov_RM(
340 sizeof(long),
341 REG_EAX,
342 REG_ESP,
343 ( ( ParmsNum - i2 - 1 ) + ( nCountOfTempObjects - nCountOfNowTempObjects ) ) * PTR_SIZE,
344 MOD_BASE_DISP32 );
345
346 //push eax
347 compiler.codeGenerator.op_push(REG_EAX);
348 }
349 else{
350 //変数のアドレスを取得
351 Type varType;
352 if(GetVarOffset(
353 false,
354 false,
355 Parms[i2],
356 &RelativeVar,
357 varType)){
358 if( !dummyType.IsAny() ){
359 //型チェックを行う
360 if( dummyType.GetBasicType() == varType.GetBasicType() ){
361 if( dummyType.IsObject() ){
362 if( !dummyType.GetClass().IsEqualsOrSubClass( &varType.GetClass() ) ){
363 SetError(11,Parms[i2],cp);
364 }
365 }
366 else if( dummyType.IsStruct() ){
367 if( !dummyType.GetClass().IsEquals( &varType.GetClass() ) ){
368 SetError(11,Parms[i2],cp);
369 }
370 }
371 }
372 else if( (varType.GetBasicType()&FLAG_PTR)
373 &&((varType.GetBasicType()^FLAG_PTR)==dummyType.GetBasicType())){
374 //仮引数がポインタ参照で、実引数が配列の先頭ポインタのとき
375 }
376 else{
377 SetError(11,Parms[i2],cp);
378 }
379 }
380
381 //変数アドレスをレジスタにセット
382 SetVarPtrToEax(&RelativeVar);
383
384 //push eax
385 compiler.codeGenerator.op_push(REG_EAX);
386 }
387 }
388 }
389
390 ParmSize+=PTR_SIZE;
391 }
392
393next:;
394 }
395
396 return ParmSize;
397}
Note: See TracBrowser for help on using the repository browser.