source: dev/BasicCompiler32/Compile_Set_Var.cpp@ 97

Last change on this file since 97 was 96, checked in by dai_9181, 18 years ago

不要なソースコード(TODO: 消す)を削除

File size: 8.4 KB
Line 
1#include "../BasicCompiler_Common/common.h"
2#include "Opcode.h"
3
4BOOL IsUse_ecx(RELATIVE_VAR *pRelativeVar){
5 if(pRelativeVar->bOffsetOffset||pRelativeVar->dwKind==VAR_DIRECTMEM) return 1;
6 return 0;
7}
8
9void SetStructVariable( const Type &varType, const Type &calcType, BOOL bUseHeap){
10 if( calcType.IsStruct() ){
11 if( varType.GetClass().IsEquals( &calcType.GetClass() ) ){ //等しい
12
13 //双方のオブジェクト型が一致、または派生・継承関係にあるとき
14 //※コピーを行う
15
16 int object_size = varType.GetClass().GetSize();
17
18 //mov ecx,object_size
19 op_mov_RV(REG_ECX,object_size);
20
21 //pop esi
22 op_pop(REG_ESI);
23
24 //pop edi
25 op_pop(REG_EDI);
26
27 if(bUseHeap){
28 //mov eax,esi
29 op_mov_RR(REG_EAX,REG_ESI);
30 }
31
32 //rep movs byte ptr[edi],byte ptr[esi]
33 op_rep_movs(sizeof(BYTE));
34
35 if(bUseHeap){
36 //push eax
37 op_push(REG_EAX);
38
39 //call free
40 extern UserProc *pSub_free;
41 op_call(pSub_free);
42 }
43
44 return;
45 }
46 }
47
48 SetError(1,NULL,cp);
49}
50
51
52void SetRealVariable(int VarType,int CalcType,RELATIVE_VAR *pRelativeVar){
53 if( !IsRealNumberType( CalcType ) ){
54 // 実数へ変換
55 // 64bit edx:eax -> st(0)
56 // 32bit eax -> st(0)
57
58 if( Is64Type( CalcType ) ){
59 //64ビット整数型
60
61 //push edx
62 op_push( REG_EDX );
63
64 //push eax
65 op_push( REG_EAX );
66
67 //fild qword ptr[esp]
68 op_fld_ptr_esp(DEF_INT64);
69
70 //pop
71 op_pop( REG_NON );
72
73 //pop
74 op_pop( REG_NON );
75 }
76 else{
77 //push eax
78 op_push( REG_EAX );
79
80 //fild qword ptr[esp]
81 op_fld_ptr_esp(DEF_LONG);
82
83 //pop
84 op_pop( REG_NON );
85 }
86 }
87
88 if(pRelativeVar->dwKind==VAR_GLOBAL){
89 if(pRelativeVar->bOffsetOffset){
90 //fstp ptr[ecx+offset]
91 op_fstp_base_offset(VarType,REG_ECX,(int)pRelativeVar->offset);
92 obp-=sizeof(long);
93 pobj_GlobalVarSchedule->add();
94 obp+=sizeof(long);
95 }
96 else{
97 //mov ecx,offset
98 op_mov_RV(REG_ECX,(int)pRelativeVar->offset);
99 obp-=sizeof(long);
100 pobj_GlobalVarSchedule->add();
101 obp+=sizeof(long);
102
103 //fstp ptr[ecx]
104 op_fstp_basereg(VarType,REG_ECX);
105 }
106 }
107 else if(pRelativeVar->dwKind==VAR_REFGLOBAL){
108 if(pRelativeVar->bOffsetOffset){
109 //add ecx,qword ptr[offset]
110 op_add_RM(sizeof(long),REG_ECX,REG_NON,(int)pRelativeVar->offset,MOD_DISP32);
111 }
112 else{
113 //mov ecx,qword ptr[offset]
114 op_mov_RM(sizeof(long),REG_ECX,REG_NON,(int)pRelativeVar->offset,MOD_DISP32);
115 }
116 obp-=sizeof(long);
117 pobj_GlobalVarSchedule->add();
118 obp+=sizeof(long);
119
120 goto directmem;
121 }
122 else if(pRelativeVar->dwKind==VAR_LOCAL){
123 if(pRelativeVar->bOffsetOffset){
124 //fstp ptr[ebp+ecx+offset]
125 op_fstp_base_offset_ex(VarType,REG_EBP,REG_ECX,(int)pRelativeVar->offset,USE_OFFSET);
126 }
127 else{
128 //fstp ptr[ebp+offset]
129 op_fstp_base_offset(VarType,REG_EBP,(int)pRelativeVar->offset);
130 }
131 obp-=sizeof(long);
132 AddLocalVarAddrSchedule();
133 obp+=sizeof(long);
134 }
135 else if(pRelativeVar->dwKind==VAR_REFLOCAL){
136 if(pRelativeVar->bOffsetOffset){
137 //add ecx,qword ptr[ebp+offset]
138 op_add_RM(sizeof(long),REG_ECX,REG_EBP,(int)pRelativeVar->offset,MOD_BASE_DISP32);
139 }
140 else{
141 //mov ecx,qword ptr[ebp+offset]
142 op_mov_RM(sizeof(long),REG_ECX,REG_EBP,(int)pRelativeVar->offset,MOD_BASE_DISP32);
143 }
144 obp-=sizeof(long);
145 AddLocalVarAddrSchedule();
146 obp+=sizeof(long);
147
148 goto directmem;
149 }
150 else if(pRelativeVar->dwKind==VAR_DIRECTMEM){
151directmem:
152 //fstp ptr[ecx]
153 op_fstp_basereg(VarType,REG_ECX);
154 }
155}
156
157void SetBooleanVariable(int type,RELATIVE_VAR *pRelative){
158 if(type==DEF_DOUBLE){
159 // TODO: 実装
160 SetError();
161 }
162 else if(type==DEF_SINGLE){
163 // TODO: 実装
164 SetError();
165 }
166 else if(type==DEF_INT64||type==DEF_QWORD){
167 //cmp eax,0
168 op_cmp_value(GetTypeSize(type,-1),REG_EAX,0);
169
170 //setne al
171 op_setne( REG_EAX );
172
173 //cmp edx,0
174 op_cmp_value(GetTypeSize(type,-1),REG_EDX,0);
175
176 //setne cl
177 op_setne( REG_ECX );
178
179 //or al,cl
180 op_or_RR( sizeof( _int8 ), REG_EAX, REG_ECX );
181 }
182 else{
183 if(!IsWholeNumberType(type)){
184 //不正な型の場合
185 SetError(9,NULL,cp);
186 return;
187 }
188 }
189
190 //cmp eax,0
191 op_cmp_value(GetTypeSize(type,-1),REG_EAX,0);
192
193 //setne al
194 op_setne( REG_EAX );
195
196 SetWholeVariable( sizeof(char), DEF_BYTE, pRelative );
197}
198
199void ExtendTypeTo32(int type,int reg);
200void ExtendTypeTo64(int type){
201 if(Is64Type(type)) return;
202
203 ExtendTypeTo32(type,REG_EAX);
204
205 if(IsSignedType(type)){
206 //cdq
207 op_cdq();
208 }
209 else{
210 //xor edx,edx
211 op_zero_reg(REG_EDX);
212 }
213}
214void ExtendTypeTo32(int type,int reg){
215 if(type==DEF_INTEGER || (isUnicode&&type==DEF_CHAR)){
216 //movsx reg32,reg16
217 op_movsx_R32R16(reg,reg);
218 }
219 else if(type==DEF_WORD){
220 //and reg,0000FFFFh
221 op_and_RV(reg,(int)0x0000FFFF);
222 }
223 else if(type==DEF_SBYTE || (isUnicode==false&&type==DEF_CHAR)){
224 //movsx reg32,reg8
225 op_movsx_R32R8(reg,reg);
226 }
227 else if(type==DEF_BYTE||type==DEF_BOOLEAN){
228 //and reg,000000FFh
229 op_and_RV(reg,(int)0xFF);
230 }
231}
232void ExtendTypeTo16(int type,int reg){
233 if(type==DEF_SBYTE || (isUnicode==false&&type==DEF_CHAR)){
234 //movsx reg16,reg8
235 op_movsx_R16R8(reg,reg);
236 }
237 else if(type==DEF_BYTE||type==DEF_BOOLEAN){
238 //and reg,000000FFh
239 op_and_RV(reg,(int)0xFF);
240 }
241}
242
243void SetWholeVariable(int varSize,int calcType,RELATIVE_VAR *pRelative){
244 if( IsRealNumberType( calcType ) ){
245 // 実数型から整数型へ変換する
246
247 if( varSize == sizeof(_int64) ){
248 // 64bitへ
249 // st(0) -> edx:eax
250 breakpoint;
251
252 //push
253 //push
254 op_sub_esp( PTR_SIZE * 2 );
255
256 //fistp qword ptr[esp]
257 op_fistp_ptr_esp( sizeof(_int64) );
258
259 //pop eax
260 op_pop( REG_EAX );
261
262 //pop edx
263 op_pop( REG_EDX );
264 }
265 else{
266 // 32bit
267 // st(0) -> eax
268
269 //push
270 op_push( REG_NON );
271
272 //fistp dword ptr[esp]
273 op_fistp_ptr_esp( sizeof(long) );
274
275 //pop eax
276 op_pop( REG_EAX );
277 }
278 }
279 else{
280 //その他の整数
281
282 if(varSize==sizeof(_int64)){
283 //eaxの値を64ビット(edx:eax)に拡張する
284 ExtendTypeTo64(calcType);
285 }
286 else if(varSize==sizeof(long)){
287 //レジスタの値を32ビット(eax)に拡張する
288 ExtendTypeTo32(calcType,REG_EAX);
289 }
290 else if(varSize==sizeof(short)){
291 //レジスタの値を16ビット(ax)に拡張する
292 ExtendTypeTo16(calcType,REG_EAX);
293 }
294 //8ビットは拡張なし
295 }
296
297 if(varSize==sizeof(_int64)){
298 //下位32ビット
299 SetWholeVariable(sizeof(long),DEF_LONG,pRelative);
300
301 //上位32ビット
302
303 //直接参照に切り替え
304 SetVarPtrToEax(pRelative);
305 pRelative->dwKind=VAR_DIRECTMEM;
306
307 //mov ecx,eax
308 op_mov_RR( REG_ECX, REG_EAX );
309
310 //add ecx,sizeof(long)
311 op_add_RV8( REG_ECX, sizeof(long) );
312
313 //mov eax,edx
314 op_mov_RR( REG_EAX, REG_EDX );
315
316 SetWholeVariable(sizeof(long),DEF_LONG,pRelative);
317
318 return;
319 }
320
321 if(pRelative->dwKind==VAR_GLOBAL){
322 if(pRelative->bOffsetOffset){
323 //mov ptr[ecx+offset],eax/ax/al
324 op_mov_MR(varSize,REG_EAX,REG_ECX,(int)pRelative->offset,MOD_BASE_DISP32);
325 }
326 else{
327 //mov ptr[offset],eax/ax/al
328 op_mov_MR(varSize,REG_EAX,0,(int)pRelative->offset,MOD_DISP32);
329 }
330 obp-=sizeof(long);
331 pobj_GlobalVarSchedule->add();
332 obp+=sizeof(long);
333 }
334 else if(pRelative->dwKind==VAR_REFGLOBAL){
335 if(pRelative->bOffsetOffset){
336 //add ecx,qword ptr[offset]
337 op_add_RM(varSize,REG_ECX,REG_NON,(int)pRelative->offset,MOD_DISP32);
338 }
339 else{
340 //mov ecx,qword ptr[offset]
341 op_mov_RM(varSize,REG_ECX,REG_NON,(int)pRelative->offset,MOD_DISP32);
342 }
343 obp-=sizeof(long);
344 pobj_GlobalVarSchedule->add();
345 obp+=sizeof(long);
346
347 goto directmem;
348 }
349 else if(pRelative->dwKind==VAR_LOCAL){
350 if(pRelative->bOffsetOffset){
351 //mov ptr[ebp+ecx+offset],eax/ax/al
352 op_mov_MR_ex(varSize,REG_EAX,REG_EBP,REG_ECX,(int)pRelative->offset,USE_OFFSET);
353 }
354 else{
355 //mov ptr[ebp+offset],eax/ax/al
356 op_mov_MR(varSize,REG_EAX,REG_EBP,(int)pRelative->offset,MOD_BASE_DISP32);
357 }
358 obp-=sizeof(long);
359 AddLocalVarAddrSchedule();
360 obp+=sizeof(long);
361 }
362 else if(pRelative->dwKind==VAR_REFLOCAL){
363 if(pRelative->bOffsetOffset){
364 //add ecx,qword ptr[ebp+offset]
365 op_add_RM(varSize,REG_ECX,REG_EBP,(int)pRelative->offset,MOD_BASE_DISP32);
366 }
367 else{
368 //mov ecx,qword ptr[ebp+offset]
369 op_mov_RM(varSize,REG_ECX,REG_EBP,(int)pRelative->offset,MOD_BASE_DISP32);
370 }
371 obp-=sizeof(long);
372 AddLocalVarAddrSchedule();
373 obp+=sizeof(long);
374
375 goto directmem;
376 }
377 else if(pRelative->dwKind==VAR_DIRECTMEM){
378directmem:
379
380 //mov ptr[ecx],eax/ax/al
381 op_mov_MR(varSize,REG_EAX,REG_ECX,0,MOD_BASE);
382 }
383}
Note: See TracBrowser for help on using the repository browser.