1 //==- Serialize.cpp - Generic Object Serialization to Bitcode ----*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Ted Kremenek and is distributed under the
6 // University of Illinois Open Source 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"
19 Serializer::Serializer(BitstreamWriter& stream)
20 : Stream(stream), BlockLevel(0) {}
22 Serializer::~Serializer() {
26 while (BlockLevel > 0)
32 void Serializer::EmitRecord() {
33 assert(Record.size() > 0 && "Cannot emit empty record.");
34 Stream.EmitRecord(8,Record);
38 void Serializer::EnterBlock(unsigned BlockID,unsigned CodeLen) {
40 Stream.EnterSubblock(BlockID,CodeLen);
44 void Serializer::ExitBlock() {
45 assert (BlockLevel > 0);
51 void Serializer::EmitInt(unsigned X) {
52 assert (BlockLevel > 0);
56 void Serializer::EmitCStr(const char* s, const char* end) {
57 Record.push_back(end - s);
67 void Serializer::EmitCStr(const char* s) {
68 EmitCStr(s,s+strlen(s));
71 unsigned Serializer::getPtrId(const void* ptr) {
75 MapTy::iterator I = PtrMap.find(ptr);
77 if (I == PtrMap.end()) {
78 unsigned id = PtrMap.size()+1;
82 else return I->second;
86 #define INT_EMIT(TYPE)\
87 void SerializeTrait<TYPE>::Emit(Serializer&S, TYPE X) { S.EmitInt(X); }
90 INT_EMIT(unsigned char)
91 INT_EMIT(unsigned short)
92 INT_EMIT(unsigned int)
93 INT_EMIT(unsigned long)