2007-10-23 21:29:33 +00:00
|
|
|
//==- Serialize.cpp - Generic Object Serialization to Bitcode ----*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Ted Kremenek and is distributed under the
|
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the internal methods used for object serialization.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
#include "llvm/Bitcode/Serialize.h"
|
|
|
|
#include "string.h"
|
2007-10-23 21:29:33 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
Serializer::Serializer(BitstreamWriter& stream, unsigned BlockID)
|
2007-11-05 21:36:35 +00:00
|
|
|
: Stream(stream), BlockLevel(0) {
|
2007-10-23 21:29:33 +00:00
|
|
|
|
2007-11-05 21:36:35 +00:00
|
|
|
if (BlockID >= 8)
|
|
|
|
EnterBlock(8,3);
|
2007-10-23 21:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Serializer::~Serializer() {
|
|
|
|
if (inRecord())
|
|
|
|
EmitRecord();
|
|
|
|
|
2007-11-05 21:36:35 +00:00
|
|
|
while (BlockLevel > 0)
|
2007-10-23 21:29:33 +00:00
|
|
|
Stream.ExitBlock();
|
2007-11-06 19:49:50 +00:00
|
|
|
|
2007-10-23 21:29:33 +00:00
|
|
|
Stream.FlushToWord();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Serializer::EmitRecord() {
|
|
|
|
assert(Record.size() > 0 && "Cannot emit empty record.");
|
|
|
|
Stream.EmitRecord(8,Record);
|
|
|
|
Record.clear();
|
|
|
|
}
|
|
|
|
|
2007-11-05 21:36:35 +00:00
|
|
|
void Serializer::EnterBlock(unsigned BlockID,unsigned CodeLen) {
|
2007-11-06 19:49:50 +00:00
|
|
|
FlushRecord();
|
2007-11-05 21:36:35 +00:00
|
|
|
Stream.EnterSubblock(BlockID,CodeLen);
|
|
|
|
++BlockLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Serializer::ExitBlock() {
|
|
|
|
assert (BlockLevel > 0);
|
|
|
|
--BlockLevel;
|
2007-11-06 19:49:50 +00:00
|
|
|
FlushRecord();
|
2007-11-05 21:36:35 +00:00
|
|
|
Stream.ExitBlock();
|
|
|
|
}
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
void Serializer::EmitInt(unsigned X) {
|
2007-11-05 21:36:35 +00:00
|
|
|
assert (BlockLevel > 0);
|
2007-10-23 21:29:33 +00:00
|
|
|
Record.push_back(X);
|
|
|
|
}
|
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
void Serializer::EmitCStr(const char* s, const char* end) {
|
|
|
|
Record.push_back(end - s);
|
2007-10-23 21:29:33 +00:00
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
while(s != end) {
|
|
|
|
Record.push_back(*s);
|
|
|
|
++s;
|
|
|
|
}
|
2007-10-23 21:29:33 +00:00
|
|
|
|
|
|
|
EmitRecord();
|
|
|
|
}
|
2007-10-24 19:06:40 +00:00
|
|
|
|
|
|
|
void Serializer::EmitCStr(const char* s) {
|
|
|
|
EmitCStr(s,s+strlen(s));
|
|
|
|
}
|
|
|
|
|
2007-10-31 18:23:21 +00:00
|
|
|
unsigned Serializer::getPtrId(const void* ptr) {
|
2007-10-25 18:42:52 +00:00
|
|
|
if (!ptr)
|
|
|
|
return 0;
|
|
|
|
|
2007-10-25 00:10:21 +00:00
|
|
|
MapTy::iterator I = PtrMap.find(ptr);
|
|
|
|
|
|
|
|
if (I == PtrMap.end()) {
|
2007-10-25 18:42:52 +00:00
|
|
|
unsigned id = PtrMap.size()+1;
|
2007-10-25 00:10:21 +00:00
|
|
|
PtrMap[ptr] = id;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
else return I->second;
|
|
|
|
}
|
|
|
|
|
2007-11-05 21:36:35 +00:00
|
|
|
|
2007-10-24 19:06:40 +00:00
|
|
|
#define INT_EMIT(TYPE)\
|
|
|
|
void SerializeTrait<TYPE>::Emit(Serializer&S, TYPE X) { S.EmitInt(X); }
|
|
|
|
|
|
|
|
INT_EMIT(bool)
|
|
|
|
INT_EMIT(unsigned char)
|
|
|
|
INT_EMIT(unsigned short)
|
|
|
|
INT_EMIT(unsigned int)
|
|
|
|
INT_EMIT(unsigned long)
|