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