aboutsummaryrefslogtreecommitdiff
path: root/lib/Bitcode/Writer/Serialize.cpp
blob: 9fbb97de69f97c09dd6ec24b7c2adbd5e6f736ee (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
//==- 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/Serialization.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, unsigned bits) {
  Record.push_back(X);
}

void Serializer::EmitCString(const char* cstr) {
  unsigned l = strlen(cstr);
  Record.push_back(l);
  
  for (unsigned i = 0; i < l; i++)
    Record.push_back(cstr[i]);

  EmitRecord();
}