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

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