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

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