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

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