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

Last change on this file since 234 was 234, checked in by dai_9181, 17 years ago
File size: 27.1 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 // push dword ptr[base_reg]
713 __op_format( (char)0xFF, (char)0x30, base_reg );
714}
715void CodeGenerator::op_push_M( int base_reg, long offset, Schedule::Type scheduleType )
716{
717 if( base_reg == REG_NON )
718 {
719 // push dword ptr[offset]
720 __op_format( 0, (char)0xFF, 0, /*opcode->*/0x06, 0, offset, MOD_DISP32, scheduleType );
721 }
722 else
723 {
724 // push dword ptr[base_reg+offset]
725 __op_format( 0, (char)0xFF, 0, /*opcode->*/0x06, base_reg, offset, MOD_BASE_DISP32, scheduleType );
726 }
727}
728void CodeGenerator::op_pop(int reg){
729 //pop reg
730
731 if( reg == REG_NON ){
732 op_add_esp( PTR_SIZE );
733 return;
734 }
735
736 //オペコード、レジスタ
737 __op_format(0,(char)0x58,reg);
738}
739void CodeGenerator::op_add_esp(long num){
740 //スタックポインタの加算(pop方向)
741
742 //add esp,num
743 if(0xFFFFFF80&num){
744 pNativeCode->Put( (char)0x81 );
745 pNativeCode->Put( (char)0xC4 );
746 pNativeCode->Put( num );
747 }
748 else{
749 //「128 > num > -127」の場合
750 pNativeCode->Put( (char)0x83 );
751 pNativeCode->Put( (char)0xC4 );
752 pNativeCode->Put( (char)num );
753 }
754}
755void CodeGenerator::op_sub_esp(long num){
756 //スタックポインタの減算(push方向)
757
758 //sub esp,num
759 if(0xFFFFFF80&num){
760 pNativeCode->Put( (char)0x81 );
761 pNativeCode->Put( (char)0xEC );
762 pNativeCode->Put( num );
763 }
764 else{
765 //「128 > num > -127」の場合
766 pNativeCode->Put( (char)0x83 );
767 pNativeCode->Put( (char)0xEC );
768 pNativeCode->Put( (char)num );
769 }
770}
771
772
773
774/////////////////////
775// cmp関連
776/////////////////////
777void CodeGenerator::op_cmp_RR( int reg1, int reg2 ){
778 //オペコード
779 pNativeCode->Put( (char)0x3B );
780
781 //レジスタ
782 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
783}
784void CodeGenerator::op_cmp_value(int op_size,int reg,char byte_data){
785 //cmp reg,byte_data
786
787 if(op_size==sizeof(char)&&reg==REG_EAX){
788 //alレジスタの場合は特殊
789 pNativeCode->Put( (char)0x3C );
790
791 //8ビット値
792 pNativeCode->Put( byte_data );
793
794 return;
795 }
796
797 //16ビット演算のプリフィックス
798 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
799
800 //オペコード
801 if(op_size==sizeof(char)) pNativeCode->Put( (char)0x80 );
802 else pNativeCode->Put( (char)0x83 );
803
804 //レジスタ
805 pNativeCode->Put( (char)(0xF8| REGISTER_OPERAND(reg)) );
806
807 //8ビット値
808 pNativeCode->Put( byte_data );
809}
810void CodeGenerator::op_setne( int reg ){
811 //オペコード
812 pNativeCode->Put( (char)0x0F );
813 pNativeCode->Put( (char)0x95 );
814
815 //レジスタ
816 pNativeCode->Put( (char)( 0xC0 | REGISTER_OPERAND(reg) ) );
817}
818
819
820
821////////////////////
822// test関連
823////////////////////
824
825void CodeGenerator::op_test(int reg1,int reg2){
826 //test reg1,reg2
827
828 //1000 0101 11rr rbbb
829 pNativeCode->Put( (char)0x85 );
830 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
831}
832void CodeGenerator::op_test_ah( char cValue )
833{
834 pNativeCode->Put( (char)0xF6 );
835 pNativeCode->Put( (char)0xC4 );
836 pNativeCode->Put( cValue );
837}
838
839
840
841//////////////////////////////
842// 浮動小数点関連
843//////////////////////////////
844
845void CodeGenerator::op_fld_ptr_esp(int type){
846 //スタックポインタが示すバッファのデータを浮動小数点レジスタへロード
847
848 if(type==DEF_DOUBLE){
849 //fld qword ptr[esp]
850 pNativeCode->Put( (char)0xDD );
851 pNativeCode->Put( (char)0x04 );
852 pNativeCode->Put( (char)0x24 );
853 }
854 else if(type==DEF_SINGLE){
855 //fld dword ptr[esp]
856 pNativeCode->Put( (char)0xD9 );
857 pNativeCode->Put( (char)0x04 );
858 pNativeCode->Put( (char)0x24 );
859 }
860 else if(type==DEF_INT64){
861 //fild qword ptr[esp]
862 pNativeCode->Put( (char)0xDF );
863 pNativeCode->Put( (char)0x2C );
864 pNativeCode->Put( (char)0x24 );
865 }
866 else if(type==DEF_LONG){
867 //fild dword ptr[esp]
868 pNativeCode->Put( (char)0xDB );
869 pNativeCode->Put( (char)0x04 );
870 pNativeCode->Put( (char)0x24 );
871 }
872}
873void CodeGenerator::op_fld_basereg(int type,int base_reg){
874 //fld ptr[reg]
875
876 //オペコード
877 if(type==DEF_DOUBLE) pNativeCode->Put( (char)0xDD );
878 else if(type==DEF_SINGLE) pNativeCode->Put( (char)0xD9 );
879 else SetError(300,NULL,cp);
880
881 if(base_reg==REG_ESP){
882 pNativeCode->Put( (char)0x04 );
883 pNativeCode->Put( (char)0x24 );
884 }
885 else if(base_reg==REG_EBP){
886 pNativeCode->Put( (char)0x45 );
887 pNativeCode->Put( (char)0x00 );
888 }
889 else{
890 pNativeCode->Put( (char)REGISTER_OPERAND(base_reg) );
891 }
892}
893void CodeGenerator::op_fld_base_offset(int type,int base_reg,long offset, Schedule::Type scheduleType ){
894 //fld ptr[reg+offset]
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 //オペコード、レジスタ
902 if(base_reg==REG_ESP){
903 pNativeCode->Put( (char)0x84 );
904 pNativeCode->Put( (char)0x24 );
905 }
906 else{
907 pNativeCode->Put( (char)(0x80|REGISTER_OPERAND(base_reg)) );
908 }
909
910 //オフセット値
911 pNativeCode->Put( offset, scheduleType );
912}
913void CodeGenerator::op_fld_base_offset_ex(int type,int base_reg1,int base_reg2,long offset,BOOL bUseOffset, Schedule::Type scheduleType ){
914 //fld ptr[base_reg1+base_reg2+offset]
915
916 if(base_reg1==REG_ESP){
917 //SIBバイトのindex部にespは指定できない
918 base_reg1=base_reg2;
919 base_reg2=REG_ESP;
920 }
921
922 //オペコード
923 if(type==DEF_DOUBLE) pNativeCode->Put( (char)0xDD );
924 else if(type==DEF_SINGLE) pNativeCode->Put( (char)0xD9 );
925 else SetError(300,NULL,cp);
926
927 int reg=0;
928 if(bUseOffset){
929 ///////////////////////////
930 // オフセット値を使う
931 ///////////////////////////
932
933 //レジスタ
934 pNativeCode->Put( (char)(0x84| REGISTER_OPERAND(reg)<<3) );
935
936 //ベースレジスタ
937 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
938
939 //オフセット値
940 pNativeCode->Put( offset, scheduleType );
941 }
942 else{
943 ///////////////////////////
944 // オフセット値を使わない
945 ///////////////////////////
946
947 //レジスタ
948 pNativeCode->Put( (char)(0x04| REGISTER_OPERAND(reg)<<3) );
949
950 //ベースレジスタ
951 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
952 }
953}
954void CodeGenerator::op_fstp_basereg(int type,int base_reg){
955 //fstp ptr[reg]
956
957 //オペコード
958 if(type==DEF_DOUBLE) pNativeCode->Put( (char)0xDD );
959 else if(type==DEF_SINGLE) pNativeCode->Put( (char)0xD9 );
960 else SetError(300,NULL,cp);
961
962 if(base_reg==REG_ESP){
963 pNativeCode->Put( (char)0x1C );
964 pNativeCode->Put( (char)0x24 );
965 }
966 else if(base_reg==REG_EBP){
967 pNativeCode->Put( (char)0x5D );
968 pNativeCode->Put( (char)0x00 );
969 }
970 else{
971 pNativeCode->Put( (char)(0x18|REGISTER_OPERAND(base_reg)) );
972 }
973}
974void CodeGenerator::op_fstp_base_offset(int type,int base_reg,long offset, Schedule::Type scheduleType ){
975 //fstp ptr[reg+offset]
976
977 //オペコード
978 if(type==DEF_DOUBLE) pNativeCode->Put( (char)0xDD );
979 else if(type==DEF_SINGLE) pNativeCode->Put( (char)0xD9 );
980 else SetError(300,NULL,cp);
981
982 //オペコード、レジスタ
983 if(base_reg==REG_ESP){
984 pNativeCode->Put( (char)0x9C );
985 pNativeCode->Put( (char)0x24 );
986 }
987 else{
988 pNativeCode->Put( (char)(0x98|REGISTER_OPERAND(base_reg)) );
989 }
990
991 //オフセット値
992 pNativeCode->Put( offset, scheduleType );
993}
994void CodeGenerator::op_fstp_base_offset_ex(int type,int base_reg1,int base_reg2,long offset,BOOL bUseOffset, Schedule::Type scheduleType ){
995 //fstp ptr[base_reg1+base_reg2+offset]
996
997 if(base_reg1==REG_ESP){
998 //SIBバイトのindex部にespは指定できない
999 base_reg1=base_reg2;
1000 base_reg2=REG_ESP;
1001 }
1002
1003 //オペコード
1004 if(type==DEF_DOUBLE) pNativeCode->Put( (char)0xDD );
1005 else if(type==DEF_SINGLE) pNativeCode->Put( (char)0xD9 );
1006 else SetError(300,NULL,cp);
1007
1008 int reg=0;
1009 if(bUseOffset){
1010 ///////////////////////////
1011 // オフセット値を使う
1012 ///////////////////////////
1013
1014 //レジスタ
1015 pNativeCode->Put( (char)(0x9C| REGISTER_OPERAND(reg)<<3) );
1016
1017 //ベースレジスタ
1018 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
1019
1020 //オフセット値
1021 pNativeCode->Put( offset, scheduleType );
1022 }
1023 else{
1024 ///////////////////////////
1025 // オフセット値を使わない
1026 ///////////////////////////
1027
1028 //レジスタ
1029 pNativeCode->Put( (char)(0x1C| REGISTER_OPERAND(reg)<<3) );
1030
1031 //ベースレジスタ
1032 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
1033 }
1034}
1035void CodeGenerator::op_fistp_ptr_esp( int typeSize ){
1036 if( typeSize == sizeof(_int64) ){
1037 //64bit
1038
1039 //fistp qword ptr[esp]
1040 fpu_cast();
1041 pNativeCode->Put( (char)0xDF );
1042 pNativeCode->Put( (char)0x3C );
1043 pNativeCode->Put( (char)0x24 );
1044 fpu_cast_end();
1045 }
1046 else if( typeSize == sizeof(long) ){
1047 //32bit
1048
1049 //fistp dword ptr[esp]
1050 fpu_cast();
1051 pNativeCode->Put( (char)0xDB );
1052 pNativeCode->Put( (char)0x1C );
1053 pNativeCode->Put( (char)0x24 );
1054 fpu_cast_end();
1055 }
1056 else{
1057 SetError();
1058 }
1059}
1060void CodeGenerator::op_fstp_push( Type &type ){
1061 //sub esp,size
1062 op_sub_esp( type.GetBasicSize() );
1063
1064 op_fstp_basereg( type.GetBasicType(), REG_ESP );
1065}
1066void CodeGenerator::op_fcompp(){
1067 // fcompp
1068 pNativeCode->Put( (char)0xDE );
1069 pNativeCode->Put( (char)0xD9 );
1070}
1071void CodeGenerator::op_fnstsw_ax()
1072{
1073 // fnstsw ax
1074 pNativeCode->Put( (char)0xDF );
1075 pNativeCode->Put( (char)0xE0 );
1076}
1077
1078
1079
1080//////////////////////////////
1081// レジスタ関連
1082//////////////////////////////
1083
1084void CodeGenerator::op_zero_reg(int reg){
1085 //レジスタに0をセット
1086
1087 op_xor_RR( reg );
1088}
1089
1090void CodeGenerator::fpu_cast(){
1091 ///////////////////////
1092 // FPUの切り捨て設定
1093 ///////////////////////
1094
1095 //sub esp,16
1096 op_sub_esp(16);
1097
1098 //mov dword ptr[esp+4],eax
1099 pNativeCode->Put( (char)0x89 );
1100 pNativeCode->Put( (char)0x44 );
1101 pNativeCode->Put( (char)0x24 );
1102 pNativeCode->Put( (char)0x04 );
1103
1104 //fnstcw word ptr[esp]
1105 pNativeCode->Put( (char)0xD9 );
1106 pNativeCode->Put( (char)0x3C );
1107 pNativeCode->Put( (char)0x24 );
1108
1109 //mov ax,word ptr[esp]
1110 pNativeCode->Put( (char)0x66 );
1111 pNativeCode->Put( (char)0x8B );
1112 pNativeCode->Put( (char)0x04 );
1113 pNativeCode->Put( (char)0x24 );
1114
1115 //or ah,0Ch
1116 pNativeCode->Put( (char)0x80 );
1117 pNativeCode->Put( (char)0xCC );
1118 pNativeCode->Put( (char)0x0C );
1119
1120 //mov word ptr[esp+2],ax
1121 pNativeCode->Put( (char)0x66 );
1122 pNativeCode->Put( (char)0x89 );
1123 pNativeCode->Put( (char)0x44 );
1124 pNativeCode->Put( (char)0x24 );
1125 pNativeCode->Put( (char)0x02 );
1126
1127 //fldcw word ptr[esp+2]
1128 pNativeCode->Put( (char)0xD9 );
1129 pNativeCode->Put( (char)0x6C );
1130 pNativeCode->Put( (char)0x24 );
1131 pNativeCode->Put( (char)0x02 );
1132
1133 //mov eax,dword ptr[esp+4]
1134 pNativeCode->Put( (char)0x8B );
1135 pNativeCode->Put( (char)0x44 );
1136 pNativeCode->Put( (char)0x24 );
1137 pNativeCode->Put( (char)0x04 );
1138
1139 //add esp,16
1140 op_add_esp(16);
1141}
1142void CodeGenerator::fpu_cast_end(){
1143 //sub esp,16
1144 op_sub_esp(16);
1145
1146 //fldcw word ptr[esp]
1147 pNativeCode->Put( (char)0xD9 );
1148 pNativeCode->Put( (char)0x2C );
1149 pNativeCode->Put( (char)0x24 );
1150
1151 //add esp,16
1152 op_add_esp(16);
1153}
1154
1155
1156/////////////////////////////
1157// 関数呼び出し
1158/////////////////////////////
1159
1160void CodeGenerator::op_call(const UserProc *pUserProc){
1161 pUserProc->Using();
1162
1163 pNativeCode->Put( (char)0xE8 );
1164 pobj_SubAddrSchedule->add(pUserProc,1);
1165 pNativeCode->Put( (long)0 );
1166}
1167void CodeGenerator::op_ret(){
1168 pNativeCode->Put( (char)0xC3 );
1169}
Note: See TracBrowser for help on using the repository browser.