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, unsigned BlockID)
20 : Stream(stream), BlockLevel(0) {
26 Serializer::~Serializer() {
30 while (BlockLevel > 0)
36 void Serializer::EmitRecord() {
37 assert(Record.size() > 0 && "Cannot emit empty record.");
38 Stream.EmitRecord(8,Record);
42 void Serializer::EnterBlock(unsigned BlockID,unsigned CodeLen) {
44 Stream.EnterSubblock(BlockID,CodeLen);
48 void Serializer::ExitBlock() {
49 assert (BlockLevel > 0);
55 void Serializer::EmitInt(unsigned X) {
56 assert (BlockLevel > 0);
60 void Serializer::EmitCStr(const char* s, const char* end) {
61 Record.push_back(end - s);
71 void Serializer::EmitCStr(const char* s) {
72 EmitCStr(s,s+strlen(s));
75 unsigned Serializer::getPtrId(const void* ptr) {
79 MapTy::iterator I = PtrMap.find(ptr);
81 if (I == PtrMap.end()) {
82 unsigned id = PtrMap.size()+1;
86 else return I->second;
90 #define INT_EMIT(TYPE)\
91 void SerializeTrait<TYPE>::Emit(Serializer&S, TYPE X) { S.EmitInt(X); }
94 INT_EMIT(unsigned char)
95 INT_EMIT(unsigned short)
96 INT_EMIT(unsigned int)
97 INT_EMIT(unsigned long)