1 //===-- cc1as_main.cpp - Clang Assembler ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the entry point to the clang -cc1as functionality, which implements
11 // the direct interface to the LLVM MC based assembler.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Driver/Arg.h"
17 #include "clang/Driver/ArgList.h"
18 #include "clang/Driver/DriverDiagnostic.h"
19 #include "clang/Driver/CC1AsOptions.h"
20 #include "clang/Driver/OptTable.h"
21 #include "clang/Driver/Options.h"
22 #include "clang/Frontend/DiagnosticOptions.h"
23 #include "clang/Frontend/FrontendDiagnostic.h"
24 #include "clang/Frontend/TextDiagnosticPrinter.h"
25 #include "llvm/ADT/OwningPtr.h"
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/ADT/Triple.h"
28 #include "llvm/MC/MCParser/MCAsmParser.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCCodeEmitter.h"
31 #include "llvm/MC/MCContext.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/FormattedStream.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/ManagedStatic.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/PrettyStackTrace.h"
39 #include "llvm/Support/SourceMgr.h"
40 #include "llvm/Support/Timer.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/System/Host.h"
43 #include "llvm/System/Path.h"
44 #include "llvm/System/Signals.h"
45 #include "llvm/Target/TargetAsmBackend.h"
46 #include "llvm/Target/TargetAsmParser.h"
47 #include "llvm/Target/TargetData.h"
48 #include "llvm/Target/TargetMachine.h"
49 #include "llvm/Target/TargetRegistry.h"
50 #include "llvm/Target/TargetSelect.h"
51 using namespace clang;
52 using namespace clang::driver;
57 /// \brief Helper class for representing a single invocation of the assembler.
58 struct AssemblerInvocation {
59 /// @name Target Options
65 /// @name Language Options
68 std::vector<std::string> IncludePaths;
69 unsigned NoInitialTextSection : 1;
72 /// @name Frontend Options
75 std::string InputFile;
76 std::vector<std::string> LLVMArgs;
77 std::string OutputPath;
79 FT_Asm, ///< Assembly (.s) output, transliterate mode.
80 FT_Null, ///< No output, for timing purposes.
81 FT_Obj ///< Object file output.
84 unsigned ShowHelp : 1;
85 unsigned ShowVersion : 1;
88 /// @name Transliterate Options
91 unsigned OutputAsmVariant;
92 unsigned ShowEncoding : 1;
93 unsigned ShowInst : 1;
96 /// @name Assembler Options
99 unsigned RelaxAll : 1;
104 AssemblerInvocation() {
106 NoInitialTextSection = 0;
110 OutputAsmVariant = 0;
116 static void CreateFromArgs(AssemblerInvocation &Res, const char **ArgBegin,
117 const char **ArgEnd, Diagnostic &Diags);
122 void AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
123 const char **ArgBegin,
126 using namespace clang::driver::cc1asoptions;
127 // Parse the arguments.
128 OwningPtr<OptTable> OptTbl(createCC1AsOptTable());
129 unsigned MissingArgIndex, MissingArgCount;
130 OwningPtr<InputArgList> Args(
131 OptTbl->ParseArgs(ArgBegin, ArgEnd,MissingArgIndex, MissingArgCount));
133 // Check for missing argument error.
135 Diags.Report(diag::err_drv_missing_argument)
136 << Args->getArgString(MissingArgIndex) << MissingArgCount;
138 // Issue errors on unknown arguments.
139 for (arg_iterator it = Args->filtered_begin(cc1asoptions::OPT_UNKNOWN),
140 ie = Args->filtered_end(); it != ie; ++it)
141 Diags.Report(diag::err_drv_unknown_argument) << (*it) ->getAsString(*Args);
143 // Construct the invocation.
146 Opts.Triple = Triple::normalize(Args->getLastArgValue(OPT_triple));
147 if (Opts.Triple.empty()) // Use the host triple if unspecified.
148 Opts.Triple = sys::getHostTriple();
151 Opts.IncludePaths = Args->getAllArgValues(OPT_I);
152 Opts.NoInitialTextSection = Args->hasArg(OPT_n);
155 if (Args->hasArg(OPT_INPUT)) {
157 for (arg_iterator it = Args->filtered_begin(OPT_INPUT),
158 ie = Args->filtered_end(); it != ie; ++it, First=false) {
161 Opts.InputFile = A->getValue(*Args);
163 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(*Args);
166 Opts.LLVMArgs = Args->getAllArgValues(OPT_mllvm);
167 Opts.OutputPath = Args->getLastArgValue(OPT_o);
168 if (Arg *A = Args->getLastArg(OPT_filetype)) {
169 StringRef Name = A->getValue(*Args);
170 unsigned OutputType = StringSwitch<unsigned>(Name)
172 .Case("null", FT_Null)
175 if (OutputType == ~0U)
176 Diags.Report(diag::err_drv_invalid_value)
177 << A->getAsString(*Args) << Name;
179 Opts.OutputType = FileType(OutputType);
181 Opts.ShowHelp = Args->hasArg(OPT_help);
182 Opts.ShowVersion = Args->hasArg(OPT_version);
184 // Transliterate Options
185 Opts.OutputAsmVariant = Args->getLastArgIntValue(OPT_output_asm_variant,
187 Opts.ShowEncoding = Args->hasArg(OPT_show_encoding);
188 Opts.ShowInst = Args->hasArg(OPT_show_inst);
191 Opts.RelaxAll = Args->hasArg(OPT_relax_all);
194 static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
197 if (Opts.OutputPath.empty())
198 Opts.OutputPath = "-";
200 // Make sure that the Out file gets unlinked from the disk if we get a
202 if (Opts.OutputPath != "-")
203 sys::RemoveFileOnSignal(sys::Path(Opts.OutputPath));
206 raw_fd_ostream *Out =
207 new raw_fd_ostream(Opts.OutputPath.c_str(), Error,
208 (Binary ? raw_fd_ostream::F_Binary : 0));
209 if (!Error.empty()) {
210 Diags.Report(diag::err_fe_unable_to_open_output)
211 << Opts.OutputPath << Error;
215 return new formatted_raw_ostream(*Out, formatted_raw_ostream::DELETE_STREAM);
218 static bool ExecuteAssembler(AssemblerInvocation &Opts, Diagnostic &Diags) {
219 // Get the target specific parser.
221 const Target *TheTarget(TargetRegistry::lookupTarget(Opts.Triple, Error));
223 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
227 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, &Error);
229 Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
235 // Tell SrcMgr about this buffer, which is what the parser will pick up.
236 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
238 // Record the location of the include directories so that the lexer can find
240 SrcMgr.setIncludeDirs(Opts.IncludePaths);
242 OwningPtr<MCAsmInfo> MAI(TheTarget->createAsmInfo(Opts.Triple));
243 assert(MAI && "Unable to create target asm info!");
246 bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
247 formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
251 // FIXME: We shouldn't need to do this (and link in codegen).
252 OwningPtr<TargetMachine> TM(TheTarget->createTargetMachine(Opts.Triple, ""));
254 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
258 OwningPtr<MCStreamer> Str;
260 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
262 TheTarget->createMCInstPrinter(Opts.OutputAsmVariant, *MAI);
263 MCCodeEmitter *CE = 0;
264 if (Opts.ShowEncoding)
265 CE = TheTarget->createCodeEmitter(*TM, Ctx);
266 Str.reset(createAsmStreamer(Ctx, *Out,TM->getTargetData()->isLittleEndian(),
267 /*asmverbose*/true, IP, CE, Opts.ShowInst));
268 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
269 Str.reset(createNullStreamer(Ctx));
271 assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
272 "Invalid file type!");
273 MCCodeEmitter *CE = TheTarget->createCodeEmitter(*TM, Ctx);
274 TargetAsmBackend *TAB = TheTarget->createAsmBackend(Opts.Triple);
275 Str.reset(TheTarget->createObjectStreamer(Opts.Triple, Ctx, *TAB, *Out,
279 OwningPtr<MCAsmParser> Parser(createMCAsmParser(*TheTarget, SrcMgr, Ctx,
281 OwningPtr<TargetAsmParser> TAP(TheTarget->createAsmParser(*Parser, *TM));
283 Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
287 Parser->setTargetParser(*TAP.get());
289 bool Success = !Parser->Run(Opts.NoInitialTextSection);
294 // Delete output on errors.
295 if (!Success && Opts.OutputPath != "-")
296 sys::Path(Opts.OutputPath).eraseFromDisk();
301 static void LLVMErrorHandler(void *UserData, const std::string &Message) {
302 Diagnostic &Diags = *static_cast<Diagnostic*>(UserData);
304 Diags.Report(diag::err_fe_error_backend) << Message;
306 // We cannot recover from llvm errors.
310 int cc1as_main(const char **ArgBegin, const char **ArgEnd,
311 const char *Argv0, void *MainAddr) {
312 // Print a stack trace if we signal out.
313 sys::PrintStackTraceOnErrorSignal();
314 PrettyStackTraceProgram X(ArgEnd - ArgBegin, ArgBegin);
315 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
317 // Initialize targets and assembly printers/parsers.
318 InitializeAllTargetInfos();
319 // FIXME: We shouldn't need to initialize the Target(Machine)s.
320 InitializeAllTargets();
321 InitializeAllAsmPrinters();
322 InitializeAllAsmParsers();
324 // Construct our diagnostic client.
325 TextDiagnosticPrinter *DiagClient
326 = new TextDiagnosticPrinter(errs(), DiagnosticOptions());
327 DiagClient->setPrefix("clang -cc1as");
328 Diagnostic Diags(DiagClient);
330 // Set an error handler, so that any LLVM backend diagnostics go through our
332 ScopedFatalErrorHandler FatalErrorHandler
333 (LLVMErrorHandler, static_cast<void*>(&Diags));
335 // Parse the arguments.
336 AssemblerInvocation Asm;
337 AssemblerInvocation::CreateFromArgs(Asm, ArgBegin, ArgEnd, Diags);
341 llvm::OwningPtr<driver::OptTable> Opts(driver::createCC1AsOptTable());
342 Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler");
348 // FIXME: Use a better -version message?
349 if (Asm.ShowVersion) {
350 llvm::cl::PrintVersionMessage();
356 // FIXME: Remove this, one day.
357 if (!Asm.LLVMArgs.empty()) {
358 unsigned NumArgs = Asm.LLVMArgs.size();
359 const char **Args = new const char*[NumArgs + 2];
360 Args[0] = "clang (LLVM option parsing)";
361 for (unsigned i = 0; i != NumArgs; ++i)
362 Args[i + 1] = Asm.LLVMArgs[i].c_str();
363 Args[NumArgs + 1] = 0;
364 llvm::cl::ParseCommandLineOptions(NumArgs + 1, const_cast<char **>(Args));
367 // Execute the invocation, unless there were parsing errors.
368 bool Success = false;
369 if (!Diags.getNumErrors())
370 Success = ExecuteAssembler(Asm, Diags);
372 // If any timers were active but haven't been destroyed yet, print their
374 TimerGroup::printAll(errs());