blob: 997c97d26037798c386da6d91d3e444d10261f2f [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
Chris Lattner44430192004-01-10 18:56:59 +000022#include "WriterPrimitives.h"
Reid Spencerd1fb1b72004-07-04 11:44:27 +000023#include "SlotCalculator.h"
24#include "llvm/Bytecode/Writer.h"
Chris Lattner00950542001-06-06 20:29:01 +000025#include "llvm/Bytecode/Format.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include "llvm/Instruction.h"
27
Brian Gaeked0fde302003-11-11 22:41:34 +000028namespace llvm {
29
Chris Lattnere8fdde12001-09-07 16:39:41 +000030class BytecodeWriter {
Chris Lattner697954c2002-01-20 22:54:45 +000031 std::deque<unsigned char> &Out;
Chris Lattner00950542001-06-06 20:29:01 +000032 SlotCalculator Table;
33public:
Chris Lattner697954c2002-01-20 22:54:45 +000034 BytecodeWriter(std::deque<unsigned char> &o, const Module *M);
Chris Lattner00950542001-06-06 20:29:01 +000035
Chris Lattner83bb3d22004-01-14 23:36:54 +000036private:
Chris Lattner186a1f72003-03-19 20:56:46 +000037 void outputConstants(bool isFunction);
Chris Lattner83bb3d22004-01-14 23:36:54 +000038 void outputConstantStrings();
Chris Lattner186a1f72003-03-19 20:56:46 +000039 void outputFunction(const Function *F);
Chris Lattnercf3e67f2004-01-18 21:08:52 +000040 void outputCompactionTable();
Reid Spencerd1fb1b72004-07-04 11:44:27 +000041 void outputCompactionTypes(unsigned StartNo);
Chris Lattnercf3e67f2004-01-18 21:08:52 +000042 void outputCompactionTablePlane(unsigned PlaneNo,
43 const std::vector<const Value*> &TypePlane,
44 unsigned StartNo);
45 void outputInstructions(const Function *F);
46 void outputInstruction(const Instruction &I);
Chris Lattner00950542001-06-06 20:29:01 +000047
Chris Lattner00950542001-06-06 20:29:01 +000048 void outputModuleInfoBlock(const Module *C);
49 void outputSymbolTable(const SymbolTable &ST);
Reid Spencerd1fb1b72004-07-04 11:44:27 +000050 void outputTypes(unsigned StartNo);
Vikram S. Adve054bd682002-07-14 23:05:53 +000051 void outputConstantsInPlane(const std::vector<const Value*> &Plane,
52 unsigned StartNo);
Chris Lattner83bb3d22004-01-14 23:36:54 +000053 void outputConstant(const Constant *CPV);
Chris Lattner00950542001-06-06 20:29:01 +000054 void outputType(const Type *T);
55};
56
57
58
59
Chris Lattner0baa0af2004-01-15 21:06:57 +000060/// BytecodeBlock - Little helper class is used by the bytecode writer to help
61/// do backpatching of bytecode block sizes really easily. It backpatches when
62/// it goes out of scope.
63///
Chris Lattner00950542001-06-06 20:29:01 +000064class BytecodeBlock {
65 unsigned Loc;
Chris Lattner697954c2002-01-20 22:54:45 +000066 std::deque<unsigned char> &Out;
Chris Lattner00950542001-06-06 20:29:01 +000067
Chris Lattner0baa0af2004-01-15 21:06:57 +000068 /// ElideIfEmpty - If this is true and the bytecode block ends up being empty,
69 /// the block can remove itself from the output stream entirely.
70 bool ElideIfEmpty;
71
Chris Lattner00950542001-06-06 20:29:01 +000072 BytecodeBlock(const BytecodeBlock &); // do not implement
73 void operator=(const BytecodeBlock &); // do not implement
74public:
Chris Lattner0baa0af2004-01-15 21:06:57 +000075 inline BytecodeBlock(unsigned ID, std::deque<unsigned char> &o,
76 bool elideIfEmpty = false)
77 : Out(o), ElideIfEmpty(elideIfEmpty) {
Chris Lattner00950542001-06-06 20:29:01 +000078 output(ID, Out);
Chris Lattner0baa0af2004-01-15 21:06:57 +000079 output(0U, Out); // Reserve the space for the block size...
Chris Lattner00950542001-06-06 20:29:01 +000080 Loc = Out.size();
81 }
82
83 inline ~BytecodeBlock() { // Do backpatch when block goes out
84 // of scope...
Chris Lattner0baa0af2004-01-15 21:06:57 +000085 if (Loc == Out.size() && ElideIfEmpty) {
86 // If the block is empty, and we are allowed to, do not emit the block at
87 // all!
88 Out.resize(Out.size()-8);
89 return;
90 }
91
Chris Lattnere8fdde12001-09-07 16:39:41 +000092 //cerr << "OldLoc = " << Loc << " NewLoc = " << NewLoc << " diff = "
93 // << (NewLoc-Loc) << endl;
Chris Lattner0baa0af2004-01-15 21:06:57 +000094 output(unsigned(Out.size()-Loc), Out, int(Loc-4));
Chris Lattner00950542001-06-06 20:29:01 +000095 align32(Out); // Blocks must ALWAYS be aligned
96 }
97};
98
Brian Gaeked0fde302003-11-11 22:41:34 +000099} // End llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +0000100
101#endif