source: dev/BasicCompiler32/increment.cpp@ 8

Last change on this file since 8 was 3, checked in by dai_9181, 18 years ago
File size: 9.3 KB
Line 
1#include "../BasicCompiler_Common/common.h"
2#include "Opcode.h"
3
4
5void SetRealVariable(int type,RELATIVE_VAR *pRelativeVar){
6 if(pRelativeVar->dwKind==VAR_GLOBAL){
7 if(pRelativeVar->bOffsetOffset){
8 //fstp ptr[ecx+offset]
9 op_fstp_base_offset(type,REG_ECX,(int)pRelativeVar->offset);
10 obp-=sizeof(long);
11 pobj_GlobalVarSchedule->add();
12 obp+=sizeof(long);
13 }
14 else{
15 //mov ecx,offset
16 op_mov_RV(REG_ECX,(int)pRelativeVar->offset);
17 obp-=sizeof(long);
18 pobj_GlobalVarSchedule->add();
19 obp+=sizeof(long);
20
21 //fstp ptr[ecx]
22 op_fstp_basereg(type,REG_ECX);
23 }
24 }
25 else if(pRelativeVar->dwKind==VAR_LOCAL){
26 if(pRelativeVar->bOffsetOffset){
27 //fstp ptr[ebp+ecx+offset]
28 op_fstp_base_offset_ex(type,REG_EBP,REG_ECX,(int)pRelativeVar->offset,USE_OFFSET);
29 obp-=sizeof(long);
30 AddLocalVarAddrSchedule();
31 obp+=sizeof(long);
32 }
33 else{
34 //fstp ptr[ebp+offset]
35 op_fstp_base_offset(type,REG_EBP,(int)pRelativeVar->offset);
36 obp-=sizeof(long);
37 AddLocalVarAddrSchedule();
38 obp+=sizeof(long);
39 }
40 }
41 else if(pRelativeVar->dwKind==VAR_REFLOCAL){
42 if(pRelativeVar->bOffsetOffset){
43 //add ecx,qword ptr[ebp+offset]
44 op_add_RM(sizeof(long),REG_ECX,REG_EBP,(int)pRelativeVar->offset,MOD_BASE_DISP32);
45 obp-=sizeof(long);
46 AddLocalVarAddrSchedule();
47 obp+=sizeof(long);
48 }
49 else{
50 //mov ecx,qword ptr[ebp+offset]
51 op_mov_RM(sizeof(long),REG_ECX,REG_EBP,(int)pRelativeVar->offset,MOD_BASE_DISP32);
52 obp-=sizeof(long);
53 AddLocalVarAddrSchedule();
54 obp+=sizeof(long);
55 }
56
57 goto directmem;
58 }
59 else if(pRelativeVar->dwKind==VAR_DIRECTMEM){
60directmem:
61 //fstp ptr[ecx]
62 op_fstp_basereg(type,REG_ECX);
63 }
64}
65
66void ExtendTypeTo32(int type,int reg);
67void ExtendTypeTo64(int type){
68 if(Is64Type(type)) return;
69
70 ExtendTypeTo32(type,REG_EAX);
71
72 if(IsSignedType(type)){
73 //cdq
74 op_cdq();
75 }
76 else{
77 //xor edx,edx
78 op_zero_reg(REG_EDX);
79 }
80}
81void ExtendTypeTo32(int type,int reg){
82 if(type==DEF_INTEGER){
83 //movsx reg32,reg16
84 op_movsx_R32R16(reg,reg);
85 }
86 else if(type==DEF_WORD){
87 //and reg,0000FFFFh
88 op_and_RV(reg,(int)0x0000FFFF);
89 }
90 else if(type==DEF_CHAR){
91 //movsx reg32,reg8
92 op_movsx_R32R8(reg,reg);
93 }
94 else if(type==DEF_BYTE){
95 //and reg,000000FFh
96 op_and_RV(reg,(int)0xFF);
97 }
98}
99void ExtendTypeTo16(int type,int reg){
100 if(type==DEF_CHAR){
101 //movsx reg16,reg8
102 op_movsx_R16R8(reg,reg);
103 }
104 else if(type==DEF_BYTE){
105 //and reg,000000FFh
106 op_and_RV(reg,(int)0xFF);
107 }
108}
109
110void SetWholeVariable(int var_size,int type,RELATIVE_VAR *pRelative,int reg){
111 if(type==DEF_DOUBLE){
112 //Double型
113
114 }
115 else if(type==DEF_SINGLE){
116 //Single型
117
118 }
119 else{
120 //その他の整数
121
122 if(var_size==sizeof(_int64)){
123 //eaxの値を64ビット(edx:eax)に拡張する
124 ExtendTypeTo64(type);
125 }
126 else if(var_size==sizeof(long)){
127 //レジスタの値を32ビット(eax)に拡張する
128 ExtendTypeTo32(type,reg);
129 }
130 else if(var_size==sizeof(short)){
131 //レジスタの値を16ビット(ax)に拡張する
132 ExtendTypeTo16(type,reg);
133 }
134 //8ビットは拡張なし
135 }
136
137 if(var_size==sizeof(_int64)){
138 //下位32ビット
139 SetWholeVariable(sizeof(long),DEF_LONG,pRelative,REG_EAX);
140
141 //上位32ビット
142 pRelative->offset+=sizeof(long);
143 SetWholeVariable(sizeof(long),DEF_LONG,pRelative,REG_EDX);
144 pRelative->offset-=sizeof(long);
145
146 return;
147 }
148
149 if(pRelative->dwKind==VAR_GLOBAL){
150 if(pRelative->bOffsetOffset){
151 //mov ptr[ecx+offset],eax/ax/al
152 op_mov_MR(var_size,reg,REG_ECX,(int)pRelative->offset,MOD_BASE_DISP32);
153 obp-=sizeof(long);
154 pobj_GlobalVarSchedule->add();
155 obp+=sizeof(long);
156 }
157 else{
158 //mov ptr[offset],eax/ax/al
159 op_mov_MR(var_size,reg,0,(int)pRelative->offset,MOD_DISP32);
160 obp-=sizeof(long);
161 pobj_GlobalVarSchedule->add();
162 obp+=sizeof(long);
163 }
164 }
165 else if(pRelative->dwKind==VAR_LOCAL){
166 if(pRelative->bOffsetOffset){
167 //mov ptr[ebp+ecx+offset],eax/ax/al
168 op_mov_MR_ex(var_size,reg,REG_EBP,REG_ECX,(int)pRelative->offset,USE_OFFSET);
169 obp-=sizeof(long);
170 AddLocalVarAddrSchedule();
171 obp+=sizeof(long);
172 }
173 else{
174 //mov ptr[ebp+offset],eax/ax/al
175 op_mov_MR(var_size,reg,REG_EBP,(int)pRelative->offset,MOD_BASE_DISP32);
176 obp-=sizeof(long);
177 AddLocalVarAddrSchedule();
178 obp+=sizeof(long);
179 }
180 }
181 else if(pRelative->dwKind==VAR_REFLOCAL){
182 if(pRelative->bOffsetOffset){
183 //add ecx,qword ptr[ebp+offset]
184 op_add_RM(var_size,REG_ECX,REG_EBP,(int)pRelative->offset,MOD_BASE_DISP32);
185 obp-=sizeof(long);
186 AddLocalVarAddrSchedule();
187 obp+=sizeof(long);
188 }
189 else{
190 //mov ecx,qword ptr[ebp+offset]
191 op_mov_RM(var_size,REG_ECX,REG_EBP,(int)pRelative->offset,MOD_BASE_DISP32);
192 obp-=sizeof(long);
193 AddLocalVarAddrSchedule();
194 obp+=sizeof(long);
195 }
196
197 goto directmem;
198 }
199 else if(pRelative->dwKind==VAR_DIRECTMEM){
200directmem:
201
202 //mov ptr[ecx],eax/ax/al
203 op_mov_MR(var_size,reg,REG_ECX,0,MOD_BASE);
204 }
205}
206
207void SetVariableFromReg(int VarType,int CalcType,RELATIVE_VAR *pRelativeVar){
208 /////////////////////////////////////////////////
209 // raxの内容を変数にコピーするコードを抽出
210 /////////////////////////////////////////////////
211
212 if(VarType==DEF_DOUBLE){
213 //Double型変数へスタックの内容を格納する
214 SetRealVariable(VarType,pRelativeVar);
215 }
216 else if(VarType==DEF_SINGLE){
217 //Single型変数へスタックの内容を格納する
218 SetRealVariable(VarType,pRelativeVar);
219 }
220 else{
221 //整数型
222 SetWholeVariable(GetTypeSize(VarType,-1),CalcType,pRelativeVar,REG_EAX);
223 }
224}
225
226
227void IncDec(int idCalc, char *lpszLeft, char *lpszRight){
228 int VarType;
229 LONG_PTR lpVarIndex;
230 RELATIVE_VAR VarRelativeVar;
231
232
233 ///////////////////////////
234 // 変数アドレスを取得
235 ///////////////////////////
236
237 if(!GetVarOffset(
238 1,
239 lpszLeft,
240 &VarType,
241 &VarRelativeVar,
242 &lpVarIndex)) return;
243
244 if(IsUse_ecx(&VarRelativeVar)){
245 //push ecx
246 op_push(REG_ECX);
247 }
248
249
250 ///////////////////////////////////
251 // レジスタへ変数の内容をコピー
252 ///////////////////////////////////
253
254 if(IsRealNumberType(VarType)){
255 //実数
256 SetReg_RealVariable(VarType,&VarRelativeVar);
257 }
258 else{
259 //整数
260 SetReg_WholeVariable(VarType,&VarRelativeVar,REG_EAX);
261 }
262
263
264 if(IsWholeNumberType(VarType)&&lstrcmp(lpszRight,"1")==0&&
265 (idCalc==CALC_ADDITION||idCalc==CALC_SUBTRACTION)){
266 ////////////////////////////////////////////
267 // 整数型のインクリメント・デクリメント
268 ////////////////////////////////////////////
269
270 if(Is64Type(VarType)){
271 if(idCalc==CALC_ADDITION){
272 //64ビット インクリメント
273
274 //add eax,1
275 op_add_RV8(REG_EAX,1);
276
277 //adc edx,0
278 op_adc_RV8(REG_EDX,0);
279 }
280 else if(idCalc==CALC_SUBTRACTION){
281 //64ビット デクリメント
282
283 //sub eax,1
284 op_sub_RV8(REG_EAX,1);
285
286 //sbb edx,0
287 op_sbb_RV8(REG_EDX,0);
288 }
289 }
290 else{
291 if(idCalc==CALC_ADDITION){
292 //インクリメント
293 op_inc(REG_EAX);
294 }
295 else if(idCalc==CALC_SUBTRACTION){
296 //デクリメント
297 op_dec(REG_EAX);
298 }
299 }
300 }
301 else{
302 //変数オフセットを一時退避
303 //push ecx
304 op_push(REG_ECX);
305
306
307 if(VarType==DEF_DOUBLE){
308 //sub esp,8
309 op_sub_esp(8);
310
311 //fstp qword ptr[esp]
312 op_fstp_basereg(VarType,REG_ESP);
313 }
314 else if(VarType==DEF_SINGLE){
315 //sub esp,4
316 op_sub_esp(4);
317
318 //fstp dword ptr[esp]
319 op_fstp_basereg(VarType,REG_ESP);
320 }
321 else if(Is64Type(VarType)){
322 //push edx
323 op_push(REG_EDX);
324
325 //push eax
326 op_push(REG_EAX);
327 }
328 else{
329 //push eax
330 op_push(REG_EAX);
331 }
332
333 int CalcType;
334 CalcType=NumOpe(lpszRight,VarType,lpVarIndex,0);
335
336 if(VarType==DEF_DOUBLE) ChangeTypeToDouble(CalcType);
337 else if(VarType==DEF_SINGLE) ChangeTypeToSingle(CalcType);
338 else ChangeTypeToWhole(CalcType,VarType);
339
340 int type[255],sp;
341 LONG_PTR index_stack[255];
342 type[0]=VarType;
343 type[1]=VarType;
344 index_stack[0]=lpVarIndex;
345 index_stack[1]=lpVarIndex;
346 sp=2;
347
348 switch(idCalc){
349 case CALC_XOR:
350 Calc_Xor(type,index_stack,&sp);
351 break;
352 case CALC_OR:
353 Calc_Or(type,index_stack,&sp);
354 break;
355 case CALC_AND:
356 Calc_And(type,index_stack,&sp);
357 break;
358 case CALC_SHL:
359 Calc_SHL(type,&sp);
360 break;
361 case CALC_SHR:
362 Calc_SHR(type,&sp);
363 break;
364 case CALC_ADDITION:
365 case CALC_SUBTRACTION:
366 case CALC_PRODUCT:
367 CalcTwoTerm_Arithmetic(idCalc,type,index_stack,&sp);
368 break;
369 case CALC_MOD:
370 Calc_Mod(type,&sp);
371 break;
372 case CALC_QUOTIENT:
373 Calc_Divide(type,&sp,VarType);
374 break;
375 case CALC_INTQUOTIENT:
376 Calc_IntDivide(type,index_stack,&sp);
377 break;
378 case CALC_POWER:
379 Calc_Power(type,&sp);
380 break;
381 }
382
383
384 if(VarType==DEF_DOUBLE){
385 //fld qword ptr[esp]
386 op_fld_basereg(VarType,REG_ESP);
387
388 //add esp,8
389 op_add_esp(8);
390 }
391 else if(VarType==DEF_SINGLE){
392 //fld dword ptr[esp]
393 op_fld_basereg(VarType,REG_ESP);
394
395 //add esp,4
396 op_add_esp(4);
397 }
398 else if(Is64Type(VarType)){
399 //pop eax
400 op_pop(REG_EAX);
401
402 //pop edx
403 op_pop(REG_EDX);
404 }
405 else{
406 //pop eax
407 op_pop(REG_EAX);
408 }
409
410
411 //変数オフセットを復元
412 //pop ecx
413 op_pop(REG_ECX);
414 }
415
416
417 /////////////////////////////////////////////////
418 // レジスタの内容を変数にコピー
419 /////////////////////////////////////////////////
420
421 if(IsUse_ecx(&VarRelativeVar)){
422 //pop ecx
423 op_pop(REG_ECX);
424 }
425
426 SetVariableFromReg(VarType,VarType,&VarRelativeVar);
427}
Note: See TracBrowser for help on using the repository browser.