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