//===--- Bitcode/NaCl/Writer/NaClBitcodeWriter.cpp - Bitcode Writer -------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Bitcode writer implementation.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "NaClBitcodeWriter"
#include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h"
#include "llvm/Bitcode/NaCl/NaClReaderWriter.h"
#include "NaClValueEnumerator.h"
#include "llvm/Bitcode/NaCl/NaClBitstreamWriter.h"
#include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include <cctype>
#include <map>
using namespace llvm;
static cl::opt<unsigned>
PNaClVersion("pnacl-version",
cl::desc("Specify PNaCl bitcode version to write"),
cl::init(2));
/// These are manifest constants used by the bitcode writer. They do
/// not need to be kept in sync with the reader, but need to be
/// consistent within this file.
///
/// Note that for each block type GROUP, the last entry should be of
/// the form:
///
/// GROUP_MAX_ABBREV = GROUP_LAST_ABBREV,
///
/// where GROUP_LAST_ABBREV is the last defined abbreviation. See
/// include file "llvm/Bitcode/NaCl/NaClBitCodes.h" for more
/// information on how groups should be defined.
enum {
// VALUE_SYMTAB_BLOCK abbrev id's.
VST_ENTRY_8_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV,
VST_ENTRY_7_ABBREV,
VST_ENTRY_6_ABBREV,
VST_BBENTRY_6_ABBREV,
VST_MAX_ABBREV = VST_BBENTRY_6_ABBREV,
// CONSTANTS_BLOCK abbrev id's.
CONSTANTS_SETTYPE_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV,
CONSTANTS_INTEGER_ABBREV,
CONSTANTS_INTEGER_ZERO_ABBREV,
CONSTANTS_FLOAT_ABBREV,
CONSTANTS_MAX_ABBREV = CONSTANTS_FLOAT_ABBREV,
// GLOBALVAR BLOCK abbrev id's.
GLOBALVAR_VAR_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV,
GLOBALVAR_COMPOUND_ABBREV,
GLOBALVAR_ZEROFILL_ABBREV,
GLOBALVAR_DATA_ABBREV,
GLOBALVAR_RELOC_ABBREV,
GLOBALVAR_RELOC_WITH_ADDEND_ABBREV,
GLOBALVAR_MAX_ABBREV = GLOBALVAR_RELOC_WITH_ADDEND_ABBREV,
// FUNCTION_BLOCK abbrev id's.
FUNCTION_INST_LOAD_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV,
FUNCTION_INST_BINOP_ABBREV,
FUNCTION_INST_BINOP_FLAGS_ABBREV,
FUNCTION_INST_CAST_ABBREV,
FUNCTION_INST_RET_VOID_ABBREV,
FUNCTION_INST_RET_VAL_ABBREV,
FUNCTION_INST_UNREACHABLE_ABBREV,
FUNCTION_INST_FORWARDTYPEREF_ABBREV,
FUNCTION_INST_STORE_ABBREV,
FUNCTION_INST_MAX_ABBREV = FUNCTION_INST_STORE_ABBREV,
// TYPE_BLOCK_ID_NEW abbrev id's.
TYPE_POINTER_ABBREV = naclbitc::FIRST_APPLICATION_ABBREV,
TYPE_FUNCTION_ABBREV,
TYPE_ARRAY_ABBREV,
TYPE_MAX_ABBREV = TYPE_ARRAY_ABBREV
};
LLVM_ATTRIBUTE_NORETURN
static void ReportIllegalValue(const char *ValueMessage,
const Value &Value) {
std::string Message;
raw_string_ostream StrM(Message);
StrM << "Illegal ";
if (ValueMessage != 0)
StrM << ValueMessage << " ";
StrM << ": " << Value;
report_fatal_error(StrM.str());
}
static unsigned GetEncodedCastOpcode(unsigned Opcode, const Value &V) {
switch (Opcode) {
default: ReportIllegalValue("cast", V);
case Instruction::Trunc : return naclbitc::CAST_TRUNC;
case Instruction::ZExt