source: dev/BasicCompiler64/amd64_main.cpp@ 13

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