1 //==- Serialize.cpp - Generic Object Serialization to Bitcode ----*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the internal methods used for object serialization.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bitcode/Serialize.h"
15 #include "llvm/Support/raw_ostream.h"
20 Serializer::Serializer(BitstreamWriter& stream)
21 : Stream(stream), BlockLevel(0) {}
23 Serializer::~Serializer() {
27 while (BlockLevel > 0)
33 void Serializer::EmitRecord() {
34 assert(Record.size() > 0 && "Cannot emit empty record.");
35 Stream.EmitRecord(8,Record);
39 void Serializer::EnterBlock(unsigned BlockID,unsigned CodeLen) {
41 Stream.EnterSubblock(BlockID,CodeLen);
45 void Serializer::ExitBlock() {
46 assert (BlockLevel > 0);
52 void Serializer::EmitInt(uint64_t X) {
53 assert (BlockLevel > 0);
57 void Serializer::EmitSInt(int64_t X) {
61 EmitInt((-X << 1) | 1);
64 void Serializer::EmitCStr(const char* s, const char* end) {
65 Record.push_back(end - s);
73 void Serializer::EmitCStr(const char* s) {
74 EmitCStr(s,s+strlen(s));
77 SerializedPtrID Serializer::getPtrId(const void* ptr) {
81 MapTy::iterator I = PtrMap.find(ptr);
83 if (I == PtrMap.end()) {
84 unsigned id = PtrMap.size()+1;
85 #ifdef DEBUG_BACKPATCH
86 errs() << "Registered PTR: " << ptr << " => " << id << "\n";
91 else return I->second;
94 bool Serializer::isRegistered(const void* ptr) const {
95 MapTy::const_iterator I = PtrMap.find(ptr);
96 return I != PtrMap.end();
100 #define INT_EMIT(TYPE)\
101 void SerializeTrait<TYPE>::Emit(Serializer&S, TYPE X) { S.EmitInt(X); }
104 INT_EMIT(unsigned char)
105 INT_EMIT(unsigned short)
106 INT_EMIT(unsigned int)
107 INT_EMIT(unsigned long)
109 #define SINT_EMIT(TYPE)\
110 void SerializeTrait<TYPE>::Emit(Serializer&S, TYPE X) { S.EmitSInt(X); }
112 SINT_EMIT(signed char)
113 SINT_EMIT(signed short)
114 SINT_EMIT(signed int)
115 SINT_EMIT(signed long)