asm_code.c (89248B)
1 #include "../../../config.h" 2 3 #include "arch.h" 4 5 #include <assert.h> 6 #include <stdlib.h> /* exit */ 7 #include <string.h> /* memcpy */ 8 9 #include "inst.h" 10 #include "parse_asm.h" 11 #include "tcutil.h" 12 13 #ifndef PUT_CODE 14 #define PUT_CODE(p, ...) \ 15 do { \ 16 unsigned char buf[] = {__VA_ARGS__}; \ 17 memcpy(p, buf, sizeof(buf)); \ 18 } while (0) 19 #endif 20 21 static unsigned char * 22 put_code_filtered(unsigned char *p, const short *buf, size_t count) 23 { 24 for (size_t i = 0; i < count; ++i) { 25 short c = *buf++; 26 if (c >= 0) 27 *p++ = c; 28 } 29 return p; 30 } 31 32 void 33 make_code(struct Inst *inst, struct Code *code, unsigned char *buf, int len) 34 { 35 assert(code->len + len <= (int)sizeof(code->buf)); 36 code->inst = inst; 37 memcpy(code->buf + code->len, buf, len); 38 code->len += len; 39 } 40 41 static inline char 42 opr_regno(const struct Reg *reg) 43 { 44 return reg->no | (reg->x << 3); 45 } 46 47 static inline int 48 opr_reg8(const struct Reg *reg) 49 { 50 assert(reg->size == REG8); 51 return opr_regno(reg) < 4; 52 } 53 54 static inline int 55 assemble_error(struct ParseInfo *info, const char *message) 56 { 57 parse_error(info, message); 58 return 0; 59 } 60 61 static unsigned char * 62 put_rex0(unsigned char *p, enum RegSize size, int sno, int dno, unsigned char opcode) 63 { 64 if (size == REG16) 65 *p++ = 0x66; 66 if (sno >= 8 || dno >= 8 || size == REG64) 67 *p++ = 0x40 | ((dno & 8) >> 3) | ((sno & 8) >> 1) | (size != REG64 ? 0 : 8); 68 *p++ = opcode; 69 return p; 70 } 71 72 #define MAKE_REX0(size, sno, dno, opcode) \ 73 (size) == REG16 ? 0x66 : -1, \ 74 ((sno) >= 8 || (dno) >= 8 || ((size) == REG8 && ((sno) >= 4 || (dno) >= 4)) \ 75 || (size) == REG64) \ 76 ? (0x40 | (((dno) & 8) >> 3) | (((sno) & 8) >> 1) | ((size) != REG64 ? 0 : 8)) \ 77 : -1, \ 78 opcode 79 80 static unsigned char * 81 put_rex1(unsigned char *p, enum RegSize size, int rex_prefix, int dno, unsigned char opcode) 82 { 83 p = put_rex0(p, size, 0, dno, opcode); 84 *p++ = rex_prefix | (dno & 7); 85 return p; 86 } 87 88 static unsigned char * 89 put_rex2(unsigned char *p, enum RegSize size, int sno, int dno, unsigned char opcode) 90 { 91 p = put_rex0(p, size, sno, dno, opcode); 92 *p++ = 0xc0 | ((sno & 7) << 3) | (dno & 7); 93 return p; 94 } 95 96 #define MAKE_REX_INDIRECT(size, reg, indirect_reg, opcode, op2, offset) \ 97 MAKE_REX0(size, reg, indirect_reg, (size) == REG8 ? opcode : (unsigned char)((opcode) + 1)), \ 98 (((offset) == 0 && indirect_reg != RBP - RAX) ? op2 \ 99 : is_im8(offset) ? 0x40 \ 100 : 0x80) \ 101 | (indirect_reg & 7) | ((reg & 7) << 3), \ 102 indirect_reg == RSP - RAX ? 0x24 : -1 103 104 #define MAKE_OFFSET(offset, no) \ 105 ((offset) == 0 && no) ? -1 : (offset) & 0xff, \ 106 ((offset) == 0 && no) || is_im8(offset) ? -1 : ((offset) >> 8) & 0xff, \ 107 ((offset) == 0 && no) || is_im8(offset) ? -1 : ((offset) >> 16) & 0xff, \ 108 ((offset) == 0 && no) || is_im8(offset) ? -1 : ((offset) >> 24) & 0xff 109 110 #define MAKE_IM(size, value) \ 111 (value) & 0xff, size < REG16 ? -1 : ((value) >> 8) & 0xff, \ 112 size < REG32 ? -1 : ((value) >> 16) & 0xff, size < REG32 ? -1 : ((value) >> 24) & 0xff, \ 113 size < REG64 ? -1 : ((value) >> 32) & 0xff, size < REG64 ? -1 : ((value) >> 40) & 0xff, \ 114 size < REG64 ? -1 : ((value) >> 48) & 0xff, size < REG64 ? -1 : ((value) >> 56) & 0xff 115 116 static unsigned char * 117 asm_noop(struct Inst *inst, struct Code *code) 118 { 119 UNUSED(inst); 120 unsigned char *p = code->buf; 121 return p; 122 } 123 124 static unsigned char * 125 asm_mov_rr(struct Inst *inst, struct Code *code) 126 { 127 unsigned char *p = code->buf; 128 enum RegSize size = inst->opr[0].u.reg.size; 129 p = put_rex2( 130 p, 131 size, 132 opr_regno(&inst->opr[0].u.reg), 133 opr_regno(&inst->opr[1].u.reg), 134 size == REG8 ? 0x88 : 0x89 135 ); 136 return p; 137 } 138 139 static unsigned char * 140 asm_mov_imr(struct Inst *inst, struct Code *code) 141 { 142 unsigned char *p = code->buf; 143 enum RegSize size = inst->opr[1].u.reg.size; 144 if (size == REG64 && is_im32(inst->opr[0].u.immediate)) { 145 int d = inst->opr[1].u.reg.no; 146 int pre = !inst->opr[1].u.reg.x ? 0x48 : 0x49; 147 MAKE_CODE(inst, code, pre, 0xc7, 0xc0 | d, IM32(inst->opr[0].u.immediate)); 148 return code->buf; 149 } 150 151 short buf[] = { 152 MAKE_REX0( 153 size, 154 0, 155 opr_regno(&inst->opr[1].u.reg), 156 0xb0 | (size == REG8 ? 0 : 8) | inst->opr[1].u.reg.no 157 ), 158 MAKE_IM(size, inst->opr[0].u.immediate), 159 }; 160 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 161 return p; 162 } 163 164 static unsigned char * 165 asm_mov_ir(struct Inst *inst, struct Code *code) 166 { 167 if (inst->opr[0].u.indirect.reg.no != RIP) { 168 if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) { 169 long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; 170 enum RegSize size = inst->opr[1].u.reg.size; 171 int sno = opr_regno(&inst->opr[0].u.indirect.reg); 172 int dno = opr_regno(&inst->opr[1].u.reg); 173 int ofszero = offset == 0 && ((sno & 7) != RBP - RAX); 174 short buf[] = { 175 size == REG16 ? 0x66 : -1, 176 (size == REG64 || sno >= 8 || dno >= 8) 177 ? 0x40 | (size == REG64 ? 0x08 : 0) | ((dno & 8) >> 1) | ((sno & 8) >> 3) 178 : -1, 179 0x8a | (size == REG8 ? 0 : 0x01), 180 ((dno & 7) << 3) | (sno & 7) 181 | (ofszero ? 0 182 : is_im8(offset) ? 0x40 183 : 0x80), 184 (sno & 7) == RSP - RAX ? 0x24 : -1, 185 }; 186 unsigned char *p = code->buf; 187 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 188 189 if (ofszero) { 190 /* nothing */ 191 } else if (is_im8(offset)) { 192 *p++ = offset; 193 } else { 194 PUT_CODE(p, IM32(offset)); 195 p += 4; 196 } 197 return p; 198 } 199 } else { 200 if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM) { 201 int pre = !inst->opr[1].u.reg.x ? 0x48 : 0x4c; 202 int d = inst->opr[1].u.reg.no; 203 MAKE_CODE(inst, code, pre, 0x8b, 0x05 | (d << 3), IM32(0)); 204 return code->buf; 205 } 206 } 207 return NULL; 208 } 209 210 /* reg -> (mem), rex prefix + modrm + sib + disp: shared shape behind 211 * mov, xchg, xadd, and cmpxchg storing a register into memory. only 212 * the opcode bytes differ between them, xadd/cmpxchg add a 0f escape 213 * byte ahead of theirs */ 214 static unsigned char * 215 assemble_reg_to_mem( 216 struct Inst *inst, struct Code *code, int escape, unsigned char op8, unsigned char opw 217 ) 218 { 219 if (inst->opr[1].u.indirect.offset.expr->kind != EX_FIXNUM 220 || inst->opr[1].u.indirect.reg.no == RIP) 221 return NULL; 222 223 long offset = inst->opr[1].u.indirect.offset.expr->u.fixnum; 224 enum RegSize size = inst->opr[0].u.reg.size; 225 int sno = opr_regno(&inst->opr[0].u.reg); 226 int dno = opr_regno(&inst->opr[1].u.indirect.reg); 227 int ofszero = offset == 0 && ((dno & 7) != RBP - RAX); 228 short buf[] = { 229 size == REG16 ? 0x66 : -1, 230 (size == REG64 || sno >= 8 || dno >= 8) 231 ? 0x40 | (size == REG64 ? 0x08 : 0) | ((sno & 8) >> 1) | ((dno & 8) >> 3) 232 : -1, 233 escape ? 0x0f : -1, 234 size == REG8 ? op8 : opw, 235 ((sno & 7) << 3) | (dno & 7) 236 | (ofszero ? 0 237 : is_im8(offset) ? 0x40 238 : 0x80), 239 (dno & 7) == RSP - RAX ? 0x24 : -1, 240 }; 241 unsigned char *p = code->buf; 242 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 243 244 if (ofszero) { 245 /* nothing */ 246 } else if (is_im8(offset)) { 247 *p++ = offset; 248 } else { 249 PUT_CODE(p, IM32(offset)); 250 p += 4; 251 } 252 return p; 253 } 254 255 static unsigned char * 256 asm_mov_ri(struct Inst *inst, struct Code *code) 257 { 258 return assemble_reg_to_mem(inst, code, 0, 0x88, 0x89); 259 } 260 261 static unsigned char * 262 asm_mov_iir(struct Inst *inst, struct Code *code) 263 { 264 if (inst->opr[0].u.indirect_with_index.offset->kind == EX_FIXNUM) { 265 long offset = inst->opr[0].u.indirect_with_index.offset->u.fixnum; 266 assert(is_im32(offset)); 267 short offset_bit = offset == 0 ? 0x04 : is_im8(offset) ? 0x44 : 0x84; 268 enum RegSize size = inst->opr[1].u.reg.size; 269 assert(inst->opr[0].u.indirect_with_index.base_reg.no != RIP); 270 int bno = opr_regno(&inst->opr[0].u.indirect_with_index.base_reg); 271 int ino = opr_regno(&inst->opr[0].u.indirect_with_index.index_reg); 272 int dno = opr_regno(&inst->opr[1].u.reg); 273 struct Expr *scale_expr = inst->opr[0].u.indirect_with_index.scale; 274 char scale_bit = 0; 275 if (scale_expr != NULL) { 276 int s = scale_expr->u.fixnum; 277 if (s <= 0 || s > 8 || !IS_POWER_OF_2(s)) 278 return NULL; 279 scale_bit = most_significant_bit(s); 280 } 281 short x = ((bno & 8) >> 3) | ((ino & 8) >> 2) | ((dno & 8) >> 1); 282 short prefix = size == REG16 ? 0x66 : size == REG64 || x != 0 ? 0x48 | x : -1; 283 short buf[] = { 284 prefix, 285 size == REG8 ? 0x8a : 0x8b, 286 ((dno & 7) << 3) | offset_bit, 287 (scale_bit << 6) | ((ino & 7) << 3) | (bno & 7), 288 }; 289 unsigned char *p = code->buf; 290 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 291 292 if (offset != 0) { 293 if (is_im8(offset)) { 294 *p++ = IM8(offset); 295 } else { 296 PUT_CODE(p, IM32(offset)); 297 p += 4; 298 } 299 } 300 return p; 301 } 302 return NULL; 303 } 304 305 static unsigned char * 306 assemble_direct_mov(struct Inst *inst, struct Code *code, int load) 307 { 308 struct Reg *reg = load ? &inst->opr[1].u.reg : &inst->opr[0].u.reg; 309 enum RegSize size = reg->size; 310 int dno = opr_regno(reg); 311 unsigned char opcode = load ? 0x8b : 0x89; 312 unsigned char *p = code->buf; 313 314 switch (size) { 315 case REG8: 316 opcode = load ? 0x8a : 0x88; 317 break; 318 case REG16: 319 case REG32: 320 case REG64: 321 break; 322 default: 323 return NULL; 324 } 325 326 if (size == REG16) 327 *p++ = 0x66; 328 if (size == REG64 || dno >= 8) 329 *p++ = 0x40 | (size == REG64 ? 0x08 : 0x00) | ((dno & 8) >> 1); 330 *p++ = opcode; 331 *p++ = (dno & 7) << 3 | 0x04; 332 *p++ = 0x25; 333 PUT_CODE(p, IM32(0)); 334 p += 4; 335 return p; 336 } 337 338 static unsigned char * 339 asm_mov_dr(struct Inst *inst, struct Code *code) 340 { 341 return assemble_direct_mov(inst, code, 1); 342 } 343 344 static unsigned char * 345 asm_mov_rd(struct Inst *inst, struct Code *code) 346 { 347 return assemble_direct_mov(inst, code, 0); 348 } 349 350 static unsigned char * 351 asm_mov_sr(struct Inst *inst, struct Code *code) 352 { 353 enum RegSize dsize = inst->opr[1].u.reg.size; 354 if (inst->opr[0].u.segment.reg != FS || (dsize != REG32 && dsize != REG64)) 355 return NULL; 356 357 struct Expr *offset = inst->opr[0].u.segment.offset.expr; 358 int tpoff = inst->opr[0].u.segment.offset.flag & LF_TPOFF; 359 if (offset != NULL && offset->kind != EX_FIXNUM && !tpoff) 360 return NULL; 361 362 /* tpoff's value is a link-time tls-layout quantity the assembler 363 cannot compute, 0 here is a placeholder patched in by the 364 relocation x64_resolve_code_reloc registers below */ 365 uint64_t value = (offset != NULL && offset->kind == EX_FIXNUM) ? offset->u.fixnum : 0; 366 unsigned char dno = opr_regno(&inst->opr[1].u.reg); 367 short buf[] = { 368 0x64, 369 dsize == REG64 ? 0x48 : -1, 370 0x8b, 371 (short)(((dno & 7) << 3) | 0x04), 372 0x25, 373 }; 374 unsigned char *p = code->buf; 375 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 376 PUT_CODE(p, IM32(value)); 377 p += 4; 378 return p; 379 } 380 381 static unsigned char * 382 asm_movbwlq_imi(struct Inst *inst, struct Code *code) 383 { 384 long offset = inst->opr[1].u.indirect.offset.expr->u.fixnum; 385 unsigned char sno = 0; 386 unsigned char dno = opr_regno(&inst->opr[1].u.indirect.reg); 387 unsigned char op = (offset == 0 && (dno & 7) != RBP - RAX) ? 0x00 388 : is_im8(offset) ? (unsigned char)0x40 389 : (unsigned char)0x80; 390 short buf[] = { 391 inst->op == MOVW_IMI ? 0x66 : -1, 392 (inst->op == MOVQ_IMI || dno >= 8) 393 ? 0x40 | (inst->op == MOVQ_IMI ? 0x08 : 0) | ((dno & 8) >> 3) | ((sno & 8) >> 1) 394 : -1, 395 0xc6 | (inst->op == MOVB_IMI ? 0 : 1), 396 op | (dno & 7) | ((sno & 7) << 3), 397 (dno & 7) == RSP - RAX ? 0x24 : -1, 398 }; 399 400 unsigned char *p = code->buf; 401 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 402 if (offset == 0 && (dno & 7) != RBP - RAX) { 403 ; 404 } else if (is_im8(offset)) { 405 *p++ = IM8(offset); 406 } else if (is_im32(offset)) { 407 PUT_CODE(p, IM32(offset)); 408 p += 4; 409 } 410 411 long value = inst->opr[0].u.immediate; 412 switch (inst->op) { 413 case MOVB_IMI: 414 *p++ = IM8(value); 415 break; 416 case MOVW_IMI: 417 PUT_CODE(p, IM16(value)); 418 p += 2; 419 break; 420 case MOVL_IMI: 421 case MOVQ_IMI: 422 PUT_CODE(p, IM32(value)); 423 p += 4; 424 break; 425 default: 426 assert(0); 427 break; 428 } 429 return p; 430 } 431 432 static unsigned char * 433 asm_movbwlq_imd(struct Inst *inst, struct Code *code) 434 { 435 assert(inst->opr[1].u.direct.expr->kind == EX_FIXNUM); 436 int64_t dst = inst->opr[1].u.direct.expr->u.fixnum; 437 assert(is_im32(dst)); 438 439 short buf[] = { 440 inst->op == MOVW_IMD ? 0x66 : -1, 441 inst->op == MOVQ_IMD ? 0x48 : -1, 442 0xc6 | (inst->op == MOVB_IMD ? 0 : 1), 443 0x04, 444 0x25, 445 }; 446 447 unsigned char *p = code->buf; 448 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 449 PUT_CODE(p, IM32(dst)); 450 p += 4; 451 452 long value = inst->opr[0].u.immediate; 453 switch (inst->op) { 454 case MOVB_IMD: 455 *p++ = IM8(value); 456 break; 457 case MOVW_IMD: 458 PUT_CODE(p, IM16(value)); 459 p += 2; 460 break; 461 case MOVL_IMD: 462 case MOVQ_IMD: 463 PUT_CODE(p, IM32(value)); 464 p += 4; 465 break; 466 default: 467 assert(0); 468 break; 469 } 470 return p; 471 } 472 473 static unsigned char * 474 asm_movszx_rr(struct Inst *inst, struct Code *code) 475 { 476 int s = inst->opr[0].u.reg.no; 477 int d = inst->opr[1].u.reg.no; 478 unsigned char op = inst->op == MOVZX ? 0xb6 : 0xbe; 479 unsigned char *p = code->buf; 480 switch (inst->opr[0].u.reg.size) { 481 case REG8: 482 switch (inst->opr[1].u.reg.size) { 483 case REG16: 484 if (opr_reg8(&inst->opr[0].u.reg) && !inst->opr[1].u.reg.x) { 485 MAKE_CODE(inst, code, 0x66, 0x0f, op, 0xc0 + s + d * 8); 486 } else { 487 int pre = 488 ((inst->opr[0].u.reg.x & 1) == 0 ? 0x40 : 0x41) + (!inst->opr[1].u.reg.x ? 0 : 4); 489 MAKE_CODE(inst, code, 0x66, pre, 0x0f, op, 0xc0 + s + d * 8); 490 } 491 return p; 492 case REG32: 493 if (opr_reg8(&inst->opr[0].u.reg) && !inst->opr[1].u.reg.x) { 494 MAKE_CODE(inst, code, 0x0f, op, 0xc0 + s + d * 8); 495 } else { 496 int pre = 497 ((inst->opr[0].u.reg.x & 1) == 0 ? 0x40 : 0x41) + (!inst->opr[1].u.reg.x ? 0 : 4); 498 MAKE_CODE(inst, code, pre, 0x0f, op, 0xc0 + s + d * 8); 499 } 500 return p; 501 case REG64: { 502 int pre = 503 ((inst->opr[0].u.reg.x & 1) == 0 ? 0x48 : 0x49) + (!inst->opr[1].u.reg.x ? 0 : 4); 504 MAKE_CODE(inst, code, pre, 0x0f, op, 0xc0 + s + d * 8); 505 return p; 506 } 507 default: 508 break; 509 } 510 break; 511 case REG16: 512 switch (inst->opr[1].u.reg.size) { 513 case REG32: 514 if (!inst->opr[0].u.reg.x && !inst->opr[1].u.reg.x) { 515 MAKE_CODE(inst, code, 0x0f, op | 1, 0xc0 + s + d * 8); 516 } else { 517 int pre = (!inst->opr[0].u.reg.x ? 0x40 : 0x41) + (!inst->opr[1].u.reg.x ? 0 : 4); 518 MAKE_CODE(inst, code, pre, 0x0f, op | 1, 0xc0 + s + d * 8); 519 } 520 return p; 521 case REG64: { 522 int pre = (!inst->opr[0].u.reg.x ? 0x48 : 0x49) + (!inst->opr[1].u.reg.x ? 0 : 4); 523 MAKE_CODE(inst, code, pre, 0x0f, op | 1, 0xc0 + s + d * 8); 524 return p; 525 } 526 default: 527 break; 528 } 529 break; 530 case REG32: 531 /* "MOVZX %32bit, %64bit" doesnt exist! */ 532 if (inst->opr[1].u.reg.size == REG64 && inst->op == MOVSX) { 533 int pre = (!inst->opr[0].u.reg.x ? 0x48 : 0x49) + (!inst->opr[1].u.reg.x ? 0 : 4); 534 MAKE_CODE(inst, code, pre, 0x63, 0xc0 + s + d * 8); 535 return p; 536 } 537 break; 538 default: 539 break; 540 } 541 return NULL; 542 } 543 544 static unsigned char * 545 asm_movsds_xx(struct Inst *inst, struct Code *code, int single) 546 { 547 unsigned char prefix = single ? 0xf3 : 0xf2; 548 unsigned char sno = inst->opr[0].u.regxmm - XMM0; 549 unsigned char dno = inst->opr[1].u.regxmm - XMM0; 550 short buf[] = { 551 prefix, 552 sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, 553 0x0f, 554 0x10, 555 (unsigned char)0xc0 | ((dno & 7) << 3) | (sno & 7), 556 }; 557 unsigned char *p = code->buf; 558 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 559 return p; 560 } 561 static unsigned char * 562 asm_movsd_xx(struct Inst *inst, struct Code *code) 563 { 564 return asm_movsds_xx(inst, code, 0); 565 } 566 static unsigned char * 567 asm_movss_xx(struct Inst *inst, struct Code *code) 568 { 569 return asm_movsds_xx(inst, code, 1); 570 } 571 572 static unsigned char * 573 asm_movsds_ix(struct Inst *inst, struct Code *code, int single) 574 { 575 long offset; 576 unsigned char prefix = single ? 0xf3 : 0xf2; 577 unsigned char dno = inst->opr[1].u.regxmm - XMM0; 578 int d = dno & 7; 579 unsigned char *p = code->buf; 580 581 if (inst->opr[0].u.indirect.reg.no != RIP) { 582 if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM 583 || !(offset = inst->opr[0].u.indirect.offset.expr->u.fixnum, is_im32(offset))) 584 return NULL; 585 { 586 unsigned char sno = opr_regno(&inst->opr[0].u.indirect.reg); 587 int s = sno & 7; 588 unsigned char op = (offset == 0 && s != RBP - RAX) ? (unsigned char)0x00 589 : is_im8(offset) ? (unsigned char)0x40 590 : (unsigned char)0x80; 591 592 short buf[] = { 593 prefix, 594 sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, 595 0x0f, 596 0x10, 597 op | s | (d << 3), 598 s == RSP - RAX ? 0x24 : -1, 599 }; 600 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 601 602 if (offset == 0 && s != RBP - RAX) { 603 ; 604 } else if (is_im8(offset)) { 605 *p++ = IM8(offset); 606 } else { /* is_im32(offset) */ 607 PUT_CODE(p, IM32(offset)); 608 p += 4; 609 } 610 return p; 611 } 612 } 613 614 /* rip-relative with a symbolic offset: emit a placeholder disp32, 615 the same convention asm_lea_ir uses, resolved to a pc32 616 relocation by the ir pass */ 617 if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) 618 return NULL; 619 { 620 short buf[] = { 621 prefix, 622 dno >= 8 ? (unsigned char)0x44 : -1, 623 0x0f, 624 0x10, 625 0x05 | (d << 3), 626 }; 627 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 628 PUT_CODE(p, IM32(0)); 629 p += 4; 630 return p; 631 } 632 } 633 static unsigned char * 634 asm_movsd_ix(struct Inst *inst, struct Code *code) 635 { 636 return asm_movsds_ix(inst, code, 0); 637 } 638 static unsigned char * 639 asm_movss_ix(struct Inst *inst, struct Code *code) 640 { 641 return asm_movsds_ix(inst, code, 1); 642 } 643 644 static unsigned char * 645 asm_movsds_xi(struct Inst *inst, struct Code *code, int single) 646 { 647 long offset; 648 if (inst->opr[1].u.indirect.offset.expr->kind == EX_FIXNUM 649 && (offset = inst->opr[1].u.indirect.offset.expr->u.fixnum, is_im32(offset))) { 650 if (inst->opr[1].u.indirect.reg.no != RIP) { 651 unsigned char prefix = single ? 0xf3 : 0xf2; 652 unsigned char sno = inst->opr[0].u.regxmm - XMM0; 653 unsigned char dno = opr_regno(&inst->opr[1].u.indirect.reg); 654 int d = dno & 7; 655 int s = sno & 7; 656 unsigned char op = (offset == 0 && d != RBP - RAX) ? (unsigned char)0x00 657 : is_im8(offset) ? (unsigned char)0x40 658 : (unsigned char)0x80; 659 660 short buf[] = { 661 prefix, 662 sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((dno & 8) >> 3) | ((sno & 8) >> 1) : -1, 663 0x0f, 664 0x11, 665 op | d | (s << 3), 666 d == RSP - RAX ? 0x24 : -1, 667 }; 668 unsigned char *p = code->buf; 669 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 670 671 if (offset == 0 && d != RBP - RAX) { 672 ; 673 } else if (is_im8(offset)) { 674 *p++ = IM8(offset); 675 } else { /* is_im32(offset) */ 676 PUT_CODE(p, IM32(offset)); 677 p += 4; 678 } 679 return p; 680 } 681 } 682 return NULL; 683 } 684 static unsigned char * 685 asm_movsd_xi(struct Inst *inst, struct Code *code) 686 { 687 return asm_movsds_xi(inst, code, 0); 688 } 689 static unsigned char * 690 asm_movss_xi(struct Inst *inst, struct Code *code) 691 { 692 return asm_movsds_xi(inst, code, 1); 693 } 694 695 static unsigned char * 696 assemble_bop_sd(struct Inst *inst, struct Code *code, int single, unsigned char op) 697 { 698 unsigned char *p = code->buf; 699 unsigned char prefix = single ? 0xf3 : 0xf2; 700 if (inst->opr[0].type == REG_XMM && inst->opr[1].type == REG_XMM) { 701 unsigned char sno = inst->opr[0].u.regxmm - XMM0; 702 unsigned char dno = inst->opr[1].u.regxmm - XMM0; 703 short buf[] = { 704 prefix, 705 sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, 706 0x0f, 707 op, 708 (unsigned char)0xc0 | ((dno & 7) << 3) | (sno & 7), 709 }; 710 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 711 } 712 713 return p; 714 } 715 716 static unsigned char * 717 asm_addsd_xx(struct Inst *inst, struct Code *code) 718 { 719 return assemble_bop_sd(inst, code, 0, 0x58); 720 } 721 static unsigned char * 722 asm_addss_xx(struct Inst *inst, struct Code *code) 723 { 724 return assemble_bop_sd(inst, code, 1, 0x58); 725 } 726 727 static unsigned char * 728 asm_subsd_xx(struct Inst *inst, struct Code *code) 729 { 730 return assemble_bop_sd(inst, code, 0, 0x5c); 731 } 732 static unsigned char * 733 asm_subss_xx(struct Inst *inst, struct Code *code) 734 { 735 return assemble_bop_sd(inst, code, 1, 0x5c); 736 } 737 738 static unsigned char * 739 asm_mulsd_xx(struct Inst *inst, struct Code *code) 740 { 741 return assemble_bop_sd(inst, code, 0, 0x59); 742 } 743 static unsigned char * 744 asm_mulss_xx(struct Inst *inst, struct Code *code) 745 { 746 return assemble_bop_sd(inst, code, 1, 0x59); 747 } 748 749 static unsigned char * 750 asm_divsd_xx(struct Inst *inst, struct Code *code) 751 { 752 return assemble_bop_sd(inst, code, 0, 0x5e); 753 } 754 static unsigned char * 755 asm_divss_xx(struct Inst *inst, struct Code *code) 756 { 757 return assemble_bop_sd(inst, code, 1, 0x5e); 758 } 759 760 static unsigned char * 761 assemble_xorpd_xx(struct Inst *inst, struct Code *code, int single) 762 { 763 unsigned char *p = code->buf; 764 assert(inst->opr[0].type == REG_XMM); 765 assert(inst->opr[1].type == REG_XMM); 766 unsigned char sno = inst->opr[0].u.regxmm - XMM0; 767 unsigned char dno = inst->opr[1].u.regxmm - XMM0; 768 short buf[] = { 769 single ? -1 : 0x66, 770 sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, 771 0x0f, 772 0x57, 773 (unsigned char)0xc0 | ((dno & 7) << 3) | (sno & 7), 774 }; 775 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 776 return p; 777 } 778 static unsigned char * 779 asm_xorpd_xx(struct Inst *inst, struct Code *code) 780 { 781 return assemble_xorpd_xx(inst, code, 0); 782 } 783 static unsigned char * 784 asm_xorps_xx(struct Inst *inst, struct Code *code) 785 { 786 return assemble_xorpd_xx(inst, code, 1); 787 } 788 789 static unsigned char * 790 assemble_xorpd_ix(struct Inst *inst, struct Code *code, int single) 791 { 792 long offset; 793 unsigned char dno = inst->opr[1].u.regxmm - XMM0; 794 int d = dno & 7; 795 unsigned char *p = code->buf; 796 797 if (inst->opr[0].u.indirect.reg.no != RIP) { 798 unsigned char sno; 799 int s; 800 unsigned char op; 801 if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM 802 || !(offset = inst->opr[0].u.indirect.offset.expr->u.fixnum, is_im32(offset))) 803 return NULL; 804 sno = opr_regno(&inst->opr[0].u.indirect.reg); 805 s = sno & 7; 806 op = (offset == 0 && s != RBP - RAX) ? (unsigned char)0x00 807 : is_im8(offset) ? (unsigned char)0x40 808 : (unsigned char)0x80; 809 { 810 short buf[] = { 811 single ? -1 : 0x66, 812 sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, 813 0x0f, 814 0x57, 815 op | s | (d << 3), 816 s == RSP - RAX ? 0x24 : -1, 817 }; 818 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 819 } 820 if (offset == 0 && s != RBP - RAX) { 821 ; 822 } else if (is_im8(offset)) { 823 *p++ = IM8(offset); 824 } else { /* is_im32(offset) */ 825 PUT_CODE(p, IM32(offset)); 826 p += 4; 827 } 828 return p; 829 } 830 831 /* rip-relative with a symbolic offset, same convention as 832 asm_movsds_ix: placeholder disp32, resolved by the ir pass */ 833 if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) 834 return NULL; 835 { 836 short buf[] = { 837 single ? -1 : 0x66, 838 dno >= 8 ? (unsigned char)0x44 : -1, 839 0x0f, 840 0x57, 841 0x05 | (d << 3), 842 }; 843 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 844 PUT_CODE(p, IM32(0)); 845 p += 4; 846 return p; 847 } 848 } 849 static unsigned char * 850 asm_xorpd_ix(struct Inst *inst, struct Code *code) 851 { 852 return assemble_xorpd_ix(inst, code, 0); 853 } 854 static unsigned char * 855 asm_xorps_ix(struct Inst *inst, struct Code *code) 856 { 857 return assemble_xorpd_ix(inst, code, 1); 858 } 859 860 static unsigned char * 861 assemble_ucomisd(struct Inst *inst, struct Code *code, unsigned char opc, int single) 862 { 863 unsigned char *p = code->buf; 864 assert(inst->opr[0].type == REG_XMM); 865 assert(inst->opr[1].type == REG_XMM); 866 unsigned char sno = inst->opr[0].u.regxmm - XMM0; 867 unsigned char dno = inst->opr[1].u.regxmm - XMM0; 868 short buf[] = { 869 single ? -1 : 0x66, 870 sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, 871 0x0f, 872 opc, 873 (unsigned char)0xc0 | ((dno & 7) << 3) | (sno & 7), 874 }; 875 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 876 return p; 877 } 878 static unsigned char * 879 asm_comisd_xx(struct Inst *inst, struct Code *code) 880 { 881 return assemble_ucomisd(inst, code, 0x2f, 0); 882 } 883 static unsigned char * 884 asm_comiss_xx(struct Inst *inst, struct Code *code) 885 { 886 return assemble_ucomisd(inst, code, 0x2f, 1); 887 } 888 static unsigned char * 889 asm_ucomisd_xx(struct Inst *inst, struct Code *code) 890 { 891 return assemble_ucomisd(inst, code, 0x2e, 0); 892 } 893 static unsigned char * 894 asm_ucomiss_xx(struct Inst *inst, struct Code *code) 895 { 896 return assemble_ucomisd(inst, code, 0x2e, 1); 897 } 898 899 /* comisd/ucomisd reg, (mem): same shape as assemble_xorpd_ix with the 900 * opcode byte as a parameter instead of fixed at 0x57. qbe emits it to 901 * compare a register against a float literal in a rip-relative 902 * constant pool */ 903 static unsigned char * 904 assemble_ucomisd_ix(struct Inst *inst, struct Code *code, unsigned char opc, int single) 905 { 906 long offset; 907 unsigned char dno = inst->opr[1].u.regxmm - XMM0; 908 int d = dno & 7; 909 unsigned char *p = code->buf; 910 911 if (inst->opr[0].u.indirect.reg.no != RIP) { 912 unsigned char sno; 913 int s; 914 unsigned char op; 915 if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM 916 || !(offset = inst->opr[0].u.indirect.offset.expr->u.fixnum, is_im32(offset))) 917 return NULL; 918 sno = opr_regno(&inst->opr[0].u.indirect.reg); 919 s = sno & 7; 920 op = (offset == 0 && s != RBP - RAX) ? (unsigned char)0x00 921 : is_im8(offset) ? (unsigned char)0x40 922 : (unsigned char)0x80; 923 { 924 short buf[] = { 925 single ? -1 : 0x66, 926 sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, 927 0x0f, 928 opc, 929 op | s | (d << 3), 930 s == RSP - RAX ? 0x24 : -1, 931 }; 932 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 933 } 934 if (offset == 0 && s != RBP - RAX) { 935 ; 936 } else if (is_im8(offset)) { 937 *p++ = IM8(offset); 938 } else { 939 PUT_CODE(p, IM32(offset)); 940 p += 4; 941 } 942 return p; 943 } 944 945 /* rip-relative with a symbolic offset, same convention as 946 asm_movsds_ix/assemble_xorpd_ix: placeholder disp32, resolved by 947 the ir pass */ 948 if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) 949 return NULL; 950 { 951 short buf[] = { 952 single ? -1 : 0x66, 953 dno >= 8 ? (unsigned char)0x44 : -1, 954 0x0f, 955 opc, 956 0x05 | (d << 3), 957 }; 958 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 959 PUT_CODE(p, IM32(0)); 960 p += 4; 961 return p; 962 } 963 } 964 static unsigned char * 965 asm_comisd_ix(struct Inst *inst, struct Code *code) 966 { 967 return assemble_ucomisd_ix(inst, code, 0x2f, 0); 968 } 969 static unsigned char * 970 asm_comiss_ix(struct Inst *inst, struct Code *code) 971 { 972 return assemble_ucomisd_ix(inst, code, 0x2f, 1); 973 } 974 static unsigned char * 975 asm_ucomisd_ix(struct Inst *inst, struct Code *code) 976 { 977 return assemble_ucomisd_ix(inst, code, 0x2e, 0); 978 } 979 static unsigned char * 980 asm_ucomiss_ix(struct Inst *inst, struct Code *code) 981 { 982 return assemble_ucomisd_ix(inst, code, 0x2e, 1); 983 } 984 985 static unsigned char * 986 assemble_cvtsi2sd(struct Inst *inst, struct Code *code, int single) 987 { 988 unsigned char *p = code->buf; 989 unsigned char prefix = single ? 0xf3 : 0xf2; 990 unsigned char sno = opr_regno(&inst->opr[0].u.reg); 991 unsigned char dno = inst->opr[1].u.regxmm - XMM0; 992 short buf[] = { 993 prefix, 994 sno >= 8 || dno >= 8 || inst->opr[0].u.reg.size == REG64 995 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) 996 | (inst->opr[0].u.reg.size == REG64 ? 8 : 0) 997 : -1, 998 0x0f, 999 0x2a, 1000 (unsigned char)0xc0 | ((dno & 7) << 3) | (sno & 7), 1001 }; 1002 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1003 return p; 1004 } 1005 static unsigned char * 1006 asm_cvtsi2sd_rx(struct Inst *inst, struct Code *code) 1007 { 1008 return assemble_cvtsi2sd(inst, code, 0); 1009 } 1010 static unsigned char * 1011 asm_cvtsi2ss_rx(struct Inst *inst, struct Code *code) 1012 { 1013 return assemble_cvtsi2sd(inst, code, 1); 1014 } 1015 1016 static unsigned char * 1017 assemble_cvttsd2si(struct Inst *inst, struct Code *code, int single) 1018 { 1019 unsigned char *p = code->buf; 1020 unsigned char prefix = single ? 0xf3 : 0xf2; 1021 unsigned char sno = inst->opr[0].u.regxmm - XMM0; 1022 unsigned char dno = opr_regno(&inst->opr[1].u.reg); 1023 short buf[] = { 1024 prefix, 1025 sno >= 8 || dno >= 8 || inst->opr[1].u.reg.size == REG64 1026 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) 1027 | (inst->opr[1].u.reg.size == REG64 ? 8 : 0) 1028 : -1, 1029 0x0f, 1030 0x2c, 1031 (unsigned char)0xc0 | ((dno & 7) << 3) | (sno & 7), 1032 }; 1033 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1034 return p; 1035 } 1036 static unsigned char * 1037 asm_cvttsd2si_xr(struct Inst *inst, struct Code *code) 1038 { 1039 return assemble_cvttsd2si(inst, code, 0); 1040 } 1041 static unsigned char * 1042 asm_cvttss2si_xr(struct Inst *inst, struct Code *code) 1043 { 1044 return assemble_cvttsd2si(inst, code, 1); 1045 } 1046 1047 static unsigned char * 1048 assemble_cvtsd2ss(struct Inst *inst, struct Code *code, int single) 1049 { 1050 unsigned char *p = code->buf; 1051 unsigned char prefix = single ? 0xf3 : 0xf2; 1052 unsigned char sno = inst->opr[0].u.regxmm - XMM0; 1053 unsigned char dno = inst->opr[1].u.regxmm - XMM0; 1054 short buf[] = { 1055 prefix, 1056 sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, 1057 0x0f, 1058 0x5a, 1059 (unsigned char)0xc0 | ((dno & 7) << 3) | (sno & 7), 1060 }; 1061 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1062 return p; 1063 } 1064 static unsigned char * 1065 asm_cvtsd2ss_xx(struct Inst *inst, struct Code *code) 1066 { 1067 return assemble_cvtsd2ss(inst, code, 0); 1068 } 1069 static unsigned char * 1070 asm_cvtss2sd_xx(struct Inst *inst, struct Code *code) 1071 { 1072 return assemble_cvtsd2ss(inst, code, 1); 1073 } 1074 1075 static unsigned char * 1076 asm_sqrtsd_xx(struct Inst *inst, struct Code *code) 1077 { 1078 unsigned char sno = inst->opr[0].u.regxmm - XMM0; 1079 unsigned char dno = inst->opr[1].u.regxmm - XMM0; 1080 short buf[] = { 1081 0xf2, 1082 sno >= 8 || dno >= 8 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) : -1, 1083 0x0f, 1084 0x51, 1085 (unsigned char)0xc0 | ((dno & 7) << 3) | (sno & 7), 1086 }; 1087 unsigned char *p = code->buf; 1088 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1089 return p; 1090 } 1091 1092 /* movq %gp64, %xmm: bitwise move, the gp register's 64 bits become 1093 * the xmm register's low 64 bits (high 64 zeroed). qbe emits this to 1094 * spill a double through a gp register and to build the sign-bit mask 1095 * operand xorpd needs for float negation */ 1096 static unsigned char * 1097 asm_movq_gpxmm(struct Inst *inst, struct Code *code) 1098 { 1099 unsigned char sno = opr_regno(&inst->opr[0].u.reg); 1100 unsigned char dno = inst->opr[1].u.regxmm - XMM0; 1101 short buf[] = { 1102 0x66, 1103 (unsigned char)0x48 | ((sno & 8) >> 3) | ((dno & 8) >> 1), 1104 0x0f, 1105 0x6e, 1106 (unsigned char)0xc0 | ((dno & 7) << 3) | (sno & 7), 1107 }; 1108 unsigned char *p = code->buf; 1109 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1110 return p; 1111 } 1112 1113 /* mirror of asm_movq_gpxmm: the xmm register's low 64 bits become the 1114 * gp register's value */ 1115 static unsigned char * 1116 asm_movq_xmmgp(struct Inst *inst, struct Code *code) 1117 { 1118 unsigned char sno = inst->opr[0].u.regxmm - XMM0; 1119 unsigned char dno = opr_regno(&inst->opr[1].u.reg); 1120 short buf[] = { 1121 0x66, 1122 (unsigned char)0x48 | ((dno & 8) >> 3) | ((sno & 8) >> 1), 1123 0x0f, 1124 0x7e, 1125 (unsigned char)0xc0 | ((sno & 7) << 3) | (dno & 7), 1126 }; 1127 unsigned char *p = code->buf; 1128 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1129 return p; 1130 } 1131 1132 static unsigned char * 1133 asm_movq_ixmm(struct Inst *inst, struct Code *code) 1134 { 1135 struct Expr *offset_expr; 1136 struct Expr *scale_expr = NULL; 1137 long offset; 1138 int bno; 1139 int ino = 0; 1140 int dno; 1141 int d; 1142 int mod; 1143 int scale_bit = 0; 1144 int scale; 1145 int indexed = 0; 1146 short buf[7]; 1147 unsigned char *p; 1148 1149 dno = inst->opr[1].u.regxmm - XMM0; 1150 d = dno & 7; 1151 if (inst->opr[0].type == INDIRECT) { 1152 bno = opr_regno(&inst->opr[0].u.indirect.reg); 1153 offset_expr = inst->opr[0].u.indirect.offset.expr; 1154 } else if (inst->opr[0].type == INDIRECT_WITH_INDEX) { 1155 bno = opr_regno(&inst->opr[0].u.indirect_with_index.base_reg); 1156 ino = opr_regno(&inst->opr[0].u.indirect_with_index.index_reg); 1157 offset_expr = inst->opr[0].u.indirect_with_index.offset; 1158 scale_expr = inst->opr[0].u.indirect_with_index.scale; 1159 indexed = 1; 1160 } else { 1161 return NULL; 1162 } 1163 if (bno == RIP || offset_expr->kind != EX_FIXNUM 1164 || !(offset = offset_expr->u.fixnum, is_im32(offset))) 1165 return NULL; 1166 if (scale_expr != NULL) { 1167 if (scale_expr->kind != EX_FIXNUM) 1168 return NULL; 1169 scale = scale_expr->u.fixnum; 1170 if (scale <= 0 || scale > 8 || !IS_POWER_OF_2(scale)) 1171 return NULL; 1172 scale_bit = most_significant_bit(scale); 1173 } 1174 1175 mod = offset == 0 && (bno & 7) != RBP - RAX ? 0x00 : is_im8(offset) ? 0x40 : 0x80; 1176 buf[0] = 0xf3; 1177 buf[1] = bno >= 8 || ino >= 8 || dno >= 8 1178 ? 0x40 | ((bno & 8) >> 3) | ((ino & 8) >> 2) | ((dno & 8) >> 1) 1179 : -1; 1180 buf[2] = 0x0f; 1181 buf[3] = 0x7e; 1182 buf[4] = mod | (d << 3) | (indexed ? 0x04 : bno & 7); 1183 buf[5] = indexed ? (scale_bit << 6) | ((ino & 7) << 3) | (bno & 7) 1184 : (bno & 7) == RSP - RAX ? 0x24 1185 : -1; 1186 buf[6] = -1; 1187 1188 p = put_code_filtered(code->buf, buf, ARRAY_SIZE(buf)); 1189 if (offset == 0 && (bno & 7) != RBP - RAX) { 1190 ; 1191 } else if (is_im8(offset)) { 1192 *p++ = IM8(offset); 1193 } else { 1194 PUT_CODE(p, IM32(offset)); 1195 p += 4; 1196 } 1197 return p; 1198 } 1199 1200 /* mirror of asm_movq_ixmm: movq %xmm, (mem), same 0f 7e opcode 1201 * asm_movq_xmmgp uses, addressing a memory operand instead of a gp 1202 * register */ 1203 static unsigned char * 1204 asm_movq_xmmi(struct Inst *inst, struct Code *code) 1205 { 1206 long offset; 1207 1208 if (inst->opr[1].u.indirect.reg.no == RIP) 1209 return NULL; 1210 if (inst->opr[1].u.indirect.offset.expr->kind != EX_FIXNUM 1211 || !(offset = inst->opr[1].u.indirect.offset.expr->u.fixnum, is_im32(offset))) 1212 return NULL; 1213 { 1214 unsigned char sno = inst->opr[0].u.regxmm - XMM0; 1215 unsigned char dno = opr_regno(&inst->opr[1].u.indirect.reg); 1216 int d = dno & 7; 1217 int s = sno & 7; 1218 unsigned char op = (offset == 0 && d != RBP - RAX) ? (unsigned char)0x00 1219 : is_im8(offset) ? (unsigned char)0x40 1220 : (unsigned char)0x80; 1221 short buf[] = { 1222 0x66, 1223 (unsigned char)0x48 | ((dno & 8) >> 3) | ((sno & 8) >> 1), 1224 0x0f, 1225 0x7e, 1226 op | d | (s << 3), 1227 d == RSP - RAX ? 0x24 : -1, 1228 }; 1229 unsigned char *p = code->buf; 1230 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1231 if (offset == 0 && d != RBP - RAX) { 1232 ; 1233 } else if (is_im8(offset)) { 1234 *p++ = IM8(offset); 1235 } else { 1236 PUT_CODE(p, IM32(offset)); 1237 p += 4; 1238 } 1239 return p; 1240 } 1241 } 1242 1243 static unsigned char * 1244 asm_endbr64(struct Inst *inst, struct Code *code) 1245 { 1246 UNUSED(inst); 1247 static const short buf[] = { 1248 0xf3, 1249 0x0f, 1250 0x1e, 1251 0xfa, 1252 }; 1253 unsigned char *p = code->buf; 1254 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1255 return p; 1256 } 1257 1258 static long 1259 signed_immediate(long value, enum RegSize size) 1260 { 1261 switch (size) { 1262 case REG8: 1263 return (int8_t)value; 1264 case REG16: 1265 return (int16_t)value; 1266 case REG32: 1267 return (int32_t)value; 1268 case REG64: 1269 return (int64_t)value; 1270 } 1271 assert(!"Must not reached"); 1272 return value; 1273 } 1274 1275 static unsigned char * 1276 asm_lea_ir(struct Inst *inst, struct Code *code) 1277 { 1278 assert(inst->opr[0].u.indirect.offset.expr != NULL); 1279 unsigned char *p = code->buf; 1280 if (inst->opr[0].u.indirect.reg.no != RIP) { 1281 if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) { 1282 long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; 1283 enum RegSize size = inst->opr[1].u.reg.size; 1284 short buf[] = { 1285 MAKE_REX_INDIRECT( 1286 size, 1287 opr_regno(&inst->opr[1].u.reg), 1288 opr_regno(&inst->opr[0].u.indirect.reg), 1289 0x8c, 1290 0x00, 1291 offset 1292 ), 1293 MAKE_OFFSET(offset, inst->opr[0].u.indirect.reg.no != RBP - RAX), 1294 }; 1295 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1296 return p; 1297 } 1298 } else { 1299 if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM) { 1300 int pre = !inst->opr[1].u.reg.x ? 0x48 : 0x4c; 1301 int d = inst->opr[1].u.reg.no; 1302 MAKE_CODE(inst, code, pre, 0x8d, 0x05 | (d << 3), IM32(0)); 1303 return p; 1304 } 1305 } 1306 return NULL; 1307 } 1308 1309 static unsigned char * 1310 asm_lea_iir(struct Inst *inst, struct Code *code) 1311 { 1312 struct Expr *offset_expr = inst->opr[0].u.indirect_with_index.offset; 1313 struct Expr *scale_expr = inst->opr[0].u.indirect_with_index.scale; 1314 if ((offset_expr == NULL || offset_expr->kind == EX_FIXNUM) 1315 && (scale_expr == NULL || scale_expr->kind == EX_FIXNUM)) { 1316 long offset = offset_expr != NULL ? offset_expr->u.fixnum : 0; 1317 long scale = scale_expr != NULL ? scale_expr->u.fixnum : 1; 1318 if (is_im32(offset) && 1 <= scale && scale <= 8 && IS_POWER_OF_2(scale)) { 1319 int breg = opr_regno(&inst->opr[0].u.indirect_with_index.base_reg); 1320 int ireg = opr_regno(&inst->opr[0].u.indirect_with_index.index_reg); 1321 int dreg = opr_regno(&inst->opr[1].u.reg); 1322 int noofs = offset == 0 && breg != RBP - RAX; 1323 int pow = most_significant_bit(scale); 1324 short buf[] = { 1325 0x48 | ((breg & 8) >> 3) | ((ireg & 8) >> 2) | ((dreg & 8) >> 1), 1326 0x8d, 1327 (noofs ? 0x00 1328 : is_im8(offset) ? 0x40 1329 : 0x80) 1330 | ((dreg & 7) << 3) | 0x04, 1331 (breg & 7) | ((ireg & 7) << 3) | (pow << 6), 1332 breg == RSP - RAX ? 0x24 : -1, 1333 }; 1334 1335 unsigned char *p = code->buf; 1336 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1337 if (noofs) { 1338 ; 1339 } else if (is_im8(offset)) { 1340 *p++ = IM8(offset); 1341 } else if (is_im32(offset)) { 1342 PUT_CODE(p, IM32(offset)); 1343 p += 4; 1344 } 1345 return p; 1346 } 1347 } 1348 return NULL; 1349 } 1350 1351 static unsigned char * 1352 asm_add_rr(struct Inst *inst, struct Code *code) 1353 { 1354 enum RegSize size = inst->opr[0].u.reg.size; 1355 unsigned char *p = code->buf; 1356 p = put_rex2( 1357 p, 1358 size, 1359 opr_regno(&inst->opr[0].u.reg), 1360 opr_regno(&inst->opr[1].u.reg), 1361 size == REG8 ? 0x00 : 0x01 1362 ); 1363 return p; 1364 } 1365 1366 static unsigned char * 1367 asm_add_imr(struct Inst *inst, struct Code *code) 1368 { 1369 long value = inst->opr[0].u.immediate; 1370 if (is_im32(value)) { 1371 enum RegSize size = inst->opr[1].u.reg.size; 1372 int im8 = is_im8(value) || size == REG8; 1373 int d = opr_regno(&inst->opr[1].u.reg); 1374 unsigned char *p = code->buf; 1375 if (d == RAX - RAX && (size == REG8 || !im8)) 1376 p = put_rex0(p, size, 0, d, im8 ? 0x04 : 0x05); 1377 else 1378 p = put_rex1(p, size, 0xc0, d, size == REG8 ? 0x80 : im8 ? 0x83 : 0x81); 1379 1380 if (im8) { 1381 *p++ = IM8(value); 1382 } else { 1383 switch (size) { 1384 case REG8: 1385 *p++ = IM8(value); 1386 break; 1387 case REG16: 1388 PUT_CODE(p, IM16(value)); 1389 p += 2; 1390 break; 1391 case REG32: 1392 case REG64: 1393 PUT_CODE(p, IM32(value)); 1394 p += 4; 1395 break; 1396 } 1397 } 1398 return p; 1399 } 1400 return NULL; 1401 } 1402 1403 static unsigned char * 1404 asm_add_ir(struct Inst *inst, struct Code *code) 1405 { 1406 if (inst->opr[0].u.indirect.reg.no != RIP) { 1407 if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM) { 1408 long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; 1409 enum RegSize size = inst->opr[1].u.reg.size; 1410 unsigned char *p = code->buf; 1411 short buf[] = { 1412 MAKE_REX_INDIRECT( 1413 size, 1414 opr_regno(&inst->opr[1].u.reg), 1415 opr_regno(&inst->opr[0].u.indirect.reg), 1416 0x02, 1417 0x00, 1418 offset 1419 ), 1420 MAKE_OFFSET(offset, inst->opr[0].u.indirect.reg.no != RBP - RAX), 1421 }; 1422 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1423 return p; 1424 } 1425 } else { 1426 /* sym(%rip)/sym@gottpoff(%rip): symbolic rip-relative operand, 1427 * disp32 patched later once the target address (or got slot) is 1428 * known, same shape as asm_mov_ir's rip branch */ 1429 if (inst->opr[0].u.indirect.offset.expr->kind != EX_FIXNUM) { 1430 int pre = !inst->opr[1].u.reg.x ? 0x48 : 0x4c; 1431 int d = inst->opr[1].u.reg.no; 1432 MAKE_CODE(inst, code, pre, 0x03, 0x05 | (d << 3), IM32(0)); 1433 return code->buf; 1434 } 1435 } 1436 return NULL; 1437 } 1438 1439 static unsigned char * 1440 asm_add_iir(struct Inst *inst, struct Code *code) 1441 { 1442 assert( 1443 inst->opr[0].u.indirect_with_index.offset->kind == EX_FIXNUM 1444 && inst->opr[0].u.indirect_with_index.offset->u.fixnum == 0 1445 ); /* TODO */ 1446 unsigned char scale = 0; 1447 struct Expr *scale_expr = inst->opr[0].u.indirect_with_index.scale; 1448 if (scale_expr != NULL) { 1449 assert(scale_expr->kind == EX_FIXNUM); 1450 int s = scale_expr->u.fixnum; 1451 if (s <= 0 || s > 8 || !IS_POWER_OF_2(s)) 1452 return NULL; 1453 scale = most_significant_bit(s); 1454 } 1455 1456 unsigned char bno = inst->opr[0].u.indirect_with_index.base_reg.no; 1457 unsigned char ino = inst->opr[0].u.indirect_with_index.index_reg.no; 1458 unsigned char dno = inst->opr[1].u.reg.no; 1459 short buf[] = { 1460 (unsigned char)0x48 | (inst->opr[0].u.indirect_with_index.base_reg.x) 1461 | (inst->opr[0].u.indirect_with_index.index_reg.x << 1) | (inst->opr[1].u.reg.x << 2), 1462 0x03, 1463 0x04 | (dno << 3), 1464 bno | (ino << 3) | (scale << 6), 1465 }; 1466 unsigned char *p = code->buf; 1467 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1468 return p; 1469 } 1470 1471 static unsigned char * 1472 asm_addq_imi(struct Inst *inst, struct Code *code) 1473 { 1474 if (inst->opr[1].u.indirect.offset.expr->kind == EX_FIXNUM) { 1475 long value = inst->opr[0].u.immediate; 1476 if (is_im32(value)) { 1477 long offset = inst->opr[1].u.indirect.offset.expr->u.fixnum; 1478 unsigned char sno = 0; 1479 unsigned char dno = opr_regno(&inst->opr[1].u.indirect.reg); 1480 unsigned char op = (offset == 0 && (dno & 7) != RBP - RAX) ? 0x00 1481 : is_im8(offset) ? (unsigned char)0x40 1482 : (unsigned char)0x80; 1483 short buf[] = { 1484 (unsigned char)0x48 | ((dno & 8) >> 3) | ((sno & 8) >> 1), 1485 (is_im8(value) ? 0x83 : 0x81), 1486 op | (dno & 7) | ((sno & 7) << 3), 1487 (dno & 7) == RSP - RAX ? 0x24 : -1, 1488 }; 1489 unsigned char *p = code->buf; 1490 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1491 if (offset == 0 && (dno & 7) != RBP - RAX) { 1492 ; 1493 } else if (is_im8(offset)) { 1494 *p++ = IM8(offset); 1495 } else if (is_im32(offset)) { 1496 PUT_CODE(p, IM32(offset)); 1497 p += 4; 1498 } 1499 1500 if (is_im8(value)) { 1501 *p++ = IM8(value); 1502 } else { 1503 PUT_CODE(p, IM32(value)); 1504 p += 4; 1505 } 1506 return p; 1507 } 1508 } 1509 return NULL; 1510 } 1511 1512 static unsigned char * 1513 asm_sub_rr(struct Inst *inst, struct Code *code) 1514 { 1515 enum RegSize size = inst->opr[0].u.reg.size; 1516 unsigned char *p = code->buf; 1517 p = put_rex2( 1518 p, 1519 size, 1520 opr_regno(&inst->opr[0].u.reg), 1521 opr_regno(&inst->opr[1].u.reg), 1522 size == REG8 ? 0x28 : 0x29 1523 ); 1524 return p; 1525 } 1526 1527 static unsigned char * 1528 asm_sub_imr(struct Inst *inst, struct Code *code) 1529 { 1530 long value = inst->opr[0].u.immediate; 1531 if (is_im32(value)) { 1532 enum RegSize size = inst->opr[1].u.reg.size; 1533 int im8 = is_im8(value) || size == REG8; 1534 int d = opr_regno(&inst->opr[1].u.reg); 1535 unsigned char *p = code->buf; 1536 if (d == RAX - RAX && (size == REG8 || !im8)) 1537 p = put_rex0(p, size, 0, d, im8 ? 0x2c : 0x2d); 1538 else 1539 p = put_rex1(p, size, 0xe8, d, size == REG8 ? 0x80 : im8 ? 0x83 : 0x81); 1540 1541 if (im8) { 1542 *p++ = IM8(value); 1543 } else { 1544 switch (size) { 1545 case REG8: 1546 *p++ = IM8(value); 1547 break; 1548 case REG16: 1549 PUT_CODE(p, IM16(value)); 1550 p += 2; 1551 break; 1552 case REG32: 1553 case REG64: 1554 PUT_CODE(p, IM32(value)); 1555 p += 4; 1556 break; 1557 } 1558 } 1559 return p; 1560 } 1561 return NULL; 1562 } 1563 1564 static unsigned char * 1565 asm_sub_ir(struct Inst *inst, struct Code *code) 1566 { 1567 if (inst->opr[0].u.indirect.offset.expr->kind == EX_FIXNUM 1568 && inst->opr[0].u.indirect.reg.no != RIP) { 1569 long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; 1570 enum RegSize size = inst->opr[1].u.reg.size; 1571 unsigned char *p = code->buf; 1572 short buf[] = { 1573 MAKE_REX_INDIRECT( 1574 size, 1575 opr_regno(&inst->opr[1].u.reg), 1576 opr_regno(&inst->opr[0].u.indirect.reg), 1577 0x2a, 1578 0x00, 1579 offset 1580 ), 1581 MAKE_OFFSET(offset, inst->opr[0].u.indirect.reg.no != RBP - RAX), 1582 }; 1583 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1584 return p; 1585 } 1586 return NULL; 1587 } 1588 1589 static unsigned char * 1590 asm_sub_iir(struct Inst *inst, struct Code *code) 1591 { 1592 assert( 1593 inst->opr[0].u.indirect_with_index.offset->kind == EX_FIXNUM 1594 && inst->opr[0].u.indirect_with_index.offset->u.fixnum == 0 1595 ); /* TODO */ 1596 unsigned char scale = 0; 1597 struct Expr *scale_expr = inst->opr[0].u.indirect_with_index.scale; 1598 if (scale_expr != NULL) { 1599 assert(scale_expr->kind == EX_FIXNUM); 1600 int s = scale_expr->u.fixnum; 1601 if (s <= 0 || s > 8 || !IS_POWER_OF_2(s)) 1602 return NULL; 1603 scale = most_significant_bit(s); 1604 } 1605 1606 unsigned char bno = inst->opr[0].u.indirect_with_index.base_reg.no; 1607 unsigned char ino = inst->opr[0].u.indirect_with_index.index_reg.no; 1608 unsigned char dno = inst->opr[1].u.reg.no; 1609 short buf[] = { 1610 (unsigned char)0x48 | (inst->opr[0].u.indirect_with_index.base_reg.x) 1611 | (inst->opr[0].u.indirect_with_index.index_reg.x << 1) | (inst->opr[1].u.reg.x << 2), 1612 0x2b, 1613 0x04 | (dno << 3), 1614 bno | (ino << 3) | (scale << 6), 1615 }; 1616 unsigned char *p = code->buf; 1617 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1618 return p; 1619 } 1620 1621 static unsigned char * 1622 asm_subq_imi(struct Inst *inst, struct Code *code) 1623 { 1624 if (inst->opr[1].u.indirect.offset.expr->kind == EX_FIXNUM) { 1625 long value = inst->opr[0].u.immediate; 1626 if (is_im32(value)) { 1627 long offset = inst->opr[1].u.indirect.offset.expr->u.fixnum; 1628 unsigned char sno = 0; 1629 unsigned char dno = opr_regno(&inst->opr[1].u.indirect.reg); 1630 unsigned char op = (offset == 0 && (dno & 7) != RBP - RAX) ? 0x28 1631 : is_im8(offset) ? (unsigned char)0x40 1632 : (unsigned char)0x80; 1633 short buf[] = { 1634 (unsigned char)0x48 | ((dno & 8) >> 3) | ((sno & 8) >> 1), 1635 (is_im8(value) ? 0x83 : 0x81), 1636 op | (dno & 7) | ((sno & 7) << 3), 1637 (dno & 7) == RSP - RAX ? 0x24 : -1, 1638 }; 1639 unsigned char *p = code->buf; 1640 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1641 if (offset == 0 && (dno & 7) != RBP - RAX) { 1642 ; 1643 } else if (is_im8(offset)) { 1644 *p++ = IM8(offset); 1645 } else if (is_im32(offset)) { 1646 PUT_CODE(p, IM32(offset)); 1647 p += 4; 1648 } 1649 1650 if (is_im8(value)) { 1651 *p++ = IM8(value); 1652 } else { 1653 PUT_CODE(p, IM32(value)); 1654 p += 4; 1655 } 1656 return p; 1657 } 1658 } 1659 return NULL; 1660 } 1661 1662 static unsigned char * 1663 asm_mul_r(struct Inst *inst, struct Code *code) 1664 { 1665 enum RegSize size = inst->opr[0].u.reg.size; 1666 unsigned char *p = code->buf; 1667 short buf[] = { 1668 MAKE_REX0(size, 0, opr_regno(&inst->opr[0].u.reg), 0xf6 | (size == REG8 ? 0 : 1)), 1669 0xe0 | inst->opr[0].u.reg.no, 1670 }; 1671 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1672 return p; 1673 } 1674 1675 static unsigned char * 1676 asm_div_r(struct Inst *inst, struct Code *code) 1677 { 1678 enum RegSize size = inst->opr[0].u.reg.size; 1679 unsigned char *p = code->buf; 1680 short buf[] = { 1681 MAKE_REX0(size, 0, opr_regno(&inst->opr[0].u.reg), 0xf6 | (size == REG8 ? 0 : 1)), 1682 0xf0 | inst->opr[0].u.reg.no, 1683 }; 1684 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1685 return p; 1686 } 1687 1688 static unsigned char * 1689 asm_idiv_r(struct Inst *inst, struct Code *code) 1690 { 1691 enum RegSize size = inst->opr[0].u.reg.size; 1692 unsigned char *p = code->buf; 1693 short buf[] = { 1694 MAKE_REX0(size, 0, opr_regno(&inst->opr[0].u.reg), 0xf6 | (size == REG8 ? 0 : 1)), 1695 0xf8 | inst->opr[0].u.reg.no, 1696 }; 1697 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1698 return p; 1699 } 1700 1701 static unsigned char * 1702 asm_neg_r(struct Inst *inst, struct Code *code) 1703 { 1704 unsigned char *p = code->buf; 1705 p = put_rex1(p, inst->opr[0].u.reg.size, 0xd8, opr_regno(&inst->opr[0].u.reg), 0xf7); 1706 return p; 1707 } 1708 1709 static unsigned char * 1710 asm_not_r(struct Inst *inst, struct Code *code) 1711 { 1712 unsigned char *p = code->buf; 1713 p = put_rex1(p, inst->opr[0].u.reg.size, 0xd0, opr_regno(&inst->opr[0].u.reg), 0xf7); 1714 return p; 1715 } 1716 1717 static unsigned char * 1718 asm_inc_r(struct Inst *inst, struct Code *code) 1719 { 1720 unsigned char *p = code->buf; 1721 struct Reg *reg = &inst->opr[0].u.reg; 1722 p = put_rex1(p, reg->size, 0xc0, opr_regno(reg), reg->size == REG8 ? 0xfe : 0xff); 1723 return p; 1724 } 1725 1726 static unsigned char * 1727 asm_incbwlq_i(struct Inst *inst, struct Code *code) 1728 { 1729 if (inst->opr[0].u.indirect.reg.no != RIP) { 1730 enum RegSize size = inst->op + (REG8 - INCB); 1731 long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; 1732 unsigned char *p = code->buf; 1733 short buf[] = { 1734 MAKE_REX_INDIRECT(size, 0, opr_regno(&inst->opr[0].u.indirect.reg), 0xfe, 0x00, offset), 1735 MAKE_OFFSET(offset, inst->opr[0].u.indirect.reg.no != RBP - RAX), 1736 }; 1737 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1738 return p; 1739 } 1740 return NULL; 1741 } 1742 1743 static unsigned char * 1744 asm_dec_r(struct Inst *inst, struct Code *code) 1745 { 1746 unsigned char *p = code->buf; 1747 struct Reg *reg = &inst->opr[0].u.reg; 1748 p = put_rex1(p, reg->size, 0xc8, opr_regno(reg), reg->size == REG8 ? 0xfe : 0xff); 1749 return p; 1750 } 1751 1752 static unsigned char * 1753 asm_decbwlq_i(struct Inst *inst, struct Code *code) 1754 { 1755 if (inst->opr[0].u.indirect.reg.no != RIP) { 1756 enum RegSize size = inst->op + (REG8 - DECB); 1757 long offset = inst->opr[0].u.indirect.offset.expr->u.fixnum; 1758 unsigned char *p = code->buf; 1759 short buf[] = { 1760 MAKE_REX_INDIRECT(size, 1, opr_regno(&inst->opr[0].u.indirect.reg), 0xfe, 0x08, offset), 1761 MAKE_OFFSET(offset, inst->opr[0].u.indirect.reg.no != RBP - RAX), 1762 }; 1763 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 1764 return p; 1765 } 1766 return NULL; 1767 } 1768 1769 static unsigned char * 1770 asm_and_rr(struct Inst *inst, struct Code *code) 1771 { 1772 enum RegSize size = inst->opr[1].u.reg.size; 1773 unsigned char *p = code->buf; 1774 p = put_rex2( 1775 p, 1776 size, 1777 opr_regno(&inst->opr[0].u.reg), 1778 opr_regno(&inst->opr[1].u.reg), 1779 size == REG8 ? 0x20 : 0x21 1780 ); 1781 return p; 1782 } 1783 1784 static unsigned char * 1785 asm_and_imr(struct Inst *inst, struct Code *code) 1786 { 1787 long value = inst->opr[0].u.immediate; 1788 enum RegSize size = inst->opr[1].u.reg.size; 1789 unsigned char *p = code->buf; 1790 if (is_im8(value) && (size != REG8 || opr_regno(&inst->opr[1].u.reg) != AL - AL)) { 1791 p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x83); 1792 *p++ = IM8(value); 1793 return p; 1794 } else if (size <= REG32 || is_im32(value)) { 1795 if (opr_regno(&inst->opr[1].u.reg) == RAX - RAX) { 1796 p = put_rex0(p, size, 0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x24 : 0x25); 1797 } else { 1798 p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x81); 1799 } 1800 1801 switch (size) { 1802 case REG8: 1803 PUT_CODE(p, IM8(value)); 1804 p += 1; 1805 break; 1806 case REG16: 1807 PUT_CODE(p, IM16(value)); 1808 p += 2; 1809 break; 1810 case REG32: 1811 case REG64: 1812 PUT_CODE(p, IM32(value)); 1813 p += 4; 1814 break; 1815 } 1816 return p; 1817 } 1818 return NULL; 1819 } 1820 1821 static unsigned char * 1822 asm_or_rr(struct Inst *inst, struct Code *code) 1823 { 1824 enum RegSize size = inst->opr[1].u.reg.size; 1825 unsigned char *p = code->buf; 1826 p = put_rex2( 1827 p, 1828 size, 1829 opr_regno(&inst->opr[0].u.reg), 1830 opr_regno(&inst->opr[1].u.reg), 1831 size == REG8 ? 0x08 : 0x09 1832 ); 1833 return p; 1834 } 1835 1836 static unsigned char * 1837 asm_or_imr(struct Inst *inst, struct Code *code) 1838 { 1839 long value = inst->opr[0].u.immediate; 1840 enum RegSize size = inst->opr[1].u.reg.size; 1841 unsigned char *p = code->buf; 1842 if (is_im8(value) && (size != REG8 || opr_regno(&inst->opr[1].u.reg) != AL - AL)) { 1843 p = put_rex1(p, size, 0xc8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x83); 1844 *p++ = IM8(value); 1845 return p; 1846 } else if (size <= REG32 || is_im32(value)) { 1847 if (opr_regno(&inst->opr[1].u.reg) == RAX - RAX) { 1848 p = put_rex0(p, size, 0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x0c : 0x0d); 1849 } else { 1850 p = put_rex1(p, size, 0xc8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x81); 1851 } 1852 switch (size) { 1853 case REG8: 1854 PUT_CODE(p, IM8(value)); 1855 p += 1; 1856 break; 1857 case REG16: 1858 PUT_CODE(p, IM16(value)); 1859 p += 2; 1860 break; 1861 case REG32: 1862 case REG64: 1863 PUT_CODE(p, IM32(value)); 1864 p += 4; 1865 break; 1866 } 1867 return p; 1868 } 1869 return NULL; 1870 } 1871 1872 static unsigned char * 1873 asm_xor_rr(struct Inst *inst, struct Code *code) 1874 { 1875 enum RegSize size = inst->opr[1].u.reg.size; 1876 unsigned char *p = code->buf; 1877 p = put_rex2( 1878 p, 1879 size, 1880 opr_regno(&inst->opr[0].u.reg), 1881 opr_regno(&inst->opr[1].u.reg), 1882 size == REG8 ? 0x30 : 0x31 1883 ); 1884 return p; 1885 } 1886 1887 static unsigned char * 1888 asm_xor_imr(struct Inst *inst, struct Code *code) 1889 { 1890 long value = inst->opr[0].u.immediate; 1891 enum RegSize size = inst->opr[1].u.reg.size; 1892 unsigned char *p = code->buf; 1893 if (is_im8(value) && (size != REG8 || opr_regno(&inst->opr[1].u.reg) != AL - AL)) { 1894 p = put_rex1(p, size, 0xf0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x83); 1895 *p++ = IM8(value); 1896 return p; 1897 } else if (size <= REG32 || is_im32(value)) { 1898 if (opr_regno(&inst->opr[1].u.reg) == RAX - RAX) { 1899 p = put_rex0(p, size, 0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x34 : 0x35); 1900 } else { 1901 p = put_rex1(p, size, 0xf0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0x80 : 0x81); 1902 } 1903 switch (size) { 1904 case REG8: 1905 PUT_CODE(p, IM8(value)); 1906 p += 1; 1907 break; 1908 case REG16: 1909 PUT_CODE(p, IM16(value)); 1910 p += 2; 1911 break; 1912 case REG32: 1913 case REG64: 1914 PUT_CODE(p, IM32(value)); 1915 p += 4; 1916 break; 1917 } 1918 return p; 1919 } 1920 return NULL; 1921 } 1922 1923 static unsigned char * 1924 asm_shl_rr(struct Inst *inst, struct Code *code) 1925 { 1926 enum RegSize size = inst->opr[1].u.reg.size; 1927 unsigned char *p = code->buf; 1928 p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd2 : 0xd3); 1929 return p; 1930 } 1931 1932 static unsigned char * 1933 asm_shl_imr(struct Inst *inst, struct Code *code) 1934 { 1935 enum RegSize size = inst->opr[1].u.reg.size; 1936 unsigned char *p = code->buf; 1937 uint64_t shift = inst->opr[0].u.immediate; 1938 if (shift >= 256) 1939 return NULL; 1940 if (shift == 1) { 1941 p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd0 : 0xd1); 1942 } else { 1943 p = put_rex1(p, size, 0xe0, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xc0 : 0xc1); 1944 *p++ = IM8(shift); 1945 } 1946 return p; 1947 } 1948 1949 static unsigned char * 1950 asm_shr_rr(struct Inst *inst, struct Code *code) 1951 { 1952 enum RegSize size = inst->opr[1].u.reg.size; 1953 unsigned char *p = code->buf; 1954 p = put_rex1(p, size, 0xe8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd2 : 0xd3); 1955 return p; 1956 } 1957 1958 static unsigned char * 1959 asm_shr_imr(struct Inst *inst, struct Code *code) 1960 { 1961 enum RegSize size = inst->opr[1].u.reg.size; 1962 unsigned char *p = code->buf; 1963 uint64_t shift = inst->opr[0].u.immediate; 1964 if (shift >= 256) 1965 return NULL; 1966 if (shift == 1) { 1967 p = put_rex1(p, size, 0xe8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd0 : 0xd1); 1968 } else { 1969 p = put_rex1(p, size, 0xe8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xc0 : 0xc1); 1970 *p++ = IM8(shift); 1971 } 1972 return p; 1973 } 1974 1975 static unsigned char * 1976 asm_sar_rr(struct Inst *inst, struct Code *code) 1977 { 1978 enum RegSize size = inst->opr[1].u.reg.size; 1979 unsigned char *p = code->buf; 1980 p = put_rex1(p, size, 0xf8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd2 : 0xd3); 1981 return p; 1982 } 1983 1984 static unsigned char * 1985 asm_sar_imr(struct Inst *inst, struct Code *code) 1986 { 1987 enum RegSize size = inst->opr[1].u.reg.size; 1988 unsigned char *p = code->buf; 1989 uint64_t shift = inst->opr[0].u.immediate; 1990 if (shift >= 256) 1991 return NULL; 1992 if (shift == 1) { 1993 p = put_rex1(p, size, 0xf8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xd0 : 0xd1); 1994 } else { 1995 p = put_rex1(p, size, 0xf8, opr_regno(&inst->opr[1].u.reg), size == REG8 ? 0xc0 : 0xc1); 1996 *p++ = IM8(shift); 1997 } 1998 return p; 1999 } 2000 2001 static unsigned char * 2002 asm_cmp_rr(struct Inst *inst, struct Code *code) 2003 { 2004 enum RegSize size = inst->opr[0].u.reg.size; 2005 unsigned char *p = code->buf; 2006 p = put_rex2( 2007 p, 2008 size, 2009 opr_regno(&inst->opr[0].u.reg), 2010 opr_regno(&inst->opr[1].u.reg), 2011 size == REG8 ? 0x38 : 0x39 2012 ); 2013 return p; 2014 } 2015 2016 static unsigned char * 2017 asm_cmp_imr(struct Inst *inst, struct Code *code) 2018 { 2019 enum RegSize size = inst->opr[1].u.reg.size; 2020 long value = signed_immediate(inst->opr[0].u.immediate, size); 2021 if (is_im32(value) || size <= REG32) { 2022 int im8 = is_im8(value); 2023 int d = opr_regno(&inst->opr[1].u.reg); 2024 unsigned char *p = code->buf; 2025 if (d == RAX - RAX && (size == REG8 || !im8)) 2026 p = put_rex0(p, size, 0, d, size == REG8 ? 0x3c : 0x3d); 2027 else 2028 p = put_rex1(p, size, 0xf8, d, 0x80 | (size == REG8 ? 0 : im8 ? 3 : 1)); 2029 2030 if (im8) { 2031 *p++ = IM8(value); 2032 } else { 2033 switch (size) { 2034 case REG8: 2035 *p++ = IM8(value); 2036 break; 2037 case REG16: 2038 PUT_CODE(p, IM16(value)); 2039 p += 2; 2040 break; 2041 case REG32: 2042 case REG64: 2043 PUT_CODE(p, IM32(value)); 2044 p += 4; 2045 break; 2046 } 2047 } 2048 return p; 2049 } 2050 return NULL; 2051 } 2052 2053 static unsigned char * 2054 asm_test_rr(struct Inst *inst, struct Code *code) 2055 { 2056 enum RegSize size = inst->opr[0].u.reg.size; 2057 unsigned char *p = code->buf; 2058 p = put_rex2( 2059 p, 2060 size, 2061 opr_regno(&inst->opr[0].u.reg), 2062 opr_regno(&inst->opr[1].u.reg), 2063 size == REG8 ? 0x84 : 0x85 2064 ); 2065 return p; 2066 } 2067 2068 static unsigned char * 2069 asm_cwtl(struct Inst *inst, struct Code *code) 2070 { 2071 MAKE_CODE(inst, code, 0x98); 2072 return code->buf; 2073 } 2074 2075 static unsigned char * 2076 asm_cltd(struct Inst *inst, struct Code *code) 2077 { 2078 MAKE_CODE(inst, code, 0x99); 2079 return code->buf; 2080 } 2081 2082 static unsigned char * 2083 asm_cqto(struct Inst *inst, struct Code *code) 2084 { 2085 MAKE_CODE(inst, code, 0x48, 0x99); 2086 return code->buf; 2087 } 2088 2089 static unsigned char * 2090 asm_bsr(struct Inst *inst, struct Code *code) 2091 { 2092 enum RegSize size = inst->opr[0].u.reg.size; 2093 int sno = opr_regno(&inst->opr[0].u.reg); 2094 int dno = opr_regno(&inst->opr[1].u.reg); 2095 short buf[] = { 2096 MAKE_REX0(size, dno, sno, 0x0f), 2097 0xbd, 2098 0xc0 | (inst->opr[1].u.reg.no << 3) | inst->opr[0].u.reg.no, 2099 }; 2100 unsigned char *p = code->buf; 2101 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2102 return p; 2103 } 2104 2105 static unsigned char * 2106 asm_tzcnt(struct Inst *inst, struct Code *code) 2107 { 2108 enum RegSize size = inst->opr[0].u.reg.size; 2109 unsigned char prefix = 0xf3; 2110 int sno = opr_regno(&inst->opr[0].u.reg); 2111 int dno = opr_regno(&inst->opr[1].u.reg); 2112 short buf[] = { 2113 (size) == REG16 ? 0x66 : -1, 2114 prefix, 2115 sno >= 8 || dno >= 8 || size == REG64 2116 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) | (size != REG64 ? 0 : 8) 2117 : -1, 2118 0x0f, 2119 inst->op == LZCNT ? 0xbd : 0xbc, 2120 (unsigned char)0xc0 | ((dno & 7) << 3) | (sno & 7), 2121 }; 2122 unsigned char *p = code->buf; 2123 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2124 return p; 2125 } 2126 2127 static unsigned char * 2128 asm_popcnt(struct Inst *inst, struct Code *code) 2129 { 2130 enum RegSize size = inst->opr[0].u.reg.size; 2131 unsigned char prefix = 0xf3; 2132 int sno = opr_regno(&inst->opr[0].u.reg); 2133 int dno = opr_regno(&inst->opr[1].u.reg); 2134 short buf[] = { 2135 (size) == REG16 ? 0x66 : -1, 2136 prefix, 2137 sno >= 8 || dno >= 8 || size == REG64 2138 ? (unsigned char)0x40 | ((sno & 8) >> 3) | ((dno & 8) >> 1) | (size != REG64 ? 0 : 8) 2139 : -1, 2140 0x0f, 2141 0xb8, 2142 (unsigned char)0xc0 | ((dno & 7) << 3) | (sno & 7), 2143 }; 2144 unsigned char *p = code->buf; 2145 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2146 return p; 2147 } 2148 2149 static unsigned char * 2150 asm_set_r(struct Inst *inst, struct Code *code) 2151 { 2152 unsigned char *p = code->buf; 2153 short buf[] = { 2154 MAKE_REX0(REG8, 0, opr_regno(&inst->opr[0].u.reg), 0x0f), 2155 0x90 | (inst->op - SETO), 2156 0xc0 | inst->opr[0].u.reg.no, 2157 }; 2158 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2159 return p; 2160 } 2161 2162 static unsigned char * 2163 asm_push_r(struct Inst *inst, struct Code *code) 2164 { 2165 unsigned char *p = code->buf; 2166 short buf[] = { 2167 MAKE_REX0(REG32, 0, opr_regno(&inst->opr[0].u.reg), 0x50 | inst->opr[0].u.reg.no), 2168 }; 2169 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2170 return p; 2171 } 2172 2173 static unsigned char * 2174 asm_push_im(struct Inst *inst, struct Code *code) 2175 { 2176 unsigned char *p = code->buf; 2177 long value = inst->opr[0].u.immediate; 2178 if (is_im8(value)) { 2179 *p++ = 0x6a; 2180 *p++ = IM8(value); 2181 return p; 2182 } else if (is_im32(value)) { 2183 *p++ = 0x68; 2184 PUT_CODE(p, IM32(value)); 2185 p += 4; 2186 return p; 2187 } 2188 return NULL; 2189 } 2190 2191 static unsigned char * 2192 asm_pop_r(struct Inst *inst, struct Code *code) 2193 { 2194 unsigned char *p = code->buf; 2195 short buf[] = { 2196 MAKE_REX0(REG32, 0, opr_regno(&inst->opr[0].u.reg), 0x58 | inst->opr[0].u.reg.no), 2197 }; 2198 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2199 return p; 2200 } 2201 2202 static unsigned char * 2203 asm_jmp_d(struct Inst *inst, struct Code *code) 2204 { 2205 MAKE_CODE(inst, code, 0xeb, IM8(0)); /* short jmp in default */ 2206 return code->buf; 2207 } 2208 2209 static unsigned char * 2210 asm_jmp_der(struct Inst *inst, struct Code *code) 2211 { 2212 int s = inst->opr[0].u.reg.no; 2213 short buf[] = { 2214 inst->opr[0].u.reg.x ? 0x41 : -1, 2215 0xff, 2216 0xe0 | s, 2217 }; 2218 unsigned char *p = code->buf; 2219 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2220 return p; 2221 } 2222 2223 static unsigned char * 2224 asm_jmp_dei(struct Inst *inst, struct Code *code) 2225 { 2226 struct Expr *offset_expr = inst->opr[0].u.indirect.offset.expr; 2227 if ((offset_expr == NULL || offset_expr->kind == EX_FIXNUM)) { 2228 long offset = offset_expr != NULL ? offset_expr->u.fixnum : 0; 2229 if (is_im32(offset)) { 2230 short offset_bit = offset == 0 ? 0x20 : is_im8(offset) ? 0x60 : 0xa0; 2231 short b = inst->opr[0].u.indirect.reg.no; 2232 short buf[] = { 2233 inst->opr[0].u.indirect.reg.x ? 0x41 : -1, 2234 0xff, 2235 offset_bit | b, 2236 }; 2237 unsigned char *p = code->buf; 2238 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2239 if (offset != 0) { 2240 if (is_im8(offset)) { 2241 *p++ = IM8(offset); 2242 } else { 2243 PUT_CODE(p, IM32(offset)); 2244 p += 4; 2245 } 2246 } 2247 return p; 2248 } 2249 } 2250 return NULL; 2251 } 2252 2253 static unsigned char * 2254 asm_jmp_deii(struct Inst *inst, struct Code *code) 2255 { 2256 struct Expr *offset_expr = inst->opr[0].u.indirect_with_index.offset; 2257 struct Expr *scale_expr = inst->opr[0].u.indirect_with_index.scale; 2258 if ((offset_expr == NULL || offset_expr->kind == EX_FIXNUM) 2259 && (scale_expr == NULL || scale_expr->kind == EX_FIXNUM)) { 2260 long offset = offset_expr != NULL ? offset_expr->u.fixnum : 0; 2261 long scale = scale_expr != NULL ? scale_expr->u.fixnum : 1; 2262 if (is_im32(offset) && 1 <= scale && scale <= 8 && IS_POWER_OF_2(scale)) { 2263 short b = inst->opr[0].u.indirect_with_index.base_reg.no; 2264 short scale_bit = most_significant_bit(scale); 2265 short i = inst->opr[0].u.indirect_with_index.index_reg.no; 2266 short prefix = inst->opr[0].u.indirect_with_index.base_reg.x 2267 | (inst->opr[0].u.indirect_with_index.index_reg.x << 1); 2268 short offset_bit = offset == 0 && b != RBP - RAX ? 0x20 : is_im8(offset) ? 0x60 : 0xa0; 2269 short buf[] = { 2270 prefix != 0 ? 0x40 | prefix : -1, 2271 0xff, 2272 offset_bit | 0x04, 2273 (scale_bit << 6) | (i << 3) | b, 2274 }; 2275 unsigned char *p = code->buf; 2276 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2277 if (offset != 0 || b == RBP - RAX) { 2278 if (is_im8(offset)) { 2279 *p++ = IM8(offset); 2280 } else { 2281 PUT_CODE(p, IM32(offset)); 2282 p += 4; 2283 } 2284 } 2285 return p; 2286 } 2287 } 2288 return NULL; 2289 } 2290 2291 static unsigned char * 2292 asm_jxx_d(struct Inst *inst, struct Code *code) 2293 { 2294 MAKE_CODE(inst, code, 0x70 + (inst->op - JO), IM8(0)); /* short jmp in default */ 2295 return code->buf; 2296 } 2297 2298 static unsigned char * 2299 asm_call_d(struct Inst *inst, struct Code *code) 2300 { 2301 MAKE_CODE(inst, code, 0xe8, IM32(0)); 2302 return code->buf; 2303 } 2304 2305 static unsigned char * 2306 asm_call_der(struct Inst *inst, struct Code *code) 2307 { 2308 int s = inst->opr[0].u.reg.no; 2309 if (!inst->opr[0].u.reg.x) { 2310 MAKE_CODE(inst, code, 0xff, 0xd0 + s); 2311 } else { 2312 MAKE_CODE(inst, code, 0x41, 0xff, 0xd0 + s); 2313 } 2314 return code->buf; 2315 } 2316 2317 static unsigned char * 2318 asm_ret(struct Inst *inst, struct Code *code) 2319 { 2320 MAKE_CODE(inst, code, 0xc3); 2321 return code->buf; 2322 } 2323 2324 static unsigned char * 2325 asm_int_im(struct Inst *inst, struct Code *code) 2326 { 2327 long value = inst->opr[0].u.immediate; 2328 MAKE_CODE(inst, code, 0xcd, IM8(value)); 2329 return code->buf; 2330 } 2331 2332 static unsigned char * 2333 asm_syscall(struct Inst *inst, struct Code *code) 2334 { 2335 MAKE_CODE(inst, code, 0x0f, 0x05); 2336 return code->buf; 2337 } 2338 2339 /*// //////////////////////////////////////////// */ 2340 /* new instruction encoders */ 2341 2342 /* no-operand fixed-encoding instructions */ 2343 static unsigned char * 2344 asm_simple(struct Inst *inst, struct Code *code) 2345 { 2346 switch (inst->op) { 2347 case NOP: 2348 MAKE_CODE(inst, code, 0x90); 2349 return code->buf; 2350 case PUSHFQ: 2351 MAKE_CODE(inst, code, 0x9c); 2352 return code->buf; 2353 case POPFQ: 2354 MAKE_CODE(inst, code, 0x9d); 2355 return code->buf; 2356 case PUSHF: 2357 MAKE_CODE(inst, code, 0x66, 0x9c); 2358 return code->buf; 2359 case POPF: 2360 MAKE_CODE(inst, code, 0x66, 0x9d); 2361 return code->buf; 2362 case CPUID: 2363 MAKE_CODE(inst, code, 0x0f, 0xa2); 2364 return code->buf; 2365 case RDTSC: 2366 MAKE_CODE(inst, code, 0x0f, 0x31); 2367 return code->buf; 2368 case UD2: 2369 MAKE_CODE(inst, code, 0x0f, 0x0b); 2370 return code->buf; 2371 case PAUSE: 2372 MAKE_CODE(inst, code, 0xf3, 0x90); 2373 return code->buf; 2374 case HLT: 2375 MAKE_CODE(inst, code, 0xf4); 2376 return code->buf; 2377 case CMC: 2378 MAKE_CODE(inst, code, 0xf5); 2379 return code->buf; 2380 case CLC: 2381 MAKE_CODE(inst, code, 0xf8); 2382 return code->buf; 2383 case STC: 2384 MAKE_CODE(inst, code, 0xf9); 2385 return code->buf; 2386 case CLI: 2387 MAKE_CODE(inst, code, 0xfa); 2388 return code->buf; 2389 case STI: 2390 MAKE_CODE(inst, code, 0xfb); 2391 return code->buf; 2392 case CLD: 2393 MAKE_CODE(inst, code, 0xfc); 2394 return code->buf; 2395 case STD: 2396 MAKE_CODE(inst, code, 0xfd); 2397 return code->buf; 2398 case CLTS: 2399 MAKE_CODE(inst, code, 0x0f, 0x06); 2400 return code->buf; 2401 case WBINVD: 2402 MAKE_CODE(inst, code, 0x0f, 0x09); 2403 return code->buf; 2404 case MFENCE: 2405 MAKE_CODE(inst, code, 0x0f, 0xae, 0xf0); 2406 return code->buf; 2407 case LFENCE: 2408 MAKE_CODE(inst, code, 0x0f, 0xae, 0xe8); 2409 return code->buf; 2410 case SFENCE: 2411 MAKE_CODE(inst, code, 0x0f, 0xae, 0xf8); 2412 return code->buf; 2413 default: 2414 return NULL; 2415 } 2416 } 2417 2418 /* one-operand imul: f7 /5 */ 2419 static unsigned char * 2420 asm_imul_r(struct Inst *inst, struct Code *code) 2421 { 2422 enum RegSize size = inst->opr[0].u.reg.size; 2423 unsigned char *p = code->buf; 2424 short buf[] = { 2425 MAKE_REX0(size, 0, opr_regno(&inst->opr[0].u.reg), 0xf6 | (size == REG8 ? 0 : 1)), 2426 0xe8 | inst->opr[0].u.reg.no, 2427 }; 2428 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2429 return p; 2430 } 2431 2432 /* two-operand imul: 0f af /r, where reg is the destination (unlike 2433 * mov/add, where it is the source). MAKE_REX0 wants the reg-field 2434 * operand in the middle argument, so dno goes first, as in asm_bsf 2435 * below */ 2436 static unsigned char * 2437 asm_imul_rr(struct Inst *inst, struct Code *code) 2438 { 2439 enum RegSize size = inst->opr[0].u.reg.size; 2440 unsigned char *p = code->buf; 2441 int sno = opr_regno(&inst->opr[0].u.reg); 2442 int dno = opr_regno(&inst->opr[1].u.reg); 2443 short buf[] = { 2444 MAKE_REX0(size, dno, sno, 0x0f), 2445 0xaf, 2446 0xc0 | ((dno & 7) << 3) | (sno & 7), 2447 }; 2448 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2449 return p; 2450 } 2451 2452 /* imul $imm, %reg: 6b/69 /r ib/id (reg used as both src and dst) */ 2453 static unsigned char * 2454 asm_imul_irr(struct Inst *inst, struct Code *code) 2455 { 2456 enum RegSize size = inst->opr[1].u.reg.size; 2457 int dno = opr_regno(&inst->opr[1].u.reg); 2458 long value = inst->opr[0].u.immediate; 2459 int im8 = is_im8(value); 2460 short buf[] = { 2461 MAKE_REX0(size, dno, dno, im8 ? 0x6b : 0x69), 2462 0xc0 | ((dno & 7) << 3) | (dno & 7), 2463 }; 2464 unsigned char *p = code->buf; 2465 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2466 if (im8) { 2467 *p++ = IM8(value); 2468 } else { 2469 PUT_CODE(p, IM32(value)); 2470 p += 4; 2471 } 2472 return p; 2473 } 2474 2475 static unsigned char * 2476 asm_leave(struct Inst *inst, struct Code *code) 2477 { 2478 MAKE_CODE(inst, code, 0xc9); 2479 return code->buf; 2480 } 2481 2482 static unsigned char * 2483 asm_enter(struct Inst *inst, struct Code *code) 2484 { 2485 long size = inst->opr[0].u.immediate; 2486 long depth = inst->opr[1].u.immediate; 2487 MAKE_CODE(inst, code, 0xc8, IM16(size), IM8(depth)); 2488 return code->buf; 2489 } 2490 2491 /* xchg: 86/87 /r */ 2492 static unsigned char * 2493 asm_xchg_rr(struct Inst *inst, struct Code *code) 2494 { 2495 enum RegSize size = inst->opr[0].u.reg.size; 2496 unsigned char *p = code->buf; 2497 p = put_rex2( 2498 p, 2499 size, 2500 opr_regno(&inst->opr[0].u.reg), 2501 opr_regno(&inst->opr[1].u.reg), 2502 size == REG8 ? 0x86 : 0x87 2503 ); 2504 return p; 2505 } 2506 2507 /* xchg reg, (mem): a memory operand always locks implicitly, needed 2508 * for the atomic runtime's __atomic_exchange_N */ 2509 static unsigned char * 2510 asm_xchg_ri(struct Inst *inst, struct Code *code) 2511 { 2512 return assemble_reg_to_mem(inst, code, 0, 0x86, 0x87); 2513 } 2514 2515 /* bsf: 0f bc /r */ 2516 static unsigned char * 2517 asm_bsf(struct Inst *inst, struct Code *code) 2518 { 2519 enum RegSize size = inst->opr[0].u.reg.size; 2520 int sno = opr_regno(&inst->opr[0].u.reg); 2521 int dno = opr_regno(&inst->opr[1].u.reg); 2522 short buf[] = { 2523 MAKE_REX0(size, dno, sno, 0x0f), 2524 0xbc, 2525 0xc0 | ((dno & 7) << 3) | (sno & 7), 2526 }; 2527 unsigned char *p = code->buf; 2528 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2529 return p; 2530 } 2531 2532 /* bswap: 0f c8+r (32/64 only) */ 2533 static unsigned char * 2534 asm_bswap(struct Inst *inst, struct Code *code) 2535 { 2536 enum RegSize size = inst->opr[0].u.reg.size; 2537 int dno = opr_regno(&inst->opr[0].u.reg); 2538 if (size != REG32 && size != REG64) 2539 return NULL; 2540 short buf[] = { 2541 MAKE_REX0(size, 0, dno, 0x0f), 2542 0xc8 | (dno & 7), 2543 }; 2544 unsigned char *p = code->buf; 2545 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2546 return p; 2547 } 2548 2549 /* xadd: 0f c0(8bit)/c1(16+32+64) /r */ 2550 static unsigned char * 2551 asm_xadd_rr(struct Inst *inst, struct Code *code) 2552 { 2553 enum RegSize size = inst->opr[0].u.reg.size; 2554 int sno = opr_regno(&inst->opr[0].u.reg); 2555 int dno = opr_regno(&inst->opr[1].u.reg); 2556 short buf[] = { 2557 MAKE_REX0(size, sno, dno, 0x0f), 2558 size == REG8 ? 0xc0 : 0xc1, 2559 0xc0 | ((sno & 7) << 3) | (dno & 7), 2560 }; 2561 unsigned char *p = code->buf; 2562 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2563 return p; 2564 } 2565 2566 /* cmpxchg: 0f b0(8bit)/b1(16+32+64) /r */ 2567 static unsigned char * 2568 asm_cmpxchg_rr(struct Inst *inst, struct Code *code) 2569 { 2570 enum RegSize size = inst->opr[0].u.reg.size; 2571 int sno = opr_regno(&inst->opr[0].u.reg); 2572 int dno = opr_regno(&inst->opr[1].u.reg); 2573 short buf[] = { 2574 MAKE_REX0(size, sno, dno, 0x0f), 2575 size == REG8 ? 0xb0 : 0xb1, 2576 0xc0 | ((sno & 7) << 3) | (dno & 7), 2577 }; 2578 unsigned char *p = code->buf; 2579 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2580 return p; 2581 } 2582 2583 /* xadd/cmpxchg reg, (mem): 0f escape byte ahead of the real (r/m,r) 2584 * opcode pair, otherwise identical in shape to mov_ri. needed for the 2585 * atomic runtime's __atomic_fetch_add_N/__atomic_compare_exchange_N, 2586 * xadd also for __atomic_fetch_sub_N (negate then xadd) */ 2587 static unsigned char * 2588 asm_xadd_ri(struct Inst *inst, struct Code *code) 2589 { 2590 return assemble_reg_to_mem(inst, code, 1, 0xc0, 0xc1); 2591 } 2592 2593 static unsigned char * 2594 asm_cmpxchg_ri(struct Inst *inst, struct Code *code) 2595 { 2596 return assemble_reg_to_mem(inst, code, 1, 0xb0, 0xb1); 2597 } 2598 2599 /* bt family: register form 0f [a3|ab|b3|bb] /r, 2600 * immediate form 0f ba /[4|5|6|7] ib */ 2601 static unsigned char * 2602 asm_bt_family(struct Inst *inst, struct Code *code) 2603 { 2604 int rr_opcodes[] = { 2605 [BT_RR - BT_RR] = 0xa3, 2606 [BTS_RR - BT_RR] = 0xab, 2607 [BTR_RR - BT_RR] = 0xb3, 2608 [BTC_RR - BT_RR] = 0xbb 2609 }; 2610 int imm_digits[] = { 2611 [BT_RR - BT_RR] = 4, [BTS_RR - BT_RR] = 5, [BTR_RR - BT_RR] = 6, [BTC_RR - BT_RR] = 7 2612 }; 2613 int idx = inst->op - BT_RR; 2614 2615 if (inst->opr[0].type == IMMEDIATE) { 2616 enum RegSize size = inst->opr[1].u.reg.size; 2617 int dno = opr_regno(&inst->opr[1].u.reg); 2618 long value = inst->opr[0].u.immediate; 2619 short buf[] = { 2620 MAKE_REX0(size, imm_digits[idx], dno, 0x0f), 2621 0xba, 2622 0xc0 | (imm_digits[idx] << 3) | (dno & 7), 2623 value & 0xff, 2624 }; 2625 unsigned char *p = code->buf; 2626 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2627 return p; 2628 } 2629 2630 enum RegSize size = inst->opr[0].u.reg.size; 2631 int sno = opr_regno(&inst->opr[0].u.reg); 2632 int dno = opr_regno(&inst->opr[1].u.reg); 2633 short buf[] = { 2634 MAKE_REX0(size, sno, dno, 0x0f), 2635 rr_opcodes[idx], 2636 0xc0 | ((sno & 7) << 3) | (dno & 7), 2637 }; 2638 unsigned char *p = code->buf; 2639 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2640 return p; 2641 } 2642 2643 /* cmovcc: 0f 40+cc /r */ 2644 static unsigned char * 2645 asm_cmov_rr(struct Inst *inst, struct Code *code) 2646 { 2647 enum RegSize size = inst->opr[0].u.reg.size; 2648 int sno = opr_regno(&inst->opr[0].u.reg); 2649 int dno = opr_regno(&inst->opr[1].u.reg); 2650 short buf[] = { 2651 MAKE_REX0(size, sno, dno, 0x0f), 2652 0x40 + (inst->op - CMOVO), 2653 0xc0 | ((sno & 7) << 3) | (dno & 7), 2654 }; 2655 unsigned char *p = code->buf; 2656 p = put_code_filtered(p, buf, ARRAY_SIZE(buf)); 2657 return p; 2658 } 2659 2660 /* string operations: fixed encoding by size */ 2661 static unsigned char * 2662 asm_string(struct Inst *inst, struct Code *code) 2663 { 2664 /* base opcodes: movs=0xa4, stos=0xaa, lods=0xac, scas=0xae, cmps=0xa6 2665 * the byte form has opcode x, word/dword/qword form has opcode X+1 2666 * the table below stores the byte-form opcode for each group */ 2667 static const unsigned char base[] = { 2668 [MOVSB - MOVSB] = 0xa4, 2669 [STOSB - MOVSB] = 0xaa, 2670 [LODSB - MOVSB] = 0xac, 2671 [SCASB - MOVSB] = 0xae, 2672 [CMPSB - MOVSB] = 0xa6, 2673 }; 2674 int group_idx = (inst->op - MOVSB) / 4; 2675 int size_idx = (inst->op - MOVSB) % 4; /* 0=byte, 1=word, 2=long, 3=quad */ 2676 2677 if (group_idx > 4) 2678 return NULL; 2679 2680 unsigned char opcode = base[group_idx * 4]; 2681 2682 switch (size_idx) { 2683 case 0: /* byte */ 2684 MAKE_CODE(inst, code, opcode); 2685 break; 2686 case 1: /* word */ 2687 MAKE_CODE(inst, code, 0x66, opcode + 1); 2688 break; 2689 case 2: /* long */ 2690 MAKE_CODE(inst, code, opcode + 1); 2691 break; 2692 case 3: /* quad */ 2693 MAKE_CODE(inst, code, 0x48, opcode + 1); 2694 break; 2695 } 2696 return code->buf; 2697 } 2698 2699 /* gas intel-syntax conversion aliases */ 2700 static unsigned char * 2701 asm_cbw(struct Inst *inst, struct Code *code) 2702 { 2703 MAKE_CODE(inst, code, 0x66, 0x98); 2704 return code->buf; 2705 } 2706 2707 static unsigned char * 2708 asm_cdqe(struct Inst *inst, struct Code *code) 2709 { 2710 MAKE_CODE(inst, code, 0x48, 0x98); 2711 return code->buf; 2712 } 2713 2714 static unsigned char * 2715 asm_cwd(struct Inst *inst, struct Code *code) 2716 { 2717 MAKE_CODE(inst, code, 0x66, 0x99); 2718 return code->buf; 2719 } 2720 2721 /*// //////////////////////////////////////////// */ 2722 2723 typedef unsigned char *(*AsmInstFunc)(struct Inst *inst, struct Code *code); 2724 2725 static const AsmInstFunc table[] = { 2726 [NOOP] = asm_noop, 2727 [MOV_RR] = asm_mov_rr, 2728 [MOV_IMR] = asm_mov_imr, 2729 [MOV_IR] = asm_mov_ir, 2730 [MOV_RI] = asm_mov_ri, 2731 [MOV_IIR] = asm_mov_iir, 2732 [MOV_DR] = asm_mov_dr, 2733 [MOV_RD] = asm_mov_rd, 2734 [MOV_SR] = asm_mov_sr, 2735 [MOVB_IMI] = asm_movbwlq_imi, 2736 [MOVW_IMI] = asm_movbwlq_imi, 2737 [MOVL_IMI] = asm_movbwlq_imi, 2738 [MOVQ_IMI] = asm_movbwlq_imi, 2739 [MOVB_IMD] = asm_movbwlq_imd, 2740 [MOVW_IMD] = asm_movbwlq_imd, 2741 [MOVL_IMD] = asm_movbwlq_imd, 2742 [MOVQ_IMD] = asm_movbwlq_imd, 2743 [MOVSX] = asm_movszx_rr, 2744 [MOVZX] = asm_movszx_rr, 2745 [LEA_IR] = asm_lea_ir, 2746 [LEA_IIR] = asm_lea_iir, 2747 [ADD_RR] = asm_add_rr, 2748 [ADD_IMR] = asm_add_imr, 2749 [ADD_IR] = asm_add_ir, 2750 [ADD_IIR] = asm_add_iir, 2751 [ADDQ] = asm_addq_imi, 2752 [SUB_RR] = asm_sub_rr, 2753 [SUB_IMR] = asm_sub_imr, 2754 [SUB_IR] = asm_sub_ir, 2755 [SUB_IIR] = asm_sub_iir, 2756 [SUBQ] = asm_subq_imi, 2757 [MUL] = asm_mul_r, 2758 [DIV] = asm_div_r, 2759 [IDIV] = asm_idiv_r, 2760 [NEG] = asm_neg_r, 2761 [NOT] = asm_not_r, 2762 [INC] = asm_inc_r, 2763 [INCB] = asm_incbwlq_i, 2764 [INCW] = asm_incbwlq_i, 2765 [INCL] = asm_incbwlq_i, 2766 [INCQ] = asm_incbwlq_i, 2767 [DEC] = asm_dec_r, 2768 [DECB] = asm_decbwlq_i, 2769 [DECW] = asm_decbwlq_i, 2770 [DECL] = asm_decbwlq_i, 2771 [DECQ] = asm_decbwlq_i, 2772 [AND_RR] = asm_and_rr, 2773 [AND_IMR] = asm_and_imr, 2774 [OR_RR] = asm_or_rr, 2775 [OR_IMR] = asm_or_imr, 2776 [XOR_RR] = asm_xor_rr, 2777 [XOR_IMR] = asm_xor_imr, 2778 [SHL_RR] = asm_shl_rr, 2779 [SHL_IMR] = asm_shl_imr, 2780 [SHR_RR] = asm_shr_rr, 2781 [SHR_IMR] = asm_shr_imr, 2782 [SAR_RR] = asm_sar_rr, 2783 [SAR_IMR] = asm_sar_imr, 2784 [CMP_RR] = asm_cmp_rr, 2785 [CMP_IMR] = asm_cmp_imr, 2786 [TEST] = asm_test_rr, 2787 [CWTL] = asm_cwtl, 2788 [CLTD] = asm_cltd, 2789 [CQTO] = asm_cqto, 2790 [BSR] = asm_bsr, 2791 [LZCNT] = asm_tzcnt, 2792 [TZCNT] = asm_tzcnt, 2793 [POPCNT] = asm_popcnt, 2794 [SETO] = asm_set_r, 2795 [SETNO] = asm_set_r, 2796 [SETB] = asm_set_r, 2797 [SETAE] = asm_set_r, 2798 [SETE] = asm_set_r, 2799 [SETNE] = asm_set_r, 2800 [SETBE] = asm_set_r, 2801 [SETA] = asm_set_r, 2802 [SETS] = asm_set_r, 2803 [SETNS] = asm_set_r, 2804 [SETP] = asm_set_r, 2805 [SETNP] = asm_set_r, 2806 [SETL] = asm_set_r, 2807 [SETGE] = asm_set_r, 2808 [SETLE] = asm_set_r, 2809 [SETG] = asm_set_r, 2810 [JMP_D] = asm_jmp_d, 2811 [JMP_DER] = asm_jmp_der, 2812 [JMP_DEI] = asm_jmp_dei, 2813 [JMP_DEII] = asm_jmp_deii, 2814 [JO] = asm_jxx_d, 2815 [JNO] = asm_jxx_d, 2816 [JB] = asm_jxx_d, 2817 [JAE] = asm_jxx_d, 2818 [JE] = asm_jxx_d, 2819 [JNE] = asm_jxx_d, 2820 [JBE] = asm_jxx_d, 2821 [JA] = asm_jxx_d, 2822 [JS] = asm_jxx_d, 2823 [JNS] = asm_jxx_d, 2824 [JP] = asm_jxx_d, 2825 [JNP] = asm_jxx_d, 2826 [JL] = asm_jxx_d, 2827 [JGE] = asm_jxx_d, 2828 [JLE] = asm_jxx_d, 2829 [JG] = asm_jxx_d, 2830 [CALL_D] = asm_call_d, 2831 [CALL_DER] = asm_call_der, 2832 [RET] = asm_ret, 2833 [PUSH_R] = asm_push_r, 2834 [PUSH_IM] = asm_push_im, 2835 [POP] = asm_pop_r, 2836 [INT] = asm_int_im, 2837 [SYSCALL] = asm_syscall, 2838 2839 [MOVSD_XX] = asm_movsd_xx, 2840 [MOVSD_IX] = asm_movsd_ix, 2841 [MOVSD_XI] = asm_movsd_xi, 2842 [MOVSS_XX] = asm_movss_xx, 2843 [MOVSS_IX] = asm_movss_ix, 2844 [MOVSS_XI] = asm_movss_xi, 2845 [ADDSD] = asm_addsd_xx, 2846 [ADDSS] = asm_addss_xx, 2847 [SUBSD] = asm_subsd_xx, 2848 [SUBSS] = asm_subss_xx, 2849 [MULSD] = asm_mulsd_xx, 2850 [MULSS] = asm_mulss_xx, 2851 [DIVSD] = asm_divsd_xx, 2852 [DIVSS] = asm_divss_xx, 2853 [XORPD] = asm_xorpd_xx, 2854 [XORPD_IX] = asm_xorpd_ix, 2855 [XORPS] = asm_xorps_xx, 2856 [XORPS_IX] = asm_xorps_ix, 2857 [COMISD] = asm_comisd_xx, 2858 [COMISD_IX] = asm_comisd_ix, 2859 [UCOMISD] = asm_ucomisd_xx, 2860 [UCOMISD_IX] = asm_ucomisd_ix, 2861 [COMISS] = asm_comiss_xx, 2862 [COMISS_IX] = asm_comiss_ix, 2863 [UCOMISS] = asm_ucomiss_xx, 2864 [UCOMISS_IX] = asm_ucomiss_ix, 2865 [CVTSI2SD] = asm_cvtsi2sd_rx, 2866 [CVTSI2SS] = asm_cvtsi2ss_rx, 2867 [CVTTSD2SI] = asm_cvttsd2si_xr, 2868 [CVTTSS2SI] = asm_cvttss2si_xr, 2869 [CVTSD2SS] = asm_cvtsd2ss_xx, 2870 [CVTSS2SD] = asm_cvtss2sd_xx, 2871 [SQRTSD] = asm_sqrtsd_xx, 2872 [MOVQ_GP_XMM] = asm_movq_gpxmm, 2873 [MOVQ_XMM_GP] = asm_movq_xmmgp, 2874 [MOVQ_MEM_XMM] = asm_movq_ixmm, 2875 [MOVQ_XMM_MEM] = asm_movq_xmmi, 2876 [ENDBR64] = asm_endbr64, 2877 2878 /* no-operand instructions */ 2879 [NOP] = asm_simple, 2880 [PUSHFQ] = asm_simple, 2881 [POPFQ] = asm_simple, 2882 [PUSHF] = asm_simple, 2883 [POPF] = asm_simple, 2884 [CPUID] = asm_simple, 2885 [RDTSC] = asm_simple, 2886 [UD2] = asm_simple, 2887 [PAUSE] = asm_simple, 2888 [HLT] = asm_simple, 2889 [CMC] = asm_simple, 2890 [CLC] = asm_simple, 2891 [STC] = asm_simple, 2892 [CLI] = asm_simple, 2893 [STI] = asm_simple, 2894 [CLD] = asm_simple, 2895 [STD] = asm_simple, 2896 [CLTS] = asm_simple, 2897 [WBINVD] = asm_simple, 2898 [MFENCE] = asm_simple, 2899 [LFENCE] = asm_simple, 2900 [SFENCE] = asm_simple, 2901 2902 /* register instructions */ 2903 [IMUL] = asm_imul_r, 2904 [IMUL_RR] = asm_imul_rr, 2905 [IMUL_IRR] = asm_imul_irr, 2906 [LEAVE] = asm_leave, 2907 [ENTER] = asm_enter, 2908 [XCHG_RR] = asm_xchg_rr, 2909 [XCHG_RI] = asm_xchg_ri, 2910 [BSF] = asm_bsf, 2911 [BSWAP] = asm_bswap, 2912 [XADD_RR] = asm_xadd_rr, 2913 [XADD_RI] = asm_xadd_ri, 2914 [CMPXCHG_RR] = asm_cmpxchg_rr, 2915 [CMPXCHG_RI] = asm_cmpxchg_ri, 2916 2917 /* bit test */ 2918 [BT_RR] = asm_bt_family, 2919 [BTS_RR] = asm_bt_family, 2920 [BTR_RR] = asm_bt_family, 2921 [BTC_RR] = asm_bt_family, 2922 2923 /* conditional move */ 2924 [CMOVO] = asm_cmov_rr, 2925 [CMOVNO] = asm_cmov_rr, 2926 [CMOVB] = asm_cmov_rr, 2927 [CMOVAE] = asm_cmov_rr, 2928 [CMOVE] = asm_cmov_rr, 2929 [CMOVNE] = asm_cmov_rr, 2930 [CMOVBE] = asm_cmov_rr, 2931 [CMOVA] = asm_cmov_rr, 2932 [CMOVS] = asm_cmov_rr, 2933 [CMOVNS] = asm_cmov_rr, 2934 [CMOVP] = asm_cmov_rr, 2935 [CMOVNP] = asm_cmov_rr, 2936 [CMOVL] = asm_cmov_rr, 2937 [CMOVGE] = asm_cmov_rr, 2938 [CMOVLE] = asm_cmov_rr, 2939 [CMOVG] = asm_cmov_rr, 2940 2941 /* string operations */ 2942 [MOVSB] = asm_string, 2943 [MOVSW] = asm_string, 2944 [MOVSL] = asm_string, 2945 [MOVSQ] = asm_string, 2946 [STOSB] = asm_string, 2947 [STOSW] = asm_string, 2948 [STOSL] = asm_string, 2949 [STOSQ] = asm_string, 2950 [LODSB] = asm_string, 2951 [LODSW] = asm_string, 2952 [LODSL] = asm_string, 2953 [LODSQ] = asm_string, 2954 [SCASB] = asm_string, 2955 [SCASW] = asm_string, 2956 [SCASL] = asm_string, 2957 [SCASQ] = asm_string, 2958 [CMPSB] = asm_string, 2959 [CMPSW] = asm_string, 2960 [CMPSL] = asm_string, 2961 [CMPSQ] = asm_string, 2962 2963 /* gas conversion aliases */ 2964 [CBW] = asm_cbw, 2965 [CDQE] = asm_cdqe, 2966 [CWD] = asm_cwd, 2967 }; 2968 2969 void 2970 assemble_inst(struct Inst *inst, struct ParseInfo *info, struct Code *code) 2971 { 2972 code->flag = 0; 2973 code->len = 0; 2974 2975 unsigned char prefix = 0; 2976 switch (inst->prefix) { 2977 case 0: 2978 break; 2979 case PREFIX_LOCK: 2980 prefix = 0xf0; 2981 break; 2982 case PREFIX_REP: 2983 prefix = 0xf3; 2984 break; 2985 case PREFIX_REPNE: 2986 prefix = 0xf2; 2987 break; 2988 default: 2989 assemble_error(info, "invalid instruction prefix combination"); 2990 return; 2991 } 2992 2993 const AsmInstFunc *func = NULL; 2994 if (inst->op < (enum Opcode)ARRAY_SIZE(table)) { 2995 func = &table[inst->op]; 2996 2997 if (func != NULL) { 2998 unsigned char *p = (*func)(inst, code); 2999 if (p != NULL) { 3000 if (p == code->buf) 3001 p = code->buf + code->len; 3002 if (p > code->buf) { 3003 if (prefix != 0) { 3004 size_t len = p - code->buf; 3005 assert(len + 1 <= sizeof(code->buf)); 3006 memmove(code->buf + 1, code->buf, len); 3007 code->buf[0] = prefix; 3008 p = code->buf + 1 + len; 3009 } 3010 code->inst = inst; 3011 code->len = p - code->buf; 3012 assert((size_t)code->len <= sizeof(code->buf)); 3013 } 3014 return; 3015 } 3016 } 3017 } 3018 3019 assemble_error(info, "Illegal operand"); 3020 }