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

Last change on this file since 234 was 232, checked in by dai_9181, 17 years ago
File size: 45.0 KB
RevLine 
[226]1#include "stdafx.h"
2
3#include <Procedure.h>
4#include <CodeGenerator.h>
5
6
7//////////////////////
8// rexプリフィックス
9//////////////////////
10void CodeGenerator::set_rex(int op_size,int reg,int index_reg,int base_reg){
11 char RexByte;
12
13 if(reg==REG_NON&&index_reg==REG_NON){
14 /////////////////////////////////////
15 // レジスタをr/mのみに指定するとき
16 /////////////////////////////////////
17
18 if((base_reg&0x08)==0){
19 if(op_size==sizeof(char)&&(base_reg&0x04)){
20 // r/m に spl,bpl,sil,dilを指定するとき
21 RexByte=0x40;
22 }
23 else RexByte=0;
24 }
25 else RexByte=(char)0x41;
26 }
27 else{
28 /////////////////
29 // 通常
30 /////////////////
31
32 if((reg&0x08)==0){
33 //reg … rax~rdi
34
35 if((index_reg&0x08)==0){
36 if((base_reg&0x08)==0) RexByte=0;
37 else RexByte=(char)0x41;
38 }
39 else{
40 if((base_reg&0x08)==0) RexByte=(char)0x42;
41 else RexByte=(char)0x43;
42 }
43 }
44 else{
45 //reg … r8~r15
46
47 if((index_reg&0x08)==0){
48 if((base_reg&0x08)==0) RexByte=(char)0x44;
49 else RexByte=(char)0x45;
50 }
51 else{
52 if((base_reg&0x08)==0) RexByte=(char)0x46;
53 else RexByte=(char)0x47;
54 }
55 }
56 }
57
58 if(op_size==sizeof(_int64)){
59 //64ビットオペランド
60 RexByte|=0x48;
61 }
62
63 if(RexByte) pNativeCode->Put( RexByte );
64}
65
66
67
68/////////////////////////////////////////////////
69// ModR/Mバイト、SIBバイト、ディスプレースメント
70/////////////////////////////////////////////////
71
72//スケール
73#define SCALE_NON (char)0x00
74#define SCALE_2 (char)0x40
75#define SCALE_4 (char)0x80
76#define SCALE_8 (char)0xC0
77
78//インデックスなし
79#define INDEX_NON 0x04
80
[228]81void CodeGenerator::set_mod_rm_sib_disp(char mod,int reg,int scale,int index_reg,int base_reg,long disp, Schedule::Type scheduleType ){
[226]82 if(mod==MOD_DISP32){
83 //ModR/Mバイト
84 pNativeCode->Put( (char)( REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(0x04)) );
85
86 base_reg=0x05;
87 index_reg=INDEX_NON;
88 }
89 else{
90 //ModR/Mバイト
91 pNativeCode->Put( (char)(mod | REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(base_reg)) );
92 }
93
94
95 //レジスタモードの場合は、ここで終了
96 if(mod==MOD_REG) return;
97
98
99 if(REGISTER_OPERAND(base_reg)==0x04||mod==MOD_DISP32){
100 //////////////////////
101 // SIBバイトを使う
102 //////////////////////
103
104 pNativeCode->Put( (char)(scale| REGISTER_OPERAND(index_reg)<<3 | REGISTER_OPERAND(base_reg)) );
105 }
106
107 //ディスプレースメントを必要としない場合は、ここで終了
108 if(mod==MOD_BASE) return;
109
110
111 //////////////////////////
112 // ディスプレースメント
113 //////////////////////////
114
115 if(mod==MOD_BASE_DISP8) pNativeCode->Put( (char)disp );
116 else{
[228]117 pNativeCode->Put( disp, scheduleType );
[226]118 }
119}
120
121
122
[228]123void CodeGenerator::__op_format(int op_size,char op_prefix,char opcode1,char opcode2,int reg,int base_reg,long offset,char mod, Schedule::Type scheduleType ){
[226]124 //命令プリフィックス
125 if(op_prefix) pNativeCode->Put( op_prefix );
126
127 //rexプリフィックス
128 set_rex(op_size,reg,0,base_reg);
129
130 //オペコード
131 pNativeCode->Put( opcode1 );
132 if(opcode2) pNativeCode->Put( opcode2 );
133
134 //ModR/M, SIB, disp
[228]135 set_mod_rm_sib_disp(mod,reg,SCALE_NON,INDEX_NON,base_reg,offset, scheduleType );
[226]136}
137
[228]138void CodeGenerator::__jmp_op_format( char opcode, long offset, int op_size )
139{
140 pNativeCode->Put( opcode );
141 if( op_size == sizeof(char) )
142 {
143 pNativeCode->Put( (char)offset );
144 }
145 else if( op_size == sizeof(long) )
146 {
147 SetError();
148 pNativeCode->Put( offset );
149 }
150 else
151 {
152 SetError();
153 }
154}
[226]155
156
[228]157
[226]158///////////////////
159// mov関連
160///////////////////
161
[232]162void CodeGenerator::op_mov_RV(int op_size,int reg,long i32data, Schedule::Type scheduleType ){
[226]163 //mov reg,i32data
164
165 //rexプリフィックス
166 set_rex(op_size,REG_NON,REG_NON,reg);
167
168 if(op_size==sizeof(_int64)){
169 //オペコード
170 pNativeCode->Put( (char)0xC7 );
171
172 //レジスタ
173 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg) ) );
174 }
175 else{
176 //レジスタ
177 pNativeCode->Put( (char)(0xB8| REGISTER_OPERAND(reg) ) );
178 }
179
180 //即値
[232]181 pNativeCode->Put( i32data, scheduleType );
[226]182}
183void CodeGenerator::op_mov_RV64(int reg,_int64 i64data){
184 //mov reg,i64data
185
186 //rexプリフィックス
187 set_rex(sizeof(_int64),REG_NON,REG_NON,reg);
188
189 //レジスタ
190 pNativeCode->Put( (char)(0xB8| REGISTER_OPERAND(reg) ) );
191
192 //即値
193 pNativeCode->Put( i64data );
194}
[228]195void CodeGenerator::op_mov_RM(int op_size,int reg,int base_reg,long offset,char mod, Schedule::Type scheduleType ){
[226]196 //mov reg64,qword ptr[base_reg+offset]
197 //mov reg32,dword ptr[base_reg+offset]
198 //mov reg16,word ptr[base_reg+offset]
199 //mov reg8,byte ptr[base_reg+offset]
200
201 //16ビット演算の命令プリフィックス
202 char op_prefix=0;
203 if(op_size==sizeof(short)) op_prefix=(char)0x66;
204
205 //オペコード
206 char opcode;
207 if(op_size==sizeof(char)) opcode=(char)0x8A;
208 else opcode=(char)0x8B;
209
[228]210 __op_format(op_size,op_prefix,opcode,0,reg,base_reg,offset,mod, scheduleType);
[226]211}
[228]212void CodeGenerator::op_mov_RM_ex(int op_size,int reg,int base_reg1,int base_reg2,long offset,BOOL bUseOffset, Schedule::Type scheduleType ){
[226]213 //mov reg64,qword ptr[base_reg1+base_reg2+offset]
214 //mov reg32,dword ptr[base_reg1+base_reg2+offset]
215 //mov reg16,word ptr[base_reg1+base_reg2+offset]
216 //mov reg8,byte ptr[base_reg1+base_reg2+offset]
217
218 if(base_reg1==REG_RSP){
219 //SIBバイトのindex部にrspは指定できない
220 base_reg1=base_reg2;
221 base_reg2=REG_RSP;
222 }
223
224 //16ビット演算のプリフィックス
225 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
226
227 //rexプリフィックス
228 set_rex(op_size,reg,base_reg1,base_reg2);
229
230 //オペコード
231 if(op_size==sizeof(char)) pNativeCode->Put( (char)0x8A );
232 else pNativeCode->Put( (char)0x8B );
233
234 if(bUseOffset){
235 ///////////////////////////
236 // オフセット値を使う
237 ///////////////////////////
238
239 //レジスタ
240 pNativeCode->Put( (char)(0x84| REGISTER_OPERAND(reg)<<3) );
241
242 //ベースレジスタ
243 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
244
245 //オフセット値
[228]246 pNativeCode->Put( offset, scheduleType );
[226]247 }
248 else{
249 ///////////////////////////
250 // オフセット値を使わない
251 ///////////////////////////
252
253 //レジスタ
254 pNativeCode->Put( (char)(0x04| REGISTER_OPERAND(reg)<<3) );
255
256 //ベースレジスタ
257 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
258 }
259}
[232]260void CodeGenerator::op_mov_MR(int op_size,int reg,int base_reg,long offset,char mod, Schedule::Type scheduleType ){
[226]261 //mov qword ptr[base_reg+offset],reg64
262 //mov dword ptr[base_reg+offset],reg32
263 //mov word ptr[base_reg+offset],reg16
264 //mov byte ptr[base_reg+offset],reg8
265
266 //16ビット演算の命令プリフィックス
267 char op_prefix=0;
268 if(op_size==sizeof(short)) op_prefix=(char)0x66;
269
270 //オペコード
271 char opcode;
272 if(op_size==sizeof(char)) opcode=(char)0x88;
273 else opcode=(char)0x89;
274
[232]275 __op_format(op_size,op_prefix,opcode,0,reg,base_reg,offset,mod, scheduleType );
[226]276}
[232]277void CodeGenerator::op_mov_MR_ex(int op_size,int reg,int base_reg1,int base_reg2,long offset,BOOL bUseOffset, Schedule::Type scheduleType ){
[226]278 //mov qword ptr[base_reg1+base_reg2+offset],reg64
279 //mov dword ptr[base_reg1+base_reg2+offset],reg32
280 //mov word ptr[base_reg1+base_reg2+offset],reg16
281 //mov byte ptr[base_reg1+base_reg2+offset],reg8
282
283 if(base_reg1==REG_RSP){
284 //SIBバイトのindex部にrspは指定できない
285 base_reg1=base_reg2;
286 base_reg2=REG_RSP;
287 }
288
289 //16ビット演算のプリフィックス
290 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
291
292 //rexプリフィックス
293 set_rex(op_size,reg,base_reg1,base_reg2);
294
295 //オペコード
296 if(op_size==sizeof(char)) pNativeCode->Put( (char)0x88 );
297 else pNativeCode->Put( (char)0x89 );
298
299 if(bUseOffset==USE_OFFSET){
300 //////////////////////////
301 //オフセット値を使う
302 //////////////////////////
303
304 //レジスタ
305 pNativeCode->Put( (char)(0x84| REGISTER_OPERAND(reg)<<3) );
306
307 //ベースレジスタ
308 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
309
310 //オフセット値
[232]311 pNativeCode->Put( offset, scheduleType );
[226]312 }
313 else{
314 //////////////////////////
315 //オフセット値を使わない
316 //////////////////////////
317
318 //レジスタ
319 pNativeCode->Put( (char)(0x04| REGISTER_OPERAND(reg)<<3) );
320
321 //ベースレジスタ
322 pNativeCode->Put( (char)(REGISTER_OPERAND(base_reg1)<<3 | REGISTER_OPERAND(base_reg2)) );
323 }
324}
325
326void CodeGenerator::op_mov_MV(int op_size,int base_reg,int offset,BOOL bUseOffset,long i32data){
327 //mov ptr[base_reg+offset],i32data
328 //mov ptr[base_reg ],i32data
329
330 //16ビット演算のプリフィックス
331 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
332
333 //rexプリフィックス
334 set_rex(op_size,0,0,base_reg);
335
336 //オペコード
337 if(op_size==sizeof(char)) pNativeCode->Put( (char)0xC6 );
338 else pNativeCode->Put( (char)0xC7 );
339
340 if(bUseOffset==USE_OFFSET){
341 //////////////////////////
342 //オフセット値を使う
343 //////////////////////////
344
345 //ModR/M, SIB, disp
346 set_mod_rm_sib_disp(MOD_BASE_DISP32,0,SCALE_NON,INDEX_NON,base_reg,offset);
347 }
348 else{
349 //ModR/M, SIB, disp
350 set_mod_rm_sib_disp(MOD_BASE,0,SCALE_NON,INDEX_NON,base_reg,0);
351 }
352
353 //即値
354 if(op_size==sizeof(_int64)||op_size==sizeof(long)){
355 //32/64ビット
356 pNativeCode->Put( i32data );
357 }
358 else if(op_size==sizeof(short)){
359 //16ビット
360 pNativeCode->Put( (short)i32data );
361 }
362 else if(op_size==sizeof(char)){
363 //16ビット
364 pNativeCode->Put( (char)i32data );
365 }
366}
367
368void CodeGenerator::op_mov_RR(int reg1,int reg2){
369 //mov reg1,reg2
370 char RexByte=-1;
371
372 if(reg1==reg2) return;
373
374 if(REG_RAX<=reg1&&reg1<=REG_RDI){
375 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
376 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
377 }
378 if(REG_R8<=reg1&&reg1<=REG_R15){
379 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
380 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
381 }
382
383 if(RexByte==-1) SetError(300,NULL,cp);
384
385 // [8bit rex] 1000 1011 11xx xbbb
386 pNativeCode->Put( RexByte );
387 pNativeCode->Put( (char)0x8B );
388 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
389}
390
391void CodeGenerator::op_movsxd(int reg64,int reg32){
392 //movsxd reg64,reg32
393 char RexByte=-1;
394
395 if(REG_RAX<=reg64&&reg64<=REG_RDI){
396 if(REG_RAX<=reg32&&reg32<=REG_RDI) RexByte=(char)0x48;
397 if(REG_R8<=reg32&&reg32<=REG_R15) RexByte=(char)0x49;
398 }
399 if(REG_R8<=reg64&&reg64<=REG_R15){
400 if(REG_RAX<=reg32&&reg32<=REG_RDI) RexByte=(char)0x4C;
401 if(REG_R8<=reg32&&reg32<=REG_R15) RexByte=(char)0x4D;
402 }
403
404 if(RexByte==-1) SetError(300,NULL,cp);
405
406 //[8bit rex] 0110 0011 11rr rbbb
407 pNativeCode->Put( RexByte );
408 pNativeCode->Put( (char)0x63 );
409 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg64)<<3 | REGISTER_OPERAND(reg32)) );
410}
411void CodeGenerator::op_movsx64_FromReg16(int reg64,int reg16){
412 //movsx reg64,reg16
413 char RexByte=-1;
414
415 if(REG_RAX<=reg64&&reg64<=REG_RDI){
416 if(REG_RAX<=reg16&&reg16<=REG_RDI) RexByte=(char)0x48;
417 if(REG_R8<=reg16&&reg16<=REG_R15) RexByte=(char)0x49;
418 }
419 if(REG_R8<=reg64&&reg64<=REG_R15){
420 if(REG_RAX<=reg16&&reg16<=REG_RDI) RexByte=(char)0x4C;
421 if(REG_R8<=reg16&&reg16<=REG_R15) RexByte=(char)0x4D;
422 }
423
424 if(RexByte==-1) SetError(300,NULL,cp);
425
426 //[8bit rex] 0000 1111 1011 1111 11rr rbbb
427 pNativeCode->Put( RexByte );
428 pNativeCode->Put( (char)0x0F );
429 pNativeCode->Put( (char)0xBF );
430 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg64)<<3 | REGISTER_OPERAND(reg16)) );
431}
432void CodeGenerator::op_movsx64_FromReg8(int reg64,int reg8){
433 //movsx reg64,reg8
434 char RexByte=-1;
435
436 if(REG_RAX<=reg64&&reg64<=REG_RDI){
437 if(REG_RAX<=reg8&&reg8<=REG_RDI) RexByte=(char)0x48;
438 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x49;
439 }
440 if(REG_R8<=reg64&&reg64<=REG_R15){
441 if(REG_RAX<=reg8&&reg8<=REG_RDI) RexByte=(char)0x4C;
442 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x4D;
443 }
444
445 if(RexByte==-1) SetError(300,NULL,cp);
446
447 //[8bit rex] 0000 1111 1011 1110 11rr rbbb
448 pNativeCode->Put( RexByte );
449 pNativeCode->Put( (char)0x0F );
450 pNativeCode->Put( (char)0xBE );
451 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg64)<<3 | REGISTER_OPERAND(reg8)) );
452}
453
454
455
456//////////////////
457// mov32関連
458//////////////////
459
460void CodeGenerator::op_movsx32_FromReg16(int reg32,int reg16){
461 //movsx reg32,reg16
462 char RexByte=-1;
463
464 if(REG_RAX<=reg32&&reg32<=REG_RDI){
465 if(REG_RAX<=reg16&&reg16<=REG_RDI) RexByte=0;
466 if(REG_R8<=reg16&&reg16<=REG_R15) RexByte=(char)0x41;
467 }
468 if(REG_R8<=reg32&&reg32<=REG_R15){
469 if(REG_RAX<=reg16&&reg16<=REG_RDI) RexByte=(char)0x44;
470 if(REG_R8<=reg16&&reg16<=REG_R15) RexByte=(char)0x45;
471 }
472
473 if(RexByte==-1) SetError(300,NULL,cp);
474
475 //[8bit rex] 0000 1111 1011 1111 11rr rbbb
476 if(RexByte) pNativeCode->Put( RexByte );
477 pNativeCode->Put( (char)0x0F );
478 pNativeCode->Put( (char)0xBF );
479 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg32)<<3 | REGISTER_OPERAND(reg16)) );
480}
481void CodeGenerator::op_movsx32_FromReg8(int reg32,int reg8){
482 //movsx reg32,reg8
483 char RexByte=-1;
484
485 if(REG_RAX<=reg32&&reg32<=REG_RDI){
486 if(REG_RAX<=reg8&&reg8<=REG_RBX) RexByte=0;
487 if(REG_RSP<=reg8&&reg8<=REG_RDI) RexByte=(char)0x40;
488 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x41;
489 }
490 if(REG_R8<=reg32&&reg32<=REG_R15){
491 if(REG_RAX<=reg8&&reg8<=REG_RDI) RexByte=(char)0x44;
492 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x45;
493 }
494
495 if(RexByte==-1) SetError(300,NULL,cp);
496
497 //[8bit rex] 0000 1111 1011 1110 11rr rbbb
498 if(RexByte) pNativeCode->Put( RexByte );
499 pNativeCode->Put( (char)0x0F );
500 pNativeCode->Put( (char)0xBE );
501 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg32)<<3 | REGISTER_OPERAND(reg8)) );
502}
503
504
505
506/////////////////////
507// mov16関連
508/////////////////////
509
510void CodeGenerator::op_movsx16_FromReg8(int reg32,int reg8){
511 //movsx reg16,reg8
512 char RexByte=-1;
513
514 if(REG_RAX<=reg32&&reg32<=REG_RDI){
515 if(REG_RAX<=reg8&&reg8<=REG_RBX) RexByte=0;
516 if(REG_RSP<=reg8&&reg8<=REG_RDI) RexByte=(char)0x40;
517 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x41;
518 }
519 if(REG_R8<=reg32&&reg32<=REG_R15){
520 if(REG_RAX<=reg8&&reg8<=REG_RDI) RexByte=(char)0x44;
521 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x45;
522 }
523
524 if(RexByte==-1) SetError(300,NULL,cp);
525
526 //0110 0110 [8bit rex] 0000 1111 1011 1110 11rr rbbb
527 pNativeCode->Put( (char)0x66 );
528 if(RexByte) pNativeCode->Put( RexByte );
529 pNativeCode->Put( (char)0x0F );
530 pNativeCode->Put( (char)0xBE );
531 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg32)<<3 | REGISTER_OPERAND(reg8)) );
532}
533
534
535
536//////////////////////////////////
[228]537// ビット拡張
538//////////////////////////////////
539
540void CodeGenerator::op_cqo()
541{
542 pNativeCode->Put( (char)0x48 );
543 pNativeCode->Put( (char)0x99 );
544}
545
546
547
548//////////////////////////////////
[226]549// インクリメント・デクリメント
550//////////////////////////////////
551
552void CodeGenerator::op_inc(int reg){
553 //inc reg
554
555 //16ビット演算の命令プリフィックス
556 char op_prefix=0;
557
558 //オペコード
559 char opcode=(char)0xFF;
560
561 __op_format(sizeof(_int64),op_prefix,opcode,0,0,reg,0,MOD_REG);
562}
563void CodeGenerator::op_dec(int reg){
564 //dec reg
565
566 //16ビット演算の命令プリフィックス
567 char op_prefix=0;
568
569 //オペコード
570 char opcode=(char)0xFF;
571
572 __op_format(sizeof(_int64),op_prefix,opcode,0,0x01,reg,0,MOD_REG);
573}
574
575
576
577/////////////////////
578// add関連
579/////////////////////
580
[228]581void CodeGenerator::op_add_RM(int op_size,int reg,int base_reg,int offset,char mod, Schedule::Type scheduleType ){
[226]582 //add reg64,qword ptr[base_reg+offset]
583 //add reg32,dword ptr[base_reg+offset]
584 //add reg16,word ptr[base_reg+offset]
585 //add reg8,byte ptr[base_reg+offset]
586
587 //16ビット演算の命令プリフィックス
588 char op_prefix=0;
589 if(op_size==sizeof(short)) op_prefix=(char)0x66;
590
591 //オペコード
592 char opcode;
593 if(op_size==sizeof(char)) opcode=(char)0x02;
594 else opcode=(char)0x03;
595
[228]596 __op_format(op_size,op_prefix,opcode,0,reg,base_reg,offset,mod, scheduleType );
[226]597}
598
[232]599void CodeGenerator::op_add_RV(int reg,long offset, Schedule::Type scheduleType ){
[226]600 //add reg,offset
601 char RexByte=-1;
602
603 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0x48;
604 if(REG_R8<=reg&&reg<=REG_R15) RexByte=0x49;
605
606 if(RexByte==-1) SetError(300,NULL,cp);
607
608 if(reg==REG_RAX){
609 //raxのみ特殊
610
611 // [8bit rex] 0000 0101 [32bit offset]
612 pNativeCode->Put( (char)RexByte );
613 pNativeCode->Put( (char)0x05 );
[232]614 pNativeCode->Put( offset, scheduleType );
[226]615 }
616 else{
617 //rax以外
618
619 //[8bit rex] 1000 0001 1100 0xxx [32bit offset]
620 pNativeCode->Put( (char)RexByte );
621 pNativeCode->Put( (char)0x81 );
622 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg)) );
[232]623 pNativeCode->Put( offset, scheduleType );
[226]624 }
625}
[228]626void CodeGenerator::op_add_RR(int reg1,int reg2){
[226]627 //add reg1,reg2
628 char RexByte=-1;
629
630 if(REG_RAX<=reg1&&reg1<=REG_RDI){
631 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
632 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
633 }
634 if(REG_R8<=reg1&&reg1<=REG_R15){
635 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
636 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
637 }
638
639 if(RexByte==-1) SetError(300,NULL,cp);
640
641 //[8bit rex] 0000 0011 11rr rbbb
642 pNativeCode->Put( (char)RexByte );
643 pNativeCode->Put( (char)0x03 );
644 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
645}
646void CodeGenerator::op_add32_reg(int reg1,int reg2){
647 //add reg1,reg2
648 char RexByte=-1;
649
650 if(REG_RAX<=reg1&&reg1<=REG_RDI){
651 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
652 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
653 }
654 if(REG_R8<=reg1&&reg1<=REG_R15){
655 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
656 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
657 }
658
659 if(RexByte==-1) SetError(300,NULL,cp);
660
661 //[8bit rex] 0000 0011 11rr rbbb
662 if(RexByte) pNativeCode->Put( (char)RexByte );
663 pNativeCode->Put( (char)0x03 );
664 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
665}
666
667
668
669////////////////////////
670// sub関連
671////////////////////////
672
673void CodeGenerator::op_sub_RV(int op_size,int reg,long i32data){
674 //sub reg,i32data
675
676 //rexプリフィックス
677 set_rex(op_size,REG_NON,REG_NON,reg);
678
679 if(reg==REG_RAX){
680 //raxのみ特殊
681 pNativeCode->Put( (char)0x2D );
682 }
683 else{
684 //オペコード
685 pNativeCode->Put( (char)0x81 );
686
687 //レジスタ
688 pNativeCode->Put( (char)(0xE8| REGISTER_OPERAND(reg) ) );
689 }
690
691 //即値
692 pNativeCode->Put( i32data );
693}
694void CodeGenerator::op_sub64_reg(int reg1,int reg2){
695 //sub reg1,reg2
696 char RexByte=-1;
697
698 if(REG_RAX<=reg1&&reg1<=REG_RDI){
699 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
700 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
701 }
702 if(REG_R8<=reg1&&reg1<=REG_R15){
703 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
704 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
705 }
706
707 if(RexByte==-1) SetError(300,NULL,cp);
708
709 //[8bit rex] 0010 1011 11rr rbbb
710 pNativeCode->Put( (char)RexByte );
711 pNativeCode->Put( (char)0x2B );
712 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
713}
714void CodeGenerator::op_sub32_reg(int reg1,int reg2){
715 //sub reg1,reg2
716 char RexByte=-1;
717
718 if(REG_RAX<=reg1&&reg1<=REG_RDI){
719 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
720 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
721 }
722 if(REG_R8<=reg1&&reg1<=REG_R15){
723 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
724 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
725 }
726
727 if(RexByte==-1) SetError(300,NULL,cp);
728
729 //[8bit rex] 0010 1011 11rr rbbb
730 if(RexByte) pNativeCode->Put( (char)RexByte );
731 pNativeCode->Put( (char)0x2B );
732 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
733}
734void CodeGenerator::op_sbb_RR( int op_size, int reg1, int reg2 ){
735 //sbb reg1,reg2
736
737 //rexプリフィックス
738 set_rex(0,reg1,0,reg2);
739
740 //オペコード
741 pNativeCode->Put( (char)0x1B );
742
743 //レジスタ
744 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
745}
746
747
748
749////////////////////////
750// imul関連
751////////////////////////
752
753void CodeGenerator::op_imul_RR(int op_size,int reg1,int reg2){
754 //imul reg1,reg2
755 char RexByte=-1;
756
757 if(op_size==sizeof(_int64)){
758 if(REG_RAX<=reg1&&reg1<=REG_RDI){
759 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
760 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
761 }
762 if(REG_R8<=reg1&&reg1<=REG_R15){
763 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
764 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
765 }
766 }
767 else{
768 if(REG_RAX<=reg1&&reg1<=REG_RDI){
769 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
770 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
771 }
772 if(REG_R8<=reg1&&reg1<=REG_R15){
773 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
774 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
775 }
776 }
777
778 if(RexByte==-1) SetError(300,NULL,cp);
779
780
781 //rexプリフィックス
782 if(RexByte) pNativeCode->Put( (char)RexByte );
783
784 //オペコード
785 pNativeCode->Put( (char)0x0F );
786 pNativeCode->Put( (char)0xAF );
787
788 //レジスタ
789 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
790}
791void CodeGenerator::op_imul_RV(int op_size,int reg,long i32data){
792 //imul reg,i32data
793 char RexByte=-1;
794
795 if(op_size==sizeof(_int64)){
796 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
797 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x4D;
798 }
799 else{
800 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
801 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x45;
802 }
803
804 if(RexByte==-1) SetError(300,NULL,cp);
805
806
807 //rexプリフィックス
808 if(RexByte) pNativeCode->Put( (char)RexByte );
809
810 if(-128<=i32data&&i32data<=127){
811 //オペコード
812 pNativeCode->Put( (char)0x6B );
813
814 //レジスタ
815 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg)) );
816
817 //値
818 pNativeCode->Put( (char)i32data );
819 }
820 else{
821 //オペコード
822 pNativeCode->Put( (char)0x69 );
823
824 //レジスタ
825 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg)) );
826
827 //値
828 pNativeCode->Put( i32data );
829 }
830}
831
832
833
834////////////////////////
835// div、idiv関連
836////////////////////////
837
838void CodeGenerator::op_div64_reg(int reg){
839 //div reg
840 char RexByte=-1;
841
842 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
843 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
844
845 if(RexByte==-1) SetError(300,NULL,cp);
846
847 //rexプリフィックス
848 pNativeCode->Put( (char)RexByte );
849
850 //オペコード
851 pNativeCode->Put( (char)0xF7 );
852
853 //レジスタ
854 pNativeCode->Put( (char)(0xF0| REGISTER_OPERAND(reg)) );
855}
856void CodeGenerator::op_idiv64_reg(int reg){
857 //idiv reg
858 char RexByte=-1;
859
860 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
861 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
862
863 if(RexByte==-1) SetError(300,NULL,cp);
864
865 //rexプリフィックス
866 pNativeCode->Put( (char)RexByte );
867
868 //オペコード
869 pNativeCode->Put( (char)0xF7 );
870
871 //レジスタ
872 pNativeCode->Put( (char)(0xF8| REGISTER_OPERAND(reg)) );
873}
874
875
876
877////////////////////
878// ビットシフト関連
879////////////////////
880
881void CodeGenerator::op_shl_reg(int op_size,int reg){
882 //shl reg,cl
883 char RexByte=-1;
884
885 if(op_size==sizeof(_int64)){
886 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
887 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
888 }
889 else if(op_size==sizeof(char)){
890 if(REG_RAX<=reg&&reg<=REG_RBX) RexByte=0;
891 if(REG_RSP<=reg&&reg<=REG_RDI) RexByte=(char)0x40;
892 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
893 }
894 else{
895 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
896 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
897 }
898
899 if(RexByte==-1) SetError(300,NULL,cp);
900
901
902 //16ビット演算のプリフィックス
903 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
904
905 //rexプリフィックス
906 if(RexByte) pNativeCode->Put( (char)RexByte );
907
908 //オペコード
909 if(op_size==sizeof(char)) pNativeCode->Put( (char)0xD2 );
910 else pNativeCode->Put( (char)0xD3 );
911
912 //レジスタ
913 pNativeCode->Put( (char)(0xE0| REGISTER_OPERAND(reg)) );
914}
915void CodeGenerator::op_sar_reg(int op_size,int reg){
916 //sar reg,cl
917 char RexByte=-1;
918
919 if(op_size==sizeof(_int64)){
920 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
921 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
922 }
923 else if(op_size==sizeof(char)){
924 if(REG_RAX<=reg&&reg<=REG_RBX) RexByte=0;
925 if(REG_RSP<=reg&&reg<=REG_RDI) RexByte=(char)0x40;
926 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
927 }
928 else{
929 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
930 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
931 }
932
933 if(RexByte==-1) SetError(300,NULL,cp);
934
935
936 //16ビット演算のプリフィックス
937 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
938
939 //rexプリフィックス
940 if(RexByte) pNativeCode->Put( (char)RexByte );
941
942 //オペコード
943 if(op_size==sizeof(char)) pNativeCode->Put( (char)0xD2 );
944 else pNativeCode->Put( (char)0xD3 );
945
946 //レジスタ
947 pNativeCode->Put( (char)(0xF8| REGISTER_OPERAND(reg)) );
948}
949void CodeGenerator::op_shr_reg(int op_size,int reg){
950 //shr reg,cl
951 char RexByte=-1;
952
953 if(op_size==sizeof(_int64)){
954 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
955 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
956 }
957 else if(op_size==sizeof(char)){
958 if(REG_RAX<=reg&&reg<=REG_RBX) RexByte=0;
959 if(REG_RSP<=reg&&reg<=REG_RDI) RexByte=(char)0x40;
960 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
961 }
962 else{
963 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
964 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
965 }
966
967 if(RexByte==-1) SetError(300,NULL,cp);
968
969
970 //16ビット演算のプリフィックス
971 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
972
973 //rexプリフィックス
974 if(RexByte) pNativeCode->Put( (char)RexByte );
975
976 //オペコード
977 if(op_size==sizeof(char)) pNativeCode->Put( (char)0xD2 );
978 else pNativeCode->Put( (char)0xD3 );
979
980 //レジスタ
981 pNativeCode->Put( (char)(0xE8| REGISTER_OPERAND(reg)) );
982}
983
984
985
986////////////////////
987// and 関連
988////////////////////
989
990void CodeGenerator::op_and_reg(int op_size,int reg1,int reg2){
991 //and reg1,reg2
992 char RexByte=-1;
993
994 if(op_size==sizeof(_int64)){
995 if(REG_RAX<=reg1&&reg1<=REG_RDI){
996 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
997 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
998 }
999 if(REG_R8<=reg1&&reg1<=REG_R15){
1000 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1001 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1002 }
1003 }
1004 else{
1005 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1006 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1007 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1008 }
1009 if(REG_R8<=reg1&&reg1<=REG_R15){
1010 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1011 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1012 }
1013 }
1014
1015 if(RexByte==-1) SetError(300,NULL,cp);
1016
1017
1018 //rexプリフィックス
1019 if(RexByte) pNativeCode->Put( (char)RexByte );
1020
1021 //オペコード
1022 pNativeCode->Put( (char)0x23 );
1023
1024 //レジスタ
1025 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
1026}
1027void CodeGenerator::op_and64_value(int reg,long offset){
1028 //and reg,offset
1029 char RexByte=-1;
1030
1031 if(REG_RAX<=reg&&reg<=REG_RDI) (char)RexByte=0x48;
1032 if(REG_R8<=reg&&reg<=REG_R15) (char)RexByte=0x49;
1033
1034 if(RexByte==-1) SetError(300,NULL,cp);
1035
1036 if(reg==REG_RAX){
1037 //raxのみ特殊
1038
1039 // [8bit rex] 0010 0101 [32bit offset]
1040 pNativeCode->Put( (char)RexByte );
1041 pNativeCode->Put( (char)0x25 );
1042 pNativeCode->Put( offset );
1043 }
1044 else{
1045 //rax以外
1046
1047 //[8bit rex] 1000 0001 1100 0xxx [32bit offset]
1048 pNativeCode->Put( (char)RexByte );
1049 pNativeCode->Put( (char)0x81 );
1050 pNativeCode->Put( (char)(0xE0| REGISTER_OPERAND(reg)) );
1051 pNativeCode->Put( offset );
1052 }
1053}
1054void CodeGenerator::op_and32_value(int reg,long offset){
1055 //and reg,offset
1056 char RexByte=-1;
1057
1058 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1059 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1060
1061 if(RexByte==-1) SetError(300,NULL,cp);
1062
1063 if(reg==REG_RAX){
1064 //eaxのみ特殊
1065
1066 // [8bit rex] 0010 0101 [32bit offset]
1067 pNativeCode->Put( (char)0x25 );
1068 pNativeCode->Put( offset );
1069 }
1070 else{
1071 //eax以外
1072
1073 //[8bit rex] 1000 0001 1100 0xxx [32bit offset]
1074 if(RexByte) pNativeCode->Put( (char)RexByte );
1075 pNativeCode->Put( (char)0x81 );
1076 pNativeCode->Put( (char)(0xE0| REGISTER_OPERAND(reg)) );
1077 pNativeCode->Put( offset );
1078 }
1079}
1080
1081
1082
1083////////////////////////
1084// or 関連
1085////////////////////////
1086
1087void CodeGenerator::op_or_reg(int op_size,int reg1,int reg2){
1088 //or reg1,reg2
1089 char RexByte=-1;
1090
1091 if(op_size==sizeof(_int64)){
1092 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1093 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1094 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1095 }
1096 if(REG_R8<=reg1&&reg1<=REG_R15){
1097 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1098 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1099 }
1100 }
1101 else{
1102 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1103 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1104 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1105 }
1106 if(REG_R8<=reg1&&reg1<=REG_R15){
1107 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1108 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1109 }
1110 }
1111
1112 if(RexByte==-1) SetError(300,NULL,cp);
1113
1114
1115 //rexプリフィックス
1116 if(RexByte) pNativeCode->Put( (char)RexByte );
1117
1118 //オペコード
1119 pNativeCode->Put( (char)0x0B );
1120
1121 //レジスタ
1122 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
1123}
1124
1125
1126
1127////////////////////////
1128// xor 関連
1129////////////////////////
1130
1131void CodeGenerator::op_xor_reg(int op_size,int reg1,int reg2){
1132 //xor reg1,reg2
1133 char RexByte=-1;
1134
1135 if(op_size==sizeof(_int64)){
1136 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1137 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1138 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1139 }
1140 if(REG_R8<=reg1&&reg1<=REG_R15){
1141 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1142 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1143 }
1144 }
1145 else{
1146 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1147 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1148 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1149 }
1150 if(REG_R8<=reg1&&reg1<=REG_R15){
1151 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1152 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1153 }
1154 }
1155
1156 if(RexByte==-1) SetError(300,NULL,cp);
1157
1158
1159 //rexプリフィックス
1160 if(RexByte) pNativeCode->Put( (char)RexByte );
1161
1162 //オペコード
1163 pNativeCode->Put( (char)0x33 );
1164
1165 //レジスタ
1166 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
1167}
1168
1169
1170
1171///////////////////////
1172// not 関連
1173///////////////////////
1174
1175void CodeGenerator::op_not_reg(int op_size,int reg){
1176 //not reg
1177 char RexByte=-1;
1178
1179 if(op_size==sizeof(_int64)){
1180 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1181 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1182 }
1183 else{
1184 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1185 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1186 }
1187
1188 if(RexByte==-1) SetError(300,NULL,cp);
1189
1190
1191 //rexプリフィックス
1192 if(RexByte) pNativeCode->Put( (char)RexByte );
1193
1194 //オペコード
1195 pNativeCode->Put( (char)0xF7 );
1196
1197 //レジスタ
1198 pNativeCode->Put( (char)(0xD0| REGISTER_OPERAND(reg)) );
1199}
1200void CodeGenerator::op_neg( int reg ){
1201 //neg reg
1202
1203 //オペコード
1204 pNativeCode->Put( (char)0xF7 );
1205
1206 //レジスタ
1207 pNativeCode->Put( (char)(0xD8| REGISTER_OPERAND(reg)) );
1208}
1209
1210
1211
1212////////////////////
1213// test関連
1214////////////////////
1215
1216void CodeGenerator::op_test(int reg1,int reg2){
1217 //test reg1,reg2
1218 char RexByte=-1;
1219
1220 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1221 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1222 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1223 }
1224 if(REG_R8<=reg1&&reg1<=REG_R15){
1225 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1226 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1227 }
1228
1229 if(RexByte==-1) SetError(300,NULL,cp);
1230
1231 //[8bit rex] 1000 0101 11rr rbbb
1232 pNativeCode->Put( (char)RexByte );
1233 pNativeCode->Put( (char)0x85 );
1234 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
1235}
1236
1237
1238
1239/////////////////////
1240// cmp 関連
1241/////////////////////
1242
1243void CodeGenerator::op_cmp_reg(int op_size,int reg1,int reg2){
1244 //cmp reg1,reg2
1245 char RexByte=-1;
1246
1247 if(op_size==sizeof(_int64)){
1248 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1249 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1250 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1251 }
1252 if(REG_R8<=reg1&&reg1<=REG_R15){
1253 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1254 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1255 }
1256 }
1257 else{
1258 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1259 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1260 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1261 }
1262 if(REG_R8<=reg1&&reg1<=REG_R15){
1263 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1264 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1265 }
1266 }
1267
1268 if(RexByte==-1) SetError(300,NULL,cp);
1269
1270
1271 //rexプリフィックス
1272 if(RexByte) pNativeCode->Put( (char)RexByte );
1273
1274 //オペコード
1275 pNativeCode->Put( (char)0x3B );
1276
1277 //レジスタ
1278 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2)) );
1279}
1280void CodeGenerator::op_cmp_value(int op_size,int reg,char byte_data){
1281 //cmp reg,byte_data
1282
1283 if(op_size==sizeof(char)&&reg==REG_RAX){
1284 //alレジスタの場合は特殊
1285 pNativeCode->Put( (char)0x3C );
1286
1287 //8ビット値
1288 pNativeCode->Put( byte_data );
1289
1290 return;
1291 }
1292
1293 //16ビット演算のプリフィックス
1294 if(op_size==sizeof(short)) pNativeCode->Put( (char)0x66 );
1295
1296 //rexプリフィックス
1297 set_rex(op_size,REG_NON,REG_NON,reg);
1298
1299 //オペコード
1300 if(op_size==sizeof(char)) pNativeCode->Put( (char)0x80 );
1301 else pNativeCode->Put( (char)0x83 );
1302
1303 //レジスタ
1304 pNativeCode->Put( (char)(0xF8| REGISTER_OPERAND(reg)) );
1305
1306 //8ビット値
1307 pNativeCode->Put( byte_data );
1308}
1309void CodeGenerator::op_setne( int reg ){
1310 //オペコード
1311 pNativeCode->Put( (char)0x0F );
1312 pNativeCode->Put( (char)0x95 );
1313
1314 //レジスタ
1315 pNativeCode->Put( (char)( 0xC0 | REGISTER_OPERAND(reg) ) );
1316}
1317
1318
1319////////////////////
1320// SSE2関連
1321////////////////////
1322
[232]1323void CodeGenerator::op_movlpd_MR(int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType ){
[226]1324 //movlpd qword ptr[base_reg+offset],xmm_reg
[232]1325 __op_format(0,(char)0x66,(char)0x0F,(char)0x13,xmm_reg,base_reg,offset,mod, scheduleType );
[226]1326}
[232]1327void CodeGenerator::op_movlpd_RM(int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType ){
[226]1328 //movlpd xmm_reg,qword ptr[base_reg+offset]
[232]1329 __op_format(0,(char)0x66,(char)0x0F,(char)0x12,xmm_reg,base_reg,offset,mod, scheduleType );
[226]1330}
1331void CodeGenerator::op_movsd_RR(int xmm_reg1,int xmm_reg2){
1332 if(xmm_reg1==xmm_reg2) return;
1333
1334 //movsd xmm_reg1,xmm_reg2
1335 __op_format(0,(char)0xF2,(char)0x0F,(char)0x10,xmm_reg1,xmm_reg2,0,MOD_REG);
1336}
[232]1337void CodeGenerator::op_movsd_MR(int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType ){
[226]1338 //movsd qword ptr[reg+offset],xmm_reg
1339 //movsd qword ptr[reg],xmm_reg
[232]1340 __op_format(0,(char)0xF2,(char)0x0F,(char)0x11,xmm_reg,base_reg,offset,mod, scheduleType );
[226]1341}
1342void CodeGenerator::op_movss_RR(int xmm_reg1,int xmm_reg2){
1343 if(xmm_reg1==xmm_reg2) return;
1344
1345 //movss xmm_reg1,xmm_reg2
1346 __op_format(0,(char)0xF3,(char)0x0F,(char)0x10,xmm_reg1,xmm_reg2,0,MOD_REG);
1347}
[232]1348void CodeGenerator::op_movss_RM(int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType ){
[226]1349 //movss xmm_reg,dword ptr[base_reg+offset]
[232]1350 __op_format(0,(char)0xF3,(char)0x0F,(char)0x10,xmm_reg,base_reg,offset,mod, scheduleType );
[226]1351}
[232]1352void CodeGenerator::op_movss_MR(int xmm_reg,int base_reg,int offset,char mod, Schedule::Type scheduleType ){
[226]1353 //movss dword ptr[reg+offset],xmm_reg
1354 //movss dword ptr[reg],xmm_reg
[232]1355 __op_format(0,(char)0xF3,(char)0x0F,(char)0x11,xmm_reg,base_reg,offset,mod, scheduleType );
[226]1356}
1357
1358void CodeGenerator::op_movd_RX(int reg,int xmm_reg){
1359 __op_format(sizeof(_int64),(char)0x66,(char)0x0F,(char)0x7E,xmm_reg,reg,0,MOD_REG);
1360}
1361
1362void CodeGenerator::op_cvtsd2ss(int xmm_reg1,int xmm_reg2){
1363 //cvtsd2ss xmm_reg1,xmm_reg2
1364
1365 //オペコード
1366 pNativeCode->Put( (char)0xF2 );
1367
1368 //rexプリフィックス
1369 set_rex(sizeof(long),xmm_reg1,0,xmm_reg2);
1370
1371 //オペコード
1372 pNativeCode->Put( (char)0x0F );
1373 pNativeCode->Put( (char)0x5A );
1374
1375 //レジスタ
1376 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2)) );
1377}
1378void CodeGenerator::op_cvtss2sd(int xmm_reg1,int xmm_reg2){
1379 //cvtss2sd xmm_reg1,xmm_reg2
1380
1381 //オペコード
1382 pNativeCode->Put( (char)0xF3 );
1383
1384 //rexプリフィックス
1385 set_rex(0,xmm_reg1,0,xmm_reg2);
1386
1387 //オペコード
1388 pNativeCode->Put( (char)0x0F );
1389 pNativeCode->Put( (char)0x5A );
1390
1391 //レジスタ
1392 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2)) );
1393}
1394void CodeGenerator::op_cvttsd2si_xmm(int op_size,int reg,int xmm_reg){
1395 //cvttsd2si reg,xmm_reg
1396
1397 //オペコード
1398 pNativeCode->Put( (char)0xF2 );
1399
1400 //rexプリフィックス
1401 set_rex(op_size,reg,0,xmm_reg);
1402
1403 //オペコード
1404 pNativeCode->Put( (char)0x0F );
1405 pNativeCode->Put( (char)0x2C );
1406
1407 //レジスタ
1408 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(xmm_reg)) );
1409}
1410void CodeGenerator::op_cvttss2si_xmm(int op_size,int reg,int xmm_reg){
1411 //cvttss2si reg,xmm_reg
1412
1413 //オペコード
1414 pNativeCode->Put( (char)0xF3 );
1415
1416 //rexプリフィックス
1417 set_rex(op_size,reg,0,xmm_reg);
1418
1419 //オペコード
1420 pNativeCode->Put( (char)0x0F );
1421 pNativeCode->Put( (char)0x2C );
1422
1423 //レジスタ
1424 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(xmm_reg)) );
1425}
1426void CodeGenerator::op_cvtsi2sd_reg(int op_size,int xmm_reg,int reg){
1427 //cvtsi2sd xmm_reg,reg
1428 char RexByte=-1;
1429
1430 if(op_size==sizeof(_int64)){
1431 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1432 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1433 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1434 }
1435 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1436 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x4C;
1437 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x4D;
1438 }
1439 }
1440 else{
1441 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1442 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1443 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1444 }
1445 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1446 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x44;
1447 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x45;
1448 }
1449 }
1450
1451 if(RexByte==-1) SetError(300,NULL,cp);
1452
1453
1454 //オペコード
1455 pNativeCode->Put( (char)0xF2 );
1456
1457 //rexプリフィックス
1458 if(RexByte) pNativeCode->Put( (char)RexByte );
1459
1460 //オペコード
1461 pNativeCode->Put( (char)0x0F );
1462 pNativeCode->Put( (char)0x2A );
1463
1464 //レジスタ
1465 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(xmm_reg)<<3 | REGISTER_OPERAND(reg)) );
1466}
1467void CodeGenerator::op_cvtsi2ss_reg(int op_size,int xmm_reg,int reg){
1468 //cvtsi2ss xmm_reg,reg
1469 char RexByte=-1;
1470
1471 if(op_size==sizeof(_int64)){
1472 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1473 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1474 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1475 }
1476 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1477 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x4C;
1478 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x4D;
1479 }
1480 }
1481 else{
1482 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1483 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1484 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1485 }
1486 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1487 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x44;
1488 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x45;
1489 }
1490 }
1491
1492 if(RexByte==-1) SetError(300,NULL,cp);
1493
1494
1495 //オペコード
1496 pNativeCode->Put( (char)0xF3 );
1497
1498 //rexプリフィックス
1499 if(RexByte) pNativeCode->Put( (char)RexByte );
1500
1501 //オペコード
1502 pNativeCode->Put( (char)0x0F );
1503 pNativeCode->Put( (char)0x2A );
1504
1505 //レジスタ
1506 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(xmm_reg)<<3 | REGISTER_OPERAND(reg)) );
1507}
1508void CodeGenerator::op_comisd(int xmm_reg1,int xmm_reg2){
1509 //comisd xmm_reg1,xmm_reg2
1510
1511 //オペコード
1512 pNativeCode->Put( (char)0x66 );
1513 pNativeCode->Put( (char)0x0F );
1514 pNativeCode->Put( (char)0x2F );
1515
1516 //レジスタ
1517 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2)) );
1518}
1519void CodeGenerator::op_comiss(int xmm_reg1,int xmm_reg2){
1520 //comiss xmm_reg1,xmm_reg2
1521
1522 //オペコード
1523 pNativeCode->Put( (char)0x0F );
1524 pNativeCode->Put( (char)0x2F );
1525
1526 //レジスタ
1527 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2)) );
1528}
1529
1530
1531
1532/////////////////////
1533// ストリング関係
1534/////////////////////
1535
1536void CodeGenerator::op_rep_movs(int op_size){
1537 if(op_size==sizeof(BYTE)){
1538 //rep movs byte ptr[edi],byte ptr[esi]
1539 pNativeCode->Put( (char)0xF3 );
1540 pNativeCode->Put( (char)0xA4 );
1541 }
1542 else if(op_size==sizeof(short)){
1543 //rep movs word ptr[edi],word ptr[esi]
1544 pNativeCode->Put( (char)0xF3 );
1545 pNativeCode->Put( (char)0x66 );
1546 pNativeCode->Put( (char)0xA5 );
1547 }
1548 else if(op_size==sizeof(long)){
1549 //rep movs dword ptr[edi],dword ptr[esi]
1550 pNativeCode->Put( (char)0xF3 );
1551 pNativeCode->Put( (char)0xA5 );
1552 }
1553}
1554
1555
1556
1557
1558void CodeGenerator::op_add_rsp(long num){
1559 //スタックポインタの加算(pop方向)
1560
1561 //add rsp,num
1562 if(0xFFFFFF80&num){
1563 pNativeCode->Put( (char)0x48 );
1564 pNativeCode->Put( (char)0x81 );
1565 pNativeCode->Put( (char)0xC4 );
1566 pNativeCode->Put( num );
1567 }
1568 else{
1569 //「-128 < num < 127」の場合
1570 pNativeCode->Put( (char)0x48 );
1571 pNativeCode->Put( (char)0x83 );
1572 pNativeCode->Put( (char)0xC4 );
1573 pNativeCode->Put( (char)num );
1574 }
1575}
1576void CodeGenerator::op_sub_rsp(long num){
1577 //スタックポインタの減算(push方向)
1578
1579 //sub rsp,num
1580 if(0xFFFFFF80&num){
1581 pNativeCode->Put( (char)0x48 );
1582 pNativeCode->Put( (char)0x81 );
1583 pNativeCode->Put( (char)0xEC );
1584 pNativeCode->Put( num );
1585 }
1586 else{
1587 //「-128 < num < 127」の場合
1588 pNativeCode->Put( (char)0x48 );
1589 pNativeCode->Put( (char)0x83 );
1590 pNativeCode->Put( (char)0xEC );
1591 pNativeCode->Put( (char)num );
1592 }
1593}
1594
1595
1596void CodeGenerator::op_add_esp(long num){
1597 //スタックポインタの加算(pop方向)
1598
1599 //add esp,num
1600 if(0xFFFFFF80&num){
1601 pNativeCode->Put( (char)0x81 );
1602 pNativeCode->Put( (char)0xC4 );
1603 pNativeCode->Put( num );
1604 }
1605 else{
1606 //「-128 < num < 127」の場合
1607 pNativeCode->Put( (char)0x83 );
1608 pNativeCode->Put( (char)0xC4 );
1609 pNativeCode->Put( (char)num );
1610 }
1611}
1612void CodeGenerator::op_sub_esp(long num){
1613 //スタックポインタの減算(push方向)
1614
1615 //sub esp,num
1616 if(0xFFFFFF80&num){
1617 pNativeCode->Put( (char)0x81 );
1618 pNativeCode->Put( (char)0xEC );
1619 pNativeCode->Put( num );
1620 }
1621 else{
1622 //「-128 < num < 127」の場合
1623 pNativeCode->Put( (char)0x83 );
1624 pNativeCode->Put( (char)0xEC );
1625 pNativeCode->Put( (char)num );
1626 }
1627}
1628
1629
1630
1631//////////////////////////////
1632// 浮動小数点関連
1633//////////////////////////////
1634
1635void CodeGenerator::op_fld_ptr_esp(int type){
1636 //スタックポインタが示すバッファのデータを浮動小数点レジスタへロード
1637
1638 if(type==DEF_DOUBLE){
1639 //fld qword ptr[esp]
1640 pNativeCode->Put( (char)0xDD );
1641 pNativeCode->Put( (char)0x04 );
1642 pNativeCode->Put( (char)0x24 );
1643 }
1644 else if(type==DEF_SINGLE){
1645 //fld dword ptr[esp]
1646 pNativeCode->Put( (char)0xD9 );
1647 pNativeCode->Put( (char)0x04 );
1648 pNativeCode->Put( (char)0x24 );
1649 }
1650 else if(type==DEF_INT64){
1651 //fild qword ptr[esp]
1652 pNativeCode->Put( (char)0xDF );
1653 pNativeCode->Put( (char)0x2C );
1654 pNativeCode->Put( (char)0x24 );
1655 }
1656 else if(type==DEF_LONG){
1657 //fild dword ptr[esp]
1658 pNativeCode->Put( (char)0xDB );
1659 pNativeCode->Put( (char)0x04 );
1660 pNativeCode->Put( (char)0x24 );
1661 }
1662}
1663
1664
1665
1666//////////////////////////////
1667// レジスタ関連
1668//////////////////////////////
1669
1670void CodeGenerator::op_zero_reg(int reg){
1671 //レジスタに0をセット
1672
1673 if(REG_RAX<=reg&&reg<=REG_RDI){
1674 /* rax~rdi
1675 0100 1000 0011 0011 11 xxx xxx */
1676 pNativeCode->Put( (char)0x48 );
1677 pNativeCode->Put( (char)0x33 );
1678 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg)) );
1679 }
1680 if(REG_R8<=reg&&reg<=REG_R15){
1681 /* r8~r15
1682 0100 1101 0011 0011 11 xxx xxx */
1683 pNativeCode->Put( (char)0x4D );
1684 pNativeCode->Put( (char)0x33 );
1685 pNativeCode->Put( (char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg)) );
1686 }
1687}
1688
1689
1690
1691/////////////////////////////
[228]1692// 分岐
1693/////////////////////////////
1694
1695void CodeGenerator::op_jle( long offset, int op_size )
1696{
1697 __jmp_op_format( (char)0x7E, offset, op_size );
1698}
1699void CodeGenerator::op_jbe( long offset, int op_size )
1700{
1701 __jmp_op_format( (char)0x76, offset, op_size );
1702}
1703void CodeGenerator::op_jge( long offset, int op_size )
1704{
1705 __jmp_op_format( (char)0x7D, offset, op_size );
1706}
1707void CodeGenerator::op_jae( long offset, int op_size )
1708{
1709 __jmp_op_format( (char)0x73, offset, op_size );
1710}
1711void CodeGenerator::op_jl( long offset, int op_size )
1712{
1713 __jmp_op_format( (char)0x7C, offset, op_size );
1714}
1715void CodeGenerator::op_jb( long offset, int op_size )
1716{
1717 __jmp_op_format( (char)0x72, offset, op_size );
1718}
1719void CodeGenerator::op_jg( long offset, int op_size )
1720{
1721 __jmp_op_format( (char)0x7F, offset, op_size );
1722}
1723void CodeGenerator::op_ja( long offset, int op_size )
1724{
1725 __jmp_op_format( (char)0x77, offset, op_size );
1726}
1727void CodeGenerator::op_jne( long offset, int op_size )
1728{
1729 __jmp_op_format( (char)0x75, offset, op_size );
1730}
1731void CodeGenerator::op_je( long offset, int op_size )
1732{
1733 __jmp_op_format( (char)0x74, offset, op_size );
1734}
1735void CodeGenerator::op_jmp( long offset, int op_size )
1736{
1737 __jmp_op_format( (char)0xEB, offset, op_size );
1738}
1739
1740
1741
1742/////////////////////////////
[226]1743// 関数呼び出し
1744/////////////////////////////
1745
1746void CodeGenerator::op_call( const UserProc *pUserProc ){
1747 pUserProc->Using();
1748
1749 pNativeCode->Put( (char)0xE8 );
1750 pobj_SubAddrSchedule->add( pUserProc, 1 );
1751 pNativeCode->Put( (long)0 );
1752}
1753void CodeGenerator::op_call( const DllProc *pDllProc ){
1754 pDllProc->Using();
1755
1756 pNativeCode->Put( (char)0xFF );
1757 pNativeCode->Put( (char)0x15 );
1758 pobj_ImportAddrSchedule->add(pDllProc);
1759 pNativeCode->Put( (long)0 );
1760}
1761void CodeGenerator::op_ret(){
1762 pNativeCode->Put( (char)0xC3 );
1763}
Note: See TracBrowser for help on using the repository browser.