summaryrefslogtreecommitdiff
path: root/include/llvm/Support/OutputBuffer.h
blob: 0fedb15e40507a64faf9edef7d9fbc2f832488bf (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//=== OutputBuffer.h - Output Buffer ----------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Methods to output values to a data buffer.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_OUTPUTBUFFER_H
#define LLVM_SUPPORT_OUTPUTBUFFER_H

#include <string>
#include <vector>

namespace llvm {
  
  class OutputBuffer {
    /// Output buffer.
    std::vector<unsigned char> &Output;

    /// is64Bit/isLittleEndian - This information is inferred from the target
    /// machine directly, indicating what header values and flags to set.
    bool is64Bit, isLittleEndian;
  public:
    OutputBuffer(std::vector<unsigned char> &Out,
                 bool is64bit, bool le)
      : Output(Out), is64Bit(is64bit), isLittleEndian(le) {}

    // align - Emit padding into the file until the current output position is
    // aligned to the specified power of two boundary.
    void align(unsigned Boundary) {
      assert(Boundary && (Boundary & (Boundary - 1)) == 0 &&
             "Must align to 2^k boundary");
      size_t Size = Output.size();
      
      if (Size & (Boundary - 1)) {
        // Add padding to get alignment to the correct place.
        size_t Pad = Boundary - (Size & (Boundary - 1));
        Output.resize(Size + Pad);
      }
    }

    //===------------------------------------------------------------------===//
    // Out Functions - Output the specified value to the data buffer.

    void outbyte(unsigned char X) {
      Output.push_back(X);
    }
    void outhalf(unsigned short X) {
      if (isLittleEndian) {
        Output.push_back(X & 255);
        Output.push_back(X >> 8);
      } else {
        Output.push_back(X >> 8);
        Output.push_back(X & 255);
      }
    }
    void outword(unsigned X) {
      if (isLittleEndian) {
        Output.push_back((X >>  0) & 255);
        Output.push_back((X >>  8) & 255);
        Output.push_back((X >> 16) & 255);
        Output.push_back((X >> 24) & 255);
      } else {
        Output.push_back((X >> 24) & 255);
        Output.push_back((X >> 16) & 255);
        Output.push_back((X >>  8) & 255);
        Output.push_back((X >>  0) & 255);
      }
    }
    void outxword(uint64_t X) {
      if (isLittleEndian) {
        Output.push_back(unsigned(X >>  0) & 255);
        Output.push_back(unsigned(X >>  8) & 255);
        Output.push_back(unsigned(X >> 16) & 255);
        Output.push_back(unsigned(X >> 24) & 255);
        Output.push_back(unsigned(X >> 32) & 255);
        Output.push_back(unsigned(X >> 40) & 255);
        Output.push_back(unsigned(X >> 48) & 255);
        Output.push_back(unsigned(X >> 56) & 255);
      } else {
        Output.push_back(unsigned(X >> 56) & 255);
        Output.push_back(unsigned(X >> 48) & 255);
        Output.push_back(unsigned(X >> 40) & 255);
        Output.push_back(unsigned(X >> 32) & 255);
        Output.push_back(unsigned(X >> 24) & 255);
        Output.push_back(unsigned(X >> 16) & 255);
        Output.push_back(unsigned(X >>  8) & 255);
        Output.push_back(unsigned(X >>  0) & 255);
      }
    }
    void outaddr32(unsigned X) {
      outword(X);
    }
    void outaddr64(uint64_t X) {
      outxword(X);
    }
    void outaddr(uint64_t X) {
      if (!is64Bit)
        outword((unsigned)X);
      else
        outxword(X);
    }
    void outstring(const std::string &S, unsigned Length) {
      unsigned len_to_copy = static_cast<unsigned>(S.length()) < Length
        ? static_cast<unsigned>(S.length()) : Length;
      unsigned len_to_fill = static_cast<unsigned>(S.length()) < Length
        ? Length - static_cast<unsigned>(S.length()) : 0;
      
      for (unsigned i = 0; i < len_to_copy; ++i)
        outbyte(S[i]);
      
      for (unsigned i = 0; i < len_to_fill; ++i)
        outbyte(0);
    }

    //===------------------------------------------------------------------===//
    // Fix Functions - Replace an existing entry at an offset.

    void fixhalf(unsigned short X, unsigned Offset) {
      unsigned char *P = &Output[Offset];
      P[0] = (X >> (isLittleEndian ?  0 : 8)) & 255;
      P[1] = (X >> (isLittleEndian ?  8 : 0)) & 255;
    }
    void fixword(unsigned X, unsigned Offset) {
      unsigned char *P = &Output[Offset];
      P[0] = (X >> (isLittleEndian ?  0 : 24)) & 255;
      P[1] = (X >> (isLittleEndian ?  8 : 16)) & 255;
      P[2] = (X >> (isLittleEndian ? 16 :  8)) & 255;
      P[3] = (X >> (isLittleEndian ? 24 :  0)) & 255;
    }
    void fixaddr(uint64_t X, unsigned Offset) {
      if (!is64Bit)
        fixword((unsigned)X, Offset);
      else
        assert(0 && "Emission of 64-bit data not implemented yet!");
    }

    unsigned char &operator[](unsigned Index) {
      return Output[Index];
    }
    const unsigned char &operator[](unsigned Index) const {
      return Output[Index];
    }
  };
  
} // end llvm namespace

#endif // LLVM_SUPPORT_OUTPUTBUFFER_H