LLVM API Documentation

Serialize.cpp

Go to the documentation of this file.
00001 //==- Serialize.cpp - Generic Object Serialization to Bitcode ----*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the internal methods used for object serialization.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Bitcode/Serialize.h"
00015 #include "string.h"
00016 
00017 #ifdef DEBUG_BACKPATCH
00018 #include "llvm/Support/Streams.h"
00019 #endif
00020 
00021 using namespace llvm;
00022 
00023 Serializer::Serializer(BitstreamWriter& stream)
00024   : Stream(stream), BlockLevel(0) {}
00025 
00026 Serializer::~Serializer() {
00027   if (inRecord())
00028     EmitRecord();
00029 
00030   while (BlockLevel > 0)
00031     Stream.ExitBlock();
00032    
00033   Stream.FlushToWord();
00034 }
00035 
00036 void Serializer::EmitRecord() {
00037   assert(Record.size() > 0 && "Cannot emit empty record.");
00038   Stream.EmitRecord(8,Record);
00039   Record.clear();
00040 }
00041 
00042 void Serializer::EnterBlock(unsigned BlockID,unsigned CodeLen) {
00043   FlushRecord();
00044   Stream.EnterSubblock(BlockID,CodeLen);
00045   ++BlockLevel;
00046 }
00047 
00048 void Serializer::ExitBlock() {
00049   assert (BlockLevel > 0);
00050   --BlockLevel;
00051   FlushRecord();
00052   Stream.ExitBlock();
00053 }
00054 
00055 void Serializer::EmitInt(uint64_t X) {
00056   assert (BlockLevel > 0);
00057   Record.push_back(X);
00058 }
00059 
00060 void Serializer::EmitSInt(int64_t X) {
00061   if (X >= 0)
00062     EmitInt(X << 1);
00063   else
00064     EmitInt((-X << 1) | 1);
00065 }
00066 
00067 void Serializer::EmitCStr(const char* s, const char* end) {
00068   Record.push_back(end - s);
00069   
00070   while(s != end) {
00071     Record.push_back(*s);
00072     ++s;
00073   }
00074 }
00075 
00076 void Serializer::EmitCStr(const char* s) {
00077   EmitCStr(s,s+strlen(s));
00078 }
00079 
00080 SerializedPtrID Serializer::getPtrId(const void* ptr) {
00081   if (!ptr)
00082     return 0;
00083   
00084   MapTy::iterator I = PtrMap.find(ptr);
00085   
00086   if (I == PtrMap.end()) {
00087     unsigned id = PtrMap.size()+1;
00088 #ifdef DEBUG_BACKPATCH
00089     llvm::cerr << "Registered PTR: " << ptr << " => " << id << "\n";
00090 #endif
00091     PtrMap[ptr] = id;
00092     return id;
00093   }
00094   else return I->second;
00095 }
00096 
00097 bool Serializer::isRegistered(const void* ptr) const {
00098   MapTy::const_iterator I = PtrMap.find(ptr);
00099   return I != PtrMap.end();
00100 }
00101 
00102 
00103 #define INT_EMIT(TYPE)\
00104 void SerializeTrait<TYPE>::Emit(Serializer&S, TYPE X) { S.EmitInt(X); }
00105 
00106 INT_EMIT(bool)
00107 INT_EMIT(unsigned char)
00108 INT_EMIT(unsigned short)
00109 INT_EMIT(unsigned int)
00110 INT_EMIT(unsigned long)
00111 
00112 #define SINT_EMIT(TYPE)\
00113 void SerializeTrait<TYPE>::Emit(Serializer&S, TYPE X) { S.EmitSInt(X); }
00114 
00115 SINT_EMIT(signed char)
00116 SINT_EMIT(signed short)
00117 SINT_EMIT(signed int)
00118 SINT_EMIT(signed long)