aboutsummaryrefslogtreecommitdiff
path: root/lib/Bytecode
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-05-06 19:33:40 +0000
committerChris Lattner <sabre@nondot.org>2007-05-06 19:33:40 +0000
commitb11f1a9ee167d278923e741cd11ccd0bfe58f816 (patch)
treeecabffc72b7ac17c1eb731d6ad1b37b464f54b17 /lib/Bytecode
parent5f32c01dead5623d874f442b34762f9d111be4cf (diff)
remove the old bc writer
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36881 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Bytecode')
-rw-r--r--lib/Bytecode/Writer/Makefile13
-rw-r--r--lib/Bytecode/Writer/SlotCalculator.cpp390
-rw-r--r--lib/Bytecode/Writer/SlotCalculator.h138
-rw-r--r--lib/Bytecode/Writer/Writer.cpp1266
-rw-r--r--lib/Bytecode/Writer/WriterInternals.h138
5 files changed, 0 insertions, 1945 deletions
diff --git a/lib/Bytecode/Writer/Makefile b/lib/Bytecode/Writer/Makefile
deleted file mode 100644
index e731bb14a9..0000000000
--- a/lib/Bytecode/Writer/Makefile
+++ /dev/null
@@ -1,13 +0,0 @@
-##===- lib/Bytecode/Writer/Makefile ------------------------*- Makefile -*-===##
-#
-# The LLVM Compiler Infrastructure
-#
-# This file was developed by the LLVM research group and is distributed under
-# the University of Illinois Open Source License. See LICENSE.TXT for details.
-#
-##===----------------------------------------------------------------------===##
-LEVEL = ../../..
-LIBRARYNAME = LLVMBCWriter
-BUILD_ARCHIVE = 1
-
-include $(LEVEL)/Makefile.common
diff --git a/lib/Bytecode/Writer/SlotCalculator.cpp b/lib/Bytecode/Writer/SlotCalculator.cpp
deleted file mode 100644
index 3a038cd449..0000000000
--- a/lib/Bytecode/Writer/SlotCalculator.cpp
+++ /dev/null
@@ -1,390 +0,0 @@
-//===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements a useful analysis step to figure out what numbered slots
-// values in a program will land in (keeping track of per plane information).
-//
-// This is used when writing a file to disk, either in bytecode or assembly.
-//
-//===----------------------------------------------------------------------===//
-
-#include "SlotCalculator.h"
-#include "llvm/Constants.h"
-#include "llvm/DerivedTypes.h"
-#include "llvm/Function.h"
-#include "llvm/InlineAsm.h"
-#include "llvm/Instructions.h"
-#include "llvm/Module.h"
-#include "llvm/TypeSymbolTable.h"
-#include "llvm/Type.h"
-#include "llvm/ValueSymbolTable.h"
-#include "llvm/ADT/STLExtras.h"
-#include <algorithm>
-#include <functional>
-using namespace llvm;
-
-#ifndef NDEBUG
-#include "llvm/Support/Streams.h"
-#include "llvm/Support/CommandLine.h"
-static cl::opt<bool> SlotCalculatorDebugOption("scdebug",cl::init(false),
- cl::desc("Enable SlotCalculator debug output"), cl::Hidden);
-#define SC_DEBUG(X) if (SlotCalculatorDebugOption) cerr << X
-#else
-#define SC_DEBUG(X)
-#endif
-
-void SlotCalculator::insertPrimitives() {
- // Preload the table with the built-in types. These built-in types are
- // inserted first to ensure that they have low integer indices which helps to
- // keep bytecode sizes small. Note that the first group of indices must match
- // the Type::TypeIDs for the primitive types. After that the integer types are
- // added, but the order and value is not critical. What is critical is that
- // the indices of these "well known" slot numbers be properly maintained in
- // Reader.h which uses them directly to extract values of these types.
- SC_DEBUG("Inserting primitive types:\n");
- // See WellKnownTypeSlots in Reader.h
- getOrCreateTypeSlot(Type::VoidTy ); // 0: VoidTySlot
- getOrCreateTypeSlot(Type::FloatTy ); // 1: FloatTySlot
- getOrCreateTypeSlot(Type::DoubleTy); // 2: DoubleTySlot
- getOrCreateTypeSlot(Type::LabelTy ); // 3: LabelTySlot
- assert(TypeMap.size() == Type::FirstDerivedTyID &&"Invalid primitive insert");
- // Above here *must* correspond 1:1 with the primitive types.
- getOrCreateTypeSlot(Type::Int1Ty ); // 4: Int1TySlot
- getOrCreateTypeSlot(Type::Int8Ty ); // 5: Int8TySlot
- getOrCreateTypeSlot(Type::Int16Ty ); // 6: Int16TySlot
- getOrCreateTypeSlot(Type::Int32Ty ); // 7: Int32TySlot
- getOrCreateTypeSlot(Type::Int64Ty ); // 8: Int64TySlot
-}
-
-SlotCalculator::SlotCalculator(const Module *M) {
- assert(M);
- TheModule = M;
-
- insertPrimitives();
- processModule();
-}
-
-// processModule - Process all of the module level function declarations and
-// types that are available.
-//
-void SlotCalculator::processModule() {
- SC_DEBUG("begin processModule!\n");
-
- // Add all of the global variables to the value table...
- //
- for (Module::const_global_iterator I = TheModule->global_begin(),
- E = TheModule->global_end(); I != E; ++I)
- CreateSlotIfNeeded(I);
-
- // Scavenge the types out of the functions, then add the functions themselves
- // to the value table...
- //
- for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
- I != E; ++I)
- CreateSlotIfNeeded(I);
-
- // Add all of the global aliases to the value table...
- //
- for (Module::const_alias_iterator I = TheModule->alias_begin(),
- E = TheModule->alias_end(); I != E; ++I)
- CreateSlotIfNeeded(I);
-
- // Add all of the module level constants used as initializers
- //
- for (Module::const_global_iterator I = TheModule->global_begin(),
- E = TheModule->global_end(); I != E; ++I)
- if (I->hasInitializer())
- CreateSlotIfNeeded(I->getInitializer());
-
- // Add all of the module level constants used as aliasees
- //
- for (Module::const_alias_iterator I = TheModule->alias_begin(),
- E = TheModule->alias_end(); I != E; ++I)
- if (I->getAliasee())
- CreateSlotIfNeeded(I->getAliasee());
-
- // Now that all global constants have been added, rearrange constant planes
- // that contain constant strings so that the strings occur at the start of the
- // plane, not somewhere in the middle.
- //
- for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) {
- if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane]))
- if (AT->getElementType() == Type::Int8Ty) {
- TypePlane &Plane = Table[plane];
- unsigned FirstNonStringID = 0;
- for (unsigned i = 0, e = Plane.size(); i != e; ++i)
- if (isa<ConstantAggregateZero>(Plane[i]) ||
- (isa<ConstantArray>(Plane[i]) &&
- cast<ConstantArray>(Plane[i])->isString())) {
- // Check to see if we have to shuffle this string around. If not,
- // don't do anything.
- if (i != FirstNonStringID) {
- // Swap the plane entries....
- std::swap(Plane[i], Plane[FirstNonStringID]);
-
- // Keep the NodeMap up to date.
- NodeMap[Plane[i]] = i;
- NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
- }
- ++FirstNonStringID;
- }
- }
- }
-
- // Scan all of the functions for their constants, which allows us to emit
- // more compact modules.
- SC_DEBUG("Inserting function constants:\n");
- for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
- F != E; ++F) {
- for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
- for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
- for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
- OI != E; ++OI) {
- if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
- isa<InlineAsm>(*OI))
- CreateSlotIfNeeded(*OI);
- }
- getOrCreateTypeSlot(I->getType());
- }
- }
-
- // Insert constants that are named at module level into the slot pool so that
- // the module symbol table can refer to them...
- SC_DEBUG("Inserting SymbolTable values:\n");
- processTypeSymbolTable(&TheModule->getTypeSymbolTable());
- processValueSymbolTable(&TheModule->getValueSymbolTable());
-
- // Now that we have collected together all of the information relevant to the
- // module, compactify the type table if it is particularly big and outputting
- // a bytecode file. The basic problem we run into is that some programs have
- // a large number of types, which causes the type field to overflow its size,
- // which causes instructions to explode in size (particularly call
- // instructions). To avoid this behavior, we "sort" the type table so that
- // all non-value types are pushed to the end of the type table, giving nice
- // low numbers to the types that can be used by instructions, thus reducing
- // the amount of explodage we suffer.
- if (Types.size() >= 64) {
- unsigned FirstNonValueTypeID = 0;
- for (unsigned i = 0, e = Types.size(); i != e; ++i)
- if (Types[i]->isFirstClassType() || Types[i]->isPrimitiveType()) {
- // Check to see if we have to shuffle this type around. If not, don't
- // do anything.
- if (i != FirstNonValueTypeID) {
- // Swap the type ID's.
- std::swap(Types[i], Types[FirstNonValueTypeID]);
-
- // Keep the TypeMap up to date.
- TypeMap[Types[i]] = i;
- TypeMap[Types[FirstNonValueTypeID]] = FirstNonValueTypeID;
-
- // When we move a type, make sure to move its value plane as needed.
- if (Table.size() > FirstNonValueTypeID) {
- if (Table.size() <= i) Table.resize(i+1);
- std::swap(Table[i], Table[FirstNonValueTypeID]);
- }
- }
- ++FirstNonValueTypeID;
- }
- }
-
- NumModuleTypes = getNumPlanes();
-
- SC_DEBUG("end processModule!\n");
-}
-
-// processTypeSymbolTable - Insert all of the type sin the specified symbol
-// table.
-void SlotCalculator::processTypeSymbolTable(const TypeSymbolTable *TST) {
- for (TypeSymbolTable::const_iterator TI = TST->begin(), TE = TST->end();
- TI != TE; ++TI )
- getOrCreateTypeSlot(TI->second);
-}
-
-// processSymbolTable - Insert all of the values in the specified symbol table
-// into the values table...
-//
-void SlotCalculator::processValueSymbolTable(const ValueSymbolTable *VST) {
- for (ValueSymbolTable::const_iterator VI = VST->begin(), VE = VST->end();
- VI != VE; ++VI)
- CreateSlotIfNeeded(VI->getValue());
-}
-
-void SlotCalculator::CreateSlotIfNeeded(const Value *V) {
- // Check to see if it's already in!
- if (NodeMap.count(V)) return;
-
- const Type *Ty = V->getType();
- assert(Ty != Type::VoidTy && "Can't insert void values!");
-
- if (const Constant *C = dyn_cast<Constant>(V)) {
- if (isa<GlobalValue>(C)) {
- // Initializers for globals are handled explicitly elsewhere.
- } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
- // Do not index the characters that make up constant strings. We emit
- // constant strings as special entities that don't require their
- // individual characters to be emitted.
- if (!C->isNullValue())
- ConstantStrings.push_back(cast<ConstantArray>(C));
- } else {
- // This makes sure that if a constant has uses (for example an array of
- // const ints), that they are inserted also.
- for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
- I != E; ++I)
- CreateSlotIfNeeded(*I);
- }
- }
-
- unsigned TyPlane = getOrCreateTypeSlot(Ty);
- if (Table.size() <= TyPlane) // Make sure we have the type plane allocated.
- Table.resize(TyPlane+1, TypePlane());
-
- // If this is the first value to get inserted into the type plane, make sure
- // to insert the implicit null value.
- if (Table[TyPlane].empty()) {
- // Label's and opaque types can't have a null value.
- if (Ty != Type::LabelTy && !isa<OpaqueType>(Ty)) {
- Value *ZeroInitializer = Constant::getNullValue(Ty);
-
- // If we are pushing zeroinit, it will be handled below.
- if (V != ZeroInitializer) {
- Table[TyPlane].push_back(ZeroInitializer);
- NodeMap[ZeroInitializer] = 0;
- }
- }
- }
-
- // Insert node into table and NodeMap...
- NodeMap[V] = Table[TyPlane].size();
- Table[TyPlane].push_back(V);
-
- SC_DEBUG(" Inserting value [" << TyPlane << "] = " << *V << " slot=" <<
- NodeMap[V] << "\n");
-}
-
-
-unsigned SlotCalculator::getOrCreateTypeSlot(const Type *Ty) {
- TypeMapType::iterator TyIt = TypeMap.find(Ty);
- if (TyIt != TypeMap.end()) return TyIt->second;
-
- // Insert into TypeMap.
- unsigned ResultSlot = TypeMap[Ty] = Types.size();
- Types.push_back(Ty);
- SC_DEBUG(" Inserting type [" << ResultSlot << "] = " << *Ty << "\n" );
-
- // Loop over any contained types in the definition, ensuring they are also
- // inserted.
- for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
- I != E; ++I)
- getOrCreateTypeSlot(*I);
-
- return ResultSlot;
-}
-
-
-
-void SlotCalculator::incorporateFunction(const Function *F) {
- SC_DEBUG("begin processFunction!\n");
-
- // Iterate over function arguments, adding them to the value table...
- for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
- I != E; ++I)
- CreateFunctionValueSlot(I);
-
- SC_DEBUG("Inserting Instructions:\n");
-
- // Add all of the instructions to the type planes...
- for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
- CreateFunctionValueSlot(BB);
- for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
- if (I->getType() != Type::VoidTy)
- CreateFunctionValueSlot(I);
- }
- }
-
- SC_DEBUG("end processFunction!\n");
-}
-
-void SlotCalculator::purgeFunction() {
- SC_DEBUG("begin purgeFunction!\n");
-
- // Next, remove values from existing type planes
- for (DenseMap<unsigned,unsigned,
- ModuleLevelDenseMapKeyInfo>::iterator I = ModuleLevel.begin(),
- E = ModuleLevel.end(); I != E; ++I) {
- unsigned PlaneNo = I->first;
- unsigned ModuleLev = I->second;
-
- // Pop all function-local values in this type-plane off of Table.
- TypePlane &Plane = getPlane(PlaneNo);
- assert(ModuleLev < Plane.size() && "module levels higher than elements?");
- for (unsigned i = ModuleLev, e = Plane.size(); i != e; ++i) {
- NodeMap.erase(Plane.back()); // Erase from nodemap
- Plane.pop_back(); // Shrink plane
- }
- }
-
- ModuleLevel.clear();
-
- // Finally, remove any type planes defined by the function...
- while (Table.size() > NumModuleTypes) {
- TypePlane &Plane = Table.back();
- SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
- << Plane.size() << "\n");
- for (unsigned i = 0, e = Plane.size(); i != e; ++i)
- NodeMap.erase(Plane[i]); // Erase from nodemap
-
- Table.pop_back(); // Nuke the plane, we don't like it.
- }
-
- SC_DEBUG("end purgeFunction!\n");
-}
-
-inline static bool hasImplicitNull(const Type* Ty) {
- return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
-}
-
-void SlotCalculator::CreateFunctionValueSlot(const Value *V) {
- assert(!NodeMap.count(V) && "Function-local value can't be inserted!");
-
- const Type *Ty = V->getType();
- assert(Ty != Type::VoidTy && "Can't insert void values!");
- assert(!isa<Constant>(V) && "Not a function-local value!");
-
- unsigned TyPlane = getOrCreateTypeSlot(Ty);
- if (Table.size() <= TyPlane) // Make sure we have the type plane allocated.
- Table.resize(TyPlane+1, TypePlane());
-
- // If this is the first value noticed of this type within this function,
- // remember the module level for this type plane in ModuleLevel. This reminds
- // us to remove the values in purgeFunction and tells us how many to remove.
- if (TyPlane < NumModuleTypes)
- ModuleLevel.insert(std::make_pair(TyPlane, Table[TyPlane].size()));
-
- // If this is the first value to get inserted into the type plane, make sure
- // to insert the implicit null value.
- if (Table[TyPlane].empty()) {
- // Label's and opaque types can't have a null value.
- if (hasImplicitNull(Ty)) {
- Value *ZeroInitializer = Constant::getNullValue(Ty);
-
- // If we are pushing zeroinit, it will be handled below.
- if (V != ZeroInitializer) {
- Table[TyPlane].push_back(ZeroInitializer);
- NodeMap[ZeroInitializer] = 0;
- }
- }
- }
-
- // Insert node into table and NodeMap...
- NodeMap[V] = Table[TyPlane].size();
- Table[TyPlane].push_back(V);
-
- SC_DEBUG(" Inserting value [" << TyPlane << "] = " << *V << " slot=" <<
- NodeMap[V] << "\n");
-}
diff --git a/lib/Bytecode/Writer/SlotCalculator.h b/lib/Bytecode/Writer/SlotCalculator.h
deleted file mode 100644
index 343800cf6c..0000000000
--- a/lib/Bytecode/Writer/SlotCalculator.h
+++ /dev/null
@@ -1,138 +0,0 @@
-//===-- Analysis/SlotCalculator.h - Calculate value slots -------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This class calculates the slots that values will land in. This is useful for
-// when writing bytecode or assembly out, because you have to know these things.
-//
-// Specifically, this class calculates the "type plane numbering" that you see
-// for a function if you strip out all of the symbols in it. For assembly
-// writing, this is used when a symbol does not have a name. For bytecode
-// writing, this is always used, and the symbol table is added on later.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_ANALYSIS_SLOTCALCULATOR_H
-#define LLVM_ANALYSIS_SLOTCALCULATOR_H
-
-#include "llvm/ADT/DenseMap.h"
-#include "llvm/ADT/SmallVector.h"
-#include <vector>
-
-namespace llvm {
-
-class Value;
-class Type;
-class Module;
-class Function;
-class SymbolTable;
-class TypeSymbolTable;
-class ValueSymbolTable;
-class ConstantArray;
-
-struct ModuleLevelDenseMapKeyInfo {
- static inline unsigned getEmptyKey() { return ~0U; }
- static inline unsigned getTombstoneKey() { return ~1U; }
- static unsigned getHashValue(unsigned Val) { return Val ^ Val >> 4; }
- static bool isPod() { return true; }
-};
-
-
-class SlotCalculator {
- const Module *TheModule;
-public:
- typedef std::vector<const Type*> TypeList;
- typedef SmallVector<const Value*, 16> TypePlane;
-private:
- std::vector<TypePlane> Table;
- TypeList Types;
- typedef DenseMap<const Value*, unsigned> NodeMapType;
- NodeMapType NodeMap;
-
- typedef DenseMap<const Type*, unsigned> TypeMapType;
- TypeMapType TypeMap;
-
- /// ConstantStrings - If we are indexing for a bytecode file, this keeps track
- /// of all of the constants strings that need to be emitted.
- std::vector<const ConstantArray*> ConstantStrings;
-
- /// ModuleLevel - Used to keep track of which values belong to the module,
- /// and which values belong to the currently incorporated function.
- ///
- DenseMap<unsigned,unsigned,ModuleLevelDenseMapKeyInfo> ModuleLevel;
- unsigned NumModuleTypes;
-
- SlotCalculator(const SlotCalculator &); // DO NOT IMPLEMENT
- void operator=(const SlotCalculator &); // DO NOT IMPLEMENT
-public:
- SlotCalculator(const Module *M);
-
- /// getSlot - Return the slot number of the specified value in it's type
- /// plane.
- ///
- unsigned getSlot(const Value *V) const {
- NodeMapType::const_iterator I = NodeMap.find(V);
- assert(I != NodeMap.end() && "Value not in slotcalculator!");
- return I->second;
- }
-
- unsigned getTypeSlot(const Type* T) const {
- TypeMapType::const_iterator I = TypeMap.find(T);
- assert(I != TypeMap.end() && "Type not in slotcalc!");
- return I->second;
- }
-
- inline unsigned getNumPlanes() const { return Table.size(); }
- inline unsigned getNumTypes() const { return Types.size(); }
-
- TypePlane &getPlane(unsigned Plane) {
- // Okay we are just returning an entry out of the main Table. Make sure the
- // plane exists and return it.
- if (Plane >= Table.size())
- Table.resize(Plane+1);
- return Table[Plane];
- }
-
- TypeList& getTypes() { return Types; }
-
- /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
- /// use these two methods to get its data into the SlotCalculator!
- ///
- void incorporateFunction(const Function *F);
- void purgeFunction();
-
- /// string_iterator/string_begin/end - Access the list of module-level
- /// constant strings that have been incorporated. This is only applicable to
- /// bytecode files.
- typedef std::vector<const ConstantArray*>::const_iterator string_iterator;
- string_iterator string_begin() const { return ConstantStrings.begin(); }
- string_iterator string_end() const { return ConstantStrings.end(); }
-
-private:
- void CreateSlotIfNeeded(const Value *V);
- void CreateFunctionValueSlot(const Value *V);
- unsigned getOrCreateTypeSlot(const Type *T);
-
- // processModule - Process all of the module level function declarations and
- // types that are available.
- //
- void processModule();
-
- // processSymbolTable - Insert all of the values in the specified symbol table
- // into the values table...
- //
- void processTypeSymbolTable(const TypeSymbolTable *ST);
- void processValueSymbolTable(const ValueSymbolTable *ST);
-
- // insertPrimitives - helper for constructors to insert primitive types.
- void insertPrimitives();
-};
-
-} // End llvm namespace
-
-#endif
diff --git a/lib/Bytecode/Writer/Writer.cpp b/lib/Bytecode/Writer/Writer.cpp
deleted file mode 100644
index ea5159b28f..0000000000
--- a/lib/Bytecode/Writer/Writer.cpp
+++ /dev/null
@@ -1,1266 +0,0 @@
-//===-- Writer.cpp - Library for writing LLVM bytecode files --------------===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This library implements the functionality defined in llvm/Bytecode/Writer.h
-//
-// Note that this file uses an unusual technique of outputting all the bytecode
-// to a vector of unsigned char, then copies the vector to an ostream. The
-// reason for this is that we must do "seeking" in the stream to do back-
-// patching, and some very important ostreams that we want to support (like
-// pipes) do not support seeking. :( :( :(
-//
-//===----------------------------------------------------------------------===//
-
-#define DEBUG_TYPE "bcwriter"
-#include "WriterInternals.h"
-#include "llvm/Bytecode/WriteBytecodePass.h"
-#include "llvm/CallingConv.h"
-#include "llvm/Constants.h"
-#include "llvm/DerivedTypes.h"
-#include "llvm/ParameterAttributes.h"
-#include "llvm/InlineAsm.h"
-#include "llvm/Instructions.h"
-#include "llvm/Module.h"
-#include "llvm/TypeSymbolTable.h"
-#include "llvm/ValueSymbolTable.h"
-#include "llvm/Support/GetElementPtrTypeIterator.h"
-#include "llvm/Support/Compressor.h"
-#include "llvm/Support/MathExtras.h"
-#include "llvm/Support/Streams.h"
-#include "llvm/System/Program.h"
-#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/STLExtras.h"
-#include "llvm/ADT/Statistic.h"
-#include <cstring>
-#include <algorithm>
-using namespace llvm;
-
-/// This value needs to be incremented every time the bytecode format changes
-/// so that the reader can distinguish which format of the bytecode file has
-/// been written.
-/// @brief The bytecode version number
-const unsigned BCVersionNum = 7;
-
-char WriteBytecodePass::ID = 0;
-static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
-
-STATISTIC(BytesWritten, "Number of bytecode bytes written");
-
-//===----------------------------------------------------------------------===//
-//=== Output Primitives ===//
-//===----------------------------------------------------------------------===//
-
-// output - If a position is specified, it must be in the valid portion of the
-// string... note that this should be inlined always so only the relevant IF
-// body should be included.
-inline void BytecodeWriter::output(unsigned i, int pos) {
- if (pos == -1) { // Be endian clean, little endian is our friend
- Out.push_back((unsigned char)i);
- Out.push_back((unsigned char)(i >> 8));
- Out.push_back((unsigned char)(i >> 16));
- Out.push_back((unsigned char)(i >> 24));
- } else {
- Out[pos ] = (unsigned char)i;
- Out[pos+1] = (unsigned char)(i >> 8);
- Out[pos+2] = (unsigned char)(i >> 16);
- Out[pos+3] = (unsigned char)(i >> 24);
- }
-}
-
-inline void BytecodeWriter::output(int32_t i) {
- output((uint32_t)i);
-}
-
-/// output_vbr - Output an unsigned value, by using the least number of bytes
-/// possible. This is useful because many of our "infinite" values are really
-/// very small most of the time; but can be large a few times.
-/// Data format used: If you read a byte with the high bit set, use the low
-/// seven bits as data and then read another byte.
-inline void BytecodeWriter::output_vbr(uint64_t i) {
- while (1) {
- if (i < 0x80) { // done?
- Out.push_back((unsigned char)i); // We know the high bit is clear...
- return;
- }
-
- // Nope, we are bigger than a character, output the next 7 bits and set the
- // high bit to say that there is more coming...
- Out.push_back(0x80 | ((unsigned char)i & 0x7F));
- i >>= 7; // Shift out 7 bits now...
- }
-}
-
-inline void BytecodeWriter::output_vbr(uint32_t i) {
- while (1) {
- if (i < 0x80) { // done?
- Out.push_back((unsigned char)i); // We know the high bit is clear...
- return;
- }
-
- // Nope, we are bigger than a character, output the next 7 bits and set the
- // high bit to say that there is more coming...
- Out.push_back(0x80 | ((unsigned char)i & 0x7F));
- i >>= 7; // Shift out 7 bits now...
- }
-}
-
-inline void BytecodeWriter::output_typeid(unsigned i) {
- if (i <= 0x00FFFFFF)
- this->output_vbr(i);
- else {
- this->output_vbr(0x00FFFFFF);
- this->output_vbr(i);
- }
-}
-
-inline void BytecodeWriter::output_vbr(int64_t i) {
- if (i < 0)
- output_vbr(((uint64_t)(-i) << 1) | 1); // Set low order sign bit...
- else
- output_vbr((uint64_t)i << 1); // Low order bit is clear.
-}
-
-
-inline void BytecodeWriter::output_vbr(int i) {
- if (i < 0)
- output_vbr(((unsigned)(-i) << 1) | 1); // Set low order sign bit...
- else
- output_vbr((unsigned)i << 1); // Low order bit is clear.
-}
-
-inline void BytecodeWriter::output_str(const char *Str, unsigned Len) {
- output_vbr(Len); // Strings may have an arbitrary length.
- Out.insert(Out.end(), Str, Str+Len);
-}
-
-inline void BytecodeWriter::output_data(const void *Ptr, const void *End) {
- Out.insert(Out.end(), (const unsigned char*)Ptr, (const unsigned char*)End);
-}
-
-inline void BytecodeWriter::output_float(float& FloatVal) {
- /// FIXME: This isn't optimal, it has size problems on some platforms
- /// where FP is not IEEE.
- uint32_t i = FloatToBits(FloatVal);
- Out.push_back( static_cast<unsigned char>( (i ) & 0xFF));
- Out.push_back( static_cast<unsigned char>( (i >> 8 ) & 0xFF));
- Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF));
- Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF));
-}
-
-inline void BytecodeWriter::output_double(double& DoubleVal) {
- /// FIXME: This isn't optimal, it has size problems on some platforms
- /// where FP is not IEEE.
- uint64_t i = DoubleToBits(DoubleVal);
- Out.push_back( static_cast<unsigned char>( (i ) & 0xFF));
- Out.push_back( static_cast<unsigned char>( (i >> 8 ) & 0xFF));
- Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF));
- Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF));
- Out.push_back( static_cast<unsigned char>( (i >> 32) & 0xFF));
- Out.push_back( static_cast<unsigned char>( (i >> 40) & 0xFF));
- Out.push_back( static_cast<unsigned char>( (i >> 48) & 0xFF));
- Out.push_back( static_cast<unsigned char>( (i >> 56) & 0xFF));
-}
-
-inline BytecodeBlock::BytecodeBlock(unsigned ID, BytecodeWriter &w,
- bool elideIfEmpty, bool hasLongFormat)
- : Id(ID), Writer(w), ElideIfEmpty(elideIfEmpty), HasLongFormat(hasLongFormat){
-
- if (HasLongFormat) {
- w.output(ID);
- w.output(0U); // For length in long format
- } else {
- w.output(0U); /// Place holder for ID and length for this block
- }
- Loc = w.size();
-}
-
-inline BytecodeBlock::~BytecodeBlock() { // Do backpatch when block goes out
- // of scope...
- if (Loc == Writer.size() && ElideIfEmpty) {
- // If the block is empty, and we are allowed to, do not emit the block at
- // all!
- Writer.resize(Writer.size()-(HasLongFormat?8:4));
- return;
- }
-
- if (HasLongFormat)
- Writer.output(unsigned(Writer.size()-Loc), int(Loc-4));
- else
- Writer.output(unsigned(Writer.size()-Loc) << 5 | (Id & 0x1F), int(Loc-4));
-}
-
-//===----------------------------------------------------------------------===//
-//=== Constant Output ===//
-//===----------------------------------------------------------------------===//
-
-void BytecodeWriter::outputParamAttrsList(const ParamAttrsList *Attrs) {
- if (!Attrs) {
- output_vbr(unsigned(0));
- return;
- }
- unsigned numAttrs = Attrs->size();
- output_vbr(numAttrs);
- for (unsigned i = 0; i < numAttrs; ++i) {
- uint16_t index = Attrs->getParamIndex(i);
- uint16_t attrs = Attrs->getParamAttrs(index);
- output_vbr(uint32_t(index));
- output_vbr(uint32_t(attrs));
- }
-}
-
-void BytecodeWriter::outputType(const Type *T) {
- const StructType* STy = dyn_cast<StructType>(T);
- if(STy && STy->isPacked())
- output_vbr((unsigned)Type::PackedStructTyID);
- else
- output_vbr((unsigned)T->getTypeID());
-
- // That's all there is to handling primitive types...
- if (T->isPrimitiveType())
- return; // We might do this if we alias a prim type: %x = type int
-
- switch (T->getTypeID()) { // Handle derived types now.
- case Type::IntegerTyID:
- output_vbr(cast<IntegerType>(T)->getBitWidth());
- break;
- case Type::FunctionTyID: {
- const FunctionType *FT = cast<FunctionType>(T);
- output_typeid(Table.getTypeSlot(FT->getReturnType()));
-
- // Output the number of arguments to function (+1 if varargs):
- output_vbr((unsigned)FT->getNumParams()+FT->isVarArg());
-
- // Output all of the arguments...
- FunctionType::param_iterator I = FT->param_begin();
- for (; I != FT->param_end(); ++I)
- output_typeid(Table.getTypeSlot(*I));
-
- // Terminate list with VoidTy if we are a varargs function...
- if (FT->isVarArg())
- output_typeid((unsigned)Type::VoidTyID);
-
- // Put out all the parameter attributes
- outputParamAttrsList(FT->getParamAttrs());
- break;
- }
-
- case Type::ArrayTyID: {
- const ArrayType *AT = cast<ArrayType>(T);
- output_typeid(Table.getTypeSlot(AT->getElementType()));
- output_vbr(AT->getNumElements());
- break;
- }
-
- case Type::VectorTyID: {
- const VectorType *PT = cast<VectorType>(T);
- output_typeid(Table.getTypeSlot(PT->getElementType()));
- output_vbr(PT->getNumElements());
- break;
- }
-
- case Type::StructTyID: {
- const StructType *ST = cast<StructType>(T);
- // Output all of the element types...
- for (StructType::element_iterator I = ST->element_begin(),
- E = ST->element_end(); I != E; ++I) {
- output_typeid(Table.getTypeSlot(*I));
- }
-
- // Terminate list with VoidTy
- output_typeid((unsigned)Type::VoidTyID);
- break;
- }
-
- case Type::PointerTyID:
- output_typeid(Table.getTypeSlot(cast<PointerType>(T)->getElementType()));
- break;
-
- case Type::OpaqueTyID:
- // No need to emit anything, just the count of opaque types is enough.
- break;
-
- default:
- cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
- << " Type '" << T->getDescription() << "'\n";
- break;
- }
-}
-
-void BytecodeWriter::outputConstant(const Constant *CPV) {
- assert(((CPV->getType()->isPrimitiveType() || CPV->getType()->isInteger()) ||
- !CPV->isNullValue()) && "Shouldn't output null constants!");
-
- // We must check for a ConstantExpr before switching by type because
- // a ConstantExpr can be of any type, and has no explicit value.
- //
- if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
- // FIXME: Encoding of constant exprs could be much more compact!
- assert(CE->getNumOperands() > 0 && "ConstantExpr with 0 operands");
- assert(CE->getNumOperands() != 1 || CE->isCast());
- output_vbr(1+CE->getNumOperands()); // flags as an expr
- output_vbr(CE->getOpcode()); // Put out the CE op code
-
- for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
- output_vbr(Table.getSlot(*OI));
- output_typeid(Table.getTypeSlot((*OI)->getType()));
- }
- if (CE->isCompare())
- output_vbr((unsigned)CE->getPredicate());
- return;
- } else if (isa<UndefValue>(CPV)) {
- output_vbr(1U); // 1 -> UndefValue constant.
- return;
- } else {
- output_vbr(0U); // flag as not a ConstantExpr (i.e. 0 operands)
- }
-
- switch (CPV->getType()->getTypeID()) {
- case Type::IntegerTyID: { // Integer types...
- const ConstantInt *CI = cast<ConstantInt>(CPV);
- unsigned NumBits = cast<IntegerType>(CPV->getType())->getBitWidth();
- if (NumBits <= 32)
- output_vbr(uint32_t(CI->getZExtValue()));
- else if (NumBits <= 64)
- output_vbr(uint64_t(CI->getZExtValue()));
- else {
- // We have an arbitrary precision integer value to write whose
- // bit width is > 64. However, in canonical unsigned integer
- // format it is likely that the high bits are going to be zero.
- // So, we only write the number of active words.
- uint32_t activeWords = CI->getValue().getActiveWords();
- const uint64_t *rawData = CI->getValue().getRawData();
- output_vbr(activeWords);
- for (uint32_t i = 0; i < activeWords; ++i)
- output_vbr(rawData[i]);
- }
- break;
- }
-
- case Type::ArrayTyID: {
- const Cons