]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/go/go-gcc.cc
predicates.md (indirect_branch_operand): Simplify.
[thirdparty/gcc.git] / gcc / go / go-gcc.cc
CommitLineData
a9ac13f7 1// go-gcc.cc -- Go frontend to gcc IR.
ef1ed13d 2// Copyright (C) 2011, 2012 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
27#ifndef ENABLE_BUILD_WITH_CXX
28extern "C"
29{
30#endif
31
32#include "tree.h"
94039447
ILT
33#include "tree-iterator.h"
34#include "gimple.h"
70f91024 35#include "toplev.h"
a9ac13f7
ILT
36
37#ifndef ENABLE_BUILD_WITH_CXX
38}
39#endif
40
e09ce6c5
ILT
41#include "go-c.h"
42
94039447 43#include "gogo.h"
a9ac13f7
ILT
44#include "backend.h"
45
46// A class wrapping a tree.
47
48class Gcc_tree
49{
50 public:
51 Gcc_tree(tree t)
52 : t_(t)
53 { }
54
55 tree
0aa5e7f2 56 get_tree() const
a9ac13f7
ILT
57 { return this->t_; }
58
ce842ad6
ILT
59 void
60 set_tree(tree t)
61 { this->t_ = t; }
62
a9ac13f7
ILT
63 private:
64 tree t_;
65};
66
67// In gcc, types, expressions, and statements are all trees.
68class Btype : public Gcc_tree
69{
70 public:
71 Btype(tree t)
72 : Gcc_tree(t)
73 { }
74};
75
76class Bexpression : public Gcc_tree
77{
78 public:
79 Bexpression(tree t)
80 : Gcc_tree(t)
81 { }
82};
83
84class Bstatement : public Gcc_tree
85{
86 public:
87 Bstatement(tree t)
88 : Gcc_tree(t)
89 { }
90};
91
94039447
ILT
92class Bfunction : public Gcc_tree
93{
94 public:
95 Bfunction(tree t)
96 : Gcc_tree(t)
97 { }
98};
99
5ad7db5f
ILT
100class Bblock : public Gcc_tree
101{
102 public:
103 Bblock(tree t)
104 : Gcc_tree(t)
105 { }
106};
107
e09ce6c5
ILT
108class Bvariable : public Gcc_tree
109{
110 public:
111 Bvariable(tree t)
112 : Gcc_tree(t)
113 { }
114};
115
d56e6679
ILT
116class Blabel : public Gcc_tree
117{
118 public:
119 Blabel(tree t)
120 : Gcc_tree(t)
121 { }
122};
123
a9ac13f7
ILT
124// This file implements the interface between the Go frontend proper
125// and the gcc IR. This implements specific instantiations of
126// abstract classes defined by the Go frontend proper. The Go
127// frontend proper class methods of these classes to generate the
128// backend representation.
129
130class Gcc_backend : public Backend
131{
132 public:
133 // Types.
134
135 Btype*
136 error_type()
482829ac 137 { return this->make_type(error_mark_node); }
a9ac13f7
ILT
138
139 Btype*
140 void_type()
0aa5e7f2 141 { return this->make_type(void_type_node); }
a9ac13f7
ILT
142
143 Btype*
144 bool_type()
0aa5e7f2 145 { return this->make_type(boolean_type_node); }
a9ac13f7
ILT
146
147 Btype*
0aa5e7f2 148 integer_type(bool, int);
a9ac13f7
ILT
149
150 Btype*
0aa5e7f2
ES
151 float_type(int);
152
153 Btype*
154 complex_type(int);
a9ac13f7
ILT
155
156 Btype*
482829ac 157 pointer_type(Btype*);
a9ac13f7 158
0aa5e7f2 159 Btype*
482829ac
ILT
160 function_type(const Btyped_identifier&,
161 const std::vector<Btyped_identifier>&,
162 const std::vector<Btyped_identifier>&,
8afa2bfb 163 const Location);
a9ac13f7
ILT
164
165 Btype*
6d69c02e 166 struct_type(const std::vector<Btyped_identifier>&);
a9ac13f7
ILT
167
168 Btype*
7fc2f86b
ILT
169 array_type(Btype*, Bexpression*);
170
171 Btype*
8afa2bfb 172 placeholder_pointer_type(const std::string&, Location, bool);
7fc2f86b
ILT
173
174 bool
175 set_placeholder_pointer_type(Btype*, Btype*);
176
177 bool
178 set_placeholder_function_type(Btype*, Btype*);
179
180 Btype*
8afa2bfb 181 placeholder_struct_type(const std::string&, Location);
7fc2f86b
ILT
182
183 bool
184 set_placeholder_struct_type(Btype* placeholder,
185 const std::vector<Btyped_identifier>&);
186
187 Btype*
8afa2bfb 188 placeholder_array_type(const std::string&, Location);
7fc2f86b
ILT
189
190 bool
191 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
192
193 Btype*
8afa2bfb 194 named_type(const std::string&, Btype*, Location);
7fc2f86b
ILT
195
196 Btype*
197 circular_pointer_type(Btype*, bool);
198
199 bool
200 is_circular_pointer_type(Btype*);
a9ac13f7 201
ef1ed13d
ILT
202 size_t
203 type_size(Btype*);
204
205 size_t
206 type_alignment(Btype*);
207
208 size_t
209 type_field_alignment(Btype*);
210
211 size_t
212 type_field_offset(Btype*, size_t index);
213
54466dde
ILT
214 // Expressions.
215
216 Bexpression*
217 zero_expression(Btype*);
218
a9ac13f7
ILT
219 // Statements.
220
f54d331e
ILT
221 Bstatement*
222 error_statement()
223 { return this->make_statement(error_mark_node); }
224
cfebcf30
ILT
225 Bstatement*
226 expression_statement(Bexpression*);
227
e09ce6c5
ILT
228 Bstatement*
229 init_statement(Bvariable* var, Bexpression* init);
230
a9ac13f7 231 Bstatement*
8afa2bfb 232 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
94039447 233
94039447
ILT
234 Bstatement*
235 return_statement(Bfunction*, const std::vector<Bexpression*>&,
8afa2bfb 236 Location);
a9ac13f7 237
db0adf82 238 Bstatement*
5ad7db5f 239 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
8afa2bfb 240 Location);
db0adf82 241
8d0b03a2
ILT
242 Bstatement*
243 switch_statement(Bexpression* value,
244 const std::vector<std::vector<Bexpression*> >& cases,
245 const std::vector<Bstatement*>& statements,
8afa2bfb 246 Location);
8d0b03a2 247
00b44a6e
ILT
248 Bstatement*
249 compound_statement(Bstatement*, Bstatement*);
250
8d0b03a2
ILT
251 Bstatement*
252 statement_list(const std::vector<Bstatement*>&);
253
5ad7db5f
ILT
254 // Blocks.
255
256 Bblock*
257 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
8afa2bfb 258 Location, Location);
5ad7db5f
ILT
259
260 void
261 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
262
263 Bstatement*
264 block_statement(Bblock*);
265
e09ce6c5
ILT
266 // Variables.
267
268 Bvariable*
269 error_variable()
270 { return new Bvariable(error_mark_node); }
271
272 Bvariable*
273 global_variable(const std::string& package_name,
274 const std::string& unique_prefix,
275 const std::string& name,
276 Btype* btype,
277 bool is_external,
278 bool is_hidden,
8afa2bfb 279 Location location);
e09ce6c5
ILT
280
281 void
282 global_variable_set_init(Bvariable*, Bexpression*);
283
284 Bvariable*
acf98146 285 local_variable(Bfunction*, const std::string&, Btype*, bool,
8afa2bfb 286 Location);
e09ce6c5
ILT
287
288 Bvariable*
acf98146 289 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
8afa2bfb 290 Location);
e09ce6c5 291
9131ad67
ILT
292 Bvariable*
293 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
8afa2bfb 294 Location, Bstatement**);
9131ad67 295
70f91024 296 Bvariable*
8afa2bfb 297 immutable_struct(const std::string&, bool, Btype*, Location);
70f91024
ILT
298
299 void
300 immutable_struct_set_init(Bvariable*, const std::string&, bool, Btype*,
8afa2bfb 301 Location, Bexpression*);
70f91024
ILT
302
303 Bvariable*
8afa2bfb 304 immutable_struct_reference(const std::string&, Btype*, Location);
70f91024 305
d56e6679
ILT
306 // Labels.
307
308 Blabel*
8afa2bfb 309 label(Bfunction*, const std::string& name, Location);
d56e6679
ILT
310
311 Bstatement*
312 label_definition_statement(Blabel*);
313
314 Bstatement*
8afa2bfb 315 goto_statement(Blabel*, Location);
d56e6679
ILT
316
317 Bexpression*
8afa2bfb 318 label_address(Blabel*, Location);
d56e6679 319
a9ac13f7 320 private:
d56e6679
ILT
321 // Make a Bexpression from a tree.
322 Bexpression*
323 make_expression(tree t)
324 { return new Bexpression(t); }
325
a9ac13f7
ILT
326 // Make a Bstatement from a tree.
327 Bstatement*
328 make_statement(tree t)
329 { return new Bstatement(t); }
0aa5e7f2
ES
330
331 // Make a Btype from a tree.
332 Btype*
333 make_type(tree t)
334 { return new Btype(t); }
7fc2f86b
ILT
335
336 Btype*
337 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
338
339 Btype*
340 fill_in_array(Btype*, Btype*, Bexpression*);
a9ac13f7
ILT
341};
342
d56e6679
ILT
343// A helper function.
344
345static inline tree
346get_identifier_from_string(const std::string& str)
347{
348 return get_identifier_with_length(str.data(), str.length());
349}
cfebcf30 350
0aa5e7f2
ES
351// Get an unnamed integer type.
352
353Btype*
354Gcc_backend::integer_type(bool is_unsigned, int bits)
355{
356 tree type;
357 if (is_unsigned)
358 {
359 if (bits == INT_TYPE_SIZE)
360 type = unsigned_type_node;
361 else if (bits == CHAR_TYPE_SIZE)
362 type = unsigned_char_type_node;
363 else if (bits == SHORT_TYPE_SIZE)
364 type = short_unsigned_type_node;
365 else if (bits == LONG_TYPE_SIZE)
366 type = long_unsigned_type_node;
367 else if (bits == LONG_LONG_TYPE_SIZE)
368 type = long_long_unsigned_type_node;
369 else
370 type = make_unsigned_type(bits);
371 }
372 else
373 {
374 if (bits == INT_TYPE_SIZE)
375 type = integer_type_node;
376 else if (bits == CHAR_TYPE_SIZE)
377 type = signed_char_type_node;
378 else if (bits == SHORT_TYPE_SIZE)
379 type = short_integer_type_node;
380 else if (bits == LONG_TYPE_SIZE)
381 type = long_integer_type_node;
382 else if (bits == LONG_LONG_TYPE_SIZE)
383 type = long_long_integer_type_node;
384 else
385 type = make_signed_type(bits);
386 }
387 return this->make_type(type);
388}
389
390// Get an unnamed float type.
391
392Btype*
393Gcc_backend::float_type(int bits)
394{
395 tree type;
396 if (bits == FLOAT_TYPE_SIZE)
397 type = float_type_node;
398 else if (bits == DOUBLE_TYPE_SIZE)
399 type = double_type_node;
400 else if (bits == LONG_DOUBLE_TYPE_SIZE)
401 type = long_double_type_node;
402 else
403 {
404 type = make_node(REAL_TYPE);
405 TYPE_PRECISION(type) = bits;
406 layout_type(type);
407 }
408 return this->make_type(type);
409}
410
411// Get an unnamed complex type.
412
413Btype*
414Gcc_backend::complex_type(int bits)
415{
416 tree type;
417 if (bits == FLOAT_TYPE_SIZE * 2)
418 type = complex_float_type_node;
419 else if (bits == DOUBLE_TYPE_SIZE * 2)
420 type = complex_double_type_node;
421 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
422 type = complex_long_double_type_node;
423 else
424 {
425 type = make_node(REAL_TYPE);
426 TYPE_PRECISION(type) = bits / 2;
427 layout_type(type);
428 type = build_complex_type(type);
429 }
430 return this->make_type(type);
431}
432
433// Get a pointer type.
434
435Btype*
482829ac 436Gcc_backend::pointer_type(Btype* to_type)
0aa5e7f2 437{
482829ac
ILT
438 tree to_type_tree = to_type->get_tree();
439 if (to_type_tree == error_mark_node)
440 return this->error_type();
441 tree type = build_pointer_type(to_type_tree);
0aa5e7f2
ES
442 return this->make_type(type);
443}
444
482829ac
ILT
445// Make a function type.
446
447Btype*
448Gcc_backend::function_type(const Btyped_identifier& receiver,
449 const std::vector<Btyped_identifier>& parameters,
450 const std::vector<Btyped_identifier>& results,
8afa2bfb 451 Location location)
482829ac
ILT
452{
453 tree args = NULL_TREE;
454 tree* pp = &args;
455 if (receiver.btype != NULL)
456 {
457 tree t = receiver.btype->get_tree();
458 if (t == error_mark_node)
459 return this->error_type();
460 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
461 pp = &TREE_CHAIN(*pp);
462 }
463
464 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
465 p != parameters.end();
466 ++p)
467 {
468 tree t = p->btype->get_tree();
469 if (t == error_mark_node)
470 return this->error_type();
471 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
472 pp = &TREE_CHAIN(*pp);
473 }
474
475 // Varargs is handled entirely at the Go level. When converted to
476 // GENERIC functions are not varargs.
477 *pp = void_list_node;
478
479 tree result;
480 if (results.empty())
481 result = void_type_node;
482 else if (results.size() == 1)
483 result = results.front().btype->get_tree();
484 else
485 {
486 result = make_node(RECORD_TYPE);
487 tree field_trees = NULL_TREE;
488 pp = &field_trees;
489 for (std::vector<Btyped_identifier>::const_iterator p = results.begin();
490 p != results.end();
491 ++p)
492 {
493 const std::string name = (p->name.empty()
494 ? "UNNAMED"
495 : p->name);
496 tree name_tree = get_identifier_from_string(name);
497 tree field_type_tree = p->btype->get_tree();
498 if (field_type_tree == error_mark_node)
499 return this->error_type();
26793fb5 500 gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
8afa2bfb
SD
501 tree field = build_decl(location.gcc_location(), FIELD_DECL,
502 name_tree, field_type_tree);
482829ac
ILT
503 DECL_CONTEXT(field) = result;
504 *pp = field;
505 pp = &DECL_CHAIN(field);
506 }
507 TYPE_FIELDS(result) = field_trees;
508 layout_type(result);
509 }
510 if (result == error_mark_node)
511 return this->error_type();
512
513 tree fntype = build_function_type(result, args);
514 if (fntype == error_mark_node)
515 return this->error_type();
516
517 return this->make_type(build_pointer_type(fntype));
518}
519
6d69c02e
ILT
520// Make a struct type.
521
522Btype*
523Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
524{
7fc2f86b
ILT
525 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
526}
527
528// Fill in the fields of a struct type.
529
530Btype*
531Gcc_backend::fill_in_struct(Btype* fill,
532 const std::vector<Btyped_identifier>& fields)
533{
534 tree fill_tree = fill->get_tree();
6d69c02e
ILT
535 tree field_trees = NULL_TREE;
536 tree* pp = &field_trees;
537 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
538 p != fields.end();
539 ++p)
540 {
541 tree name_tree = get_identifier_from_string(p->name);
542 tree type_tree = p->btype->get_tree();
543 if (type_tree == error_mark_node)
544 return this->error_type();
8afa2bfb
SD
545 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
546 type_tree);
7fc2f86b 547 DECL_CONTEXT(field) = fill_tree;
6d69c02e
ILT
548 *pp = field;
549 pp = &DECL_CHAIN(field);
550 }
7fc2f86b
ILT
551 TYPE_FIELDS(fill_tree) = field_trees;
552 layout_type(fill_tree);
553 return fill;
554}
555
556// Make an array type.
557
558Btype*
559Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
560{
561 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
562 element_btype, length);
563}
564
565// Fill in an array type.
566
567Btype*
568Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
569 Bexpression* length)
570{
571 tree element_type_tree = element_type->get_tree();
572 tree length_tree = length->get_tree();
573 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
574 return this->error_type();
575
576 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
577
578 length_tree = fold_convert(sizetype, length_tree);
579
580 // build_index_type takes the maximum index, which is one less than
581 // the length.
582 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
583 length_tree,
584 size_one_node));
585
586 tree fill_tree = fill->get_tree();
587 TREE_TYPE(fill_tree) = element_type_tree;
588 TYPE_DOMAIN(fill_tree) = index_type_tree;
589 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
590 layout_type(fill_tree);
591
592 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
593 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
594 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
595 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
596 TYPE_CANONICAL(fill_tree) =
597 build_array_type(TYPE_CANONICAL(element_type_tree),
598 TYPE_CANONICAL(index_type_tree));
599
600 return fill;
601}
602
603// Create a placeholder for a pointer type.
604
605Btype*
606Gcc_backend::placeholder_pointer_type(const std::string& name,
8afa2bfb 607 Location location, bool)
7fc2f86b 608{
3762c343 609 tree ret = build_distinct_type_copy(ptr_type_node);
26793fb5
ILT
610 if (!name.empty())
611 {
8afa2bfb 612 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
26793fb5
ILT
613 get_identifier_from_string(name),
614 ret);
615 TYPE_NAME(ret) = decl;
616 }
7fc2f86b
ILT
617 return this->make_type(ret);
618}
619
620// Set the real target type for a placeholder pointer type.
621
622bool
623Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
624 Btype* to_type)
625{
626 tree pt = placeholder->get_tree();
627 if (pt == error_mark_node)
628 return false;
629 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
630 tree tt = to_type->get_tree();
631 if (tt == error_mark_node)
632 {
ce842ad6 633 placeholder->set_tree(error_mark_node);
7fc2f86b
ILT
634 return false;
635 }
636 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
637 TREE_TYPE(pt) = TREE_TYPE(tt);
dcf30625
ILT
638 if (TYPE_NAME(pt) != NULL_TREE)
639 {
640 // Build the data structure gcc wants to see for a typedef.
641 tree copy = build_variant_type_copy(pt);
642 TYPE_NAME(copy) = NULL_TREE;
643 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
644 }
7fc2f86b
ILT
645 return true;
646}
647
648// Set the real values for a placeholder function type.
649
650bool
651Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
652{
653 return this->set_placeholder_pointer_type(placeholder, ft);
654}
655
656// Create a placeholder for a struct type.
657
658Btype*
659Gcc_backend::placeholder_struct_type(const std::string& name,
8afa2bfb 660 Location location)
7fc2f86b
ILT
661{
662 tree ret = make_node(RECORD_TYPE);
7c0434e5
ILT
663 if (!name.empty())
664 {
665 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
666 get_identifier_from_string(name),
667 ret);
668 TYPE_NAME(ret) = decl;
669 }
6d69c02e
ILT
670 return this->make_type(ret);
671}
672
7fc2f86b
ILT
673// Fill in the fields of a placeholder struct type.
674
675bool
676Gcc_backend::set_placeholder_struct_type(
677 Btype* placeholder,
678 const std::vector<Btyped_identifier>& fields)
679{
680 tree t = placeholder->get_tree();
681 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
682 Btype* r = this->fill_in_struct(placeholder, fields);
dcf30625 683
7c0434e5
ILT
684 if (TYPE_NAME(t) != NULL_TREE)
685 {
686 // Build the data structure gcc wants to see for a typedef.
687 tree copy = build_distinct_type_copy(t);
688 TYPE_NAME(copy) = NULL_TREE;
689 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
690 }
dcf30625 691
7fc2f86b
ILT
692 return r->get_tree() != error_mark_node;
693}
694
695// Create a placeholder for an array type.
696
697Btype*
698Gcc_backend::placeholder_array_type(const std::string& name,
8afa2bfb 699 Location location)
7fc2f86b
ILT
700{
701 tree ret = make_node(ARRAY_TYPE);
8afa2bfb 702 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
7fc2f86b
ILT
703 get_identifier_from_string(name),
704 ret);
705 TYPE_NAME(ret) = decl;
706 return this->make_type(ret);
707}
708
709// Fill in the fields of a placeholder array type.
710
711bool
712Gcc_backend::set_placeholder_array_type(Btype* placeholder,
713 Btype* element_btype,
714 Bexpression* length)
715{
716 tree t = placeholder->get_tree();
717 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
718 Btype* r = this->fill_in_array(placeholder, element_btype, length);
dcf30625
ILT
719
720 // Build the data structure gcc wants to see for a typedef.
11304b7b 721 tree copy = build_distinct_type_copy(t);
dcf30625
ILT
722 TYPE_NAME(copy) = NULL_TREE;
723 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
724
7fc2f86b
ILT
725 return r->get_tree() != error_mark_node;
726}
727
728// Return a named version of a type.
729
730Btype*
731Gcc_backend::named_type(const std::string& name, Btype* btype,
8afa2bfb 732 Location location)
7fc2f86b
ILT
733{
734 tree type = btype->get_tree();
735 if (type == error_mark_node)
736 return this->error_type();
11304b7b
ILT
737
738 // The middle-end expects a basic type to have a name. In Go every
739 // basic type will have a name. The first time we see a basic type,
740 // give it whatever Go name we have at this point.
741 if (TYPE_NAME(type) == NULL_TREE
742 && location.gcc_location() == BUILTINS_LOCATION
743 && (TREE_CODE(type) == INTEGER_TYPE
744 || TREE_CODE(type) == REAL_TYPE
745 || TREE_CODE(type) == COMPLEX_TYPE
746 || TREE_CODE(type) == BOOLEAN_TYPE))
747 {
748 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
749 get_identifier_from_string(name),
750 type);
751 TYPE_NAME(type) = decl;
752 return this->make_type(type);
753 }
754
dcf30625 755 tree copy = build_variant_type_copy(type);
8afa2bfb 756 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
7fc2f86b 757 get_identifier_from_string(name),
dcf30625
ILT
758 copy);
759 DECL_ORIGINAL_TYPE(decl) = type;
760 TYPE_NAME(copy) = decl;
761 return this->make_type(copy);
7fc2f86b
ILT
762}
763
764// Return a pointer type used as a marker for a circular type.
765
766Btype*
767Gcc_backend::circular_pointer_type(Btype*, bool)
768{
769 return this->make_type(ptr_type_node);
770}
771
772// Return whether we might be looking at a circular type.
773
774bool
775Gcc_backend::is_circular_pointer_type(Btype* btype)
776{
777 return btype->get_tree() == ptr_type_node;
778}
779
ef1ed13d
ILT
780// Return the size of a type.
781
782size_t
783Gcc_backend::type_size(Btype* btype)
784{
08be22dc
ILT
785 tree t = btype->get_tree();
786 if (t == error_mark_node)
787 return 1;
788 t = TYPE_SIZE_UNIT(t);
ef1ed13d
ILT
789 gcc_assert(TREE_CODE(t) == INTEGER_CST);
790 gcc_assert(TREE_INT_CST_HIGH(t) == 0);
791 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
792 size_t ret = static_cast<size_t>(val_wide);
793 gcc_assert(ret == val_wide);
794 return ret;
795}
796
797// Return the alignment of a type.
798
799size_t
800Gcc_backend::type_alignment(Btype* btype)
801{
08be22dc
ILT
802 tree t = btype->get_tree();
803 if (t == error_mark_node)
804 return 1;
805 return TYPE_ALIGN_UNIT(t);
ef1ed13d
ILT
806}
807
808// Return the alignment of a struct field of type BTYPE.
809
810size_t
811Gcc_backend::type_field_alignment(Btype* btype)
812{
08be22dc
ILT
813 tree t = btype->get_tree();
814 if (t == error_mark_node)
815 return 1;
816 return go_field_alignment(t);
ef1ed13d
ILT
817}
818
819// Return the offset of a field in a struct.
820
821size_t
822Gcc_backend::type_field_offset(Btype* btype, size_t index)
823{
824 tree struct_tree = btype->get_tree();
08be22dc
ILT
825 if (struct_tree == error_mark_node)
826 return 0;
ef1ed13d
ILT
827 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
828 tree field = TYPE_FIELDS(struct_tree);
829 for (; index > 0; --index)
830 {
831 field = DECL_CHAIN(field);
832 gcc_assert(field != NULL_TREE);
833 }
834 HOST_WIDE_INT offset_wide = int_byte_position(field);
835 gcc_assert(offset_wide >= 0);
836 size_t ret = static_cast<size_t>(offset_wide);
837 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
838 return ret;
839}
840
54466dde
ILT
841// Return the zero value for a type.
842
843Bexpression*
844Gcc_backend::zero_expression(Btype* btype)
845{
846 tree t = btype->get_tree();
847 tree ret;
848 if (t == error_mark_node)
849 ret = error_mark_node;
850 else
851 ret = build_zero_cst(t);
852 return tree_to_expr(ret);
853}
854
cfebcf30
ILT
855// An expression as a statement.
856
857Bstatement*
858Gcc_backend::expression_statement(Bexpression* expr)
859{
860 return this->make_statement(expr->get_tree());
861}
862
e09ce6c5
ILT
863// Variable initialization.
864
865Bstatement*
866Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
867{
868 tree var_tree = var->get_tree();
869 tree init_tree = init->get_tree();
870 if (var_tree == error_mark_node || init_tree == error_mark_node)
871 return this->error_statement();
872 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
873 DECL_INITIAL(var_tree) = init_tree;
874 return this->make_statement(build1_loc(DECL_SOURCE_LOCATION(var_tree),
875 DECL_EXPR, void_type_node, var_tree));
876}
877
a9ac13f7
ILT
878// Assignment.
879
880Bstatement*
94039447 881Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
8afa2bfb 882 Location location)
a9ac13f7 883{
94039447
ILT
884 tree lhs_tree = lhs->get_tree();
885 tree rhs_tree = rhs->get_tree();
886 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
00b44a6e 887 return this->error_statement();
8afa2bfb
SD
888 return this->make_statement(fold_build2_loc(location.gcc_location(),
889 MODIFY_EXPR,
a9ac13f7 890 void_type_node,
94039447
ILT
891 lhs_tree, rhs_tree));
892}
893
894// Return.
895
896Bstatement*
897Gcc_backend::return_statement(Bfunction* bfunction,
898 const std::vector<Bexpression*>& vals,
8afa2bfb 899 Location location)
94039447
ILT
900{
901 tree fntree = bfunction->get_tree();
902 if (fntree == error_mark_node)
00b44a6e 903 return this->error_statement();
94039447
ILT
904 tree result = DECL_RESULT(fntree);
905 if (result == error_mark_node)
00b44a6e 906 return this->error_statement();
94039447
ILT
907 tree ret;
908 if (vals.empty())
8afa2bfb
SD
909 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
910 NULL_TREE);
94039447
ILT
911 else if (vals.size() == 1)
912 {
913 tree val = vals.front()->get_tree();
914 if (val == error_mark_node)
00b44a6e 915 return this->error_statement();
8afa2bfb
SD
916 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
917 void_type_node, result,
918 vals.front()->get_tree());
919 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
920 void_type_node, set);
94039447
ILT
921 }
922 else
923 {
924 // To return multiple values, copy the values into a temporary
925 // variable of the right structure type, and then assign the
926 // temporary variable to the DECL_RESULT in the return
927 // statement.
928 tree stmt_list = NULL_TREE;
929 tree rettype = TREE_TYPE(result);
930 tree rettmp = create_tmp_var(rettype, "RESULT");
931 tree field = TYPE_FIELDS(rettype);
932 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
933 p != vals.end();
934 p++, field = DECL_CHAIN(field))
935 {
936 gcc_assert(field != NULL_TREE);
8afa2bfb
SD
937 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
938 TREE_TYPE(field), rettmp, field,
939 NULL_TREE);
94039447
ILT
940 tree val = (*p)->get_tree();
941 if (val == error_mark_node)
00b44a6e 942 return this->error_statement();
8afa2bfb
SD
943 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
944 void_type_node,
94039447
ILT
945 ref, (*p)->get_tree());
946 append_to_statement_list(set, &stmt_list);
947 }
948 gcc_assert(field == NULL_TREE);
8afa2bfb
SD
949 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
950 void_type_node,
94039447 951 result, rettmp);
8afa2bfb
SD
952 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
953 void_type_node, set);
94039447
ILT
954 append_to_statement_list(ret_expr, &stmt_list);
955 ret = stmt_list;
956 }
957 return this->make_statement(ret);
a9ac13f7
ILT
958}
959
db0adf82
ILT
960// If.
961
962Bstatement*
5ad7db5f 963Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
8afa2bfb 964 Bblock* else_block, Location location)
db0adf82
ILT
965{
966 tree cond_tree = condition->get_tree();
967 tree then_tree = then_block->get_tree();
968 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
969 if (cond_tree == error_mark_node
970 || then_tree == error_mark_node
971 || else_tree == error_mark_node)
00b44a6e 972 return this->error_statement();
8afa2bfb
SD
973 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
974 cond_tree, then_tree, else_tree);
db0adf82
ILT
975 return this->make_statement(ret);
976}
977
8d0b03a2
ILT
978// Switch.
979
980Bstatement*
981Gcc_backend::switch_statement(
982 Bexpression* value,
983 const std::vector<std::vector<Bexpression*> >& cases,
984 const std::vector<Bstatement*>& statements,
8afa2bfb 985 Location switch_location)
8d0b03a2
ILT
986{
987 gcc_assert(cases.size() == statements.size());
988
989 tree stmt_list = NULL_TREE;
990 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
991 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
992 ps != statements.end();
993 ++ps, ++pc)
994 {
995 if (pc->empty())
996 {
997 source_location loc = (*ps != NULL
8afa2bfb
SD
998 ? EXPR_LOCATION((*ps)->get_tree())
999 : UNKNOWN_LOCATION);
8d0b03a2 1000 tree label = create_artificial_label(loc);
3d528853 1001 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
8d0b03a2
ILT
1002 append_to_statement_list(c, &stmt_list);
1003 }
1004 else
1005 {
1006 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
1007 pcv != pc->end();
1008 ++pcv)
1009 {
1010 tree t = (*pcv)->get_tree();
1011 if (t == error_mark_node)
00b44a6e 1012 return this->error_statement();
8d0b03a2
ILT
1013 source_location loc = EXPR_LOCATION(t);
1014 tree label = create_artificial_label(loc);
3d528853 1015 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
8d0b03a2
ILT
1016 append_to_statement_list(c, &stmt_list);
1017 }
1018 }
1019
1020 if (*ps != NULL)
1021 {
1022 tree t = (*ps)->get_tree();
1023 if (t == error_mark_node)
00b44a6e 1024 return this->error_statement();
8d0b03a2
ILT
1025 append_to_statement_list(t, &stmt_list);
1026 }
1027 }
1028
1029 tree tv = value->get_tree();
1030 if (tv == error_mark_node)
00b44a6e 1031 return this->error_statement();
8afa2bfb
SD
1032 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
1033 void_type_node, tv, stmt_list, NULL_TREE);
8d0b03a2
ILT
1034 return this->make_statement(t);
1035}
1036
00b44a6e
ILT
1037// Pair of statements.
1038
1039Bstatement*
1040Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
1041{
1042 tree stmt_list = NULL_TREE;
1043 tree t = s1->get_tree();
1044 if (t == error_mark_node)
1045 return this->error_statement();
1046 append_to_statement_list(t, &stmt_list);
1047 t = s2->get_tree();
1048 if (t == error_mark_node)
1049 return this->error_statement();
1050 append_to_statement_list(t, &stmt_list);
1051 return this->make_statement(stmt_list);
1052}
1053
8d0b03a2
ILT
1054// List of statements.
1055
1056Bstatement*
1057Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
1058{
1059 tree stmt_list = NULL_TREE;
1060 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1061 p != statements.end();
1062 ++p)
1063 {
1064 tree t = (*p)->get_tree();
1065 if (t == error_mark_node)
00b44a6e 1066 return this->error_statement();
8d0b03a2
ILT
1067 append_to_statement_list(t, &stmt_list);
1068 }
1069 return this->make_statement(stmt_list);
1070}
1071
5ad7db5f
ILT
1072// Make a block. For some reason gcc uses a dual structure for
1073// blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1074// BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1075// the Bblock.
1076
1077Bblock*
1078Gcc_backend::block(Bfunction* function, Bblock* enclosing,
1079 const std::vector<Bvariable*>& vars,
8afa2bfb
SD
1080 Location start_location,
1081 Location)
5ad7db5f
ILT
1082{
1083 tree block_tree = make_node(BLOCK);
1084 if (enclosing == NULL)
1085 {
1086 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1087 // until we have a proper representation of the init function.
1088 tree fndecl;
1089 if (function == NULL)
1090 fndecl = current_function_decl;
1091 else
1092 fndecl = function->get_tree();
1093 gcc_assert(fndecl != NULL_TREE);
1094
1095 // We may have already created a block for local variables when
1096 // we take the address of a parameter.
1097 if (DECL_INITIAL(fndecl) == NULL_TREE)
1098 {
1099 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
1100 DECL_INITIAL(fndecl) = block_tree;
1101 }
1102 else
1103 {
1104 tree superblock_tree = DECL_INITIAL(fndecl);
1105 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1106 tree* pp;
1107 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1108 *pp != NULL_TREE;
1109 pp = &BLOCK_CHAIN(*pp))
1110 ;
1111 *pp = block_tree;
1112 }
1113 }
1114 else
1115 {
1116 tree superbind_tree = enclosing->get_tree();
1117 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1118 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1119
1120 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1121 tree* pp;
1122 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1123 *pp != NULL_TREE;
1124 pp = &BLOCK_CHAIN(*pp))
1125 ;
1126 *pp = block_tree;
1127 }
1128
1129 tree* pp = &BLOCK_VARS(block_tree);
1130 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1131 pv != vars.end();
1132 ++pv)
1133 {
1134 *pp = (*pv)->get_tree();
1135 if (*pp != error_mark_node)
1136 pp = &DECL_CHAIN(*pp);
1137 }
1138 *pp = NULL_TREE;
1139
1140 TREE_USED(block_tree) = 1;
1141
8afa2bfb
SD
1142 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1143 void_type_node, BLOCK_VARS(block_tree),
1144 NULL_TREE, block_tree);
5ad7db5f
ILT
1145 TREE_SIDE_EFFECTS(bind_tree) = 1;
1146
1147 return new Bblock(bind_tree);
1148}
1149
1150// Add statements to a block.
1151
1152void
1153Gcc_backend::block_add_statements(Bblock* bblock,
1154 const std::vector<Bstatement*>& statements)
1155{
1156 tree stmt_list = NULL_TREE;
1157 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1158 p != statements.end();
1159 ++p)
1160 {
1161 tree s = (*p)->get_tree();
1162 if (s != error_mark_node)
1163 append_to_statement_list(s, &stmt_list);
1164 }
1165
1166 tree bind_tree = bblock->get_tree();
1167 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1168 BIND_EXPR_BODY(bind_tree) = stmt_list;
1169}
1170
1171// Return a block as a statement.
1172
1173Bstatement*
1174Gcc_backend::block_statement(Bblock* bblock)
1175{
1176 tree bind_tree = bblock->get_tree();
1177 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1178 return this->make_statement(bind_tree);
1179}
1180
e09ce6c5
ILT
1181// Make a global variable.
1182
1183Bvariable*
1184Gcc_backend::global_variable(const std::string& package_name,
1185 const std::string& unique_prefix,
1186 const std::string& name,
1187 Btype* btype,
1188 bool is_external,
1189 bool is_hidden,
8afa2bfb 1190 Location location)
e09ce6c5
ILT
1191{
1192 tree type_tree = btype->get_tree();
1193 if (type_tree == error_mark_node)
1194 return this->error_variable();
1195
1196 std::string var_name(package_name);
1197 var_name.push_back('.');
1198 var_name.append(name);
8afa2bfb 1199 tree decl = build_decl(location.gcc_location(), VAR_DECL,
e09ce6c5
ILT
1200 get_identifier_from_string(var_name),
1201 type_tree);
1202 if (is_external)
1203 DECL_EXTERNAL(decl) = 1;
1204 else
1205 TREE_STATIC(decl) = 1;
1206 if (!is_hidden)
1207 {
1208 TREE_PUBLIC(decl) = 1;
1209
1210 std::string asm_name(unique_prefix);
1211 asm_name.push_back('.');
1212 asm_name.append(var_name);
1213 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1214 }
1215 TREE_USED(decl) = 1;
1216
1217 go_preserve_from_gc(decl);
1218
1219 return new Bvariable(decl);
1220}
1221
1222// Set the initial value of a global variable.
1223
1224void
1225Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1226{
1227 tree expr_tree = expr->get_tree();
1228 if (expr_tree == error_mark_node)
1229 return;
1230 gcc_assert(TREE_CONSTANT(expr_tree));
1231 tree var_decl = var->get_tree();
1232 if (var_decl == error_mark_node)
1233 return;
1234 DECL_INITIAL(var_decl) = expr_tree;
1235}
1236
1237// Make a local variable.
1238
1239Bvariable*
1240Gcc_backend::local_variable(Bfunction* function, const std::string& name,
acf98146 1241 Btype* btype, bool is_address_taken,
8afa2bfb 1242 Location location)
e09ce6c5
ILT
1243{
1244 tree type_tree = btype->get_tree();
1245 if (type_tree == error_mark_node)
1246 return this->error_variable();
8afa2bfb 1247 tree decl = build_decl(location.gcc_location(), VAR_DECL,
e09ce6c5
ILT
1248 get_identifier_from_string(name),
1249 type_tree);
1250 DECL_CONTEXT(decl) = function->get_tree();
1251 TREE_USED(decl) = 1;
acf98146
ILT
1252 if (is_address_taken)
1253 TREE_ADDRESSABLE(decl) = 1;
e09ce6c5
ILT
1254 go_preserve_from_gc(decl);
1255 return new Bvariable(decl);
1256}
1257
1258// Make a function parameter variable.
1259
1260Bvariable*
1261Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
acf98146 1262 Btype* btype, bool is_address_taken,
8afa2bfb 1263 Location location)
e09ce6c5
ILT
1264{
1265 tree type_tree = btype->get_tree();
1266 if (type_tree == error_mark_node)
1267 return this->error_variable();
8afa2bfb 1268 tree decl = build_decl(location.gcc_location(), PARM_DECL,
e09ce6c5
ILT
1269 get_identifier_from_string(name),
1270 type_tree);
1271 DECL_CONTEXT(decl) = function->get_tree();
1272 DECL_ARG_TYPE(decl) = type_tree;
1273 TREE_USED(decl) = 1;
acf98146
ILT
1274 if (is_address_taken)
1275 TREE_ADDRESSABLE(decl) = 1;
e09ce6c5
ILT
1276 go_preserve_from_gc(decl);
1277 return new Bvariable(decl);
1278}
1279
9131ad67
ILT
1280// Make a temporary variable.
1281
1282Bvariable*
1283Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1284 Btype* btype, Bexpression* binit,
1285 bool is_address_taken,
8afa2bfb 1286 Location location,
9131ad67
ILT
1287 Bstatement** pstatement)
1288{
1289 tree type_tree = btype->get_tree();
1290 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1291 if (type_tree == error_mark_node || init_tree == error_mark_node)
1292 {
1293 *pstatement = this->error_statement();
1294 return this->error_variable();
1295 }
1296
1297 tree var;
1298 // We can only use create_tmp_var if the type is not addressable.
1299 if (!TREE_ADDRESSABLE(type_tree))
1300 var = create_tmp_var(type_tree, "GOTMP");
1301 else
1302 {
1303 gcc_assert(bblock != NULL);
8afa2bfb 1304 var = build_decl(location.gcc_location(), VAR_DECL,
9131ad67
ILT
1305 create_tmp_var_name("GOTMP"),
1306 type_tree);
1307 DECL_ARTIFICIAL(var) = 1;
1308 DECL_IGNORED_P(var) = 1;
1309 TREE_USED(var) = 1;
1310 // FIXME: Permitting function to be NULL here is a temporary
1311 // measure until we have a proper representation of the init
1312 // function.
1313 if (function != NULL)
1314 DECL_CONTEXT(var) = function->get_tree();
1315 else
1316 {
1317 gcc_assert(current_function_decl != NULL_TREE);
1318 DECL_CONTEXT(var) = current_function_decl;
1319 }
1320
1321 // We have to add this variable to the BLOCK and the BIND_EXPR.
1322 tree bind_tree = bblock->get_tree();
1323 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1324 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1325 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1326 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1327 BLOCK_VARS(block_tree) = var;
1328 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1329 }
1330
1331 if (init_tree != NULL_TREE)
8afa2bfb
SD
1332 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1333 init_tree);
9131ad67
ILT
1334
1335 if (is_address_taken)
1336 TREE_ADDRESSABLE(var) = 1;
1337
8afa2bfb
SD
1338 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1339 DECL_EXPR,
9131ad67
ILT
1340 void_type_node, var));
1341 return new Bvariable(var);
1342}
1343
70f91024
ILT
1344// Create a named immutable initialized data structure.
1345
1346Bvariable*
1347Gcc_backend::immutable_struct(const std::string& name, bool, Btype* btype,
8afa2bfb 1348 Location location)
70f91024
ILT
1349{
1350 tree type_tree = btype->get_tree();
1351 if (type_tree == error_mark_node)
1352 return this->error_variable();
1353 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
8afa2bfb 1354 tree decl = build_decl(location.gcc_location(), VAR_DECL,
70f91024
ILT
1355 get_identifier_from_string(name),
1356 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1357 TREE_STATIC(decl) = 1;
1358 TREE_READONLY(decl) = 1;
1359 TREE_CONSTANT(decl) = 1;
1360 TREE_USED(decl) = 1;
1361 DECL_ARTIFICIAL(decl) = 1;
1362
1363 // We don't call rest_of_decl_compilation until we have the
1364 // initializer.
1365
1366 go_preserve_from_gc(decl);
1367 return new Bvariable(decl);
1368}
1369
1370// Set the initializer for a variable created by immutable_struct.
1371// This is where we finish compiling the variable.
1372
1373void
1374Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1375 bool is_common, Btype*,
8afa2bfb 1376 Location,
70f91024
ILT
1377 Bexpression* initializer)
1378{
1379 tree decl = var->get_tree();
1380 tree init_tree = initializer->get_tree();
1381 if (decl == error_mark_node || init_tree == error_mark_node)
1382 return;
1383
1384 DECL_INITIAL(decl) = init_tree;
1385
1386 // We can't call make_decl_one_only until we set DECL_INITIAL.
1387 if (!is_common)
1388 TREE_PUBLIC(decl) = 1;
1389 else
1390 {
1391 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1392 resolve_unique_section(decl, 1, 0);
1393 }
1394
1395 rest_of_decl_compilation(decl, 1, 0);
1396}
1397
1398// Return a reference to an immutable initialized data structure
1399// defined in another package.
1400
1401Bvariable*
1402Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
8afa2bfb 1403 Location location)
70f91024
ILT
1404{
1405 tree type_tree = btype->get_tree();
1406 if (type_tree == error_mark_node)
1407 return this->error_variable();
1408 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
8afa2bfb 1409 tree decl = build_decl(location.gcc_location(), VAR_DECL,
70f91024
ILT
1410 get_identifier_from_string(name),
1411 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1412 TREE_READONLY(decl) = 1;
1413 TREE_CONSTANT(decl) = 1;
1414 DECL_ARTIFICIAL(decl) = 1;
1415 TREE_PUBLIC(decl) = 1;
1416 DECL_EXTERNAL(decl) = 1;
1417 go_preserve_from_gc(decl);
1418 return new Bvariable(decl);
1419}
1420
d56e6679
ILT
1421// Make a label.
1422
1423Blabel*
1424Gcc_backend::label(Bfunction* function, const std::string& name,
8afa2bfb 1425 Location location)
d56e6679
ILT
1426{
1427 tree decl;
1428 if (name.empty())
8afa2bfb 1429 decl = create_artificial_label(location.gcc_location());
d56e6679
ILT
1430 else
1431 {
1432 tree id = get_identifier_from_string(name);
8afa2bfb
SD
1433 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1434 void_type_node);
d56e6679
ILT
1435 DECL_CONTEXT(decl) = function->get_tree();
1436 }
1437 return new Blabel(decl);
1438}
1439
1440// Make a statement which defines a label.
1441
1442Bstatement*
1443Gcc_backend::label_definition_statement(Blabel* label)
1444{
1445 tree lab = label->get_tree();
1446 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1447 void_type_node, lab);
1448 return this->make_statement(ret);
1449}
1450
1451// Make a goto statement.
1452
1453Bstatement*
8afa2bfb 1454Gcc_backend::goto_statement(Blabel* label, Location location)
d56e6679
ILT
1455{
1456 tree lab = label->get_tree();
8afa2bfb
SD
1457 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
1458 lab);
d56e6679
ILT
1459 return this->make_statement(ret);
1460}
1461
1462// Get the address of a label.
1463
1464Bexpression*
8afa2bfb 1465Gcc_backend::label_address(Blabel* label, Location location)
d56e6679
ILT
1466{
1467 tree lab = label->get_tree();
1468 TREE_USED(lab) = 1;
1469 TREE_ADDRESSABLE(lab) = 1;
8afa2bfb
SD
1470 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
1471 build_fold_addr_expr_loc(location.gcc_location(),
1472 lab));
d56e6679
ILT
1473 return this->make_expression(ret);
1474}
1475
a9ac13f7
ILT
1476// The single backend.
1477
1478static Gcc_backend gcc_backend;
1479
1480// Return the backend generator.
1481
1482Backend*
1483go_get_backend()
1484{
1485 return &gcc_backend;
1486}
1487
1488// FIXME: Temporary functions while converting to the new backend
1489// interface.
1490
e09ce6c5
ILT
1491Btype*
1492tree_to_type(tree t)
1493{
1494 return new Btype(t);
1495}
1496
a9ac13f7
ILT
1497Bexpression*
1498tree_to_expr(tree t)
1499{
1500 return new Bexpression(t);
1501}
1502
db0adf82
ILT
1503Bstatement*
1504tree_to_stat(tree t)
1505{
1506 return new Bstatement(t);
1507}
1508
94039447
ILT
1509Bfunction*
1510tree_to_function(tree t)
1511{
1512 return new Bfunction(t);
1513}
1514
5ad7db5f
ILT
1515Bblock*
1516tree_to_block(tree t)
1517{
1518 gcc_assert(TREE_CODE(t) == BIND_EXPR);
1519 return new Bblock(t);
1520}
1521
0aa5e7f2
ES
1522tree
1523type_to_tree(Btype* bt)
1524{
1525 return bt->get_tree();
1526}
1527
d56e6679 1528tree
db0adf82 1529expr_to_tree(Bexpression* be)
d56e6679
ILT
1530{
1531 return be->get_tree();
1532}
1533
a9ac13f7 1534tree
db0adf82 1535stat_to_tree(Bstatement* bs)
a9ac13f7
ILT
1536{
1537 return bs->get_tree();
1538}
e09ce6c5 1539
5ad7db5f
ILT
1540tree
1541block_to_tree(Bblock* bb)
1542{
1543 return bb->get_tree();
1544}
1545
e09ce6c5
ILT
1546tree
1547var_to_tree(Bvariable* bv)
1548{
1549 return bv->get_tree();
1550}