Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / gcc / go / gofrontend / go.cc
blob66d481681f3bddb42fb84dc8817724fae3897ae4
1 // go.cc -- Go frontend main file for gcc.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #include "go-system.h"
9 #include "go-c.h"
10 #include "go-diagnostics.h"
12 #include "lex.h"
13 #include "parse.h"
14 #include "backend.h"
15 #include "gogo.h"
17 // The data structures we build to represent the file.
18 static Gogo* gogo;
20 // Create the main IR data structure.
22 GO_EXTERN_C
23 void
24 go_create_gogo(const struct go_create_gogo_args* args)
26 go_assert(::gogo == NULL);
27 ::gogo = new Gogo(args->backend, args->linemap, args->int_type_size,
28 args->pointer_size);
30 if (args->pkgpath != NULL)
31 ::gogo->set_pkgpath(args->pkgpath);
32 else if (args->prefix != NULL)
33 ::gogo->set_prefix(args->prefix);
35 if (args->relative_import_path != NULL)
36 ::gogo->set_relative_import_path(args->relative_import_path);
37 ::gogo->set_check_divide_by_zero(args->check_divide_by_zero);
38 ::gogo->set_check_divide_overflow(args->check_divide_overflow);
39 if (args->compiling_runtime)
40 ::gogo->set_compiling_runtime(args->compiling_runtime);
41 if (args->c_header != NULL)
42 ::gogo->set_c_header(args->c_header);
43 if (args->importcfg != NULL)
44 ::gogo->read_importcfg(args->importcfg);
45 if (args->embedcfg != NULL)
46 ::gogo->read_embedcfg(args->embedcfg);
47 ::gogo->set_debug_escape_level(args->debug_escape_level);
48 if (args->debug_escape_hash != NULL)
49 ::gogo->set_debug_escape_hash(args->debug_escape_hash);
50 ::gogo->set_nil_check_size_threshold(args->nil_check_size_threshold);
51 if (args->debug_optimization)
52 ::gogo->set_debug_optimization(args->debug_optimization);
53 if (args->need_eqtype)
54 ::gogo->set_need_eqtype(args->need_eqtype);
57 // Parse the input files.
59 GO_EXTERN_C
60 void
61 go_parse_input_files(const char** filenames, unsigned int filename_count,
62 bool only_check_syntax, bool)
64 go_assert(filename_count > 0);
66 Lex::Linknames all_linknames;
67 for (unsigned int i = 0; i < filename_count; ++i)
69 if (i > 0)
70 ::gogo->clear_file_scope();
72 const char* filename = filenames[i];
73 FILE* file;
74 if (strcmp(filename, "-") == 0)
75 file = stdin;
76 else
78 file = fopen(filename, "r");
79 if (file == NULL)
80 go_fatal_error(Linemap::unknown_location(),
81 "cannot open %s: %m", filename);
84 Lex lexer(filename, file, ::gogo->linemap());
86 Parse parse(&lexer, ::gogo);
87 parse.program();
89 if (strcmp(filename, "-") != 0)
90 fclose(file);
92 Lex::Linknames* linknames = lexer.get_and_clear_linknames();
93 if (linknames != NULL)
95 if (!::gogo->current_file_imported_unsafe())
97 for (Lex::Linknames::const_iterator p = linknames->begin();
98 p != linknames->end();
99 ++p)
100 go_error_at(p->second.loc,
101 ("%<//go:linkname%> only allowed in Go files that "
102 "import \"unsafe\""));
104 all_linknames.insert(linknames->begin(), linknames->end());
108 ::gogo->clear_file_scope();
110 // If the global predeclared names are referenced but not defined,
111 // define them now.
112 ::gogo->define_global_names();
114 // Apply any go:linkname directives.
115 for (Lex::Linknames::const_iterator p = all_linknames.begin();
116 p != all_linknames.end();
117 ++p)
118 ::gogo->add_linkname(p->first, p->second.is_exported, p->second.ext_name,
119 p->second.loc);
121 // Finalize method lists and build stub methods for named types.
122 ::gogo->finalize_methods();
124 // Check that functions have a terminating statement.
125 ::gogo->check_return_statements();
127 // Now that we have seen all the names, lower the parse tree into a
128 // form which is easier to use.
129 ::gogo->lower_parse_tree();
131 // At this point we have handled all inline functions, so we no
132 // longer need the linemap.
133 ::gogo->linemap()->stop();
135 // Create function descriptors as needed.
136 ::gogo->create_function_descriptors();
138 // Now that we have seen all the names, verify that types are
139 // correct.
140 ::gogo->verify_types();
142 // Work out types of unspecified constants and variables.
143 ::gogo->determine_types();
145 // Check types and issue errors as appropriate.
146 ::gogo->check_types();
148 if (only_check_syntax)
149 return;
151 // Record global variable initializer dependencies.
152 ::gogo->record_global_init_refs();
154 // Do simple deadcode elimination.
155 ::gogo->remove_deadcode();
157 // Make implicit type conversions explicit.
158 ::gogo->add_conversions();
160 // Analyze the program flow for escape information.
161 ::gogo->analyze_escape();
163 // Export global identifiers as appropriate.
164 ::gogo->do_exports();
166 // Use temporary variables to force order of evaluation.
167 ::gogo->order_evaluations();
169 // Turn short-cut operators (&&, ||) into explicit if statements.
170 ::gogo->remove_shortcuts();
172 // Convert named types to backend representation.
173 ::gogo->convert_named_types();
175 // Build thunks for functions which call recover.
176 ::gogo->build_recover_thunks();
178 // Convert complicated go and defer statements into simpler ones.
179 ::gogo->simplify_thunk_statements();
181 // Write out queued up functions for hash and comparison of types.
182 ::gogo->write_specific_type_functions();
184 // Add write barriers.
185 ::gogo->add_write_barriers();
187 // Flatten the parse tree.
188 ::gogo->flatten();
190 // Reclaim memory of escape analysis Nodes.
191 ::gogo->reclaim_escape_nodes();
193 // Dump ast, use filename[0] as the base name
194 ::gogo->dump_ast(filenames[0]);
197 // Write out globals.
199 GO_EXTERN_C
200 void
201 go_write_globals()
203 return ::gogo->write_globals();
206 // Return the global IR structure. This is used by some of the
207 // langhooks to pass to other code.
209 Gogo*
210 go_get_gogo()
212 return ::gogo;