/
outbuf.d
329 lines (279 loc) · 6.71 KB
/
outbuf.d
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1994-1998 by Symantec
* Copyright (C) 2000-2020 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/outbuf.d, backend/outbuf.d)
* Documentation: https://dlang.org/phobos/dmd_backend_outbuf.html
*/
module dmd.backend.outbuf;
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
// Output buffer
// (This used to be called OutBuffer, renamed to avoid name conflicts with Mars.)
extern (C++):
private nothrow void err_nomem();
struct Outbuffer
{
ubyte *buf; // the buffer itself
ubyte *pend; // pointer past the end of the buffer
private ubyte *p; // current position in buffer
nothrow:
this(size_t initialSize)
{
reserve(initialSize);
}
//~this() { dtor(); }
void dtor()
{
if (auto slice = this.extractSlice())
free(slice.ptr);
}
void reset()
{
p = buf;
}
// Returns: A slice to the data written so far
extern(D) inout(ubyte)[] opSlice(size_t from, size_t to) inout
@trusted pure nothrow @nogc
{
assert(this.buf, "Attempt to dereference a null pointer");
assert(from < to, "First index must be <= to second one");
assert(this.length() <= (to - from), "Out of bound access");
return this.buf[from .. to];
}
/// Ditto
extern(D) inout(ubyte)[] opSlice() inout @trusted pure nothrow @nogc
{
return this.buf[0 .. this.p - this.buf];
}
extern(D) ubyte[] extractSlice() @safe pure nothrow @nogc
{
auto ret = this[];
this.buf = this.p = this.pend = null;
return ret;
}
// Make sure we have at least `nbyte` available for writting
void reserve(size_t nbytes)
{
const size_t oldlen = pend - buf;
const size_t used = p - buf;
size_t len = used + nbytes;
// No need to reallocate
if (nbytes < (pend - p))
return;
const size_t newlen = oldlen + (oldlen >> 1); // oldlen * 1.5
if (len < newlen)
len = newlen;
len = (len + 15) & ~15;
buf = cast(ubyte*) realloc(buf,len);
if (!buf)
err_nomem();
pend = buf + len;
p = buf + used;
}
// Write n zeros; return pointer to start of zeros
void *writezeros(size_t n)
{
reserve(n);
void *pstart = memset(p,0,n);
p += n;
return pstart;
}
// Position buffer to accept the specified number of bytes at offset
void position(size_t offset, size_t nbytes)
{
if (offset + nbytes > pend - buf)
{
reserve(offset + nbytes - (p - buf));
}
p = buf + offset;
debug assert(buf <= p);
debug assert(p <= pend);
debug assert(p + nbytes <= pend);
}
// Write an array to the buffer, no reserve check
void writen(const void *b, size_t len)
{
memcpy(p,b,len);
p += len;
}
// Write an array to the buffer.
extern (D)
void write(const(void)[] b)
{
reserve(b.length);
memcpy(p, b.ptr, b.length);
p += b.length;
}
void write(const(void)* b, size_t len)
{
write(b[0 .. len]);
}
/**
* Writes an 8 bit byte, no reserve check.
*/
void writeByten(ubyte v)
{
*p++ = v;
}
/**
* Writes an 8 bit byte.
*/
void writeByte(int v)
{
reserve(1);
*p++ = cast(ubyte)v;
}
/**
* Writes a 16 bit value, no reserve check.
*/
void write16n(int v)
{
*(cast(ushort *) p) = cast(ushort)v;
p += 2;
}
/**
* Writes a 16 bit value.
*/
void write16(int v)
{
reserve(2);
write16n(v);
}
/**
* Writes a 32 bit int.
*/
void write32(int v)
{
reserve(4);
*cast(int *)p = v;
p += 4;
}
/**
* Writes a 64 bit long.
*/
void write64(long v)
{
reserve(8);
*cast(long *)p = v;
p += 8;
}
/**
* Writes a 32 bit float.
*/
void writeFloat(float v)
{
reserve(float.sizeof);
*cast(float *)p = v;
p += float.sizeof;
}
/**
* Writes a 64 bit double.
*/
void writeDouble(double v)
{
reserve(double.sizeof);
*cast(double *)p = v;
p += double.sizeof;
}
/**
* Writes a String as a sequence of bytes.
*/
void write(const(char)* s)
{
write(s[0 .. strlen(s)]);
}
/**
* Writes a 0 terminated String
*/
void writeString(const(char)* s)
{
write(s[0 .. strlen(s)+1]);
}
/// Ditto
extern(D) void writeString(const(char)[] s)
{
write(s);
writeByte(0);
}
/// Disembiguation for `string`
extern(D) void writeString(string s)
{
writeString(cast(const(char)[])(s));
}
/**
* Inserts string at beginning of buffer.
*/
void prependBytes(const(char)* s)
{
prepend(s, strlen(s));
}
/**
* Inserts bytes at beginning of buffer.
*/
void prepend(const(void)* b, size_t len)
{
reserve(len);
memmove(buf + len,buf,p - buf);
memcpy(buf,b,len);
p += len;
}
/**
* Bracket buffer contents with c1 and c2.
*/
void bracket(char c1,char c2)
{
reserve(2);
memmove(buf + 1,buf,p - buf);
buf[0] = c1;
p[1] = c2;
p += 2;
}
/**
* Returns the number of bytes written.
*/
size_t length() const @safe pure nothrow @nogc
{
return p - buf;
}
/**
* Set current size of buffer.
*/
void setsize(size_t size)
{
p = buf + size;
//debug assert(buf <= p);
//debug assert(p <= pend);
}
void writesLEB128(int value)
{
while (1)
{
ubyte b = value & 0x7F;
value >>= 7; // arithmetic right shift
if (value == 0 && !(b & 0x40) ||
value == -1 && (b & 0x40))
{
writeByte(b);
break;
}
writeByte(b | 0x80);
}
}
void writeuLEB128(uint value)
{
do
{
ubyte b = value & 0x7F;
value >>= 7;
if (value)
b |= 0x80;
writeByte(b);
} while (value);
}
}