source: dev/BasicCompiler32/NumOpe_Arithmetic.cpp@ 115

Last change on this file since 115 was 75, checked in by dai_9181, 18 years ago

TYPEINFO→Typeへのリファクタリングを実施。64bitはほぼ完了。32bitが全般的に未完成。

File size: 26.9 KB
RevLine 
[3]1#include "../BasicCompiler_Common/common.h"
2#include "Opcode.h"
3
4void GetStackData_ToRegister(int *type,int sp){
5 /*NumOpeポーランドのスタック蓄積による演算内容(2つのデータ)を
6 レジスタedx:eax、ecx:ebxに取得する*/
7
8 if(type[sp-1]==DEF_DOUBLE){
9 //fld qword ptr[esp]
10 op_fld_ptr_esp(DEF_DOUBLE);
11
12 //fistp qword ptr[esp]
[66]13 op_fistp_ptr_esp( sizeof(_int64) );
[3]14
15 //pop ebx
16 op_pop(REG_EBX);
17
18 //pop ecx
19 op_pop(REG_ECX);
20 }
21 else if(type[sp-1]==DEF_SINGLE){
22 //fld dword ptr[esp]
23 op_fld_ptr_esp(DEF_SINGLE);
24
25 //sub esp,4
26 op_sub_esp(4);
27
28 //fistp qword ptr[esp]
[66]29 op_fistp_ptr_esp( sizeof(_int64) );
[3]30
31 //pop ebx
32 op_pop(REG_EBX);
33
34 //pop ecx
35 op_pop(REG_ECX);
36 }
37 else if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
38 //pop ebx
39 op_pop(REG_EBX);
40
41 //pop ecx
42 op_pop(REG_ECX);
43 }
44 else{
45 //pop eax
46 op_pop(REG_EAX);
47
48 if(IsSignedType(type[sp-1])){
49 //符号拡張
50 //edx:eax ← eax
51
52 //cdq
53 op_cdq();
54 }
55 else{
56 //ビット拡張
57 //edx:eax ← eax
58
59 //xor edx,edx
60 op_zero_reg(REG_EDX);
61 }
62
63 //mov ebx,eax
64 OpBuffer[obp++]=(char)0x8B;
65 OpBuffer[obp++]=(char)0xD8;
66
67 //mov ecx,edx
68 OpBuffer[obp++]=(char)0x8B;
69 OpBuffer[obp++]=(char)0xCA;
70 }
71
72 //第1項を64ビットに対応させる
73 if(type[sp-2]==DEF_DOUBLE){
74 //fld qword ptr[esp]
75 op_fld_ptr_esp(DEF_DOUBLE);
76
77 //fistp qword ptr[esp]
[66]78 op_fistp_ptr_esp( sizeof(_int64) );
[3]79
80 //pop eax
81 op_pop(REG_EAX);
82
83 //pop edx
84 op_pop(REG_EDX);
85 }
86 else if(type[sp-2]==DEF_SINGLE){
87 //fld dword ptr[esp]
88 op_fld_ptr_esp(DEF_SINGLE);
89
90 //sub esp,4
91 op_sub_esp(4);
92
93 //fistp qword ptr[esp]
[66]94 op_fistp_ptr_esp( sizeof(_int64) );
[3]95
96 //pop eax
97 op_pop(REG_EAX);
98
99 //pop edx
100 op_pop(REG_EDX);
101 }
102 else if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
103 //pop eax
104 op_pop(REG_EAX);
105
106 //pop edx
107 op_pop(REG_EDX);
108 }
109 else{
110 //pop eax
111 op_pop(REG_EAX);
112
113 if(IsSignedType(type[sp-2])){
114 //符号拡張
115 //edx:eax ← eax
116
117 //cdq
118 op_cdq();
119 }
120 else{
121 //ビット拡張
122 //edx:eax ← eax
123
124 //xor edx,edx
125 op_zero_reg(REG_EDX);
126 }
127 }
128}
129
130void FormatStackData_To64bit(int *type,int sp){
131 //NumOpeポーランドのスタック蓄積による演算内容(2つのデータ)を64ビット整数型にする
132
133 GetStackData_ToRegister(type,sp);
134
135 //push ecx
136 op_push(REG_ECX);
137
138 //push ebx
139 op_push(REG_EBX);
140
141 //push edx
142 op_push(REG_EDX);
143
144 //push eax
145 op_push(REG_EAX);
146}
147
148BOOL CalcTwoTerm_Arithmetic(int idCalc,int *type,LONG_PTR *index_stack,int *pStackPointer){
149 /* value[sp-2] = value[sp-2] + value[sp-1]
150 value[sp-2] = value[sp-2] - value[sp-1]
151 value[sp-2] = value[sp-2] * value[sp-1] */
152
153 int sp;
154 sp=*pStackPointer;
155
156 int AnswerType;
157 AnswerType=NeutralizationType(type[sp-2],index_stack[sp-2],type[sp-1],index_stack[sp-1]);
158
159 if(type[sp-2]==DEF_DOUBLE||type[sp-2]==DEF_SINGLE||
160 type[sp-1]==DEF_DOUBLE||type[sp-1]==DEF_SINGLE){
161 /////////////
162 // 実数演算
163 /////////////
164
165 if(type[sp-1]==DEF_DOUBLE){
166 //fld qword ptr[esp]
167 op_fld_ptr_esp(DEF_DOUBLE);
168
169 //add esp,8
170 op_add_esp(8);
171 }
172 else if(type[sp-1]==DEF_SINGLE){
173 //fld dword ptr[esp]
174 op_fld_ptr_esp(DEF_SINGLE);
175
176 //add esp,4
177 op_add_esp(4);
178 }
179 else if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
180 //64ビット整数値
181
182 //fild qword ptr[esp]
183 op_fld_ptr_esp(DEF_INT64);
184
185 //add esp,8
186 op_add_esp(8);
187 }
188 else{
189 //その他整数型
190
191 //fild dword ptr[esp]
192 op_fld_ptr_esp(DEF_LONG);
193
194 //add esp,4
195 op_add_esp(4);
196 }
197
198 if(type[sp-2]==DEF_DOUBLE){
199 //fld qword ptr[esp]
200 op_fld_ptr_esp(DEF_DOUBLE);
201 }
202 else if(type[sp-2]==DEF_SINGLE){
203 //fld dword ptr[esp]
204 op_fld_ptr_esp(DEF_SINGLE);
205 }
206 else if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
207 //64ビット整数値
208 op_fld_ptr_esp(DEF_INT64);
209 }
210 else{
211 //その他整数型
212
213 //fild dword ptr[esp]
214 op_fld_ptr_esp(DEF_LONG);
215 }
216
217 if(GetTypeSize(type[sp-2],-1)==sizeof(_int64)){
218 if(AnswerType==DEF_SINGLE){
219 //add esp,4
220 op_add_esp(4);
221 }
222 }
223 else{
224 if(AnswerType==DEF_DOUBLE){
225 //sub esp,4
226 op_sub_esp(4);
227 }
228 }
229
230 if(idCalc==CALC_ADDITION){
231 //faddp st(1),st
232 OpBuffer[obp++]=(char)0xDE;
233 OpBuffer[obp++]=(char)0xC1;
234 }
235 else if(idCalc==CALC_SUBTRACTION){
236 //fsubrp st(1),st
237 OpBuffer[obp++]=(char)0xDE;
238 OpBuffer[obp++]=(char)0xE1;
239 }
240 else if(idCalc==CALC_PRODUCT){
241 //fmulp st(1),st
242 OpBuffer[obp++]=(char)0xDE;
243 OpBuffer[obp++]=(char)0xC9;
244 }
245
246 if(AnswerType==DEF_DOUBLE){
247 //fstp qword ptr[esp]
248 OpBuffer[obp++]=(char)0xDD;
249 OpBuffer[obp++]=(char)0x1C;
250 OpBuffer[obp++]=(char)0x24;
251 }
252 else{
253 //fstp dword ptr[esp]
254 OpBuffer[obp++]=(char)0xD9;
255 OpBuffer[obp++]=(char)0x1C;
256 OpBuffer[obp++]=(char)0x24;
257 }
258 }
259 else if(Is64Type(type[sp-2])||Is64Type(type[sp-1])){
260 //////////////////////
261 // 64ビット整数演算
262 //////////////////////
263
264 if(idCalc==CALC_PRODUCT){
265 if(!((type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD)&&
266 (type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD))){
267 //2つの項のどちらかが64ビット整数でないとき
268 FormatStackData_To64bit(type,sp);
269 }
270
271 //call _allmul
[75]272 extern UserProc *pSub_allmul;
[3]273 op_call(pSub_allmul);
274
275 //push edx
276 op_push(REG_EDX);
277
278 //push eax
279 op_push(REG_EAX);
280 }
281 else{
282 if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
283 //第2項が64ビット整数値のとき
284
285 //pop ebx
286 op_pop(REG_EBX);
287
288 //pop ecx
289 op_pop(REG_ECX);
290 }
291 else{
292 //第2項がその他整数値のとき
293
294 //pop eax
295 op_pop(REG_EAX);
296
297 if(IsSignedType(type[sp-1])){
298 //符号拡張
299 //edx:eax ← eax
300
301 //cdq
302 op_cdq();
303 }
304 else{
305 //ビット拡張
306 //edx:eax ← eax
307
308 //xor edx,edx
309 op_zero_reg(REG_EDX);
310 }
311
312 //mov ebx,eax
313 OpBuffer[obp++]=(char)0x8B;
314 OpBuffer[obp++]=(char)0xD8;
315
316 //mov ecx,edx
317 OpBuffer[obp++]=(char)0x8B;
318 OpBuffer[obp++]=(char)0xCA;
319 }
320
321 if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
322 //第1項が64ビット整数値のとき
323
324 if(idCalc==CALC_ADDITION){
325 //add dword ptr[esp],ebx
326 OpBuffer[obp++]=(char)0x01;
327 OpBuffer[obp++]=(char)0x1C;
328 OpBuffer[obp++]=(char)0x24;
329
330 //adc dword ptr[esp+sizeof(long)],ecx
331 OpBuffer[obp++]=(char)0x11;
332 OpBuffer[obp++]=(char)0x4C;
333 OpBuffer[obp++]=(char)0x24;
334 OpBuffer[obp++]=(char)0x04;
335 }
336 else if(idCalc==CALC_SUBTRACTION){
337 //sub dword ptr[esp],ebx
338 OpBuffer[obp++]=(char)0x29;
339 OpBuffer[obp++]=(char)0x1C;
340 OpBuffer[obp++]=(char)0x24;
341
342 //sbb dword ptr[esp+sizeof(long)],ecx
343 OpBuffer[obp++]=(char)0x19;
344 OpBuffer[obp++]=(char)0x4C;
345 OpBuffer[obp++]=(char)0x24;
346 OpBuffer[obp++]=(char)0x04;
347 }
348 }
349 else{
350 //第1項がその他整数値のとき
351
352 //pop eax
353 op_pop(REG_EAX);
354
355 if(IsSignedType(type[sp-2])){
356 //符号拡張
357 //edx:eax ← eax
358
359 //cdq
360 op_cdq();
361 }
362 else{
363 //ビット拡張
364 //edx:eax ← eax
365
366 //xor edx,edx
367 op_zero_reg(REG_EDX);
368 }
369
370 if(idCalc==CALC_ADDITION){
371 //add ebx,eax
372 OpBuffer[obp++]=(char)0x03;
373 OpBuffer[obp++]=(char)0xD8;
374
375 //adc ecx,edx
376 OpBuffer[obp++]=(char)0x13;
377 OpBuffer[obp++]=(char)0xCA;
378 }
379 else if(idCalc==CALC_SUBTRACTION){
380 //sub ebx,eax
381 OpBuffer[obp++]=(char)0x2B;
382 OpBuffer[obp++]=(char)0xC3;
383
384 //sbb ecx,edx
385 OpBuffer[obp++]=(char)0x1B;
386 OpBuffer[obp++]=(char)0xD1;
387 }
388
389 //push ecx
390 op_push(REG_ECX);
391
392 //push ebx
393 op_push(REG_EBX);
394 }
395 }
396 }
397 else{
398 //////////////////////////
399 //32ビット以下の整数演算
400 //////////////////////////
401
402 //pop ebx
403 op_pop(REG_EBX);
404
405 //pop eax
406 op_pop(REG_EAX);
407
408 //sub esp,4
409 op_sub_esp(4);
410
411 if(idCalc==CALC_ADDITION){
412 //add eax,ebx
413 OpBuffer[obp++]=(char)0x03;
414 OpBuffer[obp++]=(char)0xC3;
415 }
416 else if(idCalc==CALC_SUBTRACTION){
417 //sub eax,ebx
418 OpBuffer[obp++]=(char)0x2B;
419 OpBuffer[obp++]=(char)0xC3;
420 }
421 else if(idCalc==CALC_PRODUCT){
422 //imul eax,ebx(64ビット演算ではないので、符号を考慮しない)
423 OpBuffer[obp++]=(char)0x0F;
424 OpBuffer[obp++]=(char)0xAF;
425 OpBuffer[obp++]=(char)0xC3;
426 }
427
428 //mov dword ptr[esp],eax
429 OpBuffer[obp++]=(char)0x89;
430 OpBuffer[obp++]=(char)0x04;
431 OpBuffer[obp++]=(char)0x24;
432 }
433
434 sp--;
435 type[sp-1]=AnswerType;
436
437 *pStackPointer=sp;
438
439 return 1;
440}
441
442BOOL Calc_Mod(int *type,int *pStackPointer){
443 //value[sp-2]%=value[sp-1]
444 //剰余演算
445
446 int sp;
447 sp=*pStackPointer;
448
449 if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD||
450 type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
451 ////////////////
452 // 64ビット演算
453 ////////////////
454
455 //第2項を64ビットに対応させる
456 FormatStackData_To64bit(type,sp);
457
458 if(IsSignedType(type[sp-2])==0&&IsSignedType(type[sp-1])==0){
459 //符号なし演算
460
461 //call _aullrem
[75]462 extern UserProc *pSub_aullrem;
[3]463 op_call(pSub_aullrem);
464 }
465 else{
466 //符号あり演算
467
468 //call _allrem
[75]469 extern UserProc *pSub_allrem;
[3]470 op_call(pSub_allrem);
471 }
472
473 //push edx
474 op_push(REG_EDX);
475
476 //push eax
477 op_push(REG_EAX);
478
479 sp--;
480 if(type[sp-1]==DEF_QWORD&&type[sp]==DEF_QWORD) type[sp-1]=DEF_QWORD;
481 else type[sp-1]=DEF_INT64;
482 }
483 else{
484 ////////////////
485 // 32ビット演算
486 ////////////////
487
488 if(type[sp-1]==DEF_DOUBLE){
489 //fld qword ptr[esp]
490 op_fld_ptr_esp(DEF_DOUBLE);
491
492 //add esp,4
493 op_add_esp(4);
494
495 //fistp dword ptr[esp]
[66]496 op_fistp_ptr_esp( sizeof(long) );
[3]497 }
498 else if(type[sp-1]==DEF_SINGLE){
499 //fld dword ptr[esp]
500 op_fld_ptr_esp(DEF_SINGLE);
501
502 //fistp dword ptr[esp]
[66]503 op_fistp_ptr_esp( sizeof(long) );
[3]504 }
505
506 //pop ebx
507 op_pop(REG_EBX);
508
509 if(type[sp-2]==DEF_DOUBLE){
510 //fld qword ptr[esp]
511 op_fld_ptr_esp(DEF_DOUBLE);
512
513 //add esp,4
514 op_add_esp(4);
515
516 //fistp dword ptr[esp]
[66]517 op_fistp_ptr_esp( sizeof(long) );
[3]518 }
519 else if(type[sp-2]==DEF_SINGLE){
520 //fld dword ptr[esp]
521 op_fld_ptr_esp(DEF_SINGLE);
522
523 //fistp dword ptr[esp]
[66]524 op_fistp_ptr_esp( sizeof(long) );
[3]525 }
526
527 //pop eax
528 op_pop(REG_EAX);
529
530 //sub esp,4
531 op_sub_esp(4);
532
533 if(type[sp-2]==DEF_DWORD&&type[sp-1]==DEF_DWORD){
534 //xor edx,edx
535 op_zero_reg(REG_EDX);
536
537 //div ebx (eax=eax/ebx...edx)
538 OpBuffer[obp++]=(char)0xF7;
539 OpBuffer[obp++]=(char)0xF3;
540 }
541 else{
542 //cdq
543 op_cdq();
544
545 //idiv ebx (eax=eax/ebx...edx)
546 OpBuffer[obp++]=(char)0xF7;
547 OpBuffer[obp++]=(char)0xFB;
548 }
549
550 //mov dword ptr[esp],edx
551 OpBuffer[obp++]=(char)0x89;
552 OpBuffer[obp++]=(char)0x14;
553 OpBuffer[obp++]=(char)0x24;
554
555 sp--;
556 type[sp-1]=DEF_LONG;
557 }
558
559 *pStackPointer=sp;
560
561 return 1;
562}
563
564BOOL Calc_Divide(int *type,int *pStackPointer,int BaseType){
565 //value[sp-2]/=value[sp-1];
566 //除算
567
568 int sp;
569 sp=*pStackPointer;
570
571 ///////////////////////
572 // 浮動小数点演算のみ
573 ///////////////////////
574
575 int AnswerType;
576 if(type[sp-2]==DEF_DOUBLE||type[sp-1]==DEF_DOUBLE||BaseType==DEF_DOUBLE) AnswerType=DEF_DOUBLE;
577 else AnswerType=DEF_SINGLE;
578
579 if(type[sp-1]==DEF_DOUBLE){
580 //fld qword ptr[esp]
581 op_fld_ptr_esp(DEF_DOUBLE);
582
583 //add esp,8
584 op_add_esp(8);
585 }
586 else if(type[sp-1]==DEF_SINGLE){
587 //fld dword ptr[esp]
588 op_fld_ptr_esp(DEF_SINGLE);
589
590 //add esp,4
591 op_add_esp(4);
592 }
593 else if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
594 //64ビット整数値
595
596 //fild qword ptr[esp]
597 op_fld_ptr_esp(DEF_INT64);
598
599 //add esp,8
600 op_add_esp(8);
601 }
602 else if(type[sp-1]==DEF_DWORD){
603 //pop eax
604 op_pop(REG_EAX);
605
606 //push 0
[67]607 op_push_V(0);
[3]608
609 //push eax
610 op_push(REG_EAX);
611
612 //fild qword ptr[esp]
613 OpBuffer[obp++]=(char)0xDF;
614 OpBuffer[obp++]=(char)0x2C;
615 OpBuffer[obp++]=(char)0x24;
616
617 //add esp,8
618 op_add_esp(8);
619 }
620 else{
621 //fild dword ptr[esp]
622 op_fld_ptr_esp(DEF_LONG);
623
624 //add esp,4
625 op_add_esp(4);
626 }
627
628 if(type[sp-2]==DEF_DOUBLE){
629 //fld qword ptr[esp]
630 op_fld_ptr_esp(DEF_DOUBLE);
631 }
632 else if(type[sp-2]==DEF_SINGLE){
633 //fld dword ptr[esp]
634 op_fld_ptr_esp(DEF_SINGLE);
635 }
636 else if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
637 //64ビット整数値
638
639 //fild qword ptr[esp]
640 op_fld_ptr_esp(DEF_INT64);
641 }
642 else if(type[sp-2]==DEF_DWORD){
643 //pop eax
644 op_pop(REG_EAX);
645
646 //push 0
[67]647 op_push_V(0);
[3]648
649 //push eax
650 op_push(REG_EAX);
651
652 //fild qword ptr[esp]
653 OpBuffer[obp++]=(char)0xDF;
654 OpBuffer[obp++]=(char)0x2C;
655 OpBuffer[obp++]=(char)0x24;
656 }
657 else{ //Long
658 //fild dword ptr[esp]
659 op_fld_ptr_esp(DEF_LONG);
660 }
661 //↓ここだけ例外DWord
662 if(GetTypeSize(type[sp-2],-1)==sizeof(_int64)||type[sp-2]==DEF_DWORD){
663 if(AnswerType==DEF_SINGLE){
664 //add esp,4
665 op_add_esp(4);
666 }
667 }
668 else{
669 if(AnswerType==DEF_DOUBLE){
670 //sub esp,4
671 op_sub_esp(4);
672 }
673 }
674
675 //fdivrp st(1),st
676 OpBuffer[obp++]=(char)0xDE;
677 OpBuffer[obp++]=(char)0xF1;
678
679 sp--;
680 if(AnswerType==DEF_DOUBLE){
681 //fstp qword ptr[esp]
682 OpBuffer[obp++]=(char)0xDD;
683 OpBuffer[obp++]=(char)0x1C;
684 OpBuffer[obp++]=(char)0x24;
685
686 type[sp-1]=DEF_DOUBLE;
687 }
688 else{
689 //fstp dword ptr[esp]
690 OpBuffer[obp++]=(char)0xD9;
691 OpBuffer[obp++]=(char)0x1C;
692 OpBuffer[obp++]=(char)0x24;
693
694 type[sp-1]=DEF_SINGLE;
695 }
696
697 *pStackPointer=sp;
698
699 return 1;
700}
701
702BOOL Calc_IntDivide(int *type,LONG_PTR *index_stack,int *pStackPointer){
703 //value[sp-2]/=value[sp-1]
704 //除算(整数)
705
706 int sp;
707 sp=*pStackPointer;
708
709 if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD||
710 type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
711 ////////////////
712 // 64ビット演算
713 ////////////////
714
715 //2つの項を64ビットに対応させる
716 FormatStackData_To64bit(type,sp);
717
718 if(IsSignedType(type[sp-2])==0&&IsSignedType(type[sp-1])==0){
719 //符号なし演算
720
721 //call _aulldiv
[75]722 extern UserProc *pSub_aulldiv;
[3]723 op_call(pSub_aulldiv);
724 }
725 else{
726 //符号あり演算
727
728 //call _alldiv
[75]729 extern UserProc *pSub_alldiv;
[3]730 op_call(pSub_alldiv);
731 }
732
733 //push edx
734 op_push(REG_EDX);
735
736 //push eax
737 op_push(REG_EAX);
738
739 sp--;
740 if(type[sp-1]==DEF_QWORD&&type[sp]==DEF_QWORD) type[sp-1]=DEF_QWORD;
741 else type[sp-1]=DEF_INT64;
742 }
743 else{
744 ////////////////
745 // 32ビット演算
746 ////////////////
747
748 if(type[sp-1]==DEF_DOUBLE){
749 //fld qword ptr[esp]
750 op_fld_ptr_esp(DEF_DOUBLE);
751
752 //add esp,4
753 op_add_esp(4);
754
755 //fistp dword ptr[esp]
[66]756 op_fistp_ptr_esp( sizeof(long) );
[3]757 }
758 else if(type[sp-1]==DEF_SINGLE){
759 //fld dword ptr[esp]
760 op_fld_ptr_esp(DEF_SINGLE);
761
762 //fistp dword ptr[esp]
[66]763 op_fistp_ptr_esp( sizeof(long) );
[3]764 }
765
766 //pop ebx
767 op_pop(REG_EBX);
768
769 if(type[sp-2]==DEF_DOUBLE){
770 //fld qword ptr[esp]
771 op_fld_ptr_esp(DEF_DOUBLE);
772
773 //add esp,4
774 op_add_esp(4);
775
776 //fistp dword ptr[esp]
[66]777 op_fistp_ptr_esp( sizeof(long) );
[3]778 }
779 else if(type[sp-2]==DEF_SINGLE){
780 //fld dword ptr[esp]
781 op_fld_ptr_esp(DEF_SINGLE);
782
783 //fistp dword ptr[esp]
[66]784 op_fistp_ptr_esp( sizeof(long) );
[3]785 }
786
787 //pop eax
788 op_pop(REG_EAX);
789
790 //sub esp,4
791 op_sub_esp(4);
792
793 if((type[sp-2]==DEF_DWORD&&type[sp-1]==DEF_DWORD)||
794 (IS_POSITIVE_LITERAL(index_stack[sp-2])&&type[sp-1]==DEF_DWORD)||
795 (type[sp-2]==DEF_DWORD&&IS_POSITIVE_LITERAL(index_stack[sp-1]))){
796 //xor edx,edx
797 op_zero_reg(REG_EDX);
798
799 //div ebx (eax=eax/ebx...edx)
800 OpBuffer[obp++]=(char)0xF7;
801 OpBuffer[obp++]=(char)0xF3;
802 }
803 else{
804 //cdq
805 op_cdq();
806
807 //idiv ebx (eax=eax/ebx...edx)
808 OpBuffer[obp++]=(char)0xF7;
809 OpBuffer[obp++]=(char)0xFB;
810 }
811
812 //mov dword ptr[esp],eax
813 OpBuffer[obp++]=(char)0x89;
814 OpBuffer[obp++]=(char)0x04;
815 OpBuffer[obp++]=(char)0x24;
816
817 sp--;
818
819 //整数以外の型だったときはLong型にする
820 if(!IsWholeNumberType(type[sp-1])) type[sp-1]=DEF_LONG;
821 }
822
823 *pStackPointer=sp;
824 return 1;
825}
826
827BOOL Calc_MinusMark(int *type,int sp){
828 //value[sp-1]=-value[sp-1]
829 //符号反転
830
831 if(type[sp-1]==DEF_DOUBLE){
832 //fld qword ptr[esp]
833 op_fld_ptr_esp(DEF_DOUBLE);
834
835 //push -1
[67]836 op_push_V(-1);
[3]837
838 //fild dword ptr[esp]
839 op_fld_ptr_esp(DEF_LONG);
840
841 //add esp,4
842 op_add_esp(4);
843
844 //fmulp st(1),st
845 OpBuffer[obp++]=(char)0xDE;
846 OpBuffer[obp++]=(char)0xC9;
847
848 //fstp qword ptr[esp]
849 OpBuffer[obp++]=(char)0xDD;
850 OpBuffer[obp++]=(char)0x1C;
851 OpBuffer[obp++]=(char)0x24;
852 }
853 else if(type[sp-1]==DEF_SINGLE){
854 //fld dword ptr[esp]
855 op_fld_ptr_esp(DEF_SINGLE);
856
857 //push -1
[67]858 op_push_V(-1);
[3]859
860 //fild dword ptr[esp]
861 op_fld_ptr_esp(DEF_LONG);
862
863 //add esp,4
864 op_add_esp(4);
865
866 //fmulp st(1),st
867 OpBuffer[obp++]=(char)0xDE;
868 OpBuffer[obp++]=(char)0xC9;
869
870 //fstp dword ptr[esp]
871 OpBuffer[obp++]=(char)0xD9;
872 OpBuffer[obp++]=(char)0x1C;
873 OpBuffer[obp++]=(char)0x24;
874 }
875 else if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
876 //pop eax
877 op_pop(REG_EAX);
878
879 //neg eax
880 OpBuffer[obp++]=(char)0xF7;
881 OpBuffer[obp++]=(char)0xD8;
882
883 //pop edx
884 op_pop(REG_EDX);
885
886 //adc edx,0
887 OpBuffer[obp++]=(char)0x83;
888 OpBuffer[obp++]=(char)0xD2;
889 OpBuffer[obp++]=(char)0x00;
890
891 //neg edx
892 OpBuffer[obp++]=(char)0xF7;
893 OpBuffer[obp++]=(char)0xDA;
894
895 //push edx
896 op_push(REG_EDX);
897
898 //push eax
899 op_push(REG_EAX);
900
901 type[sp-1]=DEF_INT64; //QWordはInt64へ
902 }
903 else if(IsWholeNumberType(type[sp-1])){
904 //pop eax
905 op_pop(REG_EAX);
906
907 //imul eax,-1
908 OpBuffer[obp++]=(char)0x6B;
909 OpBuffer[obp++]=(char)0xC0;
910 OpBuffer[obp++]=(char)0xFF;
911
912 //push eax
913 op_push(REG_EAX);
914
915 type[sp-1]=GetSignedType(type[sp-1]);
916 }
917
918 return 1;
919}
920
921BOOL Calc_Power(int *type,int *pStackPointer){
922 //べき乗(実数演算のみ)
923
924 int sp;
925 sp=*pStackPointer;
926
927 if(type[sp-1]==DEF_DOUBLE){
928 //fld qword ptr[esp]
929 op_fld_ptr_esp(DEF_DOUBLE);
930
931 //add esp,8
932 op_add_esp(8);
933 }
934 else if(type[sp-1]==DEF_SINGLE){
935 //fld dword ptr[esp]
936 op_fld_ptr_esp(DEF_SINGLE);
937
938 //add esp,4
939 op_add_esp(4);
940 }
941 else if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
942 //64ビット整数値
943
944 //fild qword ptr[esp]
945 op_fld_ptr_esp(DEF_INT64);
946
947 //add esp,8
948 op_add_esp(8);
949 }
950 else{
951 //32ビット整数値
952
953 //fild dword ptr[esp]
954 op_fld_ptr_esp(DEF_LONG);
955
956 //add esp,4
957 op_add_esp(4);
958 }
959
960 if(type[sp-2]==DEF_DOUBLE){
961 //fld qword ptr[esp]
962 op_fld_ptr_esp(DEF_DOUBLE);
963 }
964 else if(type[sp-2]==DEF_SINGLE){
965 //fld dword ptr[esp]
966 op_fld_ptr_esp(DEF_SINGLE);
967
968 //sub esp,4
969 op_sub_esp(4);
970 }
971 else if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
972 //64ビット整数値
973
974 //fild qword ptr[esp]
975 op_fld_ptr_esp(DEF_INT64);
976 }
977 else{
978 //32ビット整数値
979
980 //fild dword ptr[esp]
981 op_fld_ptr_esp(DEF_LONG);
982
983 //sub esp,4
984 op_sub_esp(4);
985 }
986
987 //sub esp,8
988 op_sub_esp(8);
989
990 //fstp qword ptr[esp]
991 OpBuffer[obp++]=(char)0xDD;
992 OpBuffer[obp++]=(char)0x1C;
993 OpBuffer[obp++]=(char)0x24;
994
995 //fstp qword ptr[esp+8]
996 OpBuffer[obp++]=(char)0xDD;
997 OpBuffer[obp++]=(char)0x5C;
998 OpBuffer[obp++]=(char)0x24;
999 OpBuffer[obp++]=(char)0x08;
1000
1001 //call pow
[75]1002 extern UserProc *pSub_pow;
[3]1003 op_call(pSub_pow);
1004
1005 //sub esp,8
1006 op_sub_esp(8);
1007
1008 //fstp qword ptr[esp]
1009 OpBuffer[obp++]=(char)0xDD;
1010 OpBuffer[obp++]=(char)0x1C;
1011 OpBuffer[obp++]=(char)0x24;
1012
1013 sp--;
1014 type[sp-1]=DEF_DOUBLE;
1015
1016 *pStackPointer=sp;
1017 return 1;
1018}
1019
1020BOOL Calc_Cast(int *type,LONG_PTR *index_stack,int *pStackPointer){
1021 //キャスト
1022
1023 int sp;
1024 sp=*pStackPointer;
1025
1026 int CastType;
1027 CastType=type[sp-1];
1028 if((CastType&FLAG_CAST)==0){
1029 SetError(47,NULL,cp);
1030 return 0;
1031 }
1032 CastType=CastType&(~FLAG_CAST);
1033
1034 if(IsPtrType(CastType)){
1035 ChangeTypeToLong(type[sp-2]);
1036 }
1037 else if(IsRealNumberType(CastType)){
1038 if(CastType==DEF_DOUBLE) ChangeTypeToDouble(type[sp-2]);
1039 else if(CastType==DEF_SINGLE) ChangeTypeToSingle(type[sp-2]);
1040 }
1041 else ChangeTypeToWhole(type[sp-2],CastType);
1042
1043 type[sp-2]=CastType;
1044 index_stack[sp-2]=index_stack[sp-1];
1045
1046 sp--;
1047
1048 *pStackPointer=sp;
1049 return 1;
1050}
1051
1052BOOL Calc_SHL(int *type,int *pStackPointer){
1053 //左ビットシフト
1054 //value[sp-2]=value[sp-2]<<value[sp-1]
1055
1056 int sp;
1057 sp=*pStackPointer;
1058
1059 if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
1060 ////////////////
1061 // 64ビット演算
1062 ////////////////
1063
1064 //2項目は32ビット整数として利用
1065 if(type[sp-1]==DEF_DOUBLE){
1066 //fld qword ptr[esp]
1067 op_fld_ptr_esp(DEF_DOUBLE);
1068
1069 //add esp,4
1070 op_add_esp(4);
1071
1072 //fistp dword ptr[esp]
[66]1073 op_fistp_ptr_esp( sizeof(long) );
[3]1074
1075 //pop ecx
1076 op_pop(REG_ECX);
1077 }
1078 else if(type[sp-1]==DEF_SINGLE){
1079 //fld dword ptr[esp]
1080 op_fld_ptr_esp(DEF_SINGLE);
1081
1082 //fistp dword ptr[esp]
[66]1083 op_fistp_ptr_esp( sizeof(long) );
[3]1084
1085 //pop ecx
1086 op_pop(REG_ECX);
1087 }
1088 else if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
1089 //pop ecx
1090 op_pop(REG_ECX);
1091
1092 //add esp,4
1093 op_add_esp(4);
1094 }
1095 else{
1096 //pop ecx
1097 op_pop(REG_ECX);
1098 }
1099
1100 //第1項を64ビットに対応させる
1101 if(type[sp-2]==DEF_DOUBLE){
1102 //fld qword ptr[esp]
1103 op_fld_ptr_esp(DEF_DOUBLE);
1104
1105 //fistp qword ptr[esp]
[66]1106 op_fistp_ptr_esp( sizeof(_int64) );
[3]1107
1108 //pop eax
1109 op_pop(REG_EAX);
1110
1111 //pop edx
1112 op_pop(REG_EDX);
1113 }
1114 else if(type[sp-2]==DEF_SINGLE){
1115 //fld dword ptr[esp]
1116 op_fld_ptr_esp(DEF_SINGLE);
1117
1118 //sub esp,4
1119 op_sub_esp(4);
1120
1121 //fistp qword ptr[esp]
[66]1122 op_fistp_ptr_esp( sizeof(_int64) );
[3]1123
1124 //pop eax
1125 op_pop(REG_EAX);
1126
1127 //pop edx
1128 op_pop(REG_EDX);
1129 }
1130 else if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
1131 //pop eax
1132 op_pop(REG_EAX);
1133
1134 //pop edx
1135 op_pop(REG_EDX);
1136 }
1137 else{
1138 //pop eax
1139 op_pop(REG_EAX);
1140
1141 if(IsSignedType(type[sp-2])){
1142 //符号拡張
1143 //edx:eax ← eax
1144
1145 //cdq
1146 op_cdq();
1147 }
1148 else{
1149 //ビット拡張
1150 //edx:eax ← eax
1151
1152 //xor edx,edx
1153 op_zero_reg(REG_EDX);
1154 }
1155 }
1156
1157 //call _allshl
[75]1158 extern UserProc *pSub_allshl;
[3]1159 op_call(pSub_allshl);
1160
1161 //push edx
1162 op_push(REG_EDX);
1163
1164 //push eax
1165 op_push(REG_EAX);
1166
1167 sp--;
1168 }
1169 else{
1170 ////////////////
1171 // 32ビット演算
1172 ////////////////
1173
1174 //2項目は32ビット整数として利用
1175 if(type[sp-1]==DEF_DOUBLE){
1176 //fld qword ptr[esp]
1177 op_fld_ptr_esp(DEF_DOUBLE);
1178
1179 //add esp,4
1180 op_add_esp(4);
1181
1182 //fistp dword ptr[esp]
[66]1183 op_fistp_ptr_esp( sizeof(long) );
[3]1184
1185 //pop ecx
1186 op_pop(REG_ECX);
1187 }
1188 else if(type[sp-1]==DEF_SINGLE){
1189 //fld dword ptr[esp]
1190 op_fld_ptr_esp(DEF_SINGLE);
1191
1192 //fistp dword ptr[esp]
[66]1193 op_fistp_ptr_esp( sizeof(long) );
[3]1194
1195 //pop ecx
1196 op_pop(REG_ECX);
1197 }
1198 else if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
1199 //pop ecx
1200 op_pop(REG_ECX);
1201
1202 //add esp,4
1203 op_add_esp(4);
1204 }
1205 else{
1206 //pop ecx
1207 op_pop(REG_ECX);
1208 }
1209
1210 if(type[sp-2]==DEF_DOUBLE){
1211 //fld qword ptr[esp]
1212 op_fld_ptr_esp(DEF_DOUBLE);
1213
1214 //add esp,4
1215 op_add_esp(4);
1216
1217 //fistp dword ptr[esp]
[66]1218 op_fistp_ptr_esp( sizeof(long) );
[3]1219 }
1220 else if(type[sp-2]==DEF_SINGLE){
1221 //fld dword ptr[esp]
1222 op_fld_ptr_esp(DEF_SINGLE);
1223
1224 //fistp dword ptr[esp]
[66]1225 op_fistp_ptr_esp( sizeof(long) );
[3]1226 }
1227
1228 //pop eax
1229 op_pop(REG_EAX);
1230
1231 //sub esp,4
1232 op_sub_esp(4);
1233
1234 //shl eax,cl
1235 OpBuffer[obp++]=(char)0xD3;
1236 OpBuffer[obp++]=(char)0xE0;
1237
1238 //mov dword ptr[esp],eax
1239 OpBuffer[obp++]=(char)0x89;
1240 OpBuffer[obp++]=(char)0x04;
1241 OpBuffer[obp++]=(char)0x24;
1242
1243 sp--;
1244
1245 //32ビット型にする
1246 if(IsSignedType(type[sp-1])) type[sp-1]=DEF_LONG;
1247 else type[sp-1]=DEF_DWORD;
1248 }
1249
1250 *pStackPointer=sp;
1251 return 1;
1252}
1253
1254BOOL Calc_SHR(int *type,int *pStackPointer){
1255 //右ビットシフト
1256 //value[sp-2]=value[sp-2]>>value[sp-1]
1257
1258 int sp;
1259 sp=*pStackPointer;
1260
1261 if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
1262 ////////////////
1263 // 64ビット演算
1264 ////////////////
1265
1266 //2項目は32ビット整数として利用
1267 if(type[sp-1]==DEF_DOUBLE){
1268 //fld qword ptr[esp]
1269 op_fld_ptr_esp(DEF_DOUBLE);
1270
1271 //add esp,4
1272 op_add_esp(4);
1273
1274 //fistp dword ptr[esp]
[66]1275 op_fistp_ptr_esp( sizeof(long) );
[3]1276
1277 //pop ecx
1278 op_pop(REG_ECX);
1279 }
1280 else if(type[sp-1]==DEF_SINGLE){
1281 //fld dword ptr[esp]
1282 op_fld_ptr_esp(DEF_SINGLE);
1283
1284 //fistp dword ptr[esp]
[66]1285 op_fistp_ptr_esp( sizeof(long) );
[3]1286
1287 //pop ecx
1288 op_pop(REG_ECX);
1289 }
1290 else if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
1291 //pop ecx
1292 op_pop(REG_ECX);
1293
1294 //add esp,4
1295 op_add_esp(4);
1296 }
1297 else{
1298 //pop ecx
1299 op_pop(REG_ECX);
1300 }
1301
1302 //第1項を64ビットに対応させる
1303 if(type[sp-2]==DEF_DOUBLE){
1304 //fld qword ptr[esp]
1305 op_fld_ptr_esp(DEF_DOUBLE);
1306
1307 //fistp qword ptr[esp]
[66]1308 op_fistp_ptr_esp( sizeof(_int64) );
[3]1309
1310 //pop eax
1311 op_pop(REG_EAX);
1312
1313 //pop edx
1314 op_pop(REG_EDX);
1315 }
1316 else if(type[sp-2]==DEF_SINGLE){
1317 //fld dword ptr[esp]
1318 op_fld_ptr_esp(DEF_SINGLE);
1319
1320 //sub esp,4
1321 op_sub_esp(4);
1322
1323 //fistp qword ptr[esp]
[66]1324 op_fistp_ptr_esp( sizeof(_int64) );
[3]1325
1326 //pop eax
1327 op_pop(REG_EAX);
1328
1329 //pop edx
1330 op_pop(REG_EDX);
1331 }
1332 else if(type[sp-2]==DEF_INT64||type[sp-2]==DEF_QWORD){
1333 //pop eax
1334 op_pop(REG_EAX);
1335
1336 //pop edx
1337 op_pop(REG_EDX);
1338 }
1339 else{
1340 //pop eax
1341 op_pop(REG_EAX);
1342
1343 if(IsSignedType(type[sp-2])){
1344 //符号拡張
1345 //edx:eax ← eax
1346
1347 //cdq
1348 op_cdq();
1349 }
1350 else{
1351 //ビット拡張
1352 //edx:eax ← eax
1353
1354 //xor edx,edx
1355 op_zero_reg(REG_EDX);
1356 }
1357 }
1358
1359 if(type[sp-2]==DEF_QWORD){
1360 //符号なし演算
1361
1362 //call _aullshr
[75]1363 extern UserProc *pSub_aullshr;
[3]1364 op_call(pSub_aullshr);
1365 }
1366 else{
1367 //符号あり演算
1368
1369 //call _allshr
[75]1370 extern UserProc *pSub_allshr;
[3]1371 op_call(pSub_allshr);
1372 }
1373
1374 //push edx
1375 op_push(REG_EDX);
1376
1377 //push eax
1378 op_push(REG_EAX);
1379
1380 sp--;
1381 }
1382 else{
1383 ////////////////
1384 // 32ビット演算
1385 ////////////////
1386
1387 //2項目は32ビット整数として利用
1388 if(type[sp-1]==DEF_DOUBLE){
1389 //fld qword ptr[esp]
1390 op_fld_ptr_esp(DEF_DOUBLE);
1391
1392 //add esp,4
1393 op_add_esp(4);
1394
1395 //fistp dword ptr[esp]
[66]1396 op_fistp_ptr_esp( sizeof(long) );
[3]1397
1398 //pop ecx
1399 op_pop(REG_ECX);
1400 }
1401 else if(type[sp-1]==DEF_SINGLE){
1402 //fld dword ptr[esp]
1403 op_fld_ptr_esp(DEF_SINGLE);
1404
1405 //fistp dword ptr[esp]
[66]1406 op_fistp_ptr_esp( sizeof(long) );
[3]1407
1408 //pop ecx
1409 op_pop(REG_ECX);
1410 }
1411 else if(type[sp-1]==DEF_INT64||type[sp-1]==DEF_QWORD){
1412 //pop ecx
1413 op_pop(REG_ECX);
1414
1415 //add esp,4
1416 op_add_esp(4);
1417 }
1418 else{
1419 //pop ecx
1420 op_pop(REG_ECX);
1421 }
1422
1423 if(type[sp-2]==DEF_DOUBLE){
1424 //fld qword ptr[esp]
1425 op_fld_ptr_esp(DEF_DOUBLE);
1426
1427 //add esp,4
1428 op_add_esp(4);
1429
1430 //fistp dword ptr[esp]
[66]1431 op_fistp_ptr_esp( sizeof(long) );
[3]1432 }
1433 else if(type[sp-2]==DEF_SINGLE){
1434 //fld dword ptr[esp]
1435 op_fld_ptr_esp(DEF_SINGLE);
1436
1437 //fistp dword ptr[esp]
[66]1438 op_fistp_ptr_esp( sizeof(long) );
[3]1439 }
1440
1441 //pop eax
1442 op_pop(REG_EAX);
1443
1444 //sub esp,4
1445 op_sub_esp(4);
1446
1447 if(type[sp-2]==DEF_DWORD){
1448 //shr eax,cl
1449 OpBuffer[obp++]=(char)0xD3;
1450 OpBuffer[obp++]=(char)0xE8;
1451 }
1452 else{
1453 //sar eax,cl
1454 OpBuffer[obp++]=(char)0xD3;
1455 OpBuffer[obp++]=(char)0xF8;
1456 }
1457
1458 //mov dword ptr[esp],eax
1459 OpBuffer[obp++]=(char)0x89;
1460 OpBuffer[obp++]=(char)0x04;
1461 OpBuffer[obp++]=(char)0x24;
1462
1463 sp--;
1464
1465 //整数以外の型だったときはLong型にする
1466 if(!IsWholeNumberType(type[sp-1])) type[sp-1]=DEF_LONG;
1467 }
1468
1469 *pStackPointer=sp;
1470 return 1;
1471}
Note: See TracBrowser for help on using the repository browser.