2 * alchemist - code generation library
3 * Copyright (C) 2008 Robbert Haarman
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "alchemist/x86.h"
26 * \section concepts Concepts
28 * This library is used to emit <em>instructions</em>.
30 * Instructions consist of an <em>opcode</em> and
31 * zero, one, or two <em>arguments</em>.
33 * Arguments can be <em>immediate values</em>,
34 * <em>registers</em>, or <em>memory locations</em>.
36 * An immediate value can be 8, 16, or 32 bits (immediate
37 * values of these sizes are abbreviated <em>imm8</em>,
38 * <em>imm16</em> and <em>imm32</em>, respectively).
39 * Whichever the size of the immediate value, its value is
40 * always given as an int in the API.
42 * Registers are specified using integers. The
43 * CG_X86_REG_* macros can be used to designate specific
44 * registers (the name of the register is used in place of
45 * the *). In the emitted instructions, registers are
46 * designated by their code, which can be obtained by
47 * using the int used by this API as an index in the
48 * array register_code.
50 * Memory addresses are specified using a base, a scale
51 * factor, and index, and a displacement. Base and
52 * index are registers, whereas scale factor and displacement
53 * are integers. The scale factor must be 0, 1, 2, 4, or 8.
54 * The actual address is given by
55 * value_of_base_register + scale_factor * value_of_index_register
59 /** Allocate memory for a value.
60 * Allocates memory for a single value of a given type.
61 * @param T The type of value to allocate memory for.
62 * @return A pointer to the newly allocated memory, or NULL on error.
64 #define NEW(T) (T*) my_malloc(sizeof(T))
67 #define INVALID_INSTRUCTION { error = CG_X86_ERR_INVALID_INSTRUCTION; return -1; }
68 #define INVALID_SCALE_FACTOR { error = CG_X86_ERR_INVALID_SCALE_FACTOR; return -1; }
70 #define EMIT_BYTE(X) { if(size > n) str[n] = (BYTE) (X); n++; }
71 #define EMIT_WORD(X) { EMIT_BYTE((X) & 0xff); EMIT_BYTE(((X) >> 8) & 0xff); }
72 #define EMIT_DWORD(X) { EMIT_BYTE((X) & 0xff); EMIT_BYTE(((X) >> 8) & 0xff); \
73 EMIT_BYTE(((X) >> 16) & 0xff); EMIT_BYTE(((X) >> 24) & 0xff); }
74 #define EMIT_MR(X, Y) { m = cg_x86_emit_mem_or_reg_arg(str + n, size - n, X, Y); \
75 if(m < 0) return m; else n += m; }
76 #define EMIT_REG_MODRM(REG, SPARE) { m = cg_x86_emit_reg_modrm(str + n, size - n, REG, SPARE); \
77 if(m < 0) return m; else n+= m; }
78 #define EMIT_MEM_MODRM(BASE, SCALE, INDEX, DISPLACEMENT, SPARE) {\
79 m = cg_x86_emit_mem_modrm(str + n, size - n, BASE, SCALE, INDEX, DISPLACEMENT, SPARE); \
80 if(m < 0) return m; else n+= m; }
82 #define EMIT_JCC16(CC) cg_x86_emit_jcc16(str, size, CC, imm);
83 #define EMIT_JCC32(CC) cg_x86_emit_jcc32(str, size, CC, imm);
84 #define EMIT_OP_IMM16(OP) cg_x86_emit_op_imm16(str, size, OP, imm)
85 #define EMIT_OP_IMM16_IMM16(OP) cg_x86_emit_op_imm16_imm16(str, size, OP, imma, immb)
86 #define EMIT_OP_IMM16_IMM32(OP) cg_x86_emit_op_imm16_imm32(str, size, OP, imma, immb)
87 #define EMIT_OP_IMM32(OP) cg_x86_emit_op_imm32(str, size, OP, imm)
88 #define EMIT_OP_IMM8(OP) cg_x86_emit_op_imm8(str, size, OP, imm)
89 #define EMIT_OP_MEM_REG16(OP) cg_x86_emit_op_mem_reg16(str, size, OP, base, scale, index, displacement, reg)
90 #define EMIT_OP_MEM_REG32(OP) cg_x86_emit_op_mem_reg32(str, size, OP, base, scale, index, displacement, reg)
91 #define EMIT_OP_MEM_REG8(OP) cg_x86_emit_op_mem_reg8(str, size, OP, base, scale, index, displacement, reg)
92 #define EMIT_OP_REG_MEM16(OP) cg_x86_emit_op_mem_reg16(str, size, OP, base, scale, index, displacement, reg)
93 #define EMIT_OP_REG_MEM32(OP) cg_x86_emit_op_mem_reg32(str, size, OP, base, scale, index, displacement, reg)
94 #define EMIT_OP_REG_MEM8(OP) cg_x86_emit_op_mem_reg8(str, size, OP, base, scale, index, displacement, reg)
95 #define EMIT_OP_REG_REG16(OP) cg_x86_emit_op_reg_reg16(str, size, OP, rega, regb)
96 #define EMIT_OP_REG_REG32(OP) cg_x86_emit_op_reg_reg32(str, size, OP, rega, regb)
97 #define EMIT_OP_REG_REG8(OP) cg_x86_emit_op_reg_reg8(str, size, OP, rega, regb)
98 #define EMIT_OP_SPARE_MEM16(OP, SPARE) cg_x86_emit_op_spare_mem16(str, size, OP, SPARE, base, scale, index, displacement)
99 #define EMIT_OP_SPARE_MEM32(OP, SPARE) cg_x86_emit_op_spare_mem32(str, size, OP, SPARE, base, scale, index, displacement)
100 #define EMIT_OP_SPARE_MEM8(OP, SPARE) cg_x86_emit_op_spare_mem8(str, size, OP, SPARE, base, scale, index, displacement)
101 #define EMIT_OP_SPARE_MEM_IMM16(OP, SPARE) cg_x86_emit_op_spare_mem_imm16(str, size, OP, SPARE, base, scale, index, displacement, imm)
102 #define EMIT_OP_SPARE_MEM_IMM32(OP, SPARE) cg_x86_emit_op_spare_mem_imm32(str, size, OP, SPARE, base, scale, index, displacement, imm)
103 #define EMIT_OP_SPARE_MEM_IMM8(OP, SPARE) cg_x86_emit_op_spare_mem_imm8(str, size, OP, SPARE, base, scale, index, displacement, imm)
104 #define EMIT_OP_SPARE_REG16(OP, SPARE) cg_x86_emit_op_spare_reg16(str, size, OP, SPARE, reg)
105 #define EMIT_OP_SPARE_REG32(OP, SPARE) cg_x86_emit_op_spare_reg32(str, size, OP, SPARE, reg)
106 #define EMIT_OP_SPARE_REG8(OP, SPARE) cg_x86_emit_op_spare_reg8(str, size, OP, SPARE, reg)
107 #define EMIT_OP_SPARE_REG_IMM16(OP, SPARE) cg_x86_emit_op_spare_reg_imm16(str, size, OP, SPARE, reg, imm)
108 #define EMIT_OP_SPARE_REG_IMM32(OP, SPARE) cg_x86_emit_op_spare_reg_imm32(str, size, OP, SPARE, reg, imm)
109 #define EMIT_OP_SPARE_REG_IMM8(OP, SPARE) cg_x86_emit_op_spare_reg_imm8(str, size, OP, SPARE, reg, imm)
111 #define INSTR16 if(mode == CG_X86_MODE_32BIT) EMIT_BYTE('\x66')
112 #define INSTR32 if(mode == CG_X86_MODE_16BIT) EMIT_BYTE('\x66')
113 #define ADDRESS16 if(mode == CG_X86_MODE_32BIT) EMIT_BYTE('\x67')
114 #define ADDRESS32 if(mode == CG_X86_MODE_16BIT) EMIT_BYTE('\x67')
116 static char *error
= NULL
;
117 static int mode
= CG_X86_MODE_32BIT
;
119 /** Table translating CG_X86_OP_* macros to operation names. */
120 char *cg_x86_opcodes
[] = {
217 /** Table translating CG_X86_REG_* macros to register names. */
218 char *cg_x86_registers
[] = {
246 /** Table translating CG_X86_REG_* macros to register codes. */
247 static int register_code
[] = {
275 /** Get the last error. */
276 char *cg_x86_get_last_error() {
280 /** Emits a modrm parameter that refers to a memory location.
281 * @param str The buffer to store the modrm in.
282 * @param size The size of the buffer.
283 * @param base The register that holds the base of the memory location.
284 * @param scale The scale factor (must be 0, 1, 2, 4, or 8).
285 * @param index The register that holds the index (ignored if scale == 0).
286 * @param displacement The displacement of the memory location.
287 * @param spare The value to store in the spare bits of the modrm.
289 * @return The number of bytes needed to store the modrm.
291 int cg_x86_emit_mem_modrm(char *str
, size_t size
, int base
, int scale
, int index
, int displacement
, int spare
) {
293 int n
= 0, address_mode
= 0;
297 /* Calculate the size of the displacement we need to use. */
298 if(displacement
== 0) {
299 /* No displacement. */
301 modrm
|= 0x40; /* Put one anyway... */
302 } else if((displacement
& 0xffffff00) == 0) {
303 /* byte displacement. */
305 } else if((displacement
& 0xffff0000) == 0) {
306 /* word displacement. */
310 /* dword displacement. */
315 /* Calculate SIB byte. */
316 /* Scale must be 1, 2, 4, or 8 */
332 /* No other values of scale are allowed. */
337 /* If scale == 0, we don't have an index. In that case,
338 * the index code is 4 (so we or with 4 << 3, which is 0x20). */
339 if(scale
== 0) sib
|= 0x20;
341 /* ESP cannot be used as an index. */
342 if(index
== CG_X86_REG_ESP
) INVALID_INSTRUCTION
343 sib
|= register_code
[index
] << 3;
347 sib
|= register_code
[base
];
349 /* For now, we only support addressing using the SIB byte.
350 That also means we must use 32-bit mode. */
355 switch(modrm
& 0xc0) {
359 EMIT_BYTE(displacement
);
362 EMIT_DWORD(displacement
);
372 /** Emits a modrm that refers to a register.
373 * @param str The buffer to store the emitted code in.
374 * @param size The size of the buffer.
375 * @param reg The register to encode in the modrm.
376 * @param spare The value to encode in the spare of the modrm.
378 * @return The number of bytes needed to store the emitted modrm.
380 int cg_x86_emit_reg_modrm(char *str
, size_t size
, int reg
, int spare
) {
382 EMIT_BYTE(0xc0 | (spare
<< 3) | register_code
[reg
]);
386 /** Emits a conditional jump instruction with a 16-bit offset.
387 * @param str The buffer to emit the instruction to.
388 * @param size The size of the buffer.
389 * @param cc The condition code.
390 * @param imm The offset.
391 * @return The number of bytes needed to store the instruction.
393 int cg_x86_emit_jcc16(char *str
, size_t size
, int cc
, int imm
) {
397 EMIT_BYTE(0x80 + cc
);
402 /** Emits a conditional jump instruction with a 32-bit offset.
403 * @param str The buffer to emit the instruction to.
404 * @param size The size of the buffer.
405 * @param cc The condition code.
406 * @param imm The offset.
407 * @return The number of bytes needed to store the instruction.
409 int cg_x86_emit_jcc32(char *str
, size_t size
, int cc
, int imm
) {
413 EMIT_BYTE(0x80 + cc
);
418 /** Emits an instruction consisting of an opcode and a 16-bit immediate argument.
419 * @param str The buffer to emit the instruction to.
420 * @param size The size of the buffer.
421 * @param op The opcode.
422 * @param imm The immediate value.
423 * @return The number of bytes needed to store the instruction.
425 int cg_x86_emit_op_imm16(char *str
, size_t size
, char op
, int imm
) {
433 /** Emits an instruction consisting of an opcode and two 16-bit immediate arguments.
434 * @param str The buffer to emit the instruction to.
435 * @param size The size of the buffer.
436 * @param op The opcode.
437 * @param imma The first immediate value.
438 * @param immb The second immediate value.
439 * @return The number of bytes needed to store the instruction.
441 int cg_x86_emit_op_imm16_imm16(char *str
, size_t size
, char op
, int imma
, int immb
) {
450 /** Emits an instruction consisting of an opcode, one 16-bit immediate argument,
451 * and one 32-bit immediate argument.
452 * @param str The buffer to emit the instruction to.
453 * @param size The size of the buffer.
454 * @param op The opcode.
455 * @param imma The 16-bit immediate value.
456 * @param immb The 32-bit immediate value.
457 * @return The number of bytes needed to store the instruction.
459 int cg_x86_emit_op_imm16_imm32(char *str
, size_t size
, char op
, int imma
, int immb
) {
468 /** Emits an instruction consisting of an opcode and a 32-bit immediate argument.
469 * @param str The buffer to emit the instruction to.
470 * @param size The size of the buffer.
471 * @param op The opcode.
472 * @param imm The immediate value.
473 * @return The number of bytes needed to store the instruction.
475 int cg_x86_emit_op_imm32(char *str
, size_t size
, char op
, int imm
) {
483 /** Emits an instruction consisting of an opcode and a 8-bit immediate argument.
484 * @param str The buffer to emit the instruction to.
485 * @param size The size of the buffer.
486 * @param op The opcode.
487 * @param imm The immediate value.
488 * @return The number of bytes needed to store the instruction.
490 int cg_x86_emit_op_imm8(char *str
, size_t size
, char op
, int imm
) {
497 /** Emits an instruction consisting of an opcode and a modrm encoding a memory
498 * address and a 16-bit register.
499 * @param str The buffer to emit the instruction to.
500 * @param size The size of the buffer.
501 * @param op The opcode.
502 * @param base The base of the memory address.
503 * @param scale The scale of the memory address.
504 * @param index The index of the memory address.
505 * @param displacement The displacement of the memory address.
506 * @param reg The register.
507 * @return The number of bytes needed to store the instruction.
509 int cg_x86_emit_op_mem_reg16(char *str
, size_t size
, char op
, int base
, int scale
, int index
, int displacement
, int reg
) {
513 EMIT_MEM_MODRM(base
, scale
, index
, displacement
, register_code
[reg
]);
517 /** Emits an instruction consisting of an opcode and a modrm encoding a memory
518 * address and a 32-bit register.
519 * @param str The buffer to emit the instruction to.
520 * @param size The size of the buffer.
521 * @param op The opcode.
522 * @param base The base of the memory address.
523 * @param scale The scale of the memory address.
524 * @param index The index of the memory address.
525 * @param displacement The displacement of the memory address.
526 * @param reg The register.
527 * @return The number of bytes needed to store the instruction.
529 int cg_x86_emit_op_mem_reg32(char *str
, size_t size
, char op
, int base
, int scale
, int index
, int displacement
, int reg
) {
533 EMIT_MEM_MODRM(base
, scale
, index
, displacement
, register_code
[reg
]);
537 /** Emits an instruction consisting of an opcode and a modrm encoding a memory
538 * address and an 8-bit register.
539 * @param str The buffer to emit the instruction to.
540 * @param size The size of the buffer.
541 * @param op The opcode.
542 * @param base The base of the memory address.
543 * @param scale The scale of the memory address.
544 * @param index The index of the memory address.
545 * @param displacement The displacement of the memory address.
546 * @param reg The register.
547 * @return The number of bytes needed to store the instruction.
549 int cg_x86_emit_op_mem_reg8(char *str
, size_t size
, char op
, int base
, int scale
, int index
, int displacement
, int reg
) {
552 EMIT_MEM_MODRM(base
, scale
, index
, displacement
, register_code
[reg
]);
556 /** Emits an instruction consisting of an opcode and a modrm encoding two 16-bit registers.
557 * @param str The buffer to emit the instruction to.
558 * @param size The size of the buffer.
559 * @param op The opcode.
560 * @param rega The first register.
561 * @param regb The second register.
562 * @return The number of bytes needed to store the instruction.
564 int cg_x86_emit_op_reg_reg16(char *str
, size_t size
, char op
, int rega
, int regb
) {
568 EMIT_REG_MODRM(regb
, register_code
[rega
]);
572 /** Emits an instruction consisting of an opcode and a modrm encoding two 32-bit registers.
573 * @param str The buffer to emit the instruction to.
574 * @param size The size of the buffer.
575 * @param op The opcode.
576 * @param rega The first register.
577 * @param regb The second register.
578 * @return The number of bytes needed to store the instruction.
580 int cg_x86_emit_op_reg_reg32(char *str
, size_t size
, char op
, int rega
, int regb
) {
584 EMIT_REG_MODRM(regb
, register_code
[rega
]);
588 /** Emits an instruction consisting of an opcode and a modrm encoding two 8-bit registers.
589 * @param str The buffer to emit the instruction to.
590 * @param size The size of the buffer.
591 * @param op The opcode.
592 * @param rega The first register.
593 * @param regb The second register.
594 * @return The number of bytes needed to store the instruction.
596 int cg_x86_emit_op_reg_reg8(char *str
, size_t size
, char op
, int rega
, int regb
) {
599 EMIT_REG_MODRM(regb
, register_code
[rega
]);
603 /** Emits an instruction consisting of an opcode, a spare, and a memory address
604 * referring to a 16-bit value.
605 * @param str The buffer to emit the instruction to.
606 * @param size The size of the buffer.
607 * @param op The opcode.
608 * @param spare The spare.
609 * @param base The base of the memory address.
610 * @param scale The scale of the memory address.
611 * @param index The index of the memory address.
612 * @param displacement The displacement of the memory address.
613 * @return The number of bytes needed to store the instruction.
615 int cg_x86_emit_op_spare_mem16(char *str
, size_t size
, char op
, int spare
, int base
, int scale
, int index
, int displacement
) {
619 EMIT_MEM_MODRM(base
, scale
, index
, displacement
, spare
);
623 /** Emits an instruction consisting of an opcode, a spare, and a memory address
624 * referring to a 32-bit value.
625 * @param str The buffer to emit the instruction to.
626 * @param size The size of the buffer.
627 * @param op The opcode.
628 * @param spare The spare.
629 * @param base The base of the memory address.
630 * @param scale The scale of the memory address.
631 * @param index The index of the memory address.
632 * @param displacement The displacement of the memory address.
633 * @return The number of bytes needed to store the instruction.
635 int cg_x86_emit_op_spare_mem32(char *str
, size_t size
, char op
, int spare
, int base
, int scale
, int index
, int displacement
) {
639 EMIT_MEM_MODRM(base
, scale
, index
, displacement
, spare
);
643 /** Emits an instruction consisting of an opcode, a spare, and a memory address
644 * referring to a 8-bit value.
645 * @param str The buffer to emit the instruction to.
646 * @param size The size of the buffer.
647 * @param op The opcode.
648 * @param spare The spare.
649 * @param base The base of the memory address.
650 * @param scale The scale of the memory address.
651 * @param index The index of the memory address.
652 * @param displacement The displacement of the memory address.
653 * @return The number of bytes needed to store the instruction.
655 int cg_x86_emit_op_spare_mem8(char *str
, size_t size
, char op
, int spare
, int base
, int scale
, int index
, int displacement
) {
658 EMIT_MEM_MODRM(base
, scale
, index
, displacement
, spare
);
662 /** Emits an instruction consisting of an opcode, a spare, and a 16-bit register.
663 * @param str The buffer to emit the instruction to.
664 * @param size The size of the buffer.
665 * @param op The opcode.
666 * @param spare The spare.
667 * @param reg The register.
668 * @return The number of bytes needed to store the instruction.
670 int cg_x86_emit_op_spare_reg16(char *str
, size_t size
, char op
, int spare
, int reg
) {
674 EMIT_REG_MODRM(reg
, spare
);
678 /** Emits an instruction consisting of an opcode, a spare, and a 32-bit register.
679 * @param str The buffer to emit the instruction to.
680 * @param size The size of the buffer.
681 * @param op The opcode.
682 * @param spare The spare.
683 * @param reg The register.
684 * @return The number of bytes needed to store the instruction.
686 int cg_x86_emit_op_spare_reg32(char *str
, size_t size
, char op
, int spare
, int reg
) {
690 EMIT_REG_MODRM(reg
, spare
);
694 /** Emits an instruction consisting of an opcode, a spare, and a 8-bit register.
695 * @param str The buffer to emit the instruction to.
696 * @param size The size of the buffer.
697 * @param op The opcode.
698 * @param spare The spare.
699 * @param reg The register.
700 * @return The number of bytes needed to store the instruction.
702 int cg_x86_emit_op_spare_reg8(char *str
, size_t size
, char op
, int spare
, int reg
) {
705 EMIT_REG_MODRM(reg
, spare
);
709 /** Emits an instruction consisting of an opcode, a spare, a register,
710 * and a 16-bit immediate value.
711 * @param str The buffer to emit the instruction to.
712 * @param size The size of the buffer.
713 * @param op The opcode.
714 * @param spare The spare.
715 * @param reg The register.
716 * @param imm The immediate value.
717 * @return The number of bytes needed to store the instruction.
719 int cg_x86_emit_op_spare_reg_imm16(char *str
, size_t size
, char op
, int spare
, int reg
, int imm
) {
723 EMIT_REG_MODRM(reg
, spare
);
728 /** Emits an instruction consisting of an opcode, a spare, a register,
729 * and a 32-bit immediate value.
730 * @param str The buffer to emit the instruction to.
731 * @param size The size of the buffer.
732 * @param op The opcode.
733 * @param spare The spare.
734 * @param reg The register.
735 * @param imm The immediate value.
736 * @return The number of bytes needed to store the instruction.
738 int cg_x86_emit_op_spare_reg_imm32(char *str
, size_t size
, char op
, int spare
, int reg
, int imm
) {
742 EMIT_REG_MODRM(reg
, spare
);
747 /** Emits an instruction consisting of an opcode, a spare, a register,
748 * and an 8-bit immediate value.
749 * @param str The buffer to emit the instruction to.
750 * @param size The size of the buffer.
751 * @param op The opcode.
752 * @param spare The spare.
753 * @param reg The register.
754 * @param imm The immediate value.
755 * @return The number of bytes needed to store the instruction.
757 int cg_x86_emit_op_spare_reg_imm8(char *str
, size_t size
, char op
, int spare
, int reg
, int imm
) {
760 EMIT_REG_MODRM(reg
, spare
);
765 /** Emits an instruction consiting of an opcode, a spare, a memory address,
766 * and a 16-bit immediate value.
767 * @param str The buffer to emit the instruction to.
768 * @param size The size of the buffer.
769 * @param op The opcode.
770 * @param spare The spare.
771 * @param base The base of the memory address.
772 * @param scale The scale of the memory address.
773 * @param index The index of the memory address.
774 * @param displacement The displacement of the memory address.
775 * @param imm The immediate value.
776 * @return The number of bytes needed to store the instruction.
778 int cg_x86_emit_op_spare_mem_imm16(char *str
, size_t size
, char op
, int spare
, int base
, int scale
, int index
, int displacement
, int imm
) {
782 EMIT_MEM_MODRM(base
, scale
, index
, displacement
, spare
);
787 /** Emits an instruction consiting of an opcode, a spare, a memory address,
788 * and a 32-bit immediate value.
789 * @param str The buffer to emit the instruction to.
790 * @param size The size of the buffer.
791 * @param op The opcode.
792 * @param spare The spare.
793 * @param base The base of the memory address.
794 * @param scale The scale of the memory address.
795 * @param index The index of the memory address.
796 * @param displacement The displacement of the memory address.
797 * @param imm The immediate value.
798 * @return The number of bytes needed to store the instruction.
800 int cg_x86_emit_op_spare_mem_imm32(char *str
, size_t size
, char op
, int spare
, int base
, int scale
, int index
, int displacement
, int imm
) {
804 EMIT_MEM_MODRM(base
, scale
, index
, displacement
, spare
);
809 /** Emits an instruction consiting of an opcode, a spare, a memory address,
810 * and a 8-bit immediate value.
811 * @param str The buffer to emit the instruction to.
812 * @param size The size of the buffer.
813 * @param op The opcode.
814 * @param spare The spare.
815 * @param base The base of the memory address.
816 * @param scale The scale of the memory address.
817 * @param index The index of the memory address.
818 * @param displacement The displacement of the memory address.
819 * @param imm The immediate value.
820 * @return The number of bytes needed to store the instruction.
822 int cg_x86_emit_op_spare_mem_imm8(char *str
, size_t size
, char op
, int spare
, int base
, int scale
, int index
, int displacement
, int imm
) {
825 EMIT_MEM_MODRM(base
, scale
, index
, displacement
, spare
);
830 /** Emits an instruction without any arguments.
831 * @param str The buffer to emit the instruction to.
832 * @param size The size of the buffer.
833 * @param opcode The operation the instruction performs.
834 * @return The number of bytes needed to store the instruction.
836 int cg_x86_emit_nullary_instr(char *str
, size_t size
, int opcode
) {
860 /** Emits an instruction with a 16-bit immediate argument.
861 * @param str The buffer to emit the instruction to.
862 * @param size The size of the buffer.
863 * @param opcode The operation the instruction performs.
864 * @param imm The immediate value.
865 * @return The number of bytes needed to store the instruction.
867 int cg_x86_emit_imm16_instr(char *str
, size_t size
, int opcode
, int imm
) {
871 return EMIT_OP_IMM16(0xe8);
873 return EMIT_JCC16(7);
875 return EMIT_JCC16(2);
877 return EMIT_JCC16(15);
879 return EMIT_JCC16(12);
881 return EMIT_JCC16(6);
883 return EMIT_JCC16(3);
885 return EMIT_JCC16(14);
887 return EMIT_JCC16(13);
889 return EMIT_JCC16(1);
891 return EMIT_JCC16(11);
893 return EMIT_JCC16(9);
895 return EMIT_JCC16(5);
897 return EMIT_JCC16(0);
899 return EMIT_JCC16(10);
901 return EMIT_JCC16(8);
903 return EMIT_JCC16(4);
905 return EMIT_OP_IMM16(0xe9);
907 return EMIT_OP_IMM16(0xc2);
909 return EMIT_OP_IMM16(0xca);
916 /** Emits an instruction with two 16-bit immediate arguments.
917 * @param str The buffer to emit the instruction to.
918 * @param size The size of the buffer.
919 * @param opcode The operation the instruction performs.
920 * @param imma The first immediate value.
921 * @param immb The second immediate value.
922 * @return The number of bytes needed to store the instruction.
924 int cg_x86_emit_imm16_imm16_instr(char *str
, size_t size
, int opcode
, int imma
, int immb
) {
928 return EMIT_OP_IMM16_IMM16(0x9a);
935 /** Emits an instruction with one 16-bit immediate argument and one 32-bit
936 * immediate argument.
937 * @param str The buffer to emit the instruction to.
938 * @param size The size of the buffer.
939 * @param opcode The operation the instruction performs.
940 * @param imma The 16-bit immediate value.
941 * @param immb The 32-bit immediate value.
942 * @return The number of bytes needed to store the instruction.
944 int cg_x86_emit_imm16_imm32_instr(char *str
, size_t size
, int opcode
, int imma
, int immb
) {
948 return EMIT_OP_IMM16_IMM32(0x9a);
955 /** Emits an instruction with a 32-bit immediate argument.
956 * @param str The buffer to emit the instruction to.
957 * @param size The size of the buffer.
958 * @param opcode The operation the instruction performs.
959 * @param imm The immediate value.
960 * @return The number of bytes needed to store the instruction.
962 int cg_x86_emit_imm32_instr(char *str
, size_t size
, int opcode
, int imm
) {
966 return EMIT_OP_IMM32(0xe8);
968 return EMIT_JCC32(7);
970 return EMIT_JCC32(2);
972 return EMIT_JCC32(15);
974 return EMIT_JCC32(12);
976 return EMIT_JCC32(6);
978 return EMIT_JCC32(3);
980 return EMIT_JCC32(14);
982 return EMIT_JCC32(13);
984 return EMIT_JCC32(1);
986 return EMIT_JCC32(11);
988 return EMIT_JCC32(9);
990 return EMIT_JCC32(5);
992 return EMIT_JCC32(0);
994 return EMIT_JCC32(10);
996 return EMIT_JCC32(8);
998 return EMIT_JCC32(4);
1000 return EMIT_OP_IMM32(0xe9);
1007 /** Emits an instruction with an 8-bit immediate argument.
1008 * @param str The buffer to emit the instruction to.
1009 * @param size The size of the buffer.
1010 * @param opcode The operation the instruction performs.
1011 * @param imm The immediate value.
1012 * @return The number of bytes needed to store the instruction.
1014 int cg_x86_emit_imm8_instr(char *str
, size_t size
, int opcode
, int imm
) {
1018 return EMIT_OP_IMM8(0xcd);
1020 return EMIT_OP_IMM8(0x77);
1022 return EMIT_OP_IMM8(0x72);
1023 case CG_X86_OP_JCXZ
:
1024 return EMIT_OP_IMM8(0xe3);
1025 case CG_X86_OP_JECXZ
:
1026 return EMIT_OP_IMM8(0xe3);
1028 return EMIT_OP_IMM8(0x85);
1030 return EMIT_OP_IMM8(0x82);
1032 return EMIT_OP_IMM8(0x76);
1034 return EMIT_OP_IMM8(0x73);
1036 return EMIT_OP_IMM8(0x84);
1038 return EMIT_OP_IMM8(0x83);
1040 return EMIT_OP_IMM8(0x71);
1042 return EMIT_OP_IMM8(0x81);
1044 return EMIT_OP_IMM8(0x79);
1046 return EMIT_OP_IMM8(0x75);
1048 return EMIT_OP_IMM8(0x70);
1050 return EMIT_OP_IMM8(0x80);
1052 return EMIT_OP_IMM8(0x78);
1054 return EMIT_OP_IMM8(0x74);
1056 return EMIT_OP_IMM8(0xeb);
1063 /** Emits an instruction with a memory address that refers to a 16-bit value.
1064 * @param str The buffer to emit the instruction to.
1065 * @param size The size of the buffer.
1066 * @param opcode The operation the instruction performs.
1067 * @param base The base of the memory address.
1068 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1069 * @param index The index of the memory address.
1070 * @param displacement The displacement of the memory address.
1071 * @return The number of bytes needed to store the instruction.
1073 int cg_x86_emit_mem16_instr(char *str
, size_t size
, int opcode
, int base
, int scale
, int index
, int displacement
) {
1076 case CG_X86_OP_CALL
:
1077 return EMIT_OP_SPARE_MEM16(0xff, 2);
1079 return EMIT_OP_SPARE_MEM16(0xff, 0);
1086 /** Emits an instruction with a memory address that refers to a 16-bit value
1087 * as its first argument, and a 16-bit immediate value as its second argument.
1088 * @param str The buffer to emit the instruction to.
1089 * @param size The size of the buffer.
1090 * @param opcode The operation the instruction performs.
1091 * @param base The base of the memory address.
1092 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1093 * @param index The index of the memory address.
1094 * @param displacement The displacement of the memory address.
1095 * @param imm The immediate value.
1096 * @return The number of bytes needed to store the instruction.
1098 int cg_x86_emit_mem16_imm16_instr(char *str
, size_t size
, int opcode
, int base
, int scale
, int index
, int displacement
, int imm
) {
1099 int n
= 0, m
, spare
;
1103 return EMIT_OP_SPARE_MEM_IMM16(0x81, 2);
1105 return EMIT_OP_SPARE_MEM_IMM16(0x81, 0);
1107 return EMIT_OP_SPARE_MEM_IMM16(0x81, 4);
1109 return EMIT_OP_SPARE_MEM_IMM16(0x81, 7);
1111 return EMIT_OP_SPARE_MEM_IMM16(0x81, 1);
1113 return EMIT_OP_SPARE_MEM_IMM16(0xc7, 0);
1120 /** Emits an instruction with a memory address that refers to a 16-bit value
1121 * as its first argument, and a 16-bit register as its second argument.
1122 * @param str The buffer to emit the instruction to.
1123 * @param size The size of the buffer.
1124 * @param opcode The operation the instruction performs.
1125 * @param base The base of the memory address.
1126 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1127 * @param index The index of the memory address.
1128 * @param displacement The displacement of the memory address.
1129 * @param reg The register.
1130 * @return The number of bytes needed to store the instruction.
1132 int cg_x86_emit_mem16_reg16_instr(char *str
, size_t size
, int opcode
, int base
, int scale
, int index
, int displacement
, int reg
) {
1137 return EMIT_OP_MEM_REG16(0x11);
1139 return EMIT_OP_MEM_REG16(0x01);
1141 return EMIT_OP_MEM_REG16(0x23);
1143 return EMIT_OP_MEM_REG16(0x39);
1145 return EMIT_OP_MEM_REG16(0x09);
1152 /** Emits an instruction with a memory address that refers to a 32-bit value.
1153 * @param str The buffer to emit the instruction to.
1154 * @param size The size of the buffer.
1155 * @param opcode The operation the instruction performs.
1156 * @param base The base of the memory address.
1157 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1158 * @param index The index of the memory address.
1159 * @param displacement The displacement of the memory address.
1160 * @return The number of bytes needed to store the instruction.
1162 int cg_x86_emit_mem32_instr(char *str
, size_t size
, int opcode
, int base
, int scale
, int index
, int displacement
) {
1165 case CG_X86_OP_CALL
:
1166 return EMIT_OP_SPARE_MEM32(0xff, 2);
1168 return EMIT_OP_SPARE_MEM32(0xff, 0);
1175 /** Emits an instruction with a memory address that refers to a 32-bit value
1176 * as its first argument, and a 32-bit immediate value as its second argument.
1177 * @param str The buffer to emit the instruction to.
1178 * @param size The size of the buffer.
1179 * @param opcode The operation the instruction performs.
1180 * @param base The base of the memory address.
1181 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1182 * @param index The index of the memory address.
1183 * @param displacement The displacement of the memory address.
1184 * @param imm The immediate value.
1185 * @return The number of bytes needed to store the instruction.
1187 int cg_x86_emit_mem32_imm32_instr(char *str
, size_t size
, int opcode
, int base
, int scale
, int index
, int displacement
, int imm
) {
1188 int n
= 0, m
, spare
;
1192 return EMIT_OP_SPARE_MEM_IMM32(0x81, 2);
1194 return EMIT_OP_SPARE_MEM_IMM32(0x81, 0);
1196 return EMIT_OP_SPARE_MEM_IMM32(0x81, 4);
1198 return EMIT_OP_SPARE_MEM_IMM32(0x81, 7);
1200 return EMIT_OP_SPARE_MEM_IMM32(0x81, 1);
1202 return EMIT_OP_SPARE_MEM_IMM32(0xc7, 0);
1209 /** Emits an instruction with a memory address that refers to a 32-bit value
1210 * as its first argument, and a 32-bit register as its second argument.
1211 * @param str The buffer to emit the instruction to.
1212 * @param size The size of the buffer.
1213 * @param opcode The operation the instruction performs.
1214 * @param base The base of the memory address.
1215 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1216 * @param index The index of the memory address.
1217 * @param displacement The displacement of the memory address.
1218 * @param reg The register.
1219 * @return The number of bytes needed to store the instruction.
1221 int cg_x86_emit_mem32_reg32_instr(char *str
, size_t size
, int opcode
, int base
, int scale
, int index
, int displacement
, int reg
) {
1226 return EMIT_OP_MEM_REG32(0x11);
1228 return EMIT_OP_MEM_REG32(0x01);
1230 return EMIT_OP_MEM_REG32(0x23);
1232 return EMIT_OP_MEM_REG32(0x39);
1234 return EMIT_OP_MEM_REG32(0x09);
1241 /** Emits an instruction with a memory address that refers to an 8-bit value.
1242 * @param str The buffer to emit the instruction to.
1243 * @param size The size of the buffer.
1244 * @param opcode The operation the instruction performs.
1245 * @param base The base of the memory address.
1246 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1247 * @param index The index of the memory address.
1248 * @param displacement The displacement of the memory address.
1249 * @return The number of bytes needed to store the instruction.
1251 int cg_x86_emit_mem8_instr(char *str
, size_t size
, int opcode
, int base
, int scale
, int index
, int displacement
) {
1255 return EMIT_OP_SPARE_MEM8(0xfe, 0);
1262 /** Emits an instruction with a memory address that refers to an 8-bit value
1263 * as its first argument, and a 8-bit immediate value as its second argument.
1264 * @param str The buffer to emit the instruction to.
1265 * @param size The size of the buffer.
1266 * @param opcode The operation the instruction performs.
1267 * @param base The base of the memory address.
1268 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1269 * @param index The index of the memory address.
1270 * @param displacement The displacement of the memory address.
1271 * @param imm The immediate value.
1272 * @return The number of bytes needed to store the instruction.
1274 int cg_x86_emit_mem8_imm8_instr(char *str
, size_t size
, int opcode
, int base
, int scale
, int index
, int displacement
, int imm
) {
1278 return EMIT_OP_SPARE_MEM_IMM8(0x80, 2);
1280 return EMIT_OP_SPARE_MEM_IMM8(0x80, 0);
1282 return EMIT_OP_SPARE_MEM_IMM8(0x80, 4);
1284 return EMIT_OP_SPARE_MEM_IMM8(0x80, 7);
1286 return EMIT_OP_SPARE_MEM_IMM8(0x80, 1);
1288 return EMIT_OP_SPARE_MEM_IMM8(0xc6, 0);
1295 /** Emits an instruction with a memory address that refers to an 8-bit value
1296 * as its first argument, and a 8-bit register as its second argument.
1297 * @param str The buffer to emit the instruction to.
1298 * @param size The size of the buffer.
1299 * @param opcode The operation the instruction performs.
1300 * @param base The base of the memory address.
1301 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1302 * @param index The index of the memory address.
1303 * @param displacement The displacement of the memory address.
1304 * @param reg The register.
1305 * @return The number of bytes needed to store the instruction.
1307 int cg_x86_emit_mem8_reg8_instr(char *str
, size_t size
, int opcode
, int base
, int scale
, int index
, int displacement
, int reg
) {
1312 return EMIT_OP_MEM_REG8(0x10);
1314 return EMIT_OP_MEM_REG8(0x00);
1316 return EMIT_OP_MEM_REG8(0x20);
1318 return EMIT_OP_MEM_REG8(0x38);
1320 return EMIT_OP_MEM_REG8(0x08);
1327 /** Emits an instruction with a 16-bit register argument.
1328 * @param str The buffer to emit the instruction to.
1329 * @param size The size of the buffer.
1330 * @param opcode The operation the instruction performs.
1331 * @param reg The register.
1332 * @return The number of bytes needed to store the instruction.
1334 int cg_x86_emit_reg16_instr(char *str
, size_t size
, int opcode
, int reg
) {
1337 case CG_X86_OP_CALL
:
1338 return EMIT_OP_SPARE_REG16(0xff, 2);
1341 EMIT_BYTE(0x48 + register_code
[reg
]);
1344 return EMIT_OP_SPARE_REG16(0xf7, 6);
1345 case CG_X86_OP_IDIV
:
1346 return EMIT_OP_SPARE_REG16(0xf7, 7);
1347 case CG_X86_OP_IMUL
:
1348 return EMIT_OP_SPARE_REG16(0xf7, 5);
1351 EMIT_BYTE(0x40 + register_code
[reg
]);
1354 return EMIT_OP_SPARE_REG16(0xf7, 4);
1356 return EMIT_OP_SPARE_REG16(0xf7, 3);
1358 return EMIT_OP_SPARE_REG16(0xf7, 2);
1365 /** Emits an instruction with a 16-bit register argument and
1366 * a 16-bit immediate argument.
1367 * @param str The buffer to emit the instruction to.
1368 * @param size The size of the buffer.
1369 * @param opcode The operation the instruction performs.
1370 * @param reg The register.
1371 * @param imm The immediate value.
1372 * @return The number of bytes needed to store the instruction.
1374 int cg_x86_emit_reg16_imm16_instr(char *str
, size_t size
, int opcode
, int reg
, int imm
) {
1375 int n
= 0, m
, spare
;
1379 if(reg
== CG_X86_REG_AX
) {
1380 return EMIT_OP_IMM16(0x15);
1382 return EMIT_OP_SPARE_REG_IMM16(0x81, 2);
1385 if(reg
== CG_X86_REG_AX
) {
1386 return EMIT_OP_IMM16(0x05);
1388 return EMIT_OP_SPARE_REG_IMM16(0x81, 0);
1391 if(reg
== CG_X86_REG_AX
) {
1392 return EMIT_OP_IMM16(0x25);
1394 return EMIT_OP_SPARE_REG_IMM16(0x81, 4);
1397 if(reg
== CG_X86_REG_AX
) {
1398 return EMIT_OP_IMM16(0x3d);
1400 return EMIT_OP_SPARE_REG_IMM16(0x81, 7);
1403 if(reg
== CG_X86_REG_AX
) {
1404 return EMIT_OP_IMM16(0x0d);
1406 return EMIT_OP_SPARE_REG_IMM16(0x81, 1);
1409 if(reg
== CG_X86_REG_AX
) {
1410 return EMIT_OP_IMM16(0x35);
1412 return EMIT_OP_SPARE_REG_IMM16(0x81, 6);
1415 return EMIT_OP_IMM16(0xb8 + register_code
[reg
]);
1422 /** Emits an instruction with a 16-bit register argument and
1423 * an 8-bit immediate argument.
1424 * @param str The buffer to emit the instruction to.
1425 * @param size The size of the buffer.
1426 * @param opcode The operation the instruction performs.
1427 * @param reg The register.
1428 * @param imm The immediate value.
1429 * @return The number of bytes needed to store the instruction.
1431 int cg_x86_emit_reg16_imm8_instr(char *str
, size_t size
, int opcode
, int reg
, int imm
) {
1432 int n
= 0, m
, spare
;
1436 return EMIT_OP_SPARE_REG_IMM8(0x83, 2);
1438 return EMIT_OP_SPARE_REG_IMM8(0x83, 0);
1440 return EMIT_OP_SPARE_REG_IMM8(0x83, 4);
1442 return EMIT_OP_SPARE_REG_IMM8(0x83, 7);
1444 return EMIT_OP_SPARE_REG_IMM8(0x83, 1);
1446 return EMIT_OP_SPARE_REG_IMM8(0x83, 6);
1453 /** Emits an instruction with a 16-bit register as its first argument
1454 * and a memory address that refers to a 16-bit value as its second
1456 * @param str The buffer to emit the instruction to.
1457 * @param size The size of the buffer.
1458 * @param opcode The operation the instruction performs.
1459 * @param reg The register.
1460 * @param base The base of the memory address.
1461 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1462 * @param index The index of the memory address.
1463 * @param displacement The displacement of the memory address.
1464 * @return The number of bytes needed to store the instruction.
1466 int cg_x86_emit_reg16_mem16_instr(char *str
, size_t size
, int opcode
, int reg
, int base
, int scale
, int index
, int displacement
) {
1471 return EMIT_OP_REG_MEM16(0x13);
1473 return EMIT_OP_REG_MEM16(0x03);
1475 return EMIT_OP_REG_MEM16(0x23);
1477 return EMIT_OP_REG_MEM16(0x3b);
1479 return EMIT_OP_REG_MEM16(0x0b);
1481 return EMIT_OP_REG_MEM16(0x33);
1488 /** Emits an instruction with two 16-bit register arguments.
1489 * @param str The buffer to emit the instruction to.
1490 * @param size The size of the buffer.
1491 * @param opcode The operation the instruction performs.
1492 * @param rega The first register.
1493 * @param regb The second register.
1494 * @return The number of bytes needed to store the instruction.
1496 int cg_x86_emit_reg16_reg16_instr(char *str
, size_t size
, int opcode
, int rega
, int regb
) {
1501 return EMIT_OP_REG_REG16(0x13);
1503 return EMIT_OP_REG_REG16(0x03);
1505 return EMIT_OP_REG_REG16(0x23);
1507 return EMIT_OP_REG_REG16(0x3b);
1509 return EMIT_OP_REG_REG16(0x0b);
1511 return EMIT_OP_REG_REG16(0x33);
1518 /** Emits an instruction with a 32-bit register argument.
1519 * @param str The buffer to emit the instruction to.
1520 * @param size The size of the buffer.
1521 * @param opcode The operation the instruction performs.
1522 * @param reg The register.
1523 * @return The number of bytes needed to store the instruction.
1525 int cg_x86_emit_reg32_instr(char *str
, size_t size
, int opcode
, int reg
) {
1528 case CG_X86_OP_CALL
:
1529 return EMIT_OP_SPARE_REG32(0xff, 2);
1532 EMIT_BYTE(0x48 + register_code
[reg
]);
1535 return EMIT_OP_SPARE_REG32(0xf7, 6);
1536 case CG_X86_OP_IDIV
:
1537 return EMIT_OP_SPARE_REG32(0xf7, 7);
1538 case CG_X86_OP_IMUL
:
1539 return EMIT_OP_SPARE_REG32(0xf7, 5);
1542 EMIT_BYTE(0x40 + register_code
[reg
]);
1545 return EMIT_OP_SPARE_REG32(0xf7, 4);
1547 return EMIT_OP_SPARE_REG32(0xf7, 3);
1549 return EMIT_OP_SPARE_REG32(0xf7, 2);
1556 /** Emits an instruction with a 32-bit register argument and
1557 * a 32-bit immediate argument.
1558 * @param str The buffer to emit the instruction to.
1559 * @param size The size of the buffer.
1560 * @param opcode The operation the instruction performs.
1561 * @param reg The register.
1562 * @param imm The immediate value.
1563 * @return The number of bytes needed to store the instruction.
1565 int cg_x86_emit_reg32_imm32_instr(char *str
, size_t size
, int opcode
, int reg
, int imm
) {
1566 int n
= 0, m
, spare
;
1570 if(reg
== CG_X86_REG_EAX
) {
1571 return EMIT_OP_IMM32(0x15);
1573 return EMIT_OP_SPARE_REG_IMM32(0x81, 2);
1576 if(reg
== CG_X86_REG_EAX
) {
1577 return EMIT_OP_IMM32(0x05);
1579 return EMIT_OP_SPARE_REG_IMM32(0x81, 0);
1582 if(reg
== CG_X86_REG_EAX
) {
1583 return EMIT_OP_IMM32(0x25);
1585 return EMIT_OP_SPARE_REG_IMM32(0x81, 4);
1588 if(reg
== CG_X86_REG_EAX
) {
1589 return EMIT_OP_IMM32(0x3d);
1591 return EMIT_OP_SPARE_REG_IMM32(0x81, 7);
1594 if(reg
== CG_X86_REG_EAX
) {
1595 return EMIT_OP_IMM32(0x0d);
1597 return EMIT_OP_SPARE_REG_IMM32(0x81, 1);
1600 if(reg
== CG_X86_REG_EAX
) {
1601 return EMIT_OP_IMM32(0x35);
1603 return EMIT_OP_SPARE_REG_IMM32(0x81, 6);
1606 return EMIT_OP_IMM32(0xb8 + register_code
[reg
]);
1613 /** Emits an instruction with a 32-bit register argument and
1614 * an 8-bit immediate argument.
1615 * @param str The buffer to emit the instruction to.
1616 * @param size The size of the buffer.
1617 * @param opcode The operation the instruction performs.
1618 * @param reg The register.
1619 * @param imm The immediate value.
1620 * @return The number of bytes needed to store the instruction.
1622 int cg_x86_emit_reg32_imm8_instr(char *str
, size_t size
, int opcode
, int reg
, int imm
) {
1623 int n
= 0, m
, spare
;
1627 return EMIT_OP_SPARE_REG_IMM8(0x83, 2);
1629 return EMIT_OP_SPARE_REG_IMM8(0x83, 0);
1631 return EMIT_OP_SPARE_REG_IMM8(0x83, 4);
1633 return EMIT_OP_SPARE_REG_IMM8(0x83, 7);
1635 return EMIT_OP_SPARE_REG_IMM8(0x83, 1);
1637 return EMIT_OP_SPARE_REG_IMM8(0x83, 6);
1644 /** Emits an instruction with a 32-bit register as its first argument
1645 * and a memory address that refers to a 32-bit value as its second
1647 * @param str The buffer to emit the instruction to.
1648 * @param size The size of the buffer.
1649 * @param opcode The operation the instruction performs.
1650 * @param reg The register.
1651 * @param base The base of the memory address.
1652 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1653 * @param index The index of the memory address.
1654 * @param displacement The displacement of the memory address.
1655 * @return The number of bytes needed to store the instruction.
1657 int cg_x86_emit_reg32_mem32_instr(char *str
, size_t size
, int opcode
, int reg
, int base
, int scale
, int index
, int displacement
) {
1662 return EMIT_OP_REG_MEM32(0x13);
1664 return EMIT_OP_REG_MEM32(0x03);
1666 return EMIT_OP_REG_MEM32(0x23);
1668 return EMIT_OP_REG_MEM32(0x3b);
1670 return EMIT_OP_REG_MEM32(0x0b);
1672 return EMIT_OP_REG_MEM32(0x33);
1679 /** Emits an instruction with two 32-bit register arguments.
1680 * @param str The buffer to emit the instruction to.
1681 * @param size The size of the buffer.
1682 * @param opcode The operation the instruction performs.
1683 * @param rega The first register.
1684 * @param regb The second register.
1685 * @return The number of bytes needed to store the instruction.
1687 int cg_x86_emit_reg32_reg32_instr(char *str
, size_t size
, int opcode
, int rega
, int regb
) {
1692 return EMIT_OP_REG_REG32(0x13);
1694 return EMIT_OP_REG_REG32(0x03);
1696 return EMIT_OP_REG_REG32(0x23);
1698 return EMIT_OP_REG_REG32(0x3b);
1700 return EMIT_OP_REG_REG32(0x0b);
1702 return EMIT_OP_REG_REG32(0x33);
1709 /** Emits an instruction with an 8-bit register argument.
1710 * @param str The buffer to emit the instruction to.
1711 * @param size The size of the buffer.
1712 * @param opcode The operation the instruction performs.
1713 * @param reg The register.
1714 * @return The number of bytes needed to store the instruction.
1716 int cg_x86_emit_reg8_instr(char *str
, size_t size
, int opcode
, int reg
) {
1720 return EMIT_OP_SPARE_REG8(0xfe, 1);
1722 return EMIT_OP_SPARE_REG8(0xf6, 6);
1723 case CG_X86_OP_IDIV
:
1724 return EMIT_OP_SPARE_REG8(0xf6, 7);
1725 case CG_X86_OP_IMUL
:
1726 return EMIT_OP_SPARE_REG8(0xf6, 5);
1728 return EMIT_OP_SPARE_REG8(0xfe, 0);
1730 return EMIT_OP_SPARE_REG8(0xf6, 4);
1732 return EMIT_OP_SPARE_REG8(0xf6, 3);
1734 return EMIT_OP_SPARE_REG8(0xf6, 2);
1741 /** Emits an instruction with an 8-bit register argument and
1742 * an 8-bit immediate argument.
1743 * @param str The buffer to emit the instruction to.
1744 * @param size The size of the buffer.
1745 * @param opcode The operation the instruction performs.
1746 * @param reg The register.
1747 * @param imm The immediate value.
1748 * @return The number of bytes needed to store the instruction.
1750 int cg_x86_emit_reg8_imm8_instr(char *str
, size_t size
, int opcode
, int reg
, int imm
) {
1751 int n
= 0, m
, spare
;
1755 if(reg
== CG_X86_REG_AL
) {
1756 return EMIT_OP_IMM8(0x14);
1758 return EMIT_OP_SPARE_REG_IMM8(0x80, 2);
1761 if(reg
== CG_X86_REG_AL
) {
1762 return EMIT_OP_IMM8(0x04);
1764 return EMIT_OP_SPARE_REG_IMM8(0x80, 0);
1767 if(reg
== CG_X86_REG_AL
) {
1768 return EMIT_OP_IMM8(0x24);
1770 return EMIT_OP_SPARE_REG_IMM8(0x80, 4);
1773 if(reg
== CG_X86_REG_AL
) {
1774 return EMIT_OP_IMM8(0x3c);
1776 return EMIT_OP_SPARE_REG_IMM8(0x80, 7);
1779 if(reg
== CG_X86_REG_AL
) {
1780 return EMIT_OP_IMM8(0x0c);
1782 return EMIT_OP_SPARE_REG_IMM8(0x80, 1);
1785 if(reg
== CG_X86_REG_AL
) {
1786 return EMIT_OP_IMM8(0x34);
1788 return EMIT_OP_SPARE_REG_IMM8(0x80, 6);
1791 return EMIT_OP_IMM8(0xb0 + register_code
[reg
]);
1798 /** Emits an instruction with an 8-bit register as its first argument
1799 * and a memory address that refers to an 8-bit value as its second
1801 * @param str The buffer to emit the instruction to.
1802 * @param size The size of the buffer.
1803 * @param opcode The operation the instruction performs.
1804 * @param reg The register.
1805 * @param base The base of the memory address.
1806 * @param scale The scale factor of the memory address (must be 0, 1, 2, 4, or 8).
1807 * @param index The index of the memory address.
1808 * @param displacement The displacement of the memory address.
1809 * @return The number of bytes needed to store the instruction.
1811 int cg_x86_emit_reg8_mem8_instr(char *str
, size_t size
, int opcode
, int reg
, int base
, int scale
, int index
, int displacement
) {
1815 return EMIT_OP_MEM_REG8(0x12);
1817 return EMIT_OP_MEM_REG8(0x02);
1819 return EMIT_OP_MEM_REG8(0x22);
1821 return EMIT_OP_MEM_REG8(0x3a);
1823 return EMIT_OP_MEM_REG8(0x0a);
1825 return EMIT_OP_MEM_REG8(0x32);
1832 /** Emits an instruction with two 8-bit register arguments.
1833 * @param str The buffer to emit the instruction to.
1834 * @param size The size of the buffer.
1835 * @param opcode The operation the instruction performs.
1836 * @param rega The first register.
1837 * @param regb The second register.
1838 * @return The number of bytes needed to store the instruction.
1840 int cg_x86_emit_reg8_reg8_instr(char *str
, size_t size
, int opcode
, int rega
, int regb
) {
1844 return EMIT_OP_REG_REG8(0x12);
1846 return EMIT_OP_REG_REG8(0x02);
1848 return EMIT_OP_REG_REG8(0x22);
1850 return EMIT_OP_REG_REG8(0x3a);
1852 return EMIT_OP_REG_REG8(0x0a);
1854 return EMIT_OP_REG_REG8(0x32);