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
|
#include "rust-session-manager.h"
#include "rust-diagnostics.h"
#include "diagnostic.h"
#include "input.h"
#include "target.h"
#include "tm.h"
#include "tm_p.h"
#include "rust-lex.h"
#include "rust-parse.h"
#include "rust-scan.h"
#include "rust-resolution.h"
#include "rust-compile.h"
#include "rust-target.h"
#include <algorithm>
extern Linemap *
rust_get_linemap ();
extern Backend *
rust_get_backend ();
namespace Rust {
// Simple wrapper for FILE* that simplifies destruction.
struct RAIIFile
{
FILE *file;
RAIIFile (const char *filename) : file (fopen (filename, "r")) {}
~RAIIFile () { fclose (file); }
};
// Implicitly enable a target_feature (and recursively enable dependencies).
void
Session::implicitly_enable_feature (::std::string feature_name)
{
// TODO: is this really required since features added would be complete via
// target spec?
if (!options.target_data.has_key_value_pair ("target_data", feature_name))
{
// if feature has dependencies, enable them
if (feature_name == "aes")
{
implicitly_enable_feature ("sse2");
}
else if (feature_name == "avx")
{
implicitly_enable_feature ("sse4.2");
}
else if (feature_name == "avx2")
{
implicitly_enable_feature ("avx");
}
else if (feature_name == "fma")
{
implicitly_enable_feature ("avx");
}
else if (feature_name == "pclmulqdq")
{
implicitly_enable_feature ("sse2");
}
else if (feature_name == "sha")
{
implicitly_enable_feature ("sse2");
}
else if (feature_name == "sse2")
{
implicitly_enable_feature ("sse");
}
else if (feature_name == "sse3")
{
implicitly_enable_feature ("sse2");
}
else if (feature_name == "sse4.1")
{
implicitly_enable_feature ("sse3");
}
else if (feature_name == "sse4.2")
{
implicitly_enable_feature ("sse4.1");
}
else if (feature_name == "ssse3")
{
implicitly_enable_feature ("sse3");
}
options.target_data.insert_key_value_pair ("target_feature",
::std::move (feature_name));
}
}
// Meant to enable all target features. As this will be done by target hook,
// this method's deprecated.
void
Session::enable_features ()
{
bool has_target_crt_static = false;
const char *target = "PLACEHOLDER";
fprintf (
stderr,
"ERROR: Somewhere in call chain Session::enable_features is called.\n");
if (has_target_crt_static)
{
// enable "crt-static" attribute
}
/* TODO: do this via target hook. have one for each target that implicitly
* enables the
* features for that platform. Would probably have to make custom target hook.
*/
/*
if (target == "x86" || target == "x86_64") {
if (TARGET_ISA_AES) {
// enable aes, implicitly enable sse2
implicitly_enable_feature("aes");
}
if (TARGET_ISA_AVX) {
// enable avx, implicitly enable sse4.2
implicitly_enable_feature("sse4.2");
}
if (TARGET_ISA_AVX2) {
// enable avx2, implicitly enable avx
implicitly_enable_feature("avx");
}
if (TARGET_ISA_BMI) {
// enable bmi1
implicitly_enable_feature("bmi1");
}
if (TARGET_ISA_BMI2) {
// enable bmi2
implicitly_enable_feature("bmi2");
}
if (TARGET_ISA_FMA) {
// enable fma, implicitly enable avx
implicitly_enable_feature("fma");
}
if (TARGET_ISA_FXSR) {
// enable fxsr
implicitly_enable_feature("fxsr");
}
if (TARGET_ISA_LZCNT) {
// enable lzcnt
implicitly_enable_feature("lzcnt");
}
if (TARGET_ISA_VPCLMULQDQ) {
// enable pclmulqdq, implicitly enable sse2
implicitly_enable_feature("pclmulqdq");
}
if (TARGET_ISA_POPCNT) {
// enable popcnt
implicitly_enable_feature("popcnt");
}
if (TARGET_ISA_RDRND) {
// enable rdrand
implicitly_enable_feature("rdrand");
}
if (TARGET_ISA_RDSEED) {
// enable rdseed
implicitly_enable_feature("rdseed");
}
if (TARGET_ISA_SHA) {
// enable sha, implicitly enable sse2
implicitly_enable_feature("sha");
}
if (TARGET_ISA_SSE) {
// enable sse
implicitly_enable_feature("sse");
}
if (TARGET_ISA_SSE2) {
// enable sse2, implicitly enable sse
implicitly_enable_feature("sse2");
}
if (TARGET_ISA_SSE3) {
// enable sse3, implicitly enable sse2
implicitly_enable_feature("sse3");
}
if (TARGET_ISA_SSE4_1) {
// enable sse4.1, implicitly enable sse3
implicitly_enable_feature("sse4.1");
}
if (TARGET_ISA_SSE4_2) {
// enable sse4.2, implicitly enable sse4.1
implicitly_enable_feature("sse4.2");
}
if (TARGET_ISA_SSSE3) {
// enable ssse3, implicitly enable sse3
implicitly_enable_feature("ssse3");
}
if (TARGET_ISA_XSAVE) {
// enable xsave
implicitly_enable_feature("xsave");
}
if (TARGET_ISA_XSAVEC) {
// enable xsavec
implicitly_enable_feature("xsavec");
}
if (TARGET_ISA_XSAVEOPT) {
// enable xsaveopt
implicitly_enable_feature("xsaveopt");
}
if (TARGET_ISA_XSAVES) {
// enable xsaves
implicitly_enable_feature("xsaves");
}
}
options.target_data.features.shrink_to_fit();
::std::sort(options.target_data.features.begin(),
options.target_data.features.end());*/
}
void
Session::init ()
{
#ifndef TARGET_RUST_OS_INFO
#define TARGET_RUST_OS_INFO()
#endif
//#define builtin_rust_info(KEY, VALUE) rust_add_target_info (KEY, VALUE)
// might as well use c++ stuff
#define builtin_rust_info(KEY, VALUE) \
options.target_data.insert_key_value_pair (KEY, VALUE)
// initialise target hooks
// targetrustm.rust_cpu_info();
// targetrustm.rust_os_info();
// ok, that's not working too well TODO - see if can salvage old
// implementation
TARGET_RUST_CPU_INFO ();
TARGET_RUST_OS_INFO ();
#undef builtin_rust_info
// target-independent values that should exist in all targets
options.target_data.insert_key_value_pair ("target_pointer_width",
std::to_string (POINTER_SIZE));
options.target_data.insert_key_value_pair ("target_endian", BYTES_BIG_ENDIAN
? "big"
: "little");
// TODO: find min atomic width and max atomic width
// from it, add atomic-related stuff for sizes 8, 16, 32, 64, and 128 (if
// inside bounds) in rustc, min atomic width is a known quantity (or 8 if not
// known), and max is also a known quantity (or is pointer size if not known)
// TODO: add atomic pointer if some criteria is satisfied
// TODO: find whether target has "atomic cas"
// add debug_assertions if enabled and proc_macro if crate type has it or
// whatever
// derived values from hook
options.target_data.init_derived_values ();
}
// Initialise default options. Actually called before handle_option, unlike init
// itself.
void
Session::init_options ()
{
options.dump_option = CompileOptions::NO_DUMP;
}
// Handle option selection.
bool
Session::handle_option (
enum opt_code code, const char *arg, HOST_WIDE_INT value ATTRIBUTE_UNUSED,
int kind ATTRIBUTE_UNUSED, location_t loc ATTRIBUTE_UNUSED,
const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
{
// used to store whether results of various stuff are successful
bool ret = true;
// Handles options as listed in lang.opt.
switch (code)
{
case OPT_I:
// TODO: add search path
break;
case OPT_L:
// TODO: add library link path or something
break;
case OPT_frust_dump_:
// enable dump and return whether this was successful
if (arg != NULL)
{
ret = enable_dump (::std::string (arg));
}
else
{
ret = false;
}
break;
// no option handling for -o
default:
// return 1 to indicate option is valid
break;
}
return ret;
}
/* Enables a certain dump depending on the name passed in. Returns true if name
* is valid, false otherwise. */
bool
Session::enable_dump (::std::string arg)
{
// FIXME: change dumping algorithm when new non-inhibiting dump system is
// created
if (arg == "all")
{
error_at (
UNKNOWN_LOCATION,
"dumping all is not supported as of now. choose 'lex', 'parse', or 'target_options");
return false;
}
else if (arg == "lex")
{
options.dump_option = CompileOptions::LEXER_DUMP;
}
else if (arg == "parse")
{
options.dump_option = CompileOptions::PARSER_AST_DUMP;
}
else if (arg == "register_plugins")
{
options.dump_option = CompileOptions::REGISTER_PLUGINS_DUMP;
}
else if (arg == "injection")
{
options.dump_option = CompileOptions::INJECTION_DUMP;
}
else if (arg == "expansion")
{
options.dump_option = CompileOptions::EXPANSION_DUMP;
}
else if (arg == "resolution")
{
options.dump_option = CompileOptions::RESOLUTION_DUMP;
}
else if (arg == "target_options")
{
// special case - dump all target options, and then quit compilation
// nope, option handling called before init, so have to make this an
// actual compile option
// options.target_data.dump_target_options();
// return false;
options.dump_option = CompileOptions::TARGET_OPTION_DUMP;
}
else if (arg == "")
{
error_at (UNKNOWN_LOCATION,
"dump option was not given a name. choose 'lex', 'parse', or 'target_options'");
return false;
}
else
{
error_at (UNKNOWN_LOCATION,
"dump option '%s' was unrecognised. choose 'lex', 'parse', or 'target_options",
arg.c_str ());
return false;
}
return true;
}
/* Actual main entry point for front-end. Called from langhook to parse files.
*/
void
Session::parse_files (int num_files, const char **files)
{
for (int i = 0; i < num_files; i++)
{
parse_file (files[i]);
}
// TODO: should semantic analysis be dealed with here? or per file? for now,
// per-file.
}
// Parses a single file with filename filename.
void
Session::parse_file (const char *filename)
{
RAIIFile file_wrap (filename);
if (file_wrap.file == NULL)
{
fatal_error (UNKNOWN_LOCATION, "cannot open filename %s: %m", filename);
}
Backend *backend = rust_get_backend ();
// parse file here
// create lexer and parser - these are file-specific and so aren't instance
// variables
Rust::Lexer lex (filename, file_wrap.file, rust_get_linemap ());
Rust::Parser parser (lex);
// generate crate from parser
auto parsed_crate = parser.parse_crate ();
// give a chance to give some debug
switch (options.dump_option)
{
case CompileOptions::LEXER_DUMP:
parser.debug_dump_lex_output ();
break;
case CompileOptions::PARSER_AST_DUMP:
parser.debug_dump_ast_output (parsed_crate);
break;
default:
break;
}
/* basic pipeline:
* - lex
* - parse
* - register plugins (dummy stage for now) - attribute injection? what is
* this? (attribute injection is injecting attributes specified in command
* line into crate root)
* - injection (some lint checks or dummy, register builtin macros, crate
* injection)
* - expansion (expands all macros, maybe build test harness, AST validation,
* maybe macro crate)
* - resolution (name resolution, type resolution, maybe feature checking,
* maybe buffered lints)
* TODO not done */
fprintf (stderr, "\033[0;31mSUCCESSFULLY PARSED CRATE \n\033[0m");
// register plugins pipeline stage
register_plugins (parsed_crate);
fprintf (stderr, "\033[0;31mSUCCESSFULLY REGISTERED PLUGINS \n\033[0m");
if (options.dump_option == CompileOptions::REGISTER_PLUGINS_DUMP)
{
// TODO: what do I dump here?
return;
}
// injection pipeline stage
injection (parsed_crate);
fprintf (stderr, "\033[0;31mSUCCESSFULLY FINISHED INJECTION \n\033[0m");
if (options.dump_option == CompileOptions::INJECTION_DUMP)
{
// TODO: what do I dump here? injected crate names?
return;
}
// expansion pipeline stage
expansion (parsed_crate);
fprintf (stderr, "\033[0;31mSUCCESSFULLY FINISHED EXPANSION \n\033[0m");
if (options.dump_option == CompileOptions::EXPANSION_DUMP)
{
// TODO: what do I dump here? expanded macros? AST with expanded macros?
return;
}
// resolution pipeline stage
resolution (parsed_crate);
fprintf (stderr, "\033[0;31mSUCCESSFULLY FINISHED RESOLUTION \n\033[0m");
if (options.dump_option == CompileOptions::RESOLUTION_DUMP)
{
// TODO: what do I dump here? resolved names? AST with resolved names?
return;
}
if (saw_errors ())
return;
// do compile
Compile::Compilation::Compile (parsed_crate, backend);
}
// Checks whether 'cfg' attribute prevents compilation.
bool
check_cfg (const AST::Attribute &attr ATTRIBUTE_UNUSED)
{
// if "has sub items", and if 'cfg' attr, recursively call this on sub items?
// TODO: actually implement. assume true for now
return true;
}
// TODO: deprecated - don't use
// Checks whether any 'cfg' attribute on the item prevents compilation of that
// item.
bool
check_item_cfg (::std::vector<AST::Attribute> attrs)
{
for (const auto &attr : attrs)
{
if (attr.get_path () == "cfg" && !check_cfg (attr))
{
return false;
}
}
return true;
}
// TODO: deprecated - don't use
// TODO: actually implement method
void
load_extern_crate (::std::string crate_name ATTRIBUTE_UNUSED)
{}
// TODO: deprecated - don't use
// Parses up to the "load (external) crates" part of the frontend.
// TODO: lots of this code is probably actually useful outside of dumping, so
// maybe split off function
void
Session::debug_dump_load_crates (Parser &parser)
{
// parse crate as AST
AST::Crate crate = parser.parse_crate ();
/* TODO: search through inner attrs and see whether any of those attr paths
* contain "no_core", "no_std", "compiler_builtins". If so/not, save certain
* crate names. In these names, insert items at beginning of crate items. This
* is crate injection. Also, inject prelude use decl at beginning (first name
* is assumed to be prelude - prelude is a use decl automatically generated to
* enable using Option and Copy without qualifying it or importing it via
* 'use' manually) */
::std::vector< ::std::string> crate_names;
for (const auto &item : crate.items)
{
// if item is extern crate, add name? to list of stuff ONLY IF config is
// checked if item is module, iterate this loop inside it as well
// (recursive?) ONLY IF config is checked
// TODO: actually do the checks somewhere - probably in the items
item->add_crate_name (crate_names);
}
/* loop through list of crate names/paths/whatever, attempting to load each
* one. save loaded crates to a Session variable? Or save to current
* AST::Crate? */
for (const auto &name : crate_names)
{
load_extern_crate (name /*, basename = ""?*/);
}
// for each loaded crate, load dependencies of it as well
}
// TODO: deprecated - don't use
void
Session::register_plugins (AST::Crate &crate ATTRIBUTE_UNUSED)
{
fprintf (stderr, "ran register_plugins (with no body)\n");
}
// TODO: move somewhere else
bool
contains_name (const std::vector<AST::Attribute> &attrs, std::string name)
{
for (const auto &attr : attrs)
{
if (attr.get_path () == name)
{
return true;
}
}
return false;
}
void
Session::injection (AST::Crate &crate)
{
fprintf (stderr, "started injection\n");
// lint checks in future maybe?
// register builtin macros
/* In rustc, builtin macros are divided into 3 categories depending on use -
* "bang" macros, "attr" macros, and "derive" macros. I think the meanings of
* these categories should be fairly obvious to anyone who has used rust.
* Builtin macro list by category: Bang
* - asm
* - assert
* - cfg
* - column
* - compile_error
* - concat_idents
* - concat
* - env
* - file
* - format_args_nl
* - format_args
* - global_asm
* - include_bytes
* - include_str
* - include
* - line
* - log_syntax
* - module_path
* - option_env
* - stringify
* - trace_macros
* Attr
* - bench
* - global_allocator
* - test
* - test_case
* Derive
* - Clone
* - Copy
* - Debug
* - Default
* - Eq
* - Hash
* - Ord
* - PartialEq
* - PartialOrd
* - RustcDecodable
* - RustcEncodable
* rustc also has a "quote" macro that is defined differently and is
* supposedly not stable so eh. */
/* TODO: actually implement injection of these macros. In particular, derive
* macros, cfg, and
* test should be prioritised since they seem to be used the most. */
// crate injection
::std::vector< ::std::string> names;
if (contains_name (crate.inner_attrs, "no_core"))
{
// no prelude
injected_crate_name = "";
}
else if (contains_name (crate.inner_attrs, "no_std"))
{
names.push_back ("core");
if (!contains_name (crate.inner_attrs, "compiler_builtins"))
{
names.push_back ("compiler_builtins");
}
injected_crate_name = "core";
}
else
{
names.push_back ("std");
injected_crate_name = "std";
}
// reverse iterate through names to insert crate items in "forward" order at
// beginning of crate
for (auto it = names.rbegin (); it != names.rend (); ++it)
{
// create "macro use" attribute for use on extern crate item to enable
// loading macros from it
AST::Attribute attr (AST::SimplePath::from_str ("macro_use"), NULL);
// create "extern crate" item with the name
::std::unique_ptr<AST::ExternCrate> extern_crate (
new AST::ExternCrate (*it, AST::Visibility::create_error (),
{::std::move (attr)},
Linemap::unknown_location ()));
// insert at beginning
crate.items.insert (crate.items.begin (), ::std::move (extern_crate));
}
// create use tree path
// prelude is injected_crate_name
::std::vector<AST::SimplePathSegment> segments
= {AST::SimplePathSegment (injected_crate_name),
AST::SimplePathSegment ("prelude"), AST::SimplePathSegment ("v1")};
// create use tree and decl
::std::unique_ptr<AST::UseTreeGlob> use_tree (
new AST::UseTreeGlob (AST::UseTreeGlob::PATH_PREFIXED,
AST::SimplePath (::std::move (segments)),
Location ()));
AST::Attribute prelude_attr (AST::SimplePath::from_str ("prelude_import"),
NULL);
::std::unique_ptr<AST::UseDeclaration> use_decl (
new AST::UseDeclaration (::std::move (use_tree),
AST::Visibility::create_error (),
{::std::move (prelude_attr)}, Location ()));
crate.items.insert (crate.items.begin (), ::std::move (use_decl));
/* TODO: potentially add checking attribute crate type? I can't figure out
* what this does currently comment says "Unconditionally collect crate types
* from attributes to make them used", which presumably refers to checking the
* linkage info by "crate_type". It also seems to ensure that an invalid crate
* type is not specified, so maybe just do that. Valid crate types: bin lib
* dylib staticlib cdylib rlib proc-macro */
fprintf (stderr, "finished injection\n");
}
void
Session::expansion (AST::Crate &crate ATTRIBUTE_UNUSED)
{
fprintf (stderr, "started expansion\n");
// rustc has a modification to windows PATH temporarily here, which may end up
// being required
// create macro expansion config?
// if not, would at least have to configure recursion_limit
// create extctxt? from parse session, cfg, and resolver?
// expand by calling cxtctxt object's monotonic_expander's expand_crate
// method.
// error reporting - check unused macros, get missing fragment specifiers
// build test harness
// ast validation (also with proc macro decls)
// maybe create macro crate if not rustdoc
fprintf (stderr, "finished expansion\n");
}
void
Session::resolution (AST::Crate &crate)
{
fprintf (stderr, "started name resolution\n");
Analysis::TopLevelScan toplevel (crate);
// Name resolution must be in front of type resolution
Analysis::TypeResolution::ResolveNamesAndTypes (crate, toplevel);
fprintf (stderr, "finished name resolution\n");
}
void
TargetOptions::dump_target_options () const
{
fprintf (stderr,
"\033[0;31m--PREPARING TO DUMP ALL TARGET OPTIONS--\n\033[0m");
for (const auto &pairs : features)
{
for (const auto &value : pairs.second)
{
fprintf (stderr, "%s: \"%s\"\n", pairs.first.c_str (),
value.c_str ());
}
if (pairs.second.empty ())
{
fprintf (stderr, "%s\n", pairs.first.c_str ());
}
}
if (features.empty ())
{
fprintf (stderr, "No target options available!\n");
}
fprintf (stderr, "\033[0;31m--END OF TARGET OPTION DUMP--\n\033[0m");
}
void
TargetOptions::init_derived_values ()
{
// enable derived values based on target families
if (has_key_value_pair ("target_family", "unix"))
insert_key ("unix");
if (has_key_value_pair ("target_family", "windows"))
insert_key ("windows");
// implicitly enable features
if (has_key_value_pair ("target_feature", "aes"))
enable_implicit_feature_reqs ("aes");
if (has_key_value_pair ("target_feature", "avx"))
enable_implicit_feature_reqs ("sse4.2");
if (has_key_value_pair ("target_feature", "avx2"))
enable_implicit_feature_reqs ("avx");
if (has_key_value_pair ("target_feature", "pclmulqdq"))
enable_implicit_feature_reqs ("sse2");
if (has_key_value_pair ("target_feature", "sha"))
enable_implicit_feature_reqs ("sse2");
if (has_key_value_pair ("target_feature", "sse2"))
enable_implicit_feature_reqs ("sse");
if (has_key_value_pair ("target_feature", "sse3"))
enable_implicit_feature_reqs ("sse2");
if (has_key_value_pair ("target_feature", "sse4.1"))
enable_implicit_feature_reqs ("sse3");
if (has_key_value_pair ("target_feature", "sse4.2"))
enable_implicit_feature_reqs ("sse4.1");
if (has_key_value_pair ("target_feature", "ssse3"))
enable_implicit_feature_reqs ("sse3");
}
void
TargetOptions::enable_implicit_feature_reqs (std::string feature)
{
if (feature == "aes")
enable_implicit_feature_reqs ("sse2");
else if (feature == "avx")
enable_implicit_feature_reqs ("sse4.2");
else if (feature == "avx2")
enable_implicit_feature_reqs ("avx");
else if (feature == "fma")
enable_implicit_feature_reqs ("avx");
else if (feature == "pclmulqdq")
enable_implicit_feature_reqs ("sse2");
else if (feature == "sha")
enable_implicit_feature_reqs ("sse2");
else if (feature == "sse2")
enable_implicit_feature_reqs ("sse");
else if (feature == "sse3")
enable_implicit_feature_reqs ("sse2");
else if (feature == "sse4.1")
enable_implicit_feature_reqs ("sse3");
else if (feature == "sse4.2")
enable_implicit_feature_reqs ("sse4.1");
else if (feature == "ssse3")
enable_implicit_feature_reqs ("sse3");
if (!has_key_value_pair ("target_feature", feature))
insert_key_value_pair ("target_feature", feature);
}
// NOTEs:
/* mrustc compile pipeline:
* - target load (pass target spec to parser?)
* - parse (convert source to AST)
* - load crates (load any explicitly mentioned extern crates [not all of
* them])
* - expand (AST transformations from attributes and macros, loads remaining
* extern crates [std/core and any triggered by macro expansion])
* - implicit crates (test harness, allocator crate, panic crate)
* - resolve use (annotate every 'use' item with source [supposedly handles
* nasty recursion])
* - resolve index (generate index of visible items for every module [avoids
* recursion in next pass])
* - resolve absolute (resolve all paths into either variable names
* [types/values] or absolute paths)
* - HIR lower (convert modified AST to simpler HIR [both expressions and
* module tree])
* - resolve type aliases (replace any usages of type aliases with actual type
* [except associated types])
* - resolve bind (iterate HIR tree and set binding annotations on all concrete
* types [avoids path lookups later])
* - resolve HIR markings (generate "markings" [e.g. for Copy/Send/Sync/...]
* for all types
* - sort impls (small pass - sort impls into groups)
* - resolve UFCS outer (determine source trait for all top-level <T>::Type
* [qualified] paths)
* - resolve UFCS paths (do the same, but include for exprs this time. also
* normalises results of previous pass [expanding known associated types])
* - constant evaluate (evaluate all constants)
* - typecheck outer (checks impls are sane)
* - typecheck expressions (resolve and check types for all exprs)
* - expand HIR annotate (annotate how exprs are used - used for closure
* extractions and reborrows)
* - expand HIR closures (extract closures into structs implementing Fn*
* traits)
* - expand HIR vtables (generate vtables for types with dyn dispatch)
* - expand HIR calls (converts method and callable calls into explicit
* function calls)
* - expand HIR reborrows (apply reborrow rules [taking '&mut *v' instead of
* 'v'])
* - expand HIR erasedtype (replace all erased types 'impl Trait' with the true
* type)
* - typecheck expressions (validate - double check that previous passes
* haven't broke type system rules)
* - lower MIR (convert HIR exprs into a control-flow graph [MIR])
* - MIR validate (check that the generated MIR is consistent)
* - MIR cleanup (perform various transformations on MIR - replace reads of
* const items with the item itself; convert casts to unsized types into
* 'MakeDst' operations)
* - MIR optimise (perform various simple optimisations on the MIR - constant
* propagation, dead code elimination, borrow elimination, some inlining)
* - MIR validate PO (re-validate the MIR)
* - MIR validate full (optionally: perform expensive state-tracking validation
* on MIR)
* - trans enumerate (enumerate all items needed for code generation, primarily
* types used for generics)
* - trans auto impls (create magic trait impls as enumerated in previous pass)
* - trans monomorph (generate monomorphised copies of all functions [with
* generics replaced with real types])
* - MIR optimise inline (run optimisation again, this time with full type info
* [primarily for inlining])
* - HIR serialise (write out HIR dump [module tree and generic/inline MIR])
* - trans codegen (generate final output file: emit C source file and call C
* compiler) */
/* rustc compile pipeline (basic, in way less detail):
* - parse input (parse .rs to AST)
* - name resolution, macro expansion, and configuration (process AST
* recursively, resolving paths, expanding macros, processing #[cfg] nodes [i.e.
* maybe stripping stuff from AST])
* - lower to HIR
* - type check and other analyses (e.g. privacy checking)
* - lower to MIR and post-processing (and do stuff like borrow checking)
* - translation to LLVM IR and LLVM optimisations (produce the .o files)
* - linking (link together .o files) */
/* Pierced-together rustc compile pipeline (from source):
* - parse input (parse file to crate)
* - register plugins (attributes injection, set various options, register
* lints, load plugins)
* - expansion/configure and expand (initial 'cfg' processing, 'loading
* compiler plugins', syntax expansion, secondary 'cfg' expansion, synthesis of
* a test harness if required, injection of any std lib dependency and prelude,
* and name resolution) - actually documented inline
* - seeming pierced-together order: pre-AST expansion lint checks,
* registering builtin macros, crate injection, then expand all macros, then
* maybe build test harness, AST validation, maybe create a macro crate (if not
* rustdoc), name resolution, complete gated feature checking, add all buffered
* lints
* - create global context (lower to HIR)
* - analysis on global context (HIR optimisations? create MIR?)
* - code generation
* - link */
} // namespace Rust
|