]>
Commit | Line | Data |
---|---|---|
ee890fe2 | 1 | /* Functions for generic Darwin as target machine for GNU C compiler. |
0ea5865c | 2 | Copyright (C) 1989, 1990, 1991, 1992, 1993, 2000, 2001, 2002, 2003, 2004 |
ee890fe2 SS |
3 | Free Software Foundation, Inc. |
4 | Contributed by Apple Computer Inc. | |
5 | ||
7ec022b2 | 6 | This file is part of GCC. |
ee890fe2 | 7 | |
7ec022b2 | 8 | GCC is free software; you can redistribute it and/or modify |
ee890fe2 SS |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
7ec022b2 | 13 | GCC is distributed in the hope that it will be useful, |
ee890fe2 SS |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
7ec022b2 | 19 | along with GCC; see the file COPYING. If not, write to |
ee890fe2 SS |
20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | #include "config.h" | |
24 | #include "system.h" | |
4977bab6 ZW |
25 | #include "coretypes.h" |
26 | #include "tm.h" | |
ee890fe2 SS |
27 | #include "rtl.h" |
28 | #include "regs.h" | |
29 | #include "hard-reg-set.h" | |
30 | #include "real.h" | |
31 | #include "insn-config.h" | |
32 | #include "conditions.h" | |
33 | #include "insn-flags.h" | |
34 | #include "output.h" | |
35 | #include "insn-attr.h" | |
36 | #include "flags.h" | |
37 | #include "tree.h" | |
38 | #include "expr.h" | |
39 | #include "reload.h" | |
ee890fe2 SS |
40 | #include "function.h" |
41 | #include "ggc.h" | |
3ac88239 | 42 | #include "langhooks.h" |
1adaa117 | 43 | #include "target.h" |
245ff137 | 44 | #include "tm_p.h" |
6ce4806b | 45 | #include "errors.h" |
11abc112 | 46 | #include "hashtab.h" |
ee890fe2 | 47 | |
699c914a MS |
48 | /* Darwin supports a feature called fix-and-continue, which is used |
49 | for rapid turn around debugging. When code is compiled with the | |
50 | -mfix-and-continue flag, two changes are made to the generated code | |
51 | that allow the system to do things that it would normally not be | |
52 | able to do easily. These changes allow gdb to load in | |
53 | recompilation of a translation unit that has been changed into a | |
54 | running program and replace existing functions and methods of that | |
55 | translation unit with with versions of those functions and methods | |
56 | from the newly compiled translation unit. The new functions access | |
57 | the existing static data from the old translation unit, if the data | |
58 | existed in the unit to be replaced, and from the new translation | |
59 | unit, for new data. | |
60 | ||
61 | The changes are to insert 4 nops at the beginning of all functions | |
62 | and to use indirection to get at static duration data. The 4 nops | |
63 | are required by consumers of the generated code. Currently, gdb | |
64 | uses this to patch in a jump to the overriding function, this | |
65 | allows all uses of the old name to forward to the replacement, | |
c112cf2b | 66 | including existing function pointers and virtual methods. See |
699c914a MS |
67 | rs6000_emit_prologue for the code that handles the nop insertions. |
68 | ||
69 | The added indirection allows gdb to redirect accesses to static | |
70 | duration data from the newly loaded translation unit to the | |
71 | existing data, if any. @code{static} data is special and is | |
72 | handled by setting the second word in the .non_lazy_symbol_pointer | |
73 | data structure to the address of the data. See indirect_data for | |
74 | the code that handles the extra indirection, and | |
75 | machopic_output_indirection and its use of MACHO_SYMBOL_STATIC for | |
76 | the code that handles @code{static} data indirection. */ | |
77 | ||
78 | ||
8f4220dc MA |
79 | /* Nonzero if the user passes the -mone-byte-bool switch, which forces |
80 | sizeof(bool) to be 1. */ | |
81 | const char *darwin_one_byte_bool = 0; | |
82 | ||
ee890fe2 | 83 | int |
9c808aad | 84 | name_needs_quotes (const char *name) |
ee890fe2 SS |
85 | { |
86 | int c; | |
87 | while ((c = *name++) != '\0') | |
6f94a68e | 88 | if (! ISIDNUM (c) && c != '.' && c != '$') |
ee890fe2 SS |
89 | return 1; |
90 | return 0; | |
91 | } | |
92 | ||
9c808aad | 93 | /* |
ee890fe2 SS |
94 | * flag_pic = 1 ... generate only indirections |
95 | * flag_pic = 2 ... generate indirections and pure code | |
96 | */ | |
97 | ||
11abc112 MM |
98 | static int |
99 | machopic_symbol_defined_p (rtx sym_ref) | |
ee890fe2 | 100 | { |
156a126c MS |
101 | return (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED) |
102 | || (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref)); | |
ee890fe2 SS |
103 | } |
104 | ||
11abc112 MM |
105 | /* This module assumes that (const (symbol_ref "foo")) is a legal pic |
106 | reference, which will not be changed. */ | |
9c808aad | 107 | |
ee890fe2 | 108 | enum machopic_addr_class |
11abc112 | 109 | machopic_classify_symbol (rtx sym_ref) |
ee890fe2 | 110 | { |
11abc112 MM |
111 | int flags; |
112 | bool function_p; | |
113 | ||
114 | flags = SYMBOL_REF_FLAGS (sym_ref); | |
115 | function_p = SYMBOL_REF_FUNCTION_P (sym_ref); | |
116 | if (machopic_symbol_defined_p (sym_ref)) | |
117 | return (function_p | |
118 | ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA); | |
119 | else | |
120 | return (function_p | |
121 | ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA); | |
ee890fe2 SS |
122 | } |
123 | ||
699c914a MS |
124 | #ifndef TARGET_FIX_AND_CONTINUE |
125 | #define TARGET_FIX_AND_CONTINUE 0 | |
126 | #endif | |
127 | ||
128 | /* Indicate when fix-and-continue style code generation is being used | |
129 | and when a reference to data should be indirected so that it can be | |
130 | rebound in a new translation unit to refernce the original instance | |
131 | of that data. Symbol names that are for code generation local to | |
132 | the translation unit are bound to the new translation unit; | |
133 | currently this means symbols that begin with L or _OBJC_; | |
134 | otherwise, we indicate that an indirect reference should be made to | |
135 | permit the runtime to rebind new instances of the translation unit | |
136 | to the original instance of the data. */ | |
137 | ||
138 | static int | |
139 | indirect_data (rtx sym_ref) | |
140 | { | |
141 | int lprefix; | |
142 | const char *name; | |
143 | ||
144 | /* If we aren't generating fix-and-continue code, don't do anything special. */ | |
145 | if (TARGET_FIX_AND_CONTINUE == 0) | |
146 | return 0; | |
147 | ||
148 | /* Otherwise, all symbol except symbols that begin with L or _OBJC_ | |
149 | are indirected. Symbols that begin with L and _OBJC_ are always | |
150 | bound to the current translation unit as they are used for | |
151 | generated local data of the translation unit. */ | |
152 | ||
153 | name = XSTR (sym_ref, 0); | |
154 | ||
155 | lprefix = (((name[0] == '*' || name[0] == '&') | |
156 | && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L'))) | |
157 | || (strncmp (name, "_OBJC_", 6))); | |
158 | ||
159 | return ! lprefix; | |
160 | } | |
161 | ||
162 | ||
ee890fe2 | 163 | static int |
11abc112 | 164 | machopic_data_defined_p (rtx sym_ref) |
ee890fe2 | 165 | { |
699c914a MS |
166 | if (indirect_data (sym_ref)) |
167 | return 0; | |
168 | ||
11abc112 | 169 | switch (machopic_classify_symbol (sym_ref)) |
ee890fe2 SS |
170 | { |
171 | case MACHOPIC_DEFINED_DATA: | |
0e1ad529 | 172 | case MACHOPIC_DEFINED_FUNCTION: |
ee890fe2 SS |
173 | return 1; |
174 | default: | |
175 | return 0; | |
176 | } | |
177 | } | |
178 | ||
ee890fe2 | 179 | void |
11abc112 | 180 | machopic_define_symbol (rtx mem) |
ee890fe2 | 181 | { |
11abc112 MM |
182 | rtx sym_ref; |
183 | if (GET_CODE (mem) != MEM) | |
184 | abort (); | |
185 | sym_ref = XEXP (mem, 0); | |
186 | SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED; | |
ee890fe2 SS |
187 | } |
188 | ||
6788f5ca | 189 | static GTY(()) char * function_base; |
ee890fe2 | 190 | |
92c1a778 | 191 | const char * |
9c808aad | 192 | machopic_function_base_name (void) |
ee890fe2 | 193 | { |
ab82a49f AP |
194 | /* if dynamic-no-pic is on, we should not get here */ |
195 | if (MACHO_DYNAMIC_NO_PIC_P) | |
196 | abort (); | |
ee890fe2 | 197 | |
1622229c | 198 | if (function_base == NULL) |
9c808aad | 199 | function_base = |
1622229c AP |
200 | (char *) ggc_alloc_string ("<pic base>", sizeof ("<pic base>")); |
201 | ||
202 | current_function_uses_pic_offset_table = 1; | |
203 | ||
204 | return function_base; | |
205 | } | |
206 | ||
11abc112 MM |
207 | /* Return a SYMBOL_REF for the PIC function base. */ |
208 | ||
209 | rtx | |
210 | machopic_function_base_sym (void) | |
211 | { | |
212 | rtx sym_ref; | |
213 | ||
214 | sym_ref = gen_rtx_SYMBOL_REF (Pmode, machopic_function_base_name ()); | |
215 | SYMBOL_REF_FLAGS (sym_ref) | |
216 | |= (MACHO_SYMBOL_FLAG_VARIABLE | MACHO_SYMBOL_FLAG_DEFINED); | |
217 | return sym_ref; | |
218 | } | |
219 | ||
1622229c AP |
220 | static GTY(()) const char * function_base_func_name; |
221 | static GTY(()) int current_pic_label_num; | |
222 | ||
223 | void | |
224 | machopic_output_function_base_name (FILE *file) | |
225 | { | |
226 | const char *current_name; | |
227 | ||
ff482c8d | 228 | /* If dynamic-no-pic is on, we should not get here. */ |
1622229c AP |
229 | if (MACHO_DYNAMIC_NO_PIC_P) |
230 | abort (); | |
9c808aad | 231 | current_name = |
1622229c | 232 | IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)); |
6788f5ca | 233 | if (function_base_func_name != current_name) |
ee890fe2 | 234 | { |
ee890fe2 | 235 | ++current_pic_label_num; |
6788f5ca | 236 | function_base_func_name = current_name; |
ee890fe2 | 237 | } |
1622229c | 238 | fprintf (file, "\"L%011d$pb\"", current_pic_label_num); |
ee890fe2 SS |
239 | } |
240 | ||
11abc112 MM |
241 | /* The suffix attached to non-lazy pointer symbols. */ |
242 | #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr" | |
243 | /* The suffix attached to stub symbols. */ | |
244 | #define STUB_SUFFIX "$stub" | |
ee890fe2 | 245 | |
11abc112 | 246 | typedef struct machopic_indirection GTY (()) |
ee890fe2 | 247 | { |
11abc112 MM |
248 | /* The SYMBOL_REF for the entity referenced. */ |
249 | rtx symbol; | |
1adaa117 GK |
250 | /* The name of the stub or non-lazy pointer. */ |
251 | const char * ptr_name; | |
11abc112 MM |
252 | /* True iff this entry is for a stub (as opposed to a non-lazy |
253 | pointer). */ | |
254 | bool stub_p; | |
255 | /* True iff this stub or pointer pointer has been referenced. */ | |
256 | bool used; | |
257 | } machopic_indirection; | |
258 | ||
259 | /* A table mapping stub names and non-lazy pointer names to | |
260 | SYMBOL_REFs for the stubbed-to and pointed-to entities. */ | |
261 | ||
262 | static GTY ((param_is (struct machopic_indirection))) htab_t | |
263 | machopic_indirections; | |
264 | ||
265 | /* Return a hash value for a SLOT in the indirections hash table. */ | |
266 | ||
267 | static hashval_t | |
268 | machopic_indirection_hash (const void *slot) | |
269 | { | |
270 | const machopic_indirection *p = (const machopic_indirection *) slot; | |
1adaa117 | 271 | return htab_hash_string (p->ptr_name); |
11abc112 | 272 | } |
9c808aad | 273 | |
11abc112 MM |
274 | /* Returns true if the KEY is the same as that associated with |
275 | SLOT. */ | |
276 | ||
277 | static int | |
278 | machopic_indirection_eq (const void *slot, const void *key) | |
279 | { | |
1adaa117 | 280 | return strcmp (((const machopic_indirection *) slot)->ptr_name, key) == 0; |
11abc112 | 281 | } |
ee890fe2 | 282 | |
11abc112 MM |
283 | /* Return the name of the non-lazy pointer (if STUB_P is false) or |
284 | stub (if STUB_B is true) corresponding to the given name. */ | |
df56a27f | 285 | |
11abc112 MM |
286 | const char * |
287 | machopic_indirection_name (rtx sym_ref, bool stub_p) | |
288 | { | |
289 | char *buffer; | |
290 | const char *name = XSTR (sym_ref, 0); | |
1adaa117 | 291 | size_t namelen = strlen (name); |
11abc112 | 292 | machopic_indirection *p; |
1adaa117 | 293 | void ** slot; |
11abc112 MM |
294 | |
295 | /* Construct the name of the non-lazy pointer or stub. */ | |
296 | if (stub_p) | |
df56a27f | 297 | { |
11abc112 MM |
298 | int needs_quotes = name_needs_quotes (name); |
299 | buffer = alloca (strlen ("&L") | |
300 | + namelen | |
301 | + strlen (STUB_SUFFIX) | |
302 | + 2 /* possible quotes */ | |
303 | + 1 /* '\0' */); | |
304 | ||
305 | if (needs_quotes) | |
df56a27f | 306 | { |
11abc112 MM |
307 | if (name[0] == '*') |
308 | sprintf (buffer, "&\"L%s" STUB_SUFFIX "\"", name + 1); | |
309 | else | |
310 | sprintf (buffer, "&\"L%s%s" STUB_SUFFIX "\"", user_label_prefix, | |
311 | name); | |
df56a27f | 312 | } |
11abc112 MM |
313 | else if (name[0] == '*') |
314 | sprintf (buffer, "&L%s" STUB_SUFFIX, name + 1); | |
315 | else | |
316 | sprintf (buffer, "&L%s%s" STUB_SUFFIX, user_label_prefix, name); | |
317 | } | |
318 | else | |
319 | { | |
320 | buffer = alloca (strlen ("&L") | |
321 | + strlen (user_label_prefix) | |
322 | + namelen | |
323 | + strlen (NON_LAZY_POINTER_SUFFIX) | |
324 | + 1 /* '\0' */); | |
325 | if (name[0] == '*') | |
326 | sprintf (buffer, "&L%s" NON_LAZY_POINTER_SUFFIX, name + 1); | |
327 | else | |
328 | sprintf (buffer, "&L%s%s" NON_LAZY_POINTER_SUFFIX, | |
329 | user_label_prefix, name); | |
df56a27f SS |
330 | } |
331 | ||
1adaa117 GK |
332 | if (!machopic_indirections) |
333 | machopic_indirections = htab_create_ggc (37, | |
334 | machopic_indirection_hash, | |
335 | machopic_indirection_eq, | |
336 | /*htab_del=*/NULL); | |
337 | ||
338 | slot = htab_find_slot_with_hash (machopic_indirections, buffer, | |
339 | htab_hash_string (buffer), INSERT); | |
340 | if (*slot) | |
341 | { | |
342 | p = (machopic_indirection *) *slot; | |
343 | } | |
344 | else | |
11abc112 | 345 | { |
11abc112 MM |
346 | p = (machopic_indirection *) ggc_alloc (sizeof (machopic_indirection)); |
347 | p->symbol = sym_ref; | |
1adaa117 | 348 | p->ptr_name = xstrdup (buffer); |
11abc112 | 349 | p->stub_p = stub_p; |
1adaa117 GK |
350 | p->used = false; |
351 | *slot = p; | |
11abc112 MM |
352 | } |
353 | ||
1adaa117 | 354 | return p->ptr_name; |
ee890fe2 SS |
355 | } |
356 | ||
11abc112 | 357 | /* Return the name of the stub for the mcount function. */ |
ee890fe2 | 358 | |
11abc112 MM |
359 | const char* |
360 | machopic_mcount_stub_name (void) | |
ee890fe2 | 361 | { |
76f60aa5 AP |
362 | rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount"); |
363 | return machopic_indirection_name (symbol, /*stub_p=*/true); | |
ee890fe2 SS |
364 | } |
365 | ||
11abc112 MM |
366 | /* If NAME is the name of a stub or a non-lazy pointer , mark the stub |
367 | or non-lazy pointer as used -- and mark the object to which the | |
368 | pointer/stub refers as used as well, since the pointer/stub will | |
369 | emit a reference to it. */ | |
370 | ||
ee890fe2 | 371 | void |
11abc112 | 372 | machopic_validate_stub_or_non_lazy_ptr (const char *name) |
ee890fe2 | 373 | { |
11abc112 MM |
374 | machopic_indirection *p; |
375 | ||
376 | p = ((machopic_indirection *) | |
1adaa117 GK |
377 | (htab_find_with_hash (machopic_indirections, name, |
378 | htab_hash_string (name)))); | |
379 | if (p && ! p->used) | |
11abc112 | 380 | { |
1adaa117 GK |
381 | const char *real_name; |
382 | tree id; | |
383 | ||
384 | p->used = true; | |
385 | ||
ca472546 GK |
386 | /* Do what output_addr_const will do when we actually call it. */ |
387 | if (SYMBOL_REF_DECL (p->symbol)) | |
388 | mark_decl_referenced (SYMBOL_REF_DECL (p->symbol)); | |
389 | ||
390 | real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0)); | |
1adaa117 GK |
391 | |
392 | id = maybe_get_identifier (real_name); | |
393 | if (id) | |
394 | mark_referenced (id); | |
11abc112 | 395 | } |
ee890fe2 SS |
396 | } |
397 | ||
398 | /* Transform ORIG, which may be any data source, to the corresponding | |
399 | source using indirections. */ | |
400 | ||
401 | rtx | |
9c808aad | 402 | machopic_indirect_data_reference (rtx orig, rtx reg) |
ee890fe2 SS |
403 | { |
404 | rtx ptr_ref = orig; | |
9c808aad | 405 | |
ee890fe2 SS |
406 | if (! MACHOPIC_INDIRECT) |
407 | return orig; | |
408 | ||
409 | if (GET_CODE (orig) == SYMBOL_REF) | |
410 | { | |
11abc112 | 411 | int defined = machopic_data_defined_p (orig); |
ab82a49f AP |
412 | |
413 | if (defined && MACHO_DYNAMIC_NO_PIC_P) | |
414 | { | |
415 | #if defined (TARGET_TOC) | |
b8a55285 AP |
416 | emit_insn (gen_macho_high (reg, orig)); |
417 | emit_insn (gen_macho_low (reg, reg, orig)); | |
ab82a49f AP |
418 | #else |
419 | /* some other cpu -- writeme! */ | |
420 | abort (); | |
421 | #endif | |
422 | return reg; | |
423 | } | |
424 | else if (defined) | |
ee890fe2 | 425 | { |
7ae8cf75 | 426 | #if defined (TARGET_TOC) || defined (HAVE_lo_sum) |
11abc112 | 427 | rtx pic_base = machopic_function_base_sym (); |
6f94a68e GK |
428 | rtx offset = gen_rtx_CONST (Pmode, |
429 | gen_rtx_MINUS (Pmode, orig, pic_base)); | |
7ae8cf75 | 430 | #endif |
ee890fe2 SS |
431 | |
432 | #if defined (TARGET_TOC) /* i.e., PowerPC */ | |
d9b46dfb | 433 | rtx hi_sum_reg = (no_new_pseudos ? reg : gen_reg_rtx (Pmode)); |
ee890fe2 SS |
434 | |
435 | if (reg == NULL) | |
436 | abort (); | |
437 | ||
6f94a68e GK |
438 | emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, |
439 | gen_rtx_PLUS (Pmode, pic_offset_table_rtx, | |
440 | gen_rtx_HIGH (Pmode, offset)))); | |
441 | emit_insn (gen_rtx_SET (Pmode, reg, | |
442 | gen_rtx_LO_SUM (Pmode, hi_sum_reg, offset))); | |
ee890fe2 SS |
443 | |
444 | orig = reg; | |
445 | #else | |
446 | #if defined (HAVE_lo_sum) | |
447 | if (reg == 0) abort (); | |
448 | ||
6f94a68e GK |
449 | emit_insn (gen_rtx_SET (VOIDmode, reg, |
450 | gen_rtx_HIGH (Pmode, offset))); | |
451 | emit_insn (gen_rtx_SET (VOIDmode, reg, | |
452 | gen_rtx_LO_SUM (Pmode, reg, offset))); | |
453 | emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx)); | |
ee890fe2 | 454 | |
6f94a68e | 455 | orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg); |
ee890fe2 SS |
456 | #endif |
457 | #endif | |
458 | return orig; | |
459 | } | |
460 | ||
828a4fe4 | 461 | ptr_ref = (gen_rtx_SYMBOL_REF |
11abc112 MM |
462 | (Pmode, |
463 | machopic_indirection_name (orig, /*stub_p=*/false))); | |
ee890fe2 | 464 | |
542a8afa | 465 | SYMBOL_REF_DECL (ptr_ref) = SYMBOL_REF_DECL (orig); |
c0d594f1 | 466 | |
542a8afa | 467 | ptr_ref = gen_const_mem (Pmode, ptr_ref); |
828a4fe4 | 468 | machopic_define_symbol (ptr_ref); |
ee890fe2 SS |
469 | |
470 | return ptr_ref; | |
471 | } | |
472 | else if (GET_CODE (orig) == CONST) | |
473 | { | |
474 | rtx base, result; | |
475 | ||
476 | /* legitimize both operands of the PLUS */ | |
477 | if (GET_CODE (XEXP (orig, 0)) == PLUS) | |
478 | { | |
479 | base = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 0), | |
480 | reg); | |
481 | orig = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 1), | |
482 | (base == reg ? 0 : reg)); | |
483 | } | |
9c808aad | 484 | else |
ee890fe2 SS |
485 | return orig; |
486 | ||
487 | if (MACHOPIC_PURE && GET_CODE (orig) == CONST_INT) | |
ed8908e7 | 488 | result = plus_constant (base, INTVAL (orig)); |
ee890fe2 | 489 | else |
6f94a68e | 490 | result = gen_rtx_PLUS (Pmode, base, orig); |
ee890fe2 | 491 | |
ee890fe2 SS |
492 | if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM) |
493 | { | |
494 | if (reg) | |
495 | { | |
496 | emit_move_insn (reg, result); | |
497 | result = reg; | |
498 | } | |
499 | else | |
500 | { | |
501 | result = force_reg (GET_MODE (result), result); | |
502 | } | |
503 | } | |
504 | ||
505 | return result; | |
506 | ||
507 | } | |
508 | else if (GET_CODE (orig) == MEM) | |
509 | XEXP (ptr_ref, 0) = machopic_indirect_data_reference (XEXP (orig, 0), reg); | |
510 | /* When the target is i386, this code prevents crashes due to the | |
511 | compiler's ignorance on how to move the PIC base register to | |
512 | other registers. (The reload phase sometimes introduces such | |
513 | insns.) */ | |
514 | else if (GET_CODE (orig) == PLUS | |
515 | && GET_CODE (XEXP (orig, 0)) == REG | |
516 | && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM | |
517 | #ifdef I386 | |
518 | /* Prevent the same register from being erroneously used | |
519 | as both the base and index registers. */ | |
520 | && GET_CODE (XEXP (orig, 1)) == CONST | |
521 | #endif | |
522 | && reg) | |
523 | { | |
524 | emit_move_insn (reg, XEXP (orig, 0)); | |
525 | XEXP (ptr_ref, 0) = reg; | |
526 | } | |
527 | return ptr_ref; | |
528 | } | |
529 | ||
ee890fe2 SS |
530 | /* Transform TARGET (a MEM), which is a function call target, to the |
531 | corresponding symbol_stub if necessary. Return a new MEM. */ | |
532 | ||
533 | rtx | |
9c808aad | 534 | machopic_indirect_call_target (rtx target) |
ee890fe2 SS |
535 | { |
536 | if (GET_CODE (target) != MEM) | |
537 | return target; | |
538 | ||
11abc112 MM |
539 | if (MACHOPIC_INDIRECT |
540 | && GET_CODE (XEXP (target, 0)) == SYMBOL_REF | |
541 | && !(SYMBOL_REF_FLAGS (XEXP (target, 0)) | |
542 | & MACHO_SYMBOL_FLAG_DEFINED)) | |
9c808aad | 543 | { |
11abc112 MM |
544 | rtx sym_ref = XEXP (target, 0); |
545 | const char *stub_name = machopic_indirection_name (sym_ref, | |
546 | /*stub_p=*/true); | |
547 | enum machine_mode mode = GET_MODE (sym_ref); | |
548 | tree decl = SYMBOL_REF_DECL (sym_ref); | |
549 | ||
550 | XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name); | |
551 | SYMBOL_REF_DECL (XEXP (target, 0)) = decl; | |
389fdba0 | 552 | MEM_READONLY_P (target) = 1; |
542a8afa | 553 | MEM_NOTRAP_P (target) = 1; |
ee890fe2 SS |
554 | } |
555 | ||
556 | return target; | |
557 | } | |
558 | ||
559 | rtx | |
9c808aad | 560 | machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg) |
ee890fe2 SS |
561 | { |
562 | rtx pic_ref = orig; | |
563 | ||
ab82a49f | 564 | if (! MACHOPIC_INDIRECT) |
ee890fe2 SS |
565 | return orig; |
566 | ||
567 | /* First handle a simple SYMBOL_REF or LABEL_REF */ | |
568 | if (GET_CODE (orig) == LABEL_REF | |
569 | || (GET_CODE (orig) == SYMBOL_REF | |
570 | )) | |
571 | { | |
572 | /* addr(foo) = &func+(foo-func) */ | |
573 | rtx pic_base; | |
574 | ||
575 | orig = machopic_indirect_data_reference (orig, reg); | |
576 | ||
9c808aad | 577 | if (GET_CODE (orig) == PLUS |
ee890fe2 SS |
578 | && GET_CODE (XEXP (orig, 0)) == REG) |
579 | { | |
580 | if (reg == 0) | |
581 | return force_reg (mode, orig); | |
582 | ||
583 | emit_move_insn (reg, orig); | |
584 | return reg; | |
9c808aad | 585 | } |
ee890fe2 | 586 | |
ab82a49f AP |
587 | /* if dynamic-no-pic then use 0 as the pic base */ |
588 | if (MACHO_DYNAMIC_NO_PIC_P) | |
589 | pic_base = CONST0_RTX (Pmode); | |
590 | else | |
11abc112 | 591 | pic_base = machopic_function_base_sym (); |
ee890fe2 SS |
592 | |
593 | if (GET_CODE (orig) == MEM) | |
594 | { | |
595 | if (reg == 0) | |
596 | { | |
597 | if (reload_in_progress) | |
598 | abort (); | |
599 | else | |
600 | reg = gen_reg_rtx (Pmode); | |
601 | } | |
9c808aad | 602 | |
ee890fe2 | 603 | #ifdef HAVE_lo_sum |
ab82a49f AP |
604 | if (MACHO_DYNAMIC_NO_PIC_P |
605 | && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF | |
606 | || GET_CODE (XEXP (orig, 0)) == LABEL_REF)) | |
607 | { | |
608 | #if defined (TARGET_TOC) /* ppc */ | |
609 | rtx temp_reg = (no_new_pseudos) ? reg : gen_reg_rtx (Pmode); | |
610 | rtx asym = XEXP (orig, 0); | |
611 | rtx mem; | |
612 | ||
b8a55285 | 613 | emit_insn (gen_macho_high (temp_reg, asym)); |
542a8afa RH |
614 | mem = gen_const_mem (GET_MODE (orig), |
615 | gen_rtx_LO_SUM (Pmode, temp_reg, asym)); | |
6f94a68e | 616 | emit_insn (gen_rtx_SET (VOIDmode, reg, mem)); |
ab82a49f AP |
617 | #else |
618 | /* Some other CPU -- WriteMe! but right now there are no other platform that can use dynamic-no-pic */ | |
619 | abort (); | |
620 | #endif | |
621 | pic_ref = reg; | |
622 | } | |
623 | else | |
9c808aad | 624 | if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF |
ee890fe2 SS |
625 | || GET_CODE (XEXP (orig, 0)) == LABEL_REF) |
626 | { | |
6f94a68e GK |
627 | rtx offset = gen_rtx_CONST (Pmode, |
628 | gen_rtx_MINUS (Pmode, | |
629 | XEXP (orig, 0), | |
630 | pic_base)); | |
ee890fe2 SS |
631 | #if defined (TARGET_TOC) /* i.e., PowerPC */ |
632 | /* Generating a new reg may expose opportunities for | |
633 | common subexpression elimination. */ | |
49bd1d27 | 634 | rtx hi_sum_reg = no_new_pseudos ? reg : gen_reg_rtx (Pmode); |
6f94a68e GK |
635 | rtx mem; |
636 | rtx insn; | |
637 | rtx sum; | |
638 | ||
639 | sum = gen_rtx_HIGH (Pmode, offset); | |
640 | if (! MACHO_DYNAMIC_NO_PIC_P) | |
641 | sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum); | |
ee890fe2 | 642 | |
6f94a68e GK |
643 | emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum)); |
644 | ||
542a8afa RH |
645 | mem = gen_const_mem (GET_MODE (orig), |
646 | gen_rtx_LO_SUM (Pmode, | |
647 | hi_sum_reg, offset)); | |
6f94a68e GK |
648 | insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem)); |
649 | REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, pic_ref, | |
650 | REG_NOTES (insn)); | |
651 | ||
652 | pic_ref = reg; | |
ee890fe2 | 653 | #else |
6f94a68e GK |
654 | emit_insn (gen_rtx_USE (VOIDmode, |
655 | gen_rtx_REG (Pmode, | |
656 | PIC_OFFSET_TABLE_REGNUM))); | |
657 | ||
658 | emit_insn (gen_rtx_SET (VOIDmode, reg, | |
659 | gen_rtx_HIGH (Pmode, | |
660 | gen_rtx_CONST (Pmode, | |
661 | offset)))); | |
662 | emit_insn (gen_rtx_SET (VOIDmode, reg, | |
663 | gen_rtx_LO_SUM (Pmode, reg, | |
664 | gen_rtx_CONST (Pmode, offset)))); | |
665 | pic_ref = gen_rtx_PLUS (Pmode, | |
666 | pic_offset_table_rtx, reg); | |
ee890fe2 SS |
667 | #endif |
668 | } | |
669 | else | |
670 | #endif /* HAVE_lo_sum */ | |
671 | { | |
672 | rtx pic = pic_offset_table_rtx; | |
673 | if (GET_CODE (pic) != REG) | |
674 | { | |
675 | emit_move_insn (reg, pic); | |
676 | pic = reg; | |
677 | } | |
678 | #if 0 | |
6f94a68e GK |
679 | emit_insn (gen_rtx_USE (VOIDmode, |
680 | gen_rtx_REG (Pmode, | |
681 | PIC_OFFSET_TABLE_REGNUM))); | |
ee890fe2 SS |
682 | #endif |
683 | ||
6f94a68e GK |
684 | pic_ref = gen_rtx_PLUS (Pmode, |
685 | pic, | |
686 | gen_rtx_CONST (Pmode, | |
687 | gen_rtx_MINUS (Pmode, | |
688 | XEXP (orig, 0), | |
689 | pic_base))); | |
ee890fe2 | 690 | } |
9c808aad | 691 | |
ee890fe2 | 692 | #if !defined (TARGET_TOC) |
ee890fe2 | 693 | emit_move_insn (reg, pic_ref); |
542a8afa | 694 | pic_ref = gen_const_mem (GET_MODE (orig), reg); |
f9b0ac3b | 695 | #endif |
ee890fe2 SS |
696 | } |
697 | else | |
698 | { | |
699 | ||
700 | #ifdef HAVE_lo_sum | |
9c808aad | 701 | if (GET_CODE (orig) == SYMBOL_REF |
ee890fe2 SS |
702 | || GET_CODE (orig) == LABEL_REF) |
703 | { | |
6f94a68e GK |
704 | rtx offset = gen_rtx_CONST (Pmode, |
705 | gen_rtx_MINUS (Pmode, | |
706 | orig, pic_base)); | |
ee890fe2 SS |
707 | #if defined (TARGET_TOC) /* i.e., PowerPC */ |
708 | rtx hi_sum_reg; | |
709 | ||
710 | if (reg == 0) | |
711 | { | |
712 | if (reload_in_progress) | |
713 | abort (); | |
714 | else | |
49bd1d27 | 715 | reg = gen_reg_rtx (Pmode); |
ee890fe2 | 716 | } |
9c808aad | 717 | |
ee890fe2 SS |
718 | hi_sum_reg = reg; |
719 | ||
6f94a68e GK |
720 | emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, |
721 | (MACHO_DYNAMIC_NO_PIC_P) | |
722 | ? gen_rtx_HIGH (Pmode, offset) | |
723 | : gen_rtx_PLUS (Pmode, | |
724 | pic_offset_table_rtx, | |
725 | gen_rtx_HIGH (Pmode, | |
726 | offset)))); | |
727 | emit_insn (gen_rtx_SET (VOIDmode, reg, | |
728 | gen_rtx_LO_SUM (Pmode, | |
729 | hi_sum_reg, offset))); | |
ee890fe2 SS |
730 | pic_ref = reg; |
731 | #else | |
6f94a68e GK |
732 | emit_insn (gen_rtx_SET (VOIDmode, reg, |
733 | gen_rtx_HIGH (Pmode, offset))); | |
734 | emit_insn (gen_rtx_SET (VOIDmode, reg, | |
735 | gen_rtx_LO_SUM (Pmode, reg, offset))); | |
736 | pic_ref = gen_rtx_PLUS (Pmode, | |
737 | pic_offset_table_rtx, reg); | |
ee890fe2 SS |
738 | #endif |
739 | } | |
740 | else | |
741 | #endif /* HAVE_lo_sum */ | |
742 | { | |
743 | if (GET_CODE (orig) == REG) | |
744 | { | |
745 | return orig; | |
746 | } | |
747 | else | |
748 | { | |
749 | rtx pic = pic_offset_table_rtx; | |
750 | if (GET_CODE (pic) != REG) | |
751 | { | |
752 | emit_move_insn (reg, pic); | |
753 | pic = reg; | |
754 | } | |
755 | #if 0 | |
6f94a68e GK |
756 | emit_insn (gen_rtx_USE (VOIDmode, |
757 | pic_offset_table_rtx)); | |
ee890fe2 | 758 | #endif |
6f94a68e GK |
759 | pic_ref = gen_rtx_PLUS (Pmode, |
760 | pic, | |
761 | gen_rtx_CONST (Pmode, | |
762 | gen_rtx_MINUS (Pmode, | |
763 | orig, pic_base))); | |
ee890fe2 SS |
764 | } |
765 | } | |
766 | } | |
767 | ||
ee890fe2 SS |
768 | if (GET_CODE (pic_ref) != REG) |
769 | { | |
770 | if (reg != 0) | |
771 | { | |
772 | emit_move_insn (reg, pic_ref); | |
773 | return reg; | |
774 | } | |
775 | else | |
776 | { | |
777 | return force_reg (mode, pic_ref); | |
778 | } | |
779 | } | |
780 | else | |
781 | { | |
782 | return pic_ref; | |
783 | } | |
784 | } | |
785 | ||
786 | else if (GET_CODE (orig) == SYMBOL_REF) | |
787 | return orig; | |
788 | ||
789 | else if (GET_CODE (orig) == PLUS | |
790 | && (GET_CODE (XEXP (orig, 0)) == MEM | |
791 | || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF | |
792 | || GET_CODE (XEXP (orig, 0)) == LABEL_REF) | |
793 | && XEXP (orig, 0) != pic_offset_table_rtx | |
794 | && GET_CODE (XEXP (orig, 1)) != REG) | |
9c808aad | 795 | |
ee890fe2 SS |
796 | { |
797 | rtx base; | |
798 | int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM); | |
799 | ||
800 | base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); | |
801 | orig = machopic_legitimize_pic_address (XEXP (orig, 1), | |
802 | Pmode, (base == reg ? 0 : reg)); | |
803 | if (GET_CODE (orig) == CONST_INT) | |
804 | { | |
ed8908e7 | 805 | pic_ref = plus_constant (base, INTVAL (orig)); |
ee890fe2 SS |
806 | is_complex = 1; |
807 | } | |
808 | else | |
6f94a68e | 809 | pic_ref = gen_rtx_PLUS (Pmode, base, orig); |
ee890fe2 | 810 | |
ee890fe2 SS |
811 | if (reg && is_complex) |
812 | { | |
813 | emit_move_insn (reg, pic_ref); | |
814 | pic_ref = reg; | |
815 | } | |
816 | /* Likewise, should we set special REG_NOTEs here? */ | |
817 | } | |
818 | ||
819 | else if (GET_CODE (orig) == CONST) | |
820 | { | |
821 | return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); | |
822 | } | |
823 | ||
824 | else if (GET_CODE (orig) == MEM | |
825 | && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF) | |
826 | { | |
827 | rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); | |
389fdba0 | 828 | addr = replace_equiv_address (orig, addr); |
ee890fe2 SS |
829 | emit_move_insn (reg, addr); |
830 | pic_ref = reg; | |
831 | } | |
832 | ||
833 | return pic_ref; | |
834 | } | |
835 | ||
11abc112 MM |
836 | /* Output the stub or non-lazy pointer in *SLOT, if it has been used. |
837 | DATA is the FILE* for assembly output. Called from | |
838 | htab_traverse. */ | |
ee890fe2 | 839 | |
11abc112 MM |
840 | static int |
841 | machopic_output_indirection (void **slot, void *data) | |
ee890fe2 | 842 | { |
11abc112 MM |
843 | machopic_indirection *p = *((machopic_indirection **) slot); |
844 | FILE *asm_out_file = (FILE *) data; | |
845 | rtx symbol; | |
846 | const char *sym_name; | |
847 | const char *ptr_name; | |
848 | ||
849 | if (!p->used) | |
850 | return 1; | |
ee890fe2 | 851 | |
11abc112 MM |
852 | symbol = p->symbol; |
853 | sym_name = XSTR (symbol, 0); | |
1adaa117 | 854 | ptr_name = p->ptr_name; |
11abc112 MM |
855 | |
856 | if (p->stub_p) | |
ee890fe2 | 857 | { |
ee890fe2 SS |
858 | char *sym; |
859 | char *stub; | |
ee890fe2 | 860 | |
ee890fe2 SS |
861 | sym = alloca (strlen (sym_name) + 2); |
862 | if (sym_name[0] == '*' || sym_name[0] == '&') | |
863 | strcpy (sym, sym_name + 1); | |
864 | else if (sym_name[0] == '-' || sym_name[0] == '+') | |
9c808aad | 865 | strcpy (sym, sym_name); |
ee890fe2 | 866 | else |
789a4ea3 | 867 | sprintf (sym, "%s%s", user_label_prefix, sym_name); |
ee890fe2 | 868 | |
11abc112 MM |
869 | stub = alloca (strlen (ptr_name) + 2); |
870 | if (ptr_name[0] == '*' || ptr_name[0] == '&') | |
871 | strcpy (stub, ptr_name + 1); | |
ee890fe2 | 872 | else |
11abc112 | 873 | sprintf (stub, "%s%s", user_label_prefix, ptr_name); |
ee890fe2 | 874 | |
ca472546 | 875 | machopic_output_stub (asm_out_file, sym, stub); |
ee890fe2 | 876 | } |
699c914a | 877 | else if (! indirect_data (symbol) |
156a126c MS |
878 | && (machopic_symbol_defined_p (symbol) |
879 | || SYMBOL_REF_LOCAL_P (symbol))) | |
ee890fe2 | 880 | { |
11abc112 MM |
881 | data_section (); |
882 | assemble_align (GET_MODE_ALIGNMENT (Pmode)); | |
883 | assemble_label (ptr_name); | |
884 | assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name), | |
885 | GET_MODE_SIZE (Pmode), | |
886 | GET_MODE_ALIGNMENT (Pmode), 1); | |
ee890fe2 | 887 | } |
11abc112 MM |
888 | else |
889 | { | |
699c914a MS |
890 | rtx init = const0_rtx; |
891 | ||
11abc112 MM |
892 | machopic_nl_symbol_ptr_section (); |
893 | assemble_name (asm_out_file, ptr_name); | |
894 | fprintf (asm_out_file, ":\n"); | |
895 | ||
896 | fprintf (asm_out_file, "\t.indirect_symbol "); | |
897 | assemble_name (asm_out_file, sym_name); | |
898 | fprintf (asm_out_file, "\n"); | |
899 | ||
699c914a MS |
900 | /* Variables that are marked with MACHO_SYMBOL_STATIC need to |
901 | have their symbol name instead of 0 in the second entry of | |
902 | the non-lazy symbol pointer data structure when they are | |
903 | defined. This allows the runtime to rebind newer instances | |
904 | of the translation unit with the original instance of the | |
905 | data. */ | |
906 | ||
907 | if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC) | |
908 | && machopic_symbol_defined_p (symbol)) | |
909 | init = gen_rtx_SYMBOL_REF (Pmode, sym_name); | |
910 | ||
911 | assemble_integer (init, GET_MODE_SIZE (Pmode), | |
11abc112 MM |
912 | GET_MODE_ALIGNMENT (Pmode), 1); |
913 | } | |
914 | ||
915 | return 1; | |
916 | } | |
917 | ||
918 | void | |
919 | machopic_finish (FILE *asm_out_file) | |
920 | { | |
921 | if (machopic_indirections) | |
699c914a | 922 | htab_traverse_noresize (machopic_indirections, |
11abc112 MM |
923 | machopic_output_indirection, |
924 | asm_out_file); | |
ee890fe2 SS |
925 | } |
926 | ||
9c808aad AJ |
927 | int |
928 | machopic_operand_p (rtx op) | |
ee890fe2 SS |
929 | { |
930 | if (MACHOPIC_JUST_INDIRECT) | |
931 | { | |
932 | while (GET_CODE (op) == CONST) | |
933 | op = XEXP (op, 0); | |
934 | ||
935 | if (GET_CODE (op) == SYMBOL_REF) | |
11abc112 | 936 | return machopic_symbol_defined_p (op); |
ee890fe2 SS |
937 | else |
938 | return 0; | |
939 | } | |
940 | ||
941 | while (GET_CODE (op) == CONST) | |
942 | op = XEXP (op, 0); | |
943 | ||
944 | if (GET_CODE (op) == MINUS | |
945 | && GET_CODE (XEXP (op, 0)) == SYMBOL_REF | |
946 | && GET_CODE (XEXP (op, 1)) == SYMBOL_REF | |
11abc112 MM |
947 | && machopic_symbol_defined_p (XEXP (op, 0)) |
948 | && machopic_symbol_defined_p (XEXP (op, 1))) | |
ee890fe2 SS |
949 | return 1; |
950 | ||
ee890fe2 SS |
951 | return 0; |
952 | } | |
df56a27f SS |
953 | |
954 | /* This function records whether a given name corresponds to a defined | |
955 | or undefined function or variable, for machopic_classify_ident to | |
956 | use later. */ | |
957 | ||
958 | void | |
9c808aad | 959 | darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED) |
df56a27f | 960 | { |
353e51f8 | 961 | rtx sym_ref; |
df56a27f | 962 | |
e1a4211d SS |
963 | /* Do the standard encoding things first. */ |
964 | default_encode_section_info (decl, rtl, first); | |
965 | ||
11abc112 MM |
966 | if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL) |
967 | return; | |
e1a4211d | 968 | |
11abc112 MM |
969 | sym_ref = XEXP (rtl, 0); |
970 | if (TREE_CODE (decl) == VAR_DECL) | |
971 | SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE; | |
972 | ||
973 | if (!DECL_EXTERNAL (decl) | |
f1a66265 | 974 | && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl)) |
df56a27f SS |
975 | && ((TREE_STATIC (decl) |
976 | && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl))) | |
bfbdca0b | 977 | || (!DECL_COMMON (decl) && DECL_INITIAL (decl) |
abe72dd8 | 978 | && DECL_INITIAL (decl) != error_mark_node))) |
11abc112 | 979 | SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED; |
699c914a MS |
980 | |
981 | if (TREE_CODE (decl) == VAR_DECL | |
982 | && indirect_data (sym_ref) | |
983 | && ! TREE_PUBLIC (decl)) | |
984 | SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC; | |
4e08ba6c | 985 | } |
2cc07db4 | 986 | |
8e3e233b DP |
987 | void |
988 | darwin_mark_decl_preserved (const char *name) | |
989 | { | |
990 | fprintf (asm_out_file, ".no_dead_strip "); | |
991 | assemble_name (asm_out_file, name); | |
992 | fputc ('\n', asm_out_file); | |
993 | } | |
994 | ||
ae46c4e0 | 995 | void |
9c808aad AJ |
996 | machopic_select_section (tree exp, int reloc, |
997 | unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED) | |
ae46c4e0 | 998 | { |
3d7964d5 | 999 | void (*base_function)(void); |
f1a66265 GK |
1000 | bool weak_p = DECL_P (exp) && DECL_WEAK (exp); |
1001 | static void (* const base_funs[][2])(void) = { | |
1002 | { text_section, text_coal_section }, | |
1003 | { text_unlikely_section, text_unlikely_coal_section }, | |
1004 | { readonly_data_section, const_coal_section }, | |
1005 | { const_data_section, const_data_coal_section }, | |
1006 | { data_section, data_coal_section } | |
1007 | }; | |
1008 | ||
1009 | if (TREE_CODE (exp) == FUNCTION_DECL) | |
1010 | base_function = base_funs[reloc][weak_p]; | |
1011 | else if (decl_readonly_section_1 (exp, reloc, MACHOPIC_INDIRECT)) | |
1012 | base_function = base_funs[2][weak_p]; | |
3d7964d5 | 1013 | else if (TREE_READONLY (exp) || TREE_CONSTANT (exp)) |
f1a66265 | 1014 | base_function = base_funs[3][weak_p]; |
3d7964d5 | 1015 | else |
f1a66265 | 1016 | base_function = base_funs[4][weak_p]; |
9c808aad | 1017 | |
3d7964d5 | 1018 | if (TREE_CODE (exp) == STRING_CST |
9c808aad | 1019 | && ((size_t) TREE_STRING_LENGTH (exp) |
3521b33c | 1020 | == strlen (TREE_STRING_POINTER (exp)) + 1)) |
3d7964d5 GK |
1021 | cstring_section (); |
1022 | else if ((TREE_CODE (exp) == INTEGER_CST || TREE_CODE (exp) == REAL_CST) | |
1023 | && flag_merge_constants) | |
ae46c4e0 RH |
1024 | { |
1025 | tree size = TYPE_SIZE (TREE_TYPE (exp)); | |
1026 | ||
1027 | if (TREE_CODE (size) == INTEGER_CST && | |
1028 | TREE_INT_CST_LOW (size) == 4 && | |
1029 | TREE_INT_CST_HIGH (size) == 0) | |
1030 | literal4_section (); | |
1031 | else if (TREE_CODE (size) == INTEGER_CST && | |
1032 | TREE_INT_CST_LOW (size) == 8 && | |
1033 | TREE_INT_CST_HIGH (size) == 0) | |
1034 | literal8_section (); | |
1035 | else | |
3d7964d5 | 1036 | base_function (); |
ae46c4e0 RH |
1037 | } |
1038 | else if (TREE_CODE (exp) == CONSTRUCTOR | |
1039 | && TREE_TYPE (exp) | |
1040 | && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE | |
1041 | && TYPE_NAME (TREE_TYPE (exp))) | |
1042 | { | |
1043 | tree name = TYPE_NAME (TREE_TYPE (exp)); | |
1044 | if (TREE_CODE (name) == TYPE_DECL) | |
1045 | name = DECL_NAME (name); | |
1046 | if (!strcmp (IDENTIFIER_POINTER (name), "NSConstantString")) | |
1047 | objc_constant_string_object_section (); | |
1048 | else if (!strcmp (IDENTIFIER_POINTER (name), "NXConstantString")) | |
1049 | objc_string_object_section (); | |
ae46c4e0 | 1050 | else |
3d7964d5 | 1051 | base_function (); |
ae46c4e0 RH |
1052 | } |
1053 | else if (TREE_CODE (exp) == VAR_DECL && | |
1054 | DECL_NAME (exp) && | |
1055 | TREE_CODE (DECL_NAME (exp)) == IDENTIFIER_NODE && | |
1056 | IDENTIFIER_POINTER (DECL_NAME (exp)) && | |
1057 | !strncmp (IDENTIFIER_POINTER (DECL_NAME (exp)), "_OBJC_", 6)) | |
1058 | { | |
1059 | const char *name = IDENTIFIER_POINTER (DECL_NAME (exp)); | |
1060 | ||
1061 | if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20)) | |
1062 | objc_cls_meth_section (); | |
1063 | else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23)) | |
1064 | objc_inst_meth_section (); | |
1065 | else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 20)) | |
1066 | objc_cat_cls_meth_section (); | |
1067 | else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 23)) | |
1068 | objc_cat_inst_meth_section (); | |
1069 | else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22)) | |
1070 | objc_class_vars_section (); | |
1071 | else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25)) | |
1072 | objc_instance_vars_section (); | |
1073 | else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22)) | |
1074 | objc_cat_cls_meth_section (); | |
1075 | else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17)) | |
1076 | objc_class_names_section (); | |
1077 | else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20)) | |
1078 | objc_meth_var_names_section (); | |
1079 | else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20)) | |
1080 | objc_meth_var_types_section (); | |
1081 | else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22)) | |
1082 | objc_cls_refs_section (); | |
1083 | else if (!strncmp (name, "_OBJC_CLASS_", 12)) | |
1084 | objc_class_section (); | |
1085 | else if (!strncmp (name, "_OBJC_METACLASS_", 16)) | |
1086 | objc_meta_class_section (); | |
1087 | else if (!strncmp (name, "_OBJC_CATEGORY_", 15)) | |
1088 | objc_category_section (); | |
1089 | else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25)) | |
1090 | objc_selector_refs_section (); | |
1091 | else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20)) | |
1092 | objc_selector_fixup_section (); | |
1093 | else if (!strncmp (name, "_OBJC_SYMBOLS", 13)) | |
1094 | objc_symbols_section (); | |
1095 | else if (!strncmp (name, "_OBJC_MODULES", 13)) | |
1096 | objc_module_info_section (); | |
264fa2db ZL |
1097 | else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16)) |
1098 | objc_image_info_section (); | |
ae46c4e0 RH |
1099 | else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32)) |
1100 | objc_cat_inst_meth_section (); | |
1101 | else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29)) | |
1102 | objc_cat_cls_meth_section (); | |
1103 | else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20)) | |
1104 | objc_cat_cls_meth_section (); | |
1105 | else if (!strncmp (name, "_OBJC_PROTOCOL_", 15)) | |
1106 | objc_protocol_section (); | |
ae46c4e0 | 1107 | else |
3d7964d5 | 1108 | base_function (); |
ae46c4e0 | 1109 | } |
f1a66265 GK |
1110 | /* ::operator new and ::operator delete must be coalesced, even |
1111 | if not weak. There are 8 variants that we look for. */ | |
1112 | else if (TREE_CODE (exp) == FUNCTION_DECL | |
1113 | && ! DECL_ONE_ONLY (exp)) | |
1114 | { | |
1115 | const char * name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (exp)); | |
1116 | if (name[0] == '_' && name[1] == 'Z' | |
1117 | && ((name[2] == 'n' && (name[3] == 'a' || name[3] == 'w') | |
1118 | && name[4] == 'm') | |
1119 | || (name[2] == 'd' && (name[3] == 'a' || name[3] == 'l') | |
1120 | && name[4] == 'P' && name[5] == 'v'))) | |
1121 | { | |
1122 | bool delete_p = name[2] == 'd'; | |
1123 | if (name[5 + delete_p] == 0 | |
1124 | || strcmp (name + 5 + delete_p, "KSt9nothrow_t") == 0) | |
1125 | base_funs[reloc][1] (); | |
1126 | else | |
1127 | base_function (); | |
1128 | } | |
1129 | else | |
1130 | base_function (); | |
1131 | } | |
ae46c4e0 | 1132 | else |
3d7964d5 | 1133 | base_function (); |
ae46c4e0 RH |
1134 | } |
1135 | ||
b64a1b53 | 1136 | /* This can be called with address expressions as "rtx". |
991b6592 | 1137 | They must go in "const". */ |
b64a1b53 RH |
1138 | |
1139 | void | |
9c808aad AJ |
1140 | machopic_select_rtx_section (enum machine_mode mode, rtx x, |
1141 | unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED) | |
b64a1b53 RH |
1142 | { |
1143 | if (GET_MODE_SIZE (mode) == 8) | |
1144 | literal8_section (); | |
1145 | else if (GET_MODE_SIZE (mode) == 4 | |
1146 | && (GET_CODE (x) == CONST_INT | |
1147 | || GET_CODE (x) == CONST_DOUBLE)) | |
1148 | literal4_section (); | |
86b0a4f3 | 1149 | else if (MACHOPIC_INDIRECT |
2e53734e GK |
1150 | && (GET_CODE (x) == SYMBOL_REF |
1151 | || GET_CODE (x) == CONST | |
1152 | || GET_CODE (x) == LABEL_REF)) | |
1153 | const_data_section (); | |
b64a1b53 RH |
1154 | else |
1155 | const_section (); | |
1156 | } | |
1157 | ||
2cc07db4 | 1158 | void |
9c808aad | 1159 | machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED) |
2cc07db4 | 1160 | { |
ab82a49f | 1161 | if (MACHOPIC_INDIRECT) |
2cc07db4 RH |
1162 | mod_init_section (); |
1163 | else | |
1164 | constructor_section (); | |
c8af3574 RH |
1165 | assemble_align (POINTER_SIZE); |
1166 | assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1); | |
2cc07db4 | 1167 | |
ab82a49f | 1168 | if (! MACHOPIC_INDIRECT) |
2cc07db4 RH |
1169 | fprintf (asm_out_file, ".reference .constructors_used\n"); |
1170 | } | |
1171 | ||
1172 | void | |
9c808aad | 1173 | machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED) |
2cc07db4 | 1174 | { |
ab82a49f | 1175 | if (MACHOPIC_INDIRECT) |
2cc07db4 RH |
1176 | mod_term_section (); |
1177 | else | |
1178 | destructor_section (); | |
c8af3574 RH |
1179 | assemble_align (POINTER_SIZE); |
1180 | assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1); | |
2cc07db4 | 1181 | |
ab82a49f | 1182 | if (! MACHOPIC_INDIRECT) |
2cc07db4 RH |
1183 | fprintf (asm_out_file, ".reference .destructors_used\n"); |
1184 | } | |
e2500fed | 1185 | |
5eb99654 | 1186 | void |
9c808aad | 1187 | darwin_globalize_label (FILE *stream, const char *name) |
5eb99654 KG |
1188 | { |
1189 | if (!!strncmp (name, "_OBJC_", 6)) | |
1190 | default_globalize_label (stream, name); | |
1191 | } | |
1192 | ||
4746cf84 | 1193 | void |
c18a5b6c MM |
1194 | darwin_asm_named_section (const char *name, |
1195 | unsigned int flags ATTRIBUTE_UNUSED, | |
1196 | tree decl ATTRIBUTE_UNUSED) | |
4746cf84 | 1197 | { |
f1a66265 | 1198 | fprintf (asm_out_file, "\t.section %s\n", name); |
4746cf84 MA |
1199 | } |
1200 | ||
4746cf84 | 1201 | void |
f1a66265 | 1202 | darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED) |
4746cf84 | 1203 | { |
f1a66265 | 1204 | /* Darwin does not use unique sections. */ |
4746cf84 MA |
1205 | } |
1206 | ||
005c1a13 GK |
1207 | /* Handle a "weak_import" attribute; arguments as in |
1208 | struct attribute_spec.handler. */ | |
1209 | ||
1210 | tree | |
1211 | darwin_handle_weak_import_attribute (tree *node, tree name, | |
1212 | tree ARG_UNUSED (args), | |
1213 | int ARG_UNUSED (flags), | |
1214 | bool * no_add_attrs) | |
1215 | { | |
1216 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
1217 | { | |
9e637a26 | 1218 | warning ("%qs attribute ignored", IDENTIFIER_POINTER (name)); |
005c1a13 GK |
1219 | *no_add_attrs = true; |
1220 | } | |
1221 | else | |
1222 | declare_weak (*node); | |
1223 | ||
1224 | return NULL_TREE; | |
1225 | } | |
45cc4783 MS |
1226 | |
1227 | static void | |
1228 | no_dead_strip (FILE *file, const char *lab) | |
1229 | { | |
005c1a13 | 1230 | fprintf (file, ".no_dead_strip %s\n", lab); |
45cc4783 MS |
1231 | } |
1232 | ||
4746cf84 | 1233 | /* Emit a label for an FDE, making it global and/or weak if appropriate. |
eeab4d81 MS |
1234 | The third parameter is nonzero if this is for exception handling. |
1235 | The fourth parameter is nonzero if this is just a placeholder for an | |
4746cf84 | 1236 | FDE that we are omitting. */ |
eeab4d81 | 1237 | |
4746cf84 | 1238 | void |
eeab4d81 | 1239 | darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty) |
4746cf84 MA |
1240 | { |
1241 | tree id = DECL_ASSEMBLER_NAME (decl) | |
1242 | ? DECL_ASSEMBLER_NAME (decl) | |
1243 | : DECL_NAME (decl); | |
1244 | ||
1245 | const char *prefix = "_"; | |
1246 | const int prefix_len = 1; | |
1247 | ||
1248 | const char *base = IDENTIFIER_POINTER (id); | |
1249 | unsigned int base_len = IDENTIFIER_LENGTH (id); | |
1250 | ||
1251 | const char *suffix = ".eh"; | |
4746cf84 MA |
1252 | |
1253 | int need_quotes = name_needs_quotes (base); | |
1254 | int quotes_len = need_quotes ? 2 : 0; | |
eeab4d81 MS |
1255 | char *lab; |
1256 | ||
1257 | if (! for_eh) | |
1258 | suffix = ".eh1"; | |
4746cf84 | 1259 | |
eeab4d81 | 1260 | lab = xmalloc (prefix_len + base_len + strlen (suffix) + quotes_len + 1); |
4746cf84 MA |
1261 | lab[0] = '\0'; |
1262 | ||
1263 | if (need_quotes) | |
1264 | strcat(lab, "\""); | |
1265 | strcat(lab, prefix); | |
1266 | strcat(lab, base); | |
1267 | strcat(lab, suffix); | |
1268 | if (need_quotes) | |
1269 | strcat(lab, "\""); | |
1270 | ||
1271 | if (TREE_PUBLIC (decl)) | |
f1a66265 | 1272 | fprintf (file, "\t%s %s\n", |
4746cf84 MA |
1273 | (DECL_VISIBILITY (decl) != VISIBILITY_HIDDEN |
1274 | ? ".globl" | |
1275 | : ".private_extern"), | |
1276 | lab); | |
1277 | ||
f1a66265 GK |
1278 | if (DECL_WEAK (decl)) |
1279 | fprintf (file, "\t.weak_definition %s\n", lab); | |
4746cf84 MA |
1280 | |
1281 | if (empty) | |
45cc4783 MS |
1282 | { |
1283 | fprintf (file, "%s = 0\n", lab); | |
1284 | ||
1285 | /* Mark the absolute .eh and .eh1 style labels as needed to | |
1286 | ensure that we don't dead code strip them and keep such | |
1287 | labels from another instantiation point until we can fix this | |
1288 | properly with group comdat support. */ | |
1289 | no_dead_strip (file, lab); | |
1290 | } | |
4746cf84 MA |
1291 | else |
1292 | fprintf (file, "%s:\n", lab); | |
1293 | ||
1294 | free (lab); | |
1295 | } | |
1296 | ||
1297 | /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */ | |
eeab4d81 | 1298 | |
4746cf84 MA |
1299 | void |
1300 | darwin_non_lazy_pcrel (FILE *file, rtx addr) | |
1301 | { | |
4746cf84 MA |
1302 | const char *nlp_name; |
1303 | ||
1304 | if (GET_CODE (addr) != SYMBOL_REF) | |
1305 | abort (); | |
1306 | ||
11abc112 | 1307 | nlp_name = machopic_indirection_name (addr, /*stub_p=*/false); |
4746cf84 MA |
1308 | fputs ("\t.long\t", file); |
1309 | ASM_OUTPUT_LABELREF (file, nlp_name); | |
1310 | fputs ("-.", file); | |
1311 | } | |
1312 | ||
6ce4806b MA |
1313 | /* Emit an assembler directive to set visibility for a symbol. The |
1314 | only supported visibilities are VISIBILITY_DEFAULT and | |
1315 | VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private | |
1316 | extern". There is no MACH-O equivalent of ELF's | |
1317 | VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */ | |
1318 | ||
1319 | void | |
1320 | darwin_assemble_visibility (tree decl, int vis) | |
1321 | { | |
1322 | if (vis == VISIBILITY_DEFAULT) | |
1323 | ; | |
1324 | else if (vis == VISIBILITY_HIDDEN) | |
1325 | { | |
1326 | fputs ("\t.private_extern ", asm_out_file); | |
1327 | assemble_name (asm_out_file, | |
1328 | (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)))); | |
1329 | fputs ("\n", asm_out_file); | |
1330 | } | |
1331 | else | |
9940eedb | 1332 | warning ("internal and protected visibility attributes not supported " |
6ce4806b MA |
1333 | "in this configuration; ignored"); |
1334 | } | |
1335 | ||
7606e68f SS |
1336 | /* Output a difference of two labels that will be an assembly time |
1337 | constant if the two labels are local. (.long lab1-lab2 will be | |
1338 | very different if lab1 is at the boundary between two sections; it | |
1339 | will be relocated according to the second section, not the first, | |
1340 | so one ends up with a difference between labels in different | |
1341 | sections, which is bad in the dwarf2 eh context for instance.) */ | |
1342 | ||
1343 | static int darwin_dwarf_label_counter; | |
1344 | ||
1345 | void | |
9c808aad AJ |
1346 | darwin_asm_output_dwarf_delta (FILE *file, int size ATTRIBUTE_UNUSED, |
1347 | const char *lab1, const char *lab2) | |
7606e68f | 1348 | { |
4746cf84 MA |
1349 | int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L' |
1350 | && lab2[0] == '*' && lab2[1] == 'L'); | |
7606e68f SS |
1351 | |
1352 | if (islocaldiff) | |
1353 | fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter); | |
1354 | else | |
1355 | fprintf (file, "\t%s\t", ".long"); | |
1356 | assemble_name (file, lab1); | |
1357 | fprintf (file, "-"); | |
1358 | assemble_name (file, lab2); | |
1359 | if (islocaldiff) | |
1360 | fprintf (file, "\n\t.long L$set$%d", darwin_dwarf_label_counter++); | |
1361 | } | |
1362 | ||
a5fe455b | 1363 | void |
9c808aad | 1364 | darwin_file_end (void) |
a5fe455b ZW |
1365 | { |
1366 | machopic_finish (asm_out_file); | |
1367 | if (strcmp (lang_hooks.name, "GNU C++") == 0) | |
1368 | { | |
1369 | constructor_section (); | |
1370 | destructor_section (); | |
1371 | ASM_OUTPUT_ALIGN (asm_out_file, 1); | |
1372 | } | |
92b9a671 | 1373 | fprintf (asm_out_file, "\t.subsections_via_symbols\n"); |
a5fe455b ZW |
1374 | } |
1375 | ||
699c914a MS |
1376 | /* True, iff we're generating fast turn around debugging code. When |
1377 | true, we arrange for function prologues to start with 4 nops so | |
1378 | that gdb may insert code to redirect them, and for data to accessed | |
1379 | indirectly. The runtime uses this indirection to forward | |
1380 | references for data to the original instance of that data. */ | |
1381 | ||
1382 | int darwin_fix_and_continue; | |
1383 | const char *darwin_fix_and_continue_switch; | |
1384 | ||
e2500fed | 1385 | #include "gt-darwin.h" |