authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-23 02:00:23-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-23 02:00:23-04:00
log46eb77dbb200756b96bfae4c5166397fefba66d0
tree618f0117563dfabbe0e4253f6fa817e526b5a64c
parent4b902b44a244e29106637f9777aa71b1cd4c22d2

stack trace is able to figure out compilation unit

each address is contained within also fix a bug having to do with codegen for enum value initialization expressions

9 files changed, 376 insertions(+), 174 deletions(-)

src/codegen.cpp+1-1
......@@ -729,7 +729,6 @@ static LLVMValueRef gen_enum_value_expr(CodeGen *g, AstNode *node, TypeTableEntr
729729 LLVMValueRef new_union_val = gen_expr(g, arg_node);
730730 if (arg_node) {
731731 arg_node_type = get_expr_type(arg_node);
732 new_union_val = gen_expr(g, arg_node);
733732 } else {
734733 arg_node_type = g->builtin_types.entry_void;
735734 }
......@@ -3460,6 +3459,7 @@ static LLVMValueRef gen_switch_expr(CodeGen *g, AstNode *node) {
34603459 zig_unreachable();
34613460 }
34623461 if (make_item_blocks) {
3462 set_debug_source_node(g, var_node);
34633463 LLVMBuildBr(g->builder, prong_block);
34643464 }
34653465 } else {
std/cstr.zig+19-17
......@@ -38,33 +38,37 @@ pub struct CBuf {
3838 list: List(u8),
3939
4040 /// Must deinitialize with deinit.
41 pub fn init(self: &CBuf, allocator: &Allocator) {
42 self.list.init(allocator);
43 // This resize is guaranteed to not have an error because we use a list
44 // with preallocated memory of at least 1 byte.
45 %%self.resize(0);
41 pub fn initEmpty(allocator: &Allocator) -> %CBuf {
42 const self = CBuf {
43 .list = List(u8).init(allocator),
44 };
45 %return self.resize(0);
46 return self;
4647 }
4748
4849 /// Must deinitialize with deinit.
49 pub fn initFromMem(self: &CBuf, allocator: &Allocator, m: []const u8) -> %void {
50 self.init(allocator);
50 pub fn initFromMem(allocator: &Allocator, m: []const u8) -> %CBuf {
51 const self = CBuf {
52 .list = List(u8).init(allocator),
53 };
5154 %return self.resize(m.len);
5255 mem.copy(u8, self.list.items, m);
56 return self;
5357 }
5458
5559 /// Must deinitialize with deinit.
56 pub fn initFromCStr(self: &CBuf, allocator: &Allocator, s: &const u8) -> %void {
57 self.initFromMem(allocator, s[0...strlen(s)])
60 pub fn initFromCStr(allocator: &Allocator, s: &const u8) -> %CBuf {
61 return CBuf.initFromMem(allocator, s[0...strlen(s)]);
5862 }
5963
6064 /// Must deinitialize with deinit.
61 pub fn initFromCBuf(self: &CBuf, cbuf: &const CBuf) -> %void {
62 self.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()])
65 pub fn initFromCBuf(cbuf: &const CBuf) -> %CBuf {
66 return CBuf.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()]);
6367 }
6468
6569 /// Must deinitialize with deinit.
66 pub fn initFromSlice(self: &CBuf, other: &const CBuf, start: usize, end: usize) -> %void {
67 self.initFromMem(other.list.allocator, other.list.items[start...end])
70 pub fn initFromSlice(other: &const CBuf, start: usize, end: usize) -> %CBuf {
71 return CBuf.initFromMem(other.list.allocator, other.list.items[start...end]);
6872 }
6973
7074 pub fn deinit(self: &CBuf) {
......@@ -124,8 +128,7 @@ pub struct CBuf {
124128
125129#attribute("test")
126130fn testSimpleCBuf() {
127 var buf: CBuf = undefined;
128 buf.init(&debug.global_allocator);
131 var buf = %%CBuf.initEmpty(&debug.global_allocator);
129132 assert(buf.len() == 0);
130133 %%buf.appendCStr(c"hello");
131134 %%buf.appendChar(' ');
......@@ -133,8 +136,7 @@ fn testSimpleCBuf() {
133136 assert(buf.eqlCStr(c"hello world"));
134137 assert(buf.eqlMem("hello world"));
135138
136 var buf2: CBuf = undefined;
137 %%buf2.initFromCBuf(&buf);
139 var buf2 = %%CBuf.initFromCBuf(&buf);
138140 assert(buf.eqlCBuf(&buf2));
139141
140142 assert(buf.startsWithMem("hell"));
std/debug.zig+263-112
......@@ -1,4 +1,4 @@
1const Allocator = @import("mem.zig").Allocator;
1const mem = @import("mem.zig");
22const io = @import("io.zig");
33const os = @import("os.zig");
44const elf = @import("elf.zig");
......@@ -21,28 +21,38 @@ pub fn printStackTrace() -> %void {
2121pub fn writeStackTrace(out_stream: &io.OutStream) -> %void {
2222 switch (@compileVar("object_format")) {
2323 elf => {
24 var st: ElfStackTrace = undefined;
24 var stack_trace = ElfStackTrace {
25 .self_exe_stream = undefined,
26 .elf = undefined,
27 .debug_info = undefined,
28 .debug_abbrev = undefined,
29 .debug_str = undefined,
30 .abbrev_table_list = List(AbbrevTableHeader).init(&global_allocator),
31 .compile_unit_list = List(CompileUnit).init(&global_allocator),
32 };
33 const st = &stack_trace;
2534 %return io.openSelfExe(&st.self_exe_stream);
26 defer %return st.self_exe_stream.close();
35 defer st.self_exe_stream.close() %% {};
2736
2837 %return st.elf.openStream(&global_allocator, &st.self_exe_stream);
2938 defer %return st.elf.close();
3039
31 st.aranges = %return st.elf.findSection(".debug_aranges");
3240 st.debug_info = (%return st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo;
3341 st.debug_abbrev = (%return st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo;
42 st.debug_str = (%return st.elf.findSection(".debug_str")) ?? return error.MissingDebugInfo;
43 %return scanAllCompileUnits(st);
3444
3545 var maybe_fp: ?&const u8 = @frameAddress();
3646 while (true) {
3747 const fp = maybe_fp ?? break;
3848 const return_address = *(&const usize)(usize(fp) + @sizeOf(usize));
3949
40 // read .debug_aranges to find out which compile unit the address is in
41 const compile_unit_offset = %return findCompileUnitOffset(&st, return_address);
50 const compile_unit = findCompileUnit(st, return_address) ?? return error.MissingDebugInfo;
51 const name = %return compile_unit.die.getAttrString(st, DW.AT_name);
4252
4353 %return out_stream.printInt(usize, return_address);
4454 %return out_stream.printf(" -> ");
45 %return out_stream.printInt(u64, compile_unit_offset);
55 %return out_stream.printf(name);
4656 %return out_stream.printf("\n");
4757 maybe_fp = *(&const ?&const u8)(fp);
4858 }
......@@ -62,9 +72,38 @@ pub fn writeStackTrace(out_stream: &io.OutStream) -> %void {
6272struct ElfStackTrace {
6373 self_exe_stream: io.InStream,
6474 elf: elf.Elf,
65 aranges: ?&elf.SectionHeader,
6675 debug_info: &elf.SectionHeader,
6776 debug_abbrev: &elf.SectionHeader,
77 debug_str: &elf.SectionHeader,
78 abbrev_table_list: List(AbbrevTableHeader),
79 compile_unit_list: List(CompileUnit),
80}
81
82struct CompileUnit {
83 is_64: bool,
84 die: &Die,
85 pc_start: u64,
86 pc_end: u64,
87}
88
89const AbbrevTable = List(AbbrevTableEntry);
90
91struct AbbrevTableHeader {
92 // offset from .debug_abbrev
93 offset: u64,
94 table: AbbrevTable,
95}
96
97struct AbbrevTableEntry {
98 has_children: bool,
99 abbrev_code: u64,
100 tag_id: u64,
101 attrs: List(AbbrevAttr),
102}
103
104struct AbbrevAttr {
105 attr_id: u64,
106 form_id: u64,
68107}
69108
70109enum FormValue {
......@@ -84,8 +123,76 @@ enum FormValue {
84123struct Constant {
85124 payload: []u8,
86125 signed: bool,
126
127 fn asUnsignedLe(self: &const Constant) -> %u64 {
128 if (self.payload.len > @sizeOf(u64))
129 return error.InvalidDebugInfo;
130 if (self.signed)
131 return error.InvalidDebugInfo;
132 return mem.sliceAsInt(self.payload, false, u64);
133 }
134}
135
136struct Die {
137 tag_id: u64,
138 has_children: bool,
139 attrs: List(Attr),
140
141 struct Attr {
142 id: u64,
143 value: FormValue,
144 }
145
146 fn getAttr(self: &const Die, id: u64) -> ?&const FormValue {
147 for (self.attrs.toSlice()) |*attr| {
148 if (attr.id == id)
149 return &attr.value;
150 }
151 return null;
152 }
153
154 fn getAttrAddr(self: &const Die, id: u64) -> %u64 {
155 const form_value = self.getAttr(id) ?? return error.InvalidDebugInfo;
156 return switch (*form_value) {
157 Address => |value| value,
158 else => error.InvalidDebugInfo,
159 };
160 }
161
162 fn getAttrUnsignedLe(self: &const Die, id: u64) -> %u64 {
163 const form_value = self.getAttr(id) ?? return error.InvalidDebugInfo;
164 return switch (*form_value) {
165 Const => |value| value.asUnsignedLe(),
166 else => error.InvalidDebugInfo,
167 };
168 }
169
170 fn getAttrString(self: &const Die, st: &ElfStackTrace, id: u64) -> %[]u8 {
171 const form_value = self.getAttr(id) ?? return error.InvalidDebugInfo;
172 return switch (*form_value) {
173 String => |value| value,
174 StrPtr => |offset| getString(st, offset),
175 else => error.InvalidDebugInfo,
176 }
177 }
87178}
88179
180fn readString(in_stream: &io.InStream) -> %[]u8 {
181 var buf = List(u8).init(&global_allocator);
182 while (true) {
183 const byte = %return in_stream.readByte();
184 if (byte == 0)
185 break;
186 %return buf.append(byte);
187 }
188 return buf.items;
189}
190
191fn getString(st: &ElfStackTrace, offset: u64) -> %[]u8 {
192 const pos = st.debug_str.offset + offset;
193 %return st.self_exe_stream.seekTo(pos);
194 return readString(&st.self_exe_stream);
195}
89196
90197fn readAllocBytes(in_stream: &io.InStream, size: usize) -> %[]u8 {
91198 const buf = %return global_allocator.alloc(u8, size);
......@@ -99,25 +206,19 @@ fn parseFormValueBlockLen(in_stream: &io.InStream, size: usize) -> %FormValue {
99206 return FormValue.Block { buf };
100207}
101208
102fn parseFormValueBlock(in_stream: &io.InStream, inline T: type) -> %FormValue {
103 const block_len = %return in_stream.readIntLe(T);
209fn parseFormValueBlock(in_stream: &io.InStream, size: usize) -> %FormValue {
210 const block_len = %return in_stream.readVarInt(false, usize, size);
104211 return parseFormValueBlockLen(in_stream, block_len);
105212}
106213
107fn parseFormValueConstantLen(in_stream: &io.InStream, signed: bool, size: usize) -> %FormValue {
108 const buf = %return readAllocBytes(in_stream, size);
109 return FormValue.Const { Constant {
214fn parseFormValueConstant(in_stream: &io.InStream, signed: bool, size: usize) -> %FormValue {
215 FormValue.Const { Constant {
110216 .signed = signed,
111 .payload = buf,
112 }};
217 .payload = %return readAllocBytes(in_stream, size),
218 }}
113219}
114220
115fn parseFormValueConstant(in_stream: &io.InStream, signed: bool, inline T: type) -> %FormValue {
116 const block_len = %return in_stream.readIntLe(T);
117 return parseFormValueConstantLen(in_stream, signed, block_len);
118}
119
120fn parseFormValueAddrSize(in_stream: &io.InStream, is_64: bool) -> %u64 {
221fn parseFormValueDwarfOffsetSize(in_stream: &io.InStream, is_64: bool) -> %u64 {
121222 return if (is_64) {
122223 %return in_stream.readIntLe(u64)
123224 } else {
......@@ -125,6 +226,16 @@ fn parseFormValueAddrSize(in_stream: &io.InStream, is_64: bool) -> %u64 {
125226 };
126227}
127228
229fn parseFormValueTargetAddrSize(in_stream: &io.InStream) -> %u64 {
230 return if (@sizeOf(usize) == 4) {
231 u64(%return in_stream.readIntLe(u32))
232 } else if (@sizeOf(usize) == 8) {
233 %return in_stream.readIntLe(u64)
234 } else {
235 @unreachable();
236 };
237}
238
128239fn parseFormValueRefLen(in_stream: &io.InStream, size: usize) -> %FormValue {
129240 const buf = %return readAllocBytes(in_stream, size);
130241 return FormValue.Ref { buf };
......@@ -137,24 +248,22 @@ fn parseFormValueRef(in_stream: &io.InStream, inline T: type) -> %FormValue {
137248
138249fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormValue {
139250 return switch (form_id) {
140 DW.FORM_addr => FormValue.Address {
141 %return parseFormValueAddrSize(in_stream, is_64)
142 },
143 DW.FORM_block1 => parseFormValueBlock(in_stream, u8),
144 DW.FORM_block2 => parseFormValueBlock(in_stream, u16),
145 DW.FORM_block4 => parseFormValueBlock(in_stream, u32),
251 DW.FORM_addr => FormValue.Address { %return parseFormValueTargetAddrSize(in_stream) },
252 DW.FORM_block1 => parseFormValueBlock(in_stream, 1),
253 DW.FORM_block2 => parseFormValueBlock(in_stream, 2),
254 DW.FORM_block4 => parseFormValueBlock(in_stream, 4),
146255 DW.FORM_block => {
147256 const block_len = %return readULeb128(in_stream);
148257 parseFormValueBlockLen(in_stream, block_len)
149258 },
150 DW.FORM_data1 => parseFormValueConstant(in_stream, false, u8),
151 DW.FORM_data2 => parseFormValueConstant(in_stream, false, u16),
152 DW.FORM_data4 => parseFormValueConstant(in_stream, false, u32),
153 DW.FORM_data8 => parseFormValueConstant(in_stream, false, u64),
259 DW.FORM_data1 => parseFormValueConstant(in_stream, false, 1),
260 DW.FORM_data2 => parseFormValueConstant(in_stream, false, 2),
261 DW.FORM_data4 => parseFormValueConstant(in_stream, false, 4),
262 DW.FORM_data8 => parseFormValueConstant(in_stream, false, 8),
154263 DW.FORM_udata, DW.FORM_sdata => {
155264 const block_len = %return readULeb128(in_stream);
156265 const signed = form_id == DW.FORM_sdata;
157 parseFormValueConstantLen(in_stream, signed, block_len)
266 parseFormValueConstant(in_stream, signed, block_len)
158267 },
159268 DW.FORM_exprloc => {
160269 const size = %return readULeb128(in_stream);
......@@ -164,7 +273,7 @@ fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormVa
164273 DW.FORM_flag => FormValue.Flag { (%return in_stream.readByte()) != 0 },
165274 DW.FORM_flag_present => FormValue.Flag { true },
166275 DW.FORM_sec_offset => FormValue.SecOffset {
167 %return parseFormValueAddrSize(in_stream, is_64)
276 %return parseFormValueDwarfOffsetSize(in_stream, is_64)
168277 },
169278
170279 DW.FORM_ref1 => parseFormValueRef(in_stream, u8),
......@@ -176,22 +285,11 @@ fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormVa
176285 parseFormValueRefLen(in_stream, ref_len)
177286 },
178287
179 DW.FORM_ref_addr => FormValue.RefAddr { %return parseFormValueAddrSize(in_stream, is_64) },
288 DW.FORM_ref_addr => FormValue.RefAddr { %return parseFormValueDwarfOffsetSize(in_stream, is_64) },
180289 DW.FORM_ref_sig8 => FormValue.RefSig8 { %return in_stream.readIntLe(u64) },
181290
182 DW.FORM_string => {
183 var buf: List(u8) = undefined;
184 buf.init(&global_allocator);
185 while (true) {
186 const byte = %return in_stream.readByte();
187 if (byte == 0)
188 break;
189 %return buf.append(byte);
190 }
191
192 FormValue.String { buf.items }
193 },
194 DW.FORM_strp => FormValue.StrPtr { %return parseFormValueAddrSize(in_stream, is_64) },
291 DW.FORM_string => FormValue.String { %return readString(in_stream) },
292 DW.FORM_strp => FormValue.StrPtr { %return parseFormValueDwarfOffsetSize(in_stream, is_64) },
195293 DW.FORM_indirect => {
196294 const child_form_id = %return readULeb128(in_stream);
197295 parseFormValue(in_stream, child_form_id, is_64)
......@@ -200,16 +298,87 @@ fn parseFormValue(in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormVa
200298 }
201299}
202300
203fn findCompileUnitOffset(st: &ElfStackTrace, target_address: usize) -> %u64 {
204 if (const result ?= %return arangesOffset(st, target_address))
205 return result;
301fn parseAbbrevTable(in_stream: &io.InStream) -> %AbbrevTable {
302 var result = AbbrevTable.init(&global_allocator);
303 while (true) {
304 const abbrev_code = %return readULeb128(in_stream);
305 if (abbrev_code == 0)
306 return result;
307 %return result.append(AbbrevTableEntry {
308 .abbrev_code = abbrev_code,
309 .tag_id = %return readULeb128(in_stream),
310 .has_children = (%return in_stream.readByte()) == DW.CHILDREN_yes,
311 .attrs = List(AbbrevAttr).init(&global_allocator),
312 });
313 const attrs = &result.items[result.len - 1].attrs;
206314
207 // iterate over compile units looking for a match with the low pc and high pc
208 %return st.elf.seekToSection(st.debug_info);
315 while (true) {
316 const attr_id = %return readULeb128(in_stream);
317 const form_id = %return readULeb128(in_stream);
318 if (attr_id == 0 && form_id == 0)
319 break;
320 %return attrs.append(AbbrevAttr {
321 .attr_id = attr_id,
322 .form_id = form_id,
323 });
324 }
325 }
326}
327
328/// Gets an already existing AbbrevTable given the abbrev_offset, or if not found,
329/// seeks in the stream and parses it.
330fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) -> %&AbbrevTable {
331 for (st.abbrev_table_list.toSlice()) |header| {
332 if (header.offset == abbrev_offset) {
333 return &header.table;
334 }
335 }
336 %return st.self_exe_stream.seekTo(st.debug_abbrev.offset + abbrev_offset);
337 %return st.abbrev_table_list.append(AbbrevTableHeader {
338 .offset = abbrev_offset,
339 .table = %return parseAbbrevTable(&st.self_exe_stream),
340 });
341 return &st.abbrev_table_list.items[st.abbrev_table_list.len - 1].table;
342}
343
344fn getAbbrevTableEntry(abbrev_table: &const AbbrevTable, abbrev_code: u64) -> ?&const AbbrevTableEntry {
345 for (abbrev_table.toSlice()) |*table_entry| {
346 if (table_entry.abbrev_code == abbrev_code)
347 return table_entry;
348 }
349 return null;
350}
351
352fn parseDie(in_stream: &io.InStream, abbrev_table: &const AbbrevTable, is_64: bool) -> %Die {
353 const abbrev_code = %return readULeb128(in_stream);
354 const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) ?? return error.InvalidDebugInfo;
355
356 var result = Die {
357 .tag_id = table_entry.tag_id,
358 .has_children = table_entry.has_children,
359 .attrs = List(Die.Attr).init(&global_allocator),
360 };
361 %return result.attrs.resize(table_entry.attrs.len);
362 for (table_entry.attrs.toSlice()) |attr, i| {
363 result.attrs.items[i] = Die.Attr {
364 .id = attr.attr_id,
365 .value = %return parseFormValue(in_stream, attr.form_id, is_64),
366 };
367 }
368 return result;
369}
370
371fn scanAllCompileUnits(st: &ElfStackTrace) -> %void {
372 const debug_info_end = st.debug_info.offset + st.debug_info.size;
373 var this_unit_offset = st.debug_info.offset;
374 while (this_unit_offset < debug_info_end) {
375 %return st.self_exe_stream.seekTo(this_unit_offset);
209376
210 while (true) {
211377 var is_64: bool = undefined;
212378 const unit_length = %return readInitialLength(&st.self_exe_stream, &is_64);
379 if (unit_length == 0)
380 return;
381 const next_offset = unit_length + (if (is_64) usize(12) else usize(4));
213382
214383 const version = %return st.self_exe_stream.readInt(st.elf.is_big_endian, u16);
215384 if (version != 4) return error.InvalidDebugInfo;
......@@ -223,10 +392,45 @@ fn findCompileUnitOffset(st: &ElfStackTrace, target_address: usize) -> %u64 {
223392 const address_size = %return st.self_exe_stream.readByte();
224393 if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo;
225394
226 const abbrev_tag_id = %return st.self_exe_stream.readByte();
395 const compile_unit_pos = %return st.self_exe_stream.getPos();
396 const abbrev_table = %return getAbbrevTable(st, debug_abbrev_offset);
397
398 %return st.self_exe_stream.seekTo(compile_unit_pos);
227399
400 const compile_unit_die = (%return global_allocator.alloc(Die, 1)).ptr;
401 *compile_unit_die = %return parseDie(&st.self_exe_stream, abbrev_table, is_64);
228402
403 if (compile_unit_die.tag_id != DW.TAG_compile_unit)
404 return error.InvalidDebugInfo;
405 const low_pc = %return compile_unit_die.getAttrAddr(DW.AT_low_pc);
406
407 const high_pc_value = compile_unit_die.getAttr(DW.AT_high_pc) ?? return error.MissingDebugInfo;
408 const pc_end = switch (*high_pc_value) {
409 Address => |value| value,
410 Const => |value| {
411 const offset = %return value.asUnsignedLe();
412 low_pc + offset
413 },
414 else => return error.InvalidDebugInfo,
415 };
416
417 %return st.compile_unit_list.append(CompileUnit {
418 .is_64 = is_64,
419 .pc_start = low_pc,
420 .pc_end = pc_end,
421 .die = compile_unit_die,
422 });
423
424 this_unit_offset += next_offset;
425 }
426}
427
428fn findCompileUnit(st: &ElfStackTrace, target_address: u64) -> ?&const CompileUnit {
429 for (st.compile_unit_list.toSlice()) |*compile_unit| {
430 if (target_address >= compile_unit.pc_start && target_address < compile_unit.pc_end)
431 return compile_unit;
229432 }
433 return null;
230434}
231435
232436fn readInitialLength(in_stream: &io.InStream, is_64: &bool) -> %u64 {
......@@ -283,79 +487,26 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 {
283487 }
284488}
285489
286fn arangesOffset(st: &ElfStackTrace, target_address: usize) -> %?u64 {
287 // TODO ability to implicitly cast null to %?T
288 const aranges = st.aranges ?? return (?u64)(null);
289
290 %return st.elf.seekToSection(aranges);
291
292 const first_32_bits = %return st.self_exe_stream.readIntLe(u32);
293 var is_64: bool = undefined;
294 const unit_length = %return readInitialLength(&st.self_exe_stream, &is_64);
295 var unit_index: u64 = 0;
296
297 while (unit_index < unit_length) {
298 const version = %return st.self_exe_stream.readIntLe(u16);
299 if (version != 2) return error.InvalidDebugInfo;
300 unit_index += 2;
301
302 const debug_info_offset = if (is_64) {
303 unit_index += 4;
304 %return st.self_exe_stream.readIntLe(u64)
305 } else {
306 unit_index += 2;
307 %return st.self_exe_stream.readIntLe(u32)
308 };
309
310 const address_size = %return st.self_exe_stream.readByte();
311 if (address_size > 8) return error.UnsupportedDebugInfo;
312 unit_index += 1;
313
314 const segment_size = %return st.self_exe_stream.readByte();
315 if (segment_size > 0) return error.UnsupportedDebugInfo;
316 unit_index += 1;
317
318 const align = segment_size + 2 * address_size;
319 const padding = (%return st.self_exe_stream.getPos()) % align;
320 %return st.self_exe_stream.seekForward(padding);
321 unit_index += padding;
322
323 while (true) {
324 const address = %return st.self_exe_stream.readVarInt(false, u64, address_size);
325 const length = %return st.self_exe_stream.readVarInt(false, u64, address_size);
326 unit_index += align;
327 if (address == 0 && length == 0) break;
328
329 if (target_address >= address && target_address < address + length) {
330 // TODO ability to implicitly cast T to %?T
331 return (?u64)(debug_info_offset);
332 }
333 }
334 }
335
336 return error.MissingDebugInfo;
337}
338
339pub var global_allocator = Allocator {
490pub var global_allocator = mem.Allocator {
340491 .allocFn = globalAlloc,
341492 .reallocFn = globalRealloc,
342493 .freeFn = globalFree,
343494 .context = null,
344495};
345496
346var some_mem: [10 * 1024]u8 = undefined;
497var some_mem: [100 * 1024]u8 = undefined;
347498var some_mem_index: usize = 0;
348499
349fn globalAlloc(self: &Allocator, n: usize) -> %[]u8 {
500fn globalAlloc(self: &mem.Allocator, n: usize) -> %[]u8 {
350501 const result = some_mem[some_mem_index ... some_mem_index + n];
351502 some_mem_index += n;
352503 return result;
353504}
354505
355fn globalRealloc(self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8 {
506fn globalRealloc(self: &mem.Allocator, old_mem: []u8, new_size: usize) -> %[]u8 {
356507 const result = %return globalAlloc(self, new_size);
357508 @memcpy(result.ptr, old_mem.ptr, old_mem.len);
358509 return result;
359510}
360511
361fn globalFree(self: &Allocator, old_mem: []u8) { }
512fn globalFree(self: &mem.Allocator, old_mem: []u8) { }
std/dwarf.zig+3
......@@ -617,3 +617,6 @@ pub const CFA_MIPS_advance_loc8 = 0x1d;
617617pub const CFA_GNU_window_save = 0x2d;
618618pub const CFA_GNU_args_size = 0x2e;
619619pub const CFA_GNU_negative_offset_extended = 0x2f;
620
621pub const CHILDREN_no = 0x00;
622pub const CHILDREN_yes = 0x01;
std/io.zig+11-16
......@@ -10,6 +10,7 @@ const endian = @import("endian.zig");
1010const debug = @import("debug.zig");
1111const assert = debug.assert;
1212const os = @import("os.zig");
13const mem = @import("mem.zig");
1314
1415pub const stdin_fileno = 0;
1516pub const stdout_fileno = 1;
......@@ -261,34 +262,28 @@ pub struct InStream {
261262 return result[0];
262263 }
263264
264 pub inline fn readIntLe(is: &InStream, inline T: type) -> %T {
265 pub fn readIntLe(is: &InStream, inline T: type) -> %T {
265266 is.readInt(false, T)
266267 }
267268
268 pub inline fn readIntBe(is: &InStream, inline T: type) -> %T {
269 pub fn readIntBe(is: &InStream, inline T: type) -> %T {
269270 is.readInt(true, T)
270271 }
271272
272 pub inline fn readInt(is: &InStream, is_be: bool, inline T: type) -> %T {
273 pub fn readInt(is: &InStream, is_be: bool, inline T: type) -> %T {
273274 var result: T = undefined;
274275 const result_slice = ([]u8)((&result)[0...1]);
275276 %return is.readNoEof(result_slice);
276277 return endian.swapIf(!is_be, T, result);
277278 }
278279
279 pub inline fn readVarInt(is: &InStream, is_be: bool, inline T: type, size: usize) -> %T {
280 var result: T = zeroes;
281 const result_slice = ([]u8)((&result)[0...1]);
282 const padding = @sizeOf(T) - size;
283 {var i: usize = 0; while (i < size; i += 1) {
284 const index = if (is_be == @compileVar("is_big_endian")) {
285 padding + i
286 } else {
287 result_slice.len - i - 1 - padding
288 };
289 result_slice[index] = %return is.readByte();
290 }}
291 return result;
280 pub fn readVarInt(is: &InStream, is_be: bool, inline T: type, size: usize) -> %T {
281 assert(size <= @sizeOf(T));
282 assert(size <= 8);
283 var input_buf: [8]u8 = undefined;
284 const input_slice = input_buf[0...size];
285 %return is.readNoEof(input_slice);
286 return mem.sliceAsInt(input_slice, is_be, T);
292287 }
293288
294289 pub fn seekForward(is: &InStream, amount: usize) -> %void {
std/list.zig+19-28
......@@ -3,30 +3,27 @@ const assert = debug.assert;
33const mem = @import("mem.zig");
44const Allocator = mem.Allocator;
55
6pub fn List(inline T: type) -> type {
7 SmallList(T, @sizeOf(usize))
8}
9
10// TODO: make sure that setting static_size to 0 codegens to the same code
11// as if this were programmed without static_size at all.
12pub struct SmallList(T: type, static_size: usize) {
13 const Self = SmallList(T, static_size);
6pub struct List(T: type) {
7 const Self = List(T);
148
159 items: []T,
1610 len: usize,
17 prealloc_items: [static_size]T,
1811 allocator: &Allocator,
1912
20 pub fn init(l: &Self, allocator: &Allocator) {
21 l.items = l.prealloc_items[0...];
22 l.len = 0;
23 l.allocator = allocator;
13 pub fn init(allocator: &Allocator) -> Self {
14 Self {
15 .items = zeroes,
16 .len = 0,
17 .allocator = allocator,
18 }
2419 }
2520
2621 pub fn deinit(l: &Self) {
27 if (l.items.ptr != &l.prealloc_items[0]) {
28 l.allocator.free(T, l.items);
29 }
22 l.allocator.free(T, l.items);
23 }
24
25 pub fn toSlice(l: &Self) -> []T {
26 return l.items[0...l.len];
3027 }
3128
3229 pub fn append(l: &Self, item: T) -> %void {
......@@ -43,24 +40,18 @@ pub struct SmallList(T: type, static_size: usize) {
4340
4441 pub fn ensureCapacity(l: &Self, new_capacity: usize) -> %void {
4542 var better_capacity = l.items.len;
46 while (better_capacity < new_capacity) {
47 better_capacity *= 2;
48 }
49 if (better_capacity != l.items.len) {
50 if (l.items.ptr == &l.prealloc_items[0]) {
51 l.items = %return l.allocator.alloc(T, better_capacity);
52 mem.copy(T, l.items, l.prealloc_items[0...l.len]);
53 } else {
54 l.items = %return l.allocator.realloc(T, l.items, better_capacity);
55 }
43 if (better_capacity >= new_capacity) return;
44 while (true) {
45 better_capacity += better_capacity / 2 + 8;
46 if (better_capacity >= new_capacity) break;
5647 }
48 l.items = %return l.allocator.realloc(T, l.items, better_capacity);
5749 }
5850}
5951
6052#attribute("test")
6153fn basicListTest() {
62 var list: List(i32) = undefined;
63 list.init(&debug.global_allocator);
54 var list = List(i32).init(&debug.global_allocator);
6455 defer list.deinit();
6556
6657 {var i: usize = 0; while (i < 10; i += 1) {
std/mem.zig+30
......@@ -60,3 +60,33 @@ pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {
6060
6161 return if (a.len > b.len) Cmp.Greater else if (a.len < b.len) Cmp.Less else Cmp.Equal;
6262}
63
64pub fn sliceAsInt(buf: []u8, is_be: bool, inline T: type) -> T {
65 var result: T = zeroes;
66 const result_slice = ([]u8)((&result)[0...1]);
67 const padding = @sizeOf(T) - buf.len;
68
69 if (is_be == @compileVar("is_big_endian")) {
70 copy(u8, result_slice, buf);
71 } else {
72 for (buf) |b, i| {
73 const index = result_slice.len - i - 1 - padding;
74 result_slice[index] = b;
75 }
76 }
77 return result;
78}
79
80#attribute("test")
81fn testSliceAsInt() {
82 {
83 const buf = []u8{0x00, 0x00, 0x12, 0x34};
84 const answer = sliceAsInt(buf[0...], true, u64);
85 assert(answer == 0x00001234);
86 }
87 {
88 const buf = []u8{0x12, 0x34, 0x00, 0x00};
89 const answer = sliceAsInt(buf[0...], false, u64);
90 assert(answer == 0x00003412);
91 }
92}
test/cases/switch_prong_err_enum.zig created+29
......@@ -0,0 +1,29 @@
1const assert = @import("std").debug.assert;
2
3var read_count: u64 = 0;
4
5fn readOnce() -> %u64 {
6 read_count += 1;
7 return read_count;
8}
9
10error InvalidDebugInfo;
11
12enum FormValue {
13 Address: u64,
14 Other: bool,
15}
16
17#static_eval_enable(false)
18fn doThing(form_id: u64) -> %FormValue {
19 return switch (form_id) {
20 17 => FormValue.Address { %return readOnce() },
21 else => error.InvalidDebugInfo,
22 }
23}
24
25#attribute("test")
26fn switchProngReturnsErrorEnum() {
27 %%doThing(17);
28 assert(read_count == 1);
29}
test/self_hosted.zig+1
......@@ -12,6 +12,7 @@ const test_max_value_type = @import("cases/max_value_type.zig");
1212const test_var_params = @import("cases/var_params.zig");
1313const test_const_slice_child = @import("cases/const_slice_child.zig");
1414const test_switch_prong_implicit_cast = @import("cases/switch_prong_implicit_cast.zig");
15const test_switch_prong_err_enum = @import("cases/switch_prong_err_enum.zig");
1516
1617// normal comment
1718/// this is a documentation comment