mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
gogo.cc
8174 lines (7060 loc) · 229 KB
/
gogo.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// gogo.cc -- Go frontend parsed representation.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go-system.h"
#include <fstream>
#include "filenames.h"
#include "go-c.h"
#include "go-diagnostics.h"
#include "go-encode-id.h"
#include "go-dump.h"
#include "go-optimize.h"
#include "lex.h"
#include "types.h"
#include "statements.h"
#include "expressions.h"
#include "runtime.h"
#include "import.h"
#include "export.h"
#include "backend.h"
#include "gogo.h"
// Class Gogo.
Gogo::Gogo(Backend* backend, Linemap* linemap, int, int pointer_size)
: backend_(backend),
linemap_(linemap),
package_(NULL),
functions_(),
globals_(new Bindings(NULL)),
file_block_names_(),
imports_(),
imported_unsafe_(false),
current_file_imported_unsafe_(false),
packages_(),
init_functions_(),
var_deps_(),
need_init_fn_(false),
init_fn_name_(),
imported_init_fns_(),
pkgpath_(),
pkgpath_symbol_(),
prefix_(),
pkgpath_set_(false),
pkgpath_from_option_(false),
prefix_from_option_(false),
relative_import_path_(),
verify_types_(),
interface_types_(),
specific_type_functions_(),
specific_type_functions_are_written_(false),
named_types_are_converted_(false),
analysis_sets_(),
gc_roots_()
{
const Location loc = Linemap::predeclared_location();
Named_type* uint8_type = Type::make_integer_type("uint8", true, 8,
RUNTIME_TYPE_KIND_UINT8);
this->add_named_type(uint8_type);
this->add_named_type(Type::make_integer_type("uint16", true, 16,
RUNTIME_TYPE_KIND_UINT16));
this->add_named_type(Type::make_integer_type("uint32", true, 32,
RUNTIME_TYPE_KIND_UINT32));
this->add_named_type(Type::make_integer_type("uint64", true, 64,
RUNTIME_TYPE_KIND_UINT64));
this->add_named_type(Type::make_integer_type("int8", false, 8,
RUNTIME_TYPE_KIND_INT8));
this->add_named_type(Type::make_integer_type("int16", false, 16,
RUNTIME_TYPE_KIND_INT16));
Named_type* int32_type = Type::make_integer_type("int32", false, 32,
RUNTIME_TYPE_KIND_INT32);
this->add_named_type(int32_type);
this->add_named_type(Type::make_integer_type("int64", false, 64,
RUNTIME_TYPE_KIND_INT64));
this->add_named_type(Type::make_float_type("float32", 32,
RUNTIME_TYPE_KIND_FLOAT32));
this->add_named_type(Type::make_float_type("float64", 64,
RUNTIME_TYPE_KIND_FLOAT64));
this->add_named_type(Type::make_complex_type("complex64", 64,
RUNTIME_TYPE_KIND_COMPLEX64));
this->add_named_type(Type::make_complex_type("complex128", 128,
RUNTIME_TYPE_KIND_COMPLEX128));
int int_type_size = pointer_size;
if (int_type_size < 32)
int_type_size = 32;
this->add_named_type(Type::make_integer_type("uint", true,
int_type_size,
RUNTIME_TYPE_KIND_UINT));
Named_type* int_type = Type::make_integer_type("int", false, int_type_size,
RUNTIME_TYPE_KIND_INT);
this->add_named_type(int_type);
this->add_named_type(Type::make_integer_type("uintptr", true,
pointer_size,
RUNTIME_TYPE_KIND_UINTPTR));
// "byte" is an alias for "uint8".
uint8_type->integer_type()->set_is_byte();
Named_object* byte_type = Named_object::make_type("byte", NULL, uint8_type,
loc);
byte_type->type_value()->set_is_alias();
this->add_named_type(byte_type->type_value());
// "rune" is an alias for "int32".
int32_type->integer_type()->set_is_rune();
Named_object* rune_type = Named_object::make_type("rune", NULL, int32_type,
loc);
rune_type->type_value()->set_is_alias();
this->add_named_type(rune_type->type_value());
this->add_named_type(Type::make_named_bool_type());
this->add_named_type(Type::make_named_string_type());
// "error" is interface { Error() string }.
{
Typed_identifier_list *methods = new Typed_identifier_list;
Typed_identifier_list *results = new Typed_identifier_list;
results->push_back(Typed_identifier("", Type::lookup_string_type(), loc));
Type *method_type = Type::make_function_type(NULL, NULL, results, loc);
methods->push_back(Typed_identifier("Error", method_type, loc));
Interface_type *error_iface = Type::make_interface_type(methods, loc);
error_iface->finalize_methods();
Named_type *error_type = Named_object::make_type("error", NULL, error_iface, loc)->type_value();
this->add_named_type(error_type);
}
this->globals_->add_constant(Typed_identifier("true",
Type::make_boolean_type(),
loc),
NULL,
Expression::make_boolean(true, loc),
0);
this->globals_->add_constant(Typed_identifier("false",
Type::make_boolean_type(),
loc),
NULL,
Expression::make_boolean(false, loc),
0);
this->globals_->add_constant(Typed_identifier("nil", Type::make_nil_type(),
loc),
NULL,
Expression::make_nil(loc),
0);
Type* abstract_int_type = Type::make_abstract_integer_type();
this->globals_->add_constant(Typed_identifier("iota", abstract_int_type,
loc),
NULL,
Expression::make_iota(),
0);
Function_type* new_type = Type::make_function_type(NULL, NULL, NULL, loc);
new_type->set_is_varargs();
new_type->set_is_builtin();
this->globals_->add_function_declaration("new", NULL, new_type, loc);
Function_type* make_type = Type::make_function_type(NULL, NULL, NULL, loc);
make_type->set_is_varargs();
make_type->set_is_builtin();
this->globals_->add_function_declaration("make", NULL, make_type, loc);
Typed_identifier_list* len_result = new Typed_identifier_list();
len_result->push_back(Typed_identifier("", int_type, loc));
Function_type* len_type = Type::make_function_type(NULL, NULL, len_result,
loc);
len_type->set_is_builtin();
this->globals_->add_function_declaration("len", NULL, len_type, loc);
Typed_identifier_list* cap_result = new Typed_identifier_list();
cap_result->push_back(Typed_identifier("", int_type, loc));
Function_type* cap_type = Type::make_function_type(NULL, NULL, len_result,
loc);
cap_type->set_is_builtin();
this->globals_->add_function_declaration("cap", NULL, cap_type, loc);
Function_type* print_type = Type::make_function_type(NULL, NULL, NULL, loc);
print_type->set_is_varargs();
print_type->set_is_builtin();
this->globals_->add_function_declaration("print", NULL, print_type, loc);
print_type = Type::make_function_type(NULL, NULL, NULL, loc);
print_type->set_is_varargs();
print_type->set_is_builtin();
this->globals_->add_function_declaration("println", NULL, print_type, loc);
Type *empty = Type::make_empty_interface_type(loc);
Typed_identifier_list* panic_parms = new Typed_identifier_list();
panic_parms->push_back(Typed_identifier("e", empty, loc));
Function_type *panic_type = Type::make_function_type(NULL, panic_parms,
NULL, loc);
panic_type->set_is_builtin();
this->globals_->add_function_declaration("panic", NULL, panic_type, loc);
Typed_identifier_list* recover_result = new Typed_identifier_list();
recover_result->push_back(Typed_identifier("", empty, loc));
Function_type* recover_type = Type::make_function_type(NULL, NULL,
recover_result,
loc);
recover_type->set_is_builtin();
this->globals_->add_function_declaration("recover", NULL, recover_type, loc);
Function_type* close_type = Type::make_function_type(NULL, NULL, NULL, loc);
close_type->set_is_varargs();
close_type->set_is_builtin();
this->globals_->add_function_declaration("close", NULL, close_type, loc);
Typed_identifier_list* copy_result = new Typed_identifier_list();
copy_result->push_back(Typed_identifier("", int_type, loc));
Function_type* copy_type = Type::make_function_type(NULL, NULL,
copy_result, loc);
copy_type->set_is_varargs();
copy_type->set_is_builtin();
this->globals_->add_function_declaration("copy", NULL, copy_type, loc);
Function_type* append_type = Type::make_function_type(NULL, NULL, NULL, loc);
append_type->set_is_varargs();
append_type->set_is_builtin();
this->globals_->add_function_declaration("append", NULL, append_type, loc);
Function_type* complex_type = Type::make_function_type(NULL, NULL, NULL, loc);
complex_type->set_is_varargs();
complex_type->set_is_builtin();
this->globals_->add_function_declaration("complex", NULL, complex_type, loc);
Function_type* real_type = Type::make_function_type(NULL, NULL, NULL, loc);
real_type->set_is_varargs();
real_type->set_is_builtin();
this->globals_->add_function_declaration("real", NULL, real_type, loc);
Function_type* imag_type = Type::make_function_type(NULL, NULL, NULL, loc);
imag_type->set_is_varargs();
imag_type->set_is_builtin();
this->globals_->add_function_declaration("imag", NULL, imag_type, loc);
Function_type* delete_type = Type::make_function_type(NULL, NULL, NULL, loc);
delete_type->set_is_varargs();
delete_type->set_is_builtin();
this->globals_->add_function_declaration("delete", NULL, delete_type, loc);
}
// Convert a pkgpath into a string suitable for a symbol. Note that
// this transformation is convenient but imperfect. A -fgo-pkgpath
// option of a/b_c will conflict with a -fgo-pkgpath option of a_b/c,
// possibly leading to link time errors.
std::string
Gogo::pkgpath_for_symbol(const std::string& pkgpath)
{
std::string s = pkgpath;
for (size_t i = 0; i < s.length(); ++i)
{
char c = s[i];
if ((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9'))
;
else
s[i] = '_';
}
return s;
}
// Get the package path to use for type reflection data. This should
// ideally be unique across the entire link.
const std::string&
Gogo::pkgpath() const
{
go_assert(this->pkgpath_set_);
return this->pkgpath_;
}
// Set the package path from the -fgo-pkgpath command line option.
void
Gogo::set_pkgpath(const std::string& arg)
{
go_assert(!this->pkgpath_set_);
this->pkgpath_ = arg;
this->pkgpath_set_ = true;
this->pkgpath_from_option_ = true;
}
// Get the package path to use for symbol names.
const std::string&
Gogo::pkgpath_symbol() const
{
go_assert(this->pkgpath_set_);
return this->pkgpath_symbol_;
}
// Set the unique prefix to use to determine the package path, from
// the -fgo-prefix command line option.
void
Gogo::set_prefix(const std::string& arg)
{
go_assert(!this->prefix_from_option_);
this->prefix_ = arg;
this->prefix_from_option_ = true;
}
// Munge name for use in an error message.
std::string
Gogo::message_name(const std::string& name)
{
return go_localize_identifier(Gogo::unpack_hidden_name(name).c_str());
}
// Get the package name.
const std::string&
Gogo::package_name() const
{
go_assert(this->package_ != NULL);
return this->package_->package_name();
}
// Set the package name.
void
Gogo::set_package_name(const std::string& package_name,
Location location)
{
if (this->package_ != NULL)
{
if (this->package_->package_name() != package_name)
go_error_at(location, "expected package %<%s%>",
Gogo::message_name(this->package_->package_name()).c_str());
return;
}
// Now that we know the name of the package we are compiling, set
// the package path to use for reflect.Type.PkgPath and global
// symbol names.
if (this->pkgpath_set_)
this->pkgpath_symbol_ = Gogo::pkgpath_for_symbol(this->pkgpath_);
else
{
if (!this->prefix_from_option_ && package_name == "main")
{
this->pkgpath_ = package_name;
this->pkgpath_symbol_ = Gogo::pkgpath_for_symbol(package_name);
}
else
{
if (!this->prefix_from_option_)
this->prefix_ = "go";
this->pkgpath_ = this->prefix_ + '.' + package_name;
this->pkgpath_symbol_ = (Gogo::pkgpath_for_symbol(this->prefix_) + '.'
+ Gogo::pkgpath_for_symbol(package_name));
}
this->pkgpath_set_ = true;
}
this->package_ = this->register_package(this->pkgpath_,
this->pkgpath_symbol_, location);
this->package_->set_package_name(package_name, location);
if (this->is_main_package())
{
// Declare "main" as a function which takes no parameters and
// returns no value.
Location uloc = Linemap::unknown_location();
this->declare_function(Gogo::pack_hidden_name("main", false),
Type::make_function_type (NULL, NULL, NULL, uloc),
uloc);
}
}
// Return whether this is the "main" package. This is not true if
// -fgo-pkgpath or -fgo-prefix was used.
bool
Gogo::is_main_package() const
{
return (this->package_name() == "main"
&& !this->pkgpath_from_option_
&& !this->prefix_from_option_);
}
// Import a package.
void
Gogo::import_package(const std::string& filename,
const std::string& local_name,
bool is_local_name_exported,
bool must_exist,
Location location)
{
if (filename.empty())
{
go_error_at(location, "import path is empty");
return;
}
const char *pf = filename.data();
const char *pend = pf + filename.length();
while (pf < pend)
{
unsigned int c;
int adv = Lex::fetch_char(pf, &c);
if (adv == 0)
{
go_error_at(location, "import path contains invalid UTF-8 sequence");
return;
}
if (c == '\0')
{
go_error_at(location, "import path contains NUL");
return;
}
if (c < 0x20 || c == 0x7f)
{
go_error_at(location, "import path contains control character");
return;
}
if (c == '\\')
{
go_error_at(location, "import path contains backslash; use slash");
return;
}
if (Lex::is_unicode_space(c))
{
go_error_at(location, "import path contains space character");
return;
}
if (c < 0x7f && strchr("!\"#$%&'()*,:;<=>?[]^`{|}", c) != NULL)
{
go_error_at(location,
"import path contains invalid character '%c'", c);
return;
}
pf += adv;
}
if (IS_ABSOLUTE_PATH(filename.c_str()))
{
go_error_at(location, "import path cannot be absolute path");
return;
}
if (local_name == "init")
go_error_at(location, "cannot import package as init");
if (filename == "unsafe")
{
this->import_unsafe(local_name, is_local_name_exported, location);
this->current_file_imported_unsafe_ = true;
return;
}
Imports::const_iterator p = this->imports_.find(filename);
if (p != this->imports_.end())
{
Package* package = p->second;
package->set_location(location);
std::string ln = local_name;
bool is_ln_exported = is_local_name_exported;
if (ln.empty())
{
ln = package->package_name();
go_assert(!ln.empty());
is_ln_exported = Lex::is_exported_name(ln);
}
if (ln == "_")
;
else if (ln == ".")
{
Bindings* bindings = package->bindings();
for (Bindings::const_declarations_iterator p =
bindings->begin_declarations();
p != bindings->end_declarations();
++p)
this->add_dot_import_object(p->second);
std::string dot_alias = "." + package->package_name();
package->add_alias(dot_alias, location);
}
else
{
package->add_alias(ln, location);
ln = this->pack_hidden_name(ln, is_ln_exported);
this->package_->bindings()->add_package(ln, package);
}
return;
}
Import::Stream* stream = Import::open_package(filename, location,
this->relative_import_path_);
if (stream == NULL)
{
if (must_exist)
go_error_at(location, "import file %qs not found", filename.c_str());
return;
}
Import imp(stream, location);
imp.register_builtin_types(this);
Package* package = imp.import(this, local_name, is_local_name_exported);
if (package != NULL)
{
if (package->pkgpath() == this->pkgpath())
go_error_at(location,
("imported package uses same package path as package "
"being compiled (see -fgo-pkgpath option)"));
this->imports_.insert(std::make_pair(filename, package));
}
delete stream;
}
Import_init *
Gogo::lookup_init(const std::string& init_name)
{
Import_init tmp("", init_name, -1);
Import_init_set::iterator it = this->imported_init_fns_.find(&tmp);
return (it != this->imported_init_fns_.end()) ? *it : NULL;
}
// Add an import control function for an imported package to the list.
void
Gogo::add_import_init_fn(const std::string& package_name,
const std::string& init_name, int prio)
{
for (Import_init_set::iterator p =
this->imported_init_fns_.begin();
p != this->imported_init_fns_.end();
++p)
{
Import_init *ii = (*p);
if (ii->init_name() == init_name)
{
// If a test of package P1, built as part of package P1,
// imports package P2, and P2 imports P1 (perhaps
// indirectly), then we will see the same import name with
// different import priorities. That is OK, so don't give
// an error about it.
if (ii->package_name() != package_name)
{
go_error_at(Linemap::unknown_location(),
"duplicate package initialization name %qs",
Gogo::message_name(init_name).c_str());
go_inform(Linemap::unknown_location(), "used by package %qs",
Gogo::message_name(ii->package_name()).c_str());
go_inform(Linemap::unknown_location(), " and by package %qs",
Gogo::message_name(package_name).c_str());
}
ii->set_priority(prio);
return;
}
}
Import_init* nii = new Import_init(package_name, init_name, prio);
this->imported_init_fns_.insert(nii);
}
// Return whether we are at the global binding level.
bool
Gogo::in_global_scope() const
{
return this->functions_.empty();
}
// Return the current binding contour.
Bindings*
Gogo::current_bindings()
{
if (!this->functions_.empty())
return this->functions_.back().blocks.back()->bindings();
else if (this->package_ != NULL)
return this->package_->bindings();
else
return this->globals_;
}
const Bindings*
Gogo::current_bindings() const
{
if (!this->functions_.empty())
return this->functions_.back().blocks.back()->bindings();
else if (this->package_ != NULL)
return this->package_->bindings();
else
return this->globals_;
}
void
Gogo::update_init_priority(Import_init* ii,
std::set<const Import_init *>* visited)
{
visited->insert(ii);
int succ_prior = -1;
for (std::set<std::string>::const_iterator pci =
ii->precursors().begin();
pci != ii->precursors().end();
++pci)
{
Import_init* succ = this->lookup_init(*pci);
if (visited->find(succ) == visited->end())
update_init_priority(succ, visited);
succ_prior = std::max(succ_prior, succ->priority());
}
if (ii->priority() <= succ_prior)
ii->set_priority(succ_prior + 1);
}
void
Gogo::recompute_init_priorities()
{
std::set<Import_init *> nonroots;
for (Import_init_set::const_iterator p =
this->imported_init_fns_.begin();
p != this->imported_init_fns_.end();
++p)
{
const Import_init *ii = *p;
for (std::set<std::string>::const_iterator pci =
ii->precursors().begin();
pci != ii->precursors().end();
++pci)
{
Import_init* ii = this->lookup_init(*pci);
nonroots.insert(ii);
}
}
// Recursively update priorities starting at roots.
std::set<const Import_init*> visited;
for (Import_init_set::iterator p =
this->imported_init_fns_.begin();
p != this->imported_init_fns_.end();
++p)
{
Import_init* ii = *p;
if (nonroots.find(ii) != nonroots.end())
continue;
update_init_priority(ii, &visited);
}
}
// Add statements to INIT_STMTS which run the initialization
// functions for imported packages. This is only used for the "main"
// package.
void
Gogo::init_imports(std::vector<Bstatement*>& init_stmts, Bfunction *bfunction)
{
go_assert(this->is_main_package());
if (this->imported_init_fns_.empty())
return;
Location unknown_loc = Linemap::unknown_location();
Function_type* func_type =
Type::make_function_type(NULL, NULL, NULL, unknown_loc);
Btype* fntype = func_type->get_backend_fntype(this);
// Recompute init priorities based on a walk of the init graph.
recompute_init_priorities();
// We must call them in increasing priority order.
std::vector<const Import_init*> v;
for (Import_init_set::const_iterator p =
this->imported_init_fns_.begin();
p != this->imported_init_fns_.end();
++p)
v.push_back(*p);
std::sort(v.begin(), v.end(), priority_compare);
// We build calls to the init functions, which take no arguments.
std::vector<Bexpression*> empty_args;
for (std::vector<const Import_init*>::const_iterator p = v.begin();
p != v.end();
++p)
{
const Import_init* ii = *p;
std::string user_name = ii->package_name() + ".init";
const std::string& init_name(ii->init_name());
Bfunction* pfunc = this->backend()->function(fntype, user_name, init_name,
true, true, true, false,
false, unknown_loc);
Bexpression* pfunc_code =
this->backend()->function_code_expression(pfunc, unknown_loc);
Bexpression* pfunc_call =
this->backend()->call_expression(pfunc_code, empty_args,
NULL, unknown_loc);
init_stmts.push_back(this->backend()->expression_statement(bfunction,
pfunc_call));
}
}
// Register global variables with the garbage collector. We need to
// register all variables which can hold a pointer value. They become
// roots during the mark phase. We build a struct that is easy to
// hook into a list of roots.
// struct __go_gc_root_list
// {
// struct __go_gc_root_list* __next;
// struct __go_gc_root
// {
// void* __decl;
// size_t __size;
// } __roots[];
// };
// The last entry in the roots array has a NULL decl field.
void
Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
std::vector<Bstatement*>& init_stmts,
Bfunction* init_bfn)
{
if (var_gc.empty())
return;
Type* pvt = Type::make_pointer_type(Type::make_void_type());
Type* uint_type = Type::lookup_integer_type("uint");
Struct_type* root_type = Type::make_builtin_struct_type(2,
"__decl", pvt,
"__size", uint_type);
Location builtin_loc = Linemap::predeclared_location();
unsigned roots_len = var_gc.size() + this->gc_roots_.size() + 1;
Expression* length = Expression::make_integer_ul(roots_len, NULL,
builtin_loc);
Array_type* root_array_type = Type::make_array_type(root_type, length);
root_array_type->set_is_array_incomparable();
Type* ptdt = Type::make_type_descriptor_ptr_type();
Struct_type* root_list_type =
Type::make_builtin_struct_type(2,
"__next", ptdt,
"__roots", root_array_type);
// Build an initializer for the __roots array.
Expression_list* roots_init = new Expression_list();
for (std::vector<Named_object*>::const_iterator p = var_gc.begin();
p != var_gc.end();
++p)
{
Expression_list* init = new Expression_list();
Location no_loc = (*p)->location();
Expression* decl = Expression::make_var_reference(*p, no_loc);
Expression* decl_addr =
Expression::make_unary(OPERATOR_AND, decl, no_loc);
init->push_back(decl_addr);
Expression* decl_size =
Expression::make_type_info(decl->type(), Expression::TYPE_INFO_SIZE);
init->push_back(decl_size);
Expression* root_ctor =
Expression::make_struct_composite_literal(root_type, init, no_loc);
roots_init->push_back(root_ctor);
}
for (std::vector<Expression*>::const_iterator p = this->gc_roots_.begin();
p != this->gc_roots_.end();
++p)
{
Expression_list *init = new Expression_list();
Expression* expr = *p;
Location eloc = expr->location();
init->push_back(expr);
Type* type = expr->type()->points_to();
go_assert(type != NULL);
Expression* size =
Expression::make_type_info(type, Expression::TYPE_INFO_SIZE);
init->push_back(size);
Expression* root_ctor =
Expression::make_struct_composite_literal(root_type, init, eloc);
roots_init->push_back(root_ctor);
}
// The list ends with a NULL entry.
Expression_list* null_init = new Expression_list();
Expression* nil = Expression::make_nil(builtin_loc);
null_init->push_back(nil);
Expression *zero = Expression::make_integer_ul(0, NULL, builtin_loc);
null_init->push_back(zero);
Expression* null_root_ctor =
Expression::make_struct_composite_literal(root_type, null_init,
builtin_loc);
roots_init->push_back(null_root_ctor);
// Build a constructor for the struct.
Expression_list* root_list_init = new Expression_list();
root_list_init->push_back(nil);
Expression* roots_ctor =
Expression::make_array_composite_literal(root_array_type, roots_init,
builtin_loc);
root_list_init->push_back(roots_ctor);
Expression* root_list_ctor =
Expression::make_struct_composite_literal(root_list_type, root_list_init,
builtin_loc);
Expression* root_addr = Expression::make_unary(OPERATOR_AND, root_list_ctor,
builtin_loc);
root_addr->unary_expression()->set_is_gc_root();
Expression* register_roots = Runtime::make_call(Runtime::REGISTER_GC_ROOTS,
builtin_loc, 1, root_addr);
Translate_context context(this, NULL, NULL, NULL);
Bexpression* bcall = register_roots->get_backend(&context);
init_stmts.push_back(this->backend()->expression_statement(init_bfn, bcall));
}
// Get the name to use for the import control function. If there is a
// global function or variable, then we know that that name must be
// unique in the link, and we use it as the basis for our name.
const std::string&
Gogo::get_init_fn_name()
{
if (this->init_fn_name_.empty())
{
go_assert(this->package_ != NULL);
if (this->is_main_package())
{
// Use a name which the runtime knows.
this->init_fn_name_ = "__go_init_main";
}
else
{
std::string s = this->pkgpath_symbol();
s.append("..import");
this->init_fn_name_ = s;
}
}
return this->init_fn_name_;
}
// Build the decl for the initialization function.
Named_object*
Gogo::initialization_function_decl()
{
std::string name = this->get_init_fn_name();
Location loc = this->package_->location();
Function_type* fntype = Type::make_function_type(NULL, NULL, NULL, loc);
Function* initfn = new Function(fntype, NULL, NULL, loc);
return Named_object::make_function(name, NULL, initfn);
}
// Create the magic initialization function. CODE_STMT is the
// code that it needs to run.
Named_object*
Gogo::create_initialization_function(Named_object* initfn,
Bstatement* code_stmt)
{
// Make sure that we thought we needed an initialization function,
// as otherwise we will not have reported it in the export data.
go_assert(this->is_main_package() || this->need_init_fn_);
if (initfn == NULL)
initfn = this->initialization_function_decl();
// Bind the initialization function code to a block.
Bfunction* fndecl = initfn->func_value()->get_or_make_decl(this, initfn);
Location pkg_loc = this->package_->location();
std::vector<Bvariable*> vars;
this->backend()->block(fndecl, NULL, vars, pkg_loc, pkg_loc);
if (!this->backend()->function_set_body(fndecl, code_stmt))
{
go_assert(saw_errors());
return NULL;
}
return initfn;
}
// Search for references to VAR in any statements or called functions.
class Find_var : public Traverse
{
public:
// A hash table we use to avoid looping. The index is the name of a
// named object. We only look through objects defined in this
// package.
typedef Unordered_set(const void*) Seen_objects;
Find_var(Named_object* var, Seen_objects* seen_objects)
: Traverse(traverse_expressions),
var_(var), seen_objects_(seen_objects), found_(false)
{ }
// Whether the variable was found.
bool
found() const
{ return this->found_; }
int
expression(Expression**);
private:
// The variable we are looking for.
Named_object* var_;
// Names of objects we have already seen.
Seen_objects* seen_objects_;
// True if the variable was found.
bool found_;
};
// See if EXPR refers to VAR, looking through function calls and
// variable initializations.
int
Find_var::expression(Expression** pexpr)
{
Expression* e = *pexpr;
Var_expression* ve = e->var_expression();
if (ve != NULL)
{
Named_object* v = ve->named_object();
if (v == this->var_)
{
this->found_ = true;
return TRAVERSE_EXIT;
}
if (v->is_variable() && v->package() == NULL)
{
Expression* init = v->var_value()->init();
if (init != NULL)
{
std::pair<Seen_objects::iterator, bool> ins =
this->seen_objects_->insert(v);
if (ins.second)
{
// This is the first time we have seen this name.
if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
}
}
}
}
// We traverse the code of any function or bound method we see. Note that
// this means that we will traverse the code of a function or bound method
// whose address is taken even if it is not called.
Func_expression* fe = e->func_expression();
Bound_method_expression* bme = e->bound_method_expression();
if (fe != NULL || bme != NULL)
{
const Named_object* f = fe != NULL ? fe->named_object() : bme->function();
if (f->is_function() && f->package() == NULL)
{
std::pair<Seen_objects::iterator, bool> ins =
this->seen_objects_->insert(f);
if (ins.second)
{
// This is the first time we have seen this name.
if (f->func_value()->block()->traverse(this) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
}
}
}
Temporary_reference_expression* tre = e->temporary_reference_expression();
if (tre != NULL)
{
Temporary_statement* ts = tre->statement();
Expression* init = ts->init();