Debian Codegraph @

gcc/m2/gm2-lang.cc

raw
1 /* gm2-lang.cc language-dependent hooks for GNU Modula-2.
2
3 Copyright (C) 2002-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #define INCLUDE_VECTOR
23 #include "gm2-gcc/gcc-consolidation.h"
24
25 #include "langhooks-def.h" /* FIXME: for lhd_set_decl_assembler_name. */
26 #include "tree-pass.h" /* FIXME: only for PROP_gimple_any. */
27 #include "toplev.h"
28 #include "debug.h"
29
30 #include "opts.h"
31
32 #define GM2_LANG_C
33 #include "gm2-lang.h"
34 #include "m2block.h"
35 #include "dynamicstrings.h"
36 #include "m2options.h"
37 #include "m2convert.h"
38 #include "m2linemap.h"
39 #include "init.h"
40 #include "m2-tree.h"
41 #include "convert.h"
42 #include "rtegraph.h"
43
44 static void write_globals (void);
45
46 static int insideCppArgs = FALSE;
47
48 /* We default to pim in the absence of fiso. */
49 static bool iso = false;
50
51 typedef struct named_path_s {
52 std::vector<const char*>path;
53 const char *name;
54 } named_path;
55
56
57 /* The language include paths are based on the libraries in use. */
58 static bool allow_libraries = true;
59 static const char *flibs = nullptr;
60 static const char *iprefix = nullptr;
61 static const char *imultilib = nullptr;
62 static std::vector<named_path>Ipaths;
63 static std::vector<const char*>isystem;
64 static std::vector<const char*>iquote;
65
66 #define EXPR_STMT_EXPR(NODE) TREE_OPERAND (EXPR_STMT_CHECK (NODE), 0)
67
68 /* start of new stuff. */
69
70 /* Language-dependent contents of a type. */
71
72 struct GTY (()) lang_type
73 {
74 char dummy;
75 };
76
77 /* Language-dependent contents of a decl. */
78
79 struct GTY (()) lang_decl
80 {
81 char dummy;
82 };
83
84 /* Language-dependent contents of an identifier. This must include a
85 tree_identifier. */
86
87 struct GTY (()) lang_identifier
88 {
89 struct tree_identifier common;
90 };
91
92 /* The resulting tree type. */
93
94 union GTY ((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
95 chain_next ("CODE_CONTAINS_STRUCT (TREE_CODE (&%h.generic), "
96 "TS_COMMON) ? ((union lang_tree_node *) TREE_CHAIN "
97 "(&%h.generic)) : NULL"))) lang_tree_node
98 {
99 union tree_node GTY ((tag ("0"),
100 desc ("tree_node_structure (&%h)"))) generic;
101 struct lang_identifier GTY ((tag ("1"))) identifier;
102 };
103
104 struct GTY (()) language_function
105 {
106
107 /* While we are parsing the function, this contains information about
108 the statement-tree that we are building. */
109 /* struct stmt_tree_s stmt_tree; */
110 tree stmt_tree;
111 };
112
113 /* Language hooks. */
114
115 static void gm2_langhook_parse_file (void);
116
117 bool
118 gm2_langhook_init (void)
119 {
120 build_common_tree_nodes (false);
121 build_common_builtin_nodes ();
122
123 /* The default precision for floating point numbers. This is used
124 for floating point constants with abstract type. This may eventually
125 be controllable by a command line option. */
126 mpfr_set_default_prec (256);
127
128 /* GNU Modula-2 uses exceptions. */
129 using_eh_for_cleanups ();
130
131 if (M2Options_GetPPOnly ())
132 {
133 /* Preprocess the file here. */
134 gm2_langhook_parse_file ();
135 return false; /* Finish now, no further compilation. */
136 }
137 return true;
138 }
139
140 /* The option mask. */
141
142 static unsigned int
143 gm2_langhook_option_lang_mask (void)
144 {
145 return CL_ModulaX2;
146 }
147
148 /* Initialize the options structure. */
149
150 static void
151 gm2_langhook_init_options_struct (struct gcc_options *opts)
152 {
153 /* Default to avoiding range issues for complex multiply and divide. */
154 opts->x_flag_complex_method = 2;
155
156 /* The builtin math functions should not set errno. */
157 opts->x_flag_errno_math = 0;
158 opts->frontend_set_flag_errno_math = true;
159
160 /* Exceptions are used. */
161 opts->x_flag_exceptions = 1;
162 init_FrontEndInit ();
163 }
164
165 /* Infrastructure for a VEC of bool values. */
166
167 /* This array determines whether the filename is associated with the
168 C preprocessor. */
169
170 static vec<bool> filename_cpp;
171
172 /* Build the C preprocessor command line here, since we need to include
173 options that are not passed to the handle_option function. */
174
175 void
176 gm2_langhook_init_options (unsigned int decoded_options_count,
177 struct cl_decoded_option *decoded_options)
178 {
179 unsigned int i;
180 bool in_cpp_args = false;
181 bool building_cpp_command = false;
182
183 for (i = 1; i < decoded_options_count; i++)
184 {
185 enum opt_code code = (enum opt_code)decoded_options[i].opt_index;
186 const struct cl_option *option = &cl_options[code];
187 const char *opt = (const char *)option->opt_text;
188 const char *arg = decoded_options[i].arg;
189 HOST_WIDE_INT value = decoded_options[i].value;
190 switch (code)
191 {
192 case OPT_fcpp:
193 if (value)
194 gcc_checking_assert (building_cpp_command);
195 break;
196 case OPT_fcpp_begin:
197 in_cpp_args = true;
198 building_cpp_command = true;
199 break;
200 case OPT_fcpp_end:
201 in_cpp_args = false;
202 break;
203 case OPT_SPECIAL_input_file:
204 filename_cpp.safe_push (in_cpp_args);
205 break;
206
207 /* C and driver opts that are not passed to the preprocessor for
208 modula-2, but that we use internally for building preprocesor
209 command lines. */
210 case OPT_B:
211 M2Options_SetB (arg);
212 break;
213 case OPT_c:
214 M2Options_Setc (value);
215 break;
216 case OPT_dumpdir:
217 M2Options_SetDumpDir (arg);
218 break;
219 case OPT_save_temps:
220 if (building_cpp_command)
221 M2Options_SetSaveTemps (value);
222 break;
223 case OPT_save_temps_:
224 if (building_cpp_command)
225 /* Also sets SaveTemps. */
226 M2Options_SetSaveTempsDir (arg);
227 break;
228
229 case OPT_E:
230 if (!in_cpp_args)
231 {
232 M2Options_SetPPOnly (value);
233 building_cpp_command = true;
234 }
235 M2Options_CppArg (opt, arg, (option->flags & CL_JOINED)
236 && !(option->flags & CL_SEPARATE));
237 break;
238
239 case OPT_M:
240 /* Output a rule suitable for make describing the dependencies of the
241 main source file. */
242 if (in_cpp_args)
243 {
244 gcc_checking_assert (building_cpp_command);
245 /* This is a preprocessor command. */
246 M2Options_CppArg (opt, arg, (option->flags & CL_JOINED)
247 && !(option->flags & CL_SEPARATE));
248 }
249 M2Options_SetPPOnly (value);
250 M2Options_SetM (value);
251 break;
252
253 case OPT_MM:
254 if (in_cpp_args)
255 {
256 gcc_checking_assert (building_cpp_command);
257 /* This is a preprocessor command. */
258 M2Options_CppArg (opt, arg, (option->flags & CL_JOINED)
259 && !(option->flags & CL_SEPARATE));
260 }
261 M2Options_SetPPOnly (value);
262 M2Options_SetMM (value);
263 break;
264
265 case OPT_MF:
266 if (!in_cpp_args)
267 M2Options_SetMF (arg);
268 break;
269
270 case OPT_MP:
271 M2Options_SetMP (value);
272 break;
273
274 /* We can only use MQ and MT when the command line is either PP-only, or
275 when there is a MD/MMD on it. */
276 case OPT_MQ:
277 M2Options_SetMQ (arg);
278 break;
279
280 case OPT_MT:
281 M2Options_SetMT (arg);
282 break;
283
284 case OPT_o:
285 M2Options_SetObj (arg);
286 break;
287
288 /* C and driver options that we ignore for the preprocessor lines. */
289 case OPT_fpch_deps:
290 case OPT_fpch_preprocess:
291 break;
292
293 case OPT_fplugin_:
294 /* FIXME: We might need to handle this specially, since the modula-2
295 plugin is not usable here, but others might be.
296 For now skip all plugins to avoid fails with the m2 one. */
297 break;
298
299 /* Preprocessor arguments with a following filename. */
300 case OPT_MD:
301 M2Options_SetMD (value);
302 if (value)
303 {
304 M2Options_SetM (true);
305 M2Options_SetMF (arg);
306 }
307 break;
308
309 case OPT_MMD:
310 M2Options_SetMMD (value);
311 if (value)
312 {
313 M2Options_SetMM (true);
314 M2Options_SetMF (arg);
315 }
316 break;
317
318 /* Modula 2 claimed options we pass to the preprocessor. */
319 case OPT_ansi:
320 case OPT_traditional_cpp:
321 if (building_cpp_command)
322 M2Options_CppArg (opt, arg, (option->flags & CL_JOINED)
323 && !(option->flags & CL_SEPARATE));
324 break;
325
326 /* Options we act on and also pass to the preprocessor. */
327 case OPT_O:
328 M2Options_SetOptimizing (value);
329 if (building_cpp_command)
330 M2Options_CppArg (opt, arg, (option->flags & CL_JOINED)
331 && !(option->flags & CL_SEPARATE));
332 break;
333 case OPT_quiet:
334 M2Options_SetQuiet (value);
335 if (building_cpp_command)
336 M2Options_CppArg (opt, arg, (option->flags & CL_JOINED)
337 && !(option->flags & CL_SEPARATE));
338 break;
339 case OPT_v:
340 M2Options_SetVerbose (value);
341 /* FALLTHROUGH */
342 default:
343 /* We handled input files above. */
344 if (code >= N_OPTS)
345 break;
346 /* Do not pass Modula-2 args to the preprocessor, any that we care
347 about here should already have been handled above. */
348 if (option->flags & CL_ModulaX2)
349 break;
350 /* Otherwise, add this to the CPP command line. */
351 if (building_cpp_command)
352 M2Options_CppArg (opt, arg, (option->flags & CL_JOINED)
353 && !(option->flags & CL_SEPARATE));
354 break;
355 }
356 }
357 filename_cpp.safe_push (false);
358 }
359
360 static bool
361 is_cpp_filename (unsigned int i)
362 {
363 gcc_assert (i < filename_cpp.length ());
364 return filename_cpp[i];
365 }
366
367 static void
368 push_back_Ipath (const char *arg)
369 {
370 if (Ipaths.empty ())
371 {
372 named_path np;
373 np.path.push_back (arg);
374 np.name = xstrdup (M2Options_GetM2PathName ());
375 Ipaths.push_back (np);
376 }
377 else
378 {
379 if (strcmp (Ipaths.back ().name,
380 M2Options_GetM2PathName ()) == 0)
381 Ipaths.back ().path.push_back (arg);
382 else
383 {
384 named_path np;
385 np.path.push_back (arg);
386 np.name = xstrdup (M2Options_GetM2PathName ());
387 Ipaths.push_back (np);
388 }
389 }
390 }
391
392 /* Handle gm2 specific options. Return 0 if we didn't do anything. */
393
394 bool
395 gm2_langhook_handle_option (
396 size_t scode, const char *arg, HOST_WIDE_INT value, int kind ATTRIBUTE_UNUSED,
397 location_t loc ATTRIBUTE_UNUSED,
398 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
399 {
400 enum opt_code code = (enum opt_code)scode;
401
402 const struct cl_option *option = &cl_options[scode];
403 /* ignore file names. */
404 if (code == N_OPTS)
405 return 1;
406
407 switch (code)
408 {
409 case OPT_dumpdir:
410 M2Options_SetDumpDir (arg);
411 return 1;
412 case OPT_I:
413 push_back_Ipath (arg);
414 return 1;
415 case OPT_fiso:
416 M2Options_SetISO (value);
417 iso = value;
418 return 1;
419 case OPT_fpim:
420 M2Options_SetPIM (value);
421 iso = value ? false : iso;
422 return 1;
423 case OPT_fpim2:
424 M2Options_SetPIM2 (value);
425 iso = value ? false : iso;
426 return 1;
427 case OPT_fpim3:
428 M2Options_SetPIM3 (value);
429 iso = value ? false : iso;
430 return 1;
431 case OPT_fpim4:
432 M2Options_SetPIM4 (value);
433 iso = value ? false : iso;
434 return 1;
435 case OPT_fpositive_mod_floor_div:
436 M2Options_SetPositiveModFloor (value);
437 return 1;
438 case OPT_flibs_:
439 allow_libraries = value;
440 flibs = arg;
441 return 1;
442 case OPT_fgen_module_list_:
443 M2Options_SetGenModuleList (value, arg);
444 return 1;
445 case OPT_fnil:
446 M2Options_SetNilCheck (value);
447 return 1;
448 case OPT_fwholediv:
449 M2Options_SetWholeDiv (value);
450 return 1;
451 case OPT_findex:
452 M2Options_SetIndex (value);
453 return 1;
454 case OPT_frange:
455 M2Options_SetRange (value);
456 return 1;
457 case OPT_ffloatvalue:
458 M2Options_SetFloatValueCheck (value);
459 return 1;
460 case OPT_fwholevalue:
461 M2Options_SetWholeValueCheck (value);
462 return 1;
463 case OPT_freturn:
464 M2Options_SetReturnCheck (value);
465 return 1;
466 case OPT_fcase:
467 M2Options_SetCaseCheck (value);
468 return 1;
469 case OPT_fd:
470 M2Options_SetCompilerDebugging (value);
471 return 1;
472 case OPT_fdebug_builtins:
473 M2Options_SetDebugBuiltins (value);
474 return 1;
475 case OPT_fdebug_function_line_numbers:
476 M2Options_SetDebugFunctionLineNumbers (value);
477 return 1;
478 case OPT_fauto_init:
479 M2Options_SetAutoInit (value);
480 return 1;
481 case OPT_fsoft_check_all:
482 M2Options_SetCheckAll (value);
483 return 1;
484 case OPT_fexceptions:
485 M2Options_SetExceptions (value);
486 return 1;
487 case OPT_Wstyle:
488 M2Options_SetStyle (value);
489 return 1;
490 case OPT_Wpedantic:
491 M2Options_SetPedantic (value);
492 return 1;
493 case OPT_Wpedantic_param_names:
494 M2Options_SetPedanticParamNames (value);
495 return 1;
496 case OPT_Wpedantic_cast:
497 M2Options_SetPedanticCast (value);
498 return 1;
499 case OPT_fextended_opaque:
500 M2Options_SetExtendedOpaque (value);
501 return 1;
502 case OPT_Wverbose_unbounded:
503 M2Options_SetVerboseUnbounded (value);
504 return 1;
505 case OPT_Wunused_variable:
506 M2Options_SetUnusedVariableChecking (value);
507 return 1;
508 case OPT_Wunused_parameter:
509 M2Options_SetUnusedParameterChecking (value);
510 return 1;
511 case OPT_Wuninit_variable_checking:
512 return M2Options_SetUninitVariableChecking (value, "known");
513 case OPT_Wuninit_variable_checking_:
514 return M2Options_SetUninitVariableChecking (value, arg);
515 case OPT_fm2_file_offset_bits_:
516 {
517 if (arg != NULL)
518 {
519 unsigned int bits = atoi (arg);
520 if (bits > 0)
521 return M2Options_SetFileOffsetBits (value, bits);
522 }
523 return 0;
524 }
525 case OPT_fm2_strict_type:
526 M2Options_SetStrictTypeChecking (value);
527 return 1;
528 case OPT_fm2_debug_trace_:
529 M2Options_SetM2DebugTraceFilter (value, arg);
530 return 1;
531 case OPT_fm2_dump_:
532 return M2Options_SetM2Dump (value, arg);
533 case OPT_fm2_dump_decl_:
534 M2Options_SetDumpDeclFilename (value, arg);
535 return 1;
536 case OPT_fm2_dump_gimple_:
537 M2Options_SetDumpGimpleFilename (value, arg);
538 return 1;
539 case OPT_fm2_dump_quad_:
540 M2Options_SetDumpQuadFilename (value, arg);
541 return 1;
542 case OPT_fm2_dump_filter_:
543 M2Options_SetM2DumpFilter (value, arg);
544 return 1;
545 case OPT_Wall:
546 M2Options_SetWall (value);
547 return 1;
548 case OPT_Wcase_enum:
549 M2Options_SetCaseEnumChecking (value);
550 return 1;
551 #if 0
552 /* Not yet implemented. */
553 case OPT_fxcode:
554 M2Options_SetXCode (value);
555 return 1;
556 #endif
557 case OPT_fm2_lower_case:
558 M2Options_SetLowerCaseKeywords (value);
559 return 1;
560 case OPT_fuse_list_:
561 M2Options_SetUselist (value, arg);
562 return 1;
563 case OPT_fruntime_modules_:
564 M2Options_SetRuntimeModuleOverride (arg);
565 return 1;
566 case OPT_fpthread:
567 /* Handled in the driver. */
568 return 1;
569 case OPT_fm2_plugin:
570 /* Handled in the driver. */
571 return 1;
572 case OPT_fscaffold_dynamic:
573 M2Options_SetScaffoldDynamic (value);
574 return 1;
575 case OPT_fscaffold_static:
576 M2Options_SetScaffoldStatic (value);
577 return 1;
578 case OPT_fscaffold_main:
579 M2Options_SetScaffoldMain (value);
580 return 1;
581 case OPT_fcpp:
582 M2Options_SetCpp (value);
583 return 1;
584 case OPT_fpreprocessed:
585 /* Provided for compatibility; ignore for now. */
586 return 1;
587 case OPT_fcpp_begin:
588 insideCppArgs = TRUE;
589 return 1;
590 case OPT_fcpp_end:
591 insideCppArgs = FALSE;
592 return 1;
593 case OPT_fq:
594 M2Options_SetQuadDebugging (value);
595 return 1;
596 case OPT_fsources:
597 M2Options_SetSources (value);
598 return 1;
599 case OPT_funbounded_by_reference:
600 M2Options_SetUnboundedByReference (value);
601 return 1;
602 case OPT_fdef_:
603 M2Options_setdefextension (arg);
604 return 1;
605 case OPT_fmod_:
606 M2Options_setmodextension (arg);
607 return 1;
608 case OPT_fdump_system_exports:
609 M2Options_SetDumpSystemExports (value);
610 return 1;
611 case OPT_fswig:
612 M2Options_SetSwig (value);
613 return 1;
614 case OPT_fshared:
615 M2Options_SetShared (value);
616 return 1;
617 case OPT_fm2_statistics:
618 M2Options_SetStatistics (value);
619 return 1;
620 case OPT_fm2_g:
621 M2Options_SetM2g (value);
622 return 1;
623 break;
624 case OPT_fm2_pathname_:
625 if (strcmp (arg, "-") == 0)
626 M2Options_SetM2PathName ("");
627 else
628 M2Options_SetM2PathName (arg);
629 return 1;
630 break;
631 case OPT_fm2_pathnameI:
632 push_back_Ipath (arg);
633 return 1;
634 break;
635 case OPT_fm2_prefix_:
636 if (strcmp (arg, "-") == 0)
637 M2Options_SetM2Prefix ("");
638 else
639 M2Options_SetM2Prefix (arg);
640 return 1;
641 break;
642 case OPT_iprefix:
643 iprefix = arg;
644 return 1;
645 break;
646 case OPT_imultilib:
647 imultilib = arg;
648 return 1;
649 break;
650 case OPT_isystem:
651 isystem.push_back (arg);
652 return 1;
653 break;
654 case OPT_iquote:
655 iquote.push_back (arg);
656 return 1;
657 break;
658 case OPT_isysroot:
659 /* Otherwise, ignored, at least for now. */
660 return 1;
661 break;
662 case OPT_fm2_whole_program:
663 M2Options_SetWholeProgram (value);
664 return 1;
665 #ifdef OPT_mabi_ibmlongdouble
666 case OPT_mabi_ibmlongdouble:
667 M2Options_SetIBMLongDouble (value);
668 return 1;
669 #endif
670 #ifdef OPT_mabi_ieeelongdouble
671 case OPT_mabi_ieeelongdouble:
672 M2Options_SetIEEELongDouble (value);
673 return 1;
674 #endif
675 case OPT_flocation_:
676 if (strcmp (arg, "builtins") == 0)
677 {
678 M2Options_SetForcedLocation (BUILTINS_LOCATION);
679 return 1;
680 }
681 else if (strcmp (arg, "unknown") == 0)
682 {
683 M2Options_SetForcedLocation (UNKNOWN_LOCATION);
684 return 1;
685 }
686 else if ((arg != NULL) && (ISDIGIT (arg[0])))
687 {
688 M2Options_SetForcedLocation (atoi (arg));
689 return 1;
690 }
691 else
692 return 0;
693 default:
694 if (insideCppArgs)
695 /* Handled in gm2_langhook_init_options (). */
696 return 1;
697 else if (option->flags & CL_DRIVER)
698 /* Driver options (unless specifically claimed above) should be handled
699 in gm2_langhook_init_options (). */
700 return 1;
701 else if (option->flags & CL_C)
702 /* C options (unless specifically claimed above) should be handled
703 in gm2_langhook_init_options (). */
704 return 1;
705 break;
706 }
707 return 0;
708 }
709
710 /* This prefixes LIBNAME with the current compiler prefix (if it has been
711 relocated) or the LIBSUBDIR, if not. */
712 static void
713 add_one_import_path (const char *libname)
714 {
715 const char *libpath = iprefix ? iprefix : LIBSUBDIR;
716 const char dir_sep[] = {DIR_SEPARATOR, (char)0};
717 size_t dir_sep_size = strlen (dir_sep);
718 unsigned int mlib_len = 0;
719
720 if (imultilib)
721 {
722 mlib_len = strlen (imultilib);
723 mlib_len += strlen (dir_sep);
724 }
725
726 char *lib = (char *)alloca (strlen (libpath) + dir_sep_size
727 + strlen ("m2") + dir_sep_size
728 + strlen (libname) + 1
729 + mlib_len + 1);
730 strcpy (lib, libpath);
731 /* iprefix has a trailing dir separator, LIBSUBDIR does not. */
732 if (!iprefix)
733 strcat (lib, dir_sep);
734
735 if (imultilib)
736 {
737 strcat (lib, imultilib);
738 strcat (lib, dir_sep);
739 }
740 strcat (lib, "m2");
741 strcat (lib, dir_sep);
742 strcat (lib, libname);
743 M2Options_SetM2PathName (libname);
744 M2Options_SetSearchPath (lib);
745 }
746
747 /* For each comma-separated standard library name in LIBLIST, add the
748 corresponding include path. */
749 static void
750 add_m2_import_paths (const char *liblist)
751 {
752 while (*liblist != 0 && *liblist != '-')
753 {
754 const char *comma = strstr (liblist, ",");
755 size_t len;
756 if (comma)
757 len = comma - liblist;
758 else
759 len = strlen (liblist);
760 char *libname = (char *) alloca (len+1);
761 strncpy (libname, liblist, len);
762 libname[len] = 0;
763 add_one_import_path (libname);
764 liblist += len;
765 if (*liblist == ',')
766 liblist++;
767 }
768 }
769
770 /* Run after parsing options. */
771
772 static bool
773 gm2_langhook_post_options (const char **pfilename)
774 {
775 const char *filename = *pfilename;
776 flag_excess_precision = EXCESS_PRECISION_FAST;
777 M2Options_SetCC1Quiet (quiet_flag);
778 M2Options_FinaliseOptions ();
779 main_input_filename = filename;
780
781 /* Add the include paths as per the libraries specified.
782 NOTE: This assumes that the driver has validated the input and makes
783 no attempt to be defensive of nonsense input in flibs=. */
784 if (allow_libraries)
785 {
786 if (!flibs)
787 {
788 if (iso)
789 flibs = "m2iso,m2cor,m2pim,m2log";
790 else
791 flibs = "m2pim,m2iso,m2cor,m2log";
792 }
793 }
794
795 /* Add search paths.
796 We are not handling all of the cases yet (e.g idirafter).
797 This (barring the missing cases) is intended to follow the directory
798 search rules used for c-family. It would be less confusing if the
799 presence of absence of these search paths was not dependent on the
800 flibs= option. */
801
802 for (auto *s : iquote)
803 M2Options_SetSearchPath (s);
804 iquote.clear();
805 for (auto np : Ipaths)
806 {
807 M2Options_SetM2PathName (np.name);
808 for (auto *s : np.path)
809 M2Options_SetSearchPath (s);
810 }
811 Ipaths.clear();
812 for (auto *s : isystem)
813 M2Options_SetSearchPath (s);
814 isystem.clear();
815 /* FIXME: this is not a good way to suppress the addition of the import
816 paths. */
817 if (allow_libraries)
818 add_m2_import_paths (flibs);
819
820 /* Returning false means that the backend should be used. */
821 return M2Options_GetPPOnly ();
822 }
823
824 /* Call the compiler for every source filename on the command line. */
825
826 static void
827 gm2_parse_input_files (const char **filenames, unsigned int filename_count)
828 {
829 unsigned int i;
830 gcc_assert (filename_count > 0);
831
832 for (i = 0; i < filename_count; i++)
833 if (!is_cpp_filename (i))
834 {
835 main_input_filename = filenames[i];
836 init_PerCompilationInit (filenames[i]);
837 }
838 }
839
840 static void
841 gm2_langhook_parse_file (void)
842 {
843 gm2_parse_input_files (in_fnames, num_in_fnames);
844 if (!M2Options_GetPPOnly ())
845 write_globals ();
846 }
847
848 static tree
849 gm2_langhook_type_for_size (unsigned int bits, int unsignedp)
850 {
851 return gm2_type_for_size (bits, unsignedp);
852 }
853
854 static tree
855 gm2_langhook_type_for_mode (machine_mode mode, int unsignedp)
856 {
857 tree type;
858
859 for (int i = 0; i < NUM_INT_N_ENTS; i ++)
860 if (int_n_enabled_p[i]
861 && mode == int_n_data[i].m)
862 return (unsignedp ? int_n_trees[i].unsigned_type
863 : int_n_trees[i].signed_type);
864
865 if (VECTOR_MODE_P (mode))
866 {
867 tree inner;
868
869 inner = gm2_langhook_type_for_mode (GET_MODE_INNER (mode), unsignedp);
870 if (inner != NULL_TREE)
871 return build_vector_type_for_mode (inner, mode);
872 return NULL_TREE;
873 }
874
875 scalar_int_mode imode;
876 if (is_int_mode (mode, &imode))
877 return gm2_langhook_type_for_size (GET_MODE_BITSIZE (imode), unsignedp);
878
879 if (mode == TYPE_MODE (float_type_node))
880 return float_type_node;
881
882 if (mode == TYPE_MODE (double_type_node))
883 return double_type_node;
884
885 if (mode == TYPE_MODE (long_double_type_node))
886 return long_double_type_node;
887
888 if ((float128_type_node != NULL) && (mode == TYPE_MODE (float128_type_node)))
889 return float128_type_node;
890
891 if (COMPLEX_MODE_P (mode))
892 {
893 machine_mode inner_mode;
894 tree inner_type;
895
896 if (mode == TYPE_MODE (complex_float_type_node))
897 return complex_float_type_node;
898 if (mode == TYPE_MODE (complex_double_type_node))
899 return complex_double_type_node;
900 if (mode == TYPE_MODE (complex_long_double_type_node))
901 return complex_long_double_type_node;
902
903 inner_mode = GET_MODE_INNER (mode);
904 inner_type = gm2_langhook_type_for_mode (inner_mode, unsignedp);
905 if (inner_type != NULL_TREE)
906 return build_complex_type (inner_type);
907 }
908
909 #if HOST_BITS_PER_WIDE_INT >= 64
910 /* The middle-end and some backends rely on TImode being supported
911 for 64-bit HWI. */
912 if (mode == TImode)
913 {
914 type = build_nonstandard_integer_type (GET_MODE_BITSIZE (TImode),
915 unsignedp);
916 if (type && TYPE_MODE (type) == TImode)
917 return type;
918 }
919 #endif
920 return NULL_TREE;
921 }
922
923 /* Record a builtin function. We just ignore builtin functions. */
924
925 static tree
926 gm2_langhook_builtin_function (tree decl)
927 {
928 return decl;
929 }
930
931 /* Return true if we are in the global binding level. */
932
933 static bool
934 gm2_langhook_global_bindings_p (void)
935 {
936 return current_function_decl == NULL_TREE;
937 }
938
939 /* Unused langhook. */
940
941 static tree
942 gm2_langhook_pushdecl (tree decl ATTRIBUTE_UNUSED)
943 {
944 gcc_unreachable ();
945 }
946
947 /* This hook is used to get the current list of declarations as trees.
948 We don't support that; instead we use write_globals. This can't
949 simply crash because it is called by -gstabs. */
950
951 static tree
952 gm2_langhook_getdecls (void)
953 {
954 return NULL;
955 }
956
957 /* m2_write_global_declarations writes out globals creating an array
958 of the declarations and calling wrapup_global_declarations. */
959
960 static void
961 m2_write_global_declarations (tree globals)
962 {
963 auto_vec<tree> global_decls;
964 tree decl = globals;
965 int n = 0;
966
967 while (decl != NULL)
968 {
969 global_decls.safe_push (decl);
970 decl = TREE_CHAIN (decl);
971 n++;
972 }
973 wrapup_global_declarations (global_decls.address (), n);
974 }
975
976 /* Write out globals. */
977
978 static void
979 write_globals (void)
980 {
981 tree t;
982 unsigned i;
983
984 m2block_finishGlobals ();
985
986 /* Process all file scopes in this compilation, and the
987 external_scope, through wrapup_global_declarations and
988 check_global_declarations. */
989 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
990 m2_write_global_declarations (BLOCK_VARS (DECL_INITIAL (t)));
991 }
992
993
994 /* Gimplify an EXPR_STMT node. */
995
996 static void
997 gimplify_expr_stmt (tree *stmt_p)
998 {
999 gcc_assert (EXPR_STMT_EXPR (*stmt_p) != NULL_TREE);
1000 *stmt_p = EXPR_STMT_EXPR (*stmt_p);
1001 }
1002
1003 /* Genericize a TRY_BLOCK. */
1004
1005 static void
1006 genericize_try_block (tree *stmt_p)
1007 {
1008 tree body = TRY_STMTS (*stmt_p);
1009 tree cleanup = TRY_HANDLERS (*stmt_p);
1010
1011 *stmt_p = build2 (TRY_CATCH_EXPR, void_type_node, body, cleanup);
1012 }
1013
1014 /* Genericize a HANDLER by converting to a CATCH_EXPR. */
1015
1016 static void
1017 genericize_catch_block (tree *stmt_p)
1018 {
1019 tree type = HANDLER_TYPE (*stmt_p);
1020 tree body = HANDLER_BODY (*stmt_p);
1021
1022 /* FIXME should the caught type go in TREE_TYPE? */
1023 *stmt_p = build2 (CATCH_EXPR, void_type_node, type, body);
1024 }
1025
1026 /* Convert the tree representation of FNDECL from m2 frontend trees
1027 to GENERIC. */
1028
1029 extern void pf (tree);
1030
1031 void
1032 gm2_genericize (tree fndecl)
1033 {
1034 tree t;
1035 struct cgraph_node *cgn;
1036
1037 #if 0
1038 pf (fndecl);
1039 #endif
1040 /* Fix up the types of parms passed by invisible reference. */
1041 for (t = DECL_ARGUMENTS (fndecl); t; t = DECL_CHAIN (t))
1042 if (TREE_ADDRESSABLE (TREE_TYPE (t)))
1043 {
1044
1045 /* If a function's arguments are copied to create a thunk, then
1046 DECL_BY_REFERENCE will be set -- but the type of the argument will be
1047 a pointer type, so we will never get here. */
1048 gcc_assert (!DECL_BY_REFERENCE (t));
1049 gcc_assert (DECL_ARG_TYPE (t) != TREE_TYPE (t));
1050 TREE_TYPE (t) = DECL_ARG_TYPE (t);
1051 DECL_BY_REFERENCE (t) = 1;
1052 TREE_ADDRESSABLE (t) = 0;
1053 relayout_decl (t);
1054 }
1055
1056 /* Dump all nested functions now. */
1057 cgn = cgraph_node::get_create (fndecl);
1058 for (cgn = first_nested_function (cgn);
1059 cgn != NULL; cgn = next_nested_function (cgn))
1060 gm2_genericize (cgn->decl);
1061 }
1062
1063 /* gm2 gimplify expression, currently just change THROW in the same
1064 way as C++ */
1065
1066 static int
1067 gm2_langhook_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED,
1068 gimple_seq *post_p ATTRIBUTE_UNUSED)
1069 {
1070 enum tree_code code = TREE_CODE (*expr_p);
1071
1072 switch (code)
1073 {
1074 case THROW_EXPR:
1075
1076 /* FIXME communicate throw type to back end, probably by moving
1077 THROW_EXPR into ../tree.def. */
1078 *expr_p = TREE_OPERAND (*expr_p, 0);
1079 return GS_OK;
1080
1081 case EXPR_STMT:
1082 gimplify_expr_stmt (expr_p);
1083 return GS_OK;
1084
1085 case TRY_BLOCK:
1086 genericize_try_block (expr_p);
1087 return GS_OK;
1088
1089 case HANDLER:
1090 genericize_catch_block (expr_p);
1091 return GS_OK;
1092
1093 default:
1094 return GS_UNHANDLED;
1095 }
1096 }
1097
1098 static GTY(()) tree gm2_eh_personality_decl;
1099
1100 static tree
1101 gm2_langhook_eh_personality (void)
1102 {
1103 if (!gm2_eh_personality_decl)
1104 gm2_eh_personality_decl = build_personality_function ("gxx");
1105
1106 return gm2_eh_personality_decl;
1107 }
1108
1109 /* Functions called directly by the generic backend. */
1110
1111 tree
1112 convert_loc (location_t location, tree type, tree expr)
1113 {
1114 if (type == error_mark_node || expr == error_mark_node
1115 || TREE_TYPE (expr) == error_mark_node)
1116 return error_mark_node;
1117
1118 if (type == TREE_TYPE (expr))
1119 return expr;
1120
1121 gcc_assert (TYPE_MAIN_VARIANT (type) != NULL);
1122 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
1123 return fold_convert (type, expr);
1124
1125 expr = m2convert_GenericToType (location, type, expr);
1126 switch (TREE_CODE (type))
1127 {
1128 case VOID_TYPE:
1129 case BOOLEAN_TYPE:
1130 return fold (convert_to_integer (type, expr));
1131 case INTEGER_TYPE:
1132 return fold (convert_to_integer (type, expr));
1133 case POINTER_TYPE:
1134 return fold (convert_to_pointer (type, expr));
1135 case REAL_TYPE:
1136 return fold (convert_to_real (type, expr));
1137 case COMPLEX_TYPE:
1138 return fold (convert_to_complex (type, expr));
1139 case ENUMERAL_TYPE:
1140 return fold (convert_to_integer (type, expr));
1141 default:
1142 error_at (location, "cannot convert expression, only base types can be converted");
1143 break;
1144 }
1145 return error_mark_node;
1146 }
1147
1148 /* Functions called directly by the generic backend. */
1149
1150 tree
1151 convert (tree type, tree expr)
1152 {
1153 return convert_loc (m2linemap_UnknownLocation (), type, expr);
1154 }
1155
1156 /* Mark EXP saying that we need to be able to take the address of it;
1157 it should not be allocated in a register. Returns true if
1158 successful. */
1159
1160 bool
1161 gm2_mark_addressable (tree exp)
1162 {
1163 tree x = exp;
1164
1165 while (TRUE)
1166 switch (TREE_CODE (x))
1167 {
1168 case COMPONENT_REF:
1169 if (DECL_PACKED (TREE_OPERAND (x, 1)))
1170 return false;
1171 x = TREE_OPERAND (x, 0);
1172 break;
1173
1174 case ADDR_EXPR:
1175 case ARRAY_REF:
1176 case REALPART_EXPR:
1177 case IMAGPART_EXPR:
1178 x = TREE_OPERAND (x, 0);
1179 break;
1180
1181 case COMPOUND_LITERAL_EXPR:
1182 case CONSTRUCTOR:
1183 case STRING_CST:
1184 case VAR_DECL:
1185 case CONST_DECL:
1186 case PARM_DECL:
1187 case RESULT_DECL:
1188 case FUNCTION_DECL:
1189 TREE_ADDRESSABLE (x) = 1;
1190 return true;
1191 default:
1192 return true;
1193 }
1194 /* Never reach here. */
1195 gcc_unreachable ();
1196 }
1197
1198 /* Return an integer type with BITS bits of precision, that is
1199 unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1200
1201 tree
1202 gm2_type_for_size (unsigned int bits, int unsignedp)
1203 {
1204 if (unsignedp)
1205 {
1206 if (bits == INT_TYPE_SIZE)
1207 return unsigned_type_node;
1208 else if (bits == CHAR_TYPE_SIZE)
1209 return unsigned_char_type_node;
1210 else if (bits == SHORT_TYPE_SIZE)
1211 return short_unsigned_type_node;
1212 else if (bits == LONG_TYPE_SIZE)
1213 return long_unsigned_type_node;
1214 else if (bits == LONG_LONG_TYPE_SIZE)
1215 return long_long_unsigned_type_node;
1216 else
1217 return build_nonstandard_integer_type (bits,
1218 unsignedp);
1219 }
1220 else
1221 {
1222 if (bits == INT_TYPE_SIZE)
1223 return integer_type_node;
1224 else if (bits == CHAR_TYPE_SIZE)
1225 return signed_char_type_node;
1226 else if (bits == SHORT_TYPE_SIZE)
1227 return short_integer_type_node;
1228 else if (bits == LONG_TYPE_SIZE)
1229 return long_integer_type_node;
1230 else if (bits == LONG_LONG_TYPE_SIZE)
1231 return long_long_integer_type_node;
1232 else
1233 return build_nonstandard_integer_type (bits,
1234 unsignedp);
1235 }
1236 /* Never reach here. */
1237 gcc_unreachable ();
1238 }
1239
1240 /* Allow the analyzer to understand Storage ALLOCATE/DEALLOCATE. */
1241
1242 bool
1243 gm2_langhook_new_dispose_storage_substitution (void)
1244 {
1245 return true;
1246 }
1247
1248 #undef LANG_HOOKS_NAME
1249 #undef LANG_HOOKS_INIT
1250 #undef LANG_HOOKS_INIT_OPTIONS
1251 #undef LANG_HOOKS_OPTION_LANG_MASK
1252 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
1253 #undef LANG_HOOKS_HANDLE_OPTION
1254 #undef LANG_HOOKS_POST_OPTIONS
1255 #undef LANG_HOOKS_PARSE_FILE
1256 #undef LANG_HOOKS_TYPE_FOR_MODE
1257 #undef LANG_HOOKS_TYPE_FOR_SIZE
1258 #undef LANG_HOOKS_BUILTIN_FUNCTION
1259 #undef LANG_HOOKS_GLOBAL_BINDINGS_P
1260 #undef LANG_HOOKS_PUSHDECL
1261 #undef LANG_HOOKS_GETDECLS
1262 #undef LANG_HOOKS_GIMPLIFY_EXPR
1263 #undef LANG_HOOKS_EH_PERSONALITY
1264 #undef LANG_HOOKS_NEW_DISPOSE_STORAGE_SUBSTITUTION
1265
1266 #define LANG_HOOKS_NAME "GNU Modula-2"
1267 #define LANG_HOOKS_INIT gm2_langhook_init
1268 #define LANG_HOOKS_INIT_OPTIONS gm2_langhook_init_options
1269 #define LANG_HOOKS_OPTION_LANG_MASK gm2_langhook_option_lang_mask
1270 #define LANG_HOOKS_INIT_OPTIONS_STRUCT gm2_langhook_init_options_struct
1271 #define LANG_HOOKS_HANDLE_OPTION gm2_langhook_handle_option
1272 #define LANG_HOOKS_POST_OPTIONS gm2_langhook_post_options
1273 #define LANG_HOOKS_PARSE_FILE gm2_langhook_parse_file
1274 #define LANG_HOOKS_TYPE_FOR_MODE gm2_langhook_type_for_mode
1275 #define LANG_HOOKS_TYPE_FOR_SIZE gm2_langhook_type_for_size
1276 #define LANG_HOOKS_BUILTIN_FUNCTION gm2_langhook_builtin_function
1277 #define LANG_HOOKS_GLOBAL_BINDINGS_P gm2_langhook_global_bindings_p
1278 #define LANG_HOOKS_PUSHDECL gm2_langhook_pushdecl
1279 #define LANG_HOOKS_GETDECLS gm2_langhook_getdecls
1280 #define LANG_HOOKS_GIMPLIFY_EXPR gm2_langhook_gimplify_expr
1281 #define LANG_HOOKS_EH_PERSONALITY gm2_langhook_eh_personality
1282 #define LANG_HOOKS_NEW_DISPOSE_STORAGE_SUBSTITUTION \
1283 gm2_langhook_new_dispose_storage_substitution
1284
1285 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
1286
1287 #include "gt-m2-gm2-lang.h"
1288 #include "gtype-m2.h"