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

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