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