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

Last change on this file since 168 was 142, checked in by dai_9181, 17 years ago

op_ret関数を用意した

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 //mov reg1,reg2
427 char RexByte=-1;
428
429 if(reg1==reg2) return;
430
431 if(REG_RAX<=reg1&&reg1<=REG_RDI){
432 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
433 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
434 }
435 if(REG_R8<=reg1&&reg1<=REG_R15){
436 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
437 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
438 }
439
440 if(RexByte==-1) SetError(300,NULL,cp);
441
442 // [8bit rex] 1000 1011 11xx xbbb
443 OpBuffer[obp++]=RexByte;
444 OpBuffer[obp++]=(char)0x8B;
445 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
446}
447
448
449
450///////////////////
451// mov64関連
452///////////////////
453
454void op_mov64_ToReg(int reg,_int64 i64data){
455 //mov reg,i64data
456
457 if(REG_RAX<=reg&&reg<=REG_RDI){
458 /* rax~rdi
459 0100 1000 1011 1xxx [64bit data] */
460 OpBuffer[obp++]=(char)0x48;
461 OpBuffer[obp++]=(char)(0xB8| REGISTER_OPERAND(reg) );
462 *((_int64 *)(OpBuffer+obp))=i64data;
463 obp+=sizeof(_int64);
464 }
465 if(REG_R8<=reg&&reg<=REG_R15){
466 /* r8~r15
467 0100 1001 1011 1xxx [64bit data] */
468 OpBuffer[obp++]=(char)0x49;
469 OpBuffer[obp++]=(char)(0xB8| REGISTER_OPERAND(reg) );
470 *((_int64 *)(OpBuffer+obp))=i64data;
471 obp+=sizeof(_int64);
472 }
473}
474void op_mov64_ToReg(int reg,int i32data){
475 //mov reg,i32data
476
477 if(REG_RAX<=reg&&reg<=REG_RDI){
478 /* rax~rdi
479 0100 1000 1100 0111 1100 0xxx [32bit data] */
480 OpBuffer[obp++]=(char)0x48;
481 OpBuffer[obp++]=(char)0xC7;
482 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg) );
483 *((long *)(OpBuffer+obp))=i32data;
484 obp+=sizeof(long);
485 }
486 if(REG_R8<=reg&&reg<=REG_R15){
487 /* r8~r15
488 0100 1001 1100 0111 1100 0xxx [32bit data] */
489 OpBuffer[obp++]=(char)0x49;
490 OpBuffer[obp++]=(char)0xC7;
491 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg) );
492 *((long *)(OpBuffer+obp))=i32data;
493 obp+=sizeof(long);
494 }
495}
496void op_movsxd(int reg64,int reg32){
497 //movsxd reg64,reg32
498 char RexByte=-1;
499
500 if(REG_RAX<=reg64&&reg64<=REG_RDI){
501 if(REG_RAX<=reg32&&reg32<=REG_RDI) RexByte=(char)0x48;
502 if(REG_R8<=reg32&&reg32<=REG_R15) RexByte=(char)0x49;
503 }
504 if(REG_R8<=reg64&&reg64<=REG_R15){
505 if(REG_RAX<=reg32&&reg32<=REG_RDI) RexByte=(char)0x4C;
506 if(REG_R8<=reg32&&reg32<=REG_R15) RexByte=(char)0x4D;
507 }
508
509 if(RexByte==-1) SetError(300,NULL,cp);
510
511 //[8bit rex] 0110 0011 11rr rbbb
512 OpBuffer[obp++]=RexByte;
513 OpBuffer[obp++]=(char)0x63;
514 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg64)<<3 | REGISTER_OPERAND(reg32));
515}
516void op_movsx64_FromReg16(int reg64,int reg16){
517 //movsx reg64,reg16
518 char RexByte=-1;
519
520 if(REG_RAX<=reg64&&reg64<=REG_RDI){
521 if(REG_RAX<=reg16&&reg16<=REG_RDI) RexByte=(char)0x48;
522 if(REG_R8<=reg16&&reg16<=REG_R15) RexByte=(char)0x49;
523 }
524 if(REG_R8<=reg64&&reg64<=REG_R15){
525 if(REG_RAX<=reg16&&reg16<=REG_RDI) RexByte=(char)0x4C;
526 if(REG_R8<=reg16&&reg16<=REG_R15) RexByte=(char)0x4D;
527 }
528
529 if(RexByte==-1) SetError(300,NULL,cp);
530
531 //[8bit rex] 0000 1111 1011 1111 11rr rbbb
532 OpBuffer[obp++]=RexByte;
533 OpBuffer[obp++]=(char)0x0F;
534 OpBuffer[obp++]=(char)0xBF;
535 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg64)<<3 | REGISTER_OPERAND(reg16));
536}
537void op_movsx64_FromReg8(int reg64,int reg8){
538 //movsx reg64,reg8
539 char RexByte=-1;
540
541 if(REG_RAX<=reg64&&reg64<=REG_RDI){
542 if(REG_RAX<=reg8&&reg8<=REG_RDI) RexByte=(char)0x48;
543 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x49;
544 }
545 if(REG_R8<=reg64&&reg64<=REG_R15){
546 if(REG_RAX<=reg8&&reg8<=REG_RDI) RexByte=(char)0x4C;
547 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x4D;
548 }
549
550 if(RexByte==-1) SetError(300,NULL,cp);
551
552 //[8bit rex] 0000 1111 1011 1110 11rr rbbb
553 OpBuffer[obp++]=RexByte;
554 OpBuffer[obp++]=(char)0x0F;
555 OpBuffer[obp++]=(char)0xBE;
556 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg64)<<3 | REGISTER_OPERAND(reg8));
557}
558
559
560
561//////////////////
562// mov32関連
563//////////////////
564
565void op_movsx32_FromReg16(int reg32,int reg16){
566 //movsx reg32,reg16
567 char RexByte=-1;
568
569 if(REG_RAX<=reg32&&reg32<=REG_RDI){
570 if(REG_RAX<=reg16&&reg16<=REG_RDI) RexByte=0;
571 if(REG_R8<=reg16&&reg16<=REG_R15) RexByte=(char)0x41;
572 }
573 if(REG_R8<=reg32&&reg32<=REG_R15){
574 if(REG_RAX<=reg16&&reg16<=REG_RDI) RexByte=(char)0x44;
575 if(REG_R8<=reg16&&reg16<=REG_R15) RexByte=(char)0x45;
576 }
577
578 if(RexByte==-1) SetError(300,NULL,cp);
579
580 //[8bit rex] 0000 1111 1011 1111 11rr rbbb
581 if(RexByte) OpBuffer[obp++]=RexByte;
582 OpBuffer[obp++]=(char)0x0F;
583 OpBuffer[obp++]=(char)0xBF;
584 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg32)<<3 | REGISTER_OPERAND(reg16));
585}
586void op_movsx32_FromReg8(int reg32,int reg8){
587 //movsx reg32,reg8
588 char RexByte=-1;
589
590 if(REG_RAX<=reg32&&reg32<=REG_RDI){
591 if(REG_RAX<=reg8&&reg8<=REG_RBX) RexByte=0;
592 if(REG_RSP<=reg8&&reg8<=REG_RDI) RexByte=(char)0x40;
593 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x41;
594 }
595 if(REG_R8<=reg32&&reg32<=REG_R15){
596 if(REG_RAX<=reg8&&reg8<=REG_RDI) RexByte=(char)0x44;
597 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x45;
598 }
599
600 if(RexByte==-1) SetError(300,NULL,cp);
601
602 //[8bit rex] 0000 1111 1011 1110 11rr rbbb
603 if(RexByte) OpBuffer[obp++]=RexByte;
604 OpBuffer[obp++]=(char)0x0F;
605 OpBuffer[obp++]=(char)0xBE;
606 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg32)<<3 | REGISTER_OPERAND(reg8));
607}
608
609
610
611/////////////////////
612// mov16関連
613/////////////////////
614
615void op_movsx16_FromReg8(int reg32,int reg8){
616 //movsx reg16,reg8
617 char RexByte=-1;
618
619 if(REG_RAX<=reg32&&reg32<=REG_RDI){
620 if(REG_RAX<=reg8&&reg8<=REG_RBX) RexByte=0;
621 if(REG_RSP<=reg8&&reg8<=REG_RDI) RexByte=(char)0x40;
622 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x41;
623 }
624 if(REG_R8<=reg32&&reg32<=REG_R15){
625 if(REG_RAX<=reg8&&reg8<=REG_RDI) RexByte=(char)0x44;
626 if(REG_R8<=reg8&&reg8<=REG_R15) RexByte=(char)0x45;
627 }
628
629 if(RexByte==-1) SetError(300,NULL,cp);
630
631 //0110 0110 [8bit rex] 0000 1111 1011 1110 11rr rbbb
632 OpBuffer[obp++]=(char)0x66;
633 if(RexByte) OpBuffer[obp++]=RexByte;
634 OpBuffer[obp++]=(char)0x0F;
635 OpBuffer[obp++]=(char)0xBE;
636 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg32)<<3 | REGISTER_OPERAND(reg8));
637}
638
639
640
641//////////////////////////////////
642// インクリメント・デクリメント
643//////////////////////////////////
644
645void op_inc(int reg){
646 //inc reg
647
648 //16ビット演算の命令プリフィックス
649 char op_prefix=0;
650
651 //オペコード
652 char opcode=(char)0xFF;
653
654 __op_format(sizeof(_int64),op_prefix,opcode,0,0,reg,0,MOD_REG);
655}
656void op_dec(int reg){
657 op_inc(reg);
658 OpBuffer[obp-1]=OpBuffer[obp-1]|0x08;
659}
660
661
662
663/////////////////////
664// add関連
665/////////////////////
666
667void op_add_RM(int op_size,int reg,int base_reg,int offset,char mod){
668 //add reg64,qword ptr[base_reg+offset]
669 //add reg32,dword ptr[base_reg+offset]
670 //add reg16,word ptr[base_reg+offset]
671 //add reg8,byte ptr[base_reg+offset]
672
673 //16ビット演算の命令プリフィックス
674 char op_prefix=0;
675 if(op_size==sizeof(short)) op_prefix=(char)0x66;
676
677 //オペコード
678 char opcode;
679 if(op_size==sizeof(char)) opcode=(char)0x02;
680 else opcode=(char)0x03;
681
682 __op_format(op_size,op_prefix,opcode,0,reg,base_reg,offset,mod);
683}
684
685void op_add_RV(int reg,int offset){
686 //add reg,offset
687 char RexByte=-1;
688
689 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0x48;
690 if(REG_R8<=reg&&reg<=REG_R15) RexByte=0x49;
691
692 if(RexByte==-1) SetError(300,NULL,cp);
693
694 if(reg==REG_RAX){
695 //raxのみ特殊
696
697 // [8bit rex] 0000 0101 [32bit offset]
698 OpBuffer[obp++]=(char)RexByte;
699 OpBuffer[obp++]=(char)0x05;
700 *((long *)(OpBuffer+obp))=offset;
701 obp+=sizeof(long);
702 }
703 else{
704 //rax以外
705
706 //[8bit rex] 1000 0001 1100 0xxx [32bit offset]
707 OpBuffer[obp++]=(char)RexByte;
708 OpBuffer[obp++]=(char)0x81;
709 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg));
710 *((long *)(OpBuffer+obp))=offset;
711 obp+=sizeof(long);
712 }
713}
714void op_add64_reg(int reg1,int reg2){
715 //add reg1,reg2
716 char RexByte=-1;
717
718 if(REG_RAX<=reg1&&reg1<=REG_RDI){
719 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
720 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
721 }
722 if(REG_R8<=reg1&&reg1<=REG_R15){
723 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
724 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
725 }
726
727 if(RexByte==-1) SetError(300,NULL,cp);
728
729 //[8bit rex] 0000 0011 11rr rbbb
730 OpBuffer[obp++]=(char)RexByte;
731 OpBuffer[obp++]=(char)0x03;
732 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
733}
734void op_add32_reg(int reg1,int reg2){
735 //add reg1,reg2
736 char RexByte=-1;
737
738 if(REG_RAX<=reg1&&reg1<=REG_RDI){
739 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
740 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
741 }
742 if(REG_R8<=reg1&&reg1<=REG_R15){
743 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
744 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
745 }
746
747 if(RexByte==-1) SetError(300,NULL,cp);
748
749 //[8bit rex] 0000 0011 11rr rbbb
750 if(RexByte) OpBuffer[obp++]=(char)RexByte;
751 OpBuffer[obp++]=(char)0x03;
752 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
753}
754
755
756
757////////////////////////
758// sub関連
759////////////////////////
760
761void op_sub_RV(int op_size,int reg,int i32data){
762 //sub reg,i32data
763
764 //rexプリフィックス
765 set_rex(op_size,REG_NON,REG_NON,reg);
766
767 if(reg==REG_RAX){
768 //raxのみ特殊
769 OpBuffer[obp++]=(char)0x2D;
770 }
771 else{
772 //オペコード
773 OpBuffer[obp++]=(char)0x81;
774
775 //レジスタ
776 OpBuffer[obp++]=(char)(0xE8| REGISTER_OPERAND(reg) );
777 }
778
779 //即値
780 *((long *)(OpBuffer+obp))=i32data;
781 obp+=sizeof(long);
782}
783void op_sub64_reg(int reg1,int reg2){
784 //sub reg1,reg2
785 char RexByte=-1;
786
787 if(REG_RAX<=reg1&&reg1<=REG_RDI){
788 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
789 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
790 }
791 if(REG_R8<=reg1&&reg1<=REG_R15){
792 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
793 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
794 }
795
796 if(RexByte==-1) SetError(300,NULL,cp);
797
798 //[8bit rex] 0010 1011 11rr rbbb
799 OpBuffer[obp++]=(char)RexByte;
800 OpBuffer[obp++]=(char)0x2B;
801 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
802}
803void op_sub32_reg(int reg1,int reg2){
804 //sub reg1,reg2
805 char RexByte=-1;
806
807 if(REG_RAX<=reg1&&reg1<=REG_RDI){
808 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
809 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
810 }
811 if(REG_R8<=reg1&&reg1<=REG_R15){
812 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
813 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
814 }
815
816 if(RexByte==-1) SetError(300,NULL,cp);
817
818 //[8bit rex] 0010 1011 11rr rbbb
819 if(RexByte) OpBuffer[obp++]=(char)RexByte;
820 OpBuffer[obp++]=(char)0x2B;
821 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
822}
823void op_sbb_RR( int op_size, int reg1, int reg2 ){
824 //sbb reg1,reg2
825
826 //rexプリフィックス
827 set_rex(0,reg1,0,reg2);
828
829 //オペコード
830 OpBuffer[obp++]=(char)0x1B;
831
832 //レジスタ
833 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
834}
835
836
837
838////////////////////////
839// imul関連
840////////////////////////
841
842void op_imul_RR(int op_size,int reg1,int reg2){
843 //imul reg1,reg2
844 char RexByte=-1;
845
846 if(op_size==sizeof(_int64)){
847 if(REG_RAX<=reg1&&reg1<=REG_RDI){
848 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
849 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
850 }
851 if(REG_R8<=reg1&&reg1<=REG_R15){
852 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
853 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
854 }
855 }
856 else{
857 if(REG_RAX<=reg1&&reg1<=REG_RDI){
858 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
859 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
860 }
861 if(REG_R8<=reg1&&reg1<=REG_R15){
862 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
863 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
864 }
865 }
866
867 if(RexByte==-1) SetError(300,NULL,cp);
868
869
870 //rexプリフィックス
871 if(RexByte) OpBuffer[obp++]=(char)RexByte;
872
873 //オペコード
874 OpBuffer[obp++]=(char)0x0F;
875 OpBuffer[obp++]=(char)0xAF;
876
877 //レジスタ
878 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
879}
880void op_imul_RV(int op_size,int reg,int i32data){
881 //imul reg,i32data
882 char RexByte=-1;
883
884 if(op_size==sizeof(_int64)){
885 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
886 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x4D;
887 }
888 else{
889 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
890 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x45;
891 }
892
893 if(RexByte==-1) SetError(300,NULL,cp);
894
895
896 //rexプリフィックス
897 if(RexByte) OpBuffer[obp++]=(char)RexByte;
898
899 if(-128<=i32data&&i32data<=127){
900 //オペコード
901 OpBuffer[obp++]=(char)0x6B;
902
903 //レジスタ
904 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg));
905
906 //値
907 OpBuffer[obp++]=(char)i32data;
908 }
909 else{
910 //オペコード
911 OpBuffer[obp++]=(char)0x69;
912
913 //レジスタ
914 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg));
915
916 //値
917 *((long *)(OpBuffer+obp))=i32data;
918 obp+=sizeof(long);
919 }
920}
921
922
923
924////////////////////////
925// div、idiv関連
926////////////////////////
927
928void op_div64_reg(int reg){
929 //div reg
930 char RexByte=-1;
931
932 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
933 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
934
935 if(RexByte==-1) SetError(300,NULL,cp);
936
937 //rexプリフィックス
938 OpBuffer[obp++]=(char)RexByte;
939
940 //オペコード
941 OpBuffer[obp++]=(char)0xF7;
942
943 //レジスタ
944 OpBuffer[obp++]=(char)(0xF0| REGISTER_OPERAND(reg));
945}
946void op_idiv64_reg(int reg){
947 //idiv reg
948 char RexByte=-1;
949
950 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
951 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
952
953 if(RexByte==-1) SetError(300,NULL,cp);
954
955 //rexプリフィックス
956 OpBuffer[obp++]=(char)RexByte;
957
958 //オペコード
959 OpBuffer[obp++]=(char)0xF7;
960
961 //レジスタ
962 OpBuffer[obp++]=(char)(0xF8| REGISTER_OPERAND(reg));
963}
964
965
966
967////////////////////
968// ビットシフト関連
969////////////////////
970
971void op_shl_reg(int op_size,int reg){
972 //shl reg,cl
973 char RexByte=-1;
974
975 if(op_size==sizeof(_int64)){
976 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
977 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
978 }
979 else if(op_size==sizeof(char)){
980 if(REG_RAX<=reg&&reg<=REG_RBX) RexByte=0;
981 if(REG_RSP<=reg&&reg<=REG_RDI) RexByte=(char)0x40;
982 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
983 }
984 else{
985 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
986 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
987 }
988
989 if(RexByte==-1) SetError(300,NULL,cp);
990
991
992 //16ビット演算のプリフィックス
993 if(op_size==sizeof(short)) OpBuffer[obp++]=(char)0x66;
994
995 //rexプリフィックス
996 if(RexByte) OpBuffer[obp++]=(char)RexByte;
997
998 //オペコード
999 if(op_size==sizeof(char)) OpBuffer[obp++]=(char)0xD2;
1000 else OpBuffer[obp++]=(char)0xD3;
1001
1002 //レジスタ
1003 OpBuffer[obp++]=(char)(0xE0| REGISTER_OPERAND(reg));
1004}
1005void op_sar_reg(int op_size,int reg){
1006 //sar reg,cl
1007 char RexByte=-1;
1008
1009 if(op_size==sizeof(_int64)){
1010 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1011 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1012 }
1013 else if(op_size==sizeof(char)){
1014 if(REG_RAX<=reg&&reg<=REG_RBX) RexByte=0;
1015 if(REG_RSP<=reg&&reg<=REG_RDI) RexByte=(char)0x40;
1016 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1017 }
1018 else{
1019 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1020 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1021 }
1022
1023 if(RexByte==-1) SetError(300,NULL,cp);
1024
1025
1026 //16ビット演算のプリフィックス
1027 if(op_size==sizeof(short)) OpBuffer[obp++]=(char)0x66;
1028
1029 //rexプリフィックス
1030 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1031
1032 //オペコード
1033 if(op_size==sizeof(char)) OpBuffer[obp++]=(char)0xD2;
1034 else OpBuffer[obp++]=(char)0xD3;
1035
1036 //レジスタ
1037 OpBuffer[obp++]=(char)(0xF8| REGISTER_OPERAND(reg));
1038}
1039void op_shr_reg(int op_size,int reg){
1040 //shr reg,cl
1041 char RexByte=-1;
1042
1043 if(op_size==sizeof(_int64)){
1044 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1045 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1046 }
1047 else if(op_size==sizeof(char)){
1048 if(REG_RAX<=reg&&reg<=REG_RBX) RexByte=0;
1049 if(REG_RSP<=reg&&reg<=REG_RDI) RexByte=(char)0x40;
1050 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1051 }
1052 else{
1053 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1054 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1055 }
1056
1057 if(RexByte==-1) SetError(300,NULL,cp);
1058
1059
1060 //16ビット演算のプリフィックス
1061 if(op_size==sizeof(short)) OpBuffer[obp++]=(char)0x66;
1062
1063 //rexプリフィックス
1064 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1065
1066 //オペコード
1067 if(op_size==sizeof(char)) OpBuffer[obp++]=(char)0xD2;
1068 else OpBuffer[obp++]=(char)0xD3;
1069
1070 //レジスタ
1071 OpBuffer[obp++]=(char)(0xE8| REGISTER_OPERAND(reg));
1072}
1073
1074
1075
1076////////////////////
1077// and 関連
1078////////////////////
1079
1080void op_and_reg(int op_size,int reg1,int reg2){
1081 //and reg1,reg2
1082 char RexByte=-1;
1083
1084 if(op_size==sizeof(_int64)){
1085 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1086 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1087 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1088 }
1089 if(REG_R8<=reg1&&reg1<=REG_R15){
1090 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1091 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1092 }
1093 }
1094 else{
1095 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1096 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1097 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1098 }
1099 if(REG_R8<=reg1&&reg1<=REG_R15){
1100 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1101 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1102 }
1103 }
1104
1105 if(RexByte==-1) SetError(300,NULL,cp);
1106
1107
1108 //rexプリフィックス
1109 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1110
1111 //オペコード
1112 OpBuffer[obp++]=(char)0x23;
1113
1114 //レジスタ
1115 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
1116}
1117void op_and64_value(int reg,int offset){
1118 //and reg,offset
1119 char RexByte=-1;
1120
1121 if(REG_RAX<=reg&&reg<=REG_RDI) (char)RexByte=0x48;
1122 if(REG_R8<=reg&&reg<=REG_R15) (char)RexByte=0x49;
1123
1124 if(RexByte==-1) SetError(300,NULL,cp);
1125
1126 if(reg==REG_RAX){
1127 //raxのみ特殊
1128
1129 // [8bit rex] 0010 0101 [32bit offset]
1130 OpBuffer[obp++]=(char)RexByte;
1131 OpBuffer[obp++]=(char)0x25;
1132 *((long *)(OpBuffer+obp))=offset;
1133 obp+=sizeof(long);
1134 }
1135 else{
1136 //rax以外
1137
1138 //[8bit rex] 1000 0001 1100 0xxx [32bit offset]
1139 OpBuffer[obp++]=(char)RexByte;
1140 OpBuffer[obp++]=(char)0x81;
1141 OpBuffer[obp++]=(char)(0xE0| REGISTER_OPERAND(reg));
1142 *((long *)(OpBuffer+obp))=offset;
1143 obp+=sizeof(long);
1144 }
1145}
1146void op_and32_value(int reg,int offset){
1147 //and reg,offset
1148 char RexByte=-1;
1149
1150 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1151 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1152
1153 if(RexByte==-1) SetError(300,NULL,cp);
1154
1155 if(reg==REG_RAX){
1156 //eaxのみ特殊
1157
1158 // [8bit rex] 0010 0101 [32bit offset]
1159 OpBuffer[obp++]=(char)0x25;
1160 *((long *)(OpBuffer+obp))=offset;
1161 obp+=sizeof(long);
1162 }
1163 else{
1164 //eax以外
1165
1166 //[8bit rex] 1000 0001 1100 0xxx [32bit offset]
1167 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1168 OpBuffer[obp++]=(char)0x81;
1169 OpBuffer[obp++]=(char)(0xE0| REGISTER_OPERAND(reg));
1170 *((long *)(OpBuffer+obp))=offset;
1171 obp+=sizeof(long);
1172 }
1173}
1174
1175
1176
1177////////////////////////
1178// or 関連
1179////////////////////////
1180
1181void op_or_reg(int op_size,int reg1,int reg2){
1182 //or reg1,reg2
1183 char RexByte=-1;
1184
1185 if(op_size==sizeof(_int64)){
1186 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1187 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1188 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1189 }
1190 if(REG_R8<=reg1&&reg1<=REG_R15){
1191 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1192 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1193 }
1194 }
1195 else{
1196 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1197 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1198 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1199 }
1200 if(REG_R8<=reg1&&reg1<=REG_R15){
1201 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1202 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1203 }
1204 }
1205
1206 if(RexByte==-1) SetError(300,NULL,cp);
1207
1208
1209 //rexプリフィックス
1210 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1211
1212 //オペコード
1213 OpBuffer[obp++]=(char)0x0B;
1214
1215 //レジスタ
1216 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
1217}
1218
1219
1220
1221////////////////////////
1222// xor 関連
1223////////////////////////
1224
1225void op_xor_reg(int op_size,int reg1,int reg2){
1226 //xor reg1,reg2
1227 char RexByte=-1;
1228
1229 if(op_size==sizeof(_int64)){
1230 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1231 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1232 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1233 }
1234 if(REG_R8<=reg1&&reg1<=REG_R15){
1235 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1236 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1237 }
1238 }
1239 else{
1240 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1241 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1242 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1243 }
1244 if(REG_R8<=reg1&&reg1<=REG_R15){
1245 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1246 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1247 }
1248 }
1249
1250 if(RexByte==-1) SetError(300,NULL,cp);
1251
1252
1253 //rexプリフィックス
1254 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1255
1256 //オペコード
1257 OpBuffer[obp++]=(char)0x33;
1258
1259 //レジスタ
1260 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
1261}
1262
1263
1264
1265///////////////////////
1266// not 関連
1267///////////////////////
1268
1269void op_not_reg(int op_size,int reg){
1270 //not reg
1271 char RexByte=-1;
1272
1273 if(op_size==sizeof(_int64)){
1274 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1275 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1276 }
1277 else{
1278 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1279 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1280 }
1281
1282 if(RexByte==-1) SetError(300,NULL,cp);
1283
1284
1285 //rexプリフィックス
1286 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1287
1288 //オペコード
1289 OpBuffer[obp++]=(char)0xF7;
1290
1291 //レジスタ
1292 OpBuffer[obp++]=(char)(0xD0| REGISTER_OPERAND(reg));
1293}
1294void op_neg( int reg ){
1295 //neg reg
1296
1297 //オペコード
1298 OpBuffer[obp++]=(char)0xF7;
1299
1300 //レジスタ
1301 OpBuffer[obp++]=(char)(0xD8| REGISTER_OPERAND(reg));
1302}
1303
1304
1305
1306////////////////////
1307// test関連
1308////////////////////
1309
1310void op_test(int reg1,int reg2){
1311 //test reg1,reg2
1312 char RexByte=-1;
1313
1314 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1315 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1316 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1317 }
1318 if(REG_R8<=reg1&&reg1<=REG_R15){
1319 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1320 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1321 }
1322
1323 if(RexByte==-1) SetError(300,NULL,cp);
1324
1325 //[8bit rex] 1000 0101 11rr rbbb
1326 OpBuffer[obp++]=(char)RexByte;
1327 OpBuffer[obp++]=(char)0x85;
1328 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
1329}
1330
1331
1332
1333/////////////////////
1334// cmp 関連
1335/////////////////////
1336
1337void op_cmp_reg(int op_size,int reg1,int reg2){
1338 //cmp reg1,reg2
1339 char RexByte=-1;
1340
1341 if(op_size==sizeof(_int64)){
1342 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1343 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x48;
1344 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x49;
1345 }
1346 if(REG_R8<=reg1&&reg1<=REG_R15){
1347 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x4C;
1348 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x4D;
1349 }
1350 }
1351 else{
1352 if(REG_RAX<=reg1&&reg1<=REG_RDI){
1353 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=0;
1354 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x41;
1355 }
1356 if(REG_R8<=reg1&&reg1<=REG_R15){
1357 if(REG_RAX<=reg2&&reg2<=REG_RDI) RexByte=(char)0x44;
1358 if(REG_R8<=reg2&&reg2<=REG_R15) RexByte=(char)0x45;
1359 }
1360 }
1361
1362 if(RexByte==-1) SetError(300,NULL,cp);
1363
1364
1365 //rexプリフィックス
1366 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1367
1368 //オペコード
1369 OpBuffer[obp++]=(char)0x3B;
1370
1371 //レジスタ
1372 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg1)<<3 | REGISTER_OPERAND(reg2));
1373}
1374void op_cmp_value(int op_size,int reg,char byte_data){
1375 //cmp reg,byte_data
1376
1377 if(op_size==sizeof(char)&&reg==REG_RAX){
1378 //alレジスタの場合は特殊
1379 OpBuffer[obp++]=(char)0x3C;
1380
1381 //8ビット値
1382 OpBuffer[obp++]=byte_data;
1383
1384 return;
1385 }
1386
1387 //16ビット演算のプリフィックス
1388 if(op_size==sizeof(short)) OpBuffer[obp++]=(char)0x66;
1389
1390 //rexプリフィックス
1391 set_rex(op_size,REG_NON,REG_NON,reg);
1392
1393 //オペコード
1394 if(op_size==sizeof(char)) OpBuffer[obp++]=(char)0x80;
1395 else OpBuffer[obp++]=(char)0x83;
1396
1397 //レジスタ
1398 OpBuffer[obp++]=(char)(0xF8| REGISTER_OPERAND(reg));
1399
1400 //8ビット値
1401 OpBuffer[obp++]=byte_data;
1402}
1403void op_setne( int reg ){
1404 //オペコード
1405 OpBuffer[obp++]=(char)0x0F;
1406 OpBuffer[obp++]=(char)0x95;
1407
1408 //レジスタ
1409 OpBuffer[obp++]=(char)( 0xC0 | REGISTER_OPERAND(reg) );
1410}
1411
1412
1413////////////////////
1414// SSE2関連
1415////////////////////
1416
1417void op_movlpd_MR(int xmm_reg,int base_reg,int offset,char mod){
1418 //movlpd qword ptr[base_reg+offset],xmm_reg
1419 __op_format(0,(char)0x66,(char)0x0F,(char)0x13,xmm_reg,base_reg,offset,mod);
1420}
1421void op_movlpd_RM(int xmm_reg,int base_reg,int offset,char mod){
1422 //movlpd xmm_reg,qword ptr[base_reg+offset]
1423 __op_format(0,(char)0x66,(char)0x0F,(char)0x12,xmm_reg,base_reg,offset,mod);
1424}
1425void op_movsd_RR(int xmm_reg1,int xmm_reg2){
1426 if(xmm_reg1==xmm_reg2) return;
1427
1428 //movsd xmm_reg1,xmm_reg2
1429 __op_format(0,(char)0xF2,(char)0x0F,(char)0x10,xmm_reg1,xmm_reg2,0,MOD_REG);
1430}
1431void op_movsd_MR(int xmm_reg,int base_reg,int offset,char mod){
1432 //movsd qword ptr[reg+offset],xmm_reg
1433 //movsd qword ptr[reg],xmm_reg
1434 __op_format(0,(char)0xF2,(char)0x0F,(char)0x11,xmm_reg,base_reg,offset,mod);
1435}
1436void op_movss_RR(int xmm_reg1,int xmm_reg2){
1437 if(xmm_reg1==xmm_reg2) return;
1438
1439 //movss xmm_reg1,xmm_reg2
1440 __op_format(0,(char)0xF3,(char)0x0F,(char)0x10,xmm_reg1,xmm_reg2,0,MOD_REG);
1441}
1442void op_movss_RM(int xmm_reg,int base_reg,int offset,char mod){
1443 //movss xmm_reg,dword ptr[base_reg+offset]
1444 __op_format(0,(char)0xF3,(char)0x0F,(char)0x10,xmm_reg,base_reg,offset,mod);
1445}
1446void op_movss_MR(int xmm_reg,int base_reg,int offset,char mod){
1447 //movss dword ptr[reg+offset],xmm_reg
1448 //movss dword ptr[reg],xmm_reg
1449 __op_format(0,(char)0xF3,(char)0x0F,(char)0x11,xmm_reg,base_reg,offset,mod);
1450}
1451
1452void op_movd_RX(int reg,int xmm_reg){
1453 __op_format(sizeof(_int64),(char)0x66,(char)0x0F,(char)0x7E,xmm_reg,reg,0,MOD_REG);
1454}
1455
1456void op_cvtsd2ss(int xmm_reg1,int xmm_reg2){
1457 //cvtsd2ss xmm_reg1,xmm_reg2
1458
1459 //オペコード
1460 OpBuffer[obp++]=(char)0xF2;
1461
1462 //rexプリフィックス
1463 set_rex(sizeof(long),xmm_reg1,0,xmm_reg2);
1464
1465 //オペコード
1466 OpBuffer[obp++]=(char)0x0F;
1467 OpBuffer[obp++]=(char)0x5A;
1468
1469 //レジスタ
1470 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2));
1471}
1472void op_cvtss2sd(int xmm_reg1,int xmm_reg2){
1473 //cvtss2sd xmm_reg1,xmm_reg2
1474
1475 //オペコード
1476 OpBuffer[obp++]=(char)0xF3;
1477
1478 //rexプリフィックス
1479 set_rex(0,xmm_reg1,0,xmm_reg2);
1480
1481 //オペコード
1482 OpBuffer[obp++]=(char)0x0F;
1483 OpBuffer[obp++]=(char)0x5A;
1484
1485 //レジスタ
1486 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2));
1487}
1488void op_cvttsd2si_xmm(int op_size,int reg,int xmm_reg){
1489 //cvttsd2si reg,xmm_reg
1490
1491 //オペコード
1492 OpBuffer[obp++]=(char)0xF2;
1493
1494 //rexプリフィックス
1495 set_rex(op_size,reg,0,xmm_reg);
1496
1497 //オペコード
1498 OpBuffer[obp++]=(char)0x0F;
1499 OpBuffer[obp++]=(char)0x2C;
1500
1501 //レジスタ
1502 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(xmm_reg));
1503}
1504void op_cvttss2si_xmm(int op_size,int reg,int xmm_reg){
1505 //cvttss2si reg,xmm_reg
1506
1507 //オペコード
1508 OpBuffer[obp++]=(char)0xF3;
1509
1510 //rexプリフィックス
1511 set_rex(op_size,reg,0,xmm_reg);
1512
1513 //オペコード
1514 OpBuffer[obp++]=(char)0x0F;
1515 OpBuffer[obp++]=(char)0x2C;
1516
1517 //レジスタ
1518 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(xmm_reg));
1519}
1520void op_cvtsi2sd_reg(int op_size,int xmm_reg,int reg){
1521 //cvtsi2sd xmm_reg,reg
1522 char RexByte=-1;
1523
1524 if(op_size==sizeof(_int64)){
1525 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1526 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1527 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1528 }
1529 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1530 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x4C;
1531 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x4D;
1532 }
1533 }
1534 else{
1535 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1536 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1537 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1538 }
1539 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1540 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x44;
1541 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x45;
1542 }
1543 }
1544
1545 if(RexByte==-1) SetError(300,NULL,cp);
1546
1547
1548 //オペコード
1549 OpBuffer[obp++]=(char)0xF2;
1550
1551 //rexプリフィックス
1552 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1553
1554 //オペコード
1555 OpBuffer[obp++]=(char)0x0F;
1556 OpBuffer[obp++]=(char)0x2A;
1557
1558 //レジスタ
1559 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg)<<3 | REGISTER_OPERAND(reg));
1560}
1561void op_cvtsi2ss_reg(int op_size,int xmm_reg,int reg){
1562 //cvtsi2ss xmm_reg,reg
1563 char RexByte=-1;
1564
1565 if(op_size==sizeof(_int64)){
1566 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1567 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x48;
1568 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x49;
1569 }
1570 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1571 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x4C;
1572 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x4D;
1573 }
1574 }
1575 else{
1576 if(REG_XMM0<=xmm_reg&&xmm_reg<=REG_XMM7){
1577 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=0;
1578 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x41;
1579 }
1580 if(REG_XMM8<=xmm_reg&&xmm_reg<=REG_XMM15){
1581 if(REG_RAX<=reg&&reg<=REG_RDI) RexByte=(char)0x44;
1582 if(REG_R8<=reg&&reg<=REG_R15) RexByte=(char)0x45;
1583 }
1584 }
1585
1586 if(RexByte==-1) SetError(300,NULL,cp);
1587
1588
1589 //オペコード
1590 OpBuffer[obp++]=(char)0xF3;
1591
1592 //rexプリフィックス
1593 if(RexByte) OpBuffer[obp++]=(char)RexByte;
1594
1595 //オペコード
1596 OpBuffer[obp++]=(char)0x0F;
1597 OpBuffer[obp++]=(char)0x2A;
1598
1599 //レジスタ
1600 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg)<<3 | REGISTER_OPERAND(reg));
1601}
1602void op_comisd(int xmm_reg1,int xmm_reg2){
1603 //comisd xmm_reg1,xmm_reg2
1604
1605 //オペコード
1606 OpBuffer[obp++]=(char)0x66;
1607 OpBuffer[obp++]=(char)0x0F;
1608 OpBuffer[obp++]=(char)0x2F;
1609
1610 //レジスタ
1611 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2));
1612}
1613void op_comiss(int xmm_reg1,int xmm_reg2){
1614 //comiss xmm_reg1,xmm_reg2
1615
1616 //オペコード
1617 OpBuffer[obp++]=(char)0x0F;
1618 OpBuffer[obp++]=(char)0x2F;
1619
1620 //レジスタ
1621 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(xmm_reg1)<<3 | REGISTER_OPERAND(xmm_reg2));
1622}
1623
1624
1625
1626/////////////////////
1627// ストリング関係
1628/////////////////////
1629
1630void op_rep_movs(int op_size){
1631 if(op_size==sizeof(BYTE)){
1632 //rep movs byte ptr[edi],byte ptr[esi]
1633 OpBuffer[obp++]=(char)0xF3;
1634 OpBuffer[obp++]=(char)0xA4;
1635 }
1636 else if(op_size==sizeof(short)){
1637 //rep movs word ptr[edi],word ptr[esi]
1638 OpBuffer[obp++]=(char)0xF3;
1639 OpBuffer[obp++]=(char)0x66;
1640 OpBuffer[obp++]=(char)0xA5;
1641 }
1642 else if(op_size==sizeof(long)){
1643 //rep movs dword ptr[edi],dword ptr[esi]
1644 OpBuffer[obp++]=(char)0xF3;
1645 OpBuffer[obp++]=(char)0xA5;
1646 }
1647}
1648
1649
1650
1651
1652void op_add_rsp(int num){
1653 //スタックポインタの加算(pop方向)
1654
1655 //add rsp,num
1656 if(0xFFFFFF80&num){
1657 OpBuffer[obp++]=(char)0x48;
1658 OpBuffer[obp++]=(char)0x81;
1659 OpBuffer[obp++]=(char)0xC4;
1660 *((long *)(OpBuffer+obp))=num;
1661 obp+=sizeof(long);
1662 }
1663 else{
1664 //「-128 < num < 127」の場合
1665 OpBuffer[obp++]=(char)0x48;
1666 OpBuffer[obp++]=(char)0x83;
1667 OpBuffer[obp++]=(char)0xC4;
1668 OpBuffer[obp++]=(char)num;
1669 }
1670}
1671void op_sub_rsp(int num){
1672 //スタックポインタの減算(push方向)
1673
1674 //sub rsp,num
1675 if(0xFFFFFF80&num){
1676 OpBuffer[obp++]=(char)0x48;
1677 OpBuffer[obp++]=(char)0x81;
1678 OpBuffer[obp++]=(char)0xEC;
1679 *((long *)(OpBuffer+obp))=num;
1680 obp+=sizeof(long);
1681 }
1682 else{
1683 //「-128 < num < 127」の場合
1684 OpBuffer[obp++]=(char)0x48;
1685 OpBuffer[obp++]=(char)0x83;
1686 OpBuffer[obp++]=(char)0xEC;
1687 OpBuffer[obp++]=(char)num;
1688 }
1689}
1690
1691
1692void op_add_esp(int num){
1693 //スタックポインタの加算(pop方向)
1694
1695 //add esp,num
1696 if(0xFFFFFF80&num){
1697 OpBuffer[obp++]=(char)0x81;
1698 OpBuffer[obp++]=(char)0xC4;
1699 *((long *)(OpBuffer+obp))=num;
1700 obp+=sizeof(long);
1701 }
1702 else{
1703 //「-128 < num < 127」の場合
1704 OpBuffer[obp++]=(char)0x83;
1705 OpBuffer[obp++]=(char)0xC4;
1706 OpBuffer[obp++]=(char)num;
1707 }
1708}
1709void op_sub_esp(int num){
1710 //スタックポインタの減算(push方向)
1711
1712 //sub esp,num
1713 if(0xFFFFFF80&num){
1714 OpBuffer[obp++]=(char)0x81;
1715 OpBuffer[obp++]=(char)0xEC;
1716 *((long *)(OpBuffer+obp))=num;
1717 obp+=sizeof(long);
1718 }
1719 else{
1720 //「-128 < num < 127」の場合
1721 OpBuffer[obp++]=(char)0x83;
1722 OpBuffer[obp++]=(char)0xEC;
1723 OpBuffer[obp++]=(char)num;
1724 }
1725}
1726
1727
1728
1729//////////////////////////////
1730// 浮動小数点関連
1731//////////////////////////////
1732
1733void op_fld_ptr_esp(int type){
1734 //スタックポインタが示すバッファのデータを浮動小数点レジスタへロード
1735
1736 if(type==DEF_DOUBLE){
1737 //fld qword ptr[esp]
1738 OpBuffer[obp++]=(char)0xDD;
1739 OpBuffer[obp++]=(char)0x04;
1740 OpBuffer[obp++]=(char)0x24;
1741 }
1742 else if(type==DEF_SINGLE){
1743 //fld dword ptr[esp]
1744 OpBuffer[obp++]=(char)0xD9;
1745 OpBuffer[obp++]=(char)0x04;
1746 OpBuffer[obp++]=(char)0x24;
1747 }
1748 else if(type==DEF_INT64){
1749 //fild qword ptr[esp]
1750 OpBuffer[obp++]=(char)0xDF;
1751 OpBuffer[obp++]=(char)0x2C;
1752 OpBuffer[obp++]=(char)0x24;
1753 }
1754 else if(type==DEF_LONG){
1755 //fild dword ptr[esp]
1756 OpBuffer[obp++]=(char)0xDB;
1757 OpBuffer[obp++]=(char)0x04;
1758 OpBuffer[obp++]=(char)0x24;
1759 }
1760}
1761
1762
1763
1764//////////////////////////////
1765// レジスタ関連
1766//////////////////////////////
1767
1768void op_zero_reg(int reg){
1769 //レジスタに0をセット
1770
1771 if(REG_RAX<=reg&&reg<=REG_RDI){
1772 /* rax~rdi
1773 0100 1000 0011 0011 11 xxx xxx */
1774 OpBuffer[obp++]=(char)0x48;
1775 OpBuffer[obp++]=(char)0x33;
1776 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg));
1777 }
1778 if(REG_R8<=reg&&reg<=REG_R15){
1779 /* r8~r15
1780 0100 1101 0011 0011 11 xxx xxx */
1781 OpBuffer[obp++]=(char)0x4D;
1782 OpBuffer[obp++]=(char)0x33;
1783 OpBuffer[obp++]=(char)(0xC0| REGISTER_OPERAND(reg)<<3 | REGISTER_OPERAND(reg));
1784 }
1785}
1786
1787
1788
1789/////////////////////////////
1790// 関数呼び出し
1791/////////////////////////////
1792
1793void op_call( UserProc *pUserProc ){
1794 OpBuffer[obp++] = (char)0xE8;
1795 pobj_SubAddrSchedule->add( pUserProc, 1 );
1796 pUserProc->Using();
1797 obp += sizeof(long);
1798}
1799void op_call( DllProc *pDllProc ){
1800 OpBuffer[obp++] = (char)0xFF;
1801 OpBuffer[obp++] = (char)0x15;
1802 pobj_ImportAddrSchedule->add(pDllProc);
1803 obp += sizeof(long);
1804
1805 pDllProc->Using();
1806}
1807void op_ret(){
1808 OpBuffer[obp++]=(char)0xC3;
1809}
Note: See TracBrowser for help on using the repository browser.