| author | |
| committer | |
| log | e80702067959f99944adeb79a59c31699d7e278a |
| tree | c3d96f7aaaf583078420217face77b34cba022ac |
| parent | 00e944f7186810ac518d47b62a3e3cc8aa081cdf |
- Changed ZIR encoding of `import` metadata from having instruction
indexes to storing token indexes.3 files changed, 44 insertions(+), 25 deletions(-)
src/AstGen.zig+13-9| ... | ... | @@ -36,7 +36,7 @@ compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{}, |
| 36 | 36 | fn_block: ?*GenZir = null, |
| 37 | 37 | /// Maps string table indexes to the first `@import` ZIR instruction |
| 38 | 38 | /// that uses this string as the operand. |
| 39 | imports: std.AutoArrayHashMapUnmanaged(u32, Zir.Inst.Index) = .{}, | |
| 39 | imports: std.AutoArrayHashMapUnmanaged(u32, ast.TokenIndex) = .{}, | |
| 40 | 40 | |
| 41 | 41 | const InnerError = error{ OutOfMemory, AnalysisFail }; |
| 42 | 42 | |
| ... | ... | @@ -132,8 +132,7 @@ pub fn generate(gpa: *Allocator, tree: ast.Tree) Allocator.Error!Zir { |
| 132 | 132 | if (astgen.compile_errors.items.len == 0) { |
| 133 | 133 | astgen.extra.items[err_index] = 0; |
| 134 | 134 | } else { |
| 135 | try astgen.extra.ensureCapacity(gpa, astgen.extra.items.len + | |
| 136 | 1 + astgen.compile_errors.items.len * | |
| 135 | try astgen.extra.ensureUnusedCapacity(gpa, 1 + astgen.compile_errors.items.len * | |
| 137 | 136 | @typeInfo(Zir.Inst.CompileErrors.Item).Struct.fields.len); |
| 138 | 137 | |
| 139 | 138 | astgen.extra.items[err_index] = astgen.addExtraAssumeCapacity(Zir.Inst.CompileErrors{ |
| ... | ... | @@ -149,13 +148,20 @@ pub fn generate(gpa: *Allocator, tree: ast.Tree) Allocator.Error!Zir { |
| 149 | 148 | if (astgen.imports.count() == 0) { |
| 150 | 149 | astgen.extra.items[imports_index] = 0; |
| 151 | 150 | } else { |
| 152 | try astgen.extra.ensureCapacity(gpa, astgen.extra.items.len + | |
| 153 | @typeInfo(Zir.Inst.Imports).Struct.fields.len + astgen.imports.count()); | |
| 151 | try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.Imports).Struct.fields.len + | |
| 152 | astgen.imports.count() * @typeInfo(Zir.Inst.Imports.Item).Struct.fields.len); | |
| 154 | 153 | |
| 155 | 154 | astgen.extra.items[imports_index] = astgen.addExtraAssumeCapacity(Zir.Inst.Imports{ |
| 156 | 155 | .imports_len = @intCast(u32, astgen.imports.count()), |
| 157 | 156 | }); |
| 158 | astgen.extra.appendSliceAssumeCapacity(astgen.imports.values()); | |
| 157 | ||
| 158 | var it = astgen.imports.iterator(); | |
| 159 | while (it.next()) |entry| { | |
| 160 | _ = astgen.addExtraAssumeCapacity(Zir.Inst.Imports.Item{ | |
| 161 | .name = entry.key_ptr.*, | |
| 162 | .token = entry.value_ptr.*, | |
| 163 | }); | |
| 164 | } | |
| 159 | 165 | } |
| 160 | 166 | |
| 161 | 167 | return Zir{ |
| ... | ... | @@ -6986,12 +6992,10 @@ fn builtinCall( |
| 6986 | 6992 | const str_lit_token = main_tokens[operand_node]; |
| 6987 | 6993 | const str = try astgen.strLitAsString(str_lit_token); |
| 6988 | 6994 | const result = try gz.addStrTok(.import, str.index, str_lit_token); |
| 6989 | if (gz.refToIndex(result)) |import_inst_index| { | |
| 6990 | 6995 | const gop = try astgen.imports.getOrPut(astgen.gpa, str.index); |
| 6991 | 6996 | if (!gop.found_existing) { |
| 6992 | gop.value_ptr.* = import_inst_index; | |
| 6997 | gop.value_ptr.* = str_lit_token; | |
| 6993 | 6998 | } |
| 6994 | } | |
| 6995 | 6999 | return rvalue(gz, rl, result, node); |
| 6996 | 7000 | }, |
| 6997 | 7001 | .compile_log => { |
src/Compilation.zig+12-9| ... | ... | @@ -2315,7 +2315,7 @@ const AstGenSrc = union(enum) { |
| 2315 | 2315 | root, |
| 2316 | 2316 | import: struct { |
| 2317 | 2317 | importing_file: *Module.Scope.File, |
| 2318 | import_inst: Zir.Inst.Index, | |
| 2318 | import_tok: std.zig.ast.TokenIndex, | |
| 2319 | 2319 | }, |
| 2320 | 2320 | }; |
| 2321 | 2321 | |
| ... | ... | @@ -2352,11 +2352,15 @@ fn workerAstGenFile( |
| 2352 | 2352 | assert(file.zir_loaded); |
| 2353 | 2353 | const imports_index = file.zir.extra[@enumToInt(Zir.ExtraIndex.imports)]; |
| 2354 | 2354 | if (imports_index != 0) { |
| 2355 | const imports_len = file.zir.extra[imports_index]; | |
| 2355 | const extra = file.zir.extraData(Zir.Inst.Imports, imports_index); | |
| 2356 | var import_i: u32 = 0; | |
| 2357 | var extra_index = extra.end; | |
| 2356 | 2358 | |
| 2357 | for (file.zir.extra[imports_index + 1 ..][0..imports_len]) |import_inst| { | |
| 2358 | const inst_data = file.zir.instructions.items(.data)[import_inst].str_tok; | |
| 2359 | const import_path = inst_data.get(file.zir); | |
| 2359 | while (import_i < extra.data.imports_len) : (import_i += 1) { | |
| 2360 | const item = file.zir.extraData(Zir.Inst.Imports.Item, extra_index); | |
| 2361 | extra_index = item.end; | |
| 2362 | ||
| 2363 | const import_path = file.zir.nullTerminatedString(item.data.name); | |
| 2360 | 2364 | |
| 2361 | 2365 | const import_result = blk: { |
| 2362 | 2366 | const lock = comp.mutex.acquire(); |
| ... | ... | @@ -2370,7 +2374,7 @@ fn workerAstGenFile( |
| 2370 | 2374 | }); |
| 2371 | 2375 | const sub_src: AstGenSrc = .{ .import = .{ |
| 2372 | 2376 | .importing_file = file, |
| 2373 | .import_inst = import_inst, | |
| 2377 | .import_tok = item.data.token, | |
| 2374 | 2378 | } }; |
| 2375 | 2379 | wg.start(); |
| 2376 | 2380 | comp.thread_pool.spawn(workerAstGenFile, .{ |
| ... | ... | @@ -2602,12 +2606,11 @@ fn reportRetryableAstGenError( |
| 2602 | 2606 | }, |
| 2603 | 2607 | .import => |info| blk: { |
| 2604 | 2608 | const importing_file = info.importing_file; |
| 2605 | const import_inst = info.import_inst; | |
| 2606 | const inst_data = importing_file.zir.instructions.items(.data)[import_inst].str_tok; | |
| 2609 | ||
| 2607 | 2610 | break :blk .{ |
| 2608 | 2611 | .file_scope = importing_file, |
| 2609 | 2612 | .parent_decl_node = 0, |
| 2610 | .lazy = .{ .token_offset = inst_data.src_tok }, | |
| 2613 | .lazy = .{ .token_abs = info.import_tok }, | |
| 2611 | 2614 | }; |
| 2612 | 2615 | }, |
| 2613 | 2616 | }; |
src/Zir.zig+19-7| ... | ... | @@ -138,11 +138,17 @@ pub fn renderAsTextToFile( |
| 138 | 138 | const imports_index = scope_file.zir.extra[@enumToInt(ExtraIndex.imports)]; |
| 139 | 139 | if (imports_index != 0) { |
| 140 | 140 | try fs_file.writeAll("Imports:\n"); |
| 141 | const imports_len = scope_file.zir.extra[imports_index]; | |
| 142 | for (scope_file.zir.extra[imports_index + 1 ..][0..imports_len]) |import_inst| { | |
| 143 | const inst_data = writer.code.instructions.items(.data)[import_inst].str_tok; | |
| 144 | const src = inst_data.src(); | |
| 145 | const import_path = inst_data.get(writer.code); | |
| 141 | ||
| 142 | const extra = scope_file.zir.extraData(Inst.Imports, imports_index); | |
| 143 | var import_i: u32 = 0; | |
| 144 | var extra_index = extra.end; | |
| 145 | ||
| 146 | while (import_i < extra.data.imports_len) : (import_i += 1) { | |
| 147 | const item = scope_file.zir.extraData(Inst.Imports.Item, extra_index); | |
| 148 | extra_index = item.end; | |
| 149 | ||
| 150 | const src: LazySrcLoc = .{ .token_abs = item.data.token }; | |
| 151 | const import_path = scope_file.zir.nullTerminatedString(item.data.name); | |
| 146 | 152 | try fs_file.writer().print(" @import(\"{}\") ", .{ |
| 147 | 153 | std.zig.fmtEscapes(import_path), |
| 148 | 154 | }); |
| ... | ... | @@ -2780,10 +2786,16 @@ pub const Inst = struct { |
| 2780 | 2786 | }; |
| 2781 | 2787 | }; |
| 2782 | 2788 | |
| 2783 | /// Trailing: for each `imports_len` there is an instruction index | |
| 2784 | /// to an import instruction. | |
| 2789 | /// Trailing: for each `imports_len` there is an Item | |
| 2785 | 2790 | pub const Imports = struct { |
| 2786 | 2791 | imports_len: Zir.Inst.Index, |
| 2792 | ||
| 2793 | pub const Item = struct { | |
| 2794 | /// null terminated string index | |
| 2795 | name: u32, | |
| 2796 | /// points to the import name | |
| 2797 | token: ast.TokenIndex, | |
| 2798 | }; | |
| 2787 | 2799 | }; |
| 2788 | 2800 | }; |
| 2789 | 2801 |