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 | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/SlotCalculator.h" |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 18 | #include "llvm/Bytecode/Primitives.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | #include "llvm/Instruction.h" |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 20 | #include <deque> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 21 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 22 | class BytecodeWriter { |
| 23 | deque<unsigned char> &Out; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | SlotCalculator Table; |
| 25 | public: |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 26 | BytecodeWriter(deque<unsigned char> &o, const Module *M); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 27 | |
| 28 | protected: |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 29 | void outputConstants(bool isMethod); |
| 30 | void processMethod(const Method *M); |
| 31 | void processBasicBlock(const BasicBlock *BB); |
| 32 | void processInstruction(const Instruction *I); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | |
| 34 | private : |
| 35 | inline void outputSignature() { |
| 36 | static const unsigned char *Sig = (const unsigned char*)"llvm"; |
| 37 | Out.insert(Out.end(), Sig, Sig+4); // output the bytecode signature... |
| 38 | } |
| 39 | |
| 40 | void outputModuleInfoBlock(const Module *C); |
| 41 | void outputSymbolTable(const SymbolTable &ST); |
| 42 | bool outputConstant(const ConstPoolVal *CPV); |
| 43 | void outputType(const Type *T); |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | // BytecodeBlock - Little helper class that helps us do backpatching of bytecode |
| 50 | // block sizes really easily. It backpatches when it goes out of scope. |
| 51 | // |
| 52 | class BytecodeBlock { |
| 53 | unsigned Loc; |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 54 | deque<unsigned char> &Out; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 55 | |
| 56 | BytecodeBlock(const BytecodeBlock &); // do not implement |
| 57 | void operator=(const BytecodeBlock &); // do not implement |
| 58 | public: |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 59 | inline BytecodeBlock(unsigned ID, deque<unsigned char> &o) : Out(o) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | output(ID, Out); |
| 61 | output((unsigned)0, Out); // Reserve the space for the block size... |
| 62 | Loc = Out.size(); |
| 63 | } |
| 64 | |
| 65 | inline ~BytecodeBlock() { // Do backpatch when block goes out |
| 66 | // of scope... |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 67 | //cerr << "OldLoc = " << Loc << " NewLoc = " << NewLoc << " diff = " |
| 68 | // << (NewLoc-Loc) << endl; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 69 | output((unsigned)(Out.size()-Loc), Out, (int)Loc-4); |
| 70 | align32(Out); // Blocks must ALWAYS be aligned |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | #endif |