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