blob: 6a036d804a911ee5cf3569b8fd81ea5af253399f [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;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnere8fdde12001-09-07 16:39:41 +000028class BytecodeWriter {
Reid Spencerad89bd62004-07-25 18:07:36 +000029 std::vector<unsigned char> &Out;
Chris Lattner00950542001-06-06 20:29:01 +000030 SlotCalculator Table;
31public:
Reid Spencerad89bd62004-07-25 18:07:36 +000032 BytecodeWriter(std::vector<unsigned char> &o, const Module *M);
Chris Lattner00950542001-06-06 20:29:01 +000033
Chris Lattner83bb3d22004-01-14 23:36:54 +000034private:
Chris Lattner8dcd81c2007-02-09 07:53:20 +000035 void outputConstants();
Chris Lattner83bb3d22004-01-14 23:36:54 +000036 void outputConstantStrings();
Chris Lattner186a1f72003-03-19 20:56:46 +000037 void outputFunction(const Function *F);
Chris Lattnercf3e67f2004-01-18 21:08:52 +000038 void outputInstructions(const Function *F);
39 void outputInstruction(const Instruction &I);
Reid Spencerad89bd62004-07-25 18:07:36 +000040 void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
Misha Brukman23c6d2c2005-04-21 21:48:46 +000041 const SlotCalculator &Table,
42 unsigned Type);
43 void outputInstrVarArgsCall(const Instruction *I,
44 unsigned Opcode,
45 const SlotCalculator &Table,
46 unsigned Type) ;
47 inline void outputInstructionFormat1(const Instruction *I,
48 unsigned Opcode,
49 unsigned *Slots,
50 unsigned Type) ;
51 inline void outputInstructionFormat2(const Instruction *I,
52 unsigned Opcode,
53 unsigned *Slots,
54 unsigned Type) ;
55 inline void outputInstructionFormat3(const Instruction *I,
56 unsigned Opcode,
57 unsigned *Slots,
58 unsigned Type) ;
Chris Lattner00950542001-06-06 20:29:01 +000059
Chris Lattner00950542001-06-06 20:29:01 +000060 void outputModuleInfoBlock(const Module *C);
Reid Spencer78d033e2007-01-06 07:24:44 +000061 void outputTypeSymbolTable(const TypeSymbolTable &TST);
Reid Spenceref9b9a72007-02-05 20:47:22 +000062 void outputValueSymbolTable(const ValueSymbolTable &ST);
Reid Spencerd1fb1b72004-07-04 11:44:27 +000063 void outputTypes(unsigned StartNo);
Chris Lattner863da4c2007-02-10 07:42:59 +000064 void outputConstantsInPlane(const Value *const*Plane, unsigned PlaneSize,
Vikram S. Adve054bd682002-07-14 23:05:53 +000065 unsigned StartNo);
Chris Lattner83bb3d22004-01-14 23:36:54 +000066 void outputConstant(const Constant *CPV);
Chris Lattner3bc5a602006-01-25 23:08:15 +000067 void outputInlineAsm(const InlineAsm *IA);
Chris Lattner00950542001-06-06 20:29:01 +000068 void outputType(const Type *T);
Reid Spencerad89bd62004-07-25 18:07:36 +000069
70 /// @brief Unsigned integer output primitive
71 inline void output(unsigned i, int pos = -1);
72
73 /// @brief Signed integer output primitive
74 inline void output(int i);
75
76 /// @brief 64-bit variable bit rate output primitive.
77 inline void output_vbr(uint64_t i);
78
79 /// @brief 32-bit variable bit rate output primitive.
80 inline void output_vbr(unsigned i);
81
82 /// @brief Signed 64-bit variable bit rate output primitive.
83 inline void output_vbr(int64_t i);
84
85 /// @brief Signed 32-bit variable bit rate output primitive.
86 inline void output_vbr(int i);
87
Chris Lattner3bc5a602006-01-25 23:08:15 +000088 inline void output(const std::string &s);
Reid Spencerad89bd62004-07-25 18:07:36 +000089
90 inline void output_data(const void *Ptr, const void *End);
91
92 inline void output_float(float& FloatVal);
93 inline void output_double(double& DoubleVal);
94
95 inline void output_typeid(unsigned i);
96
97 inline size_t size() const { return Out.size(); }
98 inline void resize(size_t S) { Out.resize(S); }
99 friend class BytecodeBlock;
Chris Lattner00950542001-06-06 20:29:01 +0000100};
101
Chris Lattner0baa0af2004-01-15 21:06:57 +0000102/// BytecodeBlock - Little helper class is used by the bytecode writer to help
103/// do backpatching of bytecode block sizes really easily. It backpatches when
104/// it goes out of scope.
105///
Chris Lattner00950542001-06-06 20:29:01 +0000106class BytecodeBlock {
Reid Spencerad89bd62004-07-25 18:07:36 +0000107 unsigned Id;
Chris Lattner00950542001-06-06 20:29:01 +0000108 unsigned Loc;
Reid Spencerad89bd62004-07-25 18:07:36 +0000109 BytecodeWriter& Writer;
Chris Lattner00950542001-06-06 20:29:01 +0000110
Chris Lattner0baa0af2004-01-15 21:06:57 +0000111 /// ElideIfEmpty - If this is true and the bytecode block ends up being empty,
112 /// the block can remove itself from the output stream entirely.
113 bool ElideIfEmpty;
114
Reid Spencerad89bd62004-07-25 18:07:36 +0000115 /// If this is true then the block is written with a long format header using
116 /// a uint (32-bits) for both the block id and size. Otherwise, it uses the
117 /// short format which is a single uint with 27 bits for size and 5 bits for
118 /// the block id. Both formats are used in a bc file with version 1.3.
119 /// Previously only the long format was used.
120 bool HasLongFormat;
121
Chris Lattner00950542001-06-06 20:29:01 +0000122 BytecodeBlock(const BytecodeBlock &); // do not implement
123 void operator=(const BytecodeBlock &); // do not implement
124public:
Reid Spencerad89bd62004-07-25 18:07:36 +0000125 inline BytecodeBlock(unsigned ID, BytecodeWriter& w,
126 bool elideIfEmpty = false, bool hasLongFormat = false);
Chris Lattner00950542001-06-06 20:29:01 +0000127
Reid Spencerad89bd62004-07-25 18:07:36 +0000128 inline ~BytecodeBlock();
Chris Lattner00950542001-06-06 20:29:01 +0000129};
130
Brian Gaeked0fde302003-11-11 22:41:34 +0000131} // End llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000132
133#endif