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" |
Chris Lattner | 4443019 | 2004-01-10 18:56:59 +0000 | [diff] [blame] | 23 | #include "WriterPrimitives.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | #include "llvm/Bytecode/Format.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 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | namespace llvm { |
| 29 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 30 | class BytecodeWriter { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 31 | std::deque<unsigned char> &Out; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | SlotCalculator Table; |
| 33 | public: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 34 | BytecodeWriter(std::deque<unsigned char> &o, const Module *M); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 36 | private: |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 37 | void outputConstants(bool isFunction); |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 38 | void outputConstantStrings(); |
Chris Lattner | 186a1f7 | 2003-03-19 20:56:46 +0000 | [diff] [blame] | 39 | void outputFunction(const Function *F); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 40 | void processInstruction(const Instruction &I); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 42 | void outputModuleInfoBlock(const Module *C); |
| 43 | void outputSymbolTable(const SymbolTable &ST); |
Vikram S. Adve | 054bd68 | 2002-07-14 23:05:53 +0000 | [diff] [blame] | 44 | void outputConstantsInPlane(const std::vector<const Value*> &Plane, |
| 45 | unsigned StartNo); |
Chris Lattner | 83bb3d2 | 2004-01-14 23:36:54 +0000 | [diff] [blame] | 46 | void outputConstant(const Constant *CPV); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 47 | void outputType(const Type *T); |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame^] | 53 | /// BytecodeBlock - Little helper class is used by the bytecode writer to help |
| 54 | /// do backpatching of bytecode block sizes really easily. It backpatches when |
| 55 | /// it goes out of scope. |
| 56 | /// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 57 | class BytecodeBlock { |
| 58 | unsigned Loc; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 59 | std::deque<unsigned char> &Out; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame^] | 61 | /// ElideIfEmpty - If this is true and the bytecode block ends up being empty, |
| 62 | /// the block can remove itself from the output stream entirely. |
| 63 | bool ElideIfEmpty; |
| 64 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | BytecodeBlock(const BytecodeBlock &); // do not implement |
| 66 | void operator=(const BytecodeBlock &); // do not implement |
| 67 | public: |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame^] | 68 | inline BytecodeBlock(unsigned ID, std::deque<unsigned char> &o, |
| 69 | bool elideIfEmpty = false) |
| 70 | : Out(o), ElideIfEmpty(elideIfEmpty) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 71 | output(ID, Out); |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame^] | 72 | output(0U, Out); // Reserve the space for the block size... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 73 | Loc = Out.size(); |
| 74 | } |
| 75 | |
| 76 | inline ~BytecodeBlock() { // Do backpatch when block goes out |
| 77 | // of scope... |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame^] | 78 | if (Loc == Out.size() && ElideIfEmpty) { |
| 79 | // If the block is empty, and we are allowed to, do not emit the block at |
| 80 | // all! |
| 81 | Out.resize(Out.size()-8); |
| 82 | return; |
| 83 | } |
| 84 | |
Chris Lattner | e8fdde1 | 2001-09-07 16:39:41 +0000 | [diff] [blame] | 85 | //cerr << "OldLoc = " << Loc << " NewLoc = " << NewLoc << " diff = " |
| 86 | // << (NewLoc-Loc) << endl; |
Chris Lattner | 0baa0af | 2004-01-15 21:06:57 +0000 | [diff] [blame^] | 87 | output(unsigned(Out.size()-Loc), Out, int(Loc-4)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 88 | align32(Out); // Blocks must ALWAYS be aligned |
| 89 | } |
| 90 | }; |
| 91 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 92 | } // End llvm namespace |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 93 | |
| 94 | #endif |