blob: 9c9e1abcdd82576bc6cba12e109d0d809d84056c [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Writer.cpp - Library for writing VM bytecode files ----------------===//
John Criswellb576c942003-10-20 19:43:21 +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 library implements the functionality defined in llvm/Bytecode/Writer.h
11//
Chris Lattner00950542001-06-06 20:29:01 +000012// Note that this file uses an unusual technique of outputting all the bytecode
Chris Lattnerabe83ae2003-09-15 00:33:20 +000013// to a deque of unsigned char, then copies the deque to an ostream. The
Chris Lattner00950542001-06-06 20:29:01 +000014// reason for this is that we must do "seeking" in the stream to do back-
15// patching, and some very important ostreams that we want to support (like
16// pipes) do not support seeking. :( :( :(
17//
Chris Lattnere8fdde12001-09-07 16:39:41 +000018// The choice of the deque data structure is influenced by the extremely fast
19// "append" speed, plus the free "seek"/replace in the middle of the stream. I
20// didn't use a vector because the stream could end up very large and copying
21// the whole thing to reallocate would be kinda silly.
Chris Lattner00950542001-06-06 20:29:01 +000022//
23// Note that the performance of this library is not terribly important, because
24// it shouldn't be used by JIT type applications... so it is not a huge focus
25// at least. :)
26//
27//===----------------------------------------------------------------------===//
28
29#include "WriterInternals.h"
Chris Lattner635cd932002-07-23 19:56:44 +000030#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner00950542001-06-06 20:29:01 +000031#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000032#include "llvm/SymbolTable.h"
33#include "llvm/DerivedTypes.h"
Chris Lattnerf60dc882001-11-29 16:32:16 +000034#include "Support/STLExtras.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000035#include "Support/Statistic.h"
John Criswell7a73b802003-06-30 21:59:07 +000036#include "Config/string.h"
Chris Lattner00950542001-06-06 20:29:01 +000037#include <algorithm>
38
Brian Gaeked0fde302003-11-11 22:41:34 +000039namespace llvm {
40
Chris Lattner635cd932002-07-23 19:56:44 +000041static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
42
Chris Lattnerce6ef112002-07-26 18:40:14 +000043static Statistic<>
Chris Lattnera92f6962002-10-01 22:38:41 +000044BytesWritten("bytecodewriter", "Number of bytecode bytes written");
Chris Lattner635cd932002-07-23 19:56:44 +000045
46
Chris Lattner697954c2002-01-20 22:54:45 +000047BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M)
Chris Lattner00950542001-06-06 20:29:01 +000048 : Out(o), Table(M, false) {
49
50 outputSignature();
51
52 // Emit the top level CLASS block.
53 BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
54
Chris Lattnerd445c6b2003-08-24 13:47:36 +000055 bool isBigEndian = M->getEndianness() == Module::BigEndian;
56 bool hasLongPointers = M->getPointerSize() == Module::Pointer64;
57 bool hasNoEndianness = M->getEndianness() == Module::AnyEndianness;
58 bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize;
Chris Lattner186a1f72003-03-19 20:56:46 +000059
Chris Lattnereff112c2003-10-18 05:54:48 +000060 // Output the version identifier... we are currently on bytecode version #0
61 unsigned Version = (0 << 4) | isBigEndian | (hasLongPointers << 1) |
Chris Lattnerd445c6b2003-08-24 13:47:36 +000062 (hasNoEndianness << 2) | (hasNoPointerSize << 3);
Chris Lattner186a1f72003-03-19 20:56:46 +000063 output_vbr(Version, Out);
Chris Lattner00950542001-06-06 20:29:01 +000064 align32(Out);
65
Chris Lattner186a1f72003-03-19 20:56:46 +000066 {
67 BytecodeBlock CPool(BytecodeFormat::GlobalTypePlane, Out);
68
69 // Write the type plane for types first because earlier planes (e.g. for a
70 // primitive type like float) may have constants constructed using types
71 // coming later (e.g., via getelementptr from a pointer type). The type
72 // plane is needed before types can be fwd or bkwd referenced.
73 const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
74 assert(!Plane.empty() && "No types at all?");
75 unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types...
76 outputConstantsInPlane(Plane, ValNo); // Write out the types
77 }
Chris Lattner00950542001-06-06 20:29:01 +000078
Chris Lattner186a1f72003-03-19 20:56:46 +000079 // The ModuleInfoBlock follows directly after the type information
Chris Lattnere8fdde12001-09-07 16:39:41 +000080 outputModuleInfoBlock(M);
81
Chris Lattner186a1f72003-03-19 20:56:46 +000082 // Output module level constants, used for global variable initializers
83 outputConstants(false);
84
Chris Lattnerb5794002002-04-07 22:49:37 +000085 // Do the whole module now! Process each function at a time...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000086 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner186a1f72003-03-19 20:56:46 +000087 outputFunction(I);
Chris Lattnere8fdde12001-09-07 16:39:41 +000088
89 // If needed, output the symbol table for the module...
Chris Lattner6e6026b2002-11-20 18:36:02 +000090 outputSymbolTable(M->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +000091}
92
Vikram S. Advea7dac3d2002-07-14 23:07:51 +000093// Helper function for outputConstants().
94// Writes out all the constants in the plane Plane starting at entry StartNo.
95//
96void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
97 &Plane, unsigned StartNo) {
98 unsigned ValNo = StartNo;
99
Chris Lattner186a1f72003-03-19 20:56:46 +0000100 // Scan through and ignore function arguments/global values...
101 for (; ValNo < Plane.size() && (isa<Argument>(Plane[ValNo]) ||
102 isa<GlobalValue>(Plane[ValNo])); ValNo++)
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000103 /*empty*/;
104
105 unsigned NC = ValNo; // Number of constants
106 for (; NC < Plane.size() &&
107 (isa<Constant>(Plane[NC]) || isa<Type>(Plane[NC])); NC++)
108 /*empty*/;
109 NC -= ValNo; // Convert from index into count
110 if (NC == 0) return; // Skip empty type planes...
111
112 // Output type header: [num entries][type id number]
113 //
114 output_vbr(NC, Out);
115
116 // Output the Type ID Number...
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000117 int Slot = Table.getSlot(Plane.front()->getType());
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000118 assert (Slot != -1 && "Type in constant pool but not in function!!");
119 output_vbr((unsigned)Slot, Out);
120
121 //cerr << "Emitting " << NC << " constants of type '"
122 // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n";
123
124 for (unsigned i = ValNo; i < ValNo+NC; ++i) {
125 const Value *V = Plane[i];
126 if (const Constant *CPV = dyn_cast<Constant>(V)) {
127 //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":"
128 // << Out.size() << "\n";
129 outputConstant(CPV);
130 } else {
Chris Lattner186a1f72003-03-19 20:56:46 +0000131 outputType(cast<Type>(V));
Vikram S. Advea7dac3d2002-07-14 23:07:51 +0000132 }
133 }
134}
135
Chris Lattner79df7c02002-03-26 18:01:55 +0000136void BytecodeWriter::outputConstants(bool isFunction) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000137 BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000138
139 unsigned NumPlanes = Table.getNumPlanes();
Chris Lattnerf69315b2003-05-22 18:35:38 +0000140
141 // Output the type plane before any constants!
142 if (isFunction && NumPlanes > Type::TypeTyID) {
143 const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
Chris Lattner186a1f72003-03-19 20:56:46 +0000144 if (!Plane.empty()) { // Skip empty type planes...
Chris Lattnerf69315b2003-05-22 18:35:38 +0000145 unsigned ValNo = Table.getModuleLevel(Type::TypeTyID);
146 outputConstantsInPlane(Plane, ValNo);
Chris Lattner6463e0d2002-10-14 03:34:17 +0000147 }
Chris Lattner00950542001-06-06 20:29:01 +0000148 }
Chris Lattnerf69315b2003-05-22 18:35:38 +0000149
150 for (unsigned pno = 0; pno != NumPlanes; pno++)
151 if (pno != Type::TypeTyID) { // Type plane handled above.
152 const std::vector<const Value*> &Plane = Table.getPlane(pno);
153 if (!Plane.empty()) { // Skip empty type planes...
154 unsigned ValNo = 0;
Misha Brukman37f92e22003-09-11 22:34:13 +0000155 if (isFunction) // Don't re-emit module constants
Chris Lattnerf69315b2003-05-22 18:35:38 +0000156 ValNo += Table.getModuleLevel(pno);
157
158 if (pno >= Type::FirstDerivedTyID) {
159 // Skip zero initializer
160 if (ValNo == 0)
161 ValNo = 1;
162 }
163
164 // Write out constants in the plane
165 outputConstantsInPlane(Plane, ValNo);
166 }
167 }
Chris Lattner00950542001-06-06 20:29:01 +0000168}
169
Chris Lattner6b252422003-10-16 18:28:50 +0000170static unsigned getEncodedLinkage(const GlobalValue *GV) {
171 switch (GV->getLinkage()) {
172 default: assert(0 && "Invalid linkage!");
173 case GlobalValue::ExternalLinkage: return 0;
Chris Lattner6b252422003-10-16 18:28:50 +0000174 case GlobalValue::WeakLinkage: return 1;
175 case GlobalValue::AppendingLinkage: return 2;
176 case GlobalValue::InternalLinkage: return 3;
Chris Lattner22482a12003-10-18 06:30:21 +0000177 case GlobalValue::LinkOnceLinkage: return 4;
Chris Lattner6b252422003-10-16 18:28:50 +0000178 }
179}
180
Chris Lattner00950542001-06-06 20:29:01 +0000181void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
182 BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
183
Chris Lattner70cc3392001-09-10 07:58:01 +0000184 // Output the types for the global variables in the module...
185 for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000186 int Slot = Table.getSlot(I->getType());
Chris Lattner70cc3392001-09-10 07:58:01 +0000187 assert(Slot != -1 && "Module global vars is broken!");
Chris Lattnerd70684f2001-09-18 04:01:05 +0000188
Chris Lattner22482a12003-10-18 06:30:21 +0000189 // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage,
190 // bit5+ = Slot # for type
191 unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) |
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000192 (I->hasInitializer() << 1) | I->isConstant();
Chris Lattnerd70684f2001-09-18 04:01:05 +0000193 output_vbr(oSlot, Out);
194
Chris Lattner1b98c5c2001-10-13 06:48:38 +0000195 // If we have an initializer, output it now.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000196 if (I->hasInitializer()) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000197 Slot = Table.getSlot((Value*)I->getInitializer());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000198 assert(Slot != -1 && "No slot for global var initializer!");
199 output_vbr((unsigned)Slot, Out);
200 }
Chris Lattner70cc3392001-09-10 07:58:01 +0000201 }
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000202 output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000203
Chris Lattnerb5794002002-04-07 22:49:37 +0000204 // Output the types of the functions in this module...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000205 for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000206 int Slot = Table.getSlot(I->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000207 assert(Slot != -1 && "Module const pool is broken!");
208 assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
209 output_vbr((unsigned)Slot, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000210 }
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000211 output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out);
Chris Lattner70cc3392001-09-10 07:58:01 +0000212
Chris Lattner00950542001-06-06 20:29:01 +0000213 align32(Out);
214}
215
Chris Lattner186a1f72003-03-19 20:56:46 +0000216void BytecodeWriter::outputFunction(const Function *F) {
Chris Lattnerc9aa7df2002-03-29 03:51:11 +0000217 BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
Chris Lattner6b252422003-10-16 18:28:50 +0000218 output_vbr(getEncodedLinkage(F), Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000219 // Only output the constant pool and other goodies if needed...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000220 if (!F->isExternal()) {
Chris Lattnerd23b1d32001-11-26 18:56:10 +0000221
Chris Lattnerb5794002002-04-07 22:49:37 +0000222 // Get slot information about the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000223 Table.incorporateFunction(F);
Chris Lattner00950542001-06-06 20:29:01 +0000224
Chris Lattnerb5794002002-04-07 22:49:37 +0000225 // Output information about the constants in the function...
Chris Lattnere8fdde12001-09-07 16:39:41 +0000226 outputConstants(true);
Chris Lattner00950542001-06-06 20:29:01 +0000227
Chris Lattnere8fdde12001-09-07 16:39:41 +0000228 // Output basic block nodes...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000229 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
230 processBasicBlock(*I);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000231
Chris Lattnerb5794002002-04-07 22:49:37 +0000232 // If needed, output the symbol table for the function...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000233 outputSymbolTable(F->getSymbolTable());
Chris Lattnere8fdde12001-09-07 16:39:41 +0000234
Chris Lattnerb5794002002-04-07 22:49:37 +0000235 Table.purgeFunction();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000236 }
Chris Lattner00950542001-06-06 20:29:01 +0000237}
238
239
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000240void BytecodeWriter::processBasicBlock(const BasicBlock &BB) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000241 BytecodeBlock FunctionBlock(BytecodeFormat::BasicBlock, Out);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000242 // Process all the instructions in the bb...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000243 for(BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I)
244 processInstruction(*I);
Chris Lattner00950542001-06-06 20:29:01 +0000245}
246
247void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000248 BytecodeBlock FunctionBlock(BytecodeFormat::SymbolTable, Out);
Chris Lattner00950542001-06-06 20:29:01 +0000249
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000250 for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
Chris Lattner00950542001-06-06 20:29:01 +0000251 SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
252 SymbolTable::type_const_iterator End = MST.type_end(TI->first);
253 int Slot;
254
255 if (I == End) continue; // Don't mess with an absent type...
256
257 // Symtab block header: [num entries][type id number]
258 output_vbr(MST.type_size(TI->first), Out);
259
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000260 Slot = Table.getSlot(TI->first);
Chris Lattner00950542001-06-06 20:29:01 +0000261 assert(Slot != -1 && "Type in symtab, but not in table!");
262 output_vbr((unsigned)Slot, Out);
263
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000264 for (; I != End; ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000265 // Symtab entry: [def slot #][name]
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000266 Slot = Table.getSlot(I->second);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000267 assert(Slot != -1 && "Value in symtab but has no slot number!!");
Chris Lattner00950542001-06-06 20:29:01 +0000268 output_vbr((unsigned)Slot, Out);
269 output(I->first, Out, false); // Don't force alignment...
270 }
271 }
272}
273
Anand Shuklaeea60fc2002-06-25 20:44:04 +0000274void WriteBytecodeToFile(const Module *C, std::ostream &Out) {
Chris Lattnere8fdde12001-09-07 16:39:41 +0000275 assert(C && "You can't write a null module!!");
Chris Lattner00950542001-06-06 20:29:01 +0000276
Chris Lattner697954c2002-01-20 22:54:45 +0000277 std::deque<unsigned char> Buffer;
Chris Lattner00950542001-06-06 20:29:01 +0000278
279 // This object populates buffer for us...
280 BytecodeWriter BCW(Buffer, C);
281
Chris Lattnerce6ef112002-07-26 18:40:14 +0000282 // Keep track of how much we've written...
283 BytesWritten += Buffer.size();
284
Chris Lattnere8fdde12001-09-07 16:39:41 +0000285 // Okay, write the deque out to the ostream now... the deque is not
286 // sequential in memory, however, so write out as much as possible in big
287 // chunks, until we're done.
288 //
Chris Lattner697954c2002-01-20 22:54:45 +0000289 std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
Chris Lattnere8fdde12001-09-07 16:39:41 +0000290 while (I != E) { // Loop until it's all written
291 // Scan to see how big this chunk is...
292 const unsigned char *ChunkPtr = &*I;
293 const unsigned char *LastPtr = ChunkPtr;
294 while (I != E) {
295 const unsigned char *ThisPtr = &*++I;
Chris Lattner5cb17412001-11-04 21:32:41 +0000296 if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
297 ++LastPtr;
298 break;
299 }
Chris Lattnere8fdde12001-09-07 16:39:41 +0000300 LastPtr = ThisPtr;
301 }
302
303 // Write out the chunk...
Chris Lattner697954c2002-01-20 22:54:45 +0000304 Out.write((char*)ChunkPtr, LastPtr-ChunkPtr);
Chris Lattnere8fdde12001-09-07 16:39:41 +0000305 }
306
Chris Lattner00950542001-06-06 20:29:01 +0000307 Out.flush();
308}
Brian Gaeked0fde302003-11-11 22:41:34 +0000309
310} // End llvm namespace