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

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