blob: 050cad402a7c657a07cd5d1f31f6cfc5dc509b77 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- WriterInternals.h - Data structures shared by the Writer -*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This header defines the interface used between components of the bytecode
11// writer.
12//
13// Note that the performance of this library is not terribly important, because
14// it shouldn't be used by JIT type applications... so it is not a huge focus
15// at least. :)
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
20#define LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
21
Reid Spencerd1fb1b72004-07-04 11:44:27 +000022#include "SlotCalculator.h"
23#include "llvm/Bytecode/Writer.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include "llvm/Bytecode/Format.h"
Chris Lattner00950542001-06-06 20:29:01 +000025#include "llvm/Instruction.h"
Reid Spencerad89bd62004-07-25 18:07:36 +000026#include "Support/DataTypes.h"
27#include <string>
28#include <vector>
Chris Lattner00950542001-06-06 20:29:01 +000029
Brian Gaeked0fde302003-11-11 22:41:34 +000030namespace llvm {
31
Chris Lattnere8fdde12001-09-07 16:39:41 +000032class BytecodeWriter {
Reid Spencerad89bd62004-07-25 18:07:36 +000033 std::vector<unsigned char> &Out;
Chris Lattner00950542001-06-06 20:29:01 +000034 SlotCalculator Table;
35public:
Reid Spencerad89bd62004-07-25 18:07:36 +000036 BytecodeWriter(std::vector<unsigned char> &o, const Module *M);
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner83bb3d22004-01-14 23:36:54 +000038private:
Chris Lattner186a1f72003-03-19 20:56:46 +000039 void outputConstants(bool isFunction);
Chris Lattner83bb3d22004-01-14 23:36:54 +000040 void outputConstantStrings();
Chris Lattner186a1f72003-03-19 20:56:46 +000041 void outputFunction(const Function *F);
Chris Lattnercf3e67f2004-01-18 21:08:52 +000042 void outputCompactionTable();
Reid Spencerd1fb1b72004-07-04 11:44:27 +000043 void outputCompactionTypes(unsigned StartNo);
Chris Lattnercf3e67f2004-01-18 21:08:52 +000044 void outputCompactionTablePlane(unsigned PlaneNo,
45 const std::vector<const Value*> &TypePlane,
46 unsigned StartNo);
47 void outputInstructions(const Function *F);
48 void outputInstruction(const Instruction &I);
Reid Spencerad89bd62004-07-25 18:07:36 +000049 void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
50 const SlotCalculator &Table,
51 unsigned Type);
52 void outputInstrVarArgsCall(const Instruction *I,
53 unsigned Opcode,
54 const SlotCalculator &Table,
55 unsigned Type) ;
56 inline void outputInstructionFormat1(const Instruction *I,
57 unsigned Opcode,
58 unsigned *Slots,
59 unsigned Type) ;
60 inline void outputInstructionFormat2(const Instruction *I,
61 unsigned Opcode,
62 unsigned *Slots,
63 unsigned Type) ;
64 inline void outputInstructionFormat3(const Instruction *I,
65 unsigned Opcode,
66 unsigned *Slots,
67 unsigned Type) ;
Chris Lattner00950542001-06-06 20:29:01 +000068
Chris Lattner00950542001-06-06 20:29:01 +000069 void outputModuleInfoBlock(const Module *C);
70 void outputSymbolTable(const SymbolTable &ST);
Reid Spencerd1fb1b72004-07-04 11:44:27 +000071 void outputTypes(unsigned StartNo);
Vikram S. Adve054bd682002-07-14 23:05:53 +000072 void outputConstantsInPlane(const std::vector<const Value*> &Plane,
73 unsigned StartNo);
Chris Lattner83bb3d22004-01-14 23:36:54 +000074 void outputConstant(const Constant *CPV);
Chris Lattner00950542001-06-06 20:29:01 +000075 void outputType(const Type *T);
Reid Spencerad89bd62004-07-25 18:07:36 +000076
77 /// @brief Unsigned integer output primitive
78 inline void output(unsigned i, int pos = -1);
79
80 /// @brief Signed integer output primitive
81 inline void output(int i);
82
83 /// @brief 64-bit variable bit rate output primitive.
84 inline void output_vbr(uint64_t i);
85
86 /// @brief 32-bit variable bit rate output primitive.
87 inline void output_vbr(unsigned i);
88
89 /// @brief Signed 64-bit variable bit rate output primitive.
90 inline void output_vbr(int64_t i);
91
92 /// @brief Signed 32-bit variable bit rate output primitive.
93 inline void output_vbr(int i);
94
95 /// Emit the minimal number of bytes that will bring us to 32 bit alignment.
96 /// @brief 32-bit alignment output primitive
97 inline void align32();
98
99 inline void output(const std::string &s, bool Aligned = true);
100
101 inline void output_data(const void *Ptr, const void *End);
102
103 inline void output_float(float& FloatVal);
104 inline void output_double(double& DoubleVal);
105
106 inline void output_typeid(unsigned i);
107
108 inline size_t size() const { return Out.size(); }
109 inline void resize(size_t S) { Out.resize(S); }
110 friend class BytecodeBlock;
Chris Lattner00950542001-06-06 20:29:01 +0000111};
112
Chris Lattner0baa0af2004-01-15 21:06:57 +0000113/// BytecodeBlock - Little helper class is used by the bytecode writer to help
114/// do backpatching of bytecode block sizes really easily. It backpatches when
115/// it goes out of scope.
116///
Chris Lattner00950542001-06-06 20:29:01 +0000117class BytecodeBlock {
Reid Spencerad89bd62004-07-25 18:07:36 +0000118 unsigned Id;
Chris Lattner00950542001-06-06 20:29:01 +0000119 unsigned Loc;
Reid Spencerad89bd62004-07-25 18:07:36 +0000120 BytecodeWriter& Writer;
Chris Lattner00950542001-06-06 20:29:01 +0000121
Chris Lattner0baa0af2004-01-15 21:06:57 +0000122 /// ElideIfEmpty - If this is true and the bytecode block ends up being empty,
123 /// the block can remove itself from the output stream entirely.
124 bool ElideIfEmpty;
125
Reid Spencerad89bd62004-07-25 18:07:36 +0000126 /// If this is true then the block is written with a long format header using
127 /// a uint (32-bits) for both the block id and size. Otherwise, it uses the
128 /// short format which is a single uint with 27 bits for size and 5 bits for
129 /// the block id. Both formats are used in a bc file with version 1.3.
130 /// Previously only the long format was used.
131 bool HasLongFormat;
132
Chris Lattner00950542001-06-06 20:29:01 +0000133 BytecodeBlock(const BytecodeBlock &); // do not implement
134 void operator=(const BytecodeBlock &); // do not implement
135public:
Reid Spencerad89bd62004-07-25 18:07:36 +0000136 inline BytecodeBlock(unsigned ID, BytecodeWriter& w,
137 bool elideIfEmpty = false, bool hasLongFormat = false);
Chris Lattner00950542001-06-06 20:29:01 +0000138
Reid Spencerad89bd62004-07-25 18:07:36 +0000139 inline ~BytecodeBlock();
Chris Lattner00950542001-06-06 20:29:01 +0000140};
141
Brian Gaeked0fde302003-11-11 22:41:34 +0000142} // End llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000143
144#endif