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