blob: f8c276e8581a9563f5e27c6e78c21d40021b46ed [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- WriterInternals.h - Data structures shared by the Writer -*- C++ -*-===//
Misha Brukman23c6d2c2005-04-21 21:48:46 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// 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.
Misha Brukman23c6d2c2005-04-21 21:48:46 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This header defines the interface used between components of the bytecode
11// writer.
12//
Chris Lattner00950542001-06-06 20:29:01 +000013//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
16#define LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
17
Reid Spencerd1fb1b72004-07-04 11:44:27 +000018#include "SlotCalculator.h"
19#include "llvm/Bytecode/Writer.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include "llvm/Bytecode/Format.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include "llvm/Instruction.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/DataTypes.h"
Reid Spencerad89bd62004-07-25 18:07:36 +000023#include <string>
24#include <vector>
Chris Lattner00950542001-06-06 20:29:01 +000025
Brian Gaeked0fde302003-11-11 22:41:34 +000026namespace llvm {
Chris Lattner3bc5a602006-01-25 23:08:15 +000027 class InlineAsm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattnere8fdde12001-09-07 16:39:41 +000029class BytecodeWriter {
Reid Spencerad89bd62004-07-25 18:07:36 +000030 std::vector<unsigned char> &Out;
Chris Lattner00950542001-06-06 20:29:01 +000031 SlotCalculator Table;
32public:
Reid Spencerad89bd62004-07-25 18:07:36 +000033 BytecodeWriter(std::vector<unsigned char> &o, const Module *M);
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner83bb3d22004-01-14 23:36:54 +000035private:
Chris Lattner186a1f72003-03-19 20:56:46 +000036 void outputConstants(bool isFunction);
Chris Lattner83bb3d22004-01-14 23:36:54 +000037 void outputConstantStrings();
Chris Lattner186a1f72003-03-19 20:56:46 +000038 void outputFunction(const Function *F);
Chris Lattnercf3e67f2004-01-18 21:08:52 +000039 void outputCompactionTable();
Reid Spencerd1fb1b72004-07-04 11:44:27 +000040 void outputCompactionTypes(unsigned StartNo);
Chris Lattnercf3e67f2004-01-18 21:08:52 +000041 void outputCompactionTablePlane(unsigned PlaneNo,
42 const std::vector<const Value*> &TypePlane,
43 unsigned StartNo);
44 void outputInstructions(const Function *F);
45 void outputInstruction(const Instruction &I);
Reid Spencerad89bd62004-07-25 18:07:36 +000046 void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
Misha Brukman23c6d2c2005-04-21 21:48:46 +000047 const SlotCalculator &Table,
48 unsigned Type);
49 void outputInstrVarArgsCall(const Instruction *I,
50 unsigned Opcode,
51 const SlotCalculator &Table,
52 unsigned Type) ;
53 inline void outputInstructionFormat1(const Instruction *I,
54 unsigned Opcode,
55 unsigned *Slots,
56 unsigned Type) ;
57 inline void outputInstructionFormat2(const Instruction *I,
58 unsigned Opcode,
59 unsigned *Slots,
60 unsigned Type) ;
61 inline void outputInstructionFormat3(const Instruction *I,
62 unsigned Opcode,
63 unsigned *Slots,
64 unsigned Type) ;
Chris Lattner00950542001-06-06 20:29:01 +000065
Chris Lattner00950542001-06-06 20:29:01 +000066 void outputModuleInfoBlock(const Module *C);
67 void outputSymbolTable(const SymbolTable &ST);
Reid Spencerd1fb1b72004-07-04 11:44:27 +000068 void outputTypes(unsigned StartNo);
Vikram S. Adve054bd682002-07-14 23:05:53 +000069 void outputConstantsInPlane(const std::vector<const Value*> &Plane,
70 unsigned StartNo);
Chris Lattner83bb3d22004-01-14 23:36:54 +000071 void outputConstant(const Constant *CPV);
Chris Lattner3bc5a602006-01-25 23:08:15 +000072 void outputInlineAsm(const InlineAsm *IA);
Chris Lattner00950542001-06-06 20:29:01 +000073 void outputType(const Type *T);
Reid Spencerad89bd62004-07-25 18:07:36 +000074
75 /// @brief Unsigned integer output primitive
76 inline void output(unsigned i, int pos = -1);
77
78 /// @brief Signed integer output primitive
79 inline void output(int i);
80
81 /// @brief 64-bit variable bit rate output primitive.
82 inline void output_vbr(uint64_t i);
83
84 /// @brief 32-bit variable bit rate output primitive.
85 inline void output_vbr(unsigned i);
86
87 /// @brief Signed 64-bit variable bit rate output primitive.
88 inline void output_vbr(int64_t i);
89
90 /// @brief Signed 32-bit variable bit rate output primitive.
91 inline void output_vbr(int i);
92
Chris Lattner3bc5a602006-01-25 23:08:15 +000093 inline void output(const std::string &s);
Reid Spencerad89bd62004-07-25 18:07:36 +000094
95 inline void output_data(const void *Ptr, const void *End);
96
97 inline void output_float(float& FloatVal);
98 inline void output_double(double& DoubleVal);
99
100 inline void output_typeid(unsigned i);
101
102 inline size_t size() const { return Out.size(); }
103 inline void resize(size_t S) { Out.resize(S); }
104 friend class BytecodeBlock;
Chris Lattner00950542001-06-06 20:29:01 +0000105};
106
Chris Lattner0baa0af2004-01-15 21:06:57 +0000107/// BytecodeBlock - Little helper class is used by the bytecode writer to help
108/// do backpatching of bytecode block sizes really easily. It backpatches when
109/// it goes out of scope.
110///
Chris Lattner00950542001-06-06 20:29:01 +0000111class BytecodeBlock {
Reid Spencerad89bd62004-07-25 18:07:36 +0000112 unsigned Id;
Chris Lattner00950542001-06-06 20:29:01 +0000113 unsigned Loc;
Reid Spencerad89bd62004-07-25 18:07:36 +0000114 BytecodeWriter& Writer;
Chris Lattner00950542001-06-06 20:29:01 +0000115
Chris Lattner0baa0af2004-01-15 21:06:57 +0000116 /// ElideIfEmpty - If this is true and the bytecode block ends up being empty,
117 /// the block can remove itself from the output stream entirely.
118 bool ElideIfEmpty;
119
Reid Spencerad89bd62004-07-25 18:07:36 +0000120 /// If this is true then the block is written with a long format header using
121 /// a uint (32-bits) for both the block id and size. Otherwise, it uses the
122 /// short format which is a single uint with 27 bits for size and 5 bits for
123 /// the block id. Both formats are used in a bc file with version 1.3.
124 /// Previously only the long format was used.
125 bool HasLongFormat;
126
Chris Lattner00950542001-06-06 20:29:01 +0000127 BytecodeBlock(const BytecodeBlock &); // do not implement
128 void operator=(const BytecodeBlock &); // do not implement
129public:
Reid Spencerad89bd62004-07-25 18:07:36 +0000130 inline BytecodeBlock(unsigned ID, BytecodeWriter& w,
131 bool elideIfEmpty = false, bool hasLongFormat = false);
Chris Lattner00950542001-06-06 20:29:01 +0000132
Reid Spencerad89bd62004-07-25 18:07:36 +0000133 inline ~BytecodeBlock();
Chris Lattner00950542001-06-06 20:29:01 +0000134};
135
Brian Gaeked0fde302003-11-11 22:41:34 +0000136} // End llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000137
138#endif