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