source: dev/trunk/abdev/BasicCompiler64/amd64_main.cpp@ 220

Last change on this file since 220 was 206, checked in by dai_9181, 17 years ago

コード全体のリファクタリングを実施

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