aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Bitcode/Writer/Serialize.cpp
blob: fbe1d1007c9f054f04dd6b0c745d7e8f272de29e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//==- 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.
//
//===----------------------------------------------------------------------===//

#include "llvm/Bitcode/Serialize.h"
#include "string.h"

using namespace llvm;

Serializer::Serializer(BitstreamWriter& stream, unsigned BlockID)
  : Stream(stream), inBlock(BlockID >= 8) {
    
  if (inBlock) Stream.EnterSubblock(8,3);
}

Serializer::~Serializer() {
  if (inRecord())
    EmitRecord();

  if (inBlock)
    Stream.ExitBlock();
  
  Stream.FlushToWord();
}

void Serializer::EmitRecord() {
  assert(Record.size() > 0 && "Cannot emit empty record.");
  Stream.EmitRecord(8,Record);
  Record.clear();
}

void Serializer::EmitInt(unsigned X) {
  Record.push_back(X);
}

void Serializer::EmitCStr(const char* s, const char* end) {
  Record.push_back(end - s);
  
  while(s != end) {
    Record.push_back(*s);
    ++s;
  }

  EmitRecord();
}

void Serializer::EmitCStr(const char* s) {
  EmitCStr(s,s+strlen(s));
}

unsigned Serializer::getPtrId(void* ptr) {
  MapTy::iterator I = PtrMap.find(ptr);
  
  if (I == PtrMap.end()) {
    unsigned id = PtrMap.size();
    PtrMap[ptr] = id;
    return id;
  }
  else return I->second;
}

#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)