blob: c4dbf474a0c7ae89123a5e5f472959939c9cd6c6 [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"
22
Brian Gaeked0fde302003-11-11 22:41:34 +000023namespace llvm {
Chris Lattner3bc5a602006-01-25 23:08:15 +000024 class InlineAsm;
Reid Spencer78d033e2007-01-06 07:24:44 +000025 class TypeSymbolTable;
Reid Spenceref9b9a72007-02-05 20:47:22 +000026 class ValueSymbolTable;
Reid Spencer91ac04a2007-04-09 06:14:31 +000027 class ParamAttrsList;
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 Lattner8dcd81c2007-02-09 07:53:20 +000036 void outputConstants();
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 outputInstructions(const Function *F);
40 void outputInstruction(const Instruction &I);
Reid Spencerad89bd62004-07-25 18:07:36 +000041 void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
Misha Brukman23c6d2c2005-04-21 21:48:46 +000042 const SlotCalculator &Table,
43 unsigned Type);
44 void outputInstrVarArgsCall(const Instruction *I,
45 unsigned Opcode,
46 const SlotCalculator &Table,
47 unsigned Type) ;
48 inline void outputInstructionFormat1(const Instruction *I,
49 unsigned Opcode,
50 unsigned *Slots,
51 unsigned Type) ;
52 inline void outputInstructionFormat2(const Instruction *I,
53 unsigned Opcode,
54 unsigned *Slots,
55 unsigned Type) ;
56 inline void outputInstructionFormat3(const Instruction *I,
57 unsigned Opcode,
58 unsigned *Slots,
59 unsigned Type) ;
Chris Lattner00950542001-06-06 20:29:01 +000060
Chris Lattner00950542001-06-06 20:29:01 +000061 void outputModuleInfoBlock(const Module *C);
Reid Spencer78d033e2007-01-06 07:24:44 +000062 void outputTypeSymbolTable(const TypeSymbolTable &TST);
Reid Spenceref9b9a72007-02-05 20:47:22 +000063 void outputValueSymbolTable(const ValueSymbolTable &ST);
Reid Spencerd1fb1b72004-07-04 11:44:27 +000064 void outputTypes(unsigned StartNo);
Reid Spencer91ac04a2007-04-09 06:14:31 +000065 void outputParamAttrsList(const ParamAttrsList* Attrs);
Chris Lattner863da4c2007-02-10 07:42:59 +000066 void outputConstantsInPlane(const Value *const*Plane, unsigned PlaneSize,
Vikram S. Adve054bd682002-07-14 23:05:53 +000067 unsigned StartNo);
Chris Lattner83bb3d22004-01-14 23:36:54 +000068 void outputConstant(const Constant *CPV);
Chris Lattner3bc5a602006-01-25 23:08:15 +000069 void outputInlineAsm(const InlineAsm *IA);
Chris Lattner00950542001-06-06 20:29:01 +000070 void outputType(const Type *T);
Reid Spencerad89bd62004-07-25 18:07:36 +000071
72 /// @brief Unsigned integer output primitive
73 inline void output(unsigned i, int pos = -1);
74
75 /// @brief Signed integer output primitive
76 inline void output(int i);
77
78 /// @brief 64-bit variable bit rate output primitive.
79 inline void output_vbr(uint64_t i);
80
81 /// @brief 32-bit variable bit rate output primitive.
82 inline void output_vbr(unsigned i);
83
84 /// @brief Signed 64-bit variable bit rate output primitive.
85 inline void output_vbr(int64_t i);
86
87 /// @brief Signed 32-bit variable bit rate output primitive.
88 inline void output_vbr(int i);
89
Chris Lattnerdec628e2007-02-12 05:18:08 +000090 inline void output_str(const char *Str, unsigned Len);
91 inline void output(const std::string &s) {
92 output_str(&s[0], s.size());
93 }
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