1 /* src/vm/jit/i386/emit.c - i386 code emitter functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include "vm/jit/i386/codegen.h"
33 #include "vm/jit/i386/emit.h"
34 #include "vm/jit/i386/md-abi.h"
36 #include "mm/memory.h"
38 #include "threads/lock-common.h"
40 #include "vm/jit/abi.h"
41 #include "vm/jit/asmpart.h"
42 #include "vm/jit/dseg.h"
43 #include "vm/jit/emit-common.h"
44 #include "vm/jit/jit.h"
45 #include "vm/jit/patcher-common.h"
46 #include "vm/jit/replace.h"
47 #include "vm/jit/trace.hpp"
48 #include "vm/jit/trap.h"
50 #include "vmcore/options.h"
51 #include "vmcore/statistics.h"
54 /* emit_load ******************************************************************
56 Emits a possible load of an operand.
58 *******************************************************************************/
60 inline s4 emit_load(jitdata *jd, instruction *iptr, varinfo *src, s4 tempreg)
66 /* get required compiler data */
70 if (IS_INMEMORY(src->flags)) {
73 disp = src->vv.regoff;
78 M_ILD(tempreg, REG_SP, disp);
81 M_LLD(tempreg, REG_SP, disp);
84 M_FLD(tempreg, REG_SP, disp);
87 M_DLD(tempreg, REG_SP, disp);
90 vm_abort("emit_load: unknown type %d", src->type);
102 /* emit_load_low ************************************************************
104 Emits a possible load of the low 32-bits of an operand.
106 *******************************************************************************/
108 inline s4 emit_load_low(jitdata *jd, instruction *iptr, varinfo *src,s4 tempreg)
114 assert(src->type == TYPE_LNG);
116 /* get required compiler data */
121 if (IS_INMEMORY(src->flags)) {
124 disp = src->vv.regoff;
126 M_ILD(tempreg, REG_SP, disp);
131 reg = GET_LOW_REG(src->vv.regoff);
137 /* emit_load_high ***********************************************************
139 Emits a possible load of the high 32-bits of an operand.
141 *******************************************************************************/
143 inline s4 emit_load_high(jitdata *jd, instruction *iptr,varinfo *src,s4 tempreg)
149 /* get required compiler data */
151 assert(src->type == TYPE_LNG);
155 if (IS_INMEMORY(src->flags)) {
158 disp = src->vv.regoff;
160 M_ILD(tempreg, REG_SP, disp + 4);
165 reg = GET_HIGH_REG(src->vv.regoff);
171 /* emit_store ******************************************************************
173 Emits a possible store of the destination operand.
175 *******************************************************************************/
177 inline void emit_store(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
182 /* get required compiler data */
186 if (IS_INMEMORY(dst->flags)) {
189 disp = dst->vv.regoff;
194 M_IST(d, REG_SP, disp);
197 M_LST(d, REG_SP, disp);
200 M_FST(d, REG_SP, disp);
203 M_DST(d, REG_SP, disp);
206 vm_abort("emit_store: unknown type %d", dst->type);
212 /* emit_store_low **************************************************************
214 Emits a possible store of the low 32-bits of the destination
217 *******************************************************************************/
219 inline void emit_store_low(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
223 assert(dst->type == TYPE_LNG);
225 /* get required compiler data */
229 if (IS_INMEMORY(dst->flags)) {
231 M_IST(GET_LOW_REG(d), REG_SP, dst->vv.regoff);
236 /* emit_store_high *************************************************************
238 Emits a possible store of the high 32-bits of the destination
241 *******************************************************************************/
243 inline void emit_store_high(jitdata *jd, instruction *iptr, varinfo *dst, s4 d)
247 assert(dst->type == TYPE_LNG);
249 /* get required compiler data */
253 if (IS_INMEMORY(dst->flags)) {
255 M_IST(GET_HIGH_REG(d), REG_SP, dst->vv.regoff + 4);
260 /* emit_copy *******************************************************************
262 Generates a register/memory to register/memory copy.
264 *******************************************************************************/
266 void emit_copy(jitdata *jd, instruction *iptr)
273 /* get required compiler data */
277 /* get source and destination variables */
279 src = VAROP(iptr->s1);
280 dst = VAROP(iptr->dst);
282 if ((src->vv.regoff != dst->vv.regoff) ||
283 ((src->flags ^ dst->flags) & INMEMORY)) {
285 if ((src->type == TYPE_RET) || (dst->type == TYPE_RET)) {
286 /* emit nothing, as the value won't be used anyway */
290 /* If one of the variables resides in memory, we can eliminate
291 the register move from/to the temporary register with the
292 order of getting the destination register and the load. */
294 if (IS_INMEMORY(src->flags)) {
295 if (IS_LNG_TYPE(src->type))
296 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP12_PACKED);
298 d = codegen_reg_of_var(iptr->opc, dst, REG_ITMP1);
300 s1 = emit_load(jd, iptr, src, d);
303 if (IS_LNG_TYPE(src->type))
304 s1 = emit_load(jd, iptr, src, REG_ITMP12_PACKED);
306 s1 = emit_load(jd, iptr, src, REG_ITMP1);
308 d = codegen_reg_of_var(iptr->opc, dst, s1);
325 vm_abort("emit_copy: unknown type %d", src->type);
329 emit_store(jd, iptr, dst, d);
334 /* emit_branch *****************************************************************
336 Emits the code for conditional and unconditional branchs.
338 *******************************************************************************/
340 void emit_branch(codegendata *cd, s4 disp, s4 condition, s4 reg, u4 options)
344 /* ATTENTION: a displacement overflow cannot happen */
346 /* check which branch to generate */
348 if (condition == BRANCH_UNCONDITIONAL) {
350 /* calculate the different displacements */
352 branchdisp = disp - BRANCH_UNCONDITIONAL_SIZE;
354 M_JMP_IMM(branchdisp);
357 /* calculate the different displacements */
359 branchdisp = disp - BRANCH_CONDITIONAL_SIZE;
393 vm_abort("emit_branch: unknown condition %d", condition);
399 /* emit_arithmetic_check *******************************************************
401 Emit an ArithmeticException check.
403 *******************************************************************************/
405 void emit_arithmetic_check(codegendata *cd, instruction *iptr, s4 reg)
407 if (INSTRUCTION_MUST_CHECK(iptr)) {
410 M_ALD_MEM(reg, TRAP_ArithmeticException);
415 /* emit_arrayindexoutofbounds_check ********************************************
417 Emit a ArrayIndexOutOfBoundsException check.
419 *******************************************************************************/
421 void emit_arrayindexoutofbounds_check(codegendata *cd, instruction *iptr, s4 s1, s4 s2)
423 if (INSTRUCTION_MUST_CHECK(iptr)) {
424 M_ILD(REG_ITMP3, s1, OFFSET(java_array_t, size));
425 M_CMP(REG_ITMP3, s2);
427 M_ALD_MEM(s2, TRAP_ArrayIndexOutOfBoundsException);
432 /* emit_arraystore_check *******************************************************
434 Emit an ArrayStoreException check.
436 *******************************************************************************/
438 void emit_arraystore_check(codegendata *cd, instruction *iptr)
440 if (INSTRUCTION_MUST_CHECK(iptr)) {
443 M_ALD_MEM(REG_RESULT, TRAP_ArrayStoreException);
448 /* emit_classcast_check ********************************************************
450 Emit a ClassCastException check.
452 *******************************************************************************/
454 void emit_classcast_check(codegendata *cd, instruction *iptr, s4 condition, s4 reg, s4 s1)
456 if (INSTRUCTION_MUST_CHECK(iptr)) {
468 vm_abort("emit_classcast_check: unknown condition %d", condition);
470 M_ALD_MEM(s1, TRAP_ClassCastException);
475 /* emit_nullpointer_check ******************************************************
477 Emit a NullPointerException check.
479 *******************************************************************************/
481 void emit_nullpointer_check(codegendata *cd, instruction *iptr, s4 reg)
483 if (INSTRUCTION_MUST_CHECK(iptr)) {
486 M_ALD_MEM(reg, TRAP_NullPointerException);
491 /* emit_exception_check ********************************************************
493 Emit an Exception check.
495 *******************************************************************************/
497 void emit_exception_check(codegendata *cd, instruction *iptr)
499 if (INSTRUCTION_MUST_CHECK(iptr)) {
502 M_ALD_MEM(REG_RESULT, TRAP_CHECK_EXCEPTION);
507 /* emit_trap_compiler **********************************************************
509 Emit a trap instruction which calls the JIT compiler.
511 *******************************************************************************/
513 void emit_trap_compiler(codegendata *cd)
515 M_ALD_MEM(REG_METHODPTR, TRAP_COMPILER);
518 /* emit_trap_countdown *********************************************************
520 Emit a countdown trap.
522 counter....absolute address of the counter variable
524 *******************************************************************************/
526 void emit_trap_countdown(codegendata *cd, s4 *counter)
528 M_ISUB_IMM_MEMABS(1, (s4) counter);
530 M_ALD_MEM(REG_METHODPTR, TRAP_COUNTDOWN);
533 /* emit_trap *******************************************************************
535 Emit a trap instruction and return the original machine code.
537 *******************************************************************************/
539 uint32_t emit_trap(codegendata *cd)
543 /* Get machine code which is patched back in later. The
544 trap is 2 bytes long. */
546 mcode = *((uint16_t *) cd->mcodeptr);
549 /* XXX this breaks GDB, so we disable it for now */
550 *(cd->mcodeptr++) = 0xcc;
556 return (uint32_t) mcode;
560 /* emit_verbosecall_enter ******************************************************
562 Generates the code for the call trace.
564 *******************************************************************************/
567 void emit_verbosecall_enter(jitdata *jd)
574 int32_t stackframesize;
576 int align_off; /* offset for alignment compensation */
578 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
581 /* get required compiler data */
590 /* mark trace code */
594 /* keep stack 16-byte aligned */
596 stackframesize = 2 + TMP_CNT;
597 ALIGN_2(stackframesize);
599 M_ASUB_IMM(stackframesize * 8, REG_SP);
601 /* save temporary registers for leaf methods */
603 if (code_is_leafmethod(code)) {
604 for (i = 0; i < INT_TMP_CNT; i++)
605 M_IST(rd->tmpintregs[i], REG_SP, (2 + i) * 8);
608 /* no argument registers to save */
610 align_off = cd->stackframesize ? 4 : 0;
611 M_AST_IMM(m, REG_SP, 0 * 4);
612 M_AST_IMM(0, REG_SP, 1 * 4);
613 M_AST(REG_SP, REG_SP, 2 * 4);
614 M_IADD_IMM_MEMBASE(stackframesize * 8 + cd->stackframesize * 8 + 4 + align_off, REG_SP, 2 * 4);
615 M_MOV_IMM(trace_java_call_enter, REG_ITMP1);
618 /* no argument registers to restore */
620 /* restore temporary registers for leaf methods */
622 if (code_is_leafmethod(code)) {
623 for (i = 0; i < INT_TMP_CNT; i++)
624 M_ILD(rd->tmpintregs[i], REG_SP, (2 + i) * 8);
627 M_AADD_IMM(stackframesize * 8, REG_SP);
629 /* mark trace code */
633 #endif /* !defined(NDEBUG) */
636 /* emit_verbosecall_exit *******************************************************
638 Generates the code for the call trace.
640 *******************************************************************************/
643 void emit_verbosecall_exit(jitdata *jd)
650 if (!JITDATA_HAS_FLAG_VERBOSECALL(jd))
653 /* get required compiler data */
661 /* mark trace code */
665 /* keep stack 16-byte aligned */
667 M_ASUB_IMM(4 + 4 + 8, REG_SP);
669 /* save return value */
671 switch (md->returntype.type) {
674 M_IST(REG_RESULT, REG_SP, 2 * 4);
677 M_LST(REG_RESULT_PACKED, REG_SP, 2 * 4);
680 M_FSTNP(REG_NULL, REG_SP, 2 * 4);
683 M_DSTNP(REG_NULL, REG_SP, 2 * 4);
687 M_AST_IMM(m, REG_SP, 0 * 4);
688 M_AST(REG_SP, REG_SP, 1 * 4);
689 M_IADD_IMM_MEMBASE(2 * 4, REG_SP, 1 * 4);
690 M_MOV_IMM(trace_java_call_exit, REG_ITMP1);
693 /* restore return value */
695 switch (md->returntype.type) {
698 M_ILD(REG_RESULT, REG_SP, 2 * 4);
701 M_LLD(REG_RESULT_PACKED, REG_SP, 2 * 4);
705 M_AADD_IMM(4 + 4 + 8, REG_SP);
707 /* mark trace code */
711 #endif /* !defined(NDEBUG) */
714 /* code generation functions **************************************************/
716 static void emit_membase(codegendata *cd, s4 basereg, s4 disp, s4 dreg)
718 if (basereg == ESP) {
720 emit_address_byte(0, dreg, ESP);
721 emit_address_byte(0, ESP, ESP);
723 else if (IS_IMM8(disp)) {
724 emit_address_byte(1, dreg, ESP);
725 emit_address_byte(0, ESP, ESP);
729 emit_address_byte(2, dreg, ESP);
730 emit_address_byte(0, ESP, ESP);
734 else if ((disp == 0) && (basereg != EBP)) {
735 emit_address_byte(0, dreg, basereg);
737 else if (IS_IMM8(disp)) {
738 emit_address_byte(1, dreg, basereg);
742 emit_address_byte(2, dreg, basereg);
748 static void emit_membase32(codegendata *cd, s4 basereg, s4 disp, s4 dreg)
750 if (basereg == ESP) {
751 emit_address_byte(2, dreg, ESP);
752 emit_address_byte(0, ESP, ESP);
756 emit_address_byte(2, dreg, basereg);
762 static void emit_memindex(codegendata *cd, s4 reg, s4 disp, s4 basereg, s4 indexreg, s4 scale)
765 emit_address_byte(0, reg, 4);
766 emit_address_byte(scale, indexreg, 5);
769 else if ((disp == 0) && (basereg != EBP)) {
770 emit_address_byte(0, reg, 4);
771 emit_address_byte(scale, indexreg, basereg);
773 else if (IS_IMM8(disp)) {
774 emit_address_byte(1, reg, 4);
775 emit_address_byte(scale, indexreg, basereg);
779 emit_address_byte(2, reg, 4);
780 emit_address_byte(scale, indexreg, basereg);
786 /* low-level code emitter functions *******************************************/
788 void emit_mov_reg_reg(codegendata *cd, s4 reg, s4 dreg)
790 COUNT(count_mov_reg_reg);
791 *(cd->mcodeptr++) = 0x89;
792 emit_reg((reg),(dreg));
796 void emit_mov_imm_reg(codegendata *cd, s4 imm, s4 reg)
798 *(cd->mcodeptr++) = 0xb8 + ((reg) & 0x07);
803 void emit_movb_imm_reg(codegendata *cd, s4 imm, s4 reg)
805 *(cd->mcodeptr++) = 0xc6;
811 void emit_mov_membase_reg(codegendata *cd, s4 basereg, s4 disp, s4 reg)
813 COUNT(count_mov_mem_reg);
814 *(cd->mcodeptr++) = 0x8b;
815 emit_membase(cd, (basereg),(disp),(reg));
820 * this one is for INVOKEVIRTUAL/INVOKEINTERFACE to have a
821 * constant membase immediate length of 32bit
823 void emit_mov_membase32_reg(codegendata *cd, s4 basereg, s4 disp, s4 reg)
825 COUNT(count_mov_mem_reg);
826 *(cd->mcodeptr++) = 0x8b;
827 emit_membase32(cd, (basereg),(disp),(reg));
831 void emit_mov_reg_membase(codegendata *cd, s4 reg, s4 basereg, s4 disp)
833 COUNT(count_mov_reg_mem);
834 *(cd->mcodeptr++) = 0x89;
835 emit_membase(cd, (basereg),(disp),(reg));
839 void emit_mov_reg_membase32(codegendata *cd, s4 reg, s4 basereg, s4 disp)
841 COUNT(count_mov_reg_mem);
842 *(cd->mcodeptr++) = 0x89;
843 emit_membase32(cd, (basereg),(disp),(reg));
847 void emit_mov_memindex_reg(codegendata *cd, s4 disp, s4 basereg, s4 indexreg, s4 scale, s4 reg)
849 COUNT(count_mov_mem_reg);
850 *(cd->mcodeptr++) = 0x8b;
851 emit_memindex(cd, (reg),(disp),(basereg),(indexreg),(scale));
855 void emit_mov_reg_memindex(codegendata *cd, s4 reg, s4 disp, s4 basereg, s4 indexreg, s4 scale)
857 COUNT(count_mov_reg_mem);
858 *(cd->mcodeptr++) = 0x89;
859 emit_memindex(cd, (reg),(disp),(basereg),(indexreg),(scale));
863 void emit_movw_reg_memindex(codegendata *cd, s4 reg, s4 disp, s4 basereg, s4 indexreg, s4 scale)
865 COUNT(count_mov_reg_mem);
866 *(cd->mcodeptr++) = 0x66;
867 *(cd->mcodeptr++) = 0x89;
868 emit_memindex(cd, (reg),(disp),(basereg),(indexreg),(scale));
872 void emit_movb_reg_memindex(codegendata *cd, s4 reg, s4 disp, s4 basereg, s4 indexreg, s4 scale)
874 COUNT(count_mov_reg_mem);
875 *(cd->mcodeptr++) = 0x88;
876 emit_memindex(cd, (reg),(disp),(basereg),(indexreg),(scale));
880 void emit_mov_reg_mem(codegendata *cd, s4 reg, s4 mem)
882 COUNT(count_mov_reg_mem);
883 *(cd->mcodeptr++) = 0x89;
884 emit_mem((reg),(mem));
888 void emit_mov_mem_reg(codegendata *cd, s4 mem, s4 dreg)
890 COUNT(count_mov_mem_reg);
891 *(cd->mcodeptr++) = 0x8b;
892 emit_mem((dreg),(mem));
896 void emit_mov_imm_mem(codegendata *cd, s4 imm, s4 mem)
898 *(cd->mcodeptr++) = 0xc7;
904 void emit_mov_imm_membase(codegendata *cd, s4 imm, s4 basereg, s4 disp)
906 *(cd->mcodeptr++) = 0xc7;
907 emit_membase(cd, (basereg),(disp),0);
912 void emit_mov_imm_membase32(codegendata *cd, s4 imm, s4 basereg, s4 disp)
914 *(cd->mcodeptr++) = 0xc7;
915 emit_membase32(cd, (basereg),(disp),0);
920 void emit_movb_imm_membase(codegendata *cd, s4 imm, s4 basereg, s4 disp)
922 *(cd->mcodeptr++) = 0xc6;
923 emit_membase(cd, (basereg),(disp),0);
928 void emit_movsbl_reg_reg(codegendata *cd, s4 a, s4 b)
930 assert(a < 4); /* Can only operate on al, bl, cl, dl. */
931 *(cd->mcodeptr++) = 0x0f;
932 *(cd->mcodeptr++) = 0xbe;
937 void emit_movsbl_memindex_reg(codegendata *cd, s4 disp, s4 basereg, s4 indexreg, s4 scale, s4 reg)
939 COUNT(count_mov_mem_reg);
940 *(cd->mcodeptr++) = 0x0f;
941 *(cd->mcodeptr++) = 0xbe;
942 emit_memindex(cd, (reg),(disp),(basereg),(indexreg),(scale));
946 void emit_movswl_reg_reg(codegendata *cd, s4 a, s4 b)
948 *(cd->mcodeptr++) = 0x0f;
949 *(cd->mcodeptr++) = 0xbf;
954 void emit_movswl_memindex_reg(codegendata *cd, s4 disp, s4 basereg, s4 indexreg, s4 scale, s4 reg)
956 COUNT(count_mov_mem_reg);
957 *(cd->mcodeptr++) = 0x0f;
958 *(cd->mcodeptr++) = 0xbf;
959 emit_memindex(cd, (reg),(disp),(basereg),(indexreg),(scale));
963 void emit_movzbl_reg_reg(codegendata *cd, s4 a, s4 b)
965 assert(a < 4); /* Can only operate on al, bl, cl, dl. */
966 *(cd->mcodeptr++) = 0x0f;
967 *(cd->mcodeptr++) = 0xb6;
972 void emit_movzwl_reg_reg(codegendata *cd, s4 a, s4 b)
974 *(cd->mcodeptr++) = 0x0f;
975 *(cd->mcodeptr++) = 0xb7;
980 void emit_movzwl_memindex_reg(codegendata *cd, s4 disp, s4 basereg, s4 indexreg, s4 scale, s4 reg)
982 COUNT(count_mov_mem_reg);
983 *(cd->mcodeptr++) = 0x0f;
984 *(cd->mcodeptr++) = 0xb7;
985 emit_memindex(cd, (reg),(disp),(basereg),(indexreg),(scale));
989 void emit_mov_imm_memindex(codegendata *cd, s4 imm, s4 disp, s4 basereg, s4 indexreg, s4 scale)
991 *(cd->mcodeptr++) = 0xc7;
992 emit_memindex(cd, 0,(disp),(basereg),(indexreg),(scale));
997 void emit_movw_imm_memindex(codegendata *cd, s4 imm, s4 disp, s4 basereg, s4 indexreg, s4 scale)
999 *(cd->mcodeptr++) = 0x66;
1000 *(cd->mcodeptr++) = 0xc7;
1001 emit_memindex(cd, 0,(disp),(basereg),(indexreg),(scale));
1006 void emit_movb_imm_memindex(codegendata *cd, s4 imm, s4 disp, s4 basereg, s4 indexreg, s4 scale)
1008 *(cd->mcodeptr++) = 0xc6;
1009 emit_memindex(cd, 0,(disp),(basereg),(indexreg),(scale));
1017 void emit_alu_reg_reg(codegendata *cd, s4 opc, s4 reg, s4 dreg)
1019 *(cd->mcodeptr++) = (((u1) (opc)) << 3) + 1;
1020 emit_reg((reg),(dreg));
1024 void emit_alu_reg_membase(codegendata *cd, s4 opc, s4 reg, s4 basereg, s4 disp)
1026 *(cd->mcodeptr++) = (((u1) (opc)) << 3) + 1;
1027 emit_membase(cd, (basereg),(disp),(reg));
1031 void emit_alu_membase_reg(codegendata *cd, s4 opc, s4 basereg, s4 disp, s4 reg)
1033 *(cd->mcodeptr++) = (((u1) (opc)) << 3) + 3;
1034 emit_membase(cd, (basereg),(disp),(reg));
1038 void emit_alu_imm_reg(codegendata *cd, s4 opc, s4 imm, s4 dreg)
1041 *(cd->mcodeptr++) = 0x83;
1042 emit_reg((opc),(dreg));
1045 *(cd->mcodeptr++) = 0x81;
1046 emit_reg((opc),(dreg));
1052 void emit_alu_imm32_reg(codegendata *cd, s4 opc, s4 imm, s4 dreg)
1054 *(cd->mcodeptr++) = 0x81;
1055 emit_reg((opc),(dreg));
1060 void emit_alu_imm_membase(codegendata *cd, s4 opc, s4 imm, s4 basereg, s4 disp)
1063 *(cd->mcodeptr++) = 0x83;
1064 emit_membase(cd, (basereg),(disp),(opc));
1067 *(cd->mcodeptr++) = 0x81;
1068 emit_membase(cd, (basereg),(disp),(opc));
1074 void emit_alu_imm_memabs(codegendata *cd, s4 opc, s4 imm, s4 disp)
1077 *(cd->mcodeptr++) = 0x83;
1078 emit_mem(opc, disp);
1081 *(cd->mcodeptr++) = 0x81;
1082 emit_mem(opc, disp);
1088 void emit_test_reg_reg(codegendata *cd, s4 reg, s4 dreg)
1090 *(cd->mcodeptr++) = 0x85;
1091 emit_reg((reg),(dreg));
1095 void emit_test_imm_reg(codegendata *cd, s4 imm, s4 reg)
1097 *(cd->mcodeptr++) = 0xf7;
1105 * inc, dec operations
1107 void emit_dec_mem(codegendata *cd, s4 mem)
1109 *(cd->mcodeptr++) = 0xff;
1114 void emit_imul_reg_reg(codegendata *cd, s4 reg, s4 dreg)
1116 *(cd->mcodeptr++) = 0x0f;
1117 *(cd->mcodeptr++) = 0xaf;
1118 emit_reg((dreg),(reg));
1122 void emit_imul_membase_reg(codegendata *cd, s4 basereg, s4 disp, s4 dreg)
1124 *(cd->mcodeptr++) = 0x0f;
1125 *(cd->mcodeptr++) = 0xaf;
1126 emit_membase(cd, (basereg),(disp),(dreg));
1130 void emit_imul_imm_reg(codegendata *cd, s4 imm, s4 dreg)
1132 if (IS_IMM8((imm))) {
1133 *(cd->mcodeptr++) = 0x6b;
1137 *(cd->mcodeptr++) = 0x69;
1144 void emit_imul_imm_reg_reg(codegendata *cd, s4 imm, s4 reg, s4 dreg)
1146 if (IS_IMM8((imm))) {
1147 *(cd->mcodeptr++) = 0x6b;
1148 emit_reg((dreg),(reg));
1151 *(cd->mcodeptr++) = 0x69;
1152 emit_reg((dreg),(reg));
1158 void emit_imul_imm_membase_reg(codegendata *cd, s4 imm, s4 basereg, s4 disp, s4 dreg)
1160 if (IS_IMM8((imm))) {
1161 *(cd->mcodeptr++) = 0x6b;
1162 emit_membase(cd, (basereg),(disp),(dreg));
1165 *(cd->mcodeptr++) = 0x69;
1166 emit_membase(cd, (basereg),(disp),(dreg));
1172 void emit_mul_reg(codegendata *cd, s4 reg)
1174 *(cd->mcodeptr++) = 0xf7;
1179 void emit_mul_membase(codegendata *cd, s4 basereg, s4 disp)
1181 *(cd->mcodeptr++) = 0xf7;
1182 emit_membase(cd, (basereg),(disp),4);
1186 void emit_idiv_reg(codegendata *cd, s4 reg)
1188 *(cd->mcodeptr++) = 0xf7;
1197 void emit_shift_reg(codegendata *cd, s4 opc, s4 reg)
1199 *(cd->mcodeptr++) = 0xd3;
1200 emit_reg((opc),(reg));
1204 void emit_shift_imm_reg(codegendata *cd, s4 opc, s4 imm, s4 dreg)
1207 *(cd->mcodeptr++) = 0xd1;
1208 emit_reg((opc),(dreg));
1210 *(cd->mcodeptr++) = 0xc1;
1211 emit_reg((opc),(dreg));
1217 void emit_shld_reg_reg(codegendata *cd, s4 reg, s4 dreg)
1219 *(cd->mcodeptr++) = 0x0f;
1220 *(cd->mcodeptr++) = 0xa5;
1221 emit_reg((reg),(dreg));
1225 void emit_shld_imm_reg_reg(codegendata *cd, s4 imm, s4 reg, s4 dreg)
1227 *(cd->mcodeptr++) = 0x0f;
1228 *(cd->mcodeptr++) = 0xa4;
1229 emit_reg((reg),(dreg));
1234 void emit_shld_reg_membase(codegendata *cd, s4 reg, s4 basereg, s4 disp)
1236 *(cd->mcodeptr++) = 0x0f;
1237 *(cd->mcodeptr++) = 0xa5;
1238 emit_membase(cd, (basereg),(disp),(reg));
1242 void emit_shrd_reg_reg(codegendata *cd, s4 reg, s4 dreg)
1244 *(cd->mcodeptr++) = 0x0f;
1245 *(cd->mcodeptr++) = 0xad;
1246 emit_reg((reg),(dreg));
1250 void emit_shrd_imm_reg_reg(codegendata *cd, s4 imm, s4 reg, s4 dreg)
1252 *(cd->mcodeptr++) = 0x0f;
1253 *(cd->mcodeptr++) = 0xac;
1254 emit_reg((reg),(dreg));
1259 void emit_shrd_reg_membase(codegendata *cd, s4 reg, s4 basereg, s4 disp)
1261 *(cd->mcodeptr++) = 0x0f;
1262 *(cd->mcodeptr++) = 0xad;
1263 emit_membase(cd, (basereg),(disp),(reg));
1271 void emit_jmp_imm(codegendata *cd, s4 imm)
1273 *(cd->mcodeptr++) = 0xe9;
1278 void emit_jmp_reg(codegendata *cd, s4 reg)
1280 *(cd->mcodeptr++) = 0xff;
1285 void emit_jcc(codegendata *cd, s4 opc, s4 imm)
1287 *(cd->mcodeptr++) = 0x0f;
1288 *(cd->mcodeptr++) = 0x80 + (u1) (opc);
1295 * conditional set operations
1297 void emit_setcc_reg(codegendata *cd, s4 opc, s4 reg)
1299 *(cd->mcodeptr++) = 0x0f;
1300 *(cd->mcodeptr++) = 0x90 + (u1) (opc);
1305 void emit_setcc_membase(codegendata *cd, s4 opc, s4 basereg, s4 disp)
1307 *(cd->mcodeptr++) = 0x0f;
1308 *(cd->mcodeptr++) = 0x90 + (u1) (opc);
1309 emit_membase(cd, (basereg),(disp),0);
1313 void emit_xadd_reg_mem(codegendata *cd, s4 reg, s4 mem)
1315 *(cd->mcodeptr++) = 0x0f;
1316 *(cd->mcodeptr++) = 0xc1;
1317 emit_mem((reg),(mem));
1321 void emit_neg_reg(codegendata *cd, s4 reg)
1323 *(cd->mcodeptr++) = 0xf7;
1329 void emit_push_imm(codegendata *cd, s4 imm)
1331 *(cd->mcodeptr++) = 0x68;
1336 void emit_pop_reg(codegendata *cd, s4 reg)
1338 *(cd->mcodeptr++) = 0x58 + (0x07 & (u1) (reg));
1342 void emit_push_reg(codegendata *cd, s4 reg)
1344 *(cd->mcodeptr++) = 0x50 + (0x07 & (u1) (reg));
1348 void emit_lock(codegendata *cd)
1350 *(cd->mcodeptr++) = 0xf0;
1357 void emit_call_reg(codegendata *cd, s4 reg)
1359 *(cd->mcodeptr++) = 0xff;
1364 void emit_call_imm(codegendata *cd, s4 imm)
1366 *(cd->mcodeptr++) = 0xe8;
1373 * floating point instructions
1375 void emit_fld1(codegendata *cd)
1377 *(cd->mcodeptr++) = 0xd9;
1378 *(cd->mcodeptr++) = 0xe8;
1382 void emit_fldz(codegendata *cd)
1384 *(cd->mcodeptr++) = 0xd9;
1385 *(cd->mcodeptr++) = 0xee;
1389 void emit_fld_reg(codegendata *cd, s4 reg)
1391 *(cd->mcodeptr++) = 0xd9;
1392 *(cd->mcodeptr++) = 0xc0 + (0x07 & (u1) (reg));
1396 void emit_flds_membase(codegendata *cd, s4 basereg, s4 disp)
1398 *(cd->mcodeptr++) = 0xd9;
1399 emit_membase(cd, (basereg),(disp),0);
1403 void emit_flds_membase32(codegendata *cd, s4 basereg, s4 disp)
1405 *(cd->mcodeptr++) = 0xd9;
1406 emit_membase32(cd, (basereg),(disp),0);
1410 void emit_fldl_membase(codegendata *cd, s4 basereg, s4 disp)
1412 *(cd->mcodeptr++) = 0xdd;
1413 emit_membase(cd, (basereg),(disp),0);
1417 void emit_fldl_membase32(codegendata *cd, s4 basereg, s4 disp)
1419 *(cd->mcodeptr++) = 0xdd;
1420 emit_membase32(cd, (basereg),(disp),0);
1424 void emit_fldt_membase(codegendata *cd, s4 basereg, s4 disp)
1426 *(cd->mcodeptr++) = 0xdb;
1427 emit_membase(cd, (basereg),(disp),5);
1431 void emit_flds_memindex(codegendata *cd, s4 disp, s4 basereg, s4 indexreg, s4 scale)
1433 *(cd->mcodeptr++) = 0xd9;
1434 emit_memindex(cd, 0,(disp),(basereg),(indexreg),(scale));
1438 void emit_fldl_memindex(codegendata *cd, s4 disp, s4 basereg, s4 indexreg, s4 scale)
1440 *(cd->mcodeptr++) = 0xdd;
1441 emit_memindex(cd, 0,(disp),(basereg),(indexreg),(scale));
1445 void emit_flds_mem(codegendata *cd, s4 mem)
1447 *(cd->mcodeptr++) = 0xd9;
1452 void emit_fldl_mem(codegendata *cd, s4 mem)
1454 *(cd->mcodeptr++) = 0xdd;
1459 void emit_fildl_membase(codegendata *cd, s4 basereg, s4 disp)
1461 *(cd->mcodeptr++) = 0xdb;
1462 emit_membase(cd, (basereg),(disp),0);
1466 void emit_fildll_membase(codegendata *cd, s4 basereg, s4 disp)
1468 *(cd->mcodeptr++) = 0xdf;
1469 emit_membase(cd, (basereg),(disp),5);
1473 void emit_fst_reg(codegendata *cd, s4 reg)
1475 *(cd->mcodeptr++) = 0xdd;
1476 *(cd->mcodeptr++) = 0xd0 + (0x07 & (u1) (reg));
1480 void emit_fsts_membase(codegendata *cd, s4 basereg, s4 disp)
1482 *(cd->mcodeptr++) = 0xd9;
1483 emit_membase(cd, (basereg),(disp),2);
1487 void emit_fstl_membase(codegendata *cd, s4 basereg, s4 disp)
1489 *(cd->mcodeptr++) = 0xdd;
1490 emit_membase(cd, (basereg),(disp),2);
1494 void emit_fsts_memindex(codegendata *cd, s4 disp, s4 basereg, s4 indexreg, s4 scale)
1496 *(cd->mcodeptr++) = 0xd9;
1497 emit_memindex(cd, 2,(disp),(basereg),(indexreg),(scale));
1501 void emit_fstl_memindex(codegendata *cd, s4 disp, s4 basereg, s4 indexreg, s4 scale)
1503 *(cd->mcodeptr++) = 0xdd;
1504 emit_memindex(cd, 2,(disp),(basereg),(indexreg),(scale));
1508 void emit_fstp_reg(codegendata *cd, s4 reg)
1510 *(cd->mcodeptr++) = 0xdd;
1511 *(cd->mcodeptr++) = 0xd8 + (0x07 & (u1) (reg));
1515 void emit_fstps_membase(codegendata *cd, s4 basereg, s4 disp)
1517 *(cd->mcodeptr++) = 0xd9;
1518 emit_membase(cd, (basereg),(disp),3);
1522 void emit_fstps_membase32(codegendata *cd, s4 basereg, s4 disp)
1524 *(cd->mcodeptr++) = 0xd9;
1525 emit_membase32(cd, (basereg),(disp),3);
1529 void emit_fstpl_membase(codegendata *cd, s4 basereg, s4 disp)
1531 *(cd->mcodeptr++) = 0xdd;
1532 emit_membase(cd, (basereg),(disp),3);
1536 void emit_fstpl_membase32(codegendata *cd, s4 basereg, s4 disp)
1538 *(cd->mcodeptr++) = 0xdd;
1539 emit_membase32(cd, (basereg),(disp),3);
1543 void emit_fstpt_membase(codegendata *cd, s4 basereg, s4 disp)
1545 *(cd->mcodeptr++) = 0xdb;
1546 emit_membase(cd, (basereg),(disp),7);
1550 void emit_fstps_memindex(codegendata *cd, s4 disp, s4 basereg, s4 indexreg, s4 scale)
1552 *(cd->mcodeptr++) = 0xd9;
1553 emit_memindex(cd, 3,(disp),(basereg),(indexreg),(scale));
1557 void emit_fstpl_memindex(codegendata *cd, s4 disp, s4 basereg, s4 indexreg, s4 scale)
1559 *(cd->mcodeptr++) = 0xdd;
1560 emit_memindex(cd, 3,(disp),(basereg),(indexreg),(scale));
1564 void emit_fstps_mem(codegendata *cd, s4 mem)
1566 *(cd->mcodeptr++) = 0xd9;
1571 void emit_fstpl_mem(codegendata *cd, s4 mem)
1573 *(cd->mcodeptr++) = 0xdd;
1578 void emit_fistl_membase(codegendata *cd, s4 basereg, s4 disp)
1580 *(cd->mcodeptr++) = 0xdb;
1581 emit_membase(cd, (basereg),(disp),2);
1585 void emit_fistpl_membase(codegendata *cd, s4 basereg, s4 disp)
1587 *(cd->mcodeptr++) = 0xdb;
1588 emit_membase(cd, (basereg),(disp),3);
1592 void emit_fistpll_membase(codegendata *cd, s4 basereg, s4 disp)
1594 *(cd->mcodeptr++) = 0xdf;
1595 emit_membase(cd, (basereg),(disp),7);
1599 void emit_fchs(codegendata *cd)
1601 *(cd->mcodeptr++) = 0xd9;
1602 *(cd->mcodeptr++) = 0xe0;
1606 void emit_faddp(codegendata *cd)
1608 *(cd->mcodeptr++) = 0xde;
1609 *(cd->mcodeptr++) = 0xc1;
1613 void emit_fadd_reg_st(codegendata *cd, s4 reg)
1615 *(cd->mcodeptr++) = 0xd8;
1616 *(cd->mcodeptr++) = 0xc0 + (0x0f & (u1) (reg));
1620 void emit_fadd_st_reg(codegendata *cd, s4 reg)
1622 *(cd->mcodeptr++) = 0xdc;
1623 *(cd->mcodeptr++) = 0xc0 + (0x0f & (u1) (reg));
1627 void emit_faddp_st_reg(codegendata *cd, s4 reg)
1629 *(cd->mcodeptr++) = 0xde;
1630 *(cd->mcodeptr++) = 0xc0 + (0x0f & (u1) (reg));
1634 void emit_fadds_membase(codegendata *cd, s4 basereg, s4 disp)
1636 *(cd->mcodeptr++) = 0xd8;
1637 emit_membase(cd, (basereg),(disp),0);
1641 void emit_faddl_membase(codegendata *cd, s4 basereg, s4 disp)
1643 *(cd->mcodeptr++) = 0xdc;
1644 emit_membase(cd, (basereg),(disp),0);
1648 void emit_fsub_reg_st(codegendata *cd, s4 reg)
1650 *(cd->mcodeptr++) = 0xd8;
1651 *(cd->mcodeptr++) = 0xe0 + (0x07 & (u1) (reg));
1655 void emit_fsub_st_reg(codegendata *cd, s4 reg)
1657 *(cd->mcodeptr++) = 0xdc;
1658 *(cd->mcodeptr++) = 0xe8 + (0x07 & (u1) (reg));
1662 void emit_fsubp_st_reg(codegendata *cd, s4 reg)
1664 *(cd->mcodeptr++) = 0xde;
1665 *(cd->mcodeptr++) = 0xe8 + (0x07 & (u1) (reg));
1669 void emit_fsubp(codegendata *cd)
1671 *(cd->mcodeptr++) = 0xde;
1672 *(cd->mcodeptr++) = 0xe9;
1676 void emit_fsubs_membase(codegendata *cd, s4 basereg, s4 disp)
1678 *(cd->mcodeptr++) = 0xd8;
1679 emit_membase(cd, (basereg),(disp),4);
1683 void emit_fsubl_membase(codegendata *cd, s4 basereg, s4 disp)
1685 *(cd->mcodeptr++) = 0xdc;
1686 emit_membase(cd, (basereg),(disp),4);
1690 void emit_fmul_reg_st(codegendata *cd, s4 reg)
1692 *(cd->mcodeptr++) = 0xd8;
1693 *(cd->mcodeptr++) = 0xc8 + (0x07 & (u1) (reg));
1697 void emit_fmul_st_reg(codegendata *cd, s4 reg)
1699 *(cd->mcodeptr++) = 0xdc;
1700 *(cd->mcodeptr++) = 0xc8 + (0x07 & (u1) (reg));
1704 void emit_fmulp(codegendata *cd)
1706 *(cd->mcodeptr++) = 0xde;
1707 *(cd->mcodeptr++) = 0xc9;
1711 void emit_fmulp_st_reg(codegendata *cd, s4 reg)
1713 *(cd->mcodeptr++) = 0xde;
1714 *(cd->mcodeptr++) = 0xc8 + (0x07 & (u1) (reg));
1718 void emit_fmuls_membase(codegendata *cd, s4 basereg, s4 disp)
1720 *(cd->mcodeptr++) = 0xd8;
1721 emit_membase(cd, (basereg),(disp),1);
1725 void emit_fmull_membase(codegendata *cd, s4 basereg, s4 disp)
1727 *(cd->mcodeptr++) = 0xdc;
1728 emit_membase(cd, (basereg),(disp),1);
1732 void emit_fdiv_reg_st(codegendata *cd, s4 reg)
1734 *(cd->mcodeptr++) = 0xd8;
1735 *(cd->mcodeptr++) = 0xf0 + (0x07 & (u1) (reg));
1739 void emit_fdiv_st_reg(codegendata *cd, s4 reg)
1741 *(cd->mcodeptr++) = 0xdc;
1742 *(cd->mcodeptr++) = 0xf8 + (0x07 & (u1) (reg));
1746 void emit_fdivp(codegendata *cd)
1748 *(cd->mcodeptr++) = 0xde;
1749 *(cd->mcodeptr++) = 0xf9;
1753 void emit_fdivp_st_reg(codegendata *cd, s4 reg)
1755 *(cd->mcodeptr++) = 0xde;
1756 *(cd->mcodeptr++) = 0xf8 + (0x07 & (u1) (reg));
1760 void emit_fxch(codegendata *cd)
1762 *(cd->mcodeptr++) = 0xd9;
1763 *(cd->mcodeptr++) = 0xc9;
1767 void emit_fxch_reg(codegendata *cd, s4 reg)
1769 *(cd->mcodeptr++) = 0xd9;
1770 *(cd->mcodeptr++) = 0xc8 + (0x07 & (reg));
1774 void emit_fprem(codegendata *cd)
1776 *(cd->mcodeptr++) = 0xd9;
1777 *(cd->mcodeptr++) = 0xf8;
1781 void emit_fprem1(codegendata *cd)
1783 *(cd->mcodeptr++) = 0xd9;
1784 *(cd->mcodeptr++) = 0xf5;
1788 void emit_fucom(codegendata *cd)
1790 *(cd->mcodeptr++) = 0xdd;
1791 *(cd->mcodeptr++) = 0xe1;
1795 void emit_fucom_reg(codegendata *cd, s4 reg)
1797 *(cd->mcodeptr++) = 0xdd;
1798 *(cd->mcodeptr++) = 0xe0 + (0x07 & (u1) (reg));
1802 void emit_fucomp_reg(codegendata *cd, s4 reg)
1804 *(cd->mcodeptr++) = 0xdd;
1805 *(cd->mcodeptr++) = 0xe8 + (0x07 & (u1) (reg));
1809 void emit_fucompp(codegendata *cd)
1811 *(cd->mcodeptr++) = 0xda;
1812 *(cd->mcodeptr++) = 0xe9;
1816 void emit_fnstsw(codegendata *cd)
1818 *(cd->mcodeptr++) = 0xdf;
1819 *(cd->mcodeptr++) = 0xe0;
1823 void emit_sahf(codegendata *cd)
1825 *(cd->mcodeptr++) = 0x9e;
1829 void emit_finit(codegendata *cd)
1831 *(cd->mcodeptr++) = 0x9b;
1832 *(cd->mcodeptr++) = 0xdb;
1833 *(cd->mcodeptr++) = 0xe3;
1837 void emit_fldcw_mem(codegendata *cd, s4 mem)
1839 *(cd->mcodeptr++) = 0xd9;
1844 void emit_fldcw_membase(codegendata *cd, s4 basereg, s4 disp)
1846 *(cd->mcodeptr++) = 0xd9;
1847 emit_membase(cd, (basereg),(disp),5);
1851 void emit_wait(codegendata *cd)
1853 *(cd->mcodeptr++) = 0x9b;
1857 void emit_ffree_reg(codegendata *cd, s4 reg)
1859 *(cd->mcodeptr++) = 0xdd;
1860 *(cd->mcodeptr++) = 0xc0 + (0x07 & (u1) (reg));
1864 void emit_fdecstp(codegendata *cd)
1866 *(cd->mcodeptr++) = 0xd9;
1867 *(cd->mcodeptr++) = 0xf6;
1871 void emit_fincstp(codegendata *cd)
1873 *(cd->mcodeptr++) = 0xd9;
1874 *(cd->mcodeptr++) = 0xf7;
1877 #if defined(ENABLE_ESCAPE_CHECK)
1878 void emit_escape_check(codegendata *cd, s4 reg) {
1880 M_MOV_IMM(asm_escape_check, REG_ITMP3);
1882 M_IADD_IMM(4, REG_SP);
1887 * These are local overrides for various environment variables in Emacs.
1888 * Please do not remove this and leave it at the end of the file, where
1889 * Emacs will automagically detect them.
1890 * ---------------------------------------------------------------------
1893 * indent-tabs-mode: t