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

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