1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ART_RUNTIME_ARCH_X86_64_ASM_SUPPORT_X86_64_S_ 18 #define ART_RUNTIME_ARCH_X86_64_ASM_SUPPORT_X86_64_S_ 19 20 #include "asm_support_x86_64.h" 21 22 // Regular gas(1) & current clang/llvm assembler support named macro parameters. 23 #define MACRO0(macro_name) .macro macro_name 24 #define MACRO1(macro_name, macro_arg1) .macro macro_name macro_arg1 25 #define MACRO2(macro_name, macro_arg1, macro_arg2) .macro macro_name macro_arg1, macro_arg2 26 #define MACRO3(macro_name, macro_arg1, macro_arg2, macro_arg3) .macro macro_name macro_arg1, macro_arg2, macro_arg3 27 #define MACRO4(macro_name, macro_arg1, macro_arg2, macro_arg3, macro_arg4) .macro macro_name macro_arg1, macro_arg2, macro_arg3, macro_arg4 28 #define END_MACRO .endm 29 30 #if defined(__clang__) 31 // Clang/llvm does not support .altmacro. However, the clang/llvm preprocessor doesn't 32 // separate the backslash and parameter by a space. Everything just works. 33 #define RAW_VAR(name) \name 34 #define VAR(name) \name 35 #define CALLVAR(name) SYMBOL(\name) 36 #define PLT_VAR(name) \name@PLT 37 #define REG_VAR(name) %\name 38 #define CALL_MACRO(name) \name 39 #else 40 // Regular gas(1) uses \argument_name for macro arguments. 41 // We need to turn on alternate macro syntax so we can use & instead or the preprocessor 42 // will screw us by inserting a space between the \ and the name. Even in this mode there's 43 // no special meaning to $, so literals are still just $x. The use of altmacro means % is a 44 // special character meaning care needs to be taken when passing registers as macro 45 // arguments. 46 .altmacro 47 #define RAW_VAR(name) name& 48 #define VAR(name) name& 49 #define CALLVAR(name) SYMBOL(name&) 50 #define PLT_VAR(name) name&@PLT 51 #define REG_VAR(name) %name 52 #define CALL_MACRO(name) name& 53 #endif 54 55 #define LITERAL(value) $value 56 #if defined(__APPLE__) 57 #define MACRO_LITERAL(value) $(value) 58 #else 59 #define MACRO_LITERAL(value) $value 60 #endif 61 62 #if defined(__APPLE__) 63 #define FUNCTION_TYPE(name) 64 #define SIZE(name) 65 #else 66 #define FUNCTION_TYPE(name) .type name, @function 67 #define SIZE(name) .size name, .-name 68 #endif 69 70 // CFI support. 71 #if !defined(__APPLE__) 72 #define CFI_STARTPROC .cfi_startproc 73 #define CFI_ENDPROC .cfi_endproc 74 #define CFI_ADJUST_CFA_OFFSET(size) .cfi_adjust_cfa_offset size 75 #define CFI_DEF_CFA(reg,size) .cfi_def_cfa reg,size 76 #define CFI_DEF_CFA_REGISTER(reg) .cfi_def_cfa_register reg 77 #define CFI_RESTORE(reg) .cfi_restore reg 78 #define CFI_REL_OFFSET(reg,size) .cfi_rel_offset reg,size 79 #define CFI_RESTORE_STATE .cfi_restore_state 80 #define CFI_REMEMBER_STATE .cfi_remember_state 81 #else 82 // Mac OS' doesn't like cfi_* directives. 83 #define CFI_STARTPROC 84 #define CFI_ENDPROC 85 #define CFI_ADJUST_CFA_OFFSET(size) 86 #define CFI_DEF_CFA(reg,size) 87 #define CFI_DEF_CFA_REGISTER(reg) 88 #define CFI_RESTORE(reg) 89 #define CFI_REL_OFFSET(reg,size) 90 #define CFI_RESTORE_STATE 91 #define CFI_REMEMBER_STATE 92 #endif 93 94 // Symbols. 95 #if !defined(__APPLE__) 96 #define SYMBOL(name) name 97 #define PLT_SYMBOL(name) name ## @PLT 98 #else 99 #define SYMBOL(name) _ ## name 100 #define PLT_SYMBOL(name) _ ## name 101 #endif 102 103 // Directive to hide a function symbol. 104 #if defined(__APPLE__) 105 #define ASM_HIDDEN .private_extern 106 #else 107 #define ASM_HIDDEN .hidden 108 #endif 109 110 /* Cache alignment for function entry */ 111 MACRO0(ALIGN_FUNCTION_ENTRY) 112 .balign 16 113 END_MACRO 114 115 // TODO: we might need to use SYMBOL() here to add the underscore prefix 116 // for mac builds. 117 MACRO2(DEFINE_FUNCTION_CUSTOM_CFA, c_name, cfa_offset) 118 FUNCTION_TYPE(SYMBOL(\c_name)) 119 ASM_HIDDEN CALLVAR(c_name) 120 .globl CALLVAR(c_name) 121 ALIGN_FUNCTION_ENTRY 122 CALLVAR(c_name): 123 CFI_STARTPROC 124 // Ensure we get a sane starting CFA. 125 CFI_DEF_CFA(rsp, RAW_VAR(cfa_offset)) 126 END_MACRO 127 128 MACRO1(DEFINE_FUNCTION, c_name) 129 DEFINE_FUNCTION_CUSTOM_CFA RAW_VAR(c_name), __SIZEOF_POINTER__ 130 END_MACRO 131 132 MACRO1(END_FUNCTION, c_name) 133 CFI_ENDPROC 134 SIZE(SYMBOL(\c_name)) 135 END_MACRO 136 137 MACRO1(PUSH, reg) 138 pushq REG_VAR(reg) 139 CFI_ADJUST_CFA_OFFSET(8) 140 CFI_REL_OFFSET(REG_VAR(reg), 0) 141 END_MACRO 142 143 MACRO1(POP, reg) 144 popq REG_VAR(reg) 145 CFI_ADJUST_CFA_OFFSET(-8) 146 CFI_RESTORE(REG_VAR(reg)) 147 END_MACRO 148 149 MACRO1(UNIMPLEMENTED,name) 150 FUNCTION_TYPE(SYMBOL(\name)) 151 ASM_HIDDEN VAR(name) 152 .globl VAR(name) 153 ALIGN_FUNCTION_ENTRY 154 VAR(name): 155 CFI_STARTPROC 156 int3 157 int3 158 CFI_ENDPROC 159 SIZE(SYMBOL(\name)) 160 END_MACRO 161 162 MACRO0(UNREACHABLE) 163 int3 164 END_MACRO 165 166 MACRO0(UNTESTED) 167 int3 168 END_MACRO 169 170 // Macros to poison (negate) the reference for heap poisoning. 171 MACRO1(POISON_HEAP_REF, rRef) 172 #ifdef USE_HEAP_POISONING 173 negl REG_VAR(rRef) 174 #endif // USE_HEAP_POISONING 175 END_MACRO 176 177 // Macros to unpoison (negate) the reference for heap poisoning. 178 MACRO1(UNPOISON_HEAP_REF, rRef) 179 #ifdef USE_HEAP_POISONING 180 negl REG_VAR(rRef) 181 #endif // USE_HEAP_POISONING 182 END_MACRO 183 184 #endif // ART_RUNTIME_ARCH_X86_64_ASM_SUPPORT_X86_64_S_ 185