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