source: dev/trunk/ab5.0/abdev/compiler_x86/x86CodeGenerator.cpp@ 603

Last change on this file since 603 was 603, checked in by dai_9181, 16 years ago

ObjectModuleに関連するクラス一式をab_commonプロジェクトに移動した。

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