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_ASM_SUPPORT_X86_S_
18#define ART_RUNTIME_ARCH_X86_ASM_SUPPORT_X86_S_
19
20#include "asm_support_x86.h"
21
22#if defined(__APPLE__) || (defined(__clang__) && (__clang_major__ < 4) && (__clang_minor__ < 5))
23    // Clang's as(1) doesn't let you name macro parameters prior to 3.5.
24    #define MACRO0(macro_name) .macro macro_name
25    #define MACRO1(macro_name, macro_arg1) .macro macro_name
26    #define MACRO2(macro_name, macro_arg1, macro_args2) .macro macro_name
27    #define MACRO3(macro_name, macro_arg1, macro_args2, macro_args3) .macro macro_name
28    #define MACRO4(macro_name, macro_arg1, macro_arg2, macro_arg3, macro_arg4) .macro macro_name
29    #define MACRO5(macro_name, macro_arg1, macro_arg2, macro_arg3, macro_arg4, macro_arg5) .macro macro_name
30    #define END_MACRO .endmacro
31
32    // Clang's as(1) uses $0, $1, and so on for macro arguments.
33    #define RAW_VAR(name,index) $index
34    #define VAR(name,index) SYMBOL($index)
35    #define PLT_VAR(name, index) SYMBOL($index)
36    #define REG_VAR(name,index) %$index
37    #define CALL_MACRO(name,index) $index
38
39    //  The use of $x for arguments mean that literals need to be represented with $$x in macros.
40    #define LITERAL(value) $value
41    #define MACRO_LITERAL(value) $$value
42#else
43    // Regular gas(1) lets you name macro parameters.
44    #define MACRO0(macro_name) .macro macro_name
45    #define MACRO1(macro_name, macro_arg1) .macro macro_name macro_arg1
46    #define MACRO2(macro_name, macro_arg1, macro_arg2) .macro macro_name macro_arg1, macro_arg2
47    #define MACRO3(macro_name, macro_arg1, macro_arg2, macro_arg3) .macro macro_name macro_arg1, macro_arg2, macro_arg3
48    #define MACRO4(macro_name, macro_arg1, macro_arg2, macro_arg3, macro_arg4) .macro macro_name macro_arg1, macro_arg2, macro_arg3, macro_arg4
49    #define MACRO5(macro_name, macro_arg1, macro_arg2, macro_arg3, macro_arg4, macro_arg5) .macro macro_name macro_arg1, macro_arg2, macro_arg3, macro_arg4, macro_arg5
50    #define END_MACRO .endm
51
52    // Regular gas(1) uses \argument_name for macro arguments.
53    // We need to turn on alternate macro syntax so we can use & instead or the preprocessor
54    // will screw us by inserting a space between the \ and the name. Even in this mode there's
55    // no special meaning to $, so literals are still just $x. The use of altmacro means % is a
56    // special character meaning care needs to be taken when passing registers as macro arguments.
57    .altmacro
58    #define RAW_VAR(name,index) name&
59    #define VAR(name,index) name&
60    #define PLT_VAR(name, index) name&@PLT
61    #define REG_VAR(name,index) %name
62    #define CALL_MACRO(name,index) name&
63
64    #define LITERAL(value) $value
65    #define MACRO_LITERAL(value) $value
66#endif
67
68#if defined(__APPLE__)
69    #define FUNCTION_TYPE(name,index)
70    #define SIZE(name,index)
71#elif defined(__clang__) && (__clang_major__ < 4) && (__clang_minor__ < 5)
72    #define FUNCTION_TYPE(name,index) .type $index, @function
73    #define SIZE(name,index) .size $index, .-$index
74#else
75    #define FUNCTION_TYPE(name,index) .type name&, @function
76    #define SIZE(name,index) .size name, .-name
77#endif
78
79    // CFI support.
80#if !defined(__APPLE__)
81    #define CFI_STARTPROC .cfi_startproc
82    #define CFI_ENDPROC .cfi_endproc
83    #define CFI_ADJUST_CFA_OFFSET(size) .cfi_adjust_cfa_offset size
84    #define CFI_DEF_CFA(reg,size) .cfi_def_cfa reg,size
85    #define CFI_DEF_CFA_REGISTER(reg) .cfi_def_cfa_register reg
86    #define CFI_RESTORE(reg) .cfi_restore reg
87    #define CFI_REL_OFFSET(reg,size) .cfi_rel_offset reg,size
88    #define CFI_RESTORE_STATE .cfi_restore_state
89    #define CFI_REMEMBER_STATE .cfi_remember_state
90#else
91    // Mac OS' doesn't like cfi_* directives.
92    #define CFI_STARTPROC
93    #define CFI_ENDPROC
94    #define CFI_ADJUST_CFA_OFFSET(size)
95    #define CFI_DEF_CFA(reg,size)
96    #define CFI_DEF_CFA_REGISTER(reg)
97    #define CFI_RESTORE(reg)
98    #define CFI_REL_OFFSET(reg,size)
99    #define CFI_RESTORE_STATE
100    #define CFI_REMEMBER_STATE
101#endif
102
103    // Symbols.
104#if !defined(__APPLE__)
105    #define SYMBOL(name) name
106    #if defined(__clang__) && (__clang_major__ < 4) && (__clang_minor__ < 5)
107        // TODO: Disabled for old clang 3.3, this leads to text relocations and there should be a
108        // better fix.
109        #define PLT_SYMBOL(name) name // ## @PLT
110    #else
111        #define PLT_SYMBOL(name) name ## @PLT
112    #endif
113#else
114    // Mac OS' symbols have an _ prefix.
115    #define SYMBOL(name) _ ## name
116    #define PLT_SYMBOL(name) _ ## name
117#endif
118
119// Directive to hide a function symbol.
120#if defined(__APPLE__)
121    #define ASM_HIDDEN .private_extern
122#else
123    #define ASM_HIDDEN .hidden
124#endif
125
126    /* Cache alignment for function entry */
127MACRO0(ALIGN_FUNCTION_ENTRY)
128    .balign 16
129END_MACRO
130
131MACRO1(DEFINE_FUNCTION, c_name)
132    FUNCTION_TYPE(\c_name, 0)
133    ASM_HIDDEN VAR(c_name, 0)
134    .globl VAR(c_name, 0)
135    ALIGN_FUNCTION_ENTRY
136VAR(c_name, 0):
137    CFI_STARTPROC
138    // Ensure we get a sane starting CFA.
139    CFI_DEF_CFA(esp, 4)
140END_MACRO
141
142MACRO1(END_FUNCTION, c_name)
143    CFI_ENDPROC
144    SIZE(\c_name, 0)
145END_MACRO
146
147MACRO1(PUSH, reg)
148    pushl REG_VAR(reg, 0)
149    CFI_ADJUST_CFA_OFFSET(4)
150    CFI_REL_OFFSET(REG_VAR(reg, 0), 0)
151END_MACRO
152
153MACRO1(POP, reg)
154    popl REG_VAR(reg,0)
155    CFI_ADJUST_CFA_OFFSET(-4)
156    CFI_RESTORE(REG_VAR(reg,0))
157END_MACRO
158
159MACRO1(UNIMPLEMENTED,name)
160    FUNCTION_TYPE(\name, 0)
161    .globl VAR(name, 0)
162    ALIGN_FUNCTION_ENTRY
163VAR(name, 0):
164    CFI_STARTPROC
165    int3
166    int3
167    CFI_ENDPROC
168    SIZE(\name, 0)
169END_MACRO
170
171MACRO1(SETUP_GOT_NOSAVE, got_reg)
172#ifndef __APPLE__
173    .ifc RAW_VAR(got_reg, 0), ebx
174      call __x86.get_pc_thunk.bx
175      addl $_GLOBAL_OFFSET_TABLE_, %ebx
176    .else
177      .error "Unknown GOT register \got_reg"
178    .endif
179#endif
180END_MACRO
181
182#endif  // ART_RUNTIME_ARCH_X86_ASM_SUPPORT_X86_S_
183