Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- WriterInternals.h - Data structures shared by the Writer -*- C++ -*--=// |
| 2 | // |
| 3 | // This header defines the interface used between components of the bytecode |
| 4 | // writer. |
| 5 | // |
| 6 | // Note that the performance of this library is not terribly important, because |
| 7 | // it shouldn't be used by JIT type applications... so it is not a huge focus |
| 8 | // at least. :) |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H |
| 13 | #define LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H |
| 14 | |
| 15 | #include "llvm/Bytecode/Writer.h" |
| 16 | #include "llvm/Bytecode/Format.h" |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 17 | #include "llvm/Bytecode/Primitives.h" |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 18 | #include "llvm/SlotCalculator.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | #include "llvm/Instruction.h" |
| 20 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 21 | class BytecodeWriter { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 22 | std::deque<unsigned char> &Out; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | SlotCalculator Table; |
| 24 | public: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 25 | BytecodeWriter(std::deque<unsigned char> &o, const Module *M); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 26 | |
| 27 | protected: |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame^] | 28 | void outputConstants(bool isFunction); |
| 29 | void outputFunction(const Function *F); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 30 | void processBasicBlock(const BasicBlock &BB); |
| 31 | void processInstruction(const Instruction &I); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | |
| 33 | private : |
| 34 | inline void outputSignature() { |
| 35 | static const unsigned char *Sig = (const unsigned char*)"llvm"; |
| 36 | Out.insert(Out.end(), Sig, Sig+4); // output the bytecode signature... |
| 37 | } |
| 38 | |
| 39 | void outputModuleInfoBlock(const Module *C); |
| 40 | void outputSymbolTable(const SymbolTable &ST); |
Vikram S. Adve | 054bd68 | 2002-07-14 23:05:53 +0000 | [diff] [blame] | 41 | void outputConstantsInPlane(const std::vector<const Value*> &Plane, |
| 42 | unsigned StartNo); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 43 | bool outputConstant(const Constant *CPV); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 44 | void outputType(const Type *T); |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | // BytecodeBlock - Little helper class that helps us do backpatching of bytecode |
| 51 | // block sizes really easily. It backpatches when it goes out of scope. |
| 52 | // |
| 53 | class BytecodeBlock { |
| 54 | unsigned Loc; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 55 | std::deque<unsigned char> &Out; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 56 | |
| 57 | BytecodeBlock(const BytecodeBlock &); // do not implement |
| 58 | void operator=(const BytecodeBlock &); // do not implement |
| 59 | public: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 60 | inline BytecodeBlock(unsigned ID, std::deque<unsigned char> &o) : Out(o) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | output(ID, Out); |
| 62 | output((unsigned)0, Out); // Reserve the space for the block size... |
| 63 | Loc = Out.size(); |
| 64 | } |
| 65 | |
| 66 | inline ~BytecodeBlock() { // Do backpatch when block goes out |
| 67 | // of scope... |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 68 | //cerr << "OldLoc = " << Loc << " NewLoc = " << NewLoc << " diff = " |
| 69 | // << (NewLoc-Loc) << endl; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 70 | output((unsigned)(Out.size()-Loc), Out, (int)Loc-4); |
| 71 | align32(Out); // Blocks must ALWAYS be aligned |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | #endif |