IA MCU psABI support: changes to libraries
[gcc.git] / gcc / config / darwin-c.c
1 /* Darwin support needed only by C/C++ frontends.
2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
3 Contributed by Apple Computer Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "cpplib.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "target.h"
30 #include "incpath.h"
31 #include "c-family/c-common.h"
32 #include "c-family/c-pragma.h"
33 #include "c-family/c-format.h"
34 #include "diagnostic-core.h"
35 #include "flags.h"
36 #include "tm_p.h"
37 #include "cppdefault.h"
38 #include "prefix.h"
39 #include "c-family/c-target.h"
40 #include "c-family/c-target-def.h"
41 #include "predict.h"
42 #include "dominance.h"
43 #include "cfg.h"
44 #include "cfgrtl.h"
45 #include "cfganal.h"
46 #include "lcm.h"
47 #include "cfgbuild.h"
48 #include "cfgcleanup.h"
49 #include "basic-block.h"
50 #include "hard-reg-set.h"
51 #include "function.h"
52 #include "cgraph.h"
53 #include "../../libcpp/internal.h"
54
55 /* Pragmas. */
56
57 #define BAD(gmsgid) do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
58 #define BAD2(msgid, arg) do { warning (OPT_Wpragmas, msgid, arg); return; } while (0)
59
60 static bool using_frameworks = false;
61
62 static const char *find_subframework_header (cpp_reader *pfile, const char *header,
63 cpp_dir **dirp);
64
65 typedef struct align_stack
66 {
67 int alignment;
68 struct align_stack * prev;
69 } align_stack;
70
71 static struct align_stack * field_align_stack = NULL;
72
73 /* Maintain a small stack of alignments. This is similar to pragma
74 pack's stack, but simpler. */
75
76 static void
77 push_field_alignment (int bit_alignment)
78 {
79 align_stack *entry = XNEW (align_stack);
80
81 entry->alignment = maximum_field_alignment;
82 entry->prev = field_align_stack;
83 field_align_stack = entry;
84
85 maximum_field_alignment = bit_alignment;
86 }
87
88 static void
89 pop_field_alignment (void)
90 {
91 if (field_align_stack)
92 {
93 align_stack *entry = field_align_stack;
94
95 maximum_field_alignment = entry->alignment;
96 field_align_stack = entry->prev;
97 free (entry);
98 }
99 else
100 error ("too many #pragma options align=reset");
101 }
102
103 /* Handlers for Darwin-specific pragmas. */
104
105 void
106 darwin_pragma_ignore (cpp_reader *pfile ATTRIBUTE_UNUSED)
107 {
108 /* Do nothing. */
109 }
110
111 /* #pragma options align={mac68k|power|reset} */
112
113 void
114 darwin_pragma_options (cpp_reader *pfile ATTRIBUTE_UNUSED)
115 {
116 const char *arg;
117 tree t, x;
118
119 if (pragma_lex (&t) != CPP_NAME)
120 BAD ("malformed '#pragma options', ignoring");
121 arg = IDENTIFIER_POINTER (t);
122 if (strcmp (arg, "align"))
123 BAD ("malformed '#pragma options', ignoring");
124 if (pragma_lex (&t) != CPP_EQ)
125 BAD ("malformed '#pragma options', ignoring");
126 if (pragma_lex (&t) != CPP_NAME)
127 BAD ("malformed '#pragma options', ignoring");
128
129 if (pragma_lex (&x) != CPP_EOF)
130 warning (OPT_Wpragmas, "junk at end of '#pragma options'");
131
132 arg = IDENTIFIER_POINTER (t);
133 if (!strcmp (arg, "mac68k"))
134 push_field_alignment (16);
135 else if (!strcmp (arg, "power"))
136 push_field_alignment (0);
137 else if (!strcmp (arg, "reset"))
138 pop_field_alignment ();
139 else
140 BAD ("malformed '#pragma options align={mac68k|power|reset}', ignoring");
141 }
142
143 /* #pragma unused ([var {, var}*]) */
144
145 void
146 darwin_pragma_unused (cpp_reader *pfile ATTRIBUTE_UNUSED)
147 {
148 tree decl, x;
149 int tok;
150
151 if (pragma_lex (&x) != CPP_OPEN_PAREN)
152 BAD ("missing '(' after '#pragma unused', ignoring");
153
154 while (1)
155 {
156 tok = pragma_lex (&decl);
157 if (tok == CPP_NAME && decl)
158 {
159 tree local = lookup_name (decl);
160 if (local && (TREE_CODE (local) == PARM_DECL
161 || TREE_CODE (local) == VAR_DECL))
162 {
163 TREE_USED (local) = 1;
164 DECL_READ_P (local) = 1;
165 }
166 tok = pragma_lex (&x);
167 if (tok != CPP_COMMA)
168 break;
169 }
170 }
171
172 if (tok != CPP_CLOSE_PAREN)
173 BAD ("missing ')' after '#pragma unused', ignoring");
174
175 if (pragma_lex (&x) != CPP_EOF)
176 BAD ("junk at end of '#pragma unused'");
177 }
178
179 /* Parse the ms_struct pragma. */
180 void
181 darwin_pragma_ms_struct (cpp_reader *pfile ATTRIBUTE_UNUSED)
182 {
183 const char *arg;
184 tree t;
185
186 if (pragma_lex (&t) != CPP_NAME)
187 BAD ("malformed '#pragma ms_struct', ignoring");
188 arg = IDENTIFIER_POINTER (t);
189
190 if (!strcmp (arg, "on"))
191 darwin_ms_struct = true;
192 else if (!strcmp (arg, "off") || !strcmp (arg, "reset"))
193 darwin_ms_struct = false;
194 else
195 BAD ("malformed '#pragma ms_struct {on|off|reset}', ignoring");
196
197 if (pragma_lex (&t) != CPP_EOF)
198 BAD ("junk at end of '#pragma ms_struct'");
199 }
200
201 static struct frameworks_in_use {
202 size_t len;
203 const char *name;
204 cpp_dir* dir;
205 } *frameworks_in_use;
206 static int num_frameworks = 0;
207 static int max_frameworks = 0;
208
209
210 /* Remember which frameworks have been seen, so that we can ensure
211 that all uses of that framework come from the same framework. DIR
212 is the place where the named framework NAME, which is of length
213 LEN, was found. We copy the directory name from NAME, as it will be
214 freed by others. */
215
216 static void
217 add_framework (const char *name, size_t len, cpp_dir *dir)
218 {
219 char *dir_name;
220 int i;
221 for (i = 0; i < num_frameworks; ++i)
222 {
223 if (len == frameworks_in_use[i].len
224 && strncmp (name, frameworks_in_use[i].name, len) == 0)
225 {
226 return;
227 }
228 }
229 if (i >= max_frameworks)
230 {
231 max_frameworks = i*2;
232 max_frameworks += i == 0;
233 frameworks_in_use = XRESIZEVEC (struct frameworks_in_use,
234 frameworks_in_use, max_frameworks);
235 }
236 dir_name = XNEWVEC (char, len + 1);
237 memcpy (dir_name, name, len);
238 dir_name[len] = '\0';
239 frameworks_in_use[num_frameworks].name = dir_name;
240 frameworks_in_use[num_frameworks].len = len;
241 frameworks_in_use[num_frameworks].dir = dir;
242 ++num_frameworks;
243 }
244
245 /* Recall if we have seen the named framework NAME, before, and where
246 we saw it. NAME is LEN bytes long. The return value is the place
247 where it was seen before. */
248
249 static struct cpp_dir*
250 find_framework (const char *name, size_t len)
251 {
252 int i;
253 for (i = 0; i < num_frameworks; ++i)
254 {
255 if (len == frameworks_in_use[i].len
256 && strncmp (name, frameworks_in_use[i].name, len) == 0)
257 {
258 return frameworks_in_use[i].dir;
259 }
260 }
261 return 0;
262 }
263
264 /* There are two directories in a framework that contain header files,
265 Headers and PrivateHeaders. We search Headers first as it is more
266 common to upgrade a header from PrivateHeaders to Headers and when
267 that is done, the old one might hang around and be out of data,
268 causing grief. */
269
270 struct framework_header {const char * dirName; int dirNameLen; };
271 static struct framework_header framework_header_dirs[] = {
272 { "Headers", 7 },
273 { "PrivateHeaders", 14 },
274 { NULL, 0 }
275 };
276
277 /* Returns a pointer to a malloced string that contains the real pathname
278 to the file, given the base name and the name. */
279
280 static char *
281 framework_construct_pathname (const char *fname, cpp_dir *dir)
282 {
283 const char *buf;
284 size_t fname_len, frname_len;
285 cpp_dir *fast_dir;
286 char *frname;
287 struct stat st;
288 int i;
289
290 /* Framework names must have a / in them. */
291 buf = strchr (fname, '/');
292 if (buf)
293 fname_len = buf - fname;
294 else
295 return 0;
296
297 fast_dir = find_framework (fname, fname_len);
298
299 /* Framework includes must all come from one framework. */
300 if (fast_dir && dir != fast_dir)
301 return 0;
302
303 frname = XNEWVEC (char, strlen (fname) + dir->len + 2
304 + strlen(".framework/") + strlen("PrivateHeaders"));
305 strncpy (&frname[0], dir->name, dir->len);
306 frname_len = dir->len;
307 if (frname_len && frname[frname_len-1] != '/')
308 frname[frname_len++] = '/';
309 strncpy (&frname[frname_len], fname, fname_len);
310 frname_len += fname_len;
311 strncpy (&frname[frname_len], ".framework/", strlen (".framework/"));
312 frname_len += strlen (".framework/");
313
314 if (fast_dir == 0)
315 {
316 frname[frname_len-1] = 0;
317 if (stat (frname, &st) == 0)
318 {
319 /* As soon as we find the first instance of the framework,
320 we stop and never use any later instance of that
321 framework. */
322 add_framework (fname, fname_len, dir);
323 }
324 else
325 {
326 /* If we can't find the parent directory, no point looking
327 further. */
328 free (frname);
329 return 0;
330 }
331 frname[frname_len-1] = '/';
332 }
333
334 /* Append framework_header_dirs and header file name */
335 for (i = 0; framework_header_dirs[i].dirName; i++)
336 {
337 strncpy (&frname[frname_len],
338 framework_header_dirs[i].dirName,
339 framework_header_dirs[i].dirNameLen);
340 strcpy (&frname[frname_len + framework_header_dirs[i].dirNameLen],
341 &fname[fname_len]);
342
343 if (stat (frname, &st) == 0)
344 return frname;
345 }
346
347 free (frname);
348 return 0;
349 }
350
351 /* Search for FNAME in sub-frameworks. pname is the context that we
352 wish to search in. Return the path the file was found at,
353 otherwise return 0. */
354
355 static const char*
356 find_subframework_file (const char *fname, const char *pname)
357 {
358 char *sfrname;
359 const char *dot_framework = ".framework/";
360 const char *bufptr;
361 int sfrname_len, i, fname_len;
362 struct cpp_dir *fast_dir;
363 static struct cpp_dir subframe_dir;
364 struct stat st;
365
366 bufptr = strchr (fname, '/');
367
368 /* Subframework files must have / in the name. */
369 if (bufptr == 0)
370 return 0;
371
372 fname_len = bufptr - fname;
373 fast_dir = find_framework (fname, fname_len);
374
375 /* Sub framework header filename includes parent framework name and
376 header name in the "CarbonCore/OSUtils.h" form. If it does not
377 include slash it is not a sub framework include. */
378 bufptr = strstr (pname, dot_framework);
379
380 /* If the parent header is not of any framework, then this header
381 cannot be part of any subframework. */
382 if (!bufptr)
383 return 0;
384
385 /* Now translate. For example, +- bufptr
386 fname = CarbonCore/OSUtils.h |
387 pname = /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
388 into
389 sfrname = /System/Library/Frameworks/Foundation.framework/Frameworks/CarbonCore.framework/Headers/OSUtils.h */
390
391 sfrname = XNEWVEC (char, strlen (pname) + strlen (fname) + 2 +
392 strlen ("Frameworks/") + strlen (".framework/")
393 + strlen ("PrivateHeaders"));
394
395 bufptr += strlen (dot_framework);
396
397 sfrname_len = bufptr - pname;
398
399 strncpy (&sfrname[0], pname, sfrname_len);
400
401 strncpy (&sfrname[sfrname_len], "Frameworks/", strlen ("Frameworks/"));
402 sfrname_len += strlen("Frameworks/");
403
404 strncpy (&sfrname[sfrname_len], fname, fname_len);
405 sfrname_len += fname_len;
406
407 strncpy (&sfrname[sfrname_len], ".framework/", strlen (".framework/"));
408 sfrname_len += strlen (".framework/");
409
410 /* Append framework_header_dirs and header file name */
411 for (i = 0; framework_header_dirs[i].dirName; i++)
412 {
413 strncpy (&sfrname[sfrname_len],
414 framework_header_dirs[i].dirName,
415 framework_header_dirs[i].dirNameLen);
416 strcpy (&sfrname[sfrname_len + framework_header_dirs[i].dirNameLen],
417 &fname[fname_len]);
418
419 if (stat (sfrname, &st) == 0)
420 {
421 if (fast_dir != &subframe_dir)
422 {
423 if (fast_dir)
424 warning (0, "subframework include %s conflicts with framework include",
425 fname);
426 else
427 add_framework (fname, fname_len, &subframe_dir);
428 }
429
430 return sfrname;
431 }
432 }
433 free (sfrname);
434
435 return 0;
436 }
437
438 /* Add PATH to the system includes. PATH must be malloc-ed and
439 NUL-terminated. System framework paths are C++ aware. */
440
441 static void
442 add_system_framework_path (char *path)
443 {
444 int cxx_aware = 1;
445 cpp_dir *p;
446
447 p = XNEW (cpp_dir);
448 p->next = NULL;
449 p->name = path;
450 p->sysp = 1 + !cxx_aware;
451 p->construct = framework_construct_pathname;
452 using_frameworks = 1;
453
454 add_cpp_dir_path (p, SYSTEM);
455 }
456
457 /* Add PATH to the bracket includes. PATH must be malloc-ed and
458 NUL-terminated. */
459
460 void
461 add_framework_path (char *path)
462 {
463 cpp_dir *p;
464
465 p = XNEW (cpp_dir);
466 p->next = NULL;
467 p->name = path;
468 p->sysp = 0;
469 p->construct = framework_construct_pathname;
470 using_frameworks = 1;
471
472 add_cpp_dir_path (p, BRACKET);
473 }
474
475 static const char *framework_defaults [] =
476 {
477 "/System/Library/Frameworks",
478 "/Library/Frameworks",
479 };
480
481 /* Register the GNU objective-C runtime include path if STDINC. */
482
483 void
484 darwin_register_objc_includes (const char *sysroot, const char *iprefix,
485 int stdinc)
486 {
487 const char *fname;
488 size_t len;
489 /* We do not do anything if we do not want the standard includes. */
490 if (!stdinc)
491 return;
492
493 fname = GCC_INCLUDE_DIR "-gnu-runtime";
494
495 /* Register the GNU OBJC runtime include path if we are compiling OBJC
496 with GNU-runtime. */
497
498 if (c_dialect_objc () && !flag_next_runtime)
499 {
500 char *str;
501 /* See if our directory starts with the standard prefix.
502 "Translate" them, i.e. replace /usr/local/lib/gcc... with
503 IPREFIX and search them first. */
504 if (iprefix && (len = cpp_GCC_INCLUDE_DIR_len) != 0 && !sysroot
505 && !strncmp (fname, cpp_GCC_INCLUDE_DIR, len))
506 {
507 str = concat (iprefix, fname + len, NULL);
508 /* FIXME: wrap the headers for C++awareness. */
509 add_path (str, SYSTEM, /*c++aware=*/false, false);
510 }
511
512 /* Should this directory start with the sysroot? */
513 if (sysroot)
514 str = concat (sysroot, fname, NULL);
515 else
516 str = update_path (fname, "");
517
518 add_path (str, SYSTEM, /*c++aware=*/false, false);
519 }
520 }
521
522
523 /* Register all the system framework paths if STDINC is true and setup
524 the missing_header callback for subframework searching if any
525 frameworks had been registered. */
526
527 void
528 darwin_register_frameworks (const char *sysroot,
529 const char *iprefix ATTRIBUTE_UNUSED, int stdinc)
530 {
531 if (stdinc)
532 {
533 size_t i;
534
535 /* Setup default search path for frameworks. */
536 for (i=0; i<sizeof (framework_defaults)/sizeof(const char *); ++i)
537 {
538 char *str;
539 if (sysroot)
540 str = concat (sysroot, xstrdup (framework_defaults [i]), NULL);
541 else
542 str = xstrdup (framework_defaults[i]);
543 /* System Framework headers are cxx aware. */
544 add_system_framework_path (str);
545 }
546 }
547
548 if (using_frameworks)
549 cpp_get_callbacks (parse_in)->missing_header = find_subframework_header;
550 }
551
552 /* Search for HEADER in context dependent way. The return value is
553 the malloced name of a header to try and open, if any, or NULL
554 otherwise. This is called after normal header lookup processing
555 fails to find a header. We search each file in the include stack,
556 using FUNC, starting from the most deeply nested include and
557 finishing with the main input file. We stop searching when FUNC
558 returns nonzero. */
559
560 static const char*
561 find_subframework_header (cpp_reader *pfile, const char *header, cpp_dir **dirp)
562 {
563 const char *fname = header;
564 struct cpp_buffer *b;
565 const char *n;
566
567 for (b = cpp_get_buffer (pfile);
568 b && cpp_get_file (b) && cpp_get_path (cpp_get_file (b));
569 b = cpp_get_prev (b))
570 {
571 n = find_subframework_file (fname, cpp_get_path (cpp_get_file (b)));
572 if (n)
573 {
574 /* Logically, the place where we found the subframework is
575 the place where we found the Framework that contains the
576 subframework. This is useful for tracking wether or not
577 we are in a system header. */
578 *dirp = cpp_get_dir (cpp_get_file (b));
579 return n;
580 }
581 }
582
583 return 0;
584 }
585
586 /* Given an OS X version VERSION_STR, return it as a statically-allocated array
587 of three integers. If VERSION_STR is invalid, return NULL.
588
589 VERSION_STR must consist of one, two, or three tokens, each separated by
590 a single period. Each token must contain only the characters '0' through
591 '9' and is converted to an equivalent non-negative decimal integer. Omitted
592 tokens become zeros. For example:
593
594 "10" becomes {10,0,0}
595 "10.10" becomes {10,10,0}
596 "10.10.1" becomes {10,10,1}
597 "10.000010.1" becomes {10,10,1}
598 "10.010.001" becomes {10,10,1}
599 "000010.10.00001" becomes {10,10,1}
600 ".9.1" is invalid
601 "10..9" is invalid
602 "10.10." is invalid */
603
604 enum version_components { MAJOR, MINOR, TINY };
605
606 static const unsigned long *
607 parse_version (const char *version_str)
608 {
609 size_t version_len;
610 char *end;
611 static unsigned long version_array[3];
612
613 version_len = strlen (version_str);
614 if (version_len < 1)
615 return NULL;
616
617 /* Version string must consist of digits and periods only. */
618 if (strspn (version_str, "0123456789.") != version_len)
619 return NULL;
620
621 if (!ISDIGIT (version_str[0]) || !ISDIGIT (version_str[version_len - 1]))
622 return NULL;
623
624 version_array[MAJOR] = strtoul (version_str, &end, 10);
625 version_str = end + ((*end == '.') ? 1 : 0);
626
627 /* Version string must not contain adjacent periods. */
628 if (*version_str == '.')
629 return NULL;
630
631 version_array[MINOR] = strtoul (version_str, &end, 10);
632 version_str = end + ((*end == '.') ? 1 : 0);
633
634 version_array[TINY] = strtoul (version_str, &end, 10);
635
636 /* Version string must contain no more than three tokens. */
637 if (*end != '\0')
638 return NULL;
639
640 return version_array;
641 }
642
643 /* Given VERSION -- a three-component OS X version represented as an array of
644 non-negative integers -- return a statically-allocated string suitable for
645 the legacy __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro. If VERSION
646 is invalid and cannot be coerced into a valid form, return NULL.
647
648 The legacy format is a four-character string -- two chars for the major
649 number and one each for the minor and tiny numbers. Minor and tiny numbers
650 from 10 through 99 are permitted but are clamped to 9 (for example, {10,9,10}
651 produces "1099"). If VERSION contains numbers greater than 99, it is
652 rejected. */
653
654 static const char *
655 version_as_legacy_macro (const unsigned long *version)
656 {
657 unsigned long major, minor, tiny;
658 static char result[5];
659
660 major = version[MAJOR];
661 minor = version[MINOR];
662 tiny = version[TINY];
663
664 if (major > 99 || minor > 99 || tiny > 99)
665 return NULL;
666
667 minor = ((minor > 9) ? 9 : minor);
668 tiny = ((tiny > 9) ? 9 : tiny);
669
670 if (sprintf (result, "%lu%lu%lu", major, minor, tiny) != 4)
671 return NULL;
672
673 return result;
674 }
675
676 /* Given VERSION -- a three-component OS X version represented as an array of
677 non-negative integers -- return a statically-allocated string suitable for
678 the modern __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro. If VERSION
679 is invalid, return NULL.
680
681 The modern format is a six-character string -- two chars for each component,
682 with zero-padding if necessary (for example, {10,10,1} produces "101001"). If
683 VERSION contains numbers greater than 99, it is rejected. */
684
685 static const char *
686 version_as_modern_macro (const unsigned long *version)
687 {
688 unsigned long major, minor, tiny;
689 static char result[7];
690
691 major = version[MAJOR];
692 minor = version[MINOR];
693 tiny = version[TINY];
694
695 if (major > 99 || minor > 99 || tiny > 99)
696 return NULL;
697
698 if (sprintf (result, "%02lu%02lu%02lu", major, minor, tiny) != 6)
699 return NULL;
700
701 return result;
702 }
703
704 /* Return the value of darwin_macosx_version_min, suitably formatted for the
705 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ macro. Values representing
706 OS X 10.9 and earlier are encoded using the legacy four-character format,
707 while 10.10 and later use a modern six-character format. (For example,
708 "10.9" produces "1090", and "10.10.1" produces "101001".) If
709 darwin_macosx_version_min is invalid and cannot be coerced into a valid
710 form, print a warning and return "1000". */
711
712 static const char *
713 macosx_version_as_macro (void)
714 {
715 const unsigned long *version_array;
716 const char *version_macro;
717
718 version_array = parse_version (darwin_macosx_version_min);
719 if (!version_array)
720 goto fail;
721
722 if (version_array[MAJOR] != 10)
723 goto fail;
724
725 if (version_array[MINOR] < 10)
726 version_macro = version_as_legacy_macro (version_array);
727 else
728 version_macro = version_as_modern_macro (version_array);
729
730 if (!version_macro)
731 goto fail;
732
733 return version_macro;
734
735 fail:
736 error ("unknown value %qs of -mmacosx-version-min",
737 darwin_macosx_version_min);
738 return "1000";
739 }
740
741 /* Define additional CPP flags for Darwin. */
742
743 #define builtin_define(TXT) cpp_define (pfile, TXT)
744
745 void
746 darwin_cpp_builtins (cpp_reader *pfile)
747 {
748 builtin_define ("__MACH__");
749 builtin_define ("__APPLE__");
750
751 /* __APPLE_CC__ is defined as some old Apple include files expect it
752 to be defined and won't work if it isn't. */
753 builtin_define_with_value ("__APPLE_CC__", "1", false);
754
755 if (darwin_constant_cfstrings)
756 builtin_define ("__CONSTANT_CFSTRINGS__");
757
758 builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__",
759 macosx_version_as_macro(), false);
760
761 /* Since we do not (at 4.6) support ObjC gc for the NeXT runtime, the
762 following will cause a syntax error if one tries to compile gc attributed
763 items. However, without this, NeXT system headers cannot be parsed
764 properly (on systems >= darwin 9). */
765 if (flag_objc_gc)
766 {
767 builtin_define ("__strong=__attribute__((objc_gc(strong)))");
768 builtin_define ("__weak=__attribute__((objc_gc(weak)))");
769 builtin_define ("__OBJC_GC__");
770 }
771 else
772 {
773 builtin_define ("__strong=");
774 builtin_define ("__weak=");
775 }
776
777 if (CPP_OPTION (pfile, objc) && flag_objc_abi == 2)
778 builtin_define ("__OBJC2__");
779 }
780
781 /* Handle C family front-end options. */
782
783 static bool
784 handle_c_option (size_t code,
785 const char *arg,
786 int value ATTRIBUTE_UNUSED)
787 {
788 switch (code)
789 {
790 default:
791 /* Unrecognized options that we said we'd handle turn into
792 errors if not listed here. */
793 return false;
794
795 case OPT_iframework:
796 add_system_framework_path (xstrdup (arg));
797 break;
798
799 case OPT_fapple_kext:
800 ;
801 }
802
803 /* We recognized the option. */
804 return true;
805 }
806
807 /* Allow ObjC* access to CFStrings. */
808 static tree
809 darwin_objc_construct_string (tree str)
810 {
811 if (!darwin_constant_cfstrings)
812 {
813 /* Even though we are not using CFStrings, place our literal
814 into the cfstring_htab hash table, so that the
815 darwin_constant_cfstring_p() function will see it. */
816 darwin_enter_string_into_cfstring_table (str);
817 /* Fall back to NSConstantString. */
818 return NULL_TREE;
819 }
820
821 return darwin_build_constant_cfstring (str);
822 }
823
824 /* The string ref type is created as CFStringRef by <CFBase.h> therefore, we
825 must match for it explicitly, since it's outside the gcc code. */
826
827 static bool
828 darwin_cfstring_ref_p (const_tree strp)
829 {
830 tree tn;
831 if (!strp || TREE_CODE (strp) != POINTER_TYPE)
832 return false;
833
834 tn = TYPE_NAME (strp);
835 if (tn)
836 tn = DECL_NAME (tn);
837 return (tn
838 && IDENTIFIER_POINTER (tn)
839 && !strncmp (IDENTIFIER_POINTER (tn), "CFStringRef", 8));
840 }
841
842 /* At present the behavior of this is undefined and it does nothing. */
843 static void
844 darwin_check_cfstring_format_arg (tree ARG_UNUSED (format_arg),
845 tree ARG_UNUSED (args_list))
846 {
847 }
848
849 /* The extra format types we recognize. */
850 EXPORTED_CONST format_kind_info darwin_additional_format_types[] = {
851 { "CFString", NULL, NULL, NULL, NULL,
852 NULL, NULL,
853 FMT_FLAG_ARG_CONVERT|FMT_FLAG_PARSE_ARG_CONVERT_EXTERNAL, 0, 0, 0, 0, 0, 0,
854 NULL, NULL
855 }
856 };
857
858
859 /* Support routines to dump the class references for NeXT ABI v1, aka
860 32-bits ObjC-2.0, as top-level asms.
861 The following two functions should only be called from
862 objc/objc-next-runtime-abi-01.c. */
863
864 static void
865 darwin_objc_declare_unresolved_class_reference (const char *name)
866 {
867 const char *lazy_reference = ".lazy_reference\t";
868 const char *hard_reference = ".reference\t";
869 const char *reference = MACHOPIC_INDIRECT ? lazy_reference : hard_reference;
870 size_t len = strlen (reference) + strlen(name) + 2;
871 char *buf = (char *) alloca (len);
872
873 gcc_checking_assert (!strncmp (name, ".objc_class_name_", 17));
874
875 snprintf (buf, len, "%s%s", reference, name);
876 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
877 }
878
879 static void
880 darwin_objc_declare_class_definition (const char *name)
881 {
882 const char *xname = targetm.strip_name_encoding (name);
883 size_t len = strlen (xname) + 7 + 5;
884 char *buf = (char *) alloca (len);
885
886 gcc_checking_assert (!strncmp (name, ".objc_class_name_", 17)
887 || !strncmp (name, "*.objc_category_name_", 21));
888
889 /* Mimic default_globalize_label. */
890 snprintf (buf, len, ".globl\t%s", xname);
891 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
892
893 snprintf (buf, len, "%s = 0", xname);
894 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
895 }
896
897 #undef TARGET_HANDLE_C_OPTION
898 #define TARGET_HANDLE_C_OPTION handle_c_option
899
900 #undef TARGET_OBJC_CONSTRUCT_STRING_OBJECT
901 #define TARGET_OBJC_CONSTRUCT_STRING_OBJECT darwin_objc_construct_string
902
903 #undef TARGET_OBJC_DECLARE_UNRESOLVED_CLASS_REFERENCE
904 #define TARGET_OBJC_DECLARE_UNRESOLVED_CLASS_REFERENCE \
905 darwin_objc_declare_unresolved_class_reference
906
907 #undef TARGET_OBJC_DECLARE_CLASS_DEFINITION
908 #define TARGET_OBJC_DECLARE_CLASS_DEFINITION \
909 darwin_objc_declare_class_definition
910
911 #undef TARGET_STRING_OBJECT_REF_TYPE_P
912 #define TARGET_STRING_OBJECT_REF_TYPE_P darwin_cfstring_ref_p
913
914 #undef TARGET_CHECK_STRING_OBJECT_FORMAT_ARG
915 #define TARGET_CHECK_STRING_OBJECT_FORMAT_ARG darwin_check_cfstring_format_arg
916
917 struct gcc_targetcm targetcm = TARGETCM_INITIALIZER;