]>
Commit | Line | Data |
---|---|---|
a9ac13f7 | 1 | // go-gcc.cc -- Go frontend to gcc IR. |
a5544970 | 2 | // Copyright (C) 2011-2019 Free Software Foundation, Inc. |
a9ac13f7 ILT |
3 | // Contributed by Ian Lance Taylor, Google. |
4 | ||
5 | // This file is part of GCC. | |
6 | ||
7 | // GCC is free software; you can redistribute it and/or modify it under | |
8 | // the terms of the GNU General Public License as published by the Free | |
9 | // Software Foundation; either version 3, or (at your option) any later | |
10 | // version. | |
11 | ||
12 | // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | // 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 "go-system.h" | |
22 | ||
23 | // This has to be included outside of extern "C", so we have to | |
24 | // include it here before tree.h includes it later. | |
25 | #include <gmp.h> | |
26 | ||
c7131fb2 | 27 | #include "tree.h" |
40e23961 | 28 | #include "fold-const.h" |
d8a2d370 DN |
29 | #include "stringpool.h" |
30 | #include "stor-layout.h" | |
31 | #include "varasm.h" | |
94039447 | 32 | #include "tree-iterator.h" |
60393bbc | 33 | #include "tm.h" |
60393bbc | 34 | #include "function.h" |
c582198b AM |
35 | #include "cgraph.h" |
36 | #include "convert.h" | |
2fb9a547 | 37 | #include "gimple-expr.h" |
036165d8 | 38 | #include "gimplify.h" |
90cbaa29 | 39 | #include "langhooks.h" |
70f91024 | 40 | #include "toplev.h" |
744c3195 | 41 | #include "output.h" |
002ee4d1 | 42 | #include "realmpfr.h" |
9b2b7279 | 43 | #include "builtins.h" |
a9ac13f7 | 44 | |
e09ce6c5 | 45 | #include "go-c.h" |
3c061ac0 | 46 | #include "go-gcc.h" |
e09ce6c5 | 47 | |
94039447 | 48 | #include "gogo.h" |
a9ac13f7 ILT |
49 | #include "backend.h" |
50 | ||
51 | // A class wrapping a tree. | |
52 | ||
53 | class Gcc_tree | |
54 | { | |
55 | public: | |
56 | Gcc_tree(tree t) | |
57 | : t_(t) | |
58 | { } | |
59 | ||
60 | tree | |
0aa5e7f2 | 61 | get_tree() const |
a9ac13f7 ILT |
62 | { return this->t_; } |
63 | ||
ce842ad6 ILT |
64 | void |
65 | set_tree(tree t) | |
66 | { this->t_ = t; } | |
67 | ||
a9ac13f7 ILT |
68 | private: |
69 | tree t_; | |
70 | }; | |
71 | ||
72 | // In gcc, types, expressions, and statements are all trees. | |
73 | class Btype : public Gcc_tree | |
74 | { | |
75 | public: | |
76 | Btype(tree t) | |
77 | : Gcc_tree(t) | |
78 | { } | |
79 | }; | |
80 | ||
81 | class Bexpression : public Gcc_tree | |
82 | { | |
83 | public: | |
84 | Bexpression(tree t) | |
85 | : Gcc_tree(t) | |
86 | { } | |
87 | }; | |
88 | ||
89 | class Bstatement : public Gcc_tree | |
90 | { | |
91 | public: | |
92 | Bstatement(tree t) | |
93 | : Gcc_tree(t) | |
94 | { } | |
95 | }; | |
96 | ||
94039447 ILT |
97 | class Bfunction : public Gcc_tree |
98 | { | |
99 | public: | |
100 | Bfunction(tree t) | |
101 | : Gcc_tree(t) | |
102 | { } | |
103 | }; | |
104 | ||
5ad7db5f ILT |
105 | class Bblock : public Gcc_tree |
106 | { | |
107 | public: | |
108 | Bblock(tree t) | |
109 | : Gcc_tree(t) | |
110 | { } | |
111 | }; | |
112 | ||
399f5feb | 113 | class Blabel : public Gcc_tree |
e09ce6c5 ILT |
114 | { |
115 | public: | |
399f5feb | 116 | Blabel(tree t) |
e09ce6c5 ILT |
117 | : Gcc_tree(t) |
118 | { } | |
119 | }; | |
120 | ||
399f5feb ILT |
121 | // Bvariable is a bit more complicated, because of zero-sized types. |
122 | // The GNU linker does not permit dynamic variables with zero size. | |
123 | // When we see such a variable, we generate a version of the type with | |
124 | // non-zero size. However, when referring to the global variable, we | |
125 | // want an expression of zero size; otherwise, if, say, the global | |
126 | // variable is passed to a function, we will be passing a | |
127 | // non-zero-sized value to a zero-sized value, which can lead to a | |
128 | // miscompilation. | |
129 | ||
130 | class Bvariable | |
d56e6679 ILT |
131 | { |
132 | public: | |
399f5feb ILT |
133 | Bvariable(tree t) |
134 | : t_(t), orig_type_(NULL) | |
135 | { } | |
136 | ||
137 | Bvariable(tree t, tree orig_type) | |
138 | : t_(t), orig_type_(orig_type) | |
d56e6679 | 139 | { } |
399f5feb ILT |
140 | |
141 | // Get the tree for use as an expression. | |
142 | tree | |
143 | get_tree(Location) const; | |
144 | ||
145 | // Get the actual decl; | |
146 | tree | |
147 | get_decl() const | |
148 | { return this->t_; } | |
149 | ||
150 | private: | |
151 | tree t_; | |
152 | tree orig_type_; | |
d56e6679 ILT |
153 | }; |
154 | ||
399f5feb ILT |
155 | // Get the tree of a variable for use as an expression. If this is a |
156 | // zero-sized global, create an expression that refers to the decl but | |
157 | // has zero size. | |
158 | tree | |
159 | Bvariable::get_tree(Location location) const | |
160 | { | |
161 | if (this->orig_type_ == NULL | |
162 | || this->t_ == error_mark_node | |
163 | || TREE_TYPE(this->t_) == this->orig_type_) | |
164 | return this->t_; | |
165 | // Return *(orig_type*)&decl. */ | |
166 | tree t = build_fold_addr_expr_loc(location.gcc_location(), this->t_); | |
167 | t = fold_build1_loc(location.gcc_location(), NOP_EXPR, | |
168 | build_pointer_type(this->orig_type_), t); | |
169 | return build_fold_indirect_ref_loc(location.gcc_location(), t); | |
170 | } | |
171 | ||
a9ac13f7 ILT |
172 | // This file implements the interface between the Go frontend proper |
173 | // and the gcc IR. This implements specific instantiations of | |
174 | // abstract classes defined by the Go frontend proper. The Go | |
175 | // frontend proper class methods of these classes to generate the | |
176 | // backend representation. | |
177 | ||
178 | class Gcc_backend : public Backend | |
179 | { | |
180 | public: | |
90cbaa29 CM |
181 | Gcc_backend(); |
182 | ||
a9ac13f7 ILT |
183 | // Types. |
184 | ||
185 | Btype* | |
186 | error_type() | |
482829ac | 187 | { return this->make_type(error_mark_node); } |
a9ac13f7 ILT |
188 | |
189 | Btype* | |
190 | void_type() | |
0aa5e7f2 | 191 | { return this->make_type(void_type_node); } |
a9ac13f7 ILT |
192 | |
193 | Btype* | |
194 | bool_type() | |
0aa5e7f2 | 195 | { return this->make_type(boolean_type_node); } |
a9ac13f7 ILT |
196 | |
197 | Btype* | |
0aa5e7f2 | 198 | integer_type(bool, int); |
a9ac13f7 ILT |
199 | |
200 | Btype* | |
0aa5e7f2 ES |
201 | float_type(int); |
202 | ||
203 | Btype* | |
204 | complex_type(int); | |
a9ac13f7 ILT |
205 | |
206 | Btype* | |
482829ac | 207 | pointer_type(Btype*); |
a9ac13f7 | 208 | |
0aa5e7f2 | 209 | Btype* |
482829ac ILT |
210 | function_type(const Btyped_identifier&, |
211 | const std::vector<Btyped_identifier>&, | |
212 | const std::vector<Btyped_identifier>&, | |
18768fae | 213 | Btype*, |
8afa2bfb | 214 | const Location); |
a9ac13f7 ILT |
215 | |
216 | Btype* | |
6d69c02e | 217 | struct_type(const std::vector<Btyped_identifier>&); |
a9ac13f7 ILT |
218 | |
219 | Btype* | |
7fc2f86b ILT |
220 | array_type(Btype*, Bexpression*); |
221 | ||
222 | Btype* | |
8afa2bfb | 223 | placeholder_pointer_type(const std::string&, Location, bool); |
7fc2f86b ILT |
224 | |
225 | bool | |
226 | set_placeholder_pointer_type(Btype*, Btype*); | |
227 | ||
228 | bool | |
229 | set_placeholder_function_type(Btype*, Btype*); | |
230 | ||
231 | Btype* | |
8afa2bfb | 232 | placeholder_struct_type(const std::string&, Location); |
7fc2f86b ILT |
233 | |
234 | bool | |
235 | set_placeholder_struct_type(Btype* placeholder, | |
236 | const std::vector<Btyped_identifier>&); | |
237 | ||
238 | Btype* | |
8afa2bfb | 239 | placeholder_array_type(const std::string&, Location); |
7fc2f86b ILT |
240 | |
241 | bool | |
242 | set_placeholder_array_type(Btype*, Btype*, Bexpression*); | |
243 | ||
244 | Btype* | |
8afa2bfb | 245 | named_type(const std::string&, Btype*, Location); |
7fc2f86b ILT |
246 | |
247 | Btype* | |
248 | circular_pointer_type(Btype*, bool); | |
249 | ||
250 | bool | |
251 | is_circular_pointer_type(Btype*); | |
a9ac13f7 | 252 | |
18fbd1ec | 253 | int64_t |
ef1ed13d ILT |
254 | type_size(Btype*); |
255 | ||
18fbd1ec | 256 | int64_t |
ef1ed13d ILT |
257 | type_alignment(Btype*); |
258 | ||
18fbd1ec | 259 | int64_t |
ef1ed13d ILT |
260 | type_field_alignment(Btype*); |
261 | ||
18fbd1ec | 262 | int64_t |
ef1ed13d ILT |
263 | type_field_offset(Btype*, size_t index); |
264 | ||
54466dde ILT |
265 | // Expressions. |
266 | ||
267 | Bexpression* | |
268 | zero_expression(Btype*); | |
269 | ||
d4fe7beb CM |
270 | Bexpression* |
271 | error_expression() | |
272 | { return this->make_expression(error_mark_node); } | |
273 | ||
6f7e0b57 CM |
274 | Bexpression* |
275 | nil_pointer_expression() | |
276 | { return this->make_expression(null_pointer_node); } | |
277 | ||
d4fe7beb | 278 | Bexpression* |
f60bea11 | 279 | var_expression(Bvariable* var, Location); |
d4fe7beb CM |
280 | |
281 | Bexpression* | |
3e7b0938 | 282 | indirect_expression(Btype*, Bexpression* expr, bool known_valid, Location); |
d4fe7beb | 283 | |
e85baec7 CM |
284 | Bexpression* |
285 | named_constant_expression(Btype* btype, const std::string& name, | |
286 | Bexpression* val, Location); | |
287 | ||
002ee4d1 CM |
288 | Bexpression* |
289 | integer_constant_expression(Btype* btype, mpz_t val); | |
290 | ||
291 | Bexpression* | |
292 | float_constant_expression(Btype* btype, mpfr_t val); | |
293 | ||
294 | Bexpression* | |
5eda5bad | 295 | complex_constant_expression(Btype* btype, mpc_t val); |
002ee4d1 | 296 | |
7035307e CM |
297 | Bexpression* |
298 | string_constant_expression(const std::string& val); | |
299 | ||
6f7e0b57 CM |
300 | Bexpression* |
301 | boolean_constant_expression(bool val); | |
302 | ||
7035307e CM |
303 | Bexpression* |
304 | real_part_expression(Bexpression* bcomplex, Location); | |
305 | ||
306 | Bexpression* | |
307 | imag_part_expression(Bexpression* bcomplex, Location); | |
308 | ||
309 | Bexpression* | |
310 | complex_expression(Bexpression* breal, Bexpression* bimag, Location); | |
311 | ||
c6d2bfbb CM |
312 | Bexpression* |
313 | convert_expression(Btype* type, Bexpression* expr, Location); | |
314 | ||
b7d93b46 CM |
315 | Bexpression* |
316 | function_code_expression(Bfunction*, Location); | |
317 | ||
b93e0cfd CM |
318 | Bexpression* |
319 | address_expression(Bexpression*, Location); | |
320 | ||
71eba7a0 CM |
321 | Bexpression* |
322 | struct_field_expression(Bexpression*, size_t, Location); | |
323 | ||
eb6eb862 CM |
324 | Bexpression* |
325 | compound_expression(Bstatement*, Bexpression*, Location); | |
326 | ||
327 | Bexpression* | |
a2182c9c TM |
328 | conditional_expression(Bfunction*, Btype*, Bexpression*, Bexpression*, |
329 | Bexpression*, Location); | |
b5407ad1 | 330 | |
8a35e18d CM |
331 | Bexpression* |
332 | unary_expression(Operator, Bexpression*, Location); | |
333 | ||
b5407ad1 CM |
334 | Bexpression* |
335 | binary_expression(Operator, Bexpression*, Bexpression*, Location); | |
eb6eb862 | 336 | |
7035307e CM |
337 | Bexpression* |
338 | constructor_expression(Btype*, const std::vector<Bexpression*>&, Location); | |
339 | ||
340 | Bexpression* | |
341 | array_constructor_expression(Btype*, const std::vector<unsigned long>&, | |
342 | const std::vector<Bexpression*>&, Location); | |
343 | ||
344 | Bexpression* | |
345 | pointer_offset_expression(Bexpression* base, Bexpression* offset, Location); | |
346 | ||
347 | Bexpression* | |
348 | array_index_expression(Bexpression* array, Bexpression* index, Location); | |
349 | ||
350 | Bexpression* | |
da55a299 TM |
351 | call_expression(Bfunction* caller, Bexpression* fn, |
352 | const std::vector<Bexpression*>& args, | |
38bf819a | 353 | Bexpression* static_chain, Location); |
7035307e | 354 | |
a9ac13f7 ILT |
355 | // Statements. |
356 | ||
f54d331e ILT |
357 | Bstatement* |
358 | error_statement() | |
359 | { return this->make_statement(error_mark_node); } | |
360 | ||
cfebcf30 | 361 | Bstatement* |
8220e3f9 | 362 | expression_statement(Bfunction*, Bexpression*); |
cfebcf30 | 363 | |
e09ce6c5 | 364 | Bstatement* |
8220e3f9 | 365 | init_statement(Bfunction*, Bvariable* var, Bexpression* init); |
e09ce6c5 | 366 | |
a9ac13f7 | 367 | Bstatement* |
8220e3f9 TM |
368 | assignment_statement(Bfunction*, Bexpression* lhs, Bexpression* rhs, |
369 | Location); | |
94039447 | 370 | |
94039447 ILT |
371 | Bstatement* |
372 | return_statement(Bfunction*, const std::vector<Bexpression*>&, | |
8afa2bfb | 373 | Location); |
a9ac13f7 | 374 | |
db0adf82 | 375 | Bstatement* |
8220e3f9 TM |
376 | if_statement(Bfunction*, Bexpression* condition, Bblock* then_block, |
377 | Bblock* else_block, Location); | |
db0adf82 | 378 | |
8d0b03a2 | 379 | Bstatement* |
036165d8 | 380 | switch_statement(Bfunction* function, Bexpression* value, |
8d0b03a2 ILT |
381 | const std::vector<std::vector<Bexpression*> >& cases, |
382 | const std::vector<Bstatement*>& statements, | |
8afa2bfb | 383 | Location); |
8d0b03a2 | 384 | |
00b44a6e ILT |
385 | Bstatement* |
386 | compound_statement(Bstatement*, Bstatement*); | |
387 | ||
8d0b03a2 ILT |
388 | Bstatement* |
389 | statement_list(const std::vector<Bstatement*>&); | |
390 | ||
7035307e CM |
391 | Bstatement* |
392 | exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt, | |
393 | Bstatement* finally_stmt, Location); | |
394 | ||
5ad7db5f ILT |
395 | // Blocks. |
396 | ||
397 | Bblock* | |
398 | block(Bfunction*, Bblock*, const std::vector<Bvariable*>&, | |
8afa2bfb | 399 | Location, Location); |
5ad7db5f ILT |
400 | |
401 | void | |
402 | block_add_statements(Bblock*, const std::vector<Bstatement*>&); | |
403 | ||
404 | Bstatement* | |
405 | block_statement(Bblock*); | |
406 | ||
e09ce6c5 ILT |
407 | // Variables. |
408 | ||
409 | Bvariable* | |
410 | error_variable() | |
411 | { return new Bvariable(error_mark_node); } | |
412 | ||
413 | Bvariable* | |
f3878205 TM |
414 | global_variable(const std::string& var_name, |
415 | const std::string& asm_name, | |
e09ce6c5 ILT |
416 | Btype* btype, |
417 | bool is_external, | |
418 | bool is_hidden, | |
744c3195 | 419 | bool in_unique_section, |
8afa2bfb | 420 | Location location); |
e09ce6c5 ILT |
421 | |
422 | void | |
423 | global_variable_set_init(Bvariable*, Bexpression*); | |
424 | ||
425 | Bvariable* | |
81e0f092 | 426 | local_variable(Bfunction*, const std::string&, Btype*, Bvariable*, bool, |
8afa2bfb | 427 | Location); |
e09ce6c5 ILT |
428 | |
429 | Bvariable* | |
acf98146 | 430 | parameter_variable(Bfunction*, const std::string&, Btype*, bool, |
8afa2bfb | 431 | Location); |
e09ce6c5 | 432 | |
38bf819a RH |
433 | Bvariable* |
434 | static_chain_variable(Bfunction*, const std::string&, Btype*, Location); | |
435 | ||
9131ad67 ILT |
436 | Bvariable* |
437 | temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool, | |
8afa2bfb | 438 | Location, Bstatement**); |
9131ad67 | 439 | |
036165d8 | 440 | Bvariable* |
f3878205 TM |
441 | implicit_variable(const std::string&, const std::string&, Btype*, |
442 | bool, bool, bool, int64_t); | |
036165d8 | 443 | |
f1d2ac4f CM |
444 | void |
445 | implicit_variable_set_init(Bvariable*, const std::string&, Btype*, | |
446 | bool, bool, bool, Bexpression*); | |
447 | ||
448 | Bvariable* | |
f3878205 | 449 | implicit_variable_reference(const std::string&, const std::string&, Btype*); |
f1d2ac4f | 450 | |
70f91024 | 451 | Bvariable* |
f3878205 TM |
452 | immutable_struct(const std::string&, const std::string&, |
453 | bool, bool, Btype*, Location); | |
70f91024 ILT |
454 | |
455 | void | |
fdbc38a6 | 456 | immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*, |
8afa2bfb | 457 | Location, Bexpression*); |
70f91024 ILT |
458 | |
459 | Bvariable* | |
f3878205 TM |
460 | immutable_struct_reference(const std::string&, const std::string&, |
461 | Btype*, Location); | |
70f91024 | 462 | |
d56e6679 ILT |
463 | // Labels. |
464 | ||
465 | Blabel* | |
8afa2bfb | 466 | label(Bfunction*, const std::string& name, Location); |
d56e6679 ILT |
467 | |
468 | Bstatement* | |
469 | label_definition_statement(Blabel*); | |
470 | ||
471 | Bstatement* | |
8afa2bfb | 472 | goto_statement(Blabel*, Location); |
d56e6679 ILT |
473 | |
474 | Bexpression* | |
8afa2bfb | 475 | label_address(Blabel*, Location); |
d56e6679 | 476 | |
f7191ecd CM |
477 | // Functions. |
478 | ||
479 | Bfunction* | |
480 | error_function() | |
481 | { return this->make_function(error_mark_node); } | |
482 | ||
483 | Bfunction* | |
484 | function(Btype* fntype, const std::string& name, const std::string& asm_name, | |
a4e30bef | 485 | unsigned int flags, Location); |
f7191ecd | 486 | |
7035307e CM |
487 | Bstatement* |
488 | function_defer_statement(Bfunction* function, Bexpression* undefer, | |
489 | Bexpression* defer, Location); | |
490 | ||
491 | bool | |
492 | function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&); | |
493 | ||
494 | bool | |
495 | function_set_body(Bfunction* function, Bstatement* code_stmt); | |
496 | ||
90cbaa29 CM |
497 | Bfunction* |
498 | lookup_builtin(const std::string&); | |
499 | ||
036165d8 CM |
500 | void |
501 | write_global_definitions(const std::vector<Btype*>&, | |
502 | const std::vector<Bexpression*>&, | |
503 | const std::vector<Bfunction*>&, | |
504 | const std::vector<Bvariable*>&); | |
505 | ||
f163907e ILT |
506 | void |
507 | write_export_data(const char* bytes, unsigned int size); | |
508 | ||
509 | ||
a9ac13f7 | 510 | private: |
d56e6679 ILT |
511 | // Make a Bexpression from a tree. |
512 | Bexpression* | |
513 | make_expression(tree t) | |
514 | { return new Bexpression(t); } | |
515 | ||
a9ac13f7 ILT |
516 | // Make a Bstatement from a tree. |
517 | Bstatement* | |
518 | make_statement(tree t) | |
519 | { return new Bstatement(t); } | |
0aa5e7f2 ES |
520 | |
521 | // Make a Btype from a tree. | |
522 | Btype* | |
523 | make_type(tree t) | |
524 | { return new Btype(t); } | |
7fc2f86b | 525 | |
f7191ecd CM |
526 | Bfunction* |
527 | make_function(tree t) | |
528 | { return new Bfunction(t); } | |
529 | ||
7fc2f86b ILT |
530 | Btype* |
531 | fill_in_struct(Btype*, const std::vector<Btyped_identifier>&); | |
532 | ||
533 | Btype* | |
534 | fill_in_array(Btype*, Btype*, Bexpression*); | |
7d6be4c8 ILT |
535 | |
536 | tree | |
537 | non_zero_size_type(tree); | |
90cbaa29 | 538 | |
ebc2f401 ILT |
539 | tree |
540 | convert_tree(tree, tree, Location); | |
541 | ||
90cbaa29 CM |
542 | private: |
543 | void | |
544 | define_builtin(built_in_function bcode, const char* name, const char* libname, | |
2021ff27 | 545 | tree fntype, bool const_p, bool noreturn_p); |
90cbaa29 CM |
546 | |
547 | // A mapping of the GCC built-ins exposed to GCCGo. | |
548 | std::map<std::string, Bfunction*> builtin_functions_; | |
a9ac13f7 ILT |
549 | }; |
550 | ||
9dcd84ec | 551 | // A helper function to create a GCC identifier from a C++ string. |
d56e6679 ILT |
552 | |
553 | static inline tree | |
554 | get_identifier_from_string(const std::string& str) | |
555 | { | |
556 | return get_identifier_with_length(str.data(), str.length()); | |
557 | } | |
cfebcf30 | 558 | |
90cbaa29 CM |
559 | // Define the built-in functions that are exposed to GCCGo. |
560 | ||
561 | Gcc_backend::Gcc_backend() | |
562 | { | |
563 | /* We need to define the fetch_and_add functions, since we use them | |
564 | for ++ and --. */ | |
fdbda43d | 565 | tree t = this->integer_type(true, BITS_PER_UNIT)->get_tree(); |
90cbaa29 CM |
566 | tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE)); |
567 | this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1", | |
568 | NULL, build_function_type_list(t, p, t, NULL_TREE), | |
2021ff27 | 569 | false, false); |
90cbaa29 | 570 | |
fdbda43d | 571 | t = this->integer_type(true, BITS_PER_UNIT * 2)->get_tree(); |
90cbaa29 CM |
572 | p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE)); |
573 | this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2", | |
574 | NULL, build_function_type_list(t, p, t, NULL_TREE), | |
2021ff27 | 575 | false, false); |
90cbaa29 | 576 | |
fdbda43d | 577 | t = this->integer_type(true, BITS_PER_UNIT * 4)->get_tree(); |
90cbaa29 CM |
578 | p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE)); |
579 | this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4", | |
580 | NULL, build_function_type_list(t, p, t, NULL_TREE), | |
2021ff27 | 581 | false, false); |
90cbaa29 | 582 | |
fdbda43d | 583 | t = this->integer_type(true, BITS_PER_UNIT * 8)->get_tree(); |
90cbaa29 CM |
584 | p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE)); |
585 | this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8", | |
586 | NULL, build_function_type_list(t, p, t, NULL_TREE), | |
2021ff27 | 587 | false, false); |
90cbaa29 CM |
588 | |
589 | // We use __builtin_expect for magic import functions. | |
590 | this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL, | |
591 | build_function_type_list(long_integer_type_node, | |
592 | long_integer_type_node, | |
593 | long_integer_type_node, | |
594 | NULL_TREE), | |
2021ff27 | 595 | true, false); |
90cbaa29 CM |
596 | |
597 | // We use __builtin_memcmp for struct comparisons. | |
598 | this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp", | |
599 | build_function_type_list(integer_type_node, | |
600 | const_ptr_type_node, | |
601 | const_ptr_type_node, | |
602 | size_type_node, | |
603 | NULL_TREE), | |
2021ff27 | 604 | false, false); |
90cbaa29 | 605 | |
a8464243 ILT |
606 | // Used by runtime/internal/sys. |
607 | this->define_builtin(BUILT_IN_CTZ, "__builtin_ctz", "ctz", | |
608 | build_function_type_list(integer_type_node, | |
609 | unsigned_type_node, | |
610 | NULL_TREE), | |
611 | true, false); | |
612 | this->define_builtin(BUILT_IN_CTZLL, "__builtin_ctzll", "ctzll", | |
613 | build_function_type_list(integer_type_node, | |
614 | long_long_unsigned_type_node, | |
615 | NULL_TREE), | |
616 | true, false); | |
617 | this->define_builtin(BUILT_IN_BSWAP32, "__builtin_bswap32", "bswap32", | |
618 | build_function_type_list(uint32_type_node, | |
619 | uint32_type_node, | |
620 | NULL_TREE), | |
621 | true, false); | |
622 | this->define_builtin(BUILT_IN_BSWAP64, "__builtin_bswap64", "bswap64", | |
623 | build_function_type_list(uint64_type_node, | |
624 | uint64_type_node, | |
625 | NULL_TREE), | |
626 | true, false); | |
627 | ||
90cbaa29 CM |
628 | // We provide some functions for the math library. |
629 | tree math_function_type = build_function_type_list(double_type_node, | |
630 | double_type_node, | |
631 | NULL_TREE); | |
632 | tree math_function_type_long = | |
633 | build_function_type_list(long_double_type_node, long_double_type_node, | |
677202a2 | 634 | NULL_TREE); |
90cbaa29 CM |
635 | tree math_function_type_two = build_function_type_list(double_type_node, |
636 | double_type_node, | |
637 | double_type_node, | |
638 | NULL_TREE); | |
639 | tree math_function_type_long_two = | |
640 | build_function_type_list(long_double_type_node, long_double_type_node, | |
641 | long_double_type_node, NULL_TREE); | |
642 | this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos", | |
2021ff27 | 643 | math_function_type, true, false); |
90cbaa29 | 644 | this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl", |
2021ff27 | 645 | math_function_type_long, true, false); |
90cbaa29 | 646 | this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin", |
2021ff27 | 647 | math_function_type, true, false); |
90cbaa29 | 648 | this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl", |
2021ff27 | 649 | math_function_type_long, true, false); |
90cbaa29 | 650 | this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan", |
2021ff27 | 651 | math_function_type, true, false); |
90cbaa29 | 652 | this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl", |
2021ff27 | 653 | math_function_type_long, true, false); |
90cbaa29 | 654 | this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2", |
2021ff27 | 655 | math_function_type_two, true, false); |
90cbaa29 | 656 | this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l", |
2021ff27 | 657 | math_function_type_long_two, true, false); |
90cbaa29 | 658 | this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil", |
2021ff27 | 659 | math_function_type, true, false); |
90cbaa29 | 660 | this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill", |
2021ff27 | 661 | math_function_type_long, true, false); |
90cbaa29 | 662 | this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos", |
2021ff27 | 663 | math_function_type, true, false); |
90cbaa29 | 664 | this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl", |
2021ff27 | 665 | math_function_type_long, true, false); |
90cbaa29 | 666 | this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp", |
2021ff27 | 667 | math_function_type, true, false); |
90cbaa29 | 668 | this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl", |
2021ff27 | 669 | math_function_type_long, true, false); |
90cbaa29 | 670 | this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1", |
2021ff27 | 671 | math_function_type, true, false); |
90cbaa29 | 672 | this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l", |
2021ff27 | 673 | math_function_type_long, true, false); |
90cbaa29 | 674 | this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs", |
2021ff27 | 675 | math_function_type, true, false); |
90cbaa29 | 676 | this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl", |
2021ff27 | 677 | math_function_type_long, true, false); |
90cbaa29 | 678 | this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor", |
2021ff27 | 679 | math_function_type, true, false); |
90cbaa29 | 680 | this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl", |
2021ff27 | 681 | math_function_type_long, true, false); |
90cbaa29 | 682 | this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod", |
2021ff27 | 683 | math_function_type_two, true, false); |
90cbaa29 | 684 | this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl", |
2021ff27 | 685 | math_function_type_long_two, true, false); |
90cbaa29 CM |
686 | this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp", |
687 | build_function_type_list(double_type_node, | |
688 | double_type_node, | |
689 | integer_type_node, | |
690 | NULL_TREE), | |
2021ff27 | 691 | true, false); |
90cbaa29 CM |
692 | this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl", |
693 | build_function_type_list(long_double_type_node, | |
694 | long_double_type_node, | |
695 | integer_type_node, | |
696 | NULL_TREE), | |
2021ff27 | 697 | true, false); |
90cbaa29 | 698 | this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log", |
2021ff27 | 699 | math_function_type, true, false); |
90cbaa29 | 700 | this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl", |
2021ff27 | 701 | math_function_type_long, true, false); |
90cbaa29 | 702 | this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p", |
2021ff27 | 703 | math_function_type, true, false); |
90cbaa29 | 704 | this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl", |
2021ff27 | 705 | math_function_type_long, true, false); |
90cbaa29 | 706 | this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10", |
2021ff27 | 707 | math_function_type, true, false); |
90cbaa29 | 708 | this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l", |
2021ff27 | 709 | math_function_type_long, true, false); |
90cbaa29 | 710 | this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2", |
2021ff27 | 711 | math_function_type, true, false); |
90cbaa29 | 712 | this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l", |
2021ff27 | 713 | math_function_type_long, true, false); |
90cbaa29 | 714 | this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin", |
2021ff27 | 715 | math_function_type, true, false); |
90cbaa29 | 716 | this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl", |
2021ff27 | 717 | math_function_type_long, true, false); |
90cbaa29 | 718 | this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt", |
2021ff27 | 719 | math_function_type, true, false); |
90cbaa29 | 720 | this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl", |
2021ff27 | 721 | math_function_type_long, true, false); |
90cbaa29 | 722 | this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan", |
2021ff27 | 723 | math_function_type, true, false); |
90cbaa29 | 724 | this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl", |
2021ff27 | 725 | math_function_type_long, true, false); |
90cbaa29 | 726 | this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc", |
2021ff27 | 727 | math_function_type, true, false); |
90cbaa29 | 728 | this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl", |
2021ff27 | 729 | math_function_type_long, true, false); |
90cbaa29 CM |
730 | |
731 | // We use __builtin_return_address in the thunk we build for | |
0cb904af ILT |
732 | // functions which call recover, and for runtime.getcallerpc. |
733 | t = build_function_type_list(ptr_type_node, unsigned_type_node, NULL_TREE); | |
90cbaa29 | 734 | this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address", |
0cb904af ILT |
735 | NULL, t, false, false); |
736 | ||
737 | // The runtime calls __builtin_frame_address for runtime.getcallersp. | |
738 | this->define_builtin(BUILT_IN_FRAME_ADDRESS, "__builtin_frame_address", | |
739 | NULL, t, false, false); | |
90cbaa29 | 740 | |
9d1e3afb ILT |
741 | // The runtime calls __builtin_extract_return_addr when recording |
742 | // the address to which a function returns. | |
743 | this->define_builtin(BUILT_IN_EXTRACT_RETURN_ADDR, | |
744 | "__builtin_extract_return_addr", NULL, | |
745 | build_function_type_list(ptr_type_node, | |
746 | ptr_type_node, | |
747 | NULL_TREE), | |
748 | false, false); | |
749 | ||
90cbaa29 CM |
750 | // The compiler uses __builtin_trap for some exception handling |
751 | // cases. | |
752 | this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL, | |
753 | build_function_type(void_type_node, void_list_node), | |
2021ff27 | 754 | false, true); |
f163907e ILT |
755 | |
756 | // The runtime uses __builtin_prefetch. | |
757 | this->define_builtin(BUILT_IN_PREFETCH, "__builtin_prefetch", NULL, | |
758 | build_varargs_function_type_list(void_type_node, | |
759 | const_ptr_type_node, | |
760 | NULL_TREE), | |
761 | false, false); | |
9638589f | 762 | |
67914693 | 763 | // The compiler uses __builtin_unreachable for cases that cannot |
9638589f ILT |
764 | // occur. |
765 | this->define_builtin(BUILT_IN_UNREACHABLE, "__builtin_unreachable", NULL, | |
766 | build_function_type(void_type_node, void_list_node), | |
767 | true, true); | |
90cbaa29 CM |
768 | } |
769 | ||
0aa5e7f2 ES |
770 | // Get an unnamed integer type. |
771 | ||
772 | Btype* | |
773 | Gcc_backend::integer_type(bool is_unsigned, int bits) | |
774 | { | |
775 | tree type; | |
776 | if (is_unsigned) | |
777 | { | |
778 | if (bits == INT_TYPE_SIZE) | |
779 | type = unsigned_type_node; | |
780 | else if (bits == CHAR_TYPE_SIZE) | |
781 | type = unsigned_char_type_node; | |
782 | else if (bits == SHORT_TYPE_SIZE) | |
783 | type = short_unsigned_type_node; | |
784 | else if (bits == LONG_TYPE_SIZE) | |
785 | type = long_unsigned_type_node; | |
786 | else if (bits == LONG_LONG_TYPE_SIZE) | |
787 | type = long_long_unsigned_type_node; | |
788 | else | |
789 | type = make_unsigned_type(bits); | |
790 | } | |
791 | else | |
792 | { | |
793 | if (bits == INT_TYPE_SIZE) | |
794 | type = integer_type_node; | |
795 | else if (bits == CHAR_TYPE_SIZE) | |
796 | type = signed_char_type_node; | |
797 | else if (bits == SHORT_TYPE_SIZE) | |
798 | type = short_integer_type_node; | |
799 | else if (bits == LONG_TYPE_SIZE) | |
800 | type = long_integer_type_node; | |
801 | else if (bits == LONG_LONG_TYPE_SIZE) | |
802 | type = long_long_integer_type_node; | |
803 | else | |
804 | type = make_signed_type(bits); | |
805 | } | |
806 | return this->make_type(type); | |
807 | } | |
808 | ||
809 | // Get an unnamed float type. | |
810 | ||
811 | Btype* | |
812 | Gcc_backend::float_type(int bits) | |
813 | { | |
814 | tree type; | |
815 | if (bits == FLOAT_TYPE_SIZE) | |
816 | type = float_type_node; | |
817 | else if (bits == DOUBLE_TYPE_SIZE) | |
818 | type = double_type_node; | |
819 | else if (bits == LONG_DOUBLE_TYPE_SIZE) | |
820 | type = long_double_type_node; | |
821 | else | |
822 | { | |
823 | type = make_node(REAL_TYPE); | |
824 | TYPE_PRECISION(type) = bits; | |
825 | layout_type(type); | |
826 | } | |
827 | return this->make_type(type); | |
828 | } | |
829 | ||
830 | // Get an unnamed complex type. | |
831 | ||
832 | Btype* | |
833 | Gcc_backend::complex_type(int bits) | |
834 | { | |
835 | tree type; | |
836 | if (bits == FLOAT_TYPE_SIZE * 2) | |
837 | type = complex_float_type_node; | |
838 | else if (bits == DOUBLE_TYPE_SIZE * 2) | |
839 | type = complex_double_type_node; | |
840 | else if (bits == LONG_DOUBLE_TYPE_SIZE * 2) | |
841 | type = complex_long_double_type_node; | |
842 | else | |
843 | { | |
844 | type = make_node(REAL_TYPE); | |
845 | TYPE_PRECISION(type) = bits / 2; | |
846 | layout_type(type); | |
847 | type = build_complex_type(type); | |
848 | } | |
849 | return this->make_type(type); | |
850 | } | |
851 | ||
852 | // Get a pointer type. | |
853 | ||
854 | Btype* | |
482829ac | 855 | Gcc_backend::pointer_type(Btype* to_type) |
0aa5e7f2 | 856 | { |
482829ac ILT |
857 | tree to_type_tree = to_type->get_tree(); |
858 | if (to_type_tree == error_mark_node) | |
859 | return this->error_type(); | |
860 | tree type = build_pointer_type(to_type_tree); | |
0aa5e7f2 ES |
861 | return this->make_type(type); |
862 | } | |
863 | ||
482829ac ILT |
864 | // Make a function type. |
865 | ||
866 | Btype* | |
867 | Gcc_backend::function_type(const Btyped_identifier& receiver, | |
868 | const std::vector<Btyped_identifier>& parameters, | |
869 | const std::vector<Btyped_identifier>& results, | |
18768fae ILT |
870 | Btype* result_struct, |
871 | Location) | |
482829ac ILT |
872 | { |
873 | tree args = NULL_TREE; | |
874 | tree* pp = &args; | |
875 | if (receiver.btype != NULL) | |
876 | { | |
877 | tree t = receiver.btype->get_tree(); | |
878 | if (t == error_mark_node) | |
879 | return this->error_type(); | |
880 | *pp = tree_cons(NULL_TREE, t, NULL_TREE); | |
881 | pp = &TREE_CHAIN(*pp); | |
882 | } | |
883 | ||
884 | for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin(); | |
885 | p != parameters.end(); | |
886 | ++p) | |
887 | { | |
888 | tree t = p->btype->get_tree(); | |
889 | if (t == error_mark_node) | |
890 | return this->error_type(); | |
891 | *pp = tree_cons(NULL_TREE, t, NULL_TREE); | |
892 | pp = &TREE_CHAIN(*pp); | |
893 | } | |
894 | ||
895 | // Varargs is handled entirely at the Go level. When converted to | |
896 | // GENERIC functions are not varargs. | |
897 | *pp = void_list_node; | |
898 | ||
899 | tree result; | |
900 | if (results.empty()) | |
901 | result = void_type_node; | |
902 | else if (results.size() == 1) | |
903 | result = results.front().btype->get_tree(); | |
904 | else | |
905 | { | |
18768fae ILT |
906 | gcc_assert(result_struct != NULL); |
907 | result = result_struct->get_tree(); | |
482829ac ILT |
908 | } |
909 | if (result == error_mark_node) | |
910 | return this->error_type(); | |
911 | ||
67914693 | 912 | // The libffi library cannot represent a zero-sized object. To |
f432d128 ILT |
913 | // avoid causing confusion on 32-bit SPARC, we treat a function that |
914 | // returns a zero-sized value as returning void. That should do no | |
915 | // harm since there is no actual value to be returned. See | |
916 | // https://gcc.gnu.org/PR72814 for details. | |
917 | if (result != void_type_node && int_size_in_bytes(result) == 0) | |
918 | result = void_type_node; | |
919 | ||
482829ac ILT |
920 | tree fntype = build_function_type(result, args); |
921 | if (fntype == error_mark_node) | |
922 | return this->error_type(); | |
923 | ||
924 | return this->make_type(build_pointer_type(fntype)); | |
925 | } | |
926 | ||
6d69c02e ILT |
927 | // Make a struct type. |
928 | ||
929 | Btype* | |
930 | Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields) | |
931 | { | |
7fc2f86b ILT |
932 | return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields); |
933 | } | |
934 | ||
935 | // Fill in the fields of a struct type. | |
936 | ||
937 | Btype* | |
938 | Gcc_backend::fill_in_struct(Btype* fill, | |
939 | const std::vector<Btyped_identifier>& fields) | |
940 | { | |
941 | tree fill_tree = fill->get_tree(); | |
6d69c02e ILT |
942 | tree field_trees = NULL_TREE; |
943 | tree* pp = &field_trees; | |
944 | for (std::vector<Btyped_identifier>::const_iterator p = fields.begin(); | |
945 | p != fields.end(); | |
946 | ++p) | |
947 | { | |
948 | tree name_tree = get_identifier_from_string(p->name); | |
949 | tree type_tree = p->btype->get_tree(); | |
950 | if (type_tree == error_mark_node) | |
951 | return this->error_type(); | |
8afa2bfb SD |
952 | tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree, |
953 | type_tree); | |
7fc2f86b | 954 | DECL_CONTEXT(field) = fill_tree; |
6d69c02e ILT |
955 | *pp = field; |
956 | pp = &DECL_CHAIN(field); | |
957 | } | |
7fc2f86b ILT |
958 | TYPE_FIELDS(fill_tree) = field_trees; |
959 | layout_type(fill_tree); | |
3df9a1fe ILT |
960 | |
961 | // Because Go permits converting between named struct types and | |
962 | // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and | |
963 | // because we don't try to maintain TYPE_CANONICAL for struct types, | |
964 | // we need to tell the middle-end to use structural equality. | |
965 | SET_TYPE_STRUCTURAL_EQUALITY(fill_tree); | |
966 | ||
7fc2f86b ILT |
967 | return fill; |
968 | } | |
969 | ||
970 | // Make an array type. | |
971 | ||
972 | Btype* | |
973 | Gcc_backend::array_type(Btype* element_btype, Bexpression* length) | |
974 | { | |
975 | return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)), | |
976 | element_btype, length); | |
977 | } | |
978 | ||
979 | // Fill in an array type. | |
980 | ||
981 | Btype* | |
982 | Gcc_backend::fill_in_array(Btype* fill, Btype* element_type, | |
983 | Bexpression* length) | |
984 | { | |
985 | tree element_type_tree = element_type->get_tree(); | |
986 | tree length_tree = length->get_tree(); | |
987 | if (element_type_tree == error_mark_node || length_tree == error_mark_node) | |
988 | return this->error_type(); | |
989 | ||
990 | gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE); | |
991 | ||
992 | length_tree = fold_convert(sizetype, length_tree); | |
993 | ||
994 | // build_index_type takes the maximum index, which is one less than | |
995 | // the length. | |
996 | tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype, | |
997 | length_tree, | |
998 | size_one_node)); | |
999 | ||
1000 | tree fill_tree = fill->get_tree(); | |
1001 | TREE_TYPE(fill_tree) = element_type_tree; | |
1002 | TYPE_DOMAIN(fill_tree) = index_type_tree; | |
1003 | TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree); | |
1004 | layout_type(fill_tree); | |
1005 | ||
1006 | if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree)) | |
1007 | SET_TYPE_STRUCTURAL_EQUALITY(fill_tree); | |
1008 | else if (TYPE_CANONICAL(element_type_tree) != element_type_tree | |
1009 | || TYPE_CANONICAL(index_type_tree) != index_type_tree) | |
1010 | TYPE_CANONICAL(fill_tree) = | |
1011 | build_array_type(TYPE_CANONICAL(element_type_tree), | |
1012 | TYPE_CANONICAL(index_type_tree)); | |
1013 | ||
1014 | return fill; | |
1015 | } | |
1016 | ||
1017 | // Create a placeholder for a pointer type. | |
1018 | ||
1019 | Btype* | |
1020 | Gcc_backend::placeholder_pointer_type(const std::string& name, | |
8afa2bfb | 1021 | Location location, bool) |
7fc2f86b | 1022 | { |
3762c343 | 1023 | tree ret = build_distinct_type_copy(ptr_type_node); |
26793fb5 ILT |
1024 | if (!name.empty()) |
1025 | { | |
8afa2bfb | 1026 | tree decl = build_decl(location.gcc_location(), TYPE_DECL, |
26793fb5 ILT |
1027 | get_identifier_from_string(name), |
1028 | ret); | |
1029 | TYPE_NAME(ret) = decl; | |
1030 | } | |
7fc2f86b ILT |
1031 | return this->make_type(ret); |
1032 | } | |
1033 | ||
1034 | // Set the real target type for a placeholder pointer type. | |
1035 | ||
1036 | bool | |
1037 | Gcc_backend::set_placeholder_pointer_type(Btype* placeholder, | |
1038 | Btype* to_type) | |
1039 | { | |
1040 | tree pt = placeholder->get_tree(); | |
1041 | if (pt == error_mark_node) | |
1042 | return false; | |
1043 | gcc_assert(TREE_CODE(pt) == POINTER_TYPE); | |
1044 | tree tt = to_type->get_tree(); | |
1045 | if (tt == error_mark_node) | |
1046 | { | |
ce842ad6 | 1047 | placeholder->set_tree(error_mark_node); |
7fc2f86b ILT |
1048 | return false; |
1049 | } | |
1050 | gcc_assert(TREE_CODE(tt) == POINTER_TYPE); | |
1051 | TREE_TYPE(pt) = TREE_TYPE(tt); | |
ce4321c9 | 1052 | TYPE_CANONICAL(pt) = TYPE_CANONICAL(tt); |
dcf30625 ILT |
1053 | if (TYPE_NAME(pt) != NULL_TREE) |
1054 | { | |
1055 | // Build the data structure gcc wants to see for a typedef. | |
1056 | tree copy = build_variant_type_copy(pt); | |
1057 | TYPE_NAME(copy) = NULL_TREE; | |
1058 | DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy; | |
1059 | } | |
7fc2f86b ILT |
1060 | return true; |
1061 | } | |
1062 | ||
1063 | // Set the real values for a placeholder function type. | |
1064 | ||
1065 | bool | |
1066 | Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft) | |
1067 | { | |
1068 | return this->set_placeholder_pointer_type(placeholder, ft); | |
1069 | } | |
1070 | ||
1071 | // Create a placeholder for a struct type. | |
1072 | ||
1073 | Btype* | |
1074 | Gcc_backend::placeholder_struct_type(const std::string& name, | |
8afa2bfb | 1075 | Location location) |
7fc2f86b ILT |
1076 | { |
1077 | tree ret = make_node(RECORD_TYPE); | |
7c0434e5 ILT |
1078 | if (!name.empty()) |
1079 | { | |
1080 | tree decl = build_decl(location.gcc_location(), TYPE_DECL, | |
1081 | get_identifier_from_string(name), | |
1082 | ret); | |
1083 | TYPE_NAME(ret) = decl; | |
ce4321c9 NB |
1084 | |
1085 | // The struct type that eventually replaces this placeholder will require | |
1086 | // structural equality. The placeholder must too, so that the requirement | |
1087 | // for structural equality propagates to references that are constructed | |
1088 | // before the replacement occurs. | |
1089 | SET_TYPE_STRUCTURAL_EQUALITY(ret); | |
7c0434e5 | 1090 | } |
6d69c02e ILT |
1091 | return this->make_type(ret); |
1092 | } | |
1093 | ||
7fc2f86b ILT |
1094 | // Fill in the fields of a placeholder struct type. |
1095 | ||
1096 | bool | |
1097 | Gcc_backend::set_placeholder_struct_type( | |
1098 | Btype* placeholder, | |
1099 | const std::vector<Btyped_identifier>& fields) | |
1100 | { | |
1101 | tree t = placeholder->get_tree(); | |
1102 | gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE); | |
1103 | Btype* r = this->fill_in_struct(placeholder, fields); | |
dcf30625 | 1104 | |
7c0434e5 ILT |
1105 | if (TYPE_NAME(t) != NULL_TREE) |
1106 | { | |
1107 | // Build the data structure gcc wants to see for a typedef. | |
eea11d66 | 1108 | tree copy = build_distinct_type_copy(t); |
7c0434e5 ILT |
1109 | TYPE_NAME(copy) = NULL_TREE; |
1110 | DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy; | |
eea11d66 ILT |
1111 | TYPE_SIZE(copy) = NULL_TREE; |
1112 | Btype* bc = this->make_type(copy); | |
1113 | this->fill_in_struct(bc, fields); | |
1114 | delete bc; | |
7c0434e5 | 1115 | } |
dcf30625 | 1116 | |
7fc2f86b ILT |
1117 | return r->get_tree() != error_mark_node; |
1118 | } | |
1119 | ||
1120 | // Create a placeholder for an array type. | |
1121 | ||
1122 | Btype* | |
1123 | Gcc_backend::placeholder_array_type(const std::string& name, | |
8afa2bfb | 1124 | Location location) |
7fc2f86b ILT |
1125 | { |
1126 | tree ret = make_node(ARRAY_TYPE); | |
8afa2bfb | 1127 | tree decl = build_decl(location.gcc_location(), TYPE_DECL, |
7fc2f86b ILT |
1128 | get_identifier_from_string(name), |
1129 | ret); | |
1130 | TYPE_NAME(ret) = decl; | |
1131 | return this->make_type(ret); | |
1132 | } | |
1133 | ||
1134 | // Fill in the fields of a placeholder array type. | |
1135 | ||
1136 | bool | |
1137 | Gcc_backend::set_placeholder_array_type(Btype* placeholder, | |
1138 | Btype* element_btype, | |
1139 | Bexpression* length) | |
1140 | { | |
1141 | tree t = placeholder->get_tree(); | |
1142 | gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE); | |
1143 | Btype* r = this->fill_in_array(placeholder, element_btype, length); | |
dcf30625 ILT |
1144 | |
1145 | // Build the data structure gcc wants to see for a typedef. | |
11304b7b | 1146 | tree copy = build_distinct_type_copy(t); |
dcf30625 ILT |
1147 | TYPE_NAME(copy) = NULL_TREE; |
1148 | DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy; | |
1149 | ||
7fc2f86b ILT |
1150 | return r->get_tree() != error_mark_node; |
1151 | } | |
1152 | ||
1153 | // Return a named version of a type. | |
1154 | ||
1155 | Btype* | |
1156 | Gcc_backend::named_type(const std::string& name, Btype* btype, | |
8afa2bfb | 1157 | Location location) |
7fc2f86b ILT |
1158 | { |
1159 | tree type = btype->get_tree(); | |
1160 | if (type == error_mark_node) | |
1161 | return this->error_type(); | |
11304b7b ILT |
1162 | |
1163 | // The middle-end expects a basic type to have a name. In Go every | |
1164 | // basic type will have a name. The first time we see a basic type, | |
1165 | // give it whatever Go name we have at this point. | |
1166 | if (TYPE_NAME(type) == NULL_TREE | |
1167 | && location.gcc_location() == BUILTINS_LOCATION | |
1168 | && (TREE_CODE(type) == INTEGER_TYPE | |
1169 | || TREE_CODE(type) == REAL_TYPE | |
1170 | || TREE_CODE(type) == COMPLEX_TYPE | |
1171 | || TREE_CODE(type) == BOOLEAN_TYPE)) | |
1172 | { | |
1173 | tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, | |
1174 | get_identifier_from_string(name), | |
1175 | type); | |
1176 | TYPE_NAME(type) = decl; | |
1177 | return this->make_type(type); | |
1178 | } | |
1179 | ||
dcf30625 | 1180 | tree copy = build_variant_type_copy(type); |
8afa2bfb | 1181 | tree decl = build_decl(location.gcc_location(), TYPE_DECL, |
7fc2f86b | 1182 | get_identifier_from_string(name), |
dcf30625 ILT |
1183 | copy); |
1184 | DECL_ORIGINAL_TYPE(decl) = type; | |
1185 | TYPE_NAME(copy) = decl; | |
1186 | return this->make_type(copy); | |
7fc2f86b ILT |
1187 | } |
1188 | ||
1189 | // Return a pointer type used as a marker for a circular type. | |
1190 | ||
1191 | Btype* | |
1192 | Gcc_backend::circular_pointer_type(Btype*, bool) | |
1193 | { | |
1194 | return this->make_type(ptr_type_node); | |
1195 | } | |
1196 | ||
1197 | // Return whether we might be looking at a circular type. | |
1198 | ||
1199 | bool | |
1200 | Gcc_backend::is_circular_pointer_type(Btype* btype) | |
1201 | { | |
1202 | return btype->get_tree() == ptr_type_node; | |
1203 | } | |
1204 | ||
ef1ed13d ILT |
1205 | // Return the size of a type. |
1206 | ||
18fbd1ec | 1207 | int64_t |
ef1ed13d ILT |
1208 | Gcc_backend::type_size(Btype* btype) |
1209 | { | |
08be22dc ILT |
1210 | tree t = btype->get_tree(); |
1211 | if (t == error_mark_node) | |
1212 | return 1; | |
c2a0fe56 ILT |
1213 | if (t == void_type_node) |
1214 | return 0; | |
08be22dc | 1215 | t = TYPE_SIZE_UNIT(t); |
807e902e | 1216 | gcc_assert(tree_fits_uhwi_p (t)); |
ef1ed13d | 1217 | unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t); |
18fbd1ec | 1218 | int64_t ret = static_cast<int64_t>(val_wide); |
ec23e5b3 CM |
1219 | if (ret < 0 || static_cast<unsigned HOST_WIDE_INT>(ret) != val_wide) |
1220 | return -1; | |
ef1ed13d ILT |
1221 | return ret; |
1222 | } | |
1223 | ||
1224 | // Return the alignment of a type. | |
1225 | ||
18fbd1ec | 1226 | int64_t |
ef1ed13d ILT |
1227 | Gcc_backend::type_alignment(Btype* btype) |
1228 | { | |
08be22dc ILT |
1229 | tree t = btype->get_tree(); |
1230 | if (t == error_mark_node) | |
1231 | return 1; | |
1232 | return TYPE_ALIGN_UNIT(t); | |
ef1ed13d ILT |
1233 | } |
1234 | ||
1235 | // Return the alignment of a struct field of type BTYPE. | |
1236 | ||
18fbd1ec | 1237 | int64_t |
ef1ed13d ILT |
1238 | Gcc_backend::type_field_alignment(Btype* btype) |
1239 | { | |
08be22dc ILT |
1240 | tree t = btype->get_tree(); |
1241 | if (t == error_mark_node) | |
1242 | return 1; | |
1243 | return go_field_alignment(t); | |
ef1ed13d ILT |
1244 | } |
1245 | ||
1246 | // Return the offset of a field in a struct. | |
1247 | ||
18fbd1ec | 1248 | int64_t |
ef1ed13d ILT |
1249 | Gcc_backend::type_field_offset(Btype* btype, size_t index) |
1250 | { | |
1251 | tree struct_tree = btype->get_tree(); | |
08be22dc ILT |
1252 | if (struct_tree == error_mark_node) |
1253 | return 0; | |
ef1ed13d ILT |
1254 | gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE); |
1255 | tree field = TYPE_FIELDS(struct_tree); | |
1256 | for (; index > 0; --index) | |
1257 | { | |
1258 | field = DECL_CHAIN(field); | |
1259 | gcc_assert(field != NULL_TREE); | |
1260 | } | |
1261 | HOST_WIDE_INT offset_wide = int_byte_position(field); | |
18fbd1ec ILT |
1262 | int64_t ret = static_cast<int64_t>(offset_wide); |
1263 | gcc_assert(ret == offset_wide); | |
ef1ed13d ILT |
1264 | return ret; |
1265 | } | |
1266 | ||
54466dde ILT |
1267 | // Return the zero value for a type. |
1268 | ||
1269 | Bexpression* | |
1270 | Gcc_backend::zero_expression(Btype* btype) | |
1271 | { | |
1272 | tree t = btype->get_tree(); | |
1273 | tree ret; | |
1274 | if (t == error_mark_node) | |
1275 | ret = error_mark_node; | |
1276 | else | |
1277 | ret = build_zero_cst(t); | |
6f7e0b57 | 1278 | return this->make_expression(ret); |
d4fe7beb CM |
1279 | } |
1280 | ||
1281 | // An expression that references a variable. | |
1282 | ||
1283 | Bexpression* | |
f60bea11 | 1284 | Gcc_backend::var_expression(Bvariable* var, Location location) |
d4fe7beb | 1285 | { |
399f5feb | 1286 | tree ret = var->get_tree(location); |
d4fe7beb CM |
1287 | if (ret == error_mark_node) |
1288 | return this->error_expression(); | |
6f7e0b57 | 1289 | return this->make_expression(ret); |
d4fe7beb CM |
1290 | } |
1291 | ||
1292 | // An expression that indirectly references an expression. | |
1293 | ||
1294 | Bexpression* | |
3e7b0938 CM |
1295 | Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr, |
1296 | bool known_valid, Location location) | |
d4fe7beb | 1297 | { |
3e7b0938 CM |
1298 | tree expr_tree = expr->get_tree(); |
1299 | tree type_tree = btype->get_tree(); | |
1300 | if (expr_tree == error_mark_node || type_tree == error_mark_node) | |
1301 | return this->error_expression(); | |
1302 | ||
1303 | // If the type of EXPR is a recursive pointer type, then we | |
1304 | // need to insert a cast before indirecting. | |
1305 | tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree)); | |
1306 | if (VOID_TYPE_P(target_type_tree)) | |
1307 | expr_tree = fold_convert_loc(location.gcc_location(), | |
1308 | build_pointer_type(type_tree), expr_tree); | |
1309 | ||
d4fe7beb | 1310 | tree ret = build_fold_indirect_ref_loc(location.gcc_location(), |
3e7b0938 | 1311 | expr_tree); |
d4fe7beb CM |
1312 | if (known_valid) |
1313 | TREE_THIS_NOTRAP(ret) = 1; | |
3e7b0938 | 1314 | return this->make_expression(ret); |
002ee4d1 CM |
1315 | } |
1316 | ||
e85baec7 CM |
1317 | // Return an expression that declares a constant named NAME with the |
1318 | // constant value VAL in BTYPE. | |
1319 | ||
1320 | Bexpression* | |
1321 | Gcc_backend::named_constant_expression(Btype* btype, const std::string& name, | |
1322 | Bexpression* val, Location location) | |
1323 | { | |
1324 | tree type_tree = btype->get_tree(); | |
1325 | tree const_val = val->get_tree(); | |
1326 | if (type_tree == error_mark_node || const_val == error_mark_node) | |
1327 | return this->error_expression(); | |
1328 | ||
1329 | tree name_tree = get_identifier_from_string(name); | |
1330 | tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree, | |
1331 | type_tree); | |
1332 | DECL_INITIAL(decl) = const_val; | |
1333 | TREE_CONSTANT(decl) = 1; | |
1334 | TREE_READONLY(decl) = 1; | |
1335 | ||
1336 | go_preserve_from_gc(decl); | |
1337 | return this->make_expression(decl); | |
1338 | } | |
1339 | ||
002ee4d1 CM |
1340 | // Return a typed value as a constant integer. |
1341 | ||
1342 | Bexpression* | |
1343 | Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val) | |
1344 | { | |
1345 | tree t = btype->get_tree(); | |
1346 | if (t == error_mark_node) | |
1347 | return this->error_expression(); | |
1348 | ||
1349 | tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true)); | |
6f7e0b57 | 1350 | return this->make_expression(ret); |
002ee4d1 CM |
1351 | } |
1352 | ||
1353 | // Return a typed value as a constant floating-point number. | |
1354 | ||
1355 | Bexpression* | |
1356 | Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val) | |
1357 | { | |
1358 | tree t = btype->get_tree(); | |
1359 | tree ret; | |
1360 | if (t == error_mark_node) | |
1361 | return this->error_expression(); | |
1362 | ||
1363 | REAL_VALUE_TYPE r1; | |
1364 | real_from_mpfr(&r1, val, t, GMP_RNDN); | |
1365 | REAL_VALUE_TYPE r2; | |
1366 | real_convert(&r2, TYPE_MODE(t), &r1); | |
1367 | ret = build_real(t, r2); | |
6f7e0b57 | 1368 | return this->make_expression(ret); |
002ee4d1 CM |
1369 | } |
1370 | ||
1371 | // Return a typed real and imaginary value as a constant complex number. | |
1372 | ||
1373 | Bexpression* | |
5eda5bad | 1374 | Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val) |
002ee4d1 CM |
1375 | { |
1376 | tree t = btype->get_tree(); | |
1377 | tree ret; | |
1378 | if (t == error_mark_node) | |
1379 | return this->error_expression(); | |
1380 | ||
1381 | REAL_VALUE_TYPE r1; | |
5eda5bad | 1382 | real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN); |
002ee4d1 CM |
1383 | REAL_VALUE_TYPE r2; |
1384 | real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1); | |
1385 | ||
1386 | REAL_VALUE_TYPE r3; | |
5eda5bad | 1387 | real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN); |
002ee4d1 CM |
1388 | REAL_VALUE_TYPE r4; |
1389 | real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3); | |
1390 | ||
1391 | ret = build_complex(t, build_real(TREE_TYPE(t), r2), | |
1392 | build_real(TREE_TYPE(t), r4)); | |
6f7e0b57 | 1393 | return this->make_expression(ret); |
c6d2bfbb CM |
1394 | } |
1395 | ||
7035307e CM |
1396 | // Make a constant string expression. |
1397 | ||
1398 | Bexpression* | |
1399 | Gcc_backend::string_constant_expression(const std::string& val) | |
1400 | { | |
1401 | tree index_type = build_index_type(size_int(val.length())); | |
1402 | tree const_char_type = build_qualified_type(unsigned_char_type_node, | |
1403 | TYPE_QUAL_CONST); | |
1404 | tree string_type = build_array_type(const_char_type, index_type); | |
7035307e CM |
1405 | TYPE_STRING_FLAG(string_type) = 1; |
1406 | tree string_val = build_string(val.length(), val.data()); | |
1407 | TREE_TYPE(string_val) = string_type; | |
1408 | ||
1409 | return this->make_expression(string_val); | |
1410 | } | |
1411 | ||
6f7e0b57 CM |
1412 | // Make a constant boolean expression. |
1413 | ||
1414 | Bexpression* | |
1415 | Gcc_backend::boolean_constant_expression(bool val) | |
1416 | { | |
1417 | tree bool_cst = val ? boolean_true_node : boolean_false_node; | |
1418 | return this->make_expression(bool_cst); | |
1419 | } | |
1420 | ||
7035307e CM |
1421 | // Return the real part of a complex expression. |
1422 | ||
1423 | Bexpression* | |
1424 | Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location) | |
1425 | { | |
1426 | tree complex_tree = bcomplex->get_tree(); | |
1427 | if (complex_tree == error_mark_node) | |
1428 | return this->error_expression(); | |
1429 | gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree))); | |
1430 | tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR, | |
1431 | TREE_TYPE(TREE_TYPE(complex_tree)), | |
1432 | complex_tree); | |
1433 | return this->make_expression(ret); | |
1434 | } | |
1435 | ||
1436 | // Return the imaginary part of a complex expression. | |
1437 | ||
1438 | Bexpression* | |
1439 | Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location) | |
1440 | { | |
1441 | tree complex_tree = bcomplex->get_tree(); | |
1442 | if (complex_tree == error_mark_node) | |
1443 | return this->error_expression(); | |
1444 | gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree))); | |
1445 | tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR, | |
1446 | TREE_TYPE(TREE_TYPE(complex_tree)), | |
1447 | complex_tree); | |
1448 | return this->make_expression(ret); | |
1449 | } | |
1450 | ||
1451 | // Make a complex expression given its real and imaginary parts. | |
1452 | ||
1453 | Bexpression* | |
1454 | Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag, | |
1455 | Location location) | |
1456 | { | |
1457 | tree real_tree = breal->get_tree(); | |
1458 | tree imag_tree = bimag->get_tree(); | |
1459 | if (real_tree == error_mark_node || imag_tree == error_mark_node) | |
1460 | return this->error_expression(); | |
1461 | gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree)) | |
1462 | == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree))); | |
1463 | gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree))); | |
1464 | tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR, | |
1465 | build_complex_type(TREE_TYPE(real_tree)), | |
1466 | real_tree, imag_tree); | |
1467 | return this->make_expression(ret); | |
1468 | } | |
1469 | ||
c6d2bfbb CM |
1470 | // An expression that converts an expression to a different type. |
1471 | ||
1472 | Bexpression* | |
7035307e CM |
1473 | Gcc_backend::convert_expression(Btype* type, Bexpression* expr, |
1474 | Location location) | |
c6d2bfbb CM |
1475 | { |
1476 | tree type_tree = type->get_tree(); | |
1477 | tree expr_tree = expr->get_tree(); | |
7035307e CM |
1478 | if (type_tree == error_mark_node |
1479 | || expr_tree == error_mark_node | |
1480 | || TREE_TYPE(expr_tree) == error_mark_node) | |
c6d2bfbb CM |
1481 | return this->error_expression(); |
1482 | ||
7035307e | 1483 | tree ret; |
c2a0fe56 ILT |
1484 | if (this->type_size(type) == 0 |
1485 | || TREE_TYPE(expr_tree) == void_type_node) | |
7035307e CM |
1486 | { |
1487 | // Do not convert zero-sized types. | |
1488 | ret = expr_tree; | |
1489 | } | |
1490 | else if (TREE_CODE(type_tree) == INTEGER_TYPE) | |
1491 | ret = fold(convert_to_integer(type_tree, expr_tree)); | |
1492 | else if (TREE_CODE(type_tree) == REAL_TYPE) | |
1493 | ret = fold(convert_to_real(type_tree, expr_tree)); | |
1494 | else if (TREE_CODE(type_tree) == COMPLEX_TYPE) | |
1495 | ret = fold(convert_to_complex(type_tree, expr_tree)); | |
1496 | else if (TREE_CODE(type_tree) == POINTER_TYPE | |
1497 | && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE) | |
1498 | ret = fold(convert_to_pointer(type_tree, expr_tree)); | |
1499 | else if (TREE_CODE(type_tree) == RECORD_TYPE | |
1500 | || TREE_CODE(type_tree) == ARRAY_TYPE) | |
1501 | ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR, | |
1502 | type_tree, expr_tree); | |
1503 | else | |
1504 | ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree); | |
1505 | ||
1506 | return this->make_expression(ret); | |
54466dde ILT |
1507 | } |
1508 | ||
b7d93b46 CM |
1509 | // Get the address of a function. |
1510 | ||
1511 | Bexpression* | |
1512 | Gcc_backend::function_code_expression(Bfunction* bfunc, Location location) | |
1513 | { | |
1514 | tree func = bfunc->get_tree(); | |
1515 | if (func == error_mark_node) | |
1516 | return this->error_expression(); | |
1517 | ||
1518 | tree ret = build_fold_addr_expr_loc(location.gcc_location(), func); | |
1519 | return this->make_expression(ret); | |
1520 | } | |
1521 | ||
b93e0cfd CM |
1522 | // Get the address of an expression. |
1523 | ||
1524 | Bexpression* | |
1525 | Gcc_backend::address_expression(Bexpression* bexpr, Location location) | |
1526 | { | |
1527 | tree expr = bexpr->get_tree(); | |
1528 | if (expr == error_mark_node) | |
1529 | return this->error_expression(); | |
1530 | ||
1531 | tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr); | |
1532 | return this->make_expression(ret); | |
1533 | } | |
1534 | ||
71eba7a0 CM |
1535 | // Return an expression for the field at INDEX in BSTRUCT. |
1536 | ||
1537 | Bexpression* | |
1538 | Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index, | |
1539 | Location location) | |
1540 | { | |
1541 | tree struct_tree = bstruct->get_tree(); | |
1542 | if (struct_tree == error_mark_node | |
1543 | || TREE_TYPE(struct_tree) == error_mark_node) | |
1544 | return this->error_expression(); | |
1545 | gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE); | |
1546 | tree field = TYPE_FIELDS(TREE_TYPE(struct_tree)); | |
1547 | if (field == NULL_TREE) | |
1548 | { | |
1549 | // This can happen for a type which refers to itself indirectly | |
1550 | // and then turns out to be erroneous. | |
1551 | return this->error_expression(); | |
1552 | } | |
1553 | for (unsigned int i = index; i > 0; --i) | |
1554 | { | |
1555 | field = DECL_CHAIN(field); | |
1556 | gcc_assert(field != NULL_TREE); | |
1557 | } | |
1558 | if (TREE_TYPE(field) == error_mark_node) | |
1559 | return this->error_expression(); | |
1560 | tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF, | |
1561 | TREE_TYPE(field), struct_tree, field, | |
1562 | NULL_TREE); | |
1563 | if (TREE_CONSTANT(struct_tree)) | |
1564 | TREE_CONSTANT(ret) = 1; | |
6f7e0b57 | 1565 | return this->make_expression(ret); |
71eba7a0 CM |
1566 | } |
1567 | ||
eb6eb862 CM |
1568 | // Return an expression that executes BSTAT before BEXPR. |
1569 | ||
1570 | Bexpression* | |
1571 | Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr, | |
1572 | Location location) | |
1573 | { | |
1574 | tree stat = bstat->get_tree(); | |
1575 | tree expr = bexpr->get_tree(); | |
1576 | if (stat == error_mark_node || expr == error_mark_node) | |
1577 | return this->error_expression(); | |
1578 | tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, | |
1579 | TREE_TYPE(expr), stat, expr); | |
1580 | return this->make_expression(ret); | |
1581 | } | |
1582 | ||
1583 | // Return an expression that executes THEN_EXPR if CONDITION is true, or | |
1584 | // ELSE_EXPR otherwise. | |
1585 | ||
1586 | Bexpression* | |
a2182c9c TM |
1587 | Gcc_backend::conditional_expression(Bfunction*, Btype* btype, |
1588 | Bexpression* condition, | |
eb6eb862 CM |
1589 | Bexpression* then_expr, |
1590 | Bexpression* else_expr, Location location) | |
1591 | { | |
b5407ad1 | 1592 | tree type_tree = btype == NULL ? void_type_node : btype->get_tree(); |
eb6eb862 CM |
1593 | tree cond_tree = condition->get_tree(); |
1594 | tree then_tree = then_expr->get_tree(); | |
1595 | tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree(); | |
b5407ad1 CM |
1596 | if (type_tree == error_mark_node |
1597 | || cond_tree == error_mark_node | |
eb6eb862 CM |
1598 | || then_tree == error_mark_node |
1599 | || else_tree == error_mark_node) | |
1600 | return this->error_expression(); | |
b5407ad1 | 1601 | tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree, |
eb6eb862 CM |
1602 | cond_tree, then_tree, else_tree); |
1603 | return this->make_expression(ret); | |
1604 | } | |
1605 | ||
8a35e18d CM |
1606 | // Return an expression for the unary operation OP EXPR. |
1607 | ||
1608 | Bexpression* | |
1609 | Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location) | |
1610 | { | |
1611 | tree expr_tree = expr->get_tree(); | |
1612 | if (expr_tree == error_mark_node | |
1613 | || TREE_TYPE(expr_tree) == error_mark_node) | |
1614 | return this->error_expression(); | |
1615 | ||
1616 | tree type_tree = TREE_TYPE(expr_tree); | |
1617 | enum tree_code code; | |
1618 | switch (op) | |
1619 | { | |
1620 | case OPERATOR_MINUS: | |
1621 | { | |
1622 | tree computed_type = excess_precision_type(type_tree); | |
1623 | if (computed_type != NULL_TREE) | |
1624 | { | |
1625 | expr_tree = convert(computed_type, expr_tree); | |
1626 | type_tree = computed_type; | |
1627 | } | |
1628 | code = NEGATE_EXPR; | |
1629 | break; | |
1630 | } | |
1631 | case OPERATOR_NOT: | |
1632 | code = TRUTH_NOT_EXPR; | |
1633 | break; | |
1634 | case OPERATOR_XOR: | |
1635 | code = BIT_NOT_EXPR; | |
1636 | break; | |
1637 | default: | |
1638 | gcc_unreachable(); | |
1639 | break; | |
1640 | } | |
1641 | ||
1642 | tree ret = fold_build1_loc(location.gcc_location(), code, type_tree, | |
1643 | expr_tree); | |
1644 | return this->make_expression(ret); | |
1645 | } | |
1646 | ||
b5407ad1 CM |
1647 | // Convert a gofrontend operator to an equivalent tree_code. |
1648 | ||
1649 | static enum tree_code | |
1650 | operator_to_tree_code(Operator op, tree type) | |
1651 | { | |
1652 | enum tree_code code; | |
1653 | switch (op) | |
1654 | { | |
1655 | case OPERATOR_EQEQ: | |
1656 | code = EQ_EXPR; | |
1657 | break; | |
1658 | case OPERATOR_NOTEQ: | |
1659 | code = NE_EXPR; | |
1660 | break; | |
1661 | case OPERATOR_LT: | |
1662 | code = LT_EXPR; | |
1663 | break; | |
1664 | case OPERATOR_LE: | |
1665 | code = LE_EXPR; | |
1666 | break; | |
1667 | case OPERATOR_GT: | |
1668 | code = GT_EXPR; | |
1669 | break; | |
1670 | case OPERATOR_GE: | |
1671 | code = GE_EXPR; | |
1672 | break; | |
1673 | case OPERATOR_OROR: | |
1674 | code = TRUTH_ORIF_EXPR; | |
1675 | break; | |
1676 | case OPERATOR_ANDAND: | |
1677 | code = TRUTH_ANDIF_EXPR; | |
1678 | break; | |
1679 | case OPERATOR_PLUS: | |
1680 | code = PLUS_EXPR; | |
1681 | break; | |
1682 | case OPERATOR_MINUS: | |
1683 | code = MINUS_EXPR; | |
1684 | break; | |
1685 | case OPERATOR_OR: | |
1686 | code = BIT_IOR_EXPR; | |
1687 | break; | |
1688 | case OPERATOR_XOR: | |
1689 | code = BIT_XOR_EXPR; | |
1690 | break; | |
1691 | case OPERATOR_MULT: | |
1692 | code = MULT_EXPR; | |
1693 | break; | |
1694 | case OPERATOR_DIV: | |
1695 | if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE) | |
1696 | code = RDIV_EXPR; | |
1697 | else | |
1698 | code = TRUNC_DIV_EXPR; | |
1699 | break; | |
1700 | case OPERATOR_MOD: | |
1701 | code = TRUNC_MOD_EXPR; | |
1702 | break; | |
1703 | case OPERATOR_LSHIFT: | |
1704 | code = LSHIFT_EXPR; | |
1705 | break; | |
1706 | case OPERATOR_RSHIFT: | |
1707 | code = RSHIFT_EXPR; | |
1708 | break; | |
1709 | case OPERATOR_AND: | |
1710 | code = BIT_AND_EXPR; | |
1711 | break; | |
1712 | case OPERATOR_BITCLEAR: | |
1713 | code = BIT_AND_EXPR; | |
1714 | break; | |
1715 | default: | |
1716 | gcc_unreachable(); | |
1717 | } | |
1718 | ||
1719 | return code; | |
1720 | } | |
1721 | ||
1722 | // Return an expression for the binary operation LEFT OP RIGHT. | |
1723 | ||
1724 | Bexpression* | |
1725 | Gcc_backend::binary_expression(Operator op, Bexpression* left, | |
1726 | Bexpression* right, Location location) | |
1727 | { | |
1728 | tree left_tree = left->get_tree(); | |
1729 | tree right_tree = right->get_tree(); | |
1730 | if (left_tree == error_mark_node | |
1731 | || right_tree == error_mark_node) | |
1732 | return this->error_expression(); | |
1733 | enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree)); | |
1734 | ||
1735 | bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND; | |
1736 | tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree); | |
1737 | tree computed_type = excess_precision_type(type_tree); | |
1738 | if (computed_type != NULL_TREE) | |
1739 | { | |
1740 | left_tree = convert(computed_type, left_tree); | |
1741 | right_tree = convert(computed_type, right_tree); | |
1742 | type_tree = computed_type; | |
1743 | } | |
1744 | ||
1745 | // For comparison operators, the resulting type should be boolean. | |
1746 | switch (op) | |
1747 | { | |
1748 | case OPERATOR_EQEQ: | |
1749 | case OPERATOR_NOTEQ: | |
1750 | case OPERATOR_LT: | |
1751 | case OPERATOR_LE: | |
1752 | case OPERATOR_GT: | |
1753 | case OPERATOR_GE: | |
1754 | type_tree = boolean_type_node; | |
1755 | break; | |
1756 | default: | |
1757 | break; | |
1758 | } | |
1759 | ||
1760 | tree ret = fold_build2_loc(location.gcc_location(), code, type_tree, | |
1761 | left_tree, right_tree); | |
1762 | return this->make_expression(ret); | |
1763 | } | |
1764 | ||
7035307e CM |
1765 | // Return an expression that constructs BTYPE with VALS. |
1766 | ||
1767 | Bexpression* | |
1768 | Gcc_backend::constructor_expression(Btype* btype, | |
1769 | const std::vector<Bexpression*>& vals, | |
1770 | Location location) | |
1771 | { | |
1772 | tree type_tree = btype->get_tree(); | |
1773 | if (type_tree == error_mark_node) | |
1774 | return this->error_expression(); | |
1775 | ||
1776 | vec<constructor_elt, va_gc> *init; | |
1777 | vec_alloc(init, vals.size()); | |
1778 | ||
861393ac | 1779 | tree sink = NULL_TREE; |
7035307e CM |
1780 | bool is_constant = true; |
1781 | tree field = TYPE_FIELDS(type_tree); | |
1782 | for (std::vector<Bexpression*>::const_iterator p = vals.begin(); | |
1783 | p != vals.end(); | |
1784 | ++p, field = DECL_CHAIN(field)) | |
1785 | { | |
1786 | gcc_assert(field != NULL_TREE); | |
1787 | tree val = (*p)->get_tree(); | |
1788 | if (TREE_TYPE(field) == error_mark_node | |
1789 | || val == error_mark_node | |
1790 | || TREE_TYPE(val) == error_mark_node) | |
1791 | return this->error_expression(); | |
1792 | ||
861393ac CM |
1793 | if (int_size_in_bytes(TREE_TYPE(field)) == 0) |
1794 | { | |
1795 | // GIMPLE cannot represent indices of zero-sized types so | |
1796 | // trying to construct a map with zero-sized keys might lead | |
1797 | // to errors. Instead, we evaluate each expression that | |
1798 | // would have been added as a map element for its | |
1799 | // side-effects and construct an empty map. | |
1800 | append_to_statement_list(val, &sink); | |
1801 | continue; | |
1802 | } | |
1803 | ||
7035307e CM |
1804 | constructor_elt empty = {NULL, NULL}; |
1805 | constructor_elt* elt = init->quick_push(empty); | |
1806 | elt->index = field; | |
ebc2f401 | 1807 | elt->value = this->convert_tree(TREE_TYPE(field), val, location); |
7035307e CM |
1808 | if (!TREE_CONSTANT(elt->value)) |
1809 | is_constant = false; | |
1810 | } | |
1811 | gcc_assert(field == NULL_TREE); | |
1812 | tree ret = build_constructor(type_tree, init); | |
1813 | if (is_constant) | |
1814 | TREE_CONSTANT(ret) = 1; | |
861393ac CM |
1815 | if (sink != NULL_TREE) |
1816 | ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, | |
1817 | type_tree, sink, ret); | |
7035307e CM |
1818 | return this->make_expression(ret); |
1819 | } | |
1820 | ||
1821 | Bexpression* | |
1822 | Gcc_backend::array_constructor_expression( | |
1823 | Btype* array_btype, const std::vector<unsigned long>& indexes, | |
943cf9cf | 1824 | const std::vector<Bexpression*>& vals, Location location) |
7035307e CM |
1825 | { |
1826 | tree type_tree = array_btype->get_tree(); | |
1827 | if (type_tree == error_mark_node) | |
1828 | return this->error_expression(); | |
1829 | ||
1830 | gcc_assert(indexes.size() == vals.size()); | |
943cf9cf CM |
1831 | |
1832 | tree element_type = TREE_TYPE(type_tree); | |
1833 | HOST_WIDE_INT element_size = int_size_in_bytes(element_type); | |
7035307e | 1834 | vec<constructor_elt, va_gc> *init; |
943cf9cf | 1835 | vec_alloc(init, element_size == 0 ? 0 : vals.size()); |
7035307e | 1836 | |
943cf9cf | 1837 | tree sink = NULL_TREE; |
7035307e CM |
1838 | bool is_constant = true; |
1839 | for (size_t i = 0; i < vals.size(); ++i) | |
1840 | { | |
1841 | tree index = size_int(indexes[i]); | |
1842 | tree val = (vals[i])->get_tree(); | |
1843 | ||
1844 | if (index == error_mark_node | |
1845 | || val == error_mark_node) | |
1846 | return this->error_expression(); | |
1847 | ||
943cf9cf CM |
1848 | if (element_size == 0) |
1849 | { | |
1850 | // GIMPLE cannot represent arrays of zero-sized types so trying | |
1851 | // to construct an array of zero-sized values might lead to errors. | |
1852 | // Instead, we evaluate each expression that would have been added as | |
1853 | // an array value for its side-effects and construct an empty array. | |
1854 | append_to_statement_list(val, &sink); | |
1855 | continue; | |
1856 | } | |
1857 | ||
7035307e CM |
1858 | if (!TREE_CONSTANT(val)) |
1859 | is_constant = false; | |
1860 | ||
1861 | constructor_elt empty = {NULL, NULL}; | |
1862 | constructor_elt* elt = init->quick_push(empty); | |
1863 | elt->index = index; | |
1864 | elt->value = val; | |
1865 | } | |
1866 | ||
1867 | tree ret = build_constructor(type_tree, init); | |
1868 | if (is_constant) | |
1869 | TREE_CONSTANT(ret) = 1; | |
943cf9cf CM |
1870 | if (sink != NULL_TREE) |
1871 | ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, | |
1872 | type_tree, sink, ret); | |
7035307e CM |
1873 | return this->make_expression(ret); |
1874 | } | |
1875 | ||
1876 | // Return an expression for the address of BASE[INDEX]. | |
1877 | ||
1878 | Bexpression* | |
1879 | Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index, | |
1880 | Location location) | |
1881 | { | |
1882 | tree base_tree = base->get_tree(); | |
1883 | tree index_tree = index->get_tree(); | |
1884 | tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree)); | |
1885 | if (base_tree == error_mark_node | |
1886 | || TREE_TYPE(base_tree) == error_mark_node | |
1887 | || index_tree == error_mark_node | |
1888 | || element_type_tree == error_mark_node) | |
1889 | return this->error_expression(); | |
1890 | ||
1891 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); | |
1892 | index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree); | |
1893 | tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype, | |
1894 | index_tree, element_size); | |
1895 | tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR, | |
1896 | TREE_TYPE(base_tree), base_tree, offset); | |
1897 | return this->make_expression(ptr); | |
1898 | } | |
1899 | ||
1900 | // Return an expression representing ARRAY[INDEX] | |
1901 | ||
1902 | Bexpression* | |
1903 | Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index, | |
1904 | Location location) | |
1905 | { | |
1906 | tree array_tree = array->get_tree(); | |
1907 | tree index_tree = index->get_tree(); | |
1908 | if (array_tree == error_mark_node | |
1909 | || TREE_TYPE(array_tree) == error_mark_node | |
1910 | || index_tree == error_mark_node) | |
1911 | return this->error_expression(); | |
1912 | ||
c2a0fe56 ILT |
1913 | // A function call that returns a zero sized object will have been |
1914 | // changed to return void. If we see void here, assume we are | |
1915 | // dealing with a zero sized type and just evaluate the operands. | |
1916 | tree ret; | |
1917 | if (TREE_TYPE(array_tree) != void_type_node) | |
1918 | ret = build4_loc(location.gcc_location(), ARRAY_REF, | |
1919 | TREE_TYPE(TREE_TYPE(array_tree)), array_tree, | |
1920 | index_tree, NULL_TREE, NULL_TREE); | |
1921 | else | |
1922 | ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, | |
1923 | void_type_node, array_tree, index_tree); | |
1924 | ||
7035307e CM |
1925 | return this->make_expression(ret); |
1926 | } | |
1927 | ||
1928 | // Create an expression for a call to FN_EXPR with FN_ARGS. | |
1929 | Bexpression* | |
da55a299 TM |
1930 | Gcc_backend::call_expression(Bfunction*, // containing fcn for call |
1931 | Bexpression* fn_expr, | |
7035307e | 1932 | const std::vector<Bexpression*>& fn_args, |
da55a299 TM |
1933 | Bexpression* chain_expr, |
1934 | Location location) | |
7035307e CM |
1935 | { |
1936 | tree fn = fn_expr->get_tree(); | |
1937 | if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node) | |
1938 | return this->error_expression(); | |
1939 | ||
1940 | gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn))); | |
1941 | tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn))); | |
1942 | ||
1943 | size_t nargs = fn_args.size(); | |
1944 | tree* args = nargs == 0 ? NULL : new tree[nargs]; | |
1945 | for (size_t i = 0; i < nargs; ++i) | |
1946 | { | |
1947 | args[i] = fn_args.at(i)->get_tree(); | |
1948 | if (args[i] == error_mark_node) | |
1949 | return this->error_expression(); | |
1950 | } | |
1951 | ||
1952 | tree fndecl = fn; | |
1953 | if (TREE_CODE(fndecl) == ADDR_EXPR) | |
1954 | fndecl = TREE_OPERAND(fndecl, 0); | |
1955 | ||
1956 | // This is to support builtin math functions when using 80387 math. | |
1957 | tree excess_type = NULL_TREE; | |
1958 | if (optimize | |
1959 | && TREE_CODE(fndecl) == FUNCTION_DECL | |
3d78e008 ML |
1960 | && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL) |
1961 | && DECL_IS_BUILTIN (fndecl) | |
7035307e CM |
1962 | && nargs > 0 |
1963 | && ((SCALAR_FLOAT_TYPE_P(rettype) | |
1964 | && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0]))) | |
1965 | || (COMPLEX_FLOAT_TYPE_P(rettype) | |
1966 | && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0]))))) | |
1967 | { | |
1968 | excess_type = excess_precision_type(TREE_TYPE(args[0])); | |
1969 | if (excess_type != NULL_TREE) | |
1970 | { | |
1971 | tree excess_fndecl = mathfn_built_in(excess_type, | |
1972 | DECL_FUNCTION_CODE(fndecl)); | |
1973 | if (excess_fndecl == NULL_TREE) | |
1974 | excess_type = NULL_TREE; | |
1975 | else | |
1976 | { | |
1977 | fn = build_fold_addr_expr_loc(location.gcc_location(), | |
1978 | excess_fndecl); | |
1979 | for (size_t i = 0; i < nargs; ++i) | |
1980 | { | |
1981 | if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i])) | |
1982 | || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i]))) | |
1983 | args[i] = ::convert(excess_type, args[i]); | |
1984 | } | |
1985 | } | |
1986 | } | |
1987 | } | |
1988 | ||
1989 | tree ret = | |
1990 | build_call_array_loc(location.gcc_location(), | |
1991 | excess_type != NULL_TREE ? excess_type : rettype, | |
1992 | fn, nargs, args); | |
1993 | ||
38bf819a RH |
1994 | if (chain_expr) |
1995 | CALL_EXPR_STATIC_CHAIN (ret) = chain_expr->get_tree(); | |
1996 | ||
7035307e CM |
1997 | if (excess_type != NULL_TREE) |
1998 | { | |
1999 | // Calling convert here can undo our excess precision change. | |
2000 | // That may or may not be a bug in convert_to_real. | |
2001 | ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret); | |
2002 | } | |
2003 | ||
2004 | delete[] args; | |
2005 | return this->make_expression(ret); | |
2006 | } | |
2007 | ||
cfebcf30 ILT |
2008 | // An expression as a statement. |
2009 | ||
2010 | Bstatement* | |
8220e3f9 | 2011 | Gcc_backend::expression_statement(Bfunction*, Bexpression* expr) |
cfebcf30 ILT |
2012 | { |
2013 | return this->make_statement(expr->get_tree()); | |
2014 | } | |
2015 | ||
e09ce6c5 ILT |
2016 | // Variable initialization. |
2017 | ||
2018 | Bstatement* | |
8220e3f9 | 2019 | Gcc_backend::init_statement(Bfunction*, Bvariable* var, Bexpression* init) |
e09ce6c5 | 2020 | { |
399f5feb | 2021 | tree var_tree = var->get_decl(); |
e09ce6c5 ILT |
2022 | tree init_tree = init->get_tree(); |
2023 | if (var_tree == error_mark_node || init_tree == error_mark_node) | |
2024 | return this->error_statement(); | |
2025 | gcc_assert(TREE_CODE(var_tree) == VAR_DECL); | |
7d6be4c8 ILT |
2026 | |
2027 | // To avoid problems with GNU ld, we don't make zero-sized | |
2028 | // externally visible variables. That might lead us to doing an | |
2029 | // initialization of a zero-sized expression to a non-zero sized | |
2030 | // variable, or vice-versa. Avoid crashes by omitting the | |
2031 | // initializer. Such initializations don't mean anything anyhow. | |
2032 | if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0 | |
2033 | && init_tree != NULL_TREE | |
c2a0fe56 | 2034 | && TREE_TYPE(init_tree) != void_type_node |
7d6be4c8 ILT |
2035 | && int_size_in_bytes(TREE_TYPE(init_tree)) != 0) |
2036 | { | |
2037 | DECL_INITIAL(var_tree) = init_tree; | |
2038 | init_tree = NULL_TREE; | |
2039 | } | |
2040 | ||
2041 | tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR, | |
2042 | void_type_node, var_tree); | |
2043 | if (init_tree != NULL_TREE) | |
2044 | ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR, | |
2045 | void_type_node, init_tree, ret); | |
2046 | ||
2047 | return this->make_statement(ret); | |
e09ce6c5 ILT |
2048 | } |
2049 | ||
a9ac13f7 ILT |
2050 | // Assignment. |
2051 | ||
2052 | Bstatement* | |
8220e3f9 TM |
2053 | Gcc_backend::assignment_statement(Bfunction* bfn, Bexpression* lhs, |
2054 | Bexpression* rhs, Location location) | |
a9ac13f7 | 2055 | { |
94039447 ILT |
2056 | tree lhs_tree = lhs->get_tree(); |
2057 | tree rhs_tree = rhs->get_tree(); | |
2058 | if (lhs_tree == error_mark_node || rhs_tree == error_mark_node) | |
00b44a6e | 2059 | return this->error_statement(); |
7d6be4c8 ILT |
2060 | |
2061 | // To avoid problems with GNU ld, we don't make zero-sized | |
2062 | // externally visible variables. That might lead us to doing an | |
2063 | // assignment of a zero-sized expression to a non-zero sized | |
2064 | // expression; avoid crashes here by avoiding assignments of | |
2065 | // zero-sized expressions. Such assignments don't really mean | |
2066 | // anything anyhow. | |
c2a0fe56 ILT |
2067 | if (TREE_TYPE(lhs_tree) == void_type_node |
2068 | || int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0 | |
2069 | || TREE_TYPE(rhs_tree) == void_type_node | |
7d6be4c8 | 2070 | || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0) |
8220e3f9 TM |
2071 | return this->compound_statement(this->expression_statement(bfn, lhs), |
2072 | this->expression_statement(bfn, rhs)); | |
7d6be4c8 | 2073 | |
ebc2f401 | 2074 | rhs_tree = this->convert_tree(TREE_TYPE(lhs_tree), rhs_tree, location); |
68c5d97b | 2075 | |
8afa2bfb SD |
2076 | return this->make_statement(fold_build2_loc(location.gcc_location(), |
2077 | MODIFY_EXPR, | |
a9ac13f7 | 2078 | void_type_node, |
94039447 ILT |
2079 | lhs_tree, rhs_tree)); |
2080 | } | |
2081 | ||
2082 | // Return. | |
2083 | ||
2084 | Bstatement* | |
2085 | Gcc_backend::return_statement(Bfunction* bfunction, | |
2086 | const std::vector<Bexpression*>& vals, | |
8afa2bfb | 2087 | Location location) |
94039447 ILT |
2088 | { |
2089 | tree fntree = bfunction->get_tree(); | |
2090 | if (fntree == error_mark_node) | |
00b44a6e | 2091 | return this->error_statement(); |
94039447 ILT |
2092 | tree result = DECL_RESULT(fntree); |
2093 | if (result == error_mark_node) | |
00b44a6e | 2094 | return this->error_statement(); |
036165d8 | 2095 | |
f432d128 ILT |
2096 | // If the result size is zero bytes, we have set the function type |
2097 | // to have a result type of void, so don't return anything. | |
2098 | // See the function_type method. | |
1f599d75 TM |
2099 | tree res_type = TREE_TYPE(result); |
2100 | if (res_type == void_type_node || int_size_in_bytes(res_type) == 0) | |
f432d128 ILT |
2101 | { |
2102 | tree stmt_list = NULL_TREE; | |
2103 | for (std::vector<Bexpression*>::const_iterator p = vals.begin(); | |
2104 | p != vals.end(); | |
2105 | p++) | |
2106 | { | |
2107 | tree val = (*p)->get_tree(); | |
2108 | if (val == error_mark_node) | |
2109 | return this->error_statement(); | |
2110 | append_to_statement_list(val, &stmt_list); | |
2111 | } | |
2112 | tree ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, | |
2113 | void_type_node, NULL_TREE); | |
2114 | append_to_statement_list(ret, &stmt_list); | |
2115 | return this->make_statement(stmt_list); | |
2116 | } | |
2117 | ||
94039447 ILT |
2118 | tree ret; |
2119 | if (vals.empty()) | |
8afa2bfb SD |
2120 | ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node, |
2121 | NULL_TREE); | |
94039447 ILT |
2122 | else if (vals.size() == 1) |
2123 | { | |
2124 | tree val = vals.front()->get_tree(); | |
2125 | if (val == error_mark_node) | |
00b44a6e | 2126 | return this->error_statement(); |
8afa2bfb SD |
2127 | tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR, |
2128 | void_type_node, result, | |
2129 | vals.front()->get_tree()); | |
2130 | ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, | |
2131 | void_type_node, set); | |
94039447 ILT |
2132 | } |
2133 | else | |
2134 | { | |
2135 | // To return multiple values, copy the values into a temporary | |
2136 | // variable of the right structure type, and then assign the | |
2137 | // temporary variable to the DECL_RESULT in the return | |
2138 | // statement. | |
2139 | tree stmt_list = NULL_TREE; | |
2140 | tree rettype = TREE_TYPE(result); | |
036165d8 CM |
2141 | |
2142 | if (DECL_STRUCT_FUNCTION(fntree) == NULL) | |
2143 | push_struct_function(fntree); | |
2144 | else | |
2145 | push_cfun(DECL_STRUCT_FUNCTION(fntree)); | |
94039447 | 2146 | tree rettmp = create_tmp_var(rettype, "RESULT"); |
036165d8 CM |
2147 | pop_cfun(); |
2148 | ||
94039447 ILT |
2149 | tree field = TYPE_FIELDS(rettype); |
2150 | for (std::vector<Bexpression*>::const_iterator p = vals.begin(); | |
2151 | p != vals.end(); | |
2152 | p++, field = DECL_CHAIN(field)) | |
2153 | { | |
2154 | gcc_assert(field != NULL_TREE); | |
8afa2bfb SD |
2155 | tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF, |
2156 | TREE_TYPE(field), rettmp, field, | |
2157 | NULL_TREE); | |
94039447 ILT |
2158 | tree val = (*p)->get_tree(); |
2159 | if (val == error_mark_node) | |
00b44a6e | 2160 | return this->error_statement(); |
8afa2bfb SD |
2161 | tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR, |
2162 | void_type_node, | |
94039447 ILT |
2163 | ref, (*p)->get_tree()); |
2164 | append_to_statement_list(set, &stmt_list); | |
2165 | } | |
2166 | gcc_assert(field == NULL_TREE); | |
8afa2bfb SD |
2167 | tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR, |
2168 | void_type_node, | |
94039447 | 2169 | result, rettmp); |
8afa2bfb SD |
2170 | tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR, |
2171 | void_type_node, set); | |
94039447 ILT |
2172 | append_to_statement_list(ret_expr, &stmt_list); |
2173 | ret = stmt_list; | |
2174 | } | |
2175 | return this->make_statement(ret); | |
a9ac13f7 ILT |
2176 | } |
2177 | ||
7035307e CM |
2178 | // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an |
2179 | // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not | |
2180 | // NULL, it will always be executed. This is used for handling defers in Go | |
2181 | // functions. In C++, the resulting code is of this form: | |
2182 | // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; } | |
2183 | ||
2184 | Bstatement* | |
2185 | Gcc_backend::exception_handler_statement(Bstatement* bstat, | |
2186 | Bstatement* except_stmt, | |
2187 | Bstatement* finally_stmt, | |
2188 | Location location) | |
2189 | { | |
2190 | tree stat_tree = bstat->get_tree(); | |
2191 | tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree(); | |
2192 | tree finally_tree = finally_stmt == NULL | |
2193 | ? NULL_TREE | |
2194 | : finally_stmt->get_tree(); | |
2195 | ||
2196 | if (stat_tree == error_mark_node | |
2197 | || except_tree == error_mark_node | |
2198 | || finally_tree == error_mark_node) | |
2199 | return this->error_statement(); | |
2200 | ||
2201 | if (except_tree != NULL_TREE) | |
2202 | stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR, | |
2203 | void_type_node, stat_tree, | |
2204 | build2_loc(location.gcc_location(), CATCH_EXPR, | |
2205 | void_type_node, NULL, except_tree)); | |
2206 | if (finally_tree != NULL_TREE) | |
2207 | stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR, | |
2208 | void_type_node, stat_tree, finally_tree); | |
2209 | return this->make_statement(stat_tree); | |
2210 | } | |
2211 | ||
db0adf82 ILT |
2212 | // If. |
2213 | ||
2214 | Bstatement* | |
8220e3f9 TM |
2215 | Gcc_backend::if_statement(Bfunction*, Bexpression* condition, |
2216 | Bblock* then_block, Bblock* else_block, | |
2217 | Location location) | |
db0adf82 ILT |
2218 | { |
2219 | tree cond_tree = condition->get_tree(); | |
2220 | tree then_tree = then_block->get_tree(); | |
2221 | tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree(); | |
2222 | if (cond_tree == error_mark_node | |
2223 | || then_tree == error_mark_node | |
2224 | || else_tree == error_mark_node) | |
00b44a6e | 2225 | return this->error_statement(); |
8afa2bfb SD |
2226 | tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node, |
2227 | cond_tree, then_tree, else_tree); | |
db0adf82 ILT |
2228 | return this->make_statement(ret); |
2229 | } | |
2230 | ||
8d0b03a2 ILT |
2231 | // Switch. |
2232 | ||
2233 | Bstatement* | |
2234 | Gcc_backend::switch_statement( | |
036165d8 | 2235 | Bfunction* function, |
8d0b03a2 ILT |
2236 | Bexpression* value, |
2237 | const std::vector<std::vector<Bexpression*> >& cases, | |
2238 | const std::vector<Bstatement*>& statements, | |
8afa2bfb | 2239 | Location switch_location) |
8d0b03a2 ILT |
2240 | { |
2241 | gcc_assert(cases.size() == statements.size()); | |
2242 | ||
036165d8 CM |
2243 | tree decl = function->get_tree(); |
2244 | if (DECL_STRUCT_FUNCTION(decl) == NULL) | |
2245 | push_struct_function(decl); | |
2246 | else | |
2247 | push_cfun(DECL_STRUCT_FUNCTION(decl)); | |
2248 | ||
8d0b03a2 ILT |
2249 | tree stmt_list = NULL_TREE; |
2250 | std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin(); | |
2251 | for (std::vector<Bstatement*>::const_iterator ps = statements.begin(); | |
2252 | ps != statements.end(); | |
2253 | ++ps, ++pc) | |
2254 | { | |
2255 | if (pc->empty()) | |
2256 | { | |
620e594b DM |
2257 | location_t loc = (*ps != NULL |
2258 | ? EXPR_LOCATION((*ps)->get_tree()) | |
2259 | : UNKNOWN_LOCATION); | |
8d0b03a2 | 2260 | tree label = create_artificial_label(loc); |
3d528853 | 2261 | tree c = build_case_label(NULL_TREE, NULL_TREE, label); |
8d0b03a2 ILT |
2262 | append_to_statement_list(c, &stmt_list); |
2263 | } | |
2264 | else | |
2265 | { | |
2266 | for (std::vector<Bexpression*>::const_iterator pcv = pc->begin(); | |
2267 | pcv != pc->end(); | |
2268 | ++pcv) | |
2269 | { | |
2270 | tree t = (*pcv)->get_tree(); | |
2271 | if (t == error_mark_node) | |
00b44a6e | 2272 | return this->error_statement(); |
620e594b | 2273 | location_t loc = EXPR_LOCATION(t); |
8d0b03a2 | 2274 | tree label = create_artificial_label(loc); |
3d528853 | 2275 | tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label); |
8d0b03a2 ILT |
2276 | append_to_statement_list(c, &stmt_list); |
2277 | } | |
2278 | } | |
2279 | ||
2280 | if (*ps != NULL) | |
2281 | { | |
2282 | tree t = (*ps)->get_tree(); | |
2283 | if (t == error_mark_node) | |
00b44a6e | 2284 | return this->error_statement(); |
8d0b03a2 ILT |
2285 | append_to_statement_list(t, &stmt_list); |
2286 | } | |
2287 | } | |
036165d8 | 2288 | pop_cfun(); |
8d0b03a2 ILT |
2289 | |
2290 | tree tv = value->get_tree(); | |
2291 | if (tv == error_mark_node) | |
00b44a6e | 2292 | return this->error_statement(); |
9e851845 JJ |
2293 | tree t = build2_loc(switch_location.gcc_location(), SWITCH_EXPR, |
2294 | NULL_TREE, tv, stmt_list); | |
8d0b03a2 ILT |
2295 | return this->make_statement(t); |
2296 | } | |
2297 | ||
00b44a6e ILT |
2298 | // Pair of statements. |
2299 | ||
2300 | Bstatement* | |
2301 | Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2) | |
2302 | { | |
2303 | tree stmt_list = NULL_TREE; | |
2304 | tree t = s1->get_tree(); | |
2305 | if (t == error_mark_node) | |
2306 | return this->error_statement(); | |
2307 | append_to_statement_list(t, &stmt_list); | |
2308 | t = s2->get_tree(); | |
2309 | if (t == error_mark_node) | |
2310 | return this->error_statement(); | |
2311 | append_to_statement_list(t, &stmt_list); | |
ff09769f ILT |
2312 | |
2313 | // If neither statement has any side effects, stmt_list can be NULL | |
2314 | // at this point. | |
2315 | if (stmt_list == NULL_TREE) | |
2316 | stmt_list = integer_zero_node; | |
2317 | ||
00b44a6e ILT |
2318 | return this->make_statement(stmt_list); |
2319 | } | |
2320 | ||
8d0b03a2 ILT |
2321 | // List of statements. |
2322 | ||
2323 | Bstatement* | |
2324 | Gcc_backend::statement_list(const std::vector<Bstatement*>& statements) | |
2325 | { | |
2326 | tree stmt_list = NULL_TREE; | |
2327 | for (std::vector<Bstatement*>::const_iterator p = statements.begin(); | |
2328 | p != statements.end(); | |
2329 | ++p) | |
2330 | { | |
2331 | tree t = (*p)->get_tree(); | |
2332 | if (t == error_mark_node) | |
00b44a6e | 2333 | return this->error_statement(); |
8d0b03a2 ILT |
2334 | append_to_statement_list(t, &stmt_list); |
2335 | } | |
2336 | return this->make_statement(stmt_list); | |
2337 | } | |
2338 | ||
5ad7db5f ILT |
2339 | // Make a block. For some reason gcc uses a dual structure for |
2340 | // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the | |
2341 | // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in | |
2342 | // the Bblock. | |
2343 | ||
2344 | Bblock* | |
2345 | Gcc_backend::block(Bfunction* function, Bblock* enclosing, | |
2346 | const std::vector<Bvariable*>& vars, | |
8afa2bfb SD |
2347 | Location start_location, |
2348 | Location) | |
5ad7db5f ILT |
2349 | { |
2350 | tree block_tree = make_node(BLOCK); | |
2351 | if (enclosing == NULL) | |
2352 | { | |
036165d8 | 2353 | tree fndecl = function->get_tree(); |
5ad7db5f ILT |
2354 | gcc_assert(fndecl != NULL_TREE); |
2355 | ||
2356 | // We may have already created a block for local variables when | |
2357 | // we take the address of a parameter. | |
2358 | if (DECL_INITIAL(fndecl) == NULL_TREE) | |
2359 | { | |
2360 | BLOCK_SUPERCONTEXT(block_tree) = fndecl; | |
2361 | DECL_INITIAL(fndecl) = block_tree; | |
2362 | } | |
2363 | else | |
2364 | { | |
2365 | tree superblock_tree = DECL_INITIAL(fndecl); | |
2366 | BLOCK_SUPERCONTEXT(block_tree) = superblock_tree; | |
2367 | tree* pp; | |
2368 | for (pp = &BLOCK_SUBBLOCKS(superblock_tree); | |
2369 | *pp != NULL_TREE; | |
2370 | pp = &BLOCK_CHAIN(*pp)) | |
2371 | ; | |
2372 | *pp = block_tree; | |
2373 | } | |
2374 | } | |
2375 | else | |
2376 | { | |
2377 | tree superbind_tree = enclosing->get_tree(); | |
2378 | tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree); | |
2379 | gcc_assert(TREE_CODE(superblock_tree) == BLOCK); | |
2380 | ||
2381 | BLOCK_SUPERCONTEXT(block_tree) = superblock_tree; | |
2382 | tree* pp; | |
2383 | for (pp = &BLOCK_SUBBLOCKS(superblock_tree); | |
2384 | *pp != NULL_TREE; | |
2385 | pp = &BLOCK_CHAIN(*pp)) | |
2386 | ; | |
2387 | *pp = block_tree; | |
2388 | } | |
2389 | ||
2390 | tree* pp = &BLOCK_VARS(block_tree); | |
2391 | for (std::vector<Bvariable*>::const_iterator pv = vars.begin(); | |
2392 | pv != vars.end(); | |
2393 | ++pv) | |
2394 | { | |
399f5feb | 2395 | *pp = (*pv)->get_decl(); |
5ad7db5f ILT |
2396 | if (*pp != error_mark_node) |
2397 | pp = &DECL_CHAIN(*pp); | |
2398 | } | |
2399 | *pp = NULL_TREE; | |
2400 | ||
2401 | TREE_USED(block_tree) = 1; | |
2402 | ||
8afa2bfb SD |
2403 | tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR, |
2404 | void_type_node, BLOCK_VARS(block_tree), | |
2405 | NULL_TREE, block_tree); | |
5ad7db5f | 2406 | TREE_SIDE_EFFECTS(bind_tree) = 1; |
5ad7db5f ILT |
2407 | return new Bblock(bind_tree); |
2408 | } | |
2409 | ||
2410 | // Add statements to a block. | |
2411 | ||
2412 | void | |
2413 | Gcc_backend::block_add_statements(Bblock* bblock, | |
2414 | const std::vector<Bstatement*>& statements) | |
2415 | { | |
2416 | tree stmt_list = NULL_TREE; | |
2417 | for (std::vector<Bstatement*>::const_iterator p = statements.begin(); | |
2418 | p != statements.end(); | |
2419 | ++p) | |
2420 | { | |
2421 | tree s = (*p)->get_tree(); | |
2422 | if (s != error_mark_node) | |
2423 | append_to_statement_list(s, &stmt_list); | |
2424 | } | |
2425 | ||
2426 | tree bind_tree = bblock->get_tree(); | |
2427 | gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR); | |
2428 | BIND_EXPR_BODY(bind_tree) = stmt_list; | |
2429 | } | |
2430 | ||
2431 | // Return a block as a statement. | |
2432 | ||
2433 | Bstatement* | |
2434 | Gcc_backend::block_statement(Bblock* bblock) | |
2435 | { | |
2436 | tree bind_tree = bblock->get_tree(); | |
2437 | gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR); | |
2438 | return this->make_statement(bind_tree); | |
2439 | } | |
2440 | ||
7d6be4c8 ILT |
2441 | // This is not static because we declare it with GTY(()) in go-c.h. |
2442 | tree go_non_zero_struct; | |
2443 | ||
2444 | // Return a type corresponding to TYPE with non-zero size. | |
2445 | ||
2446 | tree | |
2447 | Gcc_backend::non_zero_size_type(tree type) | |
2448 | { | |
2449 | if (int_size_in_bytes(type) != 0) | |
2450 | return type; | |
2451 | ||
2452 | switch (TREE_CODE(type)) | |
2453 | { | |
2454 | case RECORD_TYPE: | |
f1e18725 ILT |
2455 | if (TYPE_FIELDS(type) != NULL_TREE) |
2456 | { | |
2457 | tree ns = make_node(RECORD_TYPE); | |
2458 | tree field_trees = NULL_TREE; | |
2459 | tree *pp = &field_trees; | |
2460 | for (tree field = TYPE_FIELDS(type); | |
2461 | field != NULL_TREE; | |
2462 | field = DECL_CHAIN(field)) | |
2463 | { | |
2464 | tree ft = TREE_TYPE(field); | |
2465 | if (field == TYPE_FIELDS(type)) | |
2466 | ft = non_zero_size_type(ft); | |
2467 | tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL, | |
2468 | DECL_NAME(field), ft); | |
2469 | DECL_CONTEXT(f) = ns; | |
2470 | *pp = f; | |
2471 | pp = &DECL_CHAIN(f); | |
2472 | } | |
2473 | TYPE_FIELDS(ns) = field_trees; | |
2474 | layout_type(ns); | |
2475 | return ns; | |
2476 | } | |
2477 | ||
2478 | if (go_non_zero_struct == NULL_TREE) | |
2479 | { | |
2480 | type = make_node(RECORD_TYPE); | |
2481 | tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL, | |
2482 | get_identifier("dummy"), | |
2483 | boolean_type_node); | |
2484 | DECL_CONTEXT(field) = type; | |
2485 | TYPE_FIELDS(type) = field; | |
2486 | layout_type(type); | |
2487 | go_non_zero_struct = type; | |
2488 | } | |
2489 | return go_non_zero_struct; | |
7d6be4c8 ILT |
2490 | |
2491 | case ARRAY_TYPE: | |
2492 | { | |
2493 | tree element_type = non_zero_size_type(TREE_TYPE(type)); | |
2494 | return build_array_type_nelts(element_type, 1); | |
2495 | } | |
2496 | ||
2497 | default: | |
2498 | gcc_unreachable(); | |
2499 | } | |
2500 | ||
2501 | gcc_unreachable(); | |
2502 | } | |
2503 | ||
ebc2f401 ILT |
2504 | // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Go type |
2505 | // can be created multiple times and thus have multiple tree | |
2506 | // representations. Make sure this does not confuse the middle-end. | |
2507 | ||
2508 | tree | |
2509 | Gcc_backend::convert_tree(tree type_tree, tree expr_tree, Location location) | |
2510 | { | |
2511 | if (type_tree == TREE_TYPE(expr_tree)) | |
2512 | return expr_tree; | |
2513 | ||
2514 | if (type_tree == error_mark_node | |
2515 | || expr_tree == error_mark_node | |
2516 | || TREE_TYPE(expr_tree) == error_mark_node) | |
2517 | return error_mark_node; | |
2518 | ||
2519 | gcc_assert(TREE_CODE(type_tree) == TREE_CODE(TREE_TYPE(expr_tree))); | |
2520 | if (POINTER_TYPE_P(type_tree) | |
2521 | || INTEGRAL_TYPE_P(type_tree) | |
2522 | || SCALAR_FLOAT_TYPE_P(type_tree) | |
2523 | || COMPLEX_FLOAT_TYPE_P(type_tree)) | |
2524 | return fold_convert_loc(location.gcc_location(), type_tree, expr_tree); | |
2525 | else if (TREE_CODE(type_tree) == RECORD_TYPE | |
2526 | || TREE_CODE(type_tree) == ARRAY_TYPE) | |
2527 | { | |
2528 | gcc_assert(int_size_in_bytes(type_tree) | |
2529 | == int_size_in_bytes(TREE_TYPE(expr_tree))); | |
2530 | if (TYPE_MAIN_VARIANT(type_tree) | |
2531 | == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree))) | |
2532 | return fold_build1_loc(location.gcc_location(), NOP_EXPR, | |
2533 | type_tree, expr_tree); | |
2534 | return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR, | |
2535 | type_tree, expr_tree); | |
2536 | } | |
2537 | ||
2538 | gcc_unreachable(); | |
2539 | } | |
2540 | ||
e09ce6c5 ILT |
2541 | // Make a global variable. |
2542 | ||
2543 | Bvariable* | |
f3878205 TM |
2544 | Gcc_backend::global_variable(const std::string& var_name, |
2545 | const std::string& asm_name, | |
e09ce6c5 ILT |
2546 | Btype* btype, |
2547 | bool is_external, | |
2548 | bool is_hidden, | |
744c3195 | 2549 | bool in_unique_section, |
8afa2bfb | 2550 | Location location) |
e09ce6c5 ILT |
2551 | { |
2552 | tree type_tree = btype->get_tree(); | |
2553 | if (type_tree == error_mark_node) | |
2554 | return this->error_variable(); | |
2555 | ||
7d6be4c8 | 2556 | // The GNU linker does not like dynamic variables with zero size. |
8fd41e92 | 2557 | tree orig_type_tree = type_tree; |
7d6be4c8 ILT |
2558 | if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0) |
2559 | type_tree = this->non_zero_size_type(type_tree); | |
2560 | ||
8afa2bfb | 2561 | tree decl = build_decl(location.gcc_location(), VAR_DECL, |
e09ce6c5 ILT |
2562 | get_identifier_from_string(var_name), |
2563 | type_tree); | |
2564 | if (is_external) | |
2565 | DECL_EXTERNAL(decl) = 1; | |
2566 | else | |
2567 | TREE_STATIC(decl) = 1; | |
2568 | if (!is_hidden) | |
2569 | { | |
2570 | TREE_PUBLIC(decl) = 1; | |
e09ce6c5 ILT |
2571 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); |
2572 | } | |
f3878205 TM |
2573 | else |
2574 | { | |
2575 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); | |
2576 | } | |
9dcd84ec | 2577 | |
e09ce6c5 ILT |
2578 | TREE_USED(decl) = 1; |
2579 | ||
744c3195 ILT |
2580 | if (in_unique_section) |
2581 | resolve_unique_section (decl, 0, 1); | |
2582 | ||
e09ce6c5 ILT |
2583 | go_preserve_from_gc(decl); |
2584 | ||
399f5feb | 2585 | return new Bvariable(decl, orig_type_tree); |
e09ce6c5 ILT |
2586 | } |
2587 | ||
2588 | // Set the initial value of a global variable. | |
2589 | ||
2590 | void | |
2591 | Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr) | |
2592 | { | |
2593 | tree expr_tree = expr->get_tree(); | |
2594 | if (expr_tree == error_mark_node) | |
2595 | return; | |
2596 | gcc_assert(TREE_CONSTANT(expr_tree)); | |
399f5feb | 2597 | tree var_decl = var->get_decl(); |
e09ce6c5 ILT |
2598 | if (var_decl == error_mark_node) |
2599 | return; | |
2600 | DECL_INITIAL(var_decl) = expr_tree; | |
744c3195 ILT |
2601 | |
2602 | // If this variable goes in a unique section, it may need to go into | |
2603 | // a different one now that DECL_INITIAL is set. | |
0ffc395f UB |
2604 | if (symtab_node::get(var_decl) |
2605 | && symtab_node::get(var_decl)->implicit_section) | |
744c3195 | 2606 | { |
24d047a3 | 2607 | set_decl_section_name (var_decl, NULL); |
744c3195 ILT |
2608 | resolve_unique_section (var_decl, |
2609 | compute_reloc_for_constant (expr_tree), | |
2610 | 1); | |
2611 | } | |
e09ce6c5 ILT |
2612 | } |
2613 | ||
2614 | // Make a local variable. | |
2615 | ||
2616 | Bvariable* | |
2617 | Gcc_backend::local_variable(Bfunction* function, const std::string& name, | |
81e0f092 CZ |
2618 | Btype* btype, Bvariable* decl_var, |
2619 | bool is_address_taken, Location location) | |
e09ce6c5 ILT |
2620 | { |
2621 | tree type_tree = btype->get_tree(); | |
2622 | if (type_tree == error_mark_node) | |
2623 | return this->error_variable(); | |
8afa2bfb | 2624 | tree decl = build_decl(location.gcc_location(), VAR_DECL, |
e09ce6c5 ILT |
2625 | get_identifier_from_string(name), |
2626 | type_tree); | |
2627 | DECL_CONTEXT(decl) = function->get_tree(); | |
2628 | TREE_USED(decl) = 1; | |
acf98146 ILT |
2629 | if (is_address_taken) |
2630 | TREE_ADDRESSABLE(decl) = 1; | |
81e0f092 CZ |
2631 | if (decl_var != NULL) |
2632 | { | |
2633 | DECL_HAS_VALUE_EXPR_P(decl) = 1; | |
2634 | SET_DECL_VALUE_EXPR(decl, decl_var->get_decl()); | |
2635 | } | |
e09ce6c5 ILT |
2636 | go_preserve_from_gc(decl); |
2637 | return new Bvariable(decl); | |
2638 | } | |
2639 | ||
2640 | // Make a function parameter variable. | |
2641 | ||
2642 | Bvariable* | |
2643 | Gcc_backend::parameter_variable(Bfunction* function, const std::string& name, | |
acf98146 | 2644 | Btype* btype, bool is_address_taken, |
8afa2bfb | 2645 | Location location) |
e09ce6c5 ILT |
2646 | { |
2647 | tree type_tree = btype->get_tree(); | |
2648 | if (type_tree == error_mark_node) | |
2649 | return this->error_variable(); | |
8afa2bfb | 2650 | tree decl = build_decl(location.gcc_location(), PARM_DECL, |
e09ce6c5 ILT |
2651 | get_identifier_from_string(name), |
2652 | type_tree); | |
2653 | DECL_CONTEXT(decl) = function->get_tree(); | |
2654 | DECL_ARG_TYPE(decl) = type_tree; | |
2655 | TREE_USED(decl) = 1; | |
acf98146 ILT |
2656 | if (is_address_taken) |
2657 | TREE_ADDRESSABLE(decl) = 1; | |
e09ce6c5 ILT |
2658 | go_preserve_from_gc(decl); |
2659 | return new Bvariable(decl); | |
2660 | } | |
2661 | ||
38bf819a RH |
2662 | // Make a static chain variable. |
2663 | ||
2664 | Bvariable* | |
2665 | Gcc_backend::static_chain_variable(Bfunction* function, const std::string& name, | |
2666 | Btype* btype, Location location) | |
2667 | { | |
2668 | tree type_tree = btype->get_tree(); | |
2669 | if (type_tree == error_mark_node) | |
2670 | return this->error_variable(); | |
2671 | tree decl = build_decl(location.gcc_location(), PARM_DECL, | |
2672 | get_identifier_from_string(name), type_tree); | |
2673 | tree fndecl = function->get_tree(); | |
2674 | DECL_CONTEXT(decl) = fndecl; | |
2675 | DECL_ARG_TYPE(decl) = type_tree; | |
2676 | TREE_USED(decl) = 1; | |
2677 | DECL_ARTIFICIAL(decl) = 1; | |
2678 | DECL_IGNORED_P(decl) = 1; | |
2679 | TREE_READONLY(decl) = 1; | |
2680 | ||
2681 | struct function *f = DECL_STRUCT_FUNCTION(fndecl); | |
2682 | if (f == NULL) | |
2683 | { | |
2684 | push_struct_function(fndecl); | |
2685 | pop_cfun(); | |
2686 | f = DECL_STRUCT_FUNCTION(fndecl); | |
2687 | } | |
2688 | gcc_assert(f->static_chain_decl == NULL); | |
2689 | f->static_chain_decl = decl; | |
2690 | DECL_STATIC_CHAIN(fndecl) = 1; | |
2691 | ||
2692 | go_preserve_from_gc(decl); | |
2693 | return new Bvariable(decl); | |
2694 | } | |
2695 | ||
9131ad67 ILT |
2696 | // Make a temporary variable. |
2697 | ||
2698 | Bvariable* | |
2699 | Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock, | |
2700 | Btype* btype, Bexpression* binit, | |
2701 | bool is_address_taken, | |
8afa2bfb | 2702 | Location location, |
9131ad67 ILT |
2703 | Bstatement** pstatement) |
2704 | { | |
3e7b0938 CM |
2705 | gcc_assert(function != NULL); |
2706 | tree decl = function->get_tree(); | |
9131ad67 ILT |
2707 | tree type_tree = btype->get_tree(); |
2708 | tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree(); | |
3e7b0938 CM |
2709 | if (type_tree == error_mark_node |
2710 | || init_tree == error_mark_node | |
2711 | || decl == error_mark_node) | |
9131ad67 ILT |
2712 | { |
2713 | *pstatement = this->error_statement(); | |
2714 | return this->error_variable(); | |
2715 | } | |
2716 | ||
2717 | tree var; | |
2718 | // We can only use create_tmp_var if the type is not addressable. | |
2719 | if (!TREE_ADDRESSABLE(type_tree)) | |
aa492920 CM |
2720 | { |
2721 | if (DECL_STRUCT_FUNCTION(decl) == NULL) | |
2722 | push_struct_function(decl); | |
2723 | else | |
2724 | push_cfun(DECL_STRUCT_FUNCTION(decl)); | |
2725 | ||
2726 | var = create_tmp_var(type_tree, "GOTMP"); | |
2727 | pop_cfun(); | |
2728 | } | |
9131ad67 ILT |
2729 | else |
2730 | { | |
2731 | gcc_assert(bblock != NULL); | |
8afa2bfb | 2732 | var = build_decl(location.gcc_location(), VAR_DECL, |
9131ad67 ILT |
2733 | create_tmp_var_name("GOTMP"), |
2734 | type_tree); | |
2735 | DECL_ARTIFICIAL(var) = 1; | |
2736 | DECL_IGNORED_P(var) = 1; | |
2737 | TREE_USED(var) = 1; | |
aa492920 | 2738 | DECL_CONTEXT(var) = decl; |
9131ad67 ILT |
2739 | |
2740 | // We have to add this variable to the BLOCK and the BIND_EXPR. | |
2741 | tree bind_tree = bblock->get_tree(); | |
2742 | gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR); | |
2743 | tree block_tree = BIND_EXPR_BLOCK(bind_tree); | |
2744 | gcc_assert(TREE_CODE(block_tree) == BLOCK); | |
2745 | DECL_CHAIN(var) = BLOCK_VARS(block_tree); | |
2746 | BLOCK_VARS(block_tree) = var; | |
2747 | BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree); | |
2748 | } | |
2749 | ||
c2a0fe56 ILT |
2750 | if (this->type_size(btype) != 0 |
2751 | && init_tree != NULL_TREE | |
2752 | && TREE_TYPE(init_tree) != void_type_node) | |
ebc2f401 | 2753 | DECL_INITIAL(var) = this->convert_tree(type_tree, init_tree, location); |
9131ad67 ILT |
2754 | |
2755 | if (is_address_taken) | |
2756 | TREE_ADDRESSABLE(var) = 1; | |
2757 | ||
8afa2bfb SD |
2758 | *pstatement = this->make_statement(build1_loc(location.gcc_location(), |
2759 | DECL_EXPR, | |
9131ad67 | 2760 | void_type_node, var)); |
dd373780 | 2761 | |
c2a0fe56 ILT |
2762 | // For a zero sized type, don't initialize VAR with BINIT, but still |
2763 | // evaluate BINIT for its side effects. | |
2764 | if (init_tree != NULL_TREE | |
2765 | && (this->type_size(btype) == 0 | |
2766 | || TREE_TYPE(init_tree) == void_type_node)) | |
8220e3f9 TM |
2767 | *pstatement = |
2768 | this->compound_statement(this->expression_statement(function, binit), | |
2769 | *pstatement); | |
dd373780 | 2770 | |
9131ad67 ILT |
2771 | return new Bvariable(var); |
2772 | } | |
2773 | ||
fb930306 CM |
2774 | // Create an implicit variable that is compiler-defined. This is used when |
2775 | // generating GC root variables and storing the values of a slice initializer. | |
036165d8 CM |
2776 | |
2777 | Bvariable* | |
f3878205 TM |
2778 | Gcc_backend::implicit_variable(const std::string& name, |
2779 | const std::string& asm_name, | |
2780 | Btype* type, bool is_hidden, bool is_constant, | |
18fbd1ec | 2781 | bool is_common, int64_t alignment) |
036165d8 CM |
2782 | { |
2783 | tree type_tree = type->get_tree(); | |
f1d2ac4f | 2784 | if (type_tree == error_mark_node) |
036165d8 CM |
2785 | return this->error_variable(); |
2786 | ||
2787 | tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, | |
fb930306 | 2788 | get_identifier_from_string(name), type_tree); |
036165d8 | 2789 | DECL_EXTERNAL(decl) = 0; |
f1d2ac4f | 2790 | TREE_PUBLIC(decl) = !is_hidden; |
036165d8 | 2791 | TREE_STATIC(decl) = 1; |
f1d2ac4f | 2792 | TREE_USED(decl) = 1; |
036165d8 | 2793 | DECL_ARTIFICIAL(decl) = 1; |
bae90c98 ILT |
2794 | if (is_common) |
2795 | { | |
2796 | DECL_COMMON(decl) = 1; | |
f1d2ac4f CM |
2797 | |
2798 | // When the initializer for one implicit_variable refers to another, | |
2799 | // it needs to know the visibility of the referenced struct so that | |
2800 | // compute_reloc_for_constant will return the right value. On many | |
2801 | // systems calling make_decl_one_only will mark the decl as weak, | |
2802 | // which will change the return value of compute_reloc_for_constant. | |
2803 | // We can't reliably call make_decl_one_only yet, because we don't | |
2804 | // yet know the initializer. This issue doesn't arise in C because | |
2805 | // Go initializers, unlike C initializers, can be indirectly | |
2806 | // recursive. To ensure that compute_reloc_for_constant computes | |
2807 | // the right value if some other initializer refers to this one, we | |
2808 | // mark this symbol as weak here. We undo that below in | |
2809 | // immutable_struct_set_init before calling mark_decl_one_only. | |
2810 | DECL_WEAK(decl) = 1; | |
bae90c98 | 2811 | } |
f1d2ac4f | 2812 | if (is_constant) |
fb930306 CM |
2813 | { |
2814 | TREE_READONLY(decl) = 1; | |
2815 | TREE_CONSTANT(decl) = 1; | |
2816 | } | |
bae90c98 ILT |
2817 | if (alignment != 0) |
2818 | { | |
fe37c7af | 2819 | SET_DECL_ALIGN(decl, alignment * BITS_PER_UNIT); |
bae90c98 ILT |
2820 | DECL_USER_ALIGN(decl) = 1; |
2821 | } | |
f3878205 TM |
2822 | if (! asm_name.empty()) |
2823 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); | |
bae90c98 | 2824 | |
f1d2ac4f CM |
2825 | go_preserve_from_gc(decl); |
2826 | return new Bvariable(decl); | |
2827 | } | |
2828 | ||
2829 | // Set the initalizer for a variable created by implicit_variable. | |
2830 | // This is where we finish compiling the variable. | |
2831 | ||
2832 | void | |
2833 | Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&, | |
2834 | Btype*, bool, bool, bool is_common, | |
2835 | Bexpression* init) | |
2836 | { | |
399f5feb | 2837 | tree decl = var->get_decl(); |
f1d2ac4f CM |
2838 | tree init_tree; |
2839 | if (init == NULL) | |
2840 | init_tree = NULL_TREE; | |
2841 | else | |
2842 | init_tree = init->get_tree(); | |
2843 | if (decl == error_mark_node || init_tree == error_mark_node) | |
2844 | return; | |
2845 | ||
2846 | DECL_INITIAL(decl) = init_tree; | |
2847 | ||
2848 | // Now that DECL_INITIAL is set, we can't call make_decl_one_only. | |
2849 | // See the comment where DECL_WEAK is set in implicit_variable. | |
2850 | if (is_common) | |
2851 | { | |
2852 | DECL_WEAK(decl) = 0; | |
2853 | make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl)); | |
2854 | } | |
2855 | ||
2856 | resolve_unique_section(decl, 2, 1); | |
2857 | ||
036165d8 | 2858 | rest_of_decl_compilation(decl, 1, 0); |
f1d2ac4f CM |
2859 | } |
2860 | ||
2861 | // Return a reference to an implicit variable defined in another package. | |
036165d8 | 2862 | |
f1d2ac4f | 2863 | Bvariable* |
f3878205 TM |
2864 | Gcc_backend::implicit_variable_reference(const std::string& name, |
2865 | const std::string& asm_name, | |
2866 | Btype* btype) | |
f1d2ac4f CM |
2867 | { |
2868 | tree type_tree = btype->get_tree(); | |
2869 | if (type_tree == error_mark_node) | |
2870 | return this->error_variable(); | |
2871 | ||
2872 | tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, | |
2873 | get_identifier_from_string(name), type_tree); | |
be18b176 | 2874 | DECL_EXTERNAL(decl) = 1; |
f1d2ac4f | 2875 | TREE_PUBLIC(decl) = 1; |
be18b176 | 2876 | TREE_STATIC(decl) = 0; |
f1d2ac4f | 2877 | DECL_ARTIFICIAL(decl) = 1; |
f3878205 TM |
2878 | if (! asm_name.empty()) |
2879 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); | |
f1d2ac4f | 2880 | go_preserve_from_gc(decl); |
036165d8 CM |
2881 | return new Bvariable(decl); |
2882 | } | |
2883 | ||
70f91024 ILT |
2884 | // Create a named immutable initialized data structure. |
2885 | ||
2886 | Bvariable* | |
f3878205 TM |
2887 | Gcc_backend::immutable_struct(const std::string& name, |
2888 | const std::string& asm_name, | |
2889 | bool is_hidden, | |
915182a0 | 2890 | bool is_common, Btype* btype, Location location) |
70f91024 ILT |
2891 | { |
2892 | tree type_tree = btype->get_tree(); | |
2893 | if (type_tree == error_mark_node) | |
2894 | return this->error_variable(); | |
2895 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); | |
8afa2bfb | 2896 | tree decl = build_decl(location.gcc_location(), VAR_DECL, |
70f91024 ILT |
2897 | get_identifier_from_string(name), |
2898 | build_qualified_type(type_tree, TYPE_QUAL_CONST)); | |
2899 | TREE_STATIC(decl) = 1; | |
036165d8 | 2900 | TREE_USED(decl) = 1; |
70f91024 ILT |
2901 | TREE_READONLY(decl) = 1; |
2902 | TREE_CONSTANT(decl) = 1; | |
70f91024 | 2903 | DECL_ARTIFICIAL(decl) = 1; |
2ec974d9 ILT |
2904 | if (!is_hidden) |
2905 | TREE_PUBLIC(decl) = 1; | |
f3878205 TM |
2906 | if (! asm_name.empty()) |
2907 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); | |
70f91024 | 2908 | |
915182a0 ILT |
2909 | // When the initializer for one immutable_struct refers to another, |
2910 | // it needs to know the visibility of the referenced struct so that | |
2911 | // compute_reloc_for_constant will return the right value. On many | |
2912 | // systems calling make_decl_one_only will mark the decl as weak, | |
2913 | // which will change the return value of compute_reloc_for_constant. | |
2914 | // We can't reliably call make_decl_one_only yet, because we don't | |
2915 | // yet know the initializer. This issue doesn't arise in C because | |
2916 | // Go initializers, unlike C initializers, can be indirectly | |
2917 | // recursive. To ensure that compute_reloc_for_constant computes | |
2918 | // the right value if some other initializer refers to this one, we | |
2919 | // mark this symbol as weak here. We undo that below in | |
2920 | // immutable_struct_set_init before calling mark_decl_one_only. | |
2921 | if (is_common) | |
2922 | DECL_WEAK(decl) = 1; | |
2923 | ||
70f91024 ILT |
2924 | // We don't call rest_of_decl_compilation until we have the |
2925 | // initializer. | |
2926 | ||
2927 | go_preserve_from_gc(decl); | |
2928 | return new Bvariable(decl); | |
2929 | } | |
2930 | ||
2931 | // Set the initializer for a variable created by immutable_struct. | |
2932 | // This is where we finish compiling the variable. | |
2933 | ||
2934 | void | |
2935 | Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&, | |
2ec974d9 | 2936 | bool, bool is_common, Btype*, Location, |
70f91024 ILT |
2937 | Bexpression* initializer) |
2938 | { | |
399f5feb | 2939 | tree decl = var->get_decl(); |
70f91024 ILT |
2940 | tree init_tree = initializer->get_tree(); |
2941 | if (decl == error_mark_node || init_tree == error_mark_node) | |
2942 | return; | |
2943 | ||
2944 | DECL_INITIAL(decl) = init_tree; | |
2945 | ||
915182a0 ILT |
2946 | // Now that DECL_INITIAL is set, we can't call make_decl_one_only. |
2947 | // See the comment where DECL_WEAK is set in immutable_struct. | |
2ec974d9 | 2948 | if (is_common) |
915182a0 ILT |
2949 | { |
2950 | DECL_WEAK(decl) = 0; | |
2951 | make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl)); | |
2952 | } | |
a572c454 ILT |
2953 | |
2954 | // These variables are often unneeded in the final program, so put | |
2955 | // them in their own section so that linker GC can discard them. | |
e086adbd ILT |
2956 | resolve_unique_section(decl, |
2957 | compute_reloc_for_constant (init_tree), | |
2958 | 1); | |
70f91024 ILT |
2959 | |
2960 | rest_of_decl_compilation(decl, 1, 0); | |
2961 | } | |
2962 | ||
2963 | // Return a reference to an immutable initialized data structure | |
2964 | // defined in another package. | |
2965 | ||
2966 | Bvariable* | |
f3878205 TM |
2967 | Gcc_backend::immutable_struct_reference(const std::string& name, |
2968 | const std::string& asm_name, | |
2969 | Btype* btype, | |
8afa2bfb | 2970 | Location location) |
70f91024 ILT |
2971 | { |
2972 | tree type_tree = btype->get_tree(); | |
2973 | if (type_tree == error_mark_node) | |
2974 | return this->error_variable(); | |
2975 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); | |
8afa2bfb | 2976 | tree decl = build_decl(location.gcc_location(), VAR_DECL, |
70f91024 ILT |
2977 | get_identifier_from_string(name), |
2978 | build_qualified_type(type_tree, TYPE_QUAL_CONST)); | |
2979 | TREE_READONLY(decl) = 1; | |
2980 | TREE_CONSTANT(decl) = 1; | |
2981 | DECL_ARTIFICIAL(decl) = 1; | |
2982 | TREE_PUBLIC(decl) = 1; | |
2983 | DECL_EXTERNAL(decl) = 1; | |
f3878205 TM |
2984 | if (! asm_name.empty()) |
2985 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); | |
70f91024 ILT |
2986 | go_preserve_from_gc(decl); |
2987 | return new Bvariable(decl); | |
2988 | } | |
2989 | ||
d56e6679 ILT |
2990 | // Make a label. |
2991 | ||
2992 | Blabel* | |
2993 | Gcc_backend::label(Bfunction* function, const std::string& name, | |
8afa2bfb | 2994 | Location location) |
d56e6679 ILT |
2995 | { |
2996 | tree decl; | |
2997 | if (name.empty()) | |
036165d8 CM |
2998 | { |
2999 | tree func_tree = function->get_tree(); | |
3000 | if (DECL_STRUCT_FUNCTION(func_tree) == NULL) | |
3001 | push_struct_function(func_tree); | |
3002 | else | |
3003 | push_cfun(DECL_STRUCT_FUNCTION(func_tree)); | |
3004 | ||
3005 | decl = create_artificial_label(location.gcc_location()); | |
3006 | ||
3007 | pop_cfun(); | |
3008 | } | |
d56e6679 ILT |
3009 | else |
3010 | { | |
3011 | tree id = get_identifier_from_string(name); | |
8afa2bfb SD |
3012 | decl = build_decl(location.gcc_location(), LABEL_DECL, id, |
3013 | void_type_node); | |
d56e6679 ILT |
3014 | DECL_CONTEXT(decl) = function->get_tree(); |
3015 | } | |
3016 | return new Blabel(decl); | |
3017 | } | |
3018 | ||
3019 | // Make a statement which defines a label. | |
3020 | ||
3021 | Bstatement* | |
3022 | Gcc_backend::label_definition_statement(Blabel* label) | |
3023 | { | |
3024 | tree lab = label->get_tree(); | |
3025 | tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR, | |
3026 | void_type_node, lab); | |
3027 | return this->make_statement(ret); | |
3028 | } | |
3029 | ||
3030 | // Make a goto statement. | |
3031 | ||
3032 | Bstatement* | |
8afa2bfb | 3033 | Gcc_backend::goto_statement(Blabel* label, Location location) |
d56e6679 ILT |
3034 | { |
3035 | tree lab = label->get_tree(); | |
8afa2bfb SD |
3036 | tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node, |
3037 | lab); | |
d56e6679 ILT |
3038 | return this->make_statement(ret); |
3039 | } | |
3040 | ||
3041 | // Get the address of a label. | |
3042 | ||
3043 | Bexpression* | |
8afa2bfb | 3044 | Gcc_backend::label_address(Blabel* label, Location location) |
d56e6679 ILT |
3045 | { |
3046 | tree lab = label->get_tree(); | |
3047 | TREE_USED(lab) = 1; | |
3048 | TREE_ADDRESSABLE(lab) = 1; | |
8afa2bfb SD |
3049 | tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node, |
3050 | build_fold_addr_expr_loc(location.gcc_location(), | |
3051 | lab)); | |
d56e6679 ILT |
3052 | return this->make_expression(ret); |
3053 | } | |
3054 | ||
f7191ecd CM |
3055 | // Declare or define a new function. |
3056 | ||
3057 | Bfunction* | |
3058 | Gcc_backend::function(Btype* fntype, const std::string& name, | |
a4e30bef ILT |
3059 | const std::string& asm_name, unsigned int flags, |
3060 | Location location) | |
f7191ecd CM |
3061 | { |
3062 | tree functype = fntype->get_tree(); | |
3063 | if (functype != error_mark_node) | |
3064 | { | |
3065 | gcc_assert(FUNCTION_POINTER_TYPE_P(functype)); | |
3066 | functype = TREE_TYPE(functype); | |
3067 | } | |
3068 | tree id = get_identifier_from_string(name); | |
3069 | if (functype == error_mark_node || id == error_mark_node) | |
3070 | return this->error_function(); | |
3071 | ||
3072 | tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype); | |
f3878205 | 3073 | if (! asm_name.empty()) |
f7191ecd | 3074 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); |
a4e30bef | 3075 | if ((flags & function_is_visible) != 0) |
f7191ecd | 3076 | TREE_PUBLIC(decl) = 1; |
a4e30bef | 3077 | if ((flags & function_is_declaration) != 0) |
f7191ecd CM |
3078 | DECL_EXTERNAL(decl) = 1; |
3079 | else | |
3080 | { | |
3081 | tree restype = TREE_TYPE(functype); | |
3082 | tree resdecl = | |
3083 | build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype); | |
3084 | DECL_ARTIFICIAL(resdecl) = 1; | |
3085 | DECL_IGNORED_P(resdecl) = 1; | |
3086 | DECL_CONTEXT(resdecl) = decl; | |
3087 | DECL_RESULT(decl) = resdecl; | |
3088 | } | |
a4e30bef | 3089 | if ((flags & function_is_inlinable) == 0) |
f7191ecd | 3090 | DECL_UNINLINABLE(decl) = 1; |
a4e30bef | 3091 | if ((flags & function_no_split_stack) != 0) |
f7191ecd | 3092 | { |
577eec56 | 3093 | tree attr = get_identifier ("no_split_stack"); |
f7191ecd CM |
3094 | DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE); |
3095 | } | |
a4e30bef | 3096 | if ((flags & function_does_not_return) != 0) |
9638589f | 3097 | TREE_THIS_VOLATILE(decl) = 1; |
a4e30bef | 3098 | if ((flags & function_in_unique_section) != 0) |
f7191ecd | 3099 | resolve_unique_section(decl, 0, 1); |
862ec763 ILT |
3100 | if ((flags & function_only_inline) != 0) |
3101 | { | |
3102 | DECL_EXTERNAL(decl) = 1; | |
3103 | DECL_DECLARED_INLINE_P(decl) = 1; | |
3104 | } | |
f7191ecd CM |
3105 | |
3106 | go_preserve_from_gc(decl); | |
3107 | return new Bfunction(decl); | |
3108 | } | |
3109 | ||
7035307e CM |
3110 | // Create a statement that runs all deferred calls for FUNCTION. This should |
3111 | // be a statement that looks like this in C++: | |
3112 | // finish: | |
3113 | // try { UNDEFER; } catch { CHECK_DEFER; goto finish; } | |
3114 | ||
3115 | Bstatement* | |
3116 | Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer, | |
3117 | Bexpression* defer, Location location) | |
3118 | { | |
3119 | tree undefer_tree = undefer->get_tree(); | |
3120 | tree defer_tree = defer->get_tree(); | |
036165d8 | 3121 | tree fntree = function->get_tree(); |
7035307e CM |
3122 | |
3123 | if (undefer_tree == error_mark_node | |
036165d8 CM |
3124 | || defer_tree == error_mark_node |
3125 | || fntree == error_mark_node) | |
7035307e CM |
3126 | return this->error_statement(); |
3127 | ||
036165d8 CM |
3128 | if (DECL_STRUCT_FUNCTION(fntree) == NULL) |
3129 | push_struct_function(fntree); | |
3130 | else | |
3131 | push_cfun(DECL_STRUCT_FUNCTION(fntree)); | |
3132 | ||
7035307e CM |
3133 | tree stmt_list = NULL; |
3134 | Blabel* blabel = this->label(function, "", location); | |
3135 | Bstatement* label_def = this->label_definition_statement(blabel); | |
3136 | append_to_statement_list(label_def->get_tree(), &stmt_list); | |
3137 | ||
3138 | Bstatement* jump_stmt = this->goto_statement(blabel, location); | |
3139 | tree jump = jump_stmt->get_tree(); | |
3140 | tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump); | |
3141 | catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body); | |
3142 | tree try_catch = | |
3143 | build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body); | |
3144 | append_to_statement_list(try_catch, &stmt_list); | |
036165d8 | 3145 | pop_cfun(); |
7035307e CM |
3146 | |
3147 | return this->make_statement(stmt_list); | |
3148 | } | |
3149 | ||
3150 | // Record PARAM_VARS as the variables to use for the parameters of FUNCTION. | |
3151 | // This will only be called for a function definition. | |
3152 | ||
3153 | bool | |
3154 | Gcc_backend::function_set_parameters(Bfunction* function, | |
3155 | const std::vector<Bvariable*>& param_vars) | |
3156 | { | |
3157 | tree func_tree = function->get_tree(); | |
3158 | if (func_tree == error_mark_node) | |
3159 | return false; | |
3160 | ||
3161 | tree params = NULL_TREE; | |
3162 | tree *pp = ¶ms; | |
3163 | for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin(); | |
3164 | pv != param_vars.end(); | |
3165 | ++pv) | |
3166 | { | |
399f5feb | 3167 | *pp = (*pv)->get_decl(); |
7035307e CM |
3168 | gcc_assert(*pp != error_mark_node); |
3169 | pp = &DECL_CHAIN(*pp); | |
3170 | } | |
3171 | *pp = NULL_TREE; | |
3172 | DECL_ARGUMENTS(func_tree) = params; | |
3173 | return true; | |
3174 | } | |
3175 | ||
3176 | // Set the function body for FUNCTION using the code in CODE_BLOCK. | |
3177 | ||
3178 | bool | |
3179 | Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt) | |
3180 | { | |
3181 | tree func_tree = function->get_tree(); | |
3182 | tree code = code_stmt->get_tree(); | |
3183 | ||
3184 | if (func_tree == error_mark_node || code == error_mark_node) | |
3185 | return false; | |
3186 | DECL_SAVED_TREE(func_tree) = code; | |
3187 | return true; | |
3188 | } | |
3189 | ||
90cbaa29 CM |
3190 | // Look up a named built-in function in the current backend implementation. |
3191 | // Returns NULL if no built-in function by that name exists. | |
3192 | ||
3193 | Bfunction* | |
3194 | Gcc_backend::lookup_builtin(const std::string& name) | |
3195 | { | |
3196 | if (this->builtin_functions_.count(name) != 0) | |
3197 | return this->builtin_functions_[name]; | |
3198 | return NULL; | |
3199 | } | |
3200 | ||
036165d8 | 3201 | // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS, |
d7438551 AH |
3202 | // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as |
3203 | // emit early debugging information. | |
036165d8 CM |
3204 | |
3205 | void | |
3206 | Gcc_backend::write_global_definitions( | |
3207 | const std::vector<Btype*>& type_decls, | |
3208 | const std::vector<Bexpression*>& constant_decls, | |
3209 | const std::vector<Bfunction*>& function_decls, | |
3210 | const std::vector<Bvariable*>& variable_decls) | |
3211 | { | |
3212 | size_t count_definitions = type_decls.size() + constant_decls.size() | |
3213 | + function_decls.size() + variable_decls.size(); | |
3214 | ||
3215 | tree* defs = new tree[count_definitions]; | |
3216 | ||
3217 | // Convert all non-erroneous declarations into Gimple form. | |
3218 | size_t i = 0; | |
3219 | for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin(); | |
3220 | p != variable_decls.end(); | |
3221 | ++p) | |
3222 | { | |
399f5feb ILT |
3223 | tree v = (*p)->get_decl(); |
3224 | if (v != error_mark_node) | |
036165d8 | 3225 | { |
399f5feb | 3226 | defs[i] = v; |
036165d8 CM |
3227 | go_preserve_from_gc(defs[i]); |
3228 | ++i; | |
3229 | } | |
3230 | } | |
3231 | ||
3232 | for (std::vector<Btype*>::const_iterator p = type_decls.begin(); | |
3233 | p != type_decls.end(); | |
3234 | ++p) | |
3235 | { | |
3236 | tree type_tree = (*p)->get_tree(); | |
3237 | if (type_tree != error_mark_node | |
3238 | && IS_TYPE_OR_DECL_P(type_tree)) | |
3239 | { | |
3240 | defs[i] = TYPE_NAME(type_tree); | |
3241 | gcc_assert(defs[i] != NULL); | |
3242 | go_preserve_from_gc(defs[i]); | |
3243 | ++i; | |
3244 | } | |
3245 | } | |
3246 | for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin(); | |
3247 | p != constant_decls.end(); | |
3248 | ++p) | |
3249 | { | |
3250 | if ((*p)->get_tree() != error_mark_node) | |
3251 | { | |
3252 | defs[i] = (*p)->get_tree(); | |
3253 | go_preserve_from_gc(defs[i]); | |
3254 | ++i; | |
3255 | } | |
3256 | } | |
3257 | for (std::vector<Bfunction*>::const_iterator p = function_decls.begin(); | |
3258 | p != function_decls.end(); | |
3259 | ++p) | |
3260 | { | |
3261 | tree decl = (*p)->get_tree(); | |
3262 | if (decl != error_mark_node) | |
3263 | { | |
3264 | go_preserve_from_gc(decl); | |
964f78b7 ILT |
3265 | if (DECL_STRUCT_FUNCTION(decl) == NULL) |
3266 | allocate_struct_function(decl, false); | |
3dafb85c | 3267 | cgraph_node::finalize_function(decl, true); |
036165d8 CM |
3268 | |
3269 | defs[i] = decl; | |
3270 | ++i; | |
3271 | } | |
3272 | } | |
3273 | ||
3274 | // Pass everything back to the middle-end. | |
3275 | ||
3276 | wrapup_global_declarations(defs, i); | |
3277 | ||
036165d8 CM |
3278 | delete[] defs; |
3279 | } | |
3280 | ||
f163907e ILT |
3281 | void |
3282 | Gcc_backend::write_export_data(const char* bytes, unsigned int size) | |
3283 | { | |
3284 | go_write_export_data(bytes, size); | |
3285 | } | |
3286 | ||
3287 | ||
90cbaa29 CM |
3288 | // Define a builtin function. BCODE is the builtin function code |
3289 | // defined by builtins.def. NAME is the name of the builtin function. | |
3290 | // LIBNAME is the name of the corresponding library function, and is | |
3291 | // NULL if there isn't one. FNTYPE is the type of the function. | |
3292 | // CONST_P is true if the function has the const attribute. | |
2021ff27 | 3293 | // NORETURN_P is true if the function has the noreturn attribute. |
a9ac13f7 | 3294 | |
90cbaa29 CM |
3295 | void |
3296 | Gcc_backend::define_builtin(built_in_function bcode, const char* name, | |
2021ff27 MP |
3297 | const char* libname, tree fntype, bool const_p, |
3298 | bool noreturn_p) | |
90cbaa29 CM |
3299 | { |
3300 | tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL, | |
3301 | libname, NULL_TREE); | |
3302 | if (const_p) | |
3303 | TREE_READONLY(decl) = 1; | |
2021ff27 MP |
3304 | if (noreturn_p) |
3305 | TREE_THIS_VOLATILE(decl) = 1; | |
90cbaa29 CM |
3306 | set_builtin_decl(bcode, decl, true); |
3307 | this->builtin_functions_[name] = this->make_function(decl); | |
3308 | if (libname != NULL) | |
3309 | { | |
3310 | decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL, | |
3311 | NULL, NULL_TREE); | |
3312 | if (const_p) | |
3313 | TREE_READONLY(decl) = 1; | |
2021ff27 MP |
3314 | if (noreturn_p) |
3315 | TREE_THIS_VOLATILE(decl) = 1; | |
90cbaa29 CM |
3316 | this->builtin_functions_[libname] = this->make_function(decl); |
3317 | } | |
3318 | } | |
a9ac13f7 ILT |
3319 | |
3320 | // Return the backend generator. | |
3321 | ||
3322 | Backend* | |
3323 | go_get_backend() | |
3324 | { | |
90cbaa29 | 3325 | return new Gcc_backend(); |
a9ac13f7 | 3326 | } |