Mercurial > projects > ddmd
annotate dmd/OutBuffer.d @ 68:ee3a9f34dc48
final bits of codegen implementation to compile Phobos
author | korDen |
---|---|
date | Tue, 24 Aug 2010 16:44:34 +0400 |
parents | 544b922227c7 |
children | df6d0f967680 |
rev | line source |
---|---|
0 | 1 module dmd.OutBuffer; |
2 | |
3 import std.stdarg; | |
34 | 4 import std.exception; |
0 | 5 |
6 import core.stdc.stdlib; | |
7 import core.stdc.string; | |
8 | |
4 | 9 import core.memory; |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
10 import core.stdc.stdlib; |
2 | 11 |
0 | 12 class OutBuffer |
13 { | |
14 ubyte* data; | |
15 uint offset; | |
16 uint size; | |
17 | |
18 this() | |
19 { | |
20 // do nothing | |
21 } | |
22 | |
23 final void* extractData() | |
24 { | |
25 void* p = cast(void*)data; | |
26 | |
27 data = null; | |
28 offset = 0; | |
29 size = 0; | |
30 | |
31 return p; | |
32 } | |
33 | |
34 void mark() | |
35 { | |
36 assert(false); | |
37 } | |
38 | |
39 final void reserve(uint nbytes) | |
40 { | |
41 //printf("OutBuffer::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes); | |
42 if (size - offset < nbytes) | |
43 { | |
44 size = (offset + nbytes) * 2; | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
45 data = cast(ubyte*)realloc(data, size); |
0 | 46 } |
47 } | |
48 | |
49 final void setsize(uint size) | |
50 { | |
51 assert(false); | |
52 } | |
53 | |
54 final void reset() | |
55 { | |
56 offset = 0; | |
57 } | |
58 | |
59 final void write(const(void)* data, uint nbytes) | |
60 { | |
61 reserve(nbytes); | |
62 memcpy(this.data + offset, data, nbytes); | |
63 offset += nbytes; | |
64 } | |
65 | |
66 final void writebstring(ubyte* string_) | |
67 { | |
68 assert(false); | |
69 } | |
70 | |
71 final void writestring(const(char)[] string_) | |
72 { | |
73 write(string_.ptr , string_.length); | |
74 } | |
75 | |
76 final void writedstring(const(char)* string_) | |
77 { | |
78 assert(false); | |
79 } | |
80 | |
81 final void writedstring(const(wchar)* string_) | |
82 { | |
83 assert(false); | |
84 } | |
85 | |
86 final void prependstring(const(char)[] string_) | |
87 { | |
88 uint len = string_.length; | |
89 reserve(len); | |
90 memmove(data + len, data, offset); | |
91 memcpy(data, string_.ptr, len); | |
92 offset += len; | |
93 } | |
94 | |
95 final void writenl() // write newline | |
96 { | |
97 assert(false); | |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
98 version (_WIN32) { |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
99 version (M_UNICODE) { |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
100 write4(0x000A000D); // newline is CR,LF on Microsoft OS's |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
101 } else { |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
102 writeword(0x0A0D); // newline is CR,LF on Microsoft OS's |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
103 } |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
104 } else { |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
105 version (M_UNICODE) { |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
106 writeword('\n'); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
107 } else { |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
108 writeByte('\n'); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
109 } |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
110 } |
0 | 111 } |
112 | |
113 final void writeByte(uint b) | |
114 { | |
115 reserve(1); | |
116 this.data[offset] = cast(ubyte)b; | |
117 offset++; | |
118 } | |
119 | |
120 final void writebyte(uint b) { writeByte(b); } | |
121 | |
122 final void writeUTF8(uint b) | |
123 { | |
124 reserve(6); | |
125 if (b <= 0x7F) | |
126 { | |
127 this.data[offset] = cast(ubyte)b; | |
128 offset++; | |
129 } | |
130 else if (b <= 0x7FF) | |
131 { | |
132 this.data[offset + 0] = cast(ubyte)((b >> 6) | 0xC0); | |
133 this.data[offset + 1] = cast(ubyte)((b & 0x3F) | 0x80); | |
134 offset += 2; | |
135 } | |
136 else if (b <= 0xFFFF) | |
137 { | |
138 this.data[offset + 0] = cast(ubyte)((b >> 12) | 0xE0); | |
139 this.data[offset + 1] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
140 this.data[offset + 2] = cast(ubyte)((b & 0x3F) | 0x80); | |
141 offset += 3; | |
142 } | |
143 else if (b <= 0x1FFFFF) | |
144 { | |
145 this.data[offset + 0] = cast(ubyte)((b >> 18) | 0xF0); | |
146 this.data[offset + 1] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
147 this.data[offset + 2] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
148 this.data[offset + 3] = cast(ubyte)((b & 0x3F) | 0x80); | |
149 offset += 4; | |
150 } | |
151 else if (b <= 0x3FFFFFF) | |
152 { | |
153 this.data[offset + 0] = cast(ubyte)((b >> 24) | 0xF8); | |
154 this.data[offset + 1] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80); | |
155 this.data[offset + 2] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
156 this.data[offset + 3] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
157 this.data[offset + 4] = cast(ubyte)((b & 0x3F) | 0x80); | |
158 offset += 5; | |
159 } | |
160 else if (b <= 0x7FFFFFFF) | |
161 { | |
162 this.data[offset + 0] = cast(ubyte)((b >> 30) | 0xFC); | |
163 this.data[offset + 1] = cast(ubyte)(((b >> 24) & 0x3F) | 0x80); | |
164 this.data[offset + 2] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80); | |
165 this.data[offset + 3] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
166 this.data[offset + 4] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
167 this.data[offset + 5] = cast(ubyte)((b & 0x3F) | 0x80); | |
168 offset += 6; | |
169 } | |
170 else | |
171 assert(0); | |
172 } | |
173 | |
174 final void writedchar(uint b) | |
175 { | |
176 assert(false); | |
177 } | |
178 | |
179 final void prependbyte(uint b) | |
180 { | |
181 assert(false); | |
182 } | |
183 | |
184 final void writeword(uint w) | |
185 { | |
186 reserve(2); | |
187 *cast(ushort*)(this.data + offset) = cast(ushort)w; | |
188 offset += 2; | |
189 } | |
190 | |
191 final void writeUTF16(uint w) | |
192 { | |
193 reserve(4); | |
194 if (w <= 0xFFFF) | |
195 { | |
196 *cast(ushort*)(this.data + offset) = cast(ushort)w; | |
197 offset += 2; | |
198 } | |
199 else if (w <= 0x10FFFF) | |
200 { | |
201 *cast(ushort*)(this.data + offset) = cast(ushort)((w >> 10) + 0xD7C0); | |
202 *cast(ushort*)(this.data + offset + 2) = cast(ushort)((w & 0x3FF) | 0xDC00); | |
203 offset += 4; | |
204 } | |
205 else | |
206 assert(0); | |
207 } | |
208 | |
209 final void write4(uint w) | |
210 { | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
211 reserve(4); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
212 *cast(uint*)(this.data + offset) = w; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
213 offset += 4; |
0 | 214 } |
215 | |
216 final void write(OutBuffer buf) | |
217 { | |
218 if (buf) | |
219 { | |
220 reserve(buf.offset); | |
221 memcpy(data + offset, buf.data, buf.offset); | |
222 offset += buf.offset; | |
223 } | |
224 } | |
225 | |
226 final void write(Object obj) | |
227 { | |
228 assert(false); | |
229 } | |
230 | |
231 final void fill0(uint nbytes) | |
232 { | |
233 reserve(nbytes); | |
234 memset(data + offset, 0, nbytes); | |
235 offset += nbytes; | |
236 } | |
237 | |
238 final void align_(uint size) | |
239 { | |
240 assert(false); | |
241 } | |
242 | |
243 void vprintf(const(char)* format, va_list args) | |
244 { | |
245 assert(false); | |
246 } | |
247 | |
248 void printf(T...)(string format, T t) | |
249 { | |
250 string s = std.string.format(format, t); | |
251 writestring(s); | |
252 } | |
253 | |
254 version (M_UNICODE) { | |
255 /// void vprintf(const uint short *format, va_list args); | |
256 /// void printf(const uint short *format, ...); | |
257 } | |
258 final void bracket(char left, char right) | |
259 { | |
260 assert(false); | |
261 } | |
262 | |
263 final uint bracket(uint i, const(char)* left, uint j, const(char)* right) | |
264 { | |
265 assert(false); | |
266 } | |
267 | |
268 final void spread(uint offset, uint nbytes) | |
269 { | |
270 assert(false); | |
271 } | |
272 | |
273 final uint insert(uint offset, const(void)* data, uint nbytes) | |
274 { | |
275 assert(false); | |
276 } | |
277 | |
278 final void remove(uint offset, uint nbytes) | |
279 { | |
280 assert(false); | |
281 } | |
282 | |
283 string toChars() | |
284 { | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
285 char[] s = getString(); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
286 char* copy = cast(char*)malloc(s.length); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
287 memcpy(copy, s.ptr, s.length); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
288 return assumeUnique(copy[0..s.length]); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
289 |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
290 //return getString().idup; |
0 | 291 } |
292 | |
293 final string extractString() | |
294 { | |
295 char[] s = getString(); | |
296 data = null; | |
297 offset = 0; | |
298 size = 0; | |
299 | |
300 return assumeUnique(s); | |
301 } | |
302 | |
303 final char[] getString() | |
304 { | |
305 char* s = cast(char*)data; | |
306 return s[0..offset]; | |
307 } | |
308 } |