blob: 8cb4bfd8d6cea47e2033a4e607e41d49e499f475 [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
22#include "llvm/Bytecode/Writer.h"
23#include "llvm/Bytecode/Format.h"
Chris Lattnere8fdde12001-09-07 16:39:41 +000024#include "llvm/Bytecode/Primitives.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000025#include "llvm/SlotCalculator.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
36protected:
Chris Lattner186a1f72003-03-19 20:56:46 +000037 void outputConstants(bool isFunction);
38 void outputFunction(const Function *F);
Chris Lattner0b12b5f2002-06-25 16:13:21 +000039 void processBasicBlock(const BasicBlock &BB);
40 void processInstruction(const Instruction &I);
Chris Lattner00950542001-06-06 20:29:01 +000041
42private :
43 inline void outputSignature() {
44 static const unsigned char *Sig = (const unsigned char*)"llvm";
45 Out.insert(Out.end(), Sig, Sig+4); // output the bytecode signature...
46 }
47
48 void outputModuleInfoBlock(const Module *C);
49 void outputSymbolTable(const SymbolTable &ST);
Vikram S. Adve054bd682002-07-14 23:05:53 +000050 void outputConstantsInPlane(const std::vector<const Value*> &Plane,
51 unsigned StartNo);
Chris Lattnere9bb2df2001-12-03 22:26:30 +000052 bool outputConstant(const Constant *CPV);
Chris Lattner00950542001-06-06 20:29:01 +000053 void outputType(const Type *T);
54};
55
56
57
58
59// BytecodeBlock - Little helper class that helps us do backpatching of bytecode
60// block sizes really easily. It backpatches when it goes out of scope.
61//
62class BytecodeBlock {
63 unsigned Loc;
Chris Lattner697954c2002-01-20 22:54:45 +000064 std::deque<unsigned char> &Out;
Chris Lattner00950542001-06-06 20:29:01 +000065
66 BytecodeBlock(const BytecodeBlock &); // do not implement
67 void operator=(const BytecodeBlock &); // do not implement
68public:
Chris Lattner697954c2002-01-20 22:54:45 +000069 inline BytecodeBlock(unsigned ID, std::deque<unsigned char> &o) : Out(o) {
Chris Lattner00950542001-06-06 20:29:01 +000070 output(ID, Out);
71 output((unsigned)0, Out); // Reserve the space for the block size...
72 Loc = Out.size();
73 }
74
75 inline ~BytecodeBlock() { // Do backpatch when block goes out
76 // of scope...
Chris Lattnere8fdde12001-09-07 16:39:41 +000077 //cerr << "OldLoc = " << Loc << " NewLoc = " << NewLoc << " diff = "
78 // << (NewLoc-Loc) << endl;
Chris Lattner00950542001-06-06 20:29:01 +000079 output((unsigned)(Out.size()-Loc), Out, (int)Loc-4);
80 align32(Out); // Blocks must ALWAYS be aligned
81 }
82};
83
Brian Gaeked0fde302003-11-11 22:41:34 +000084} // End llvm namespace
Chris Lattner00950542001-06-06 20:29:01 +000085
86#endif