source: dev/trunk/abdev/BasicCompiler32/CodeGenerator.cpp@ 225

Last change on this file since 225 was 225, checked in by dai_9181, 17 years ago

CodeGeneratorクラスのベースを実装

File size: 24.6 KB
Line 
1#include "stdafx.h"
2
3#include <Procedure.h>
4#include <CodeGenerator.h>
5
6
7
8/////////////////////////////////////////////////
9// ModR/Mバイト、SIBバイト、ディスプレースメント
10/////////////////////////////////////////////////
11
12//スケール
13#define SCALE_NON (char)0x00
14#define SCALE_2 (char)0x40
15#define SCALE_4 (char)0x80
16#define SCALE_8 (char)0xC0
17
18//インデックスなし
19#define INDEX_NON 0x04
20
21void CodeGenerator::set_mod_rm_sib_disp(char mod,int reg,int scale,int index_reg,int base_reg,long disp){
22 if(mod==MOD_DISP32){
23 //ModR/Mバイト
24 pNativeCode->Put( (char)(REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(0x04)) );
25
26 base_reg=0x05;
27 index_reg=INDEX_NON;
28 }
29 else{
30 //ModR/Mバイト
31 pNativeCode->Put( (char)(mod | REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(base_reg)) );
32 }
33
34
35 //レジスタモードの場合は、ここで終了
36 if(mod==MOD_REG) return;
37
38
39 if(REGISTER_OPERAND(base_reg)==0x04||mod==MOD_DISP32){
40 //////////////////////
41 // SIBバイトを使う
42 //////////////////////
43
44 pNativeCode->Put( (char)(scale| REGISTER_OPERAND(index_reg)<<3 | REGISTER_OPERAND(base_reg)) );
45 }
46
47 //ディスプレースメントを必要としない場合は、ここで終了
48 if(mod==MOD_BASE) return;
49
50
51 //////////////////////////
52 // ディスプレースメント
53 //////////////////////////
54
55 if(mod==MOD_BASE_DISP8)
56 {
57 pNativeCode->Put( (char)disp );
58 }
59 else
60 {
61 pNativeCode->Put( disp );
62 }
63}
64
65
66
67void CodeGenerator::__op_format(char op_prefix,char opcode,int reg){
68 //命令プリフィックス
69 if(op_prefix)
70 {
71 pNativeCode->Put( op_prefix );
72 }
73
74 //オペコード、レジスタ
75 pNativeCode->Put( (char)(opcode|REGISTER_OPERAND(reg)) );
76}
77void CodeGenerator::__op_format(char op_prefix,char opcode1,char opcode2,int reg,int base_reg,int offset,char mod){
78 //命令プリフィックス
79 if(op_prefix)
80 {
81 pNativeCode->Put( op_prefix );
82 }
83
84 //オペコード
85 pNativeCode->Put( opcode1 );
86 if(opcode2)
87 {
88 pNativeCode->Put( opcode2 );
89 }
90
91 //ModR/M, SIB, disp
92 set_mod_rm_sib_disp(mod,reg,SCALE_NON,INDEX_NON,base_reg,offset);
93}
94
95
96
97///////////////////
98// mov関連
99///////////////////
100
101void CodeGenerator::op_mov_RV(int reg,long offset){
102 //mov reg,value
103
104 //オペコード、レジスタ
105 pNativeCode->Put( (char)(0xB8|REGISTER_OPERAND(reg)) );
106
107 //DISP32
108 pNativeCode->Put( offset );
109}
110void CodeGenerator::op_mov_RV(int op_size,int reg,int offset){
111 if(op_size==PTR_SIZE) op_mov_RV(reg,offset);
112 else SetError(300,NULL,cp);
113}
114void CodeGenerator::op_mov_RR(int reg1,int reg2){
115 //mov reg1,reg2
116
117 if(reg1==reg2) return;
118
119 //1000 1011 11xx xbbb
120 pNativeCode->Put( (char)0x8B );
121 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
122}
123void CodeGenerator::op_mov_RM(int op_size,int reg,int base_reg,int offset,char mod){
124 //mov reg32,dword ptr[base_reg+offset]
125 //mov reg16,word ptr[base_reg+offset]
126 //mov reg8,byte ptr[base_reg+offset]
127
128 //16ビット演算の命令プリフィックス
129 char op_prefix=0;
130 if(op_size==sizeof(short)) op_prefix=(char)0x66;
131
132 //オペコード
133 char opcode;
134 if(op_size==sizeof(char)) opcode=(char)0x8A;
135 else opcode=(char)0x8B;
136
137 __op_format(op_prefix,opcode,0,reg,base_reg,offset,mod);
138}
139void CodeGenerator::op_mov_RM_ex(int op_size,int reg,int base_reg1,int base_reg2,long offset,BOOL bUseOffset){
140 //mov reg32,dword ptr[base_reg1+base_reg2+offset]
141 //mov reg16,word ptr[base_reg1+base_reg2+offset]
142 //mov reg8,byte ptr[base_reg1+base_reg2+offset]
143
144 if(base_reg1==REG_ESP){
145 //SIBバイトのindex部にespは指定できない
146 base_reg1=base_reg2;
147 base_reg2=REG_ESP;
148 }
149
150 //16ビット演算のプリフィックス
151 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
152
153 //オペコード
154 if(op_size==sizeof(char)) pNativeCode->Put( (char)0x8A );
155 else pNativeCode->Put( (char)0x8B );
156
157 if(bUseOffset){
158 ///////////////////////////
159 // オフセット値を使う
160 ///////////////////////////
161
162 //レジスタ
163 pNativeCode->Put( (char)(0x84| REGISTER_OPERAND(reg)<<3) );
164
165 //ベースレジスタ
166 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
167
168 //オフセット値
169 pNativeCode->Put( offset );
170 }
171 else{
172 ///////////////////////////
173 // オフセット値を使わない
174 ///////////////////////////
175
176 //レジスタ
177 pNativeCode->Put( (char)(0x04| REGISTER_OPERAND(reg)<<3) );
178
179 //ベースレジスタ
180 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
181 }
182}
183void CodeGenerator::op_mov_MR(int op_size,int reg,int base_reg,int offset,char mod){
184 //mov dword ptr[base_reg+offset],reg32
185 //mov word ptr[base_reg+offset],reg16
186 //mov byte ptr[base_reg+offset],reg8
187
188 //16ビット演算の命令プリフィックス
189 char op_prefix=0;
190 if(op_size==sizeof(short)) op_prefix=(char)0x66;
191
192 //オペコード
193 char opcode;
194 if(op_size==sizeof(char)) opcode=(char)0x88;
195 else opcode=(char)0x89;
196
197 __op_format(op_prefix,opcode,0,reg,base_reg,offset,mod);
198}
199void CodeGenerator::op_mov_MR_ex(int op_size,int reg,int base_reg1,int base_reg2,long offset,BOOL bUseOffset){
200 //mov dword ptr[base_reg1+base_reg2+offset],reg32
201 //mov word ptr[base_reg1+base_reg2+offset],reg16
202 //mov byte ptr[base_reg1+base_reg2+offset],reg8
203
204 if(base_reg1==REG_ESP){
205 //SIBバイトのindex部にrspは指定できない
206 base_reg1=base_reg2;
207 base_reg2=REG_ESP;
208 }
209
210 //16ビット演算のプリフィックス
211 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
212
213 //オペコード
214 if(op_size==sizeof(char)) pNativeCode->Put( (char)0x88 );
215 else pNativeCode->Put( (char)0x89 );
216
217 if(bUseOffset==USE_OFFSET){
218 //////////////////////////
219 //オフセット値を使う
220 //////////////////////////
221
222 //レジスタ
223 pNativeCode->Put( (char)(0x84| REGISTER_OPERAND(reg)<<3) );
224
225 //ベースレジスタ
226 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
227
228 //オフセット値
229 pNativeCode->Put( offset );
230 }
231 else{
232 //////////////////////////
233 //オフセット値を使わない
234 //////////////////////////
235
236 //レジスタ
237 pNativeCode->Put( (char)(0x04| REGISTER_OPERAND(reg)<<3) );
238
239 //ベースレジスタ
240 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
241 }
242}
243
244
245
246
247////////////////////////////////
248// movsx関連
249////////////////////////////////
250
251void CodeGenerator::op_movsx_R32R16(int reg32,int reg16){
252 //movsx reg32,reg16
253
254 if( reg16 == REG_NON )
255 {
256 reg16 = reg32;
257 }
258
259 //16ビット演算の命令プリフィックス
260 char op_prefix=0;
261
262 //オペコード
263 char opcode=(char)0x0F;
264 char opcode2=(char)0xBF;
265
266 __op_format(op_prefix,opcode,opcode2,reg32,reg16,0,MOD_REG);
267}
268void CodeGenerator::op_movsx_R32R8(int reg32,int reg8){
269 //movsx reg32,reg8
270
271 if( reg8 == REG_NON )
272 {
273 reg8 = reg32;
274 }
275
276 //16ビット演算の命令プリフィックス
277 char op_prefix=0;
278
279 //オペコード
280 char opcode=(char)0x0F;
281 char opcode2=(char)0xBE;
282
283 __op_format(op_prefix,opcode,opcode2,reg32,reg8,0,MOD_REG);
284}
285void CodeGenerator::op_movsx_R16R8(int reg16,int reg8){
286 //movsx reg16,reg8
287
288 if( reg8 == REG_NON )
289 {
290 reg8 = reg16;
291 }
292
293 //16ビット演算の命令プリフィックス
294 char op_prefix=(char)0x66;
295
296 //オペコード
297 char opcode=(char)0x0F;
298 char opcode2=(char)0xBE;
299
300 __op_format(op_prefix,opcode,opcode2,reg16,reg8,0,MOD_REG);
301}
302
303
304
305//////////////////////////////////
306// インクリメント・デクリメント
307//////////////////////////////////
308
309void CodeGenerator::op_inc(int reg){
310 //inc reg
311
312 //16ビット演算の命令プリフィックス
313 char op_prefix=0;
314
315 //オペコード
316 char opcode=(char)0xFF;
317
318 __op_format(op_prefix,opcode,0,0,reg,0,MOD_REG);
319}
320void CodeGenerator::op_dec(int reg){
321 //dec reg
322
323 //16ビット演算の命令プリフィックス
324 char op_prefix=0;
325
326 //オペコード
327 char opcode=(char)0xFF;
328
329 __op_format(op_prefix,opcode,0,0x01,reg,0,MOD_REG);
330}
331
332
333
334/////////////////////
335// add関連
336/////////////////////
337
338void CodeGenerator::op_add_RV8(int reg,char cValue){
339 //add reg,value8
340
341 pNativeCode->Put( (char)0x83 );
342 pNativeCode->Put( (char)(0xC0|REGISTER_OPERAND(reg)) );
343 pNativeCode->Put( cValue );
344}
345void CodeGenerator::op_add_RR( int reg1, int reg2 )
346{
347 //16ビット演算の命令プリフィックス
348 char op_prefix=0;
349
350 //オペコード
351 char opcode = (char)0x03;
352
353 __op_format(op_prefix,opcode,0,reg1,reg2,0,MOD_REG);
354}
355void CodeGenerator::op_add_RM(int op_size,int reg,int base_reg,int offset,char mod){
356 //add reg32,dword ptr[base_reg+offset]
357 //add reg16,word ptr[base_reg+offset]
358 //add reg8,byte ptr[base_reg+offset]
359
360 //16ビット演算の命令プリフィックス
361 char op_prefix=0;
362 if(op_size==sizeof(short)) op_prefix=(char)0x66;
363
364 //オペコード
365 char opcode;
366 if(op_size==sizeof(char)) opcode=(char)0x02;
367 else opcode=(char)0x03;
368
369 __op_format(op_prefix,opcode,0,reg,base_reg,offset,mod);
370}
371void CodeGenerator::op_adc_RV8(int reg,char cValue){
372 //adc reg,value8
373
374 pNativeCode->Put( (char)0x83 );
375 pNativeCode->Put( (char)(0xD0|REGISTER_OPERAND(reg)) );
376 pNativeCode->Put( cValue );
377}
378void CodeGenerator::op_adc_RR( int reg1, int reg2 )
379{
380 // adc reg1, reg2
381
382 //16ビット演算の命令プリフィックス
383 char op_prefix=0;
384
385 //オペコード
386 char opcode = (char)0x13;
387
388 __op_format( op_prefix, opcode, 0, reg1, reg2, 0, MOD_REG );
389}
390
391
392/////////////////////
393// sub関連
394/////////////////////
395
396void CodeGenerator::op_sub_RV8(int reg,char cValue){
397 //sub reg,value8
398
399 pNativeCode->Put( (char)0x83 );
400 pNativeCode->Put( (char)(0xE8|REGISTER_OPERAND(reg)) );
401 pNativeCode->Put( cValue );
402}
403void CodeGenerator::op_sub_RR( int reg1, int reg2 )
404{
405 // sub reg1, reg2
406
407 //16ビット演算の命令プリフィックス
408 char op_prefix=0;
409
410 //オペコード
411 char opcode = (char)0x2B;
412
413 __op_format( op_prefix, opcode, 0, reg1, reg2, 0, MOD_REG );
414}
415void CodeGenerator::op_sbb_RV8(int reg,char cValue){
416 //sbb reg,value8
417
418 pNativeCode->Put( (char)0x83 );
419 pNativeCode->Put( (char)(0xD8|REGISTER_OPERAND(reg)) );
420 pNativeCode->Put( cValue );
421}
422void CodeGenerator::op_sbb_RR( int reg1, int reg2 ){
423 //sbb reg1,reg2
424
425 //16ビット演算の命令プリフィックス
426 char op_prefix=0;
427
428 //オペコード
429 char opcode = (char)0x1B;
430
431 __op_format( op_prefix, opcode, 0, reg1, reg2, 0, MOD_REG );
432}
433
434
435
436////////////////////////
437// imul関連
438////////////////////////
439
440void CodeGenerator::op_imul_RR(int reg1,int reg2){
441 //imul reg1,reg2
442
443 //命令プリフィックス
444 char op_prefix = (char)0x0F;
445
446 //オペコード
447 char opcode = (char)0xAF;
448
449 __op_format( op_prefix, opcode, 0, reg1, reg2, 0, MOD_REG );
450}
451
452void CodeGenerator::op_imul_RV(int reg,long i32data){
453 //imul reg,i32data
454
455 if(-128<=i32data&&i32data<=127){
456 //オペコード
457 pNativeCode->Put( (char)0x6B );
458
459 //レジスタ
460 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg)) );
461
462 //値
463 pNativeCode->Put( (char)i32data );
464 }
465 else{
466 //オペコード
467 pNativeCode->Put( (char)0x69 );
468
469 //レジスタ
470 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg)) );
471
472 //値
473 pNativeCode->Put( i32data );
474 }
475}
476void CodeGenerator::op_imul_RV8(int reg,char cValue)
477{
478 //オペコード
479 pNativeCode->Put( (char)0x6B );
480
481 //レジスタ
482 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg)) );
483
484 //値
485 pNativeCode->Put( cValue );
486}
487
488
489
490//////////////////////
491// div関連
492//////////////////////
493
494void CodeGenerator::op_div_R( int reg )
495{
496 //div reg (eax=eax/reg...edx)
497 __op_format( (char)0xF7, (char)0xF0, reg );
498}
499void CodeGenerator::op_idiv_R( int reg )
500{
501 //idiv reg (eax=eax/reg...edx)
502 __op_format( (char)0xF7, (char)0xF8, reg );
503}
504
505
506
507//////////////////////
508// and関連
509//////////////////////
510
511void CodeGenerator::op_and_RV(int reg,long value){
512 //and reg,value
513
514 if(reg==REG_RAX){
515 //eaxのみ特殊
516
517 // [8bit rex] 0010 0101 [32bit offset]
518 pNativeCode->Put( (char)0x25 );
519 pNativeCode->Put( value );
520 }
521 else{
522 //16ビット演算の命令プリフィックス
523 char op_prefix=0;
524
525 //オペコード
526 char opcode=(char)0x81;
527
528 __op_format(op_prefix,opcode,0,0,reg,value,MOD_REG);
529 }
530}
531
532void CodeGenerator::op_and_RR( int reg1, int reg2 )
533{
534 //16ビット演算の命令プリフィックス
535 char op_prefix=0;
536
537 //オペコード
538 char opcode=(char)0x23;
539
540 __op_format(op_prefix,opcode,0,reg1,reg2,0,MOD_REG);
541}
542
543void CodeGenerator::op_or_RR( int op_size, int reg1, int reg2 ){
544 //16ビット演算のプリフィックス
545 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
546
547 //オペコード
548 if(op_size==sizeof(char)) pNativeCode->Put( (char)0x0A );
549 else pNativeCode->Put( (char)0x0B );
550
551 //レジスタ
552 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
553}
554
555void CodeGenerator::op_xor_RR( int reg1, int reg2 ){
556 // xor reg1, reg2
557
558 if( reg2 == REG_NON )
559 {
560 reg2 = reg1;
561 }
562
563 //16ビット演算の命令プリフィックス
564 char op_prefix=0;
565
566 //オペコード
567 char opcode=(char)0x33;
568
569 __op_format(op_prefix,opcode,0,reg1,reg2,0,MOD_REG);
570}
571
572
573
574void CodeGenerator::op_neg( int reg ){
575 //neg reg
576
577 //命令プリフィックス
578 char op_prefix = (char)0xF7;
579
580 //オペコード
581 char opcode = (char)0xD8;
582
583 __op_format( op_prefix, opcode, reg );
584}
585
586
587
588///////////////////////
589// 64ビット関連
590///////////////////////
591
592void CodeGenerator::op_cdq(){
593 //cdq
594 pNativeCode->Put( (char)0x99 );
595}
596
597
598
599/////////////////////
600// ストリング関係
601/////////////////////
602
603void CodeGenerator::op_rep_movs(int op_size){
604 if(op_size==sizeof(BYTE)){
605 //rep movs byte ptr[edi],byte ptr[esi]
606 pNativeCode->Put( (char)0xF3 );
607 pNativeCode->Put( (char)0xA4 );
608 }
609 else if(op_size==sizeof(short)){
610 //rep movs word ptr[edi],word ptr[esi]
611 pNativeCode->Put( (char)0xF3 );
612 pNativeCode->Put( (char)0x66 );
613 pNativeCode->Put( (char)0xA5 );
614 }
615 else if(op_size==sizeof(long)){
616 //rep movs dword ptr[edi],dword ptr[esi]
617 pNativeCode->Put( (char)0xF3 );
618 pNativeCode->Put( (char)0xA5 );
619 }
620}
621
622
623
624
625//////////////////////////
626// スタック関連
627//////////////////////////
628
629void CodeGenerator::op_push(int reg){
630 //push reg
631
632 if( reg == REG_NON ){
633 op_sub_esp( PTR_SIZE );
634 return;
635 }
636
637 //オペコード、レジスタ
638 __op_format(0,(char)0x50,reg);
639}
640void CodeGenerator::op_push_V(long data){
641 //スタックにリテラル値をプッシュ
642 if(-128<=data&&data<=127){
643 //push 8ビット値
644 pNativeCode->Put( (char)0x6A );
645 pNativeCode->Put( (char)data );
646 }
647 else{
648 //push 32ビット値
649 pNativeCode->Put( (char)0x68 );
650 pNativeCode->Put( data );
651 }
652}
653void CodeGenerator::op_push_M( int base_reg )
654{
655 // push dword ptr[base_reg]
656 __op_format( (char)0xFF, (char)0x30, base_reg );
657}
658void CodeGenerator::op_pop(int reg){
659 //pop reg
660
661 if( reg == REG_NON ){
662 op_add_esp( PTR_SIZE );
663 return;
664 }
665
666 //オペコード、レジスタ
667 __op_format(0,(char)0x58,reg);
668}
669void CodeGenerator::op_add_esp(long num){
670 //スタックポインタの加算(pop方向)
671
672 //add esp,num
673 if(0xFFFFFF80&num){
674 pNativeCode->Put( (char)0x81 );
675 pNativeCode->Put( (char)0xC4 );
676 pNativeCode->Put( num );
677 }
678 else{
679 //「128 > num > -127」の場合
680 pNativeCode->Put( (char)0x83 );
681 pNativeCode->Put( (char)0xC4 );
682 pNativeCode->Put( (char)num );
683 }
684}
685void CodeGenerator::op_sub_esp(long num){
686 //スタックポインタの減算(push方向)
687
688 //sub esp,num
689 if(0xFFFFFF80&num){
690 pNativeCode->Put( (char)0x81 );
691 pNativeCode->Put( (char)0xEC );
692 pNativeCode->Put( num );
693 }
694 else{
695 //「128 > num > -127」の場合
696 pNativeCode->Put( (char)0x83 );
697 pNativeCode->Put( (char)0xEC );
698 pNativeCode->Put( (char)num );
699 }
700}
701
702
703
704/////////////////////
705// cmp関連
706/////////////////////
707void CodeGenerator::op_cmp_RR( int reg1, int reg2 ){
708 //オペコード
709 pNativeCode->Put( (char)0x3B );
710
711 //レジスタ
712 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
713}
714void CodeGenerator::op_cmp_value(int op_size,int reg,char byte_data){
715 //cmp reg,byte_data
716
717 if(op_size==sizeof(char)&&reg==REG_EAX){
718 //alレジスタの場合は特殊
719 pNativeCode->Put( (char)0x3C );
720
721 //8ビット値
722 pNativeCode->Put( byte_data );
723
724 return;
725 }
726
727 //16ビット演算のプリフィックス
728 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
729
730 //オペコード
731 if(op_size==sizeof(char)) pNativeCode->Put( (char)0x80 );
732 else pNativeCode->Put( (char)0x83 );
733
734 //レジスタ
735 pNativeCode->Put( (char)(0xF8| REGISTER_OPERAND(reg)) );
736
737 //8ビット値
738 pNativeCode->Put( byte_data );
739}
740void CodeGenerator::op_setne( int reg ){
741 //オペコード
742 pNativeCode->Put( (char)0x0F );
743 pNativeCode->Put( (char)0x95 );
744
745 //レジスタ
746 pNativeCode->Put( (char)( 0xC0 | REGISTER_OPERAND(reg) ) );
747}
748
749
750
751////////////////////
752// test関連
753////////////////////
754
755void CodeGenerator::op_test(int reg1,int reg2){
756 //test reg1,reg2
757
758 //1000 0101 11rr rbbb
759 pNativeCode->Put( (char)0x85 );
760 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
761}
762void CodeGenerator::op_test_ah( char cValue )
763{
764 pNativeCode->Put( (char)0xF6 );
765 pNativeCode->Put( (char)0xC4 );
766 pNativeCode->Put( cValue );
767}
768
769
770
771//////////////////////////////
772// 浮動小数点関連
773//////////////////////////////
774
775void CodeGenerator::op_fld_ptr_esp(int type){
776 //スタックポインタが示すバッファのデータを浮動小数点レジスタへロード
777
778 if(type==DEF_DOUBLE){
779 //fld qword ptr[esp]
780 pNativeCode->Put( (char)0xDD );
781 pNativeCode->Put( (char)0x04 );
782 pNativeCode->Put( (char)0x24 );
783 }
784 else if(type==DEF_SINGLE){
785 //fld dword ptr[esp]
786 pNativeCode->Put( (char)0xD9 );
787 pNativeCode->Put( (char)0x04 );
788 pNativeCode->Put( (char)0x24 );
789 }
790 else if(type==DEF_INT64){
791 //fild qword ptr[esp]
792 pNativeCode->Put( (char)0xDF );
793 pNativeCode->Put( (char)0x2C );
794 pNativeCode->Put( (char)0x24 );
795 }
796 else if(type==DEF_LONG){
797 //fild dword ptr[esp]
798 pNativeCode->Put( (char)0xDB );
799 pNativeCode->Put( (char)0x04 );
800 pNativeCode->Put( (char)0x24 );
801 }
802}
803void CodeGenerator::op_fld_basereg(int type,int base_reg){
804 //fld ptr[reg]
805
806 //オペコード
807 if(type==DEF_DOUBLE) pNativeCode->Put( (char)0xDD );
808 else if(type==DEF_SINGLE) pNativeCode->Put( (char)0xD9 );
809 else SetError(300,NULL,cp);
810
811 if(base_reg==REG_ESP){
812 pNativeCode->Put( (char)0x04 );
813 pNativeCode->Put( (char)0x24 );
814 }
815 else if(base_reg==REG_EBP){
816 pNativeCode->Put( (char)0x45 );
817 pNativeCode->Put( (char)0x00 );
818 }
819 else{
820 pNativeCode->Put( (char)REGISTER_OPERAND(base_reg) );
821 }
822}
823void CodeGenerator::op_fld_base_offset(int type,int base_reg,long offset){
824 //fld ptr[reg+offset]
825
826 //オペコード
827 if(type==DEF_DOUBLE) pNativeCode->Put( (char)0xDD );
828 else if(type==DEF_SINGLE) pNativeCode->Put( (char)0xD9 );
829 else SetError(300,NULL,cp);
830
831 //オペコード、レジスタ
832 if(base_reg==REG_ESP){
833 pNativeCode->Put( (char)0x84 );
834 pNativeCode->Put( (char)0x24 );
835 }
836 else{
837 pNativeCode->Put( (char)(0x80|REGISTER_OPERAND(base_reg)) );
838 }
839
840 //オフセット値
841 pNativeCode->Put( offset );
842}
843void CodeGenerator::op_fld_base_offset_ex(int type,int base_reg1,int base_reg2,long offset,BOOL bUseOffset){
844 //fld ptr[base_reg1+base_reg2+offset]
845
846 if(base_reg1==REG_ESP){
847 //SIBバイトのindex部にespは指定できない
848 base_reg1=base_reg2;
849 base_reg2=REG_ESP;
850 }
851
852 //オペコード
853 if(type==DEF_DOUBLE) pNativeCode->Put( (char)0xDD );
854 else if(type==DEF_SINGLE) pNativeCode->Put( (char)0xD9 );
855 else SetError(300,NULL,cp);
856
857 int reg=0;
858 if(bUseOffset){
859 ///////////////////////////
860 // オフセット値を使う
861 ///////////////////////////
862
863 //レジスタ
864 pNativeCode->Put( (char)(0x84| REGISTER_OPERAND(reg)<<3) );
865
866 //ベースレジスタ
867 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
868
869 //オフセット値
870 pNativeCode->Put( offset );
871 }
872 else{
873 ///////////////////////////
874 // オフセット値を使わない
875 ///////////////////////////
876
877 //レジスタ
878 pNativeCode->Put( (char)(0x04| REGISTER_OPERAND(reg)<<3) );
879
880 //ベースレジスタ
881 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
882 }
883}
884void CodeGenerator::op_fstp_basereg(int type,int base_reg){
885 //fstp ptr[reg]
886
887 //オペコード
888 if(type==DEF_DOUBLE) pNativeCode->Put( (char)0xDD );
889 else if(type==DEF_SINGLE) pNativeCode->Put( (char)0xD9 );
890 else SetError(300,NULL,cp);
891
892 if(base_reg==REG_ESP){
893 pNativeCode->Put( (char)0x1C );
894 pNativeCode->Put( (char)0x24 );
895 }
896 else if(base_reg==REG_EBP){
897 pNativeCode->Put( (char)0x5D );
898 pNativeCode->Put( (char)0x00 );
899 }
900 else{
901 pNativeCode->Put( (char)(0x18|REGISTER_OPERAND(base_reg)) );
902 }
903}
904void CodeGenerator::op_fstp_base_offset(int type,int base_reg,long offset){
905 //fstp ptr[reg+offset]
906
907 //オペコード
908 if(type==DEF_DOUBLE) pNativeCode->Put( (char)0xDD );
909 else if(type==DEF_SINGLE) pNativeCode->Put( (char)0xD9 );
910 else SetError(300,NULL,cp);
911
912 //オペコード、レジスタ
913 if(base_reg==REG_ESP){
914 pNativeCode->Put( (char)0x9C );
915 pNativeCode->Put( (char)0x24 );
916 }
917 else{
918 pNativeCode->Put( (char)(0x98|REGISTER_OPERAND(base_reg)) );
919 }
920
921 //オフセット値
922 pNativeCode->Put( offset );
923}
924void CodeGenerator::op_fstp_base_offset_ex(int type,int base_reg1,int base_reg2,long offset,BOOL bUseOffset){
925 //fstp ptr[base_reg1+base_reg2+offset]
926
927 if(base_reg1==REG_ESP){
928 //SIBバイトのindex部にespは指定できない
929 base_reg1=base_reg2;
930 base_reg2=REG_ESP;
931 }
932
933 //オペコード
934 if(type==DEF_DOUBLE) pNativeCode->Put( (char)0xDD );
935 else if(type==DEF_SINGLE) pNativeCode->Put( (char)0xD9 );
936 else SetError(300,NULL,cp);
937
938 int reg=0;
939 if(bUseOffset){
940 ///////////////////////////
941 // オフセット値を使う
942 ///////////////////////////
943
944 //レジスタ
945 pNativeCode->Put( (char)(0x9C| REGISTER_OPERAND(reg)<<3) );
946
947 //ベースレジスタ
948 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
949
950 //オフセット値
951 pNativeCode->Put( offset );
952 }
953 else{
954 ///////////////////////////
955 // オフセット値を使わない
956 ///////////////////////////
957
958 //レジスタ
959 pNativeCode->Put( (char)(0x1C| REGISTER_OPERAND(reg)<<3) );
960
961 //ベースレジスタ
962 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
963 }
964}
965void CodeGenerator::op_fistp_ptr_esp( int typeSize ){
966 if( typeSize == sizeof(_int64) ){
967 //64bit
968
969 //fistp qword ptr[esp]
970 fpu_cast();
971 pNativeCode->Put( (char)0xDF );
972 pNativeCode->Put( (char)0x3C );
973 pNativeCode->Put( (char)0x24 );
974 fpu_cast_end();
975 }
976 else if( typeSize == sizeof(long) ){
977 //32bit
978
979 //fistp dword ptr[esp]
980 fpu_cast();
981 pNativeCode->Put( (char)0xDB );
982 pNativeCode->Put( (char)0x1C );
983 pNativeCode->Put( (char)0x24 );
984 fpu_cast_end();
985 }
986 else{
987 SetError();
988 }
989}
990void CodeGenerator::op_fstp_push( Type &type ){
991 //sub esp,size
992 op_sub_esp( type.GetBasicSize() );
993
994 op_fstp_basereg( type.GetBasicType(), REG_ESP );
995}
996void CodeGenerator::op_fcompp(){
997 // fcompp
998 pNativeCode->Put( (char)0xDE );
999 pNativeCode->Put( (char)0xD9 );
1000}
1001void CodeGenerator::op_fnstsw_ax()
1002{
1003 // fnstsw ax
1004 pNativeCode->Put( (char)0xDF );
1005 pNativeCode->Put( (char)0xE0 );
1006}
1007
1008
1009
1010//////////////////////////////
1011// レジスタ関連
1012//////////////////////////////
1013
1014void CodeGenerator::op_zero_reg(int reg){
1015 //レジスタに0をセット
1016
1017 op_xor_RR( reg );
1018}
1019
1020void CodeGenerator::fpu_cast(){
1021 ///////////////////////
1022 // FPUの切り捨て設定
1023 ///////////////////////
1024
1025 //sub esp,16
1026 op_sub_esp(16);
1027
1028 //mov dword ptr[esp+4],eax
1029 pNativeCode->Put( (char)0x89 );
1030 pNativeCode->Put( (char)0x44 );
1031 pNativeCode->Put( (char)0x24 );
1032 pNativeCode->Put( (char)0x04 );
1033
1034 //fnstcw word ptr[esp]
1035 pNativeCode->Put( (char)0xD9 );
1036 pNativeCode->Put( (char)0x3C );
1037 pNativeCode->Put( (char)0x24 );
1038
1039 //mov ax,word ptr[esp]
1040 pNativeCode->Put( (char)0x66 );
1041 pNativeCode->Put( (char)0x8B );
1042 pNativeCode->Put( (char)0x04 );
1043 pNativeCode->Put( (char)0x24 );
1044
1045 //or ah,0Ch
1046 pNativeCode->Put( (char)0x80 );
1047 pNativeCode->Put( (char)0xCC );
1048 pNativeCode->Put( (char)0x0C );
1049
1050 //mov word ptr[esp+2],ax
1051 pNativeCode->Put( (char)0x66 );
1052 pNativeCode->Put( (char)0x89 );
1053 pNativeCode->Put( (char)0x44 );
1054 pNativeCode->Put( (char)0x24 );
1055 pNativeCode->Put( (char)0x02 );
1056
1057 //fldcw word ptr[esp+2]
1058 pNativeCode->Put( (char)0xD9 );
1059 pNativeCode->Put( (char)0x6C );
1060 pNativeCode->Put( (char)0x24 );
1061 pNativeCode->Put( (char)0x02 );
1062
1063 //mov eax,dword ptr[esp+4]
1064 pNativeCode->Put( (char)0x8B );
1065 pNativeCode->Put( (char)0x44 );
1066 pNativeCode->Put( (char)0x24 );
1067 pNativeCode->Put( (char)0x04 );
1068
1069 //add esp,16
1070 op_add_esp(16);
1071}
1072void CodeGenerator::fpu_cast_end(){
1073 //sub esp,16
1074 op_sub_esp(16);
1075
1076 //fldcw word ptr[esp]
1077 pNativeCode->Put( (char)0xD9 );
1078 pNativeCode->Put( (char)0x2C );
1079 pNativeCode->Put( (char)0x24 );
1080
1081 //add esp,16
1082 op_add_esp(16);
1083}
1084
1085
1086/////////////////////////////
1087// 関数呼び出し
1088/////////////////////////////
1089
1090void CodeGenerator::op_call(const UserProc *pUserProc){
1091 pUserProc->Using();
1092
1093 pNativeCode->Put( (char)0xE8 );
1094 pobj_SubAddrSchedule->add(pUserProc,1);
1095 pNativeCode->Put( (long)0 );
1096}
1097void CodeGenerator::op_ret(){
1098 pNativeCode->Put( (char)0xC3 );
1099}
Note: See TracBrowser for help on using the repository browser.