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