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