aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Bitcode/Writer/Serialize.cpp
blob: 4395b90a89dd90941fe6894853e5eb5916a046ee (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//==- 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)
  : Stream(stream), BlockLevel(0) {}

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

  while (BlockLevel > 0)
    Stream.ExitBlock();
   
  Stream.FlushToWord();
}

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

void Serializer::EnterBlock(unsigned BlockID,unsigned CodeLen) {
  FlushRecord();
  Stream.EnterSubblock(BlockID,CodeLen);
  ++BlockLevel;
}

void Serializer::ExitBlock() {
  assert (BlockLevel > 0);
  --BlockLevel;
  FlushRecord();
  Stream.ExitBlock();
}

void Serializer::EmitInt(unsigned X) {
  assert (BlockLevel > 0);
  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(const void* ptr) {
  if (!ptr)
    return 0;
  
  MapTy::iterator I = PtrMap.find(ptr);
  
  if (I == PtrMap.end()) {
    unsigned id = PtrMap.size()+1;
    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)