1
- // ===--- SILLLVMGen .cpp ------- --------------------------------------------===//
1
+ // ===--- sil_llvm_gen_main .cpp --------------------------------------------===//
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
48
48
#include < cstdio>
49
49
using namespace swift ;
50
50
51
- static llvm::cl::opt<std::string> InputFilename (llvm::cl::desc(" input file" ),
52
- llvm::cl::init(" -" ),
53
- llvm::cl::Positional);
54
-
55
- static llvm::cl::opt<std::string>
56
- OutputFilename (" o" , llvm::cl::init(" -" ), llvm::cl::desc(" output filename" ));
57
-
58
- static llvm::cl::list<std::string>
59
- ImportPaths (" I" ,
60
- llvm::cl::desc (" add a directory to the import search path" ));
61
-
62
- static llvm::cl::list<std::string> FrameworkPaths (
63
- " F" , llvm::cl::desc(" add a directory to the framework search path" ));
64
-
65
- static llvm::cl::opt<std::string>
66
- ModuleName (" module-name" ,
67
- llvm::cl::desc (" The name of the module if processing"
68
- " a module. Necessary for processing "
69
- " stdin." ));
70
-
71
- static llvm::cl::opt<std::string> ResourceDir (
72
- " resource-dir" ,
73
- llvm::cl::desc (" The directory that holds the compiler resource files" ));
74
-
75
- static llvm::cl::opt<std::string>
76
- SDKPath (" sdk" , llvm::cl::desc(" The path to the SDK for use with the clang "
77
- " importer." ),
78
- llvm::cl::init(" " ));
79
-
80
- static llvm::cl::opt<std::string> Target (" target" ,
81
- llvm::cl::desc (" target triple" ));
82
-
83
- static llvm::cl::opt<bool >
84
- PrintStats (" print-stats" , llvm::cl::desc(" Print various statistics" ));
85
-
86
- static llvm::cl::opt<std::string>
87
- ModuleCachePath (" module-cache-path" ,
88
- llvm::cl::desc (" Clang module cache path" ));
89
-
90
- static llvm::cl::opt<bool >
91
- PerformWMO (" wmo" , llvm::cl::desc(" Enable whole-module optimizations" ));
92
-
93
- static llvm::cl::opt<IRGenOutputKind> OutputKind (
94
- " output-kind" , llvm::cl::desc(" Type of output to produce" ),
95
- llvm::cl::values(clEnumValN(IRGenOutputKind::LLVMAssemblyAfterOptimization,
96
- " llvm-as" , " Emit llvm assembly" ),
97
- clEnumValN(IRGenOutputKind::LLVMBitcode, " llvm-bc" ,
98
- " Emit llvm bitcode" ),
99
- clEnumValN(IRGenOutputKind::NativeAssembly, " as" ,
100
- " Emit native assembly" ),
101
- clEnumValN(IRGenOutputKind::ObjectFile, " object" ,
102
- " Emit an object file" )),
103
- llvm::cl::init(IRGenOutputKind::ObjectFile));
104
-
105
- static llvm::cl::opt<bool >
106
- DisableLegacyTypeInfo (" disable-legacy-type-info" ,
107
- llvm::cl::desc (" Don't try to load backward deployment layouts" ));
108
-
109
- // This function isn't referenced outside its translation unit, but it
110
- // can't use the "static" keyword because its address is used for
111
- // getMainExecutable (since some platforms don't support taking the
112
- // address of main, and some platforms can't implement getMainExecutable
113
- // without being given the address of a function in the main executable).
114
- void anchorForGetMainExecutable () {}
115
-
116
- int main (int argc, char **argv) {
117
- PROGRAM_START (argc, argv);
51
+ struct SILLLVMGenOptions {
52
+ llvm::cl::opt<std::string>
53
+ InputFilename = llvm::cl::opt<std::string>(llvm::cl::desc(" input file" ),
54
+ llvm::cl::init (" -" ),
55
+ llvm::cl::Positional);
56
+
57
+ llvm::cl::opt<std::string>
58
+ OutputFilename = llvm::cl::opt<std::string>(" o" , llvm::cl::init(" -" ), llvm::cl::desc(" output filename" ));
59
+
60
+ llvm::cl::list<std::string>
61
+ ImportPaths = llvm::cl::list<std::string>(" I" ,
62
+ llvm::cl::desc (" add a directory to the import search path" ));
63
+
64
+ llvm::cl::list<std::string>
65
+ FrameworkPaths = llvm::cl::list<std::string>(
66
+ " F" , llvm::cl::desc(" add a directory to the framework search path" ));
67
+
68
+ llvm::cl::opt<std::string>
69
+ ModuleName = llvm::cl::opt<std::string>(" module-name" ,
70
+ llvm::cl::desc (" The name of the module if processing"
71
+ " a module. Necessary for processing "
72
+ " stdin." ));
73
+
74
+ llvm::cl::opt<std::string>
75
+ ResourceDir = llvm::cl::opt<std::string>(
76
+ " resource-dir" ,
77
+ llvm::cl::desc (" The directory that holds the compiler resource files" ));
78
+
79
+ llvm::cl::opt<std::string>
80
+ SDKPath = llvm::cl::opt<std::string>(" sdk" , llvm::cl::desc(" The path to the SDK for use with the clang "
81
+ " importer." ),
82
+ llvm::cl::init (" " ));
83
+
84
+ llvm::cl::opt<std::string>
85
+ Target = llvm::cl::opt<std::string>(" target" ,
86
+ llvm::cl::desc (" target triple" ));
87
+
88
+ llvm::cl::opt<bool >
89
+ PrintStats = llvm::cl::opt<bool >(" print-stats" , llvm::cl::desc(" Print various statistics" ));
90
+
91
+ llvm::cl::opt<std::string>
92
+ ModuleCachePath = llvm::cl::opt<std::string>(" module-cache-path" ,
93
+ llvm::cl::desc (" Clang module cache path" ));
94
+
95
+ llvm::cl::opt<bool >
96
+ PerformWMO = llvm::cl::opt<bool >(" wmo" , llvm::cl::desc(" Enable whole-module optimizations" ));
97
+
98
+ llvm::cl::opt<IRGenOutputKind>
99
+ OutputKind = llvm::cl::opt<IRGenOutputKind>(
100
+ " output-kind" , llvm::cl::desc(" Type of output to produce" ),
101
+ llvm::cl::values (clEnumValN(IRGenOutputKind::LLVMAssemblyAfterOptimization,
102
+ " llvm-as" , " Emit llvm assembly" ),
103
+ clEnumValN(IRGenOutputKind::LLVMBitcode, " llvm-bc" ,
104
+ " Emit llvm bitcode" ),
105
+ clEnumValN(IRGenOutputKind::NativeAssembly, " as" ,
106
+ " Emit native assembly" ),
107
+ clEnumValN(IRGenOutputKind::ObjectFile, " object" ,
108
+ " Emit an object file" )),
109
+ llvm::cl::init(IRGenOutputKind::ObjectFile));
110
+
111
+ llvm::cl::opt<bool >
112
+ DisableLegacyTypeInfo = llvm::cl::opt<bool >(" disable-legacy-type-info" ,
113
+ llvm::cl::desc (" Don't try to load backward deployment layouts" ));
114
+ };
115
+
116
+ int sil_llvm_gen_main (ArrayRef<const char *> argv, void *MainAddr) {
118
117
INITIALIZE_LLVM ();
119
118
120
- llvm::cl::ParseCommandLineOptions (argc, argv, " Swift LLVM IR Generator \n " ) ;
119
+ SILLLVMGenOptions options ;
121
120
122
- if (PrintStats)
121
+ llvm::cl::ParseCommandLineOptions (argv.size (), argv.data (), " Swift LLVM IR Generator\n " );
122
+
123
+ if (options.PrintStats )
123
124
llvm::EnableStatistics ();
124
125
125
126
CompilerInvocation Invocation;
126
127
127
- Invocation.setMainExecutablePath (llvm::sys::fs::getMainExecutable (
128
- argv[0 ], reinterpret_cast <void *>(&anchorForGetMainExecutable)));
128
+ Invocation.setMainExecutablePath (llvm::sys::fs::getMainExecutable (argv[0 ], MainAddr));
129
129
130
130
// Give the context the list of search paths to use for modules.
131
- Invocation.setImportSearchPaths (ImportPaths);
131
+ Invocation.setImportSearchPaths (options. ImportPaths );
132
132
std::vector<SearchPathOptions::FrameworkSearchPath> FramePaths;
133
- for (const auto &path : FrameworkPaths) {
133
+ for (const auto &path : options. FrameworkPaths ) {
134
134
FramePaths.push_back ({path, /* isSystem=*/ false });
135
135
}
136
136
Invocation.setFrameworkSearchPaths (FramePaths);
137
137
// Set the SDK path and target if given.
138
- if (SDKPath.getNumOccurrences () == 0 ) {
138
+ if (options. SDKPath .getNumOccurrences () == 0 ) {
139
139
const char *SDKROOT = getenv (" SDKROOT" );
140
140
if (SDKROOT)
141
- SDKPath = SDKROOT;
141
+ options. SDKPath = SDKROOT;
142
142
}
143
- if (!SDKPath.empty ())
144
- Invocation.setSDKPath (SDKPath);
145
- if (!Target.empty ())
146
- Invocation.setTargetTriple (Target);
147
- if (!ResourceDir.empty ())
148
- Invocation.setRuntimeResourcePath (ResourceDir);
143
+ if (!options. SDKPath .empty ())
144
+ Invocation.setSDKPath (options. SDKPath );
145
+ if (!options. Target .empty ())
146
+ Invocation.setTargetTriple (options. Target );
147
+ if (!options. ResourceDir .empty ())
148
+ Invocation.setRuntimeResourcePath (options. ResourceDir );
149
149
// Set the module cache path. If not passed in we use the default swift module
150
150
// cache.
151
- Invocation.getClangImporterOptions ().ModuleCachePath = ModuleCachePath;
151
+ Invocation.getClangImporterOptions ().ModuleCachePath = options. ModuleCachePath ;
152
152
Invocation.setParseStdlib ();
153
153
154
154
// Setup the language options
@@ -160,16 +160,16 @@ int main(int argc, char **argv) {
160
160
161
161
// Setup the IRGen Options.
162
162
IRGenOptions &Opts = Invocation.getIRGenOptions ();
163
- Opts.OutputKind = OutputKind;
164
- Opts.DisableLegacyTypeInfo = DisableLegacyTypeInfo;
163
+ Opts.OutputKind = options. OutputKind ;
164
+ Opts.DisableLegacyTypeInfo = options. DisableLegacyTypeInfo ;
165
165
166
166
serialization::ExtendedValidationInfo extendedInfo;
167
167
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
168
- Invocation.setUpInputForSILTool (InputFilename, ModuleName,
168
+ Invocation.setUpInputForSILTool (options. InputFilename , options. ModuleName ,
169
169
/* alwaysSetModuleToMain*/ false ,
170
- /* bePrimary*/ !PerformWMO, extendedInfo);
170
+ /* bePrimary*/ !options. PerformWMO , extendedInfo);
171
171
if (!FileBufOrErr) {
172
- fprintf (stderr, " Error! Failed to open file: %s\n " , InputFilename.c_str ());
172
+ fprintf (stderr, " Error! Failed to open file: %s\n " , options. InputFilename .c_str ());
173
173
exit (-1 );
174
174
}
175
175
@@ -184,16 +184,16 @@ int main(int argc, char **argv) {
184
184
}
185
185
186
186
llvm::vfs::OnDiskOutputBackend Backend;
187
- auto outFile = Backend.createFile (OutputFilename);
187
+ auto outFile = Backend.createFile (options. OutputFilename );
188
188
if (!outFile) {
189
189
CI.getDiags ().diagnose (SourceLoc (), diag::error_opening_output,
190
- OutputFilename, toString (outFile.takeError ()));
190
+ options. OutputFilename , toString (outFile.takeError ()));
191
191
return 1 ;
192
192
}
193
193
auto closeFile = llvm::make_scope_exit ([&]() {
194
194
if (auto E = outFile->keep ()) {
195
195
CI.getDiags ().diagnose (SourceLoc (), diag::error_closing_output,
196
- OutputFilename, toString (std::move (E)));
196
+ options. OutputFilename , toString (std::move (E)));
197
197
}
198
198
});
199
199
@@ -204,10 +204,10 @@ int main(int argc, char **argv) {
204
204
const auto &SILOpts = Invocation.getSILOptions ();
205
205
auto &SILTypes = CI.getSILTypes ();
206
206
auto moduleName = CI.getMainModule ()->getName ().str ();
207
- const PrimarySpecificPaths PSPs (OutputFilename, InputFilename);
207
+ const PrimarySpecificPaths PSPs (options. OutputFilename , options. InputFilename );
208
208
209
209
auto getDescriptor = [&]() -> IRGenDescriptor {
210
- if (PerformWMO) {
210
+ if (options. PerformWMO ) {
211
211
return IRGenDescriptor::forWholeModule (
212
212
mod, Opts, TBDOpts, SILOpts, SILTypes,
213
213
/* SILMod*/ nullptr , moduleName, PSPs);
0 commit comments