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