source: dev/BasicCompiler32/NumOpe_Logical.cpp@ 122

Last change on this file since 122 was 36, checked in by dai_9181, 18 years ago

Boolean型に対応。

File size: 9.3 KB
Line 
1#include "../BasicCompiler_Common/common.h"
2#include "Opcode.h"
3
4BOOL Calc_Xor(int *type,LONG_PTR *index_stack,int *pStackPointer){
5 //value[sp-2] xor= value[sp-1]
6 //xor演算
7
8 int sp;
9 sp=*pStackPointer;
10
11 if(IsRealNumberType(type[sp-2])||IsRealNumberType(type[sp-1])){
12 //いずれかの項が実数のとき
13 SetError(45,"xor",cp);
14 return 0;
15 }
16
17 if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD||
18 type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
19 ////////////////////
20 // 64ビット整数演算
21 ////////////////////
22
23 //第2項を64ビットに対応させる
24 if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
25 //第2項が64ビット整数値のとき
26
27 //pop ebx
28 op_pop(REG_EBX);
29
30 //pop ecx
31 op_pop(REG_ECX);
32 }
33 else{
34 //第2項がその他整数値のとき
35
36 //pop eax
37 op_pop(REG_EAX);
38
39 if(IsSignedType(type[sp-1])){
40 //符号拡張
41 //edx:eax ← eax
42
43 //cdq
44 op_cdq();
45 }
46 else{
47 //ビット拡張
48 //edx:eax ← eax
49
50 //xor edx,edx
51 op_zero_reg(REG_EDX);
52 }
53
54 //mov ebx,eax
55 OpBuffer[obp++]=(char)0x8B;
56 OpBuffer[obp++]=(char)0xD8;
57
58 //mov ecx,edx
59 OpBuffer[obp++]=(char)0x8B;
60 OpBuffer[obp++]=(char)0xCA;
61 }
62
63 if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
64 //第1項が64ビット整数値のとき
65
66 //xor dword ptr[esp],ebx
67 OpBuffer[obp++]=(char)0x31;
68 OpBuffer[obp++]=(char)0x1C;
69 OpBuffer[obp++]=(char)0x24;
70
71 //xor dword ptr[esp+sizeof(long)],ecx
72 OpBuffer[obp++]=(char)0x31;
73 OpBuffer[obp++]=(char)0x4C;
74 OpBuffer[obp++]=(char)0x24;
75 OpBuffer[obp++]=(char)0x04;
76 }
77 else{
78 //第1項がその他整数値のとき
79 if(IsSignedType(type[sp-2])){
80 //pop eax
81 op_pop(REG_EAX);
82
83 //符号拡張
84 //edx:eax ← eax
85
86 //cdq
87 op_cdq();
88 }
89 else{
90 //pop eax
91 op_pop(REG_EAX);
92
93 //ビット拡張
94 //edx:eax ← eax
95
96 //xor edx,edx
97 op_zero_reg(REG_EDX);
98 }
99
100 //xor ebx,eax
101 OpBuffer[obp++]=(char)0x33;
102 OpBuffer[obp++]=(char)0xD8;
103
104 //xor ecx,edx
105 OpBuffer[obp++]=(char)0x33;
106 OpBuffer[obp++]=(char)0xCA;
107
108 //push ecx
109 op_push(REG_ECX);
110
111 //push ebx
112 op_push(REG_EBX);
113 }
114
115 sp--;
116 if(type[sp-1]==DEF_QWORD&&type[sp]==DEF_QWORD) type[sp-1]=DEF_QWORD;
117 else type[sp-1]=DEF_INT64;
118 }
119 else{
120 ////////////////////
121 // 整数演算
122 ////////////////////
123
124 //pop ebx
125 op_pop(REG_EBX);
126
127 //pop eax
128 op_pop(REG_EAX);
129
130 //sub esp,4
131 op_sub_esp(4);
132
133 //xor eax,ebx
134 OpBuffer[obp++]=(char)0x33;
135 OpBuffer[obp++]=(char)0xC3;
136
137 //mov dword ptr[esp],eax
138 OpBuffer[obp++]=(char)0x89;
139 OpBuffer[obp++]=(char)0x04;
140 OpBuffer[obp++]=(char)0x24;
141
142 sp--;
143 type[sp-1]=NeutralizationType(type[sp-1],index_stack[sp-1],type[sp],index_stack[sp]);
144 }
145
146 *pStackPointer=sp;
147 return 1;
148}
149
150BOOL Calc_Or(int *type,LONG_PTR *index_stack,int *pStackPointer){
151 //value[sp-2] or= value[sp-1]
152 //or演算
153
154 int sp;
155 sp=*pStackPointer;
156
157 if(IsRealNumberType(type[sp-2])||IsRealNumberType(type[sp-1])){
158 //いずれかの項が実数のとき
159 SetError(45,"or",cp);
160 return 0;
161 }
162
163 if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD||
164 type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
165 ////////////////////
166 // 64ビット整数演算
167 ////////////////////
168
169 //第2項を64ビットに対応させる
170 if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
171 //第2項が64ビット整数値のとき
172
173 //pop ebx
174 op_pop(REG_EBX);
175
176 //pop ecx
177 op_pop(REG_ECX);
178 }
179 else{
180 //第2項が32ビット整数値のとき
181
182 //pop eax
183 op_pop(REG_EAX);
184
185 if(IsSignedType(type[sp-1])){
186 //符号拡張
187 //edx:eax ← eax
188
189 //cdq
190 op_cdq();
191 }
192 else{
193 //ビット拡張
194 //edx:eax ← eax
195
196 //xor edx,edx
197 op_zero_reg(REG_EDX);
198 }
199
200 //mov ebx,eax
201 OpBuffer[obp++]=(char)0x8B;
202 OpBuffer[obp++]=(char)0xD8;
203
204 //mov ecx,edx
205 OpBuffer[obp++]=(char)0x8B;
206 OpBuffer[obp++]=(char)0xCA;
207 }
208
209 if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
210 //第1項が64ビット整数値のとき
211
212 //or dword ptr[esp],ebx
213 OpBuffer[obp++]=(char)0x09;
214 OpBuffer[obp++]=(char)0x1C;
215 OpBuffer[obp++]=(char)0x24;
216
217 //or dword ptr[esp+sizeof(long)],ecx
218 OpBuffer[obp++]=(char)0x09;
219 OpBuffer[obp++]=(char)0x4C;
220 OpBuffer[obp++]=(char)0x24;
221 OpBuffer[obp++]=(char)0x04;
222 }
223 else{
224 //第1項が32ビット整数値のとき
225 if(IsSignedType(type[sp-2])){
226 //pop eax
227 op_pop(REG_EAX);
228
229 //符号拡張
230 //edx:eax ← eax
231
232 //cdq
233 op_cdq();
234 }
235 else{
236 //pop eax
237 op_pop(REG_EAX);
238
239 //ビット拡張
240 //edx:eax ← eax
241
242 //xor edx,edx
243 op_zero_reg(REG_EDX);
244 }
245
246 //or ebx,eax
247 OpBuffer[obp++]=(char)0x0B;
248 OpBuffer[obp++]=(char)0xD8;
249
250 //or ecx,edx
251 OpBuffer[obp++]=(char)0x0B;
252 OpBuffer[obp++]=(char)0xCA;
253
254 //push ecx
255 op_push(REG_ECX);
256
257 //push ebx
258 op_push(REG_EBX);
259 }
260
261 sp--;
262 if(type[sp-1]==DEF_QWORD&&type[sp]==DEF_QWORD) type[sp-1]=DEF_QWORD;
263 else type[sp-1]=DEF_INT64;
264 }
265 else{
266 ////////////////////
267 // 32ビット整数演算
268 ////////////////////
269
270 //pop ebx
271 op_pop(REG_EBX);
272
273 //pop eax
274 op_pop(REG_EAX);
275
276 //sub esp,4
277 op_sub_esp(4);
278
279 //or eax,ebx
280 OpBuffer[obp++]=(char)0x0B;
281 OpBuffer[obp++]=(char)0xC3;
282
283 //mov dword ptr[esp],eax
284 OpBuffer[obp++]=(char)0x89;
285 OpBuffer[obp++]=(char)0x04;
286 OpBuffer[obp++]=(char)0x24;
287
288 sp--;
289 type[sp-1]=NeutralizationType(type[sp-1],index_stack[sp-1],type[sp],index_stack[sp]);
290 }
291
292 *pStackPointer=sp;
293 return 1;
294}
295
296BOOL Calc_And(int *type,LONG_PTR *index_stack,int *pStackPointer){
297 //value[sp-2] and= value[sp-1]
298 //and演算
299
300 int sp;
301 sp=*pStackPointer;
302
303 if(IsRealNumberType(type[sp-2])||IsRealNumberType(type[sp-1])){
304 //いずれかの項が実数のとき
305 SetError(45,"and",cp);
306 return 0;
307 }
308
309 if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD||
310 type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
311 ////////////////////
312 // 64ビット整数演算
313 ////////////////////
314
315 //第2項を64ビットに対応させる
316 if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
317 //第2項が64ビット整数値のとき
318
319 //pop ebx
320 op_pop(REG_EBX);
321
322 //pop ecx
323 op_pop(REG_ECX);
324 }
325 else{
326 //第2項が32ビット整数値のとき
327
328 //pop eax
329 op_pop(REG_EAX);
330
331 if(IsSignedType(type[sp-1])){
332 //符号拡張
333 //edx:eax ← eax
334
335 //cdq
336 op_cdq();
337 }
338 else{
339 //ビット拡張
340 //edx:eax ← eax
341
342 //xor edx,edx
343 op_zero_reg(REG_EDX);
344 }
345
346 //mov ebx,eax
347 OpBuffer[obp++]=(char)0x8B;
348 OpBuffer[obp++]=(char)0xD8;
349
350 //mov ecx,edx
351 OpBuffer[obp++]=(char)0x8B;
352 OpBuffer[obp++]=(char)0xCA;
353 }
354
355 if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
356 //第1項が64ビット整数値のとき
357
358 //and dword ptr[esp],ebx
359 OpBuffer[obp++]=(char)0x21;
360 OpBuffer[obp++]=(char)0x1C;
361 OpBuffer[obp++]=(char)0x24;
362
363 //and dword ptr[esp+sizeof(long)],ecx
364 OpBuffer[obp++]=(char)0x21;
365 OpBuffer[obp++]=(char)0x4C;
366 OpBuffer[obp++]=(char)0x24;
367 OpBuffer[obp++]=(char)0x04;
368 }
369 else{
370 //第1項が32ビット整数値のとき
371 if(IsSignedType(type[sp-2])){
372 //pop eax
373 op_pop(REG_EAX);
374
375 //符号拡張
376 //edx:eax ← eax
377
378 //cdq
379 op_cdq();
380 }
381 else{
382 //pop eax
383 op_pop(REG_EAX);
384
385 //ビット拡張
386 //edx:eax ← eax
387
388 //xor edx,edx
389 op_zero_reg(REG_EDX);
390 }
391
392 //and ebx,eax
393 OpBuffer[obp++]=(char)0x23;
394 OpBuffer[obp++]=(char)0xD8;
395
396 //and ecx,edx
397 OpBuffer[obp++]=(char)0x23;
398 OpBuffer[obp++]=(char)0xCA;
399
400 //push ecx
401 op_push(REG_ECX);
402
403 //push ebx
404 op_push(REG_EBX);
405 }
406
407 sp--;
408 if(type[sp-1]==DEF_QWORD&&type[sp]==DEF_QWORD) type[sp-1]=DEF_QWORD;
409 else type[sp-1]=DEF_INT64;
410 }
411 else{
412 ////////////////////
413 // 32ビット整数演算
414 ////////////////////
415
416 //pop ebx
417 op_pop(REG_EBX);
418
419 //pop eax
420 op_pop(REG_EAX);
421
422 //sub esp,4
423 op_sub_esp(4);
424
425 //and eax,ebx
426 OpBuffer[obp++]=(char)0x23;
427 OpBuffer[obp++]=(char)0xC3;
428
429 //mov dword ptr[esp],eax
430 OpBuffer[obp++]=(char)0x89;
431 OpBuffer[obp++]=(char)0x04;
432 OpBuffer[obp++]=(char)0x24;
433
434 sp--;
435 type[sp-1]=NeutralizationType(type[sp-1],index_stack[sp-1],type[sp],index_stack[sp]);
436 }
437
438 *pStackPointer=sp;
439 return 1;
440}
441
442BOOL Calc_Not(int *type,int sp){
443 //value[sp-1]=Not value[sp-1]
444 //NOT演算子
445
446 if(IsRealNumberType(type[sp-1])){
447 //実数のとき
448 SetError(45,"Not",cp);
449 return 0;
450 }
451
452 if( type[sp - 1] == DEF_BOOLEAN ){
453 //pop eax
454 op_pop( REG_EAX );
455
456 //cmp eax,0
457 op_cmp_value(GetTypeSize(type[sp-1],-1),REG_EAX,0);
458
459 //setne al
460 op_setne( REG_EAX );
461
462 //and eax,000000FFh
463 op_and_RV(REG_EAX,(int)0xFF);
464
465 //neg
466 op_neg( REG_EAX );
467
468 //sbb eax, eax
469 op_sbb_RR( REG_EAX, REG_EAX );
470
471 //add eax, 1
472 op_add_RV8( REG_EAX, 1 );
473
474 //push eax
475 op_push( REG_EAX );
476 }
477 else if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
478 ////////////////////
479 // 64ビット整数演算
480 ////////////////////
481
482 //not dword ptr[esp]
483 OpBuffer[obp++]=(char)0xF7;
484 OpBuffer[obp++]=(char)0x14;
485 OpBuffer[obp++]=(char)0x24;
486
487 //not dword ptr[esp+4]
488 OpBuffer[obp++]=(char)0xF7;
489 OpBuffer[obp++]=(char)0x54;
490 OpBuffer[obp++]=(char)0x24;
491 OpBuffer[obp++]=(char)0x04;
492 }
493 else{
494 ////////////////////
495 // 32ビット整数演算
496 ////////////////////
497
498 //not dword ptr[esp]
499 OpBuffer[obp++]=(char)0xF7;
500 OpBuffer[obp++]=(char)0x14;
501 OpBuffer[obp++]=(char)0x24;
502 }
503
504 return 1;
505}
Note: See TracBrowser for help on using the repository browser.