Path: blob/main/contrib/llvm-project/llvm/tools/llvm-as/llvm-as.cpp
35231 views
//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This utility may be invoked in the following manner:9// llvm-as --help - Output information about command line switches10// llvm-as [options] - Read LLVM asm from stdin, write bitcode to stdout11// llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode12// to the x.bc file.13//14//===----------------------------------------------------------------------===//1516#include "llvm/AsmParser/Parser.h"17#include "llvm/Bitcode/BitcodeWriter.h"18#include "llvm/IR/LLVMContext.h"19#include "llvm/IR/Module.h"20#include "llvm/IR/ModuleSummaryIndex.h"21#include "llvm/IR/Verifier.h"22#include "llvm/Support/CommandLine.h"23#include "llvm/Support/FileSystem.h"24#include "llvm/Support/InitLLVM.h"25#include "llvm/Support/SourceMgr.h"26#include "llvm/Support/SystemUtils.h"27#include "llvm/Support/ToolOutputFile.h"28#include <memory>29#include <optional>30using namespace llvm;3132cl::OptionCategory AsCat("llvm-as Options");3334static cl::opt<std::string> InputFilename(cl::Positional,35cl::desc("<input .llvm file>"),36cl::init("-"));3738static cl::opt<std::string> OutputFilename("o",39cl::desc("Override output filename"),40cl::value_desc("filename"),41cl::cat(AsCat));4243static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"),44cl::cat(AsCat));4546static cl::opt<bool> DisableOutput("disable-output", cl::desc("Disable output"),47cl::init(false), cl::cat(AsCat));4849static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),50cl::init(false), cl::cat(AsCat));5152static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),53cl::Hidden, cl::cat(AsCat));5455static cl::opt<bool>56DisableVerify("disable-verify", cl::Hidden,57cl::desc("Do not run verifier on input LLVM (dangerous!)"),58cl::cat(AsCat));5960static cl::opt<bool> PreserveBitcodeUseListOrder(61"preserve-bc-uselistorder",62cl::desc("Preserve use-list order when writing LLVM bitcode."),63cl::init(true), cl::Hidden, cl::cat(AsCat));6465static cl::opt<std::string> ClDataLayout("data-layout",66cl::desc("data layout string to use"),67cl::value_desc("layout-string"),68cl::init(""), cl::cat(AsCat));69extern cl::opt<bool> UseNewDbgInfoFormat;70extern bool WriteNewDbgInfoFormatToBitcode;7172static void WriteOutputFile(const Module *M, const ModuleSummaryIndex *Index) {73// Infer the output filename if needed.74if (OutputFilename.empty()) {75if (InputFilename == "-") {76OutputFilename = "-";77} else {78StringRef IFN = InputFilename;79OutputFilename = (IFN.ends_with(".ll") ? IFN.drop_back(3) : IFN).str();80OutputFilename += ".bc";81}82}8384std::error_code EC;85std::unique_ptr<ToolOutputFile> Out(86new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));87if (EC) {88errs() << EC.message() << '\n';89exit(1);90}9192if (Force || !CheckBitcodeOutputToConsole(Out->os())) {93const ModuleSummaryIndex *IndexToWrite = nullptr;94// Don't attempt to write a summary index unless it contains any entries or95// has non-zero flags. The latter is used to assemble dummy index files for96// skipping modules by distributed ThinLTO backends. Otherwise we get an empty97// summary section.98if (Index && (Index->begin() != Index->end() || Index->getFlags()))99IndexToWrite = Index;100if (!IndexToWrite || (M && (!M->empty() || !M->global_empty())))101// If we have a non-empty Module, then we write the Module plus102// any non-null Index along with it as a per-module Index.103// If both are empty, this will give an empty module block, which is104// the expected behavior.105WriteBitcodeToFile(*M, Out->os(), PreserveBitcodeUseListOrder,106IndexToWrite, EmitModuleHash);107else108// Otherwise, with an empty Module but non-empty Index, we write a109// combined index.110writeIndexToFile(*IndexToWrite, Out->os());111}112113// Declare success.114Out->keep();115}116117int main(int argc, char **argv) {118InitLLVM X(argc, argv);119cl::HideUnrelatedOptions(AsCat);120cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");121LLVMContext Context;122123// Parse the file now...124SMDiagnostic Err;125auto SetDataLayout = [](StringRef, StringRef) -> std::optional<std::string> {126if (ClDataLayout.empty())127return std::nullopt;128return ClDataLayout;129};130ParsedModuleAndIndex ModuleAndIndex;131if (DisableVerify) {132ModuleAndIndex = parseAssemblyFileWithIndexNoUpgradeDebugInfo(133InputFilename, Err, Context, nullptr, SetDataLayout);134} else {135ModuleAndIndex = parseAssemblyFileWithIndex(InputFilename, Err, Context,136nullptr, SetDataLayout);137}138std::unique_ptr<Module> M = std::move(ModuleAndIndex.Mod);139if (!M) {140Err.print(argv[0], errs());141return 1;142}143144// Convert to new debug format if requested.145M->setIsNewDbgInfoFormat(UseNewDbgInfoFormat &&146WriteNewDbgInfoFormatToBitcode);147if (M->IsNewDbgInfoFormat)148M->removeDebugIntrinsicDeclarations();149150std::unique_ptr<ModuleSummaryIndex> Index = std::move(ModuleAndIndex.Index);151152if (!DisableVerify) {153std::string ErrorStr;154raw_string_ostream OS(ErrorStr);155if (verifyModule(*M, &OS)) {156errs() << argv[0]157<< ": assembly parsed, but does not verify as correct!\n";158errs() << OS.str();159return 1;160}161// TODO: Implement and call summary index verifier.162}163164if (DumpAsm) {165errs() << "Here's the assembly:\n" << *M;166if (Index.get() && Index->begin() != Index->end())167Index->print(errs());168}169170if (!DisableOutput)171WriteOutputFile(M.get(), Index.get());172173return 0;174}175176177