Home | History | Annotate | Download | only in slang
      1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This utility may be invoked in the following manner:
     11 //   llvm-as --help         - Output information about command line switches
     12 //   llvm-as [options]      - Read LLVM asm from stdin, write bitcode to stdout
     13 //   llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode
     14 //                            to the x.bc file.
     15 //
     16 //===----------------------------------------------------------------------===//
     17 
     18 #include "llvm/IR/LLVMContext.h"
     19 #include "llvm/IR/Verifier.h"
     20 #include "llvm/AsmParser/Parser.h"
     21 #include "llvm/Bitcode/ReaderWriter.h"
     22 #include "llvm/IR/Module.h"
     23 #include "llvm/Support/CommandLine.h"
     24 #include "llvm/Support/FileSystem.h"
     25 #include "llvm/Support/ManagedStatic.h"
     26 #include "llvm/Support/PrettyStackTrace.h"
     27 #include "llvm/Support/Signals.h"
     28 #include "llvm/Support/SourceMgr.h"
     29 #include "llvm/Support/SystemUtils.h"
     30 #include "llvm/Support/ToolOutputFile.h"
     31 
     32 #include "BitWriter_3_2/ReaderWriter_3_2.h"
     33 #include "BitWriter_2_9/ReaderWriter_2_9.h"
     34 #include "BitWriter_2_9_func/ReaderWriter_2_9_func.h"
     35 
     36 #include <memory>
     37 using namespace llvm;
     38 
     39 static cl::opt<std::string>
     40 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
     41 
     42 static cl::opt<std::string>
     43 OutputFilename("o", cl::desc("Override output filename"),
     44                cl::value_desc("filename"));
     45 
     46 static cl::opt<bool>
     47 Force("f", cl::desc("Enable binary output on terminals"));
     48 
     49 static cl::opt<bool>
     50 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
     51 
     52 static cl::opt<bool>
     53 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
     54 
     55 static cl::opt<bool>
     56 DisableVerify("disable-verify", cl::Hidden,
     57               cl::desc("Do not run verifier on input LLVM (dangerous!)"));
     58 
     59 enum BCVersion {
     60   BC29, BC29Func, BC32, BCHEAD
     61 };
     62 
     63 cl::opt<BCVersion> BitcodeVersion("bitcode-version",
     64   cl::desc("Set the bitcode version to be written:"),
     65   cl::values(
     66     clEnumValN(BC29, "BC29", "Version 2.9"),
     67      clEnumVal(BC29Func,     "Version 2.9 func"),
     68      clEnumVal(BC32,         "Version 3.2"),
     69      clEnumVal(BCHEAD,       "Most current version"),
     70     clEnumValEnd), cl::init(BC32));
     71 
     72 static void WriteOutputFile(const Module *M) {
     73   // Infer the output filename if needed.
     74   if (OutputFilename.empty()) {
     75     if (InputFilename == "-") {
     76       OutputFilename = "-";
     77     } else {
     78       std::string IFN = InputFilename;
     79       int Len = IFN.length();
     80       if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
     81         // Source ends in .ll
     82         OutputFilename = std::string(IFN.begin(), IFN.end()-3);
     83       } else {
     84         OutputFilename = IFN;   // Append a .bc to it
     85       }
     86       OutputFilename += ".bc";
     87     }
     88   }
     89 
     90   std::string ErrorInfo;
     91   std::unique_ptr<tool_output_file> Out
     92   (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
     93                         llvm::sys::fs::F_None));
     94   if (!ErrorInfo.empty()) {
     95     errs() << ErrorInfo << '\n';
     96     exit(1);
     97   }
     98 
     99   if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) {
    100     switch(BitcodeVersion) {
    101       case BC29:
    102         llvm_2_9::WriteBitcodeToFile(M, Out->os());
    103         break;
    104       case BC29Func:
    105         llvm_2_9_func::WriteBitcodeToFile(M, Out->os());
    106         break;
    107       case BC32:
    108         llvm_3_2::WriteBitcodeToFile(M, Out->os());
    109         break;
    110       case BCHEAD:
    111         llvm::WriteBitcodeToFile(M, Out->os());
    112         break;
    113     }
    114   }
    115 
    116   // Declare success.
    117   Out->keep();
    118 }
    119 
    120 int main(int argc, char **argv) {
    121   // Print a stack trace if we signal out.
    122   sys::PrintStackTraceOnErrorSignal();
    123   PrettyStackTraceProgram X(argc, argv);
    124   LLVMContext &Context = getGlobalContext();
    125   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
    126   cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
    127 
    128   // Parse the file now...
    129   SMDiagnostic Err;
    130   std::unique_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
    131   if (M.get() == 0) {
    132     Err.print(argv[0], errs());
    133     return 1;
    134   }
    135 
    136   if (!DisableVerify) {
    137     std::string Err;
    138     raw_string_ostream stream(Err);
    139     if (verifyModule(*M.get(), &stream)) {
    140       errs() << argv[0]
    141              << ": assembly parsed, but does not verify as correct!\n";
    142       errs() << Err;
    143       return 1;
    144     }
    145   }
    146 
    147   if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
    148 
    149   if (!DisableOutput)
    150     WriteOutputFile(M.get());
    151 
    152   return 0;
    153 }
    154