]>
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); | |
dcf30625 ILT |
1052 | if (TYPE_NAME(pt) != NULL_TREE) |
1053 | { | |
1054 | // Build the data structure gcc wants to see for a typedef. | |
1055 | tree copy = build_variant_type_copy(pt); | |
1056 | TYPE_NAME(copy) = NULL_TREE; | |
1057 | DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy; | |
1058 | } | |
7fc2f86b ILT |
1059 | return true; |
1060 | } | |
1061 | ||
1062 | // Set the real values for a placeholder function type. | |
1063 | ||
1064 | bool | |
1065 | Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft) | |
1066 | { | |
1067 | return this->set_placeholder_pointer_type(placeholder, ft); | |
1068 | } | |
1069 | ||
1070 | // Create a placeholder for a struct type. | |
1071 | ||
1072 | Btype* | |
1073 | Gcc_backend::placeholder_struct_type(const std::string& name, | |
8afa2bfb | 1074 | Location location) |
7fc2f86b ILT |
1075 | { |
1076 | tree ret = make_node(RECORD_TYPE); | |
7c0434e5 ILT |
1077 | if (!name.empty()) |
1078 | { | |
1079 | tree decl = build_decl(location.gcc_location(), TYPE_DECL, | |
1080 | get_identifier_from_string(name), | |
1081 | ret); | |
1082 | TYPE_NAME(ret) = decl; | |
1083 | } | |
6d69c02e ILT |
1084 | return this->make_type(ret); |
1085 | } | |
1086 | ||
7fc2f86b ILT |
1087 | // Fill in the fields of a placeholder struct type. |
1088 | ||
1089 | bool | |
1090 | Gcc_backend::set_placeholder_struct_type( | |
1091 | Btype* placeholder, | |
1092 | const std::vector<Btyped_identifier>& fields) | |
1093 | { | |
1094 | tree t = placeholder->get_tree(); | |
1095 | gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE); | |
1096 | Btype* r = this->fill_in_struct(placeholder, fields); | |
dcf30625 | 1097 | |
7c0434e5 ILT |
1098 | if (TYPE_NAME(t) != NULL_TREE) |
1099 | { | |
1100 | // Build the data structure gcc wants to see for a typedef. | |
a0e8e630 | 1101 | tree copy = build_variant_type_copy(t); |
7c0434e5 ILT |
1102 | TYPE_NAME(copy) = NULL_TREE; |
1103 | DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy; | |
1104 | } | |
dcf30625 | 1105 | |
7fc2f86b ILT |
1106 | return r->get_tree() != error_mark_node; |
1107 | } | |
1108 | ||
1109 | // Create a placeholder for an array type. | |
1110 | ||
1111 | Btype* | |
1112 | Gcc_backend::placeholder_array_type(const std::string& name, | |
8afa2bfb | 1113 | Location location) |
7fc2f86b ILT |
1114 | { |
1115 | tree ret = make_node(ARRAY_TYPE); | |
8afa2bfb | 1116 | tree decl = build_decl(location.gcc_location(), TYPE_DECL, |
7fc2f86b ILT |
1117 | get_identifier_from_string(name), |
1118 | ret); | |
1119 | TYPE_NAME(ret) = decl; | |
1120 | return this->make_type(ret); | |
1121 | } | |
1122 | ||
1123 | // Fill in the fields of a placeholder array type. | |
1124 | ||
1125 | bool | |
1126 | Gcc_backend::set_placeholder_array_type(Btype* placeholder, | |
1127 | Btype* element_btype, | |
1128 | Bexpression* length) | |
1129 | { | |
1130 | tree t = placeholder->get_tree(); | |
1131 | gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE); | |
1132 | Btype* r = this->fill_in_array(placeholder, element_btype, length); | |
dcf30625 ILT |
1133 | |
1134 | // Build the data structure gcc wants to see for a typedef. | |
11304b7b | 1135 | tree copy = build_distinct_type_copy(t); |
dcf30625 ILT |
1136 | TYPE_NAME(copy) = NULL_TREE; |
1137 | DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy; | |
1138 | ||
7fc2f86b ILT |
1139 | return r->get_tree() != error_mark_node; |
1140 | } | |
1141 | ||
1142 | // Return a named version of a type. | |
1143 | ||
1144 | Btype* | |
1145 | Gcc_backend::named_type(const std::string& name, Btype* btype, | |
8afa2bfb | 1146 | Location location) |
7fc2f86b ILT |
1147 | { |
1148 | tree type = btype->get_tree(); | |
1149 | if (type == error_mark_node) | |
1150 | return this->error_type(); | |
11304b7b ILT |
1151 | |
1152 | // The middle-end expects a basic type to have a name. In Go every | |
1153 | // basic type will have a name. The first time we see a basic type, | |
1154 | // give it whatever Go name we have at this point. | |
1155 | if (TYPE_NAME(type) == NULL_TREE | |
1156 | && location.gcc_location() == BUILTINS_LOCATION | |
1157 | && (TREE_CODE(type) == INTEGER_TYPE | |
1158 | || TREE_CODE(type) == REAL_TYPE | |
1159 | || TREE_CODE(type) == COMPLEX_TYPE | |
1160 | || TREE_CODE(type) == BOOLEAN_TYPE)) | |
1161 | { | |
1162 | tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, | |
1163 | get_identifier_from_string(name), | |
1164 | type); | |
1165 | TYPE_NAME(type) = decl; | |
1166 | return this->make_type(type); | |
1167 | } | |
1168 | ||
dcf30625 | 1169 | tree copy = build_variant_type_copy(type); |
8afa2bfb | 1170 | tree decl = build_decl(location.gcc_location(), TYPE_DECL, |
7fc2f86b | 1171 | get_identifier_from_string(name), |
dcf30625 ILT |
1172 | copy); |
1173 | DECL_ORIGINAL_TYPE(decl) = type; | |
1174 | TYPE_NAME(copy) = decl; | |
1175 | return this->make_type(copy); | |
7fc2f86b ILT |
1176 | } |
1177 | ||
1178 | // Return a pointer type used as a marker for a circular type. | |
1179 | ||
1180 | Btype* | |
1181 | Gcc_backend::circular_pointer_type(Btype*, bool) | |
1182 | { | |
1183 | return this->make_type(ptr_type_node); | |
1184 | } | |
1185 | ||
1186 | // Return whether we might be looking at a circular type. | |
1187 | ||
1188 | bool | |
1189 | Gcc_backend::is_circular_pointer_type(Btype* btype) | |
1190 | { | |
1191 | return btype->get_tree() == ptr_type_node; | |
1192 | } | |
1193 | ||
ef1ed13d ILT |
1194 | // Return the size of a type. |
1195 | ||
18fbd1ec | 1196 | int64_t |
ef1ed13d ILT |
1197 | Gcc_backend::type_size(Btype* btype) |
1198 | { | |
08be22dc ILT |
1199 | tree t = btype->get_tree(); |
1200 | if (t == error_mark_node) | |
1201 | return 1; | |
c2a0fe56 ILT |
1202 | if (t == void_type_node) |
1203 | return 0; | |
08be22dc | 1204 | t = TYPE_SIZE_UNIT(t); |
807e902e | 1205 | gcc_assert(tree_fits_uhwi_p (t)); |
ef1ed13d | 1206 | unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t); |
18fbd1ec | 1207 | int64_t ret = static_cast<int64_t>(val_wide); |
ec23e5b3 CM |
1208 | if (ret < 0 || static_cast<unsigned HOST_WIDE_INT>(ret) != val_wide) |
1209 | return -1; | |
ef1ed13d ILT |
1210 | return ret; |
1211 | } | |
1212 | ||
1213 | // Return the alignment of a type. | |
1214 | ||
18fbd1ec | 1215 | int64_t |
ef1ed13d ILT |
1216 | Gcc_backend::type_alignment(Btype* btype) |
1217 | { | |
08be22dc ILT |
1218 | tree t = btype->get_tree(); |
1219 | if (t == error_mark_node) | |
1220 | return 1; | |
1221 | return TYPE_ALIGN_UNIT(t); | |
ef1ed13d ILT |
1222 | } |
1223 | ||
1224 | // Return the alignment of a struct field of type BTYPE. | |
1225 | ||
18fbd1ec | 1226 | int64_t |
ef1ed13d ILT |
1227 | Gcc_backend::type_field_alignment(Btype* btype) |
1228 | { | |
08be22dc ILT |
1229 | tree t = btype->get_tree(); |
1230 | if (t == error_mark_node) | |
1231 | return 1; | |
1232 | return go_field_alignment(t); | |
ef1ed13d ILT |
1233 | } |
1234 | ||
1235 | // Return the offset of a field in a struct. | |
1236 | ||
18fbd1ec | 1237 | int64_t |
ef1ed13d ILT |
1238 | Gcc_backend::type_field_offset(Btype* btype, size_t index) |
1239 | { | |
1240 | tree struct_tree = btype->get_tree(); | |
08be22dc ILT |
1241 | if (struct_tree == error_mark_node) |
1242 | return 0; | |
ef1ed13d ILT |
1243 | gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE); |
1244 | tree field = TYPE_FIELDS(struct_tree); | |
1245 | for (; index > 0; --index) | |
1246 | { | |
1247 | field = DECL_CHAIN(field); | |
1248 | gcc_assert(field != NULL_TREE); | |
1249 | } | |
1250 | HOST_WIDE_INT offset_wide = int_byte_position(field); | |
18fbd1ec ILT |
1251 | int64_t ret = static_cast<int64_t>(offset_wide); |
1252 | gcc_assert(ret == offset_wide); | |
ef1ed13d ILT |
1253 | return ret; |
1254 | } | |
1255 | ||
54466dde ILT |
1256 | // Return the zero value for a type. |
1257 | ||
1258 | Bexpression* | |
1259 | Gcc_backend::zero_expression(Btype* btype) | |
1260 | { | |
1261 | tree t = btype->get_tree(); | |
1262 | tree ret; | |
1263 | if (t == error_mark_node) | |
1264 | ret = error_mark_node; | |
1265 | else | |
1266 | ret = build_zero_cst(t); | |
6f7e0b57 | 1267 | return this->make_expression(ret); |
d4fe7beb CM |
1268 | } |
1269 | ||
1270 | // An expression that references a variable. | |
1271 | ||
1272 | Bexpression* | |
f60bea11 | 1273 | Gcc_backend::var_expression(Bvariable* var, Location location) |
d4fe7beb | 1274 | { |
399f5feb | 1275 | tree ret = var->get_tree(location); |
d4fe7beb CM |
1276 | if (ret == error_mark_node) |
1277 | return this->error_expression(); | |
6f7e0b57 | 1278 | return this->make_expression(ret); |
d4fe7beb CM |
1279 | } |
1280 | ||
1281 | // An expression that indirectly references an expression. | |
1282 | ||
1283 | Bexpression* | |
3e7b0938 CM |
1284 | Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr, |
1285 | bool known_valid, Location location) | |
d4fe7beb | 1286 | { |
3e7b0938 CM |
1287 | tree expr_tree = expr->get_tree(); |
1288 | tree type_tree = btype->get_tree(); | |
1289 | if (expr_tree == error_mark_node || type_tree == error_mark_node) | |
1290 | return this->error_expression(); | |
1291 | ||
1292 | // If the type of EXPR is a recursive pointer type, then we | |
1293 | // need to insert a cast before indirecting. | |
1294 | tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree)); | |
1295 | if (VOID_TYPE_P(target_type_tree)) | |
1296 | expr_tree = fold_convert_loc(location.gcc_location(), | |
1297 | build_pointer_type(type_tree), expr_tree); | |
1298 | ||
d4fe7beb | 1299 | tree ret = build_fold_indirect_ref_loc(location.gcc_location(), |
3e7b0938 | 1300 | expr_tree); |
d4fe7beb CM |
1301 | if (known_valid) |
1302 | TREE_THIS_NOTRAP(ret) = 1; | |
3e7b0938 | 1303 | return this->make_expression(ret); |
002ee4d1 CM |
1304 | } |
1305 | ||
e85baec7 CM |
1306 | // Return an expression that declares a constant named NAME with the |
1307 | // constant value VAL in BTYPE. | |
1308 | ||
1309 | Bexpression* | |
1310 | Gcc_backend::named_constant_expression(Btype* btype, const std::string& name, | |
1311 | Bexpression* val, Location location) | |
1312 | { | |
1313 | tree type_tree = btype->get_tree(); | |
1314 | tree const_val = val->get_tree(); | |
1315 | if (type_tree == error_mark_node || const_val == error_mark_node) | |
1316 | return this->error_expression(); | |
1317 | ||
1318 | tree name_tree = get_identifier_from_string(name); | |
1319 | tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree, | |
1320 | type_tree); | |
1321 | DECL_INITIAL(decl) = const_val; | |
1322 | TREE_CONSTANT(decl) = 1; | |
1323 | TREE_READONLY(decl) = 1; | |
1324 | ||
1325 | go_preserve_from_gc(decl); | |
1326 | return this->make_expression(decl); | |
1327 | } | |
1328 | ||
002ee4d1 CM |
1329 | // Return a typed value as a constant integer. |
1330 | ||
1331 | Bexpression* | |
1332 | Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val) | |
1333 | { | |
1334 | tree t = btype->get_tree(); | |
1335 | if (t == error_mark_node) | |
1336 | return this->error_expression(); | |
1337 | ||
1338 | tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true)); | |
6f7e0b57 | 1339 | return this->make_expression(ret); |
002ee4d1 CM |
1340 | } |
1341 | ||
1342 | // Return a typed value as a constant floating-point number. | |
1343 | ||
1344 | Bexpression* | |
1345 | Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val) | |
1346 | { | |
1347 | tree t = btype->get_tree(); | |
1348 | tree ret; | |
1349 | if (t == error_mark_node) | |
1350 | return this->error_expression(); | |
1351 | ||
1352 | REAL_VALUE_TYPE r1; | |
1353 | real_from_mpfr(&r1, val, t, GMP_RNDN); | |
1354 | REAL_VALUE_TYPE r2; | |
1355 | real_convert(&r2, TYPE_MODE(t), &r1); | |
1356 | ret = build_real(t, r2); | |
6f7e0b57 | 1357 | return this->make_expression(ret); |
002ee4d1 CM |
1358 | } |
1359 | ||
1360 | // Return a typed real and imaginary value as a constant complex number. | |
1361 | ||
1362 | Bexpression* | |
5eda5bad | 1363 | Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val) |
002ee4d1 CM |
1364 | { |
1365 | tree t = btype->get_tree(); | |
1366 | tree ret; | |
1367 | if (t == error_mark_node) | |
1368 | return this->error_expression(); | |
1369 | ||
1370 | REAL_VALUE_TYPE r1; | |
5eda5bad | 1371 | real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN); |
002ee4d1 CM |
1372 | REAL_VALUE_TYPE r2; |
1373 | real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1); | |
1374 | ||
1375 | REAL_VALUE_TYPE r3; | |
5eda5bad | 1376 | real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN); |
002ee4d1 CM |
1377 | REAL_VALUE_TYPE r4; |
1378 | real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3); | |
1379 | ||
1380 | ret = build_complex(t, build_real(TREE_TYPE(t), r2), | |
1381 | build_real(TREE_TYPE(t), r4)); | |
6f7e0b57 | 1382 | return this->make_expression(ret); |
c6d2bfbb CM |
1383 | } |
1384 | ||
7035307e CM |
1385 | // Make a constant string expression. |
1386 | ||
1387 | Bexpression* | |
1388 | Gcc_backend::string_constant_expression(const std::string& val) | |
1389 | { | |
1390 | tree index_type = build_index_type(size_int(val.length())); | |
1391 | tree const_char_type = build_qualified_type(unsigned_char_type_node, | |
1392 | TYPE_QUAL_CONST); | |
1393 | tree string_type = build_array_type(const_char_type, index_type); | |
7035307e CM |
1394 | TYPE_STRING_FLAG(string_type) = 1; |
1395 | tree string_val = build_string(val.length(), val.data()); | |
1396 | TREE_TYPE(string_val) = string_type; | |
1397 | ||
1398 | return this->make_expression(string_val); | |
1399 | } | |
1400 | ||
6f7e0b57 CM |
1401 | // Make a constant boolean expression. |
1402 | ||
1403 | Bexpression* | |
1404 | Gcc_backend::boolean_constant_expression(bool val) | |
1405 | { | |
1406 | tree bool_cst = val ? boolean_true_node : boolean_false_node; | |
1407 | return this->make_expression(bool_cst); | |
1408 | } | |
1409 | ||
7035307e CM |
1410 | // Return the real part of a complex expression. |
1411 | ||
1412 | Bexpression* | |
1413 | Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location) | |
1414 | { | |
1415 | tree complex_tree = bcomplex->get_tree(); | |
1416 | if (complex_tree == error_mark_node) | |
1417 | return this->error_expression(); | |
1418 | gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree))); | |
1419 | tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR, | |
1420 | TREE_TYPE(TREE_TYPE(complex_tree)), | |
1421 | complex_tree); | |
1422 | return this->make_expression(ret); | |
1423 | } | |
1424 | ||
1425 | // Return the imaginary part of a complex expression. | |
1426 | ||
1427 | Bexpression* | |
1428 | Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location) | |
1429 | { | |
1430 | tree complex_tree = bcomplex->get_tree(); | |
1431 | if (complex_tree == error_mark_node) | |
1432 | return this->error_expression(); | |
1433 | gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree))); | |
1434 | tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR, | |
1435 | TREE_TYPE(TREE_TYPE(complex_tree)), | |
1436 | complex_tree); | |
1437 | return this->make_expression(ret); | |
1438 | } | |
1439 | ||
1440 | // Make a complex expression given its real and imaginary parts. | |
1441 | ||
1442 | Bexpression* | |
1443 | Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag, | |
1444 | Location location) | |
1445 | { | |
1446 | tree real_tree = breal->get_tree(); | |
1447 | tree imag_tree = bimag->get_tree(); | |
1448 | if (real_tree == error_mark_node || imag_tree == error_mark_node) | |
1449 | return this->error_expression(); | |
1450 | gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree)) | |
1451 | == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree))); | |
1452 | gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree))); | |
1453 | tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR, | |
1454 | build_complex_type(TREE_TYPE(real_tree)), | |
1455 | real_tree, imag_tree); | |
1456 | return this->make_expression(ret); | |
1457 | } | |
1458 | ||
c6d2bfbb CM |
1459 | // An expression that converts an expression to a different type. |
1460 | ||
1461 | Bexpression* | |
7035307e CM |
1462 | Gcc_backend::convert_expression(Btype* type, Bexpression* expr, |
1463 | Location location) | |
c6d2bfbb CM |
1464 | { |
1465 | tree type_tree = type->get_tree(); | |
1466 | tree expr_tree = expr->get_tree(); | |
7035307e CM |
1467 | if (type_tree == error_mark_node |
1468 | || expr_tree == error_mark_node | |
1469 | || TREE_TYPE(expr_tree) == error_mark_node) | |
c6d2bfbb CM |
1470 | return this->error_expression(); |
1471 | ||
7035307e | 1472 | tree ret; |
c2a0fe56 ILT |
1473 | if (this->type_size(type) == 0 |
1474 | || TREE_TYPE(expr_tree) == void_type_node) | |
7035307e CM |
1475 | { |
1476 | // Do not convert zero-sized types. | |
1477 | ret = expr_tree; | |
1478 | } | |
1479 | else if (TREE_CODE(type_tree) == INTEGER_TYPE) | |
1480 | ret = fold(convert_to_integer(type_tree, expr_tree)); | |
1481 | else if (TREE_CODE(type_tree) == REAL_TYPE) | |
1482 | ret = fold(convert_to_real(type_tree, expr_tree)); | |
1483 | else if (TREE_CODE(type_tree) == COMPLEX_TYPE) | |
1484 | ret = fold(convert_to_complex(type_tree, expr_tree)); | |
1485 | else if (TREE_CODE(type_tree) == POINTER_TYPE | |
1486 | && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE) | |
1487 | ret = fold(convert_to_pointer(type_tree, expr_tree)); | |
1488 | else if (TREE_CODE(type_tree) == RECORD_TYPE | |
1489 | || TREE_CODE(type_tree) == ARRAY_TYPE) | |
1490 | ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR, | |
1491 | type_tree, expr_tree); | |
1492 | else | |
1493 | ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree); | |
1494 | ||
1495 | return this->make_expression(ret); | |
54466dde ILT |
1496 | } |
1497 | ||
b7d93b46 CM |
1498 | // Get the address of a function. |
1499 | ||
1500 | Bexpression* | |
1501 | Gcc_backend::function_code_expression(Bfunction* bfunc, Location location) | |
1502 | { | |
1503 | tree func = bfunc->get_tree(); | |
1504 | if (func == error_mark_node) | |
1505 | return this->error_expression(); | |
1506 | ||
1507 | tree ret = build_fold_addr_expr_loc(location.gcc_location(), func); | |
1508 | return this->make_expression(ret); | |
1509 | } | |
1510 | ||
b93e0cfd CM |
1511 | // Get the address of an expression. |
1512 | ||
1513 | Bexpression* | |
1514 | Gcc_backend::address_expression(Bexpression* bexpr, Location location) | |
1515 | { | |
1516 | tree expr = bexpr->get_tree(); | |
1517 | if (expr == error_mark_node) | |
1518 | return this->error_expression(); | |
1519 | ||
1520 | tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr); | |
1521 | return this->make_expression(ret); | |
1522 | } | |
1523 | ||
71eba7a0 CM |
1524 | // Return an expression for the field at INDEX in BSTRUCT. |
1525 | ||
1526 | Bexpression* | |
1527 | Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index, | |
1528 | Location location) | |
1529 | { | |
1530 | tree struct_tree = bstruct->get_tree(); | |
1531 | if (struct_tree == error_mark_node | |
1532 | || TREE_TYPE(struct_tree) == error_mark_node) | |
1533 | return this->error_expression(); | |
1534 | gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE); | |
1535 | tree field = TYPE_FIELDS(TREE_TYPE(struct_tree)); | |
1536 | if (field == NULL_TREE) | |
1537 | { | |
1538 | // This can happen for a type which refers to itself indirectly | |
1539 | // and then turns out to be erroneous. | |
1540 | return this->error_expression(); | |
1541 | } | |
1542 | for (unsigned int i = index; i > 0; --i) | |
1543 | { | |
1544 | field = DECL_CHAIN(field); | |
1545 | gcc_assert(field != NULL_TREE); | |
1546 | } | |
1547 | if (TREE_TYPE(field) == error_mark_node) | |
1548 | return this->error_expression(); | |
1549 | tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF, | |
1550 | TREE_TYPE(field), struct_tree, field, | |
1551 | NULL_TREE); | |
1552 | if (TREE_CONSTANT(struct_tree)) | |
1553 | TREE_CONSTANT(ret) = 1; | |
6f7e0b57 | 1554 | return this->make_expression(ret); |
71eba7a0 CM |
1555 | } |
1556 | ||
eb6eb862 CM |
1557 | // Return an expression that executes BSTAT before BEXPR. |
1558 | ||
1559 | Bexpression* | |
1560 | Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr, | |
1561 | Location location) | |
1562 | { | |
1563 | tree stat = bstat->get_tree(); | |
1564 | tree expr = bexpr->get_tree(); | |
1565 | if (stat == error_mark_node || expr == error_mark_node) | |
1566 | return this->error_expression(); | |
1567 | tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, | |
1568 | TREE_TYPE(expr), stat, expr); | |
1569 | return this->make_expression(ret); | |
1570 | } | |
1571 | ||
1572 | // Return an expression that executes THEN_EXPR if CONDITION is true, or | |
1573 | // ELSE_EXPR otherwise. | |
1574 | ||
1575 | Bexpression* | |
a2182c9c TM |
1576 | Gcc_backend::conditional_expression(Bfunction*, Btype* btype, |
1577 | Bexpression* condition, | |
eb6eb862 CM |
1578 | Bexpression* then_expr, |
1579 | Bexpression* else_expr, Location location) | |
1580 | { | |
b5407ad1 | 1581 | tree type_tree = btype == NULL ? void_type_node : btype->get_tree(); |
eb6eb862 CM |
1582 | tree cond_tree = condition->get_tree(); |
1583 | tree then_tree = then_expr->get_tree(); | |
1584 | tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree(); | |
b5407ad1 CM |
1585 | if (type_tree == error_mark_node |
1586 | || cond_tree == error_mark_node | |
eb6eb862 CM |
1587 | || then_tree == error_mark_node |
1588 | || else_tree == error_mark_node) | |
1589 | return this->error_expression(); | |
b5407ad1 | 1590 | tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree, |
eb6eb862 CM |
1591 | cond_tree, then_tree, else_tree); |
1592 | return this->make_expression(ret); | |
1593 | } | |
1594 | ||
8a35e18d CM |
1595 | // Return an expression for the unary operation OP EXPR. |
1596 | ||
1597 | Bexpression* | |
1598 | Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location) | |
1599 | { | |
1600 | tree expr_tree = expr->get_tree(); | |
1601 | if (expr_tree == error_mark_node | |
1602 | || TREE_TYPE(expr_tree) == error_mark_node) | |
1603 | return this->error_expression(); | |
1604 | ||
1605 | tree type_tree = TREE_TYPE(expr_tree); | |
1606 | enum tree_code code; | |
1607 | switch (op) | |
1608 | { | |
1609 | case OPERATOR_MINUS: | |
1610 | { | |
1611 | tree computed_type = excess_precision_type(type_tree); | |
1612 | if (computed_type != NULL_TREE) | |
1613 | { | |
1614 | expr_tree = convert(computed_type, expr_tree); | |
1615 | type_tree = computed_type; | |
1616 | } | |
1617 | code = NEGATE_EXPR; | |
1618 | break; | |
1619 | } | |
1620 | case OPERATOR_NOT: | |
1621 | code = TRUTH_NOT_EXPR; | |
1622 | break; | |
1623 | case OPERATOR_XOR: | |
1624 | code = BIT_NOT_EXPR; | |
1625 | break; | |
1626 | default: | |
1627 | gcc_unreachable(); | |
1628 | break; | |
1629 | } | |
1630 | ||
1631 | tree ret = fold_build1_loc(location.gcc_location(), code, type_tree, | |
1632 | expr_tree); | |
1633 | return this->make_expression(ret); | |
1634 | } | |
1635 | ||
b5407ad1 CM |
1636 | // Convert a gofrontend operator to an equivalent tree_code. |
1637 | ||
1638 | static enum tree_code | |
1639 | operator_to_tree_code(Operator op, tree type) | |
1640 | { | |
1641 | enum tree_code code; | |
1642 | switch (op) | |
1643 | { | |
1644 | case OPERATOR_EQEQ: | |
1645 | code = EQ_EXPR; | |
1646 | break; | |
1647 | case OPERATOR_NOTEQ: | |
1648 | code = NE_EXPR; | |
1649 | break; | |
1650 | case OPERATOR_LT: | |
1651 | code = LT_EXPR; | |
1652 | break; | |
1653 | case OPERATOR_LE: | |
1654 | code = LE_EXPR; | |
1655 | break; | |
1656 | case OPERATOR_GT: | |
1657 | code = GT_EXPR; | |
1658 | break; | |
1659 | case OPERATOR_GE: | |
1660 | code = GE_EXPR; | |
1661 | break; | |
1662 | case OPERATOR_OROR: | |
1663 | code = TRUTH_ORIF_EXPR; | |
1664 | break; | |
1665 | case OPERATOR_ANDAND: | |
1666 | code = TRUTH_ANDIF_EXPR; | |
1667 | break; | |
1668 | case OPERATOR_PLUS: | |
1669 | code = PLUS_EXPR; | |
1670 | break; | |
1671 | case OPERATOR_MINUS: | |
1672 | code = MINUS_EXPR; | |
1673 | break; | |
1674 | case OPERATOR_OR: | |
1675 | code = BIT_IOR_EXPR; | |
1676 | break; | |
1677 | case OPERATOR_XOR: | |
1678 | code = BIT_XOR_EXPR; | |
1679 | break; | |
1680 | case OPERATOR_MULT: | |
1681 | code = MULT_EXPR; | |
1682 | break; | |
1683 | case OPERATOR_DIV: | |
1684 | if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE) | |
1685 | code = RDIV_EXPR; | |
1686 | else | |
1687 | code = TRUNC_DIV_EXPR; | |
1688 | break; | |
1689 | case OPERATOR_MOD: | |
1690 | code = TRUNC_MOD_EXPR; | |
1691 | break; | |
1692 | case OPERATOR_LSHIFT: | |
1693 | code = LSHIFT_EXPR; | |
1694 | break; | |
1695 | case OPERATOR_RSHIFT: | |
1696 | code = RSHIFT_EXPR; | |
1697 | break; | |
1698 | case OPERATOR_AND: | |
1699 | code = BIT_AND_EXPR; | |
1700 | break; | |
1701 | case OPERATOR_BITCLEAR: | |
1702 | code = BIT_AND_EXPR; | |
1703 | break; | |
1704 | default: | |
1705 | gcc_unreachable(); | |
1706 | } | |
1707 | ||
1708 | return code; | |
1709 | } | |
1710 | ||
1711 | // Return an expression for the binary operation LEFT OP RIGHT. | |
1712 | ||
1713 | Bexpression* | |
1714 | Gcc_backend::binary_expression(Operator op, Bexpression* left, | |
1715 | Bexpression* right, Location location) | |
1716 | { | |
1717 | tree left_tree = left->get_tree(); | |
1718 | tree right_tree = right->get_tree(); | |
1719 | if (left_tree == error_mark_node | |
1720 | || right_tree == error_mark_node) | |
1721 | return this->error_expression(); | |
1722 | enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree)); | |
1723 | ||
1724 | bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND; | |
1725 | tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree); | |
1726 | tree computed_type = excess_precision_type(type_tree); | |
1727 | if (computed_type != NULL_TREE) | |
1728 | { | |
1729 | left_tree = convert(computed_type, left_tree); | |
1730 | right_tree = convert(computed_type, right_tree); | |
1731 | type_tree = computed_type; | |
1732 | } | |
1733 | ||
1734 | // For comparison operators, the resulting type should be boolean. | |
1735 | switch (op) | |
1736 | { | |
1737 | case OPERATOR_EQEQ: | |
1738 | case OPERATOR_NOTEQ: | |
1739 | case OPERATOR_LT: | |
1740 | case OPERATOR_LE: | |
1741 | case OPERATOR_GT: | |
1742 | case OPERATOR_GE: | |
1743 | type_tree = boolean_type_node; | |
1744 | break; | |
1745 | default: | |
1746 | break; | |
1747 | } | |
1748 | ||
1749 | tree ret = fold_build2_loc(location.gcc_location(), code, type_tree, | |
1750 | left_tree, right_tree); | |
1751 | return this->make_expression(ret); | |
1752 | } | |
1753 | ||
7035307e CM |
1754 | // Return an expression that constructs BTYPE with VALS. |
1755 | ||
1756 | Bexpression* | |
1757 | Gcc_backend::constructor_expression(Btype* btype, | |
1758 | const std::vector<Bexpression*>& vals, | |
1759 | Location location) | |
1760 | { | |
1761 | tree type_tree = btype->get_tree(); | |
1762 | if (type_tree == error_mark_node) | |
1763 | return this->error_expression(); | |
1764 | ||
1765 | vec<constructor_elt, va_gc> *init; | |
1766 | vec_alloc(init, vals.size()); | |
1767 | ||
861393ac | 1768 | tree sink = NULL_TREE; |
7035307e CM |
1769 | bool is_constant = true; |
1770 | tree field = TYPE_FIELDS(type_tree); | |
1771 | for (std::vector<Bexpression*>::const_iterator p = vals.begin(); | |
1772 | p != vals.end(); | |
1773 | ++p, field = DECL_CHAIN(field)) | |
1774 | { | |
1775 | gcc_assert(field != NULL_TREE); | |
1776 | tree val = (*p)->get_tree(); | |
1777 | if (TREE_TYPE(field) == error_mark_node | |
1778 | || val == error_mark_node | |
1779 | || TREE_TYPE(val) == error_mark_node) | |
1780 | return this->error_expression(); | |
1781 | ||
861393ac CM |
1782 | if (int_size_in_bytes(TREE_TYPE(field)) == 0) |
1783 | { | |
1784 | // GIMPLE cannot represent indices of zero-sized types so | |
1785 | // trying to construct a map with zero-sized keys might lead | |
1786 | // to errors. Instead, we evaluate each expression that | |
1787 | // would have been added as a map element for its | |
1788 | // side-effects and construct an empty map. | |
1789 | append_to_statement_list(val, &sink); | |
1790 | continue; | |
1791 | } | |
1792 | ||
7035307e CM |
1793 | constructor_elt empty = {NULL, NULL}; |
1794 | constructor_elt* elt = init->quick_push(empty); | |
1795 | elt->index = field; | |
ebc2f401 | 1796 | elt->value = this->convert_tree(TREE_TYPE(field), val, location); |
7035307e CM |
1797 | if (!TREE_CONSTANT(elt->value)) |
1798 | is_constant = false; | |
1799 | } | |
1800 | gcc_assert(field == NULL_TREE); | |
1801 | tree ret = build_constructor(type_tree, init); | |
1802 | if (is_constant) | |
1803 | TREE_CONSTANT(ret) = 1; | |
861393ac CM |
1804 | if (sink != NULL_TREE) |
1805 | ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, | |
1806 | type_tree, sink, ret); | |
7035307e CM |
1807 | return this->make_expression(ret); |
1808 | } | |
1809 | ||
1810 | Bexpression* | |
1811 | Gcc_backend::array_constructor_expression( | |
1812 | Btype* array_btype, const std::vector<unsigned long>& indexes, | |
943cf9cf | 1813 | const std::vector<Bexpression*>& vals, Location location) |
7035307e CM |
1814 | { |
1815 | tree type_tree = array_btype->get_tree(); | |
1816 | if (type_tree == error_mark_node) | |
1817 | return this->error_expression(); | |
1818 | ||
1819 | gcc_assert(indexes.size() == vals.size()); | |
943cf9cf CM |
1820 | |
1821 | tree element_type = TREE_TYPE(type_tree); | |
1822 | HOST_WIDE_INT element_size = int_size_in_bytes(element_type); | |
7035307e | 1823 | vec<constructor_elt, va_gc> *init; |
943cf9cf | 1824 | vec_alloc(init, element_size == 0 ? 0 : vals.size()); |
7035307e | 1825 | |
943cf9cf | 1826 | tree sink = NULL_TREE; |
7035307e CM |
1827 | bool is_constant = true; |
1828 | for (size_t i = 0; i < vals.size(); ++i) | |
1829 | { | |
1830 | tree index = size_int(indexes[i]); | |
1831 | tree val = (vals[i])->get_tree(); | |
1832 | ||
1833 | if (index == error_mark_node | |
1834 | || val == error_mark_node) | |
1835 | return this->error_expression(); | |
1836 | ||
943cf9cf CM |
1837 | if (element_size == 0) |
1838 | { | |
1839 | // GIMPLE cannot represent arrays of zero-sized types so trying | |
1840 | // to construct an array of zero-sized values might lead to errors. | |
1841 | // Instead, we evaluate each expression that would have been added as | |
1842 | // an array value for its side-effects and construct an empty array. | |
1843 | append_to_statement_list(val, &sink); | |
1844 | continue; | |
1845 | } | |
1846 | ||
7035307e CM |
1847 | if (!TREE_CONSTANT(val)) |
1848 | is_constant = false; | |
1849 | ||
1850 | constructor_elt empty = {NULL, NULL}; | |
1851 | constructor_elt* elt = init->quick_push(empty); | |
1852 | elt->index = index; | |
1853 | elt->value = val; | |
1854 | } | |
1855 | ||
1856 | tree ret = build_constructor(type_tree, init); | |
1857 | if (is_constant) | |
1858 | TREE_CONSTANT(ret) = 1; | |
943cf9cf CM |
1859 | if (sink != NULL_TREE) |
1860 | ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, | |
1861 | type_tree, sink, ret); | |
7035307e CM |
1862 | return this->make_expression(ret); |
1863 | } | |
1864 | ||
1865 | // Return an expression for the address of BASE[INDEX]. | |
1866 | ||
1867 | Bexpression* | |
1868 | Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index, | |
1869 | Location location) | |
1870 | { | |
1871 | tree base_tree = base->get_tree(); | |
1872 | tree index_tree = index->get_tree(); | |
1873 | tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree)); | |
1874 | if (base_tree == error_mark_node | |
1875 | || TREE_TYPE(base_tree) == error_mark_node | |
1876 | || index_tree == error_mark_node | |
1877 | || element_type_tree == error_mark_node) | |
1878 | return this->error_expression(); | |
1879 | ||
1880 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); | |
1881 | index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree); | |
1882 | tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype, | |
1883 | index_tree, element_size); | |
1884 | tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR, | |
1885 | TREE_TYPE(base_tree), base_tree, offset); | |
1886 | return this->make_expression(ptr); | |
1887 | } | |
1888 | ||
1889 | // Return an expression representing ARRAY[INDEX] | |
1890 | ||
1891 | Bexpression* | |
1892 | Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index, | |
1893 | Location location) | |
1894 | { | |
1895 | tree array_tree = array->get_tree(); | |
1896 | tree index_tree = index->get_tree(); | |
1897 | if (array_tree == error_mark_node | |
1898 | || TREE_TYPE(array_tree) == error_mark_node | |
1899 | || index_tree == error_mark_node) | |
1900 | return this->error_expression(); | |
1901 | ||
c2a0fe56 ILT |
1902 | // A function call that returns a zero sized object will have been |
1903 | // changed to return void. If we see void here, assume we are | |
1904 | // dealing with a zero sized type and just evaluate the operands. | |
1905 | tree ret; | |
1906 | if (TREE_TYPE(array_tree) != void_type_node) | |
1907 | ret = build4_loc(location.gcc_location(), ARRAY_REF, | |
1908 | TREE_TYPE(TREE_TYPE(array_tree)), array_tree, | |
1909 | index_tree, NULL_TREE, NULL_TREE); | |
1910 | else | |
1911 | ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, | |
1912 | void_type_node, array_tree, index_tree); | |
1913 | ||
7035307e CM |
1914 | return this->make_expression(ret); |
1915 | } | |
1916 | ||
1917 | // Create an expression for a call to FN_EXPR with FN_ARGS. | |
1918 | Bexpression* | |
da55a299 TM |
1919 | Gcc_backend::call_expression(Bfunction*, // containing fcn for call |
1920 | Bexpression* fn_expr, | |
7035307e | 1921 | const std::vector<Bexpression*>& fn_args, |
da55a299 TM |
1922 | Bexpression* chain_expr, |
1923 | Location location) | |
7035307e CM |
1924 | { |
1925 | tree fn = fn_expr->get_tree(); | |
1926 | if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node) | |
1927 | return this->error_expression(); | |
1928 | ||
1929 | gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn))); | |
1930 | tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn))); | |
1931 | ||
1932 | size_t nargs = fn_args.size(); | |
1933 | tree* args = nargs == 0 ? NULL : new tree[nargs]; | |
1934 | for (size_t i = 0; i < nargs; ++i) | |
1935 | { | |
1936 | args[i] = fn_args.at(i)->get_tree(); | |
1937 | if (args[i] == error_mark_node) | |
1938 | return this->error_expression(); | |
1939 | } | |
1940 | ||
1941 | tree fndecl = fn; | |
1942 | if (TREE_CODE(fndecl) == ADDR_EXPR) | |
1943 | fndecl = TREE_OPERAND(fndecl, 0); | |
1944 | ||
1945 | // This is to support builtin math functions when using 80387 math. | |
1946 | tree excess_type = NULL_TREE; | |
1947 | if (optimize | |
1948 | && TREE_CODE(fndecl) == FUNCTION_DECL | |
3d78e008 ML |
1949 | && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL) |
1950 | && DECL_IS_BUILTIN (fndecl) | |
7035307e CM |
1951 | && nargs > 0 |
1952 | && ((SCALAR_FLOAT_TYPE_P(rettype) | |
1953 | && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0]))) | |
1954 | || (COMPLEX_FLOAT_TYPE_P(rettype) | |
1955 | && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0]))))) | |
1956 | { | |
1957 | excess_type = excess_precision_type(TREE_TYPE(args[0])); | |
1958 | if (excess_type != NULL_TREE) | |
1959 | { | |
1960 | tree excess_fndecl = mathfn_built_in(excess_type, | |
1961 | DECL_FUNCTION_CODE(fndecl)); | |
1962 | if (excess_fndecl == NULL_TREE) | |
1963 | excess_type = NULL_TREE; | |
1964 | else | |
1965 | { | |
1966 | fn = build_fold_addr_expr_loc(location.gcc_location(), | |
1967 | excess_fndecl); | |
1968 | for (size_t i = 0; i < nargs; ++i) | |
1969 | { | |
1970 | if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i])) | |
1971 | || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i]))) | |
1972 | args[i] = ::convert(excess_type, args[i]); | |
1973 | } | |
1974 | } | |
1975 | } | |
1976 | } | |
1977 | ||
1978 | tree ret = | |
1979 | build_call_array_loc(location.gcc_location(), | |
1980 | excess_type != NULL_TREE ? excess_type : rettype, | |
1981 | fn, nargs, args); | |
1982 | ||
38bf819a RH |
1983 | if (chain_expr) |
1984 | CALL_EXPR_STATIC_CHAIN (ret) = chain_expr->get_tree(); | |
1985 | ||
7035307e CM |
1986 | if (excess_type != NULL_TREE) |
1987 | { | |
1988 | // Calling convert here can undo our excess precision change. | |
1989 | // That may or may not be a bug in convert_to_real. | |
1990 | ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret); | |
1991 | } | |
1992 | ||
1993 | delete[] args; | |
1994 | return this->make_expression(ret); | |
1995 | } | |
1996 | ||
cfebcf30 ILT |
1997 | // An expression as a statement. |
1998 | ||
1999 | Bstatement* | |
8220e3f9 | 2000 | Gcc_backend::expression_statement(Bfunction*, Bexpression* expr) |
cfebcf30 ILT |
2001 | { |
2002 | return this->make_statement(expr->get_tree()); | |
2003 | } | |
2004 | ||
e09ce6c5 ILT |
2005 | // Variable initialization. |
2006 | ||
2007 | Bstatement* | |
8220e3f9 | 2008 | Gcc_backend::init_statement(Bfunction*, Bvariable* var, Bexpression* init) |
e09ce6c5 | 2009 | { |
399f5feb | 2010 | tree var_tree = var->get_decl(); |
e09ce6c5 ILT |
2011 | tree init_tree = init->get_tree(); |
2012 | if (var_tree == error_mark_node || init_tree == error_mark_node) | |
2013 | return this->error_statement(); | |
2014 | gcc_assert(TREE_CODE(var_tree) == VAR_DECL); | |
7d6be4c8 ILT |
2015 | |
2016 | // To avoid problems with GNU ld, we don't make zero-sized | |
2017 | // externally visible variables. That might lead us to doing an | |
2018 | // initialization of a zero-sized expression to a non-zero sized | |
2019 | // variable, or vice-versa. Avoid crashes by omitting the | |
2020 | // initializer. Such initializations don't mean anything anyhow. | |
2021 | if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0 | |
2022 | && init_tree != NULL_TREE | |
c2a0fe56 | 2023 | && TREE_TYPE(init_tree) != void_type_node |
7d6be4c8 ILT |
2024 | && int_size_in_bytes(TREE_TYPE(init_tree)) != 0) |
2025 | { | |
2026 | DECL_INITIAL(var_tree) = init_tree; | |
2027 | init_tree = NULL_TREE; | |
2028 | } | |
2029 | ||
2030 | tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR, | |
2031 | void_type_node, var_tree); | |
2032 | if (init_tree != NULL_TREE) | |
2033 | ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR, | |
2034 | void_type_node, init_tree, ret); | |
2035 | ||
2036 | return this->make_statement(ret); | |
e09ce6c5 ILT |
2037 | } |
2038 | ||
a9ac13f7 ILT |
2039 | // Assignment. |
2040 | ||
2041 | Bstatement* | |
8220e3f9 TM |
2042 | Gcc_backend::assignment_statement(Bfunction* bfn, Bexpression* lhs, |
2043 | Bexpression* rhs, Location location) | |
a9ac13f7 | 2044 | { |
94039447 ILT |
2045 | tree lhs_tree = lhs->get_tree(); |
2046 | tree rhs_tree = rhs->get_tree(); | |
2047 | if (lhs_tree == error_mark_node || rhs_tree == error_mark_node) | |
00b44a6e | 2048 | return this->error_statement(); |
7d6be4c8 ILT |
2049 | |
2050 | // To avoid problems with GNU ld, we don't make zero-sized | |
2051 | // externally visible variables. That might lead us to doing an | |
2052 | // assignment of a zero-sized expression to a non-zero sized | |
2053 | // expression; avoid crashes here by avoiding assignments of | |
2054 | // zero-sized expressions. Such assignments don't really mean | |
2055 | // anything anyhow. | |
c2a0fe56 ILT |
2056 | if (TREE_TYPE(lhs_tree) == void_type_node |
2057 | || int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0 | |
2058 | || TREE_TYPE(rhs_tree) == void_type_node | |
7d6be4c8 | 2059 | || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0) |
8220e3f9 TM |
2060 | return this->compound_statement(this->expression_statement(bfn, lhs), |
2061 | this->expression_statement(bfn, rhs)); | |
7d6be4c8 | 2062 | |
ebc2f401 | 2063 | rhs_tree = this->convert_tree(TREE_TYPE(lhs_tree), rhs_tree, location); |
68c5d97b | 2064 | |
8afa2bfb SD |
2065 | return this->make_statement(fold_build2_loc(location.gcc_location(), |
2066 | MODIFY_EXPR, | |
a9ac13f7 | 2067 | void_type_node, |
94039447 ILT |
2068 | lhs_tree, rhs_tree)); |
2069 | } | |
2070 | ||
2071 | // Return. | |
2072 | ||
2073 | Bstatement* | |
2074 | Gcc_backend::return_statement(Bfunction* bfunction, | |
2075 | const std::vector<Bexpression*>& vals, | |
8afa2bfb | 2076 | Location location) |
94039447 ILT |
2077 | { |
2078 | tree fntree = bfunction->get_tree(); | |
2079 | if (fntree == error_mark_node) | |
00b44a6e | 2080 | return this->error_statement(); |
94039447 ILT |
2081 | tree result = DECL_RESULT(fntree); |
2082 | if (result == error_mark_node) | |
00b44a6e | 2083 | return this->error_statement(); |
036165d8 | 2084 | |
f432d128 ILT |
2085 | // If the result size is zero bytes, we have set the function type |
2086 | // to have a result type of void, so don't return anything. | |
2087 | // See the function_type method. | |
1f599d75 TM |
2088 | tree res_type = TREE_TYPE(result); |
2089 | if (res_type == void_type_node || int_size_in_bytes(res_type) == 0) | |
f432d128 ILT |
2090 | { |
2091 | tree stmt_list = NULL_TREE; | |
2092 | for (std::vector<Bexpression*>::const_iterator p = vals.begin(); | |
2093 | p != vals.end(); | |
2094 | p++) | |
2095 | { | |
2096 | tree val = (*p)->get_tree(); | |
2097 | if (val == error_mark_node) | |
2098 | return this->error_statement(); | |
2099 | append_to_statement_list(val, &stmt_list); | |
2100 | } | |
2101 | tree ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, | |
2102 | void_type_node, NULL_TREE); | |
2103 | append_to_statement_list(ret, &stmt_list); | |
2104 | return this->make_statement(stmt_list); | |
2105 | } | |
2106 | ||
94039447 ILT |
2107 | tree ret; |
2108 | if (vals.empty()) | |
8afa2bfb SD |
2109 | ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node, |
2110 | NULL_TREE); | |
94039447 ILT |
2111 | else if (vals.size() == 1) |
2112 | { | |
2113 | tree val = vals.front()->get_tree(); | |
2114 | if (val == error_mark_node) | |
00b44a6e | 2115 | return this->error_statement(); |
8afa2bfb SD |
2116 | tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR, |
2117 | void_type_node, result, | |
2118 | vals.front()->get_tree()); | |
2119 | ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, | |
2120 | void_type_node, set); | |
94039447 ILT |
2121 | } |
2122 | else | |
2123 | { | |
2124 | // To return multiple values, copy the values into a temporary | |
2125 | // variable of the right structure type, and then assign the | |
2126 | // temporary variable to the DECL_RESULT in the return | |
2127 | // statement. | |
2128 | tree stmt_list = NULL_TREE; | |
2129 | tree rettype = TREE_TYPE(result); | |
036165d8 CM |
2130 | |
2131 | if (DECL_STRUCT_FUNCTION(fntree) == NULL) | |
2132 | push_struct_function(fntree); | |
2133 | else | |
2134 | push_cfun(DECL_STRUCT_FUNCTION(fntree)); | |
94039447 | 2135 | tree rettmp = create_tmp_var(rettype, "RESULT"); |
036165d8 CM |
2136 | pop_cfun(); |
2137 | ||
94039447 ILT |
2138 | tree field = TYPE_FIELDS(rettype); |
2139 | for (std::vector<Bexpression*>::const_iterator p = vals.begin(); | |
2140 | p != vals.end(); | |
2141 | p++, field = DECL_CHAIN(field)) | |
2142 | { | |
2143 | gcc_assert(field != NULL_TREE); | |
8afa2bfb SD |
2144 | tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF, |
2145 | TREE_TYPE(field), rettmp, field, | |
2146 | NULL_TREE); | |
94039447 ILT |
2147 | tree val = (*p)->get_tree(); |
2148 | if (val == error_mark_node) | |
00b44a6e | 2149 | return this->error_statement(); |
8afa2bfb SD |
2150 | tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR, |
2151 | void_type_node, | |
94039447 ILT |
2152 | ref, (*p)->get_tree()); |
2153 | append_to_statement_list(set, &stmt_list); | |
2154 | } | |
2155 | gcc_assert(field == NULL_TREE); | |
8afa2bfb SD |
2156 | tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR, |
2157 | void_type_node, | |
94039447 | 2158 | result, rettmp); |
8afa2bfb SD |
2159 | tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR, |
2160 | void_type_node, set); | |
94039447 ILT |
2161 | append_to_statement_list(ret_expr, &stmt_list); |
2162 | ret = stmt_list; | |
2163 | } | |
2164 | return this->make_statement(ret); | |
a9ac13f7 ILT |
2165 | } |
2166 | ||
7035307e CM |
2167 | // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an |
2168 | // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not | |
2169 | // NULL, it will always be executed. This is used for handling defers in Go | |
2170 | // functions. In C++, the resulting code is of this form: | |
2171 | // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; } | |
2172 | ||
2173 | Bstatement* | |
2174 | Gcc_backend::exception_handler_statement(Bstatement* bstat, | |
2175 | Bstatement* except_stmt, | |
2176 | Bstatement* finally_stmt, | |
2177 | Location location) | |
2178 | { | |
2179 | tree stat_tree = bstat->get_tree(); | |
2180 | tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree(); | |
2181 | tree finally_tree = finally_stmt == NULL | |
2182 | ? NULL_TREE | |
2183 | : finally_stmt->get_tree(); | |
2184 | ||
2185 | if (stat_tree == error_mark_node | |
2186 | || except_tree == error_mark_node | |
2187 | || finally_tree == error_mark_node) | |
2188 | return this->error_statement(); | |
2189 | ||
2190 | if (except_tree != NULL_TREE) | |
2191 | stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR, | |
2192 | void_type_node, stat_tree, | |
2193 | build2_loc(location.gcc_location(), CATCH_EXPR, | |
2194 | void_type_node, NULL, except_tree)); | |
2195 | if (finally_tree != NULL_TREE) | |
2196 | stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR, | |
2197 | void_type_node, stat_tree, finally_tree); | |
2198 | return this->make_statement(stat_tree); | |
2199 | } | |
2200 | ||
db0adf82 ILT |
2201 | // If. |
2202 | ||
2203 | Bstatement* | |
8220e3f9 TM |
2204 | Gcc_backend::if_statement(Bfunction*, Bexpression* condition, |
2205 | Bblock* then_block, Bblock* else_block, | |
2206 | Location location) | |
db0adf82 ILT |
2207 | { |
2208 | tree cond_tree = condition->get_tree(); | |
2209 | tree then_tree = then_block->get_tree(); | |
2210 | tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree(); | |
2211 | if (cond_tree == error_mark_node | |
2212 | || then_tree == error_mark_node | |
2213 | || else_tree == error_mark_node) | |
00b44a6e | 2214 | return this->error_statement(); |
8afa2bfb SD |
2215 | tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node, |
2216 | cond_tree, then_tree, else_tree); | |
db0adf82 ILT |
2217 | return this->make_statement(ret); |
2218 | } | |
2219 | ||
8d0b03a2 ILT |
2220 | // Switch. |
2221 | ||
2222 | Bstatement* | |
2223 | Gcc_backend::switch_statement( | |
036165d8 | 2224 | Bfunction* function, |
8d0b03a2 ILT |
2225 | Bexpression* value, |
2226 | const std::vector<std::vector<Bexpression*> >& cases, | |
2227 | const std::vector<Bstatement*>& statements, | |
8afa2bfb | 2228 | Location switch_location) |
8d0b03a2 ILT |
2229 | { |
2230 | gcc_assert(cases.size() == statements.size()); | |
2231 | ||
036165d8 CM |
2232 | tree decl = function->get_tree(); |
2233 | if (DECL_STRUCT_FUNCTION(decl) == NULL) | |
2234 | push_struct_function(decl); | |
2235 | else | |
2236 | push_cfun(DECL_STRUCT_FUNCTION(decl)); | |
2237 | ||
8d0b03a2 ILT |
2238 | tree stmt_list = NULL_TREE; |
2239 | std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin(); | |
2240 | for (std::vector<Bstatement*>::const_iterator ps = statements.begin(); | |
2241 | ps != statements.end(); | |
2242 | ++ps, ++pc) | |
2243 | { | |
2244 | if (pc->empty()) | |
2245 | { | |
620e594b DM |
2246 | location_t loc = (*ps != NULL |
2247 | ? EXPR_LOCATION((*ps)->get_tree()) | |
2248 | : UNKNOWN_LOCATION); | |
8d0b03a2 | 2249 | tree label = create_artificial_label(loc); |
3d528853 | 2250 | tree c = build_case_label(NULL_TREE, NULL_TREE, label); |
8d0b03a2 ILT |
2251 | append_to_statement_list(c, &stmt_list); |
2252 | } | |
2253 | else | |
2254 | { | |
2255 | for (std::vector<Bexpression*>::const_iterator pcv = pc->begin(); | |
2256 | pcv != pc->end(); | |
2257 | ++pcv) | |
2258 | { | |
2259 | tree t = (*pcv)->get_tree(); | |
2260 | if (t == error_mark_node) | |
00b44a6e | 2261 | return this->error_statement(); |
620e594b | 2262 | location_t loc = EXPR_LOCATION(t); |
8d0b03a2 | 2263 | tree label = create_artificial_label(loc); |
3d528853 | 2264 | tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label); |
8d0b03a2 ILT |
2265 | append_to_statement_list(c, &stmt_list); |
2266 | } | |
2267 | } | |
2268 | ||
2269 | if (*ps != NULL) | |
2270 | { | |
2271 | tree t = (*ps)->get_tree(); | |
2272 | if (t == error_mark_node) | |
00b44a6e | 2273 | return this->error_statement(); |
8d0b03a2 ILT |
2274 | append_to_statement_list(t, &stmt_list); |
2275 | } | |
2276 | } | |
036165d8 | 2277 | pop_cfun(); |
8d0b03a2 ILT |
2278 | |
2279 | tree tv = value->get_tree(); | |
2280 | if (tv == error_mark_node) | |
00b44a6e | 2281 | return this->error_statement(); |
9e851845 JJ |
2282 | tree t = build2_loc(switch_location.gcc_location(), SWITCH_EXPR, |
2283 | NULL_TREE, tv, stmt_list); | |
8d0b03a2 ILT |
2284 | return this->make_statement(t); |
2285 | } | |
2286 | ||
00b44a6e ILT |
2287 | // Pair of statements. |
2288 | ||
2289 | Bstatement* | |
2290 | Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2) | |
2291 | { | |
2292 | tree stmt_list = NULL_TREE; | |
2293 | tree t = s1->get_tree(); | |
2294 | if (t == error_mark_node) | |
2295 | return this->error_statement(); | |
2296 | append_to_statement_list(t, &stmt_list); | |
2297 | t = s2->get_tree(); | |
2298 | if (t == error_mark_node) | |
2299 | return this->error_statement(); | |
2300 | append_to_statement_list(t, &stmt_list); | |
ff09769f ILT |
2301 | |
2302 | // If neither statement has any side effects, stmt_list can be NULL | |
2303 | // at this point. | |
2304 | if (stmt_list == NULL_TREE) | |
2305 | stmt_list = integer_zero_node; | |
2306 | ||
00b44a6e ILT |
2307 | return this->make_statement(stmt_list); |
2308 | } | |
2309 | ||
8d0b03a2 ILT |
2310 | // List of statements. |
2311 | ||
2312 | Bstatement* | |
2313 | Gcc_backend::statement_list(const std::vector<Bstatement*>& statements) | |
2314 | { | |
2315 | tree stmt_list = NULL_TREE; | |
2316 | for (std::vector<Bstatement*>::const_iterator p = statements.begin(); | |
2317 | p != statements.end(); | |
2318 | ++p) | |
2319 | { | |
2320 | tree t = (*p)->get_tree(); | |
2321 | if (t == error_mark_node) | |
00b44a6e | 2322 | return this->error_statement(); |
8d0b03a2 ILT |
2323 | append_to_statement_list(t, &stmt_list); |
2324 | } | |
2325 | return this->make_statement(stmt_list); | |
2326 | } | |
2327 | ||
5ad7db5f ILT |
2328 | // Make a block. For some reason gcc uses a dual structure for |
2329 | // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the | |
2330 | // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in | |
2331 | // the Bblock. | |
2332 | ||
2333 | Bblock* | |
2334 | Gcc_backend::block(Bfunction* function, Bblock* enclosing, | |
2335 | const std::vector<Bvariable*>& vars, | |
8afa2bfb SD |
2336 | Location start_location, |
2337 | Location) | |
5ad7db5f ILT |
2338 | { |
2339 | tree block_tree = make_node(BLOCK); | |
2340 | if (enclosing == NULL) | |
2341 | { | |
036165d8 | 2342 | tree fndecl = function->get_tree(); |
5ad7db5f ILT |
2343 | gcc_assert(fndecl != NULL_TREE); |
2344 | ||
2345 | // We may have already created a block for local variables when | |
2346 | // we take the address of a parameter. | |
2347 | if (DECL_INITIAL(fndecl) == NULL_TREE) | |
2348 | { | |
2349 | BLOCK_SUPERCONTEXT(block_tree) = fndecl; | |
2350 | DECL_INITIAL(fndecl) = block_tree; | |
2351 | } | |
2352 | else | |
2353 | { | |
2354 | tree superblock_tree = DECL_INITIAL(fndecl); | |
2355 | BLOCK_SUPERCONTEXT(block_tree) = superblock_tree; | |
2356 | tree* pp; | |
2357 | for (pp = &BLOCK_SUBBLOCKS(superblock_tree); | |
2358 | *pp != NULL_TREE; | |
2359 | pp = &BLOCK_CHAIN(*pp)) | |
2360 | ; | |
2361 | *pp = block_tree; | |
2362 | } | |
2363 | } | |
2364 | else | |
2365 | { | |
2366 | tree superbind_tree = enclosing->get_tree(); | |
2367 | tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree); | |
2368 | gcc_assert(TREE_CODE(superblock_tree) == BLOCK); | |
2369 | ||
2370 | BLOCK_SUPERCONTEXT(block_tree) = superblock_tree; | |
2371 | tree* pp; | |
2372 | for (pp = &BLOCK_SUBBLOCKS(superblock_tree); | |
2373 | *pp != NULL_TREE; | |
2374 | pp = &BLOCK_CHAIN(*pp)) | |
2375 | ; | |
2376 | *pp = block_tree; | |
2377 | } | |
2378 | ||
2379 | tree* pp = &BLOCK_VARS(block_tree); | |
2380 | for (std::vector<Bvariable*>::const_iterator pv = vars.begin(); | |
2381 | pv != vars.end(); | |
2382 | ++pv) | |
2383 | { | |
399f5feb | 2384 | *pp = (*pv)->get_decl(); |
5ad7db5f ILT |
2385 | if (*pp != error_mark_node) |
2386 | pp = &DECL_CHAIN(*pp); | |
2387 | } | |
2388 | *pp = NULL_TREE; | |
2389 | ||
2390 | TREE_USED(block_tree) = 1; | |
2391 | ||
8afa2bfb SD |
2392 | tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR, |
2393 | void_type_node, BLOCK_VARS(block_tree), | |
2394 | NULL_TREE, block_tree); | |
5ad7db5f | 2395 | TREE_SIDE_EFFECTS(bind_tree) = 1; |
5ad7db5f ILT |
2396 | return new Bblock(bind_tree); |
2397 | } | |
2398 | ||
2399 | // Add statements to a block. | |
2400 | ||
2401 | void | |
2402 | Gcc_backend::block_add_statements(Bblock* bblock, | |
2403 | const std::vector<Bstatement*>& statements) | |
2404 | { | |
2405 | tree stmt_list = NULL_TREE; | |
2406 | for (std::vector<Bstatement*>::const_iterator p = statements.begin(); | |
2407 | p != statements.end(); | |
2408 | ++p) | |
2409 | { | |
2410 | tree s = (*p)->get_tree(); | |
2411 | if (s != error_mark_node) | |
2412 | append_to_statement_list(s, &stmt_list); | |
2413 | } | |
2414 | ||
2415 | tree bind_tree = bblock->get_tree(); | |
2416 | gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR); | |
2417 | BIND_EXPR_BODY(bind_tree) = stmt_list; | |
2418 | } | |
2419 | ||
2420 | // Return a block as a statement. | |
2421 | ||
2422 | Bstatement* | |
2423 | Gcc_backend::block_statement(Bblock* bblock) | |
2424 | { | |
2425 | tree bind_tree = bblock->get_tree(); | |
2426 | gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR); | |
2427 | return this->make_statement(bind_tree); | |
2428 | } | |
2429 | ||
7d6be4c8 ILT |
2430 | // This is not static because we declare it with GTY(()) in go-c.h. |
2431 | tree go_non_zero_struct; | |
2432 | ||
2433 | // Return a type corresponding to TYPE with non-zero size. | |
2434 | ||
2435 | tree | |
2436 | Gcc_backend::non_zero_size_type(tree type) | |
2437 | { | |
2438 | if (int_size_in_bytes(type) != 0) | |
2439 | return type; | |
2440 | ||
2441 | switch (TREE_CODE(type)) | |
2442 | { | |
2443 | case RECORD_TYPE: | |
f1e18725 ILT |
2444 | if (TYPE_FIELDS(type) != NULL_TREE) |
2445 | { | |
2446 | tree ns = make_node(RECORD_TYPE); | |
2447 | tree field_trees = NULL_TREE; | |
2448 | tree *pp = &field_trees; | |
2449 | for (tree field = TYPE_FIELDS(type); | |
2450 | field != NULL_TREE; | |
2451 | field = DECL_CHAIN(field)) | |
2452 | { | |
2453 | tree ft = TREE_TYPE(field); | |
2454 | if (field == TYPE_FIELDS(type)) | |
2455 | ft = non_zero_size_type(ft); | |
2456 | tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL, | |
2457 | DECL_NAME(field), ft); | |
2458 | DECL_CONTEXT(f) = ns; | |
2459 | *pp = f; | |
2460 | pp = &DECL_CHAIN(f); | |
2461 | } | |
2462 | TYPE_FIELDS(ns) = field_trees; | |
2463 | layout_type(ns); | |
2464 | return ns; | |
2465 | } | |
2466 | ||
2467 | if (go_non_zero_struct == NULL_TREE) | |
2468 | { | |
2469 | type = make_node(RECORD_TYPE); | |
2470 | tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL, | |
2471 | get_identifier("dummy"), | |
2472 | boolean_type_node); | |
2473 | DECL_CONTEXT(field) = type; | |
2474 | TYPE_FIELDS(type) = field; | |
2475 | layout_type(type); | |
2476 | go_non_zero_struct = type; | |
2477 | } | |
2478 | return go_non_zero_struct; | |
7d6be4c8 ILT |
2479 | |
2480 | case ARRAY_TYPE: | |
2481 | { | |
2482 | tree element_type = non_zero_size_type(TREE_TYPE(type)); | |
2483 | return build_array_type_nelts(element_type, 1); | |
2484 | } | |
2485 | ||
2486 | default: | |
2487 | gcc_unreachable(); | |
2488 | } | |
2489 | ||
2490 | gcc_unreachable(); | |
2491 | } | |
2492 | ||
ebc2f401 ILT |
2493 | // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Go type |
2494 | // can be created multiple times and thus have multiple tree | |
2495 | // representations. Make sure this does not confuse the middle-end. | |
2496 | ||
2497 | tree | |
2498 | Gcc_backend::convert_tree(tree type_tree, tree expr_tree, Location location) | |
2499 | { | |
2500 | if (type_tree == TREE_TYPE(expr_tree)) | |
2501 | return expr_tree; | |
2502 | ||
2503 | if (type_tree == error_mark_node | |
2504 | || expr_tree == error_mark_node | |
2505 | || TREE_TYPE(expr_tree) == error_mark_node) | |
2506 | return error_mark_node; | |
2507 | ||
2508 | gcc_assert(TREE_CODE(type_tree) == TREE_CODE(TREE_TYPE(expr_tree))); | |
2509 | if (POINTER_TYPE_P(type_tree) | |
2510 | || INTEGRAL_TYPE_P(type_tree) | |
2511 | || SCALAR_FLOAT_TYPE_P(type_tree) | |
2512 | || COMPLEX_FLOAT_TYPE_P(type_tree)) | |
2513 | return fold_convert_loc(location.gcc_location(), type_tree, expr_tree); | |
2514 | else if (TREE_CODE(type_tree) == RECORD_TYPE | |
2515 | || TREE_CODE(type_tree) == ARRAY_TYPE) | |
2516 | { | |
2517 | gcc_assert(int_size_in_bytes(type_tree) | |
2518 | == int_size_in_bytes(TREE_TYPE(expr_tree))); | |
2519 | if (TYPE_MAIN_VARIANT(type_tree) | |
2520 | == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree))) | |
2521 | return fold_build1_loc(location.gcc_location(), NOP_EXPR, | |
2522 | type_tree, expr_tree); | |
2523 | return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR, | |
2524 | type_tree, expr_tree); | |
2525 | } | |
2526 | ||
2527 | gcc_unreachable(); | |
2528 | } | |
2529 | ||
e09ce6c5 ILT |
2530 | // Make a global variable. |
2531 | ||
2532 | Bvariable* | |
f3878205 TM |
2533 | Gcc_backend::global_variable(const std::string& var_name, |
2534 | const std::string& asm_name, | |
e09ce6c5 ILT |
2535 | Btype* btype, |
2536 | bool is_external, | |
2537 | bool is_hidden, | |
744c3195 | 2538 | bool in_unique_section, |
8afa2bfb | 2539 | Location location) |
e09ce6c5 ILT |
2540 | { |
2541 | tree type_tree = btype->get_tree(); | |
2542 | if (type_tree == error_mark_node) | |
2543 | return this->error_variable(); | |
2544 | ||
7d6be4c8 | 2545 | // The GNU linker does not like dynamic variables with zero size. |
8fd41e92 | 2546 | tree orig_type_tree = type_tree; |
7d6be4c8 ILT |
2547 | if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0) |
2548 | type_tree = this->non_zero_size_type(type_tree); | |
2549 | ||
8afa2bfb | 2550 | tree decl = build_decl(location.gcc_location(), VAR_DECL, |
e09ce6c5 ILT |
2551 | get_identifier_from_string(var_name), |
2552 | type_tree); | |
2553 | if (is_external) | |
2554 | DECL_EXTERNAL(decl) = 1; | |
2555 | else | |
2556 | TREE_STATIC(decl) = 1; | |
2557 | if (!is_hidden) | |
2558 | { | |
2559 | TREE_PUBLIC(decl) = 1; | |
e09ce6c5 ILT |
2560 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); |
2561 | } | |
f3878205 TM |
2562 | else |
2563 | { | |
2564 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); | |
2565 | } | |
9dcd84ec | 2566 | |
e09ce6c5 ILT |
2567 | TREE_USED(decl) = 1; |
2568 | ||
744c3195 ILT |
2569 | if (in_unique_section) |
2570 | resolve_unique_section (decl, 0, 1); | |
2571 | ||
e09ce6c5 ILT |
2572 | go_preserve_from_gc(decl); |
2573 | ||
399f5feb | 2574 | return new Bvariable(decl, orig_type_tree); |
e09ce6c5 ILT |
2575 | } |
2576 | ||
2577 | // Set the initial value of a global variable. | |
2578 | ||
2579 | void | |
2580 | Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr) | |
2581 | { | |
2582 | tree expr_tree = expr->get_tree(); | |
2583 | if (expr_tree == error_mark_node) | |
2584 | return; | |
2585 | gcc_assert(TREE_CONSTANT(expr_tree)); | |
399f5feb | 2586 | tree var_decl = var->get_decl(); |
e09ce6c5 ILT |
2587 | if (var_decl == error_mark_node) |
2588 | return; | |
2589 | DECL_INITIAL(var_decl) = expr_tree; | |
744c3195 ILT |
2590 | |
2591 | // If this variable goes in a unique section, it may need to go into | |
2592 | // a different one now that DECL_INITIAL is set. | |
0ffc395f UB |
2593 | if (symtab_node::get(var_decl) |
2594 | && symtab_node::get(var_decl)->implicit_section) | |
744c3195 | 2595 | { |
24d047a3 | 2596 | set_decl_section_name (var_decl, NULL); |
744c3195 ILT |
2597 | resolve_unique_section (var_decl, |
2598 | compute_reloc_for_constant (expr_tree), | |
2599 | 1); | |
2600 | } | |
e09ce6c5 ILT |
2601 | } |
2602 | ||
2603 | // Make a local variable. | |
2604 | ||
2605 | Bvariable* | |
2606 | Gcc_backend::local_variable(Bfunction* function, const std::string& name, | |
81e0f092 CZ |
2607 | Btype* btype, Bvariable* decl_var, |
2608 | bool is_address_taken, Location location) | |
e09ce6c5 ILT |
2609 | { |
2610 | tree type_tree = btype->get_tree(); | |
2611 | if (type_tree == error_mark_node) | |
2612 | return this->error_variable(); | |
8afa2bfb | 2613 | tree decl = build_decl(location.gcc_location(), VAR_DECL, |
e09ce6c5 ILT |
2614 | get_identifier_from_string(name), |
2615 | type_tree); | |
2616 | DECL_CONTEXT(decl) = function->get_tree(); | |
2617 | TREE_USED(decl) = 1; | |
acf98146 ILT |
2618 | if (is_address_taken) |
2619 | TREE_ADDRESSABLE(decl) = 1; | |
81e0f092 CZ |
2620 | if (decl_var != NULL) |
2621 | { | |
2622 | DECL_HAS_VALUE_EXPR_P(decl) = 1; | |
2623 | SET_DECL_VALUE_EXPR(decl, decl_var->get_decl()); | |
2624 | } | |
e09ce6c5 ILT |
2625 | go_preserve_from_gc(decl); |
2626 | return new Bvariable(decl); | |
2627 | } | |
2628 | ||
2629 | // Make a function parameter variable. | |
2630 | ||
2631 | Bvariable* | |
2632 | Gcc_backend::parameter_variable(Bfunction* function, const std::string& name, | |
acf98146 | 2633 | Btype* btype, bool is_address_taken, |
8afa2bfb | 2634 | Location location) |
e09ce6c5 ILT |
2635 | { |
2636 | tree type_tree = btype->get_tree(); | |
2637 | if (type_tree == error_mark_node) | |
2638 | return this->error_variable(); | |
8afa2bfb | 2639 | tree decl = build_decl(location.gcc_location(), PARM_DECL, |
e09ce6c5 ILT |
2640 | get_identifier_from_string(name), |
2641 | type_tree); | |
2642 | DECL_CONTEXT(decl) = function->get_tree(); | |
2643 | DECL_ARG_TYPE(decl) = type_tree; | |
2644 | TREE_USED(decl) = 1; | |
acf98146 ILT |
2645 | if (is_address_taken) |
2646 | TREE_ADDRESSABLE(decl) = 1; | |
e09ce6c5 ILT |
2647 | go_preserve_from_gc(decl); |
2648 | return new Bvariable(decl); | |
2649 | } | |
2650 | ||
38bf819a RH |
2651 | // Make a static chain variable. |
2652 | ||
2653 | Bvariable* | |
2654 | Gcc_backend::static_chain_variable(Bfunction* function, const std::string& name, | |
2655 | Btype* btype, Location location) | |
2656 | { | |
2657 | tree type_tree = btype->get_tree(); | |
2658 | if (type_tree == error_mark_node) | |
2659 | return this->error_variable(); | |
2660 | tree decl = build_decl(location.gcc_location(), PARM_DECL, | |
2661 | get_identifier_from_string(name), type_tree); | |
2662 | tree fndecl = function->get_tree(); | |
2663 | DECL_CONTEXT(decl) = fndecl; | |
2664 | DECL_ARG_TYPE(decl) = type_tree; | |
2665 | TREE_USED(decl) = 1; | |
2666 | DECL_ARTIFICIAL(decl) = 1; | |
2667 | DECL_IGNORED_P(decl) = 1; | |
2668 | TREE_READONLY(decl) = 1; | |
2669 | ||
2670 | struct function *f = DECL_STRUCT_FUNCTION(fndecl); | |
2671 | if (f == NULL) | |
2672 | { | |
2673 | push_struct_function(fndecl); | |
2674 | pop_cfun(); | |
2675 | f = DECL_STRUCT_FUNCTION(fndecl); | |
2676 | } | |
2677 | gcc_assert(f->static_chain_decl == NULL); | |
2678 | f->static_chain_decl = decl; | |
2679 | DECL_STATIC_CHAIN(fndecl) = 1; | |
2680 | ||
2681 | go_preserve_from_gc(decl); | |
2682 | return new Bvariable(decl); | |
2683 | } | |
2684 | ||
9131ad67 ILT |
2685 | // Make a temporary variable. |
2686 | ||
2687 | Bvariable* | |
2688 | Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock, | |
2689 | Btype* btype, Bexpression* binit, | |
2690 | bool is_address_taken, | |
8afa2bfb | 2691 | Location location, |
9131ad67 ILT |
2692 | Bstatement** pstatement) |
2693 | { | |
3e7b0938 CM |
2694 | gcc_assert(function != NULL); |
2695 | tree decl = function->get_tree(); | |
9131ad67 ILT |
2696 | tree type_tree = btype->get_tree(); |
2697 | tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree(); | |
3e7b0938 CM |
2698 | if (type_tree == error_mark_node |
2699 | || init_tree == error_mark_node | |
2700 | || decl == error_mark_node) | |
9131ad67 ILT |
2701 | { |
2702 | *pstatement = this->error_statement(); | |
2703 | return this->error_variable(); | |
2704 | } | |
2705 | ||
2706 | tree var; | |
2707 | // We can only use create_tmp_var if the type is not addressable. | |
2708 | if (!TREE_ADDRESSABLE(type_tree)) | |
aa492920 CM |
2709 | { |
2710 | if (DECL_STRUCT_FUNCTION(decl) == NULL) | |
2711 | push_struct_function(decl); | |
2712 | else | |
2713 | push_cfun(DECL_STRUCT_FUNCTION(decl)); | |
2714 | ||
2715 | var = create_tmp_var(type_tree, "GOTMP"); | |
2716 | pop_cfun(); | |
2717 | } | |
9131ad67 ILT |
2718 | else |
2719 | { | |
2720 | gcc_assert(bblock != NULL); | |
8afa2bfb | 2721 | var = build_decl(location.gcc_location(), VAR_DECL, |
9131ad67 ILT |
2722 | create_tmp_var_name("GOTMP"), |
2723 | type_tree); | |
2724 | DECL_ARTIFICIAL(var) = 1; | |
2725 | DECL_IGNORED_P(var) = 1; | |
2726 | TREE_USED(var) = 1; | |
aa492920 | 2727 | DECL_CONTEXT(var) = decl; |
9131ad67 ILT |
2728 | |
2729 | // We have to add this variable to the BLOCK and the BIND_EXPR. | |
2730 | tree bind_tree = bblock->get_tree(); | |
2731 | gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR); | |
2732 | tree block_tree = BIND_EXPR_BLOCK(bind_tree); | |
2733 | gcc_assert(TREE_CODE(block_tree) == BLOCK); | |
2734 | DECL_CHAIN(var) = BLOCK_VARS(block_tree); | |
2735 | BLOCK_VARS(block_tree) = var; | |
2736 | BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree); | |
2737 | } | |
2738 | ||
c2a0fe56 ILT |
2739 | if (this->type_size(btype) != 0 |
2740 | && init_tree != NULL_TREE | |
2741 | && TREE_TYPE(init_tree) != void_type_node) | |
ebc2f401 | 2742 | DECL_INITIAL(var) = this->convert_tree(type_tree, init_tree, location); |
9131ad67 ILT |
2743 | |
2744 | if (is_address_taken) | |
2745 | TREE_ADDRESSABLE(var) = 1; | |
2746 | ||
8afa2bfb SD |
2747 | *pstatement = this->make_statement(build1_loc(location.gcc_location(), |
2748 | DECL_EXPR, | |
9131ad67 | 2749 | void_type_node, var)); |
dd373780 | 2750 | |
c2a0fe56 ILT |
2751 | // For a zero sized type, don't initialize VAR with BINIT, but still |
2752 | // evaluate BINIT for its side effects. | |
2753 | if (init_tree != NULL_TREE | |
2754 | && (this->type_size(btype) == 0 | |
2755 | || TREE_TYPE(init_tree) == void_type_node)) | |
8220e3f9 TM |
2756 | *pstatement = |
2757 | this->compound_statement(this->expression_statement(function, binit), | |
2758 | *pstatement); | |
dd373780 | 2759 | |
9131ad67 ILT |
2760 | return new Bvariable(var); |
2761 | } | |
2762 | ||
fb930306 CM |
2763 | // Create an implicit variable that is compiler-defined. This is used when |
2764 | // generating GC root variables and storing the values of a slice initializer. | |
036165d8 CM |
2765 | |
2766 | Bvariable* | |
f3878205 TM |
2767 | Gcc_backend::implicit_variable(const std::string& name, |
2768 | const std::string& asm_name, | |
2769 | Btype* type, bool is_hidden, bool is_constant, | |
18fbd1ec | 2770 | bool is_common, int64_t alignment) |
036165d8 CM |
2771 | { |
2772 | tree type_tree = type->get_tree(); | |
f1d2ac4f | 2773 | if (type_tree == error_mark_node) |
036165d8 CM |
2774 | return this->error_variable(); |
2775 | ||
2776 | tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, | |
fb930306 | 2777 | get_identifier_from_string(name), type_tree); |
036165d8 | 2778 | DECL_EXTERNAL(decl) = 0; |
f1d2ac4f | 2779 | TREE_PUBLIC(decl) = !is_hidden; |
036165d8 | 2780 | TREE_STATIC(decl) = 1; |
f1d2ac4f | 2781 | TREE_USED(decl) = 1; |
036165d8 | 2782 | DECL_ARTIFICIAL(decl) = 1; |
bae90c98 ILT |
2783 | if (is_common) |
2784 | { | |
2785 | DECL_COMMON(decl) = 1; | |
f1d2ac4f CM |
2786 | |
2787 | // When the initializer for one implicit_variable refers to another, | |
2788 | // it needs to know the visibility of the referenced struct so that | |
2789 | // compute_reloc_for_constant will return the right value. On many | |
2790 | // systems calling make_decl_one_only will mark the decl as weak, | |
2791 | // which will change the return value of compute_reloc_for_constant. | |
2792 | // We can't reliably call make_decl_one_only yet, because we don't | |
2793 | // yet know the initializer. This issue doesn't arise in C because | |
2794 | // Go initializers, unlike C initializers, can be indirectly | |
2795 | // recursive. To ensure that compute_reloc_for_constant computes | |
2796 | // the right value if some other initializer refers to this one, we | |
2797 | // mark this symbol as weak here. We undo that below in | |
2798 | // immutable_struct_set_init before calling mark_decl_one_only. | |
2799 | DECL_WEAK(decl) = 1; | |
bae90c98 | 2800 | } |
f1d2ac4f | 2801 | if (is_constant) |
fb930306 CM |
2802 | { |
2803 | TREE_READONLY(decl) = 1; | |
2804 | TREE_CONSTANT(decl) = 1; | |
2805 | } | |
bae90c98 ILT |
2806 | if (alignment != 0) |
2807 | { | |
fe37c7af | 2808 | SET_DECL_ALIGN(decl, alignment * BITS_PER_UNIT); |
bae90c98 ILT |
2809 | DECL_USER_ALIGN(decl) = 1; |
2810 | } | |
f3878205 TM |
2811 | if (! asm_name.empty()) |
2812 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); | |
bae90c98 | 2813 | |
f1d2ac4f CM |
2814 | go_preserve_from_gc(decl); |
2815 | return new Bvariable(decl); | |
2816 | } | |
2817 | ||
2818 | // Set the initalizer for a variable created by implicit_variable. | |
2819 | // This is where we finish compiling the variable. | |
2820 | ||
2821 | void | |
2822 | Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&, | |
2823 | Btype*, bool, bool, bool is_common, | |
2824 | Bexpression* init) | |
2825 | { | |
399f5feb | 2826 | tree decl = var->get_decl(); |
f1d2ac4f CM |
2827 | tree init_tree; |
2828 | if (init == NULL) | |
2829 | init_tree = NULL_TREE; | |
2830 | else | |
2831 | init_tree = init->get_tree(); | |
2832 | if (decl == error_mark_node || init_tree == error_mark_node) | |
2833 | return; | |
2834 | ||
2835 | DECL_INITIAL(decl) = init_tree; | |
2836 | ||
2837 | // Now that DECL_INITIAL is set, we can't call make_decl_one_only. | |
2838 | // See the comment where DECL_WEAK is set in implicit_variable. | |
2839 | if (is_common) | |
2840 | { | |
2841 | DECL_WEAK(decl) = 0; | |
2842 | make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl)); | |
2843 | } | |
2844 | ||
2845 | resolve_unique_section(decl, 2, 1); | |
2846 | ||
036165d8 | 2847 | rest_of_decl_compilation(decl, 1, 0); |
f1d2ac4f CM |
2848 | } |
2849 | ||
2850 | // Return a reference to an implicit variable defined in another package. | |
036165d8 | 2851 | |
f1d2ac4f | 2852 | Bvariable* |
f3878205 TM |
2853 | Gcc_backend::implicit_variable_reference(const std::string& name, |
2854 | const std::string& asm_name, | |
2855 | Btype* btype) | |
f1d2ac4f CM |
2856 | { |
2857 | tree type_tree = btype->get_tree(); | |
2858 | if (type_tree == error_mark_node) | |
2859 | return this->error_variable(); | |
2860 | ||
2861 | tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, | |
2862 | get_identifier_from_string(name), type_tree); | |
be18b176 | 2863 | DECL_EXTERNAL(decl) = 1; |
f1d2ac4f | 2864 | TREE_PUBLIC(decl) = 1; |
be18b176 | 2865 | TREE_STATIC(decl) = 0; |
f1d2ac4f | 2866 | DECL_ARTIFICIAL(decl) = 1; |
f3878205 TM |
2867 | if (! asm_name.empty()) |
2868 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); | |
f1d2ac4f | 2869 | go_preserve_from_gc(decl); |
036165d8 CM |
2870 | return new Bvariable(decl); |
2871 | } | |
2872 | ||
70f91024 ILT |
2873 | // Create a named immutable initialized data structure. |
2874 | ||
2875 | Bvariable* | |
f3878205 TM |
2876 | Gcc_backend::immutable_struct(const std::string& name, |
2877 | const std::string& asm_name, | |
2878 | bool is_hidden, | |
915182a0 | 2879 | bool is_common, Btype* btype, Location location) |
70f91024 ILT |
2880 | { |
2881 | tree type_tree = btype->get_tree(); | |
2882 | if (type_tree == error_mark_node) | |
2883 | return this->error_variable(); | |
2884 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); | |
8afa2bfb | 2885 | tree decl = build_decl(location.gcc_location(), VAR_DECL, |
70f91024 ILT |
2886 | get_identifier_from_string(name), |
2887 | build_qualified_type(type_tree, TYPE_QUAL_CONST)); | |
2888 | TREE_STATIC(decl) = 1; | |
036165d8 | 2889 | TREE_USED(decl) = 1; |
70f91024 ILT |
2890 | TREE_READONLY(decl) = 1; |
2891 | TREE_CONSTANT(decl) = 1; | |
70f91024 | 2892 | DECL_ARTIFICIAL(decl) = 1; |
2ec974d9 ILT |
2893 | if (!is_hidden) |
2894 | TREE_PUBLIC(decl) = 1; | |
f3878205 TM |
2895 | if (! asm_name.empty()) |
2896 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); | |
70f91024 | 2897 | |
915182a0 ILT |
2898 | // When the initializer for one immutable_struct refers to another, |
2899 | // it needs to know the visibility of the referenced struct so that | |
2900 | // compute_reloc_for_constant will return the right value. On many | |
2901 | // systems calling make_decl_one_only will mark the decl as weak, | |
2902 | // which will change the return value of compute_reloc_for_constant. | |
2903 | // We can't reliably call make_decl_one_only yet, because we don't | |
2904 | // yet know the initializer. This issue doesn't arise in C because | |
2905 | // Go initializers, unlike C initializers, can be indirectly | |
2906 | // recursive. To ensure that compute_reloc_for_constant computes | |
2907 | // the right value if some other initializer refers to this one, we | |
2908 | // mark this symbol as weak here. We undo that below in | |
2909 | // immutable_struct_set_init before calling mark_decl_one_only. | |
2910 | if (is_common) | |
2911 | DECL_WEAK(decl) = 1; | |
2912 | ||
70f91024 ILT |
2913 | // We don't call rest_of_decl_compilation until we have the |
2914 | // initializer. | |
2915 | ||
2916 | go_preserve_from_gc(decl); | |
2917 | return new Bvariable(decl); | |
2918 | } | |
2919 | ||
2920 | // Set the initializer for a variable created by immutable_struct. | |
2921 | // This is where we finish compiling the variable. | |
2922 | ||
2923 | void | |
2924 | Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&, | |
2ec974d9 | 2925 | bool, bool is_common, Btype*, Location, |
70f91024 ILT |
2926 | Bexpression* initializer) |
2927 | { | |
399f5feb | 2928 | tree decl = var->get_decl(); |
70f91024 ILT |
2929 | tree init_tree = initializer->get_tree(); |
2930 | if (decl == error_mark_node || init_tree == error_mark_node) | |
2931 | return; | |
2932 | ||
2933 | DECL_INITIAL(decl) = init_tree; | |
2934 | ||
915182a0 ILT |
2935 | // Now that DECL_INITIAL is set, we can't call make_decl_one_only. |
2936 | // See the comment where DECL_WEAK is set in immutable_struct. | |
2ec974d9 | 2937 | if (is_common) |
915182a0 ILT |
2938 | { |
2939 | DECL_WEAK(decl) = 0; | |
2940 | make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl)); | |
2941 | } | |
a572c454 ILT |
2942 | |
2943 | // These variables are often unneeded in the final program, so put | |
2944 | // them in their own section so that linker GC can discard them. | |
e086adbd ILT |
2945 | resolve_unique_section(decl, |
2946 | compute_reloc_for_constant (init_tree), | |
2947 | 1); | |
70f91024 ILT |
2948 | |
2949 | rest_of_decl_compilation(decl, 1, 0); | |
2950 | } | |
2951 | ||
2952 | // Return a reference to an immutable initialized data structure | |
2953 | // defined in another package. | |
2954 | ||
2955 | Bvariable* | |
f3878205 TM |
2956 | Gcc_backend::immutable_struct_reference(const std::string& name, |
2957 | const std::string& asm_name, | |
2958 | Btype* btype, | |
8afa2bfb | 2959 | Location location) |
70f91024 ILT |
2960 | { |
2961 | tree type_tree = btype->get_tree(); | |
2962 | if (type_tree == error_mark_node) | |
2963 | return this->error_variable(); | |
2964 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); | |
8afa2bfb | 2965 | tree decl = build_decl(location.gcc_location(), VAR_DECL, |
70f91024 ILT |
2966 | get_identifier_from_string(name), |
2967 | build_qualified_type(type_tree, TYPE_QUAL_CONST)); | |
2968 | TREE_READONLY(decl) = 1; | |
2969 | TREE_CONSTANT(decl) = 1; | |
2970 | DECL_ARTIFICIAL(decl) = 1; | |
2971 | TREE_PUBLIC(decl) = 1; | |
2972 | DECL_EXTERNAL(decl) = 1; | |
f3878205 TM |
2973 | if (! asm_name.empty()) |
2974 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); | |
70f91024 ILT |
2975 | go_preserve_from_gc(decl); |
2976 | return new Bvariable(decl); | |
2977 | } | |
2978 | ||
d56e6679 ILT |
2979 | // Make a label. |
2980 | ||
2981 | Blabel* | |
2982 | Gcc_backend::label(Bfunction* function, const std::string& name, | |
8afa2bfb | 2983 | Location location) |
d56e6679 ILT |
2984 | { |
2985 | tree decl; | |
2986 | if (name.empty()) | |
036165d8 CM |
2987 | { |
2988 | tree func_tree = function->get_tree(); | |
2989 | if (DECL_STRUCT_FUNCTION(func_tree) == NULL) | |
2990 | push_struct_function(func_tree); | |
2991 | else | |
2992 | push_cfun(DECL_STRUCT_FUNCTION(func_tree)); | |
2993 | ||
2994 | decl = create_artificial_label(location.gcc_location()); | |
2995 | ||
2996 | pop_cfun(); | |
2997 | } | |
d56e6679 ILT |
2998 | else |
2999 | { | |
3000 | tree id = get_identifier_from_string(name); | |
8afa2bfb SD |
3001 | decl = build_decl(location.gcc_location(), LABEL_DECL, id, |
3002 | void_type_node); | |
d56e6679 ILT |
3003 | DECL_CONTEXT(decl) = function->get_tree(); |
3004 | } | |
3005 | return new Blabel(decl); | |
3006 | } | |
3007 | ||
3008 | // Make a statement which defines a label. | |
3009 | ||
3010 | Bstatement* | |
3011 | Gcc_backend::label_definition_statement(Blabel* label) | |
3012 | { | |
3013 | tree lab = label->get_tree(); | |
3014 | tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR, | |
3015 | void_type_node, lab); | |
3016 | return this->make_statement(ret); | |
3017 | } | |
3018 | ||
3019 | // Make a goto statement. | |
3020 | ||
3021 | Bstatement* | |
8afa2bfb | 3022 | Gcc_backend::goto_statement(Blabel* label, Location location) |
d56e6679 ILT |
3023 | { |
3024 | tree lab = label->get_tree(); | |
8afa2bfb SD |
3025 | tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node, |
3026 | lab); | |
d56e6679 ILT |
3027 | return this->make_statement(ret); |
3028 | } | |
3029 | ||
3030 | // Get the address of a label. | |
3031 | ||
3032 | Bexpression* | |
8afa2bfb | 3033 | Gcc_backend::label_address(Blabel* label, Location location) |
d56e6679 ILT |
3034 | { |
3035 | tree lab = label->get_tree(); | |
3036 | TREE_USED(lab) = 1; | |
3037 | TREE_ADDRESSABLE(lab) = 1; | |
8afa2bfb SD |
3038 | tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node, |
3039 | build_fold_addr_expr_loc(location.gcc_location(), | |
3040 | lab)); | |
d56e6679 ILT |
3041 | return this->make_expression(ret); |
3042 | } | |
3043 | ||
f7191ecd CM |
3044 | // Declare or define a new function. |
3045 | ||
3046 | Bfunction* | |
3047 | Gcc_backend::function(Btype* fntype, const std::string& name, | |
a4e30bef ILT |
3048 | const std::string& asm_name, unsigned int flags, |
3049 | Location location) | |
f7191ecd CM |
3050 | { |
3051 | tree functype = fntype->get_tree(); | |
3052 | if (functype != error_mark_node) | |
3053 | { | |
3054 | gcc_assert(FUNCTION_POINTER_TYPE_P(functype)); | |
3055 | functype = TREE_TYPE(functype); | |
3056 | } | |
3057 | tree id = get_identifier_from_string(name); | |
3058 | if (functype == error_mark_node || id == error_mark_node) | |
3059 | return this->error_function(); | |
3060 | ||
3061 | tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype); | |
f3878205 | 3062 | if (! asm_name.empty()) |
f7191ecd | 3063 | SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name)); |
a4e30bef | 3064 | if ((flags & function_is_visible) != 0) |
f7191ecd | 3065 | TREE_PUBLIC(decl) = 1; |
a4e30bef | 3066 | if ((flags & function_is_declaration) != 0) |
f7191ecd CM |
3067 | DECL_EXTERNAL(decl) = 1; |
3068 | else | |
3069 | { | |
3070 | tree restype = TREE_TYPE(functype); | |
3071 | tree resdecl = | |
3072 | build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype); | |
3073 | DECL_ARTIFICIAL(resdecl) = 1; | |
3074 | DECL_IGNORED_P(resdecl) = 1; | |
3075 | DECL_CONTEXT(resdecl) = decl; | |
3076 | DECL_RESULT(decl) = resdecl; | |
3077 | } | |
a4e30bef | 3078 | if ((flags & function_is_inlinable) == 0) |
f7191ecd | 3079 | DECL_UNINLINABLE(decl) = 1; |
a4e30bef | 3080 | if ((flags & function_no_split_stack) != 0) |
f7191ecd | 3081 | { |
577eec56 | 3082 | tree attr = get_identifier ("no_split_stack"); |
f7191ecd CM |
3083 | DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE); |
3084 | } | |
a4e30bef | 3085 | if ((flags & function_does_not_return) != 0) |
9638589f | 3086 | TREE_THIS_VOLATILE(decl) = 1; |
a4e30bef | 3087 | if ((flags & function_in_unique_section) != 0) |
f7191ecd | 3088 | resolve_unique_section(decl, 0, 1); |
862ec763 ILT |
3089 | if ((flags & function_only_inline) != 0) |
3090 | { | |
3091 | DECL_EXTERNAL(decl) = 1; | |
3092 | DECL_DECLARED_INLINE_P(decl) = 1; | |
3093 | } | |
f7191ecd CM |
3094 | |
3095 | go_preserve_from_gc(decl); | |
3096 | return new Bfunction(decl); | |
3097 | } | |
3098 | ||
7035307e CM |
3099 | // Create a statement that runs all deferred calls for FUNCTION. This should |
3100 | // be a statement that looks like this in C++: | |
3101 | // finish: | |
3102 | // try { UNDEFER; } catch { CHECK_DEFER; goto finish; } | |
3103 | ||
3104 | Bstatement* | |
3105 | Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer, | |
3106 | Bexpression* defer, Location location) | |
3107 | { | |
3108 | tree undefer_tree = undefer->get_tree(); | |
3109 | tree defer_tree = defer->get_tree(); | |
036165d8 | 3110 | tree fntree = function->get_tree(); |
7035307e CM |
3111 | |
3112 | if (undefer_tree == error_mark_node | |
036165d8 CM |
3113 | || defer_tree == error_mark_node |
3114 | || fntree == error_mark_node) | |
7035307e CM |
3115 | return this->error_statement(); |
3116 | ||
036165d8 CM |
3117 | if (DECL_STRUCT_FUNCTION(fntree) == NULL) |
3118 | push_struct_function(fntree); | |
3119 | else | |
3120 | push_cfun(DECL_STRUCT_FUNCTION(fntree)); | |
3121 | ||
7035307e CM |
3122 | tree stmt_list = NULL; |
3123 | Blabel* blabel = this->label(function, "", location); | |
3124 | Bstatement* label_def = this->label_definition_statement(blabel); | |
3125 | append_to_statement_list(label_def->get_tree(), &stmt_list); | |
3126 | ||
3127 | Bstatement* jump_stmt = this->goto_statement(blabel, location); | |
3128 | tree jump = jump_stmt->get_tree(); | |
3129 | tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump); | |
3130 | catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body); | |
3131 | tree try_catch = | |
3132 | build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body); | |
3133 | append_to_statement_list(try_catch, &stmt_list); | |
036165d8 | 3134 | pop_cfun(); |
7035307e CM |
3135 | |
3136 | return this->make_statement(stmt_list); | |
3137 | } | |
3138 | ||
3139 | // Record PARAM_VARS as the variables to use for the parameters of FUNCTION. | |
3140 | // This will only be called for a function definition. | |
3141 | ||
3142 | bool | |
3143 | Gcc_backend::function_set_parameters(Bfunction* function, | |
3144 | const std::vector<Bvariable*>& param_vars) | |
3145 | { | |
3146 | tree func_tree = function->get_tree(); | |
3147 | if (func_tree == error_mark_node) | |
3148 | return false; | |
3149 | ||
3150 | tree params = NULL_TREE; | |
3151 | tree *pp = ¶ms; | |
3152 | for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin(); | |
3153 | pv != param_vars.end(); | |
3154 | ++pv) | |
3155 | { | |
399f5feb | 3156 | *pp = (*pv)->get_decl(); |
7035307e CM |
3157 | gcc_assert(*pp != error_mark_node); |
3158 | pp = &DECL_CHAIN(*pp); | |
3159 | } | |
3160 | *pp = NULL_TREE; | |
3161 | DECL_ARGUMENTS(func_tree) = params; | |
3162 | return true; | |
3163 | } | |
3164 | ||
3165 | // Set the function body for FUNCTION using the code in CODE_BLOCK. | |
3166 | ||
3167 | bool | |
3168 | Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt) | |
3169 | { | |
3170 | tree func_tree = function->get_tree(); | |
3171 | tree code = code_stmt->get_tree(); | |
3172 | ||
3173 | if (func_tree == error_mark_node || code == error_mark_node) | |
3174 | return false; | |
3175 | DECL_SAVED_TREE(func_tree) = code; | |
3176 | return true; | |
3177 | } | |
3178 | ||
90cbaa29 CM |
3179 | // Look up a named built-in function in the current backend implementation. |
3180 | // Returns NULL if no built-in function by that name exists. | |
3181 | ||
3182 | Bfunction* | |
3183 | Gcc_backend::lookup_builtin(const std::string& name) | |
3184 | { | |
3185 | if (this->builtin_functions_.count(name) != 0) | |
3186 | return this->builtin_functions_[name]; | |
3187 | return NULL; | |
3188 | } | |
3189 | ||
036165d8 | 3190 | // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS, |
d7438551 AH |
3191 | // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as |
3192 | // emit early debugging information. | |
036165d8 CM |
3193 | |
3194 | void | |
3195 | Gcc_backend::write_global_definitions( | |
3196 | const std::vector<Btype*>& type_decls, | |
3197 | const std::vector<Bexpression*>& constant_decls, | |
3198 | const std::vector<Bfunction*>& function_decls, | |
3199 | const std::vector<Bvariable*>& variable_decls) | |
3200 | { | |
3201 | size_t count_definitions = type_decls.size() + constant_decls.size() | |
3202 | + function_decls.size() + variable_decls.size(); | |
3203 | ||
3204 | tree* defs = new tree[count_definitions]; | |
3205 | ||
3206 | // Convert all non-erroneous declarations into Gimple form. | |
3207 | size_t i = 0; | |
3208 | for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin(); | |
3209 | p != variable_decls.end(); | |
3210 | ++p) | |
3211 | { | |
399f5feb ILT |
3212 | tree v = (*p)->get_decl(); |
3213 | if (v != error_mark_node) | |
036165d8 | 3214 | { |
399f5feb | 3215 | defs[i] = v; |
036165d8 CM |
3216 | go_preserve_from_gc(defs[i]); |
3217 | ++i; | |
3218 | } | |
3219 | } | |
3220 | ||
3221 | for (std::vector<Btype*>::const_iterator p = type_decls.begin(); | |
3222 | p != type_decls.end(); | |
3223 | ++p) | |
3224 | { | |
3225 | tree type_tree = (*p)->get_tree(); | |
3226 | if (type_tree != error_mark_node | |
3227 | && IS_TYPE_OR_DECL_P(type_tree)) | |
3228 | { | |
3229 | defs[i] = TYPE_NAME(type_tree); | |
3230 | gcc_assert(defs[i] != NULL); | |
3231 | go_preserve_from_gc(defs[i]); | |
3232 | ++i; | |
3233 | } | |
3234 | } | |
3235 | for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin(); | |
3236 | p != constant_decls.end(); | |
3237 | ++p) | |
3238 | { | |
3239 | if ((*p)->get_tree() != error_mark_node) | |
3240 | { | |
3241 | defs[i] = (*p)->get_tree(); | |
3242 | go_preserve_from_gc(defs[i]); | |
3243 | ++i; | |
3244 | } | |
3245 | } | |
3246 | for (std::vector<Bfunction*>::const_iterator p = function_decls.begin(); | |
3247 | p != function_decls.end(); | |
3248 | ++p) | |
3249 | { | |
3250 | tree decl = (*p)->get_tree(); | |
3251 | if (decl != error_mark_node) | |
3252 | { | |
3253 | go_preserve_from_gc(decl); | |
964f78b7 ILT |
3254 | if (DECL_STRUCT_FUNCTION(decl) == NULL) |
3255 | allocate_struct_function(decl, false); | |
3dafb85c | 3256 | cgraph_node::finalize_function(decl, true); |
036165d8 CM |
3257 | |
3258 | defs[i] = decl; | |
3259 | ++i; | |
3260 | } | |
3261 | } | |
3262 | ||
3263 | // Pass everything back to the middle-end. | |
3264 | ||
3265 | wrapup_global_declarations(defs, i); | |
3266 | ||
036165d8 CM |
3267 | delete[] defs; |
3268 | } | |
3269 | ||
f163907e ILT |
3270 | void |
3271 | Gcc_backend::write_export_data(const char* bytes, unsigned int size) | |
3272 | { | |
3273 | go_write_export_data(bytes, size); | |
3274 | } | |
3275 | ||
3276 | ||
90cbaa29 CM |
3277 | // Define a builtin function. BCODE is the builtin function code |
3278 | // defined by builtins.def. NAME is the name of the builtin function. | |
3279 | // LIBNAME is the name of the corresponding library function, and is | |
3280 | // NULL if there isn't one. FNTYPE is the type of the function. | |
3281 | // CONST_P is true if the function has the const attribute. | |
2021ff27 | 3282 | // NORETURN_P is true if the function has the noreturn attribute. |
a9ac13f7 | 3283 | |
90cbaa29 CM |
3284 | void |
3285 | Gcc_backend::define_builtin(built_in_function bcode, const char* name, | |
2021ff27 MP |
3286 | const char* libname, tree fntype, bool const_p, |
3287 | bool noreturn_p) | |
90cbaa29 CM |
3288 | { |
3289 | tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL, | |
3290 | libname, NULL_TREE); | |
3291 | if (const_p) | |
3292 | TREE_READONLY(decl) = 1; | |
2021ff27 MP |
3293 | if (noreturn_p) |
3294 | TREE_THIS_VOLATILE(decl) = 1; | |
90cbaa29 CM |
3295 | set_builtin_decl(bcode, decl, true); |
3296 | this->builtin_functions_[name] = this->make_function(decl); | |
3297 | if (libname != NULL) | |
3298 | { | |
3299 | decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL, | |
3300 | NULL, NULL_TREE); | |
3301 | if (const_p) | |
3302 | TREE_READONLY(decl) = 1; | |
2021ff27 MP |
3303 | if (noreturn_p) |
3304 | TREE_THIS_VOLATILE(decl) = 1; | |
90cbaa29 CM |
3305 | this->builtin_functions_[libname] = this->make_function(decl); |
3306 | } | |
3307 | } | |
a9ac13f7 ILT |
3308 | |
3309 | // Return the backend generator. | |
3310 | ||
3311 | Backend* | |
3312 | go_get_backend() | |
3313 | { | |
90cbaa29 | 3314 | return new Gcc_backend(); |
a9ac13f7 | 3315 | } |