source: dev/BasicCompiler64/amd64_main.cpp@ 60

Last change on this file since 60 was 36, checked in by dai_9181, 18 years ago

Boolean型に対応。

File size: 43.5 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}
826void op_sbb_RR( int op_size, int reg1, int reg2 ){
827 //sbb reg1,reg2
828
829 //rexプリフィックス
830 set_rex(0,reg1,0,reg2);
831
832 //オペコード
833 OpBuffer[obp++]=(char)0x1B;
834
835 //レジスタ
836 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
837}
838
839
840
841////////////////////////
842// imul関連
843////////////////////////
844
845void op_imul_reg(int op_size,int reg1,int reg2){
846 //imul reg1,reg2
847 char RexByte=-1;
848
849 if(op_size==sizeof(_int64)){
850 if(REG_RAX<=reg1&&reg1<=REG_RDI){
851 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
852 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
853 }
854 if(REG_R8<=reg1&&reg1<=REG_R15){
855 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
856 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
857 }
858 }
859 else{
860 if(REG_RAX<=reg1&&reg1<=REG_RDI){
861 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
862 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
863 }
864 if(REG_R8<=reg1&&reg1<=REG_R15){
865 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
866 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
867 }
868 }
869
870 if(RexByte==-1) SetError(300,NULL,cp);
871
872
873 //rexプリフィックス
874 if(RexByte) OpBuffer[obp++]=(char)RexByte;
875
876 //オペコード
877 OpBuffer[obp++]=(char)0x0F;
878 OpBuffer[obp++]=(char)0xAF;
879
880 //レジスタ
881 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
882}
883void op_imul_value(int op_size,int reg,int i32data){
884 //imul reg,i32data
885 char RexByte=-1;
886
887 if(op_size==sizeof(_int64)){
888 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
889 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x4D;
890 }
891 else{
892 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
893 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x45;
894 }
895
896 if(RexByte==-1) SetError(300,NULL,cp);
897
898
899 //rexプリフィックス
900 if(RexByte) OpBuffer[obp++]=(char)RexByte;
901
902 if(-128<=i32data&&i32data<=127){
903 //オペコード
904 OpBuffer[obp++]=(char)0x6B;
905
906 //レジスタ
907 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg));
908
909 //値
910 OpBuffer[obp++]=(char)i32data;
911 }
912 else{
913 //オペコード
914 OpBuffer[obp++]=(char)0x69;
915
916 //レジスタ
917 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg));
918
919 //値
920 *((long *)(OpBuffer+obp))=i32data;
921 obp+=sizeof(long);
922 }
923}
924
925
926
927////////////////////////
928// div、idiv関連
929////////////////////////
930
931void op_div64_reg(int reg){
932 //div reg
933 char RexByte=-1;
934
935 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
936 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
937
938 if(RexByte==-1) SetError(300,NULL,cp);
939
940 //rexプリフィックス
941 OpBuffer[obp++]=(char)RexByte;
942
943 //オペコード
944 OpBuffer[obp++]=(char)0xF7;
945
946 //レジスタ
947 OpBuffer[obp++]=(char)(0xF0| REGISTER_OPERAND(reg));
948}
949void op_idiv64_reg(int reg){
950 //idiv reg
951 char RexByte=-1;
952
953 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
954 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
955
956 if(RexByte==-1) SetError(300,NULL,cp);
957
958 //rexプリフィックス
959 OpBuffer[obp++]=(char)RexByte;
960
961 //オペコード
962 OpBuffer[obp++]=(char)0xF7;
963
964 //レジスタ
965 OpBuffer[obp++]=(char)(0xF8| REGISTER_OPERAND(reg));
966}
967
968
969
970////////////////////
971// ビットシフト関連
972////////////////////
973
974void op_shl_reg(int op_size,int reg){
975 //shl reg,cl
976 char RexByte=-1;
977
978 if(op_size==sizeof(_int64)){
979 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
980 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
981 }
982 else if(op_size==sizeof(char)){
983 if(REG_RAX<=reg&&reg<=REG_RBX) RexByte=0;
984 if(REG_RSP<=reg&&reg<=REG_RDI) RexByte=(char)0x40;
985 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
986 }
987 else{
988 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
989 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
990 }
991
992 if(RexByte==-1) SetError(300,NULL,cp);
993
994
995 //16ビット演算のプリフィックス
996 if(op_size==sizeof(short)) OpBuffer[obp++]=(char)0x66;
997
998 //rexプリフィックス
999 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1000
1001 //オペコード
1002 if(op_size==sizeof(char)) OpBuffer[obp++]=(char)0xD2;
1003 else OpBuffer[obp++]=(char)0xD3;
1004
1005 //レジスタ
1006 OpBuffer[obp++]=(char)(0xE0| REGISTER_OPERAND(reg));
1007}
1008void op_sar_reg(int op_size,int reg){
1009 //sar reg,cl
1010 char RexByte=-1;
1011
1012 if(op_size==sizeof(_int64)){
1013 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1014 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1015 }
1016 else if(op_size==sizeof(char)){
1017 if(REG_RAX<=reg&&reg<=REG_RBX) RexByte=0;
1018 if(REG_RSP<=reg&&reg<=REG_RDI) RexByte=(char)0x40;
1019 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1020 }
1021 else{
1022 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1023 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1024 }
1025
1026 if(RexByte==-1) SetError(300,NULL,cp);
1027
1028
1029 //16ビット演算のプリフィックス
1030 if(op_size==sizeof(short)) OpBuffer[obp++]=(char)0x66;
1031
1032 //rexプリフィックス
1033 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1034
1035 //オペコード
1036 if(op_size==sizeof(char)) OpBuffer[obp++]=(char)0xD2;
1037 else OpBuffer[obp++]=(char)0xD3;
1038
1039 //レジスタ
1040 OpBuffer[obp++]=(char)(0xF8| REGISTER_OPERAND(reg));
1041}
1042void op_shr_reg(int op_size,int reg){
1043 //shr reg,cl
1044 char RexByte=-1;
1045
1046 if(op_size==sizeof(_int64)){
1047 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1048 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1049 }
1050 else if(op_size==sizeof(char)){
1051 if(REG_RAX<=reg&&reg<=REG_RBX) RexByte=0;
1052 if(REG_RSP<=reg&&reg<=REG_RDI) RexByte=(char)0x40;
1053 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1054 }
1055 else{
1056 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1057 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1058 }
1059
1060 if(RexByte==-1) SetError(300,NULL,cp);
1061
1062
1063 //16ビット演算のプリフィックス
1064 if(op_size==sizeof(short)) OpBuffer[obp++]=(char)0x66;
1065
1066 //rexプリフィックス
1067 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1068
1069 //オペコード
1070 if(op_size==sizeof(char)) OpBuffer[obp++]=(char)0xD2;
1071 else OpBuffer[obp++]=(char)0xD3;
1072
1073 //レジスタ
1074 OpBuffer[obp++]=(char)(0xE8| REGISTER_OPERAND(reg));
1075}
1076
1077
1078
1079////////////////////
1080// and 関連
1081////////////////////
1082
1083void op_and_reg(int op_size,int reg1,int reg2){
1084 //and reg1,reg2
1085 char RexByte=-1;
1086
1087 if(op_size==sizeof(_int64)){
1088 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1089 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1090 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1091 }
1092 if(REG_R8<=reg1&&reg1<=REG_R15){
1093 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1094 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1095 }
1096 }
1097 else{
1098 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1099 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1100 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1101 }
1102 if(REG_R8<=reg1&&reg1<=REG_R15){
1103 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1104 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1105 }
1106 }
1107
1108 if(RexByte==-1) SetError(300,NULL,cp);
1109
1110
1111 //rexプリフィックス
1112 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1113
1114 //オペコード
1115 OpBuffer[obp++]=(char)0x23;
1116
1117 //レジスタ
1118 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
1119}
1120void op_and64_value(int reg,int offset){
1121 //and reg,offset
1122 char RexByte=-1;
1123
1124 if(REG_RAX<=reg&&reg<=REG_RDI) (char)RexByte=0x48;
1125 if(REG_R8<=reg&&reg<=REG_R15) (char)RexByte=0x49;
1126
1127 if(RexByte==-1) SetError(300,NULL,cp);
1128
1129 if(reg==REG_RAX){
1130 //raxのみ特殊
1131
1132 // [8bit rex] 0010 0101 [32bit offset]
1133 OpBuffer[obp++]=(char)RexByte;
1134 OpBuffer[obp++]=(char)0x25;
1135 *((long *)(OpBuffer+obp))=offset;
1136 obp+=sizeof(long);
1137 }
1138 else{
1139 //rax以外
1140
1141 //[8bit rex] 1000 0001 1100 0xxx [32bit offset]
1142 OpBuffer[obp++]=(char)RexByte;
1143 OpBuffer[obp++]=(char)0x81;
1144 OpBuffer[obp++]=(char)(0xE0| REGISTER_OPERAND(reg));
1145 *((long *)(OpBuffer+obp))=offset;
1146 obp+=sizeof(long);
1147 }
1148}
1149void op_and32_value(int reg,int offset){
1150 //and reg,offset
1151 char RexByte=-1;
1152
1153 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1154 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1155
1156 if(RexByte==-1) SetError(300,NULL,cp);
1157
1158 if(reg==REG_RAX){
1159 //eaxのみ特殊
1160
1161 // [8bit rex] 0010 0101 [32bit offset]
1162 OpBuffer[obp++]=(char)0x25;
1163 *((long *)(OpBuffer+obp))=offset;
1164 obp+=sizeof(long);
1165 }
1166 else{
1167 //eax以外
1168
1169 //[8bit rex] 1000 0001 1100 0xxx [32bit offset]
1170 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1171 OpBuffer[obp++]=(char)0x81;
1172 OpBuffer[obp++]=(char)(0xE0| REGISTER_OPERAND(reg));
1173 *((long *)(OpBuffer+obp))=offset;
1174 obp+=sizeof(long);
1175 }
1176}
1177
1178
1179
1180////////////////////////
1181// or 関連
1182////////////////////////
1183
1184void op_or_reg(int op_size,int reg1,int reg2){
1185 //or reg1,reg2
1186 char RexByte=-1;
1187
1188 if(op_size==sizeof(_int64)){
1189 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1190 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1191 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1192 }
1193 if(REG_R8<=reg1&&reg1<=REG_R15){
1194 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1195 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1196 }
1197 }
1198 else{
1199 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1200 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1201 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1202 }
1203 if(REG_R8<=reg1&&reg1<=REG_R15){
1204 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1205 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1206 }
1207 }
1208
1209 if(RexByte==-1) SetError(300,NULL,cp);
1210
1211
1212 //rexプリフィックス
1213 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1214
1215 //オペコード
1216 OpBuffer[obp++]=(char)0x0B;
1217
1218 //レジスタ
1219 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
1220}
1221
1222
1223
1224////////////////////////
1225// xor 関連
1226////////////////////////
1227
1228void op_xor_reg(int op_size,int reg1,int reg2){
1229 //xor reg1,reg2
1230 char RexByte=-1;
1231
1232 if(op_size==sizeof(_int64)){
1233 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1234 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1235 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1236 }
1237 if(REG_R8<=reg1&&reg1<=REG_R15){
1238 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1239 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1240 }
1241 }
1242 else{
1243 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1244 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1245 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1246 }
1247 if(REG_R8<=reg1&&reg1<=REG_R15){
1248 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1249 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1250 }
1251 }
1252
1253 if(RexByte==-1) SetError(300,NULL,cp);
1254
1255
1256 //rexプリフィックス
1257 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1258
1259 //オペコード
1260 OpBuffer[obp++]=(char)0x33;
1261
1262 //レジスタ
1263 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
1264}
1265
1266
1267
1268///////////////////////
1269// not 関連
1270///////////////////////
1271
1272void op_not_reg(int op_size,int reg){
1273 //not reg
1274 char RexByte=-1;
1275
1276 if(op_size==sizeof(_int64)){
1277 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1278 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1279 }
1280 else{
1281 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1282 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1283 }
1284
1285 if(RexByte==-1) SetError(300,NULL,cp);
1286
1287
1288 //rexプリフィックス
1289 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1290
1291 //オペコード
1292 OpBuffer[obp++]=(char)0xF7;
1293
1294 //レジスタ
1295 OpBuffer[obp++]=(char)(0xD0| REGISTER_OPERAND(reg));
1296}
1297void op_neg( int reg ){
1298 //neg reg
1299
1300 //オペコード
1301 OpBuffer[obp++]=(char)0xF7;
1302
1303 //レジスタ
1304 OpBuffer[obp++]=(char)(0xD8| REGISTER_OPERAND(reg));
1305}
1306
1307
1308
1309////////////////////
1310// test関連
1311////////////////////
1312
1313void op_test(int reg1,int reg2){
1314 //test reg1,reg2
1315 char RexByte=-1;
1316
1317 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1318 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1319 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1320 }
1321 if(REG_R8<=reg1&&reg1<=REG_R15){
1322 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1323 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1324 }
1325
1326 if(RexByte==-1) SetError(300,NULL,cp);
1327
1328 //[8bit rex] 1000 0101 11rr rbbb
1329 OpBuffer[obp++]=(char)RexByte;
1330 OpBuffer[obp++]=(char)0x85;
1331 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
1332}
1333
1334
1335
1336/////////////////////
1337// cmp 関連
1338/////////////////////
1339
1340void op_cmp_reg(int op_size,int reg1,int reg2){
1341 //cmp reg1,reg2
1342 char RexByte=-1;
1343
1344 if(op_size==sizeof(_int64)){
1345 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1346 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1347 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1348 }
1349 if(REG_R8<=reg1&&reg1<=REG_R15){
1350 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1351 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1352 }
1353 }
1354 else{
1355 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1356 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1357 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1358 }
1359 if(REG_R8<=reg1&&reg1<=REG_R15){
1360 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1361 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1362 }
1363 }
1364
1365 if(RexByte==-1) SetError(300,NULL,cp);
1366
1367
1368 //rexプリフィックス
1369 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1370
1371 //オペコード
1372 OpBuffer[obp++]=(char)0x3B;
1373
1374 //レジスタ
1375 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
1376}
1377void op_cmp_value(int op_size,int reg,char byte_data){
1378 //cmp reg,byte_data
1379
1380 if(op_size==sizeof(char)&&reg==REG_RAX){
1381 //alレジスタの場合は特殊
1382 OpBuffer[obp++]=(char)0x3C;
1383
1384 //8ビット値
1385 OpBuffer[obp++]=byte_data;
1386
1387 return;
1388 }
1389
1390 //16ビット演算のプリフィックス
1391 if(op_size==sizeof(short)) OpBuffer[obp++]=(char)0x66;
1392
1393 //rexプリフィックス
1394 set_rex(op_size,REG_NON,REG_NON,reg);
1395
1396 //オペコード
1397 if(op_size==sizeof(char)) OpBuffer[obp++]=(char)0x80;
1398 else OpBuffer[obp++]=(char)0x83;
1399
1400 //レジスタ
1401 OpBuffer[obp++]=(char)(0xF8| REGISTER_OPERAND(reg));
1402
1403 //8ビット値
1404 OpBuffer[obp++]=byte_data;
1405}
1406void op_setne( int reg ){
1407 //オペコード
1408 OpBuffer[obp++]=(char)0x0F;
1409 OpBuffer[obp++]=(char)0x95;
1410
1411 //レジスタ
1412 OpBuffer[obp++]=(char)( 0xC0 | REGISTER_OPERAND(reg) );
1413}
1414
1415
1416////////////////////
1417// SSE2関連
1418////////////////////
1419
1420void op_movlpd_MR(int xmm_reg,int base_reg,int offset,char mod){
1421 //movlpd qword ptr[base_reg+offset],xmm_reg
1422 __op_format(0,(char)0x66,(char)0x0F,(char)0x13,xmm_reg,base_reg,offset,mod);
1423}
1424void op_movlpd_RM(int xmm_reg,int base_reg,int offset,char mod){
1425 //movlpd xmm_reg,qword ptr[base_reg+offset]
1426 __op_format(0,(char)0x66,(char)0x0F,(char)0x12,xmm_reg,base_reg,offset,mod);
1427}
1428void op_movsd_RR(int xmm_reg1,int xmm_reg2){
1429 if(xmm_reg1==xmm_reg2) return;
1430
1431 //movsd xmm_reg1,xmm_reg2
1432 __op_format(0,(char)0xF2,(char)0x0F,(char)0x10,xmm_reg1,xmm_reg2,0,MOD_REG);
1433}
1434void op_movsd_MR(int xmm_reg,int base_reg,int offset,char mod){
1435 //movsd qword ptr[reg+offset],xmm_reg
1436 //movsd qword ptr[reg],xmm_reg
1437 __op_format(0,(char)0xF2,(char)0x0F,(char)0x11,xmm_reg,base_reg,offset,mod);
1438}
1439void op_movss_RR(int xmm_reg1,int xmm_reg2){
1440 if(xmm_reg1==xmm_reg2) return;
1441
1442 //movss xmm_reg1,xmm_reg2
1443 __op_format(0,(char)0xF3,(char)0x0F,(char)0x10,xmm_reg1,xmm_reg2,0,MOD_REG);
1444}
1445void op_movss_RM(int xmm_reg,int base_reg,int offset,char mod){
1446 //movss xmm_reg,dword ptr[base_reg+offset]
1447 __op_format(0,(char)0xF3,(char)0x0F,(char)0x10,xmm_reg,base_reg,offset,mod);
1448}
1449void op_movss_MR(int xmm_reg,int base_reg,int offset,char mod){
1450 //movss dword ptr[reg+offset],xmm_reg
1451 //movss dword ptr[reg],xmm_reg
1452 __op_format(0,(char)0xF3,(char)0x0F,(char)0x11,xmm_reg,base_reg,offset,mod);
1453}
1454
1455void op_movd_RX(int reg,int xmm_reg){
1456 __op_format(sizeof(_int64),(char)0x66,(char)0x0F,(char)0x7E,xmm_reg,reg,0,MOD_REG);
1457}
1458
1459void op_cvtsd2ss(int xmm_reg1,int xmm_reg2){
1460 //cvtsd2ss xmm_reg1,xmm_reg2
1461
1462 //オペコード
1463 OpBuffer[obp++]=(char)0xF2;
1464
1465 //rexプリフィックス
1466 set_rex(sizeof(long),xmm_reg1,0,xmm_reg2);
1467
1468 //オペコード
1469 OpBuffer[obp++]=(char)0x0F;
1470 OpBuffer[obp++]=(char)0x5A;
1471
1472 //レジスタ
1473 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2));
1474}
1475void op_cvtss2sd(int xmm_reg1,int xmm_reg2){
1476 //cvtss2sd xmm_reg1,xmm_reg2
1477
1478 //オペコード
1479 OpBuffer[obp++]=(char)0xF3;
1480
1481 //rexプリフィックス
1482 set_rex(0,xmm_reg1,0,xmm_reg2);
1483
1484 //オペコード
1485 OpBuffer[obp++]=(char)0x0F;
1486 OpBuffer[obp++]=(char)0x5A;
1487
1488 //レジスタ
1489 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2));
1490}
1491void op_cvttsd2si_xmm(int op_size,int reg,int xmm_reg){
1492 //cvttsd2si reg,xmm_reg
1493
1494 //オペコード
1495 OpBuffer[obp++]=(char)0xF2;
1496
1497 //rexプリフィックス
1498 set_rex(op_size,reg,0,xmm_reg);
1499
1500 //オペコード
1501 OpBuffer[obp++]=(char)0x0F;
1502 OpBuffer[obp++]=(char)0x2C;
1503
1504 //レジスタ
1505 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(xmm_reg));
1506}
1507void op_cvttss2si_xmm(int op_size,int reg,int xmm_reg){
1508 //cvttss2si reg,xmm_reg
1509
1510 //オペコード
1511 OpBuffer[obp++]=(char)0xF3;
1512
1513 //rexプリフィックス
1514 set_rex(op_size,reg,0,xmm_reg);
1515
1516 //オペコード
1517 OpBuffer[obp++]=(char)0x0F;
1518 OpBuffer[obp++]=(char)0x2C;
1519
1520 //レジスタ
1521 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(xmm_reg));
1522}
1523void op_cvtsi2sd_reg(int op_size,int xmm_reg,int reg){
1524 //cvtsi2sd xmm_reg,reg
1525 char RexByte=-1;
1526
1527 if(op_size==sizeof(_int64)){
1528 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1529 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1530 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1531 }
1532 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1533 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x4C;
1534 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x4D;
1535 }
1536 }
1537 else{
1538 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1539 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1540 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1541 }
1542 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1543 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x44;
1544 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x45;
1545 }
1546 }
1547
1548 if(RexByte==-1) SetError(300,NULL,cp);
1549
1550
1551 //オペコード
1552 OpBuffer[obp++]=(char)0xF2;
1553
1554 //rexプリフィックス
1555 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1556
1557 //オペコード
1558 OpBuffer[obp++]=(char)0x0F;
1559 OpBuffer[obp++]=(char)0x2A;
1560
1561 //レジスタ
1562 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg)<<3 | REGISTER_OPERAND(reg));
1563}
1564void op_cvtsi2ss_reg(int op_size,int xmm_reg,int reg){
1565 //cvtsi2ss xmm_reg,reg
1566 char RexByte=-1;
1567
1568 if(op_size==sizeof(_int64)){
1569 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1570 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1571 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1572 }
1573 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1574 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x4C;
1575 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x4D;
1576 }
1577 }
1578 else{
1579 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1580 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1581 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1582 }
1583 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1584 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x44;
1585 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x45;
1586 }
1587 }
1588
1589 if(RexByte==-1) SetError(300,NULL,cp);
1590
1591
1592 //オペコード
1593 OpBuffer[obp++]=(char)0xF3;
1594
1595 //rexプリフィックス
1596 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1597
1598 //オペコード
1599 OpBuffer[obp++]=(char)0x0F;
1600 OpBuffer[obp++]=(char)0x2A;
1601
1602 //レジスタ
1603 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg)<<3 | REGISTER_OPERAND(reg));
1604}
1605void op_comisd(int xmm_reg1,int xmm_reg2){
1606 //comisd xmm_reg1,xmm_reg2
1607
1608 //オペコード
1609 OpBuffer[obp++]=(char)0x66;
1610 OpBuffer[obp++]=(char)0x0F;
1611 OpBuffer[obp++]=(char)0x2F;
1612
1613 //レジスタ
1614 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2));
1615}
1616void op_comiss(int xmm_reg1,int xmm_reg2){
1617 //comiss xmm_reg1,xmm_reg2
1618
1619 //オペコード
1620 OpBuffer[obp++]=(char)0x0F;
1621 OpBuffer[obp++]=(char)0x2F;
1622
1623 //レジスタ
1624 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2));
1625}
1626
1627
1628
1629/////////////////////
1630// ストリング関係
1631/////////////////////
1632
1633void op_rep_movs(int op_size){
1634 if(op_size==sizeof(BYTE)){
1635 //rep movs byte ptr[edi],byte ptr[esi]
1636 OpBuffer[obp++]=(char)0xF3;
1637 OpBuffer[obp++]=(char)0xA4;
1638 }
1639 else if(op_size==sizeof(short)){
1640 //rep movs word ptr[edi],word ptr[esi]
1641 OpBuffer[obp++]=(char)0xF3;
1642 OpBuffer[obp++]=(char)0x66;
1643 OpBuffer[obp++]=(char)0xA5;
1644 }
1645 else if(op_size==sizeof(long)){
1646 //rep movs dword ptr[edi],dword ptr[esi]
1647 OpBuffer[obp++]=(char)0xF3;
1648 OpBuffer[obp++]=(char)0xA5;
1649 }
1650}
1651
1652
1653
1654
1655void op_add_rsp(int num){
1656 //スタックポインタの加算(pop方向)
1657
1658 //add rsp,num
1659 if(0xFFFFFF80&num){
1660 OpBuffer[obp++]=(char)0x48;
1661 OpBuffer[obp++]=(char)0x81;
1662 OpBuffer[obp++]=(char)0xC4;
1663 *((long *)(OpBuffer+obp))=num;
1664 obp+=sizeof(long);
1665 }
1666 else{
1667 //「-128 < num < 127」の場合
1668 OpBuffer[obp++]=(char)0x48;
1669 OpBuffer[obp++]=(char)0x83;
1670 OpBuffer[obp++]=(char)0xC4;
1671 OpBuffer[obp++]=(char)num;
1672 }
1673}
1674void op_sub_rsp(int num){
1675 //スタックポインタの減算(push方向)
1676
1677 //sub rsp,num
1678 if(0xFFFFFF80&num){
1679 OpBuffer[obp++]=(char)0x48;
1680 OpBuffer[obp++]=(char)0x81;
1681 OpBuffer[obp++]=(char)0xEC;
1682 *((long *)(OpBuffer+obp))=num;
1683 obp+=sizeof(long);
1684 }
1685 else{
1686 //「-128 < num < 127」の場合
1687 OpBuffer[obp++]=(char)0x48;
1688 OpBuffer[obp++]=(char)0x83;
1689 OpBuffer[obp++]=(char)0xEC;
1690 OpBuffer[obp++]=(char)num;
1691 }
1692}
1693
1694
1695void op_add_esp(int num){
1696 //スタックポインタの加算(pop方向)
1697
1698 //add esp,num
1699 if(0xFFFFFF80&num){
1700 OpBuffer[obp++]=(char)0x81;
1701 OpBuffer[obp++]=(char)0xC4;
1702 *((long *)(OpBuffer+obp))=num;
1703 obp+=sizeof(long);
1704 }
1705 else{
1706 //「-128 < num < 127」の場合
1707 OpBuffer[obp++]=(char)0x83;
1708 OpBuffer[obp++]=(char)0xC4;
1709 OpBuffer[obp++]=(char)num;
1710 }
1711}
1712void op_sub_esp(int num){
1713 //スタックポインタの減算(push方向)
1714
1715 //sub esp,num
1716 if(0xFFFFFF80&num){
1717 OpBuffer[obp++]=(char)0x81;
1718 OpBuffer[obp++]=(char)0xEC;
1719 *((long *)(OpBuffer+obp))=num;
1720 obp+=sizeof(long);
1721 }
1722 else{
1723 //「-128 < num < 127」の場合
1724 OpBuffer[obp++]=(char)0x83;
1725 OpBuffer[obp++]=(char)0xEC;
1726 OpBuffer[obp++]=(char)num;
1727 }
1728}
1729
1730
1731
1732//////////////////////////////
1733// 浮動小数点関連
1734//////////////////////////////
1735
1736void op_fld_ptr_esp(int type){
1737 //スタックポインタが示すバッファのデータを浮動小数点レジスタへロード
1738
1739 if(type==DEF_DOUBLE){
1740 //fld qword ptr[esp]
1741 OpBuffer[obp++]=(char)0xDD;
1742 OpBuffer[obp++]=(char)0x04;
1743 OpBuffer[obp++]=(char)0x24;
1744 }
1745 else if(type==DEF_SINGLE){
1746 //fld dword ptr[esp]
1747 OpBuffer[obp++]=(char)0xD9;
1748 OpBuffer[obp++]=(char)0x04;
1749 OpBuffer[obp++]=(char)0x24;
1750 }
1751 else if(type==DEF_INT64){
1752 //fild qword ptr[esp]
1753 OpBuffer[obp++]=(char)0xDF;
1754 OpBuffer[obp++]=(char)0x2C;
1755 OpBuffer[obp++]=(char)0x24;
1756 }
1757 else if(type==DEF_LONG){
1758 //fild dword ptr[esp]
1759 OpBuffer[obp++]=(char)0xDB;
1760 OpBuffer[obp++]=(char)0x04;
1761 OpBuffer[obp++]=(char)0x24;
1762 }
1763}
1764
1765
1766
1767//////////////////////////////
1768// レジスタ関連
1769//////////////////////////////
1770
1771void op_zero_reg(int reg){
1772 //レジスタに0をセット
1773
1774 if(REG_RAX<=reg&&reg<=REG_RDI){
1775 /* rax~rdi
1776 0100 1000 0011 0011 11 xxx xxx */
1777 OpBuffer[obp++]=(char)0x48;
1778 OpBuffer[obp++]=(char)0x33;
1779 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg));
1780 }
1781 if(REG_R8<=reg&&reg<=REG_R15){
1782 /* r8~r15
1783 0100 1101 0011 0011 11 xxx xxx */
1784 OpBuffer[obp++]=(char)0x4D;
1785 OpBuffer[obp++]=(char)0x33;
1786 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg));
1787 }
1788}
1789
1790
1791
1792/////////////////////////////
1793// 関数呼び出し
1794/////////////////////////////
1795
1796void op_call( SUBINFO *psi ){
1797 OpBuffer[obp++] = (char)0xE8;
1798 pobj_SubAddrSchedule->add( psi, 1 );
1799 psi->bUse = 1;
1800 obp += sizeof(long);
1801}
1802void op_call( DECLAREINFO *pdi ){
1803 OpBuffer[obp++] = (char)0xFF;
1804 OpBuffer[obp++] = (char)0x15;
1805 pobj_ImportAddrSchedule->add(pdi);
1806 obp += sizeof(long);
1807
1808 pdi->bUse=1;
1809}
Note: See TracBrowser for help on using the repository browser.