Really added license. (-:
[alchemist.git] / x86 / lib / x86.c
blobfdf35c745b7b1226d5edf87895756f2ea0e7aad9
1 /*
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
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "alchemist/x86.h"
24 /** \file
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
56 * + displacement.
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))
65 #define BYTE char
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[] = {
121 NULL,
122 "add",
123 "adc",
124 "and",
125 "call",
126 "clc",
127 "cld",
128 "cli",
129 "cmp",
130 "dec",
131 "div",
132 "hlt",
133 "idiv",
134 "imul",
135 "in",
136 "inc",
137 "insb",
138 "insd",
139 "insw",
140 "int",
141 "int1",
142 "int3",
143 "into",
144 "iret",
145 "iretd",
146 "iretw",
147 "ja",
148 "jb",
149 "jcxz",
150 "jecxz",
151 "jg",
152 "jl",
153 "jna",
154 "jnb",
155 "jng",
156 "jnl",
157 "jno",
158 "jnp",
159 "jns",
160 "jnz",
161 "jo",
162 "jp",
163 "js",
164 "jz",
165 "jmp",
166 "lds",
167 "les",
168 "lfs",
169 "lgs",
170 "lss",
171 "lea",
172 "leave",
173 "lgdt",
174 "lidt",
175 "lldt",
176 "lmsw",
177 "lodsb",
178 "lodsd",
179 "lodsw",
180 "loop",
181 "loopnz",
182 "loopz",
183 "lsl",
184 "ltr",
185 "mov",
186 "movsb",
187 "movsd",
188 "movsw",
189 "mul",
190 "neg",
191 "nop",
192 "not",
193 "or",
194 "out",
195 "pause",
196 "pop",
197 "popad",
198 "popaw",
199 "popfd",
200 "popfw",
201 "push",
202 "pushad",
203 "pushaw",
204 "pushfd",
205 "pushfw",
206 "ret",
207 "retf",
208 "rol",
209 "ror",
210 "shl",
211 "shr",
212 "sub",
213 "xchg",
214 "xor",
217 /** Table translating CG_X86_REG_* macros to register names. */
218 char *cg_x86_registers[] = {
219 NULL,
220 "ax",
221 "bx",
222 "cx",
223 "dx",
224 "di",
225 "si",
226 "bp",
227 "sp",
228 "al",
229 "bl",
230 "cl",
231 "dl",
232 "ah",
233 "bh",
234 "ch",
235 "dh",
236 "eax",
237 "ebx",
238 "ecx",
239 "edx",
240 "edi",
241 "esi",
242 "ebp",
243 "esp",
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() {
277 return 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) {
292 BYTE modrm, sib;
293 int n = 0, address_mode = 0;
295 modrm = spare << 3;
297 /* Calculate the size of the displacement we need to use. */
298 if(displacement == 0) {
299 /* No displacement. */
300 /* modrm |= 0; */
301 modrm |= 0x40; /* Put one anyway... */
302 } else if((displacement & 0xffffff00) == 0) {
303 /* byte displacement. */
304 modrm |= 0x40;
305 } else if((displacement & 0xffff0000) == 0) {
306 /* word displacement. */
307 modrm |= 0x80;
308 address_mode = 16;
309 } else {
310 /* dword displacement. */
311 modrm |= 0x80;
312 address_mode = 32;
315 /* Calculate SIB byte. */
316 /* Scale must be 1, 2, 4, or 8 */
317 switch(scale) {
318 case 0:
319 case 1:
320 sib = 0x00;
321 break;
322 case 2:
323 sib = 0x40;
324 break;
325 case 4:
326 sib = 0x80;
327 break;
328 case 8:
329 sib = 0xc0;
330 break;
331 default:
332 /* No other values of scale are allowed. */
333 INVALID_SCALE_FACTOR
336 /* Index */
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;
340 else {
341 /* ESP cannot be used as an index. */
342 if(index == CG_X86_REG_ESP) INVALID_INSTRUCTION
343 sib |= register_code[index] << 3;
346 /* Base */
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. */
351 modrm |= 4;
352 ADDRESS32;
353 EMIT_BYTE(modrm);
354 EMIT_BYTE(sib);
355 switch(modrm & 0xc0) {
356 case 0:
357 break;
358 case 0x40:
359 EMIT_BYTE(displacement);
360 break;
361 case 0x80:
362 EMIT_DWORD(displacement);
363 break;
364 default:
365 /* Can't happen */
366 return -1;
369 return n;
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) {
381 int n = 0;
382 EMIT_BYTE(0xc0 | (spare << 3) | register_code[reg]);
383 return n;
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) {
394 int n = 0;
395 INSTR16
396 EMIT_BYTE(0x0f);
397 EMIT_BYTE(0x80 + cc);
398 EMIT_WORD(imm);
399 return n;
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) {
410 int n = 0;
411 INSTR32
412 EMIT_BYTE(0x0f);
413 EMIT_BYTE(0x80 + cc);
414 EMIT_DWORD(imm);
415 return n;
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) {
426 int n = 0;
427 INSTR16
428 EMIT_BYTE(op);
429 EMIT_WORD(imm);
430 return n;
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) {
442 int n = 0;
443 INSTR16
444 EMIT_BYTE(op);
445 EMIT_WORD(imma);
446 EMIT_WORD(immb);
447 return n;
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) {
460 int n = 0;
461 INSTR32
462 EMIT_BYTE(op);
463 EMIT_WORD(imma);
464 EMIT_DWORD(immb);
465 return n;
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) {
476 int n = 0;
477 INSTR32
478 EMIT_BYTE(op);
479 EMIT_DWORD(imm);
480 return n;
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) {
491 int n = 0;
492 EMIT_BYTE(op);
493 EMIT_BYTE(imm);
494 return n;
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) {
510 int n = 0, m;
511 INSTR16
512 EMIT_BYTE(op);
513 EMIT_MEM_MODRM(base, scale, index, displacement, register_code[reg]);
514 return n;
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) {
530 int n = 0, m;
531 INSTR32
532 EMIT_BYTE(op);
533 EMIT_MEM_MODRM(base, scale, index, displacement, register_code[reg]);
534 return n;
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) {
550 int n = 0, m;
551 EMIT_BYTE(op);
552 EMIT_MEM_MODRM(base, scale, index, displacement, register_code[reg]);
553 return n;
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) {
565 int n = 0, m;
566 INSTR16
567 EMIT_BYTE(op);
568 EMIT_REG_MODRM(regb, register_code[rega]);
569 return n;
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) {
581 int n = 0, m;
582 INSTR32
583 EMIT_BYTE(op);
584 EMIT_REG_MODRM(regb, register_code[rega]);
585 return n;
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) {
597 int n = 0, m;
598 EMIT_BYTE(op);
599 EMIT_REG_MODRM(regb, register_code[rega]);
600 return n;
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) {
616 int n = 0, m;
617 INSTR16
618 EMIT_BYTE(op);
619 EMIT_MEM_MODRM(base, scale, index, displacement, spare);
620 return n;
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) {
636 int n = 0, m;
637 INSTR32
638 EMIT_BYTE(op);
639 EMIT_MEM_MODRM(base, scale, index, displacement, spare);
640 return n;
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) {
656 int n = 0, m;
657 EMIT_BYTE(op);
658 EMIT_MEM_MODRM(base, scale, index, displacement, spare);
659 return n;
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) {
671 int n = 0, m;
672 INSTR16
673 EMIT_BYTE(op);
674 EMIT_REG_MODRM(reg, spare);
675 return n;
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) {
687 int n = 0, m;
688 INSTR32
689 EMIT_BYTE(op);
690 EMIT_REG_MODRM(reg, spare);
691 return n;
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) {
703 int n = 0, m;
704 EMIT_BYTE(op);
705 EMIT_REG_MODRM(reg, spare);
706 return n;
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) {
720 int n = 0, m;
721 INSTR16
722 EMIT_BYTE(op);
723 EMIT_REG_MODRM(reg, spare);
724 EMIT_WORD(imm);
725 return n;
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) {
739 int n = 0, m;
740 INSTR32
741 EMIT_BYTE(op);
742 EMIT_REG_MODRM(reg, spare);
743 EMIT_DWORD(imm);
744 return n;
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) {
758 int n = 0, m;
759 EMIT_BYTE(op);
760 EMIT_REG_MODRM(reg, spare);
761 EMIT_BYTE(imm);
762 return n;
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) {
779 int n = 0, m;
780 INSTR16
781 EMIT_BYTE(op);
782 EMIT_MEM_MODRM(base, scale, index, displacement, spare);
783 EMIT_WORD(imm);
784 return n;
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) {
801 int n = 0, m;
802 INSTR32
803 EMIT_BYTE(op);
804 EMIT_MEM_MODRM(base, scale, index, displacement, spare);
805 EMIT_DWORD(imm);
806 return n;
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) {
823 int n = 0, m;
824 EMIT_BYTE(op);
825 EMIT_MEM_MODRM(base, scale, index, displacement, spare);
826 EMIT_BYTE(imm);
827 return n;
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) {
837 int n = 0;
838 switch(opcode) {
839 case CG_X86_OP_CLC:
840 EMIT_BYTE(0xf8);
841 break;
842 case CG_X86_OP_CLD:
843 EMIT_BYTE(0xfc);
844 break;
845 case CG_X86_OP_CLI:
846 EMIT_BYTE(0xfa);
847 break;
848 case CG_X86_OP_RET:
849 EMIT_BYTE(0xc3);
850 break;
851 case CG_X86_OP_RETF:
852 EMIT_BYTE(0xcb);
853 break;
854 default:
855 INVALID_INSTRUCTION
857 return n;
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) {
868 int n = 0;
869 switch(opcode) {
870 case CG_X86_OP_CALL:
871 return EMIT_OP_IMM16(0xe8);
872 case CG_X86_OP_JA:
873 return EMIT_JCC16(7);
874 case CG_X86_OP_JB:
875 return EMIT_JCC16(2);
876 case CG_X86_OP_JG:
877 return EMIT_JCC16(15);
878 case CG_X86_OP_JL:
879 return EMIT_JCC16(12);
880 case CG_X86_OP_JNA:
881 return EMIT_JCC16(6);
882 case CG_X86_OP_JNB:
883 return EMIT_JCC16(3);
884 case CG_X86_OP_JNG:
885 return EMIT_JCC16(14);
886 case CG_X86_OP_JNL:
887 return EMIT_JCC16(13);
888 case CG_X86_OP_JNO:
889 return EMIT_JCC16(1);
890 case CG_X86_OP_JNP:
891 return EMIT_JCC16(11);
892 case CG_X86_OP_JNS:
893 return EMIT_JCC16(9);
894 case CG_X86_OP_JNZ:
895 return EMIT_JCC16(5);
896 case CG_X86_OP_JO:
897 return EMIT_JCC16(0);
898 case CG_X86_OP_JP:
899 return EMIT_JCC16(10);
900 case CG_X86_OP_JS:
901 return EMIT_JCC16(8);
902 case CG_X86_OP_JZ:
903 return EMIT_JCC16(4);
904 case CG_X86_OP_JMP:
905 return EMIT_OP_IMM16(0xe9);
906 case CG_X86_OP_RET:
907 return EMIT_OP_IMM16(0xc2);
908 case CG_X86_OP_RETF:
909 return EMIT_OP_IMM16(0xca);
910 default:
911 INVALID_INSTRUCTION
913 return n;
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) {
925 int n = 0;
926 switch(opcode) {
927 case CG_X86_OP_CALL:
928 return EMIT_OP_IMM16_IMM16(0x9a);
929 default:
930 INVALID_INSTRUCTION
932 return n;
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) {
945 int n = 0;
946 switch(opcode) {
947 case CG_X86_OP_CALL:
948 return EMIT_OP_IMM16_IMM32(0x9a);
949 default:
950 INVALID_INSTRUCTION
952 return n;
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) {
963 int n = 0;
964 switch(opcode) {
965 case CG_X86_OP_CALL:
966 return EMIT_OP_IMM32(0xe8);
967 case CG_X86_OP_JA:
968 return EMIT_JCC32(7);
969 case CG_X86_OP_JB:
970 return EMIT_JCC32(2);
971 case CG_X86_OP_JG:
972 return EMIT_JCC32(15);
973 case CG_X86_OP_JL:
974 return EMIT_JCC32(12);
975 case CG_X86_OP_JNA:
976 return EMIT_JCC32(6);
977 case CG_X86_OP_JNB:
978 return EMIT_JCC32(3);
979 case CG_X86_OP_JNG:
980 return EMIT_JCC32(14);
981 case CG_X86_OP_JNL:
982 return EMIT_JCC32(13);
983 case CG_X86_OP_JNO:
984 return EMIT_JCC32(1);
985 case CG_X86_OP_JNP:
986 return EMIT_JCC32(11);
987 case CG_X86_OP_JNS:
988 return EMIT_JCC32(9);
989 case CG_X86_OP_JNZ:
990 return EMIT_JCC32(5);
991 case CG_X86_OP_JO:
992 return EMIT_JCC32(0);
993 case CG_X86_OP_JP:
994 return EMIT_JCC32(10);
995 case CG_X86_OP_JS:
996 return EMIT_JCC32(8);
997 case CG_X86_OP_JZ:
998 return EMIT_JCC32(4);
999 case CG_X86_OP_JMP:
1000 return EMIT_OP_IMM32(0xe9);
1001 default:
1002 INVALID_INSTRUCTION
1004 return n;
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) {
1015 int n = 0;
1016 switch(opcode) {
1017 case CG_X86_OP_INT:
1018 return EMIT_OP_IMM8(0xcd);
1019 case CG_X86_OP_JA:
1020 return EMIT_OP_IMM8(0x77);
1021 case CG_X86_OP_JB:
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);
1027 case CG_X86_OP_JG:
1028 return EMIT_OP_IMM8(0x85);
1029 case CG_X86_OP_JL:
1030 return EMIT_OP_IMM8(0x82);
1031 case CG_X86_OP_JNA:
1032 return EMIT_OP_IMM8(0x76);
1033 case CG_X86_OP_JNB:
1034 return EMIT_OP_IMM8(0x73);
1035 case CG_X86_OP_JNG:
1036 return EMIT_OP_IMM8(0x84);
1037 case CG_X86_OP_JNL:
1038 return EMIT_OP_IMM8(0x83);
1039 case CG_X86_OP_JNO:
1040 return EMIT_OP_IMM8(0x71);
1041 case CG_X86_OP_JNP:
1042 return EMIT_OP_IMM8(0x81);
1043 case CG_X86_OP_JNS:
1044 return EMIT_OP_IMM8(0x79);
1045 case CG_X86_OP_JNZ:
1046 return EMIT_OP_IMM8(0x75);
1047 case CG_X86_OP_JO:
1048 return EMIT_OP_IMM8(0x70);
1049 case CG_X86_OP_JP:
1050 return EMIT_OP_IMM8(0x80);
1051 case CG_X86_OP_JS:
1052 return EMIT_OP_IMM8(0x78);
1053 case CG_X86_OP_JZ:
1054 return EMIT_OP_IMM8(0x74);
1055 case CG_X86_OP_JMP:
1056 return EMIT_OP_IMM8(0xeb);
1057 default:
1058 INVALID_INSTRUCTION
1060 return n;
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) {
1074 int n = 0;
1075 switch(opcode) {
1076 case CG_X86_OP_CALL:
1077 return EMIT_OP_SPARE_MEM16(0xff, 2);
1078 case CG_X86_OP_INC:
1079 return EMIT_OP_SPARE_MEM16(0xff, 0);
1080 default:
1081 INVALID_INSTRUCTION
1083 return n;
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;
1100 char op;
1101 switch(opcode) {
1102 case CG_X86_OP_ADC:
1103 return EMIT_OP_SPARE_MEM_IMM16(0x81, 2);
1104 case CG_X86_OP_ADD:
1105 return EMIT_OP_SPARE_MEM_IMM16(0x81, 0);
1106 case CG_X86_OP_AND:
1107 return EMIT_OP_SPARE_MEM_IMM16(0x81, 4);
1108 case CG_X86_OP_CMP:
1109 return EMIT_OP_SPARE_MEM_IMM16(0x81, 7);
1110 case CG_X86_OP_OR:
1111 return EMIT_OP_SPARE_MEM_IMM16(0x81, 1);
1112 case CG_X86_OP_MOV:
1113 return EMIT_OP_SPARE_MEM_IMM16(0xc7, 0);
1114 default:
1115 INVALID_INSTRUCTION
1117 return n;
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) {
1133 int n = 0, m;
1134 char op;
1135 switch(opcode) {
1136 case CG_X86_OP_ADC:
1137 return EMIT_OP_MEM_REG16(0x11);
1138 case CG_X86_OP_ADD:
1139 return EMIT_OP_MEM_REG16(0x01);
1140 case CG_X86_OP_AND:
1141 return EMIT_OP_MEM_REG16(0x23);
1142 case CG_X86_OP_CMP:
1143 return EMIT_OP_MEM_REG16(0x39);
1144 case CG_X86_OP_OR:
1145 return EMIT_OP_MEM_REG16(0x09);
1146 default:
1147 INVALID_INSTRUCTION
1149 return n;
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) {
1163 int n = 0;
1164 switch(opcode) {
1165 case CG_X86_OP_CALL:
1166 return EMIT_OP_SPARE_MEM32(0xff, 2);
1167 case CG_X86_OP_INC:
1168 return EMIT_OP_SPARE_MEM32(0xff, 0);
1169 default:
1170 INVALID_INSTRUCTION
1172 return n;
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;
1189 char op;
1190 switch(opcode) {
1191 case CG_X86_OP_ADC:
1192 return EMIT_OP_SPARE_MEM_IMM32(0x81, 2);
1193 case CG_X86_OP_ADD:
1194 return EMIT_OP_SPARE_MEM_IMM32(0x81, 0);
1195 case CG_X86_OP_AND:
1196 return EMIT_OP_SPARE_MEM_IMM32(0x81, 4);
1197 case CG_X86_OP_CMP:
1198 return EMIT_OP_SPARE_MEM_IMM32(0x81, 7);
1199 case CG_X86_OP_OR:
1200 return EMIT_OP_SPARE_MEM_IMM32(0x81, 1);
1201 case CG_X86_OP_MOV:
1202 return EMIT_OP_SPARE_MEM_IMM32(0xc7, 0);
1203 default:
1204 INVALID_INSTRUCTION
1206 return n;
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) {
1222 int n = 0, m;
1223 char op;
1224 switch(opcode) {
1225 case CG_X86_OP_ADC:
1226 return EMIT_OP_MEM_REG32(0x11);
1227 case CG_X86_OP_ADD:
1228 return EMIT_OP_MEM_REG32(0x01);
1229 case CG_X86_OP_AND:
1230 return EMIT_OP_MEM_REG32(0x23);
1231 case CG_X86_OP_CMP:
1232 return EMIT_OP_MEM_REG32(0x39);
1233 case CG_X86_OP_OR:
1234 return EMIT_OP_MEM_REG32(0x09);
1235 default:
1236 INVALID_INSTRUCTION
1238 return n;
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) {
1252 int n = 0;
1253 switch(opcode) {
1254 case CG_X86_OP_INC:
1255 return EMIT_OP_SPARE_MEM8(0xfe, 0);
1256 default:
1257 INVALID_INSTRUCTION
1259 return n;
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) {
1275 int n = 0, m;
1276 switch(opcode) {
1277 case CG_X86_OP_ADC:
1278 return EMIT_OP_SPARE_MEM_IMM8(0x80, 2);
1279 case CG_X86_OP_ADD:
1280 return EMIT_OP_SPARE_MEM_IMM8(0x80, 0);
1281 case CG_X86_OP_AND:
1282 return EMIT_OP_SPARE_MEM_IMM8(0x80, 4);
1283 case CG_X86_OP_CMP:
1284 return EMIT_OP_SPARE_MEM_IMM8(0x80, 7);
1285 case CG_X86_OP_OR:
1286 return EMIT_OP_SPARE_MEM_IMM8(0x80, 1);
1287 case CG_X86_OP_MOV:
1288 return EMIT_OP_SPARE_MEM_IMM8(0xc6, 0);
1289 default:
1290 INVALID_INSTRUCTION
1292 return n;
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) {
1308 int n = 0, m;
1309 char op;
1310 switch(opcode) {
1311 case CG_X86_OP_ADC:
1312 return EMIT_OP_MEM_REG8(0x10);
1313 case CG_X86_OP_ADD:
1314 return EMIT_OP_MEM_REG8(0x00);
1315 case CG_X86_OP_AND:
1316 return EMIT_OP_MEM_REG8(0x20);
1317 case CG_X86_OP_CMP:
1318 return EMIT_OP_MEM_REG8(0x38);
1319 case CG_X86_OP_OR:
1320 return EMIT_OP_MEM_REG8(0x08);
1321 default:
1322 INVALID_INSTRUCTION
1324 return n;
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) {
1335 int n = 0;
1336 switch(opcode) {
1337 case CG_X86_OP_CALL:
1338 return EMIT_OP_SPARE_REG16(0xff, 2);
1339 case CG_X86_OP_DEC:
1340 INSTR16
1341 EMIT_BYTE(0x48 + register_code[reg]);
1342 break;
1343 case CG_X86_OP_DIV:
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);
1349 case CG_X86_OP_INC:
1350 INSTR16
1351 EMIT_BYTE(0x40 + register_code[reg]);
1352 break;
1353 case CG_X86_OP_MUL:
1354 return EMIT_OP_SPARE_REG16(0xf7, 4);
1355 case CG_X86_OP_NEG:
1356 return EMIT_OP_SPARE_REG16(0xf7, 3);
1357 case CG_X86_OP_NOT:
1358 return EMIT_OP_SPARE_REG16(0xf7, 2);
1359 default:
1360 INVALID_INSTRUCTION
1362 return n;
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;
1376 char op;
1377 switch(opcode) {
1378 case CG_X86_OP_ADC:
1379 if(reg == CG_X86_REG_AX) {
1380 return EMIT_OP_IMM16(0x15);
1381 } else {
1382 return EMIT_OP_SPARE_REG_IMM16(0x81, 2);
1384 case CG_X86_OP_ADD:
1385 if(reg == CG_X86_REG_AX) {
1386 return EMIT_OP_IMM16(0x05);
1387 } else {
1388 return EMIT_OP_SPARE_REG_IMM16(0x81, 0);
1390 case CG_X86_OP_AND:
1391 if(reg == CG_X86_REG_AX) {
1392 return EMIT_OP_IMM16(0x25);
1393 } else {
1394 return EMIT_OP_SPARE_REG_IMM16(0x81, 4);
1396 case CG_X86_OP_CMP:
1397 if(reg == CG_X86_REG_AX) {
1398 return EMIT_OP_IMM16(0x3d);
1399 } else {
1400 return EMIT_OP_SPARE_REG_IMM16(0x81, 7);
1402 case CG_X86_OP_OR:
1403 if(reg == CG_X86_REG_AX) {
1404 return EMIT_OP_IMM16(0x0d);
1405 } else {
1406 return EMIT_OP_SPARE_REG_IMM16(0x81, 1);
1408 case CG_X86_OP_XOR:
1409 if(reg == CG_X86_REG_AX) {
1410 return EMIT_OP_IMM16(0x35);
1411 } else {
1412 return EMIT_OP_SPARE_REG_IMM16(0x81, 6);
1414 case CG_X86_OP_MOV:
1415 return EMIT_OP_IMM16(0xb8 + register_code[reg]);
1416 default:
1417 INVALID_INSTRUCTION
1419 return n;
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;
1433 char op;
1434 switch(opcode) {
1435 case CG_X86_OP_ADC:
1436 return EMIT_OP_SPARE_REG_IMM8(0x83, 2);
1437 case CG_X86_OP_ADD:
1438 return EMIT_OP_SPARE_REG_IMM8(0x83, 0);
1439 case CG_X86_OP_AND:
1440 return EMIT_OP_SPARE_REG_IMM8(0x83, 4);
1441 case CG_X86_OP_CMP:
1442 return EMIT_OP_SPARE_REG_IMM8(0x83, 7);
1443 case CG_X86_OP_OR:
1444 return EMIT_OP_SPARE_REG_IMM8(0x83, 1);
1445 case CG_X86_OP_XOR:
1446 return EMIT_OP_SPARE_REG_IMM8(0x83, 6);
1447 default:
1448 INVALID_INSTRUCTION
1450 return n;
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
1455 * argument.
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) {
1467 int n = 0, m;
1468 char op;
1469 switch(opcode) {
1470 case CG_X86_OP_ADC:
1471 return EMIT_OP_REG_MEM16(0x13);
1472 case CG_X86_OP_ADD:
1473 return EMIT_OP_REG_MEM16(0x03);
1474 case CG_X86_OP_AND:
1475 return EMIT_OP_REG_MEM16(0x23);
1476 case CG_X86_OP_CMP:
1477 return EMIT_OP_REG_MEM16(0x3b);
1478 case CG_X86_OP_OR:
1479 return EMIT_OP_REG_MEM16(0x0b);
1480 case CG_X86_OP_XOR:
1481 return EMIT_OP_REG_MEM16(0x33);
1482 default:
1483 INVALID_INSTRUCTION
1485 return n;
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) {
1497 int n = 0, m;
1498 char op;
1499 switch(opcode) {
1500 case CG_X86_OP_ADC:
1501 return EMIT_OP_REG_REG16(0x13);
1502 case CG_X86_OP_ADD:
1503 return EMIT_OP_REG_REG16(0x03);
1504 case CG_X86_OP_AND:
1505 return EMIT_OP_REG_REG16(0x23);
1506 case CG_X86_OP_CMP:
1507 return EMIT_OP_REG_REG16(0x3b);
1508 case CG_X86_OP_OR:
1509 return EMIT_OP_REG_REG16(0x0b);
1510 case CG_X86_OP_XOR:
1511 return EMIT_OP_REG_REG16(0x33);
1512 default:
1513 INVALID_INSTRUCTION
1515 return n;
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) {
1526 int n = 0;
1527 switch(opcode) {
1528 case CG_X86_OP_CALL:
1529 return EMIT_OP_SPARE_REG32(0xff, 2);
1530 case CG_X86_OP_DEC:
1531 INSTR32
1532 EMIT_BYTE(0x48 + register_code[reg]);
1533 break;
1534 case CG_X86_OP_DIV:
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);
1540 case CG_X86_OP_INC:
1541 INSTR32
1542 EMIT_BYTE(0x40 + register_code[reg]);
1543 break;
1544 case CG_X86_OP_MUL:
1545 return EMIT_OP_SPARE_REG32(0xf7, 4);
1546 case CG_X86_OP_NEG:
1547 return EMIT_OP_SPARE_REG32(0xf7, 3);
1548 case CG_X86_OP_NOT:
1549 return EMIT_OP_SPARE_REG32(0xf7, 2);
1550 default:
1551 INVALID_INSTRUCTION
1553 return n;
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;
1567 char op;
1568 switch(opcode) {
1569 case CG_X86_OP_ADC:
1570 if(reg == CG_X86_REG_EAX) {
1571 return EMIT_OP_IMM32(0x15);
1572 } else {
1573 return EMIT_OP_SPARE_REG_IMM32(0x81, 2);
1575 case CG_X86_OP_ADD:
1576 if(reg == CG_X86_REG_EAX) {
1577 return EMIT_OP_IMM32(0x05);
1578 } else {
1579 return EMIT_OP_SPARE_REG_IMM32(0x81, 0);
1581 case CG_X86_OP_AND:
1582 if(reg == CG_X86_REG_EAX) {
1583 return EMIT_OP_IMM32(0x25);
1584 } else {
1585 return EMIT_OP_SPARE_REG_IMM32(0x81, 4);
1587 case CG_X86_OP_CMP:
1588 if(reg == CG_X86_REG_EAX) {
1589 return EMIT_OP_IMM32(0x3d);
1590 } else {
1591 return EMIT_OP_SPARE_REG_IMM32(0x81, 7);
1593 case CG_X86_OP_OR:
1594 if(reg == CG_X86_REG_EAX) {
1595 return EMIT_OP_IMM32(0x0d);
1596 } else {
1597 return EMIT_OP_SPARE_REG_IMM32(0x81, 1);
1599 case CG_X86_OP_XOR:
1600 if(reg == CG_X86_REG_EAX) {
1601 return EMIT_OP_IMM32(0x35);
1602 } else {
1603 return EMIT_OP_SPARE_REG_IMM32(0x81, 6);
1605 case CG_X86_OP_MOV:
1606 return EMIT_OP_IMM32(0xb8 + register_code[reg]);
1607 default:
1608 INVALID_INSTRUCTION
1610 return n;
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;
1624 char op;
1625 switch(opcode) {
1626 case CG_X86_OP_ADC:
1627 return EMIT_OP_SPARE_REG_IMM8(0x83, 2);
1628 case CG_X86_OP_ADD:
1629 return EMIT_OP_SPARE_REG_IMM8(0x83, 0);
1630 case CG_X86_OP_AND:
1631 return EMIT_OP_SPARE_REG_IMM8(0x83, 4);
1632 case CG_X86_OP_CMP:
1633 return EMIT_OP_SPARE_REG_IMM8(0x83, 7);
1634 case CG_X86_OP_OR:
1635 return EMIT_OP_SPARE_REG_IMM8(0x83, 1);
1636 case CG_X86_OP_XOR:
1637 return EMIT_OP_SPARE_REG_IMM8(0x83, 6);
1638 default:
1639 INVALID_INSTRUCTION
1641 return n;
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
1646 * argument.
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) {
1658 int n = 0, m;
1659 char op;
1660 switch(opcode) {
1661 case CG_X86_OP_ADC:
1662 return EMIT_OP_REG_MEM32(0x13);
1663 case CG_X86_OP_ADD:
1664 return EMIT_OP_REG_MEM32(0x03);
1665 case CG_X86_OP_AND:
1666 return EMIT_OP_REG_MEM32(0x23);
1667 case CG_X86_OP_CMP:
1668 return EMIT_OP_REG_MEM32(0x3b);
1669 case CG_X86_OP_OR:
1670 return EMIT_OP_REG_MEM32(0x0b);
1671 case CG_X86_OP_XOR:
1672 return EMIT_OP_REG_MEM32(0x33);
1673 default:
1674 INVALID_INSTRUCTION
1676 return n;
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) {
1688 int n = 0, m;
1689 char op;
1690 switch(opcode) {
1691 case CG_X86_OP_ADC:
1692 return EMIT_OP_REG_REG32(0x13);
1693 case CG_X86_OP_ADD:
1694 return EMIT_OP_REG_REG32(0x03);
1695 case CG_X86_OP_AND:
1696 return EMIT_OP_REG_REG32(0x23);
1697 case CG_X86_OP_CMP:
1698 return EMIT_OP_REG_REG32(0x3b);
1699 case CG_X86_OP_OR:
1700 return EMIT_OP_REG_REG32(0x0b);
1701 case CG_X86_OP_XOR:
1702 return EMIT_OP_REG_REG32(0x33);
1703 default:
1704 INVALID_INSTRUCTION
1706 return n;
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) {
1717 int n = 0;
1718 switch(opcode) {
1719 case CG_X86_OP_DEC:
1720 return EMIT_OP_SPARE_REG8(0xfe, 1);
1721 case CG_X86_OP_DIV:
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);
1727 case CG_X86_OP_INC:
1728 return EMIT_OP_SPARE_REG8(0xfe, 0);
1729 case CG_X86_OP_MUL:
1730 return EMIT_OP_SPARE_REG8(0xf6, 4);
1731 case CG_X86_OP_NEG:
1732 return EMIT_OP_SPARE_REG8(0xf6, 3);
1733 case CG_X86_OP_NOT:
1734 return EMIT_OP_SPARE_REG8(0xf6, 2);
1735 default:
1736 INVALID_INSTRUCTION
1738 return n;
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;
1752 char op;
1753 switch(opcode) {
1754 case CG_X86_OP_ADC:
1755 if(reg == CG_X86_REG_AL) {
1756 return EMIT_OP_IMM8(0x14);
1757 } else {
1758 return EMIT_OP_SPARE_REG_IMM8(0x80, 2);
1760 case CG_X86_OP_ADD:
1761 if(reg == CG_X86_REG_AL) {
1762 return EMIT_OP_IMM8(0x04);
1763 } else {
1764 return EMIT_OP_SPARE_REG_IMM8(0x80, 0);
1766 case CG_X86_OP_AND:
1767 if(reg == CG_X86_REG_AL) {
1768 return EMIT_OP_IMM8(0x24);
1769 } else {
1770 return EMIT_OP_SPARE_REG_IMM8(0x80, 4);
1772 case CG_X86_OP_CMP:
1773 if(reg == CG_X86_REG_AL) {
1774 return EMIT_OP_IMM8(0x3c);
1775 } else {
1776 return EMIT_OP_SPARE_REG_IMM8(0x80, 7);
1778 case CG_X86_OP_OR:
1779 if(reg == CG_X86_REG_AL) {
1780 return EMIT_OP_IMM8(0x0c);
1781 } else {
1782 return EMIT_OP_SPARE_REG_IMM8(0x80, 1);
1784 case CG_X86_OP_XOR:
1785 if(reg == CG_X86_REG_AL) {
1786 return EMIT_OP_IMM8(0x34);
1787 } else {
1788 return EMIT_OP_SPARE_REG_IMM8(0x80, 6);
1790 case CG_X86_OP_MOV:
1791 return EMIT_OP_IMM8(0xb0 + register_code[reg]);
1792 default:
1793 INVALID_INSTRUCTION
1795 return n;
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
1800 * argument.
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) {
1812 int n = 0, m;
1813 switch(opcode) {
1814 case CG_X86_OP_ADC:
1815 return EMIT_OP_MEM_REG8(0x12);
1816 case CG_X86_OP_ADD:
1817 return EMIT_OP_MEM_REG8(0x02);
1818 case CG_X86_OP_AND:
1819 return EMIT_OP_MEM_REG8(0x22);
1820 case CG_X86_OP_CMP:
1821 return EMIT_OP_MEM_REG8(0x3a);
1822 case CG_X86_OP_OR:
1823 return EMIT_OP_MEM_REG8(0x0a);
1824 case CG_X86_OP_XOR:
1825 return EMIT_OP_MEM_REG8(0x32);
1826 default:
1827 INVALID_INSTRUCTION
1829 return n;
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) {
1841 int n = 0, m;
1842 switch(opcode) {
1843 case CG_X86_OP_ADC:
1844 return EMIT_OP_REG_REG8(0x12);
1845 case CG_X86_OP_ADD:
1846 return EMIT_OP_REG_REG8(0x02);
1847 case CG_X86_OP_AND:
1848 return EMIT_OP_REG_REG8(0x22);
1849 case CG_X86_OP_CMP:
1850 return EMIT_OP_REG_REG8(0x3a);
1851 case CG_X86_OP_OR:
1852 return EMIT_OP_REG_REG8(0x0a);
1853 case CG_X86_OP_XOR:
1854 return EMIT_OP_REG_REG8(0x32);
1855 default:
1856 INVALID_INSTRUCTION
1858 return n;