From 01170ea6f8915a627830c651711763aa6f1ea6ce Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Mon, 23 Sep 2024 09:59:27 -0700 Subject: [PATCH] StringInterner: move string interner to backend --- src/aro.zig | 2 +- src/aro/Builtins.zig | 1 - src/aro/CodeGen.zig | 10 ++-- src/aro/Compilation.zig | 38 ++++++------ src/aro/Parser.zig | 78 ++++++++++++------------- src/aro/SymbolStack.zig | 2 +- src/aro/Tree.zig | 5 +- src/aro/Type.zig | 2 +- src/backend.zig | 1 + src/{aro => backend}/StringInterner.zig | 5 -- 10 files changed, 72 insertions(+), 72 deletions(-) rename src/{aro => backend}/StringInterner.zig (93%) diff --git a/src/aro.zig b/src/aro.zig index a9b76e0e..ba9870bc 100644 --- a/src/aro.zig +++ b/src/aro.zig @@ -9,7 +9,6 @@ pub const Tokenizer = @import("aro/Tokenizer.zig"); pub const Toolchain = @import("aro/Toolchain.zig"); pub const Tree = @import("aro/Tree.zig"); pub const Type = @import("aro/Type.zig"); -pub const TypeMapper = @import("aro/StringInterner.zig").TypeMapper; pub const target_util = @import("aro/target.zig"); pub const Value = @import("aro/Value.zig"); @@ -18,6 +17,7 @@ pub const Interner = backend.Interner; pub const Ir = backend.Ir; pub const Object = backend.Object; pub const CallingConvention = backend.CallingConvention; +pub const TypeMapper = backend.StringInterner.TypeMapper; pub const version_str = backend.version_str; pub const version = backend.version; diff --git a/src/aro/Builtins.zig b/src/aro/Builtins.zig index eb67a2f3..7fe036cb 100644 --- a/src/aro/Builtins.zig +++ b/src/aro/Builtins.zig @@ -3,7 +3,6 @@ const Compilation = @import("Compilation.zig"); const Type = @import("Type.zig"); const TypeDescription = @import("Builtins/TypeDescription.zig"); const target_util = @import("target.zig"); -const StringId = @import("StringInterner.zig").StringId; const LangOpts = @import("LangOpts.zig"); const Parser = @import("Parser.zig"); diff --git a/src/aro/CodeGen.zig b/src/aro/CodeGen.zig index 52112196..7e56b8dc 100644 --- a/src/aro/CodeGen.zig +++ b/src/aro/CodeGen.zig @@ -4,12 +4,12 @@ const assert = std.debug.assert; const backend = @import("backend"); const Interner = backend.Interner; const Ir = backend.Ir; +const StrInt = backend.StringInterner; +const StringId = StrInt.StringId; const Builtins = @import("Builtins.zig"); const Builtin = Builtins.Builtin; const Compilation = @import("Compilation.zig"); const Builder = Ir.Builder; -const StrInt = @import("StringInterner.zig"); -const StringId = StrInt.StringId; const Tree = @import("Tree.zig"); const NodeIndex = Tree.NodeIndex; const Type = @import("Type.zig"); @@ -305,7 +305,7 @@ fn genExpr(c: *CodeGen, node: NodeIndex) Error!Ir.Ref { const size: u32 = @intCast(ty.sizeof(c.comp).?); // TODO add error in parser const @"align" = ty.alignof(c.comp); const alloc = try c.builder.addAlloc(size, @"align"); - const name = try StrInt.intern(c.comp, c.tree.tokSlice(data.decl.name)); + const name = try c.comp.internString(c.tree.tokSlice(data.decl.name)); try c.symbols.append(c.comp.gpa, .{ .name = name, .val = alloc }); if (data.decl.node != .none) { try c.genInitializer(alloc, ty, data.decl.node); @@ -952,7 +952,7 @@ fn genLval(c: *CodeGen, node: NodeIndex) Error!Ir.Ref { .paren_expr => return c.genLval(data.un), .decl_ref_expr => { const slice = c.tree.tokSlice(data.decl_ref); - const name = try StrInt.intern(c.comp, slice); + const name = try c.comp.internString(slice); var i = c.symbols.items.len; while (i > 0) { i -= 1; @@ -1174,7 +1174,7 @@ fn genCall(c: *CodeGen, fn_node: NodeIndex, arg_nodes: []const NodeIndex, ty: Ty }, .decl_ref_expr => { const slice = c.tree.tokSlice(c.node_data[cur].decl_ref); - const name = try StrInt.intern(c.comp, slice); + const name = try c.comp.internString(slice); var i = c.symbols.items.len; while (i > 0) { i -= 1; diff --git a/src/aro/Compilation.zig b/src/aro/Compilation.zig index aef3a452..270fb3b2 100644 --- a/src/aro/Compilation.zig +++ b/src/aro/Compilation.zig @@ -6,6 +6,7 @@ const mem = std.mem; const backend = @import("backend"); const Interner = backend.Interner; const CodeGenOptions = backend.CodeGenOptions; +const StrInt = backend.StringInterner; const Builtins = @import("Builtins.zig"); const Builtin = Builtins.Builtin; const Diagnostics = @import("Diagnostics.zig"); @@ -15,7 +16,6 @@ const Tokenizer = @import("Tokenizer.zig"); const Token = Tokenizer.Token; const Type = @import("Type.zig"); const Pragma = @import("Pragma.zig"); -const StrInt = @import("StringInterner.zig"); const record_layout = @import("record_layout.zig"); const target_util = @import("target.zig"); @@ -753,6 +753,10 @@ pub fn float80Type(comp: *const Compilation) ?Type { return target_util.float80Type(comp.target); } +pub fn internString(comp: *Compilation, str: []const u8) !StrInt.StringId { + return comp.string_interner.internExtra(comp.gpa, str); +} + /// Smallest integer type with at least N bits pub fn intLeastN(comp: *const Compilation, bits: usize, signedness: std.builtin.Signedness) Type { if (bits == 64 and (comp.target.isDarwin() or comp.target.isWasm())) { @@ -924,7 +928,7 @@ pub fn hasHalfPrecisionFloatABI(comp: *const Compilation) bool { fn generateNsConstantStringType(comp: *Compilation) !void { comp.types.ns_constant_string.record = .{ - .name = try StrInt.intern(comp, "__NSConstantString_tag"), + .name = try comp.internString("__NSConstantString_tag"), .fields = &comp.types.ns_constant_string.fields, .field_attributes = null, .type_layout = undefined, @@ -932,10 +936,10 @@ fn generateNsConstantStringType(comp: *Compilation) !void { const const_int_ptr = Type{ .specifier = .pointer, .data = .{ .sub_type = &comp.types.ns_constant_string.int_ty } }; const const_char_ptr = Type{ .specifier = .pointer, .data = .{ .sub_type = &comp.types.ns_constant_string.char_ty } }; - comp.types.ns_constant_string.fields[0] = .{ .name = try StrInt.intern(comp, "isa"), .ty = const_int_ptr }; - comp.types.ns_constant_string.fields[1] = .{ .name = try StrInt.intern(comp, "flags"), .ty = .{ .specifier = .int } }; - comp.types.ns_constant_string.fields[2] = .{ .name = try StrInt.intern(comp, "str"), .ty = const_char_ptr }; - comp.types.ns_constant_string.fields[3] = .{ .name = try StrInt.intern(comp, "length"), .ty = .{ .specifier = .long } }; + comp.types.ns_constant_string.fields[0] = .{ .name = try comp.internString("isa"), .ty = const_int_ptr }; + comp.types.ns_constant_string.fields[1] = .{ .name = try comp.internString("flags"), .ty = .{ .specifier = .int } }; + comp.types.ns_constant_string.fields[2] = .{ .name = try comp.internString("str"), .ty = const_char_ptr }; + comp.types.ns_constant_string.fields[3] = .{ .name = try comp.internString("length"), .ty = .{ .specifier = .long } }; comp.types.ns_constant_string.ty = .{ .specifier = .@"struct", .data = .{ .record = &comp.types.ns_constant_string.record } }; record_layout.compute(&comp.types.ns_constant_string.record, comp.types.ns_constant_string.ty, comp, null) catch unreachable; } @@ -971,7 +975,7 @@ fn generateVaListType(comp: *Compilation) !Type { .aarch64_va_list => { const record_ty = try arena.create(Type.Record); record_ty.* = .{ - .name = try StrInt.intern(comp, "__va_list_tag"), + .name = try comp.internString("__va_list_tag"), .fields = try arena.alloc(Type.Record.Field, 5), .field_attributes = null, .type_layout = undefined, // computed below @@ -979,18 +983,18 @@ fn generateVaListType(comp: *Compilation) !Type { const void_ty = try arena.create(Type); void_ty.* = .{ .specifier = .void }; const void_ptr = Type{ .specifier = .pointer, .data = .{ .sub_type = void_ty } }; - record_ty.fields[0] = .{ .name = try StrInt.intern(comp, "__stack"), .ty = void_ptr }; - record_ty.fields[1] = .{ .name = try StrInt.intern(comp, "__gr_top"), .ty = void_ptr }; - record_ty.fields[2] = .{ .name = try StrInt.intern(comp, "__vr_top"), .ty = void_ptr }; - record_ty.fields[3] = .{ .name = try StrInt.intern(comp, "__gr_offs"), .ty = .{ .specifier = .int } }; - record_ty.fields[4] = .{ .name = try StrInt.intern(comp, "__vr_offs"), .ty = .{ .specifier = .int } }; + record_ty.fields[0] = .{ .name = try comp.internString("__stack"), .ty = void_ptr }; + record_ty.fields[1] = .{ .name = try comp.internString("__gr_top"), .ty = void_ptr }; + record_ty.fields[2] = .{ .name = try comp.internString("__vr_top"), .ty = void_ptr }; + record_ty.fields[3] = .{ .name = try comp.internString("__gr_offs"), .ty = .{ .specifier = .int } }; + record_ty.fields[4] = .{ .name = try comp.internString("__vr_offs"), .ty = .{ .specifier = .int } }; ty = .{ .specifier = .@"struct", .data = .{ .record = record_ty } }; record_layout.compute(record_ty, ty, comp, null) catch unreachable; }, .x86_64_va_list => { const record_ty = try arena.create(Type.Record); record_ty.* = .{ - .name = try StrInt.intern(comp, "__va_list_tag"), + .name = try comp.internString("__va_list_tag"), .fields = try arena.alloc(Type.Record.Field, 4), .field_attributes = null, .type_layout = undefined, // computed below @@ -998,10 +1002,10 @@ fn generateVaListType(comp: *Compilation) !Type { const void_ty = try arena.create(Type); void_ty.* = .{ .specifier = .void }; const void_ptr = Type{ .specifier = .pointer, .data = .{ .sub_type = void_ty } }; - record_ty.fields[0] = .{ .name = try StrInt.intern(comp, "gp_offset"), .ty = .{ .specifier = .uint } }; - record_ty.fields[1] = .{ .name = try StrInt.intern(comp, "fp_offset"), .ty = .{ .specifier = .uint } }; - record_ty.fields[2] = .{ .name = try StrInt.intern(comp, "overflow_arg_area"), .ty = void_ptr }; - record_ty.fields[3] = .{ .name = try StrInt.intern(comp, "reg_save_area"), .ty = void_ptr }; + record_ty.fields[0] = .{ .name = try comp.internString("gp_offset"), .ty = .{ .specifier = .uint } }; + record_ty.fields[1] = .{ .name = try comp.internString("fp_offset"), .ty = .{ .specifier = .uint } }; + record_ty.fields[2] = .{ .name = try comp.internString("overflow_arg_area"), .ty = void_ptr }; + record_ty.fields[3] = .{ .name = try comp.internString("reg_save_area"), .ty = void_ptr }; ty = .{ .specifier = .@"struct", .data = .{ .record = record_ty } }; record_layout.compute(record_ty, ty, comp, null) catch unreachable; }, diff --git a/src/aro/Parser.zig b/src/aro/Parser.zig index 21df6625..28b658e8 100644 --- a/src/aro/Parser.zig +++ b/src/aro/Parser.zig @@ -24,7 +24,7 @@ const Value = @import("Value.zig"); const SymbolStack = @import("SymbolStack.zig"); const Symbol = SymbolStack.Symbol; const record_layout = @import("record_layout.zig"); -const StrInt = @import("StringInterner.zig"); +const StrInt = @import("backend").StringInterner; const StringId = StrInt.StringId; const Builtins = @import("Builtins.zig"); const Builtin = Builtins.Builtin; @@ -704,12 +704,12 @@ pub fn parse(pp: *Preprocessor) Compilation.Error!Tree { .record_buf = std.ArrayList(Type.Record.Field).init(pp.comp.gpa), .field_attr_buf = std.ArrayList([]const Attribute).init(pp.comp.gpa), .string_ids = .{ - .declspec_id = try StrInt.intern(pp.comp, "__declspec"), - .main_id = try StrInt.intern(pp.comp, "main"), - .file = try StrInt.intern(pp.comp, "FILE"), - .jmp_buf = try StrInt.intern(pp.comp, "jmp_buf"), - .sigjmp_buf = try StrInt.intern(pp.comp, "sigjmp_buf"), - .ucontext_t = try StrInt.intern(pp.comp, "ucontext_t"), + .declspec_id = try pp.comp.internString("__declspec"), + .main_id = try pp.comp.internString("main"), + .file = try pp.comp.internString("FILE"), + .jmp_buf = try pp.comp.internString("jmp_buf"), + .sigjmp_buf = try pp.comp.internString("sigjmp_buf"), + .ucontext_t = try pp.comp.internString("ucontext_t"), }, }; errdefer { @@ -742,27 +742,27 @@ pub fn parse(pp: *Preprocessor) Compilation.Error!Tree { { if (p.comp.langopts.hasChar8_T()) { - try p.syms.defineTypedef(&p, try StrInt.intern(p.comp, "char8_t"), .{ .specifier = .uchar }, 0, .none); + try p.syms.defineTypedef(&p, try p.comp.internString("char8_t"), .{ .specifier = .uchar }, 0, .none); } - try p.syms.defineTypedef(&p, try StrInt.intern(p.comp, "__int128_t"), .{ .specifier = .int128 }, 0, .none); - try p.syms.defineTypedef(&p, try StrInt.intern(p.comp, "__uint128_t"), .{ .specifier = .uint128 }, 0, .none); + try p.syms.defineTypedef(&p, try p.comp.internString("__int128_t"), .{ .specifier = .int128 }, 0, .none); + try p.syms.defineTypedef(&p, try p.comp.internString("__uint128_t"), .{ .specifier = .uint128 }, 0, .none); const elem_ty = try p.arena.create(Type); elem_ty.* = .{ .specifier = .char }; - try p.syms.defineTypedef(&p, try StrInt.intern(p.comp, "__builtin_ms_va_list"), .{ + try p.syms.defineTypedef(&p, try p.comp.internString("__builtin_ms_va_list"), .{ .specifier = .pointer, .data = .{ .sub_type = elem_ty }, }, 0, .none); const ty = &pp.comp.types.va_list; - try p.syms.defineTypedef(&p, try StrInt.intern(p.comp, "__builtin_va_list"), ty.*, 0, .none); + try p.syms.defineTypedef(&p, try p.comp.internString("__builtin_va_list"), ty.*, 0, .none); if (ty.isArray()) ty.decayArray(); - try p.syms.defineTypedef(&p, try StrInt.intern(p.comp, "__NSConstantString"), pp.comp.types.ns_constant_string.ty, 0, .none); + try p.syms.defineTypedef(&p, try p.comp.internString("__NSConstantString"), pp.comp.types.ns_constant_string.ty, 0, .none); if (p.comp.float80Type()) |float80_ty| { - try p.syms.defineTypedef(&p, try StrInt.intern(p.comp, "__float80"), float80_ty, 0, .none); + try p.syms.defineTypedef(&p, try p.comp.internString("__float80"), float80_ty, 0, .none); } } @@ -1019,7 +1019,7 @@ fn decl(p: *Parser) Error!bool { if (p.func.ty != null) try p.err(.func_not_in_root); const node = try p.addNode(undefined); // reserve space - const interned_declarator_name = try StrInt.intern(p.comp, p.tokSlice(init_d.d.name)); + const interned_declarator_name = try p.comp.internString(p.tokSlice(init_d.d.name)); try p.syms.defineSymbol(p, interned_declarator_name, init_d.d.ty, init_d.d.name, node, .{}, false); const func = p.func; @@ -1080,7 +1080,7 @@ fn decl(p: *Parser) Error!bool { // find and correct parameter types // TODO check for missing declarations and redefinitions const name_str = p.tokSlice(d.name); - const interned_name = try StrInt.intern(p.comp, name_str); + const interned_name = try p.comp.internString(name_str); for (init_d.d.ty.params()) |*param| { if (param.name == interned_name) { param.ty = d.ty; @@ -1174,7 +1174,7 @@ fn decl(p: *Parser) Error!bool { }); try p.decl_buf.append(node); - const interned_name = try StrInt.intern(p.comp, p.tokSlice(init_d.d.name)); + const interned_name = try p.comp.internString(p.tokSlice(init_d.d.name)); if (decl_spec.storage_class == .typedef) { try p.syms.defineTypedef(p, interned_name, init_d.d.ty, init_d.d.name, node); p.typedefDefined(interned_name, init_d.d.ty); @@ -1833,7 +1833,7 @@ fn initDeclarator(p: *Parser, decl_spec: *DeclSpec, attr_buf_top: usize) Error!? try p.syms.pushScope(p); defer p.syms.popScope(); - const interned_name = try StrInt.intern(p.comp, p.tokSlice(init_d.d.name)); + const interned_name = try p.comp.internString(p.tokSlice(init_d.d.name)); try p.syms.declareSymbol(p, interned_name, init_d.d.ty, init_d.d.name, .none); if (c23_auto or auto_type) { p.auto_type_decl_name = interned_name; @@ -2059,7 +2059,7 @@ fn typeSpec(p: *Parser, ty: *Type.Builder) Error!bool { continue; }, .identifier, .extended_identifier => { - var interned_name = try StrInt.intern(p.comp, p.tokSlice(p.tok_i)); + var interned_name = try p.comp.internString(p.tokSlice(p.tok_i)); var declspec_found = false; if (interned_name == p.string_ids.declspec_id) { @@ -2073,7 +2073,7 @@ fn typeSpec(p: *Parser, ty: *Type.Builder) Error!bool { } if (ty.typedef != null) break; if (declspec_found) { - interned_name = try StrInt.intern(p.comp, p.tokSlice(p.tok_i)); + interned_name = try p.comp.internString(p.tokSlice(p.tok_i)); } const typedef = (try p.syms.findTypedef(p, interned_name, p.tok_i, ty.specifier != .none)) orelse break; if (!ty.combineTypedef(p, typedef.ty, typedef.tok)) break; @@ -2122,7 +2122,7 @@ fn getAnonymousName(p: *Parser, kind_tok: TokenIndex) !StringId { "(anonymous {s} at {s}:{d}:{d})", .{ kind_str, source.path, line_col.line_no, line_col.col }, ); - return StrInt.intern(p.comp, str); + return p.comp.internString(str); } /// recordSpec @@ -2144,7 +2144,7 @@ fn recordSpec(p: *Parser) Error!Type { return error.ParsingFailed; }; // check if this is a reference to a previous type - const interned_name = try StrInt.intern(p.comp, p.tokSlice(ident)); + const interned_name = try p.comp.internString(p.tokSlice(ident)); if (try p.syms.findTag(p, interned_name, p.tok_ids[kind_tok], ident, p.tok_ids[p.tok_i])) |prev| { return prev.ty; } else { @@ -2178,7 +2178,7 @@ fn recordSpec(p: *Parser) Error!Type { var defined = false; const record_ty: *Type.Record = if (maybe_ident) |ident| record_ty: { const ident_str = p.tokSlice(ident); - const interned_name = try StrInt.intern(p.comp, ident_str); + const interned_name = try p.comp.internString(ident_str); if (try p.syms.defineTag(p, interned_name, p.tok_ids[kind_tok], ident)) |prev| { if (!prev.ty.hasIncompleteSize()) { // if the record isn't incomplete, this is a redefinition @@ -2274,7 +2274,7 @@ fn recordSpec(p: *Parser) Error!Type { }, attr_buf_top, null); if (ty.specifier == .attributed and maybe_ident != null) { const ident_str = p.tokSlice(maybe_ident.?); - const interned_name = try StrInt.intern(p.comp, ident_str); + const interned_name = try p.comp.internString(ident_str); const ptr = p.syms.getPtr(interned_name, .tags); ptr.ty = ty; } @@ -2432,7 +2432,7 @@ fn recordDeclarator(p: *Parser) Error!bool { } try p.err(.missing_declaration); } else { - const interned_name = if (name_tok != 0) try StrInt.intern(p.comp, p.tokSlice(name_tok)) else try p.getAnonymousName(first_tok); + const interned_name = if (name_tok != 0) try p.comp.internString(p.tokSlice(name_tok)) else try p.getAnonymousName(first_tok); try p.record_buf.append(.{ .name = interned_name, .ty = ty, @@ -2531,7 +2531,7 @@ fn enumSpec(p: *Parser) Error!Type { return error.ParsingFailed; }; // check if this is a reference to a previous type - const interned_name = try StrInt.intern(p.comp, p.tokSlice(ident)); + const interned_name = try p.comp.internString(p.tokSlice(ident)); if (try p.syms.findTag(p, interned_name, .keyword_enum, ident, p.tok_ids[p.tok_i])) |prev| { // only check fixed underlying type in forward declarations and not in references. if (p.tok_ids[p.tok_i] == .semicolon) @@ -2568,7 +2568,7 @@ fn enumSpec(p: *Parser) Error!Type { var defined = false; const enum_ty: *Type.Enum = if (maybe_ident) |ident| enum_ty: { const ident_str = p.tokSlice(ident); - const interned_name = try StrInt.intern(p.comp, ident_str); + const interned_name = try p.comp.internString(ident_str); if (try p.syms.defineTag(p, interned_name, .keyword_enum, ident)) |prev| { const enum_ty = prev.ty.get(.@"enum").?.data.@"enum"; if (!enum_ty.isIncomplete() and !enum_ty.fixed) { @@ -2860,7 +2860,7 @@ fn enumerator(p: *Parser, e: *Enumerator) Error!?EnumFieldAndNode { } } - const interned_name = try StrInt.intern(p.comp, p.tokSlice(name_tok)); + const interned_name = try p.comp.internString(p.tokSlice(name_tok)); try p.syms.defineEnumeration(p, interned_name, res.ty, name_tok, e.res.val); const node = try p.addNode(.{ .tag = .enum_field_decl, @@ -3142,7 +3142,7 @@ fn directDeclarator(p: *Parser, base_type: Type, d: *Declarator, kind: Declarato specifier = .old_style_func; while (true) { const name_tok = try p.expectIdentifier(); - const interned_name = try StrInt.intern(p.comp, p.tokSlice(name_tok)); + const interned_name = try p.comp.internString(p.tokSlice(name_tok)); try p.syms.defineParam(p, interned_name, undefined, name_tok); try p.param_buf.append(.{ .name = interned_name, @@ -3210,7 +3210,7 @@ fn paramDecls(p: *Parser, d: *Declarator) Error!?[]Type.Func.Param { if (d.old_style_func == null) d.old_style_func = identifier; try p.param_buf.append(.{ - .name = try StrInt.intern(p.comp, p.tokSlice(identifier)), + .name = try p.comp.internString(p.tokSlice(identifier)), .name_tok = identifier, .ty = .{ .specifier = .int }, }); @@ -3235,7 +3235,7 @@ fn paramDecls(p: *Parser, d: *Declarator) Error!?[]Type.Func.Param { name_tok = some.name; param_ty = some.ty; if (some.name != 0) { - const interned_name = try StrInt.intern(p.comp, p.tokSlice(name_tok)); + const interned_name = try p.comp.internString(p.tokSlice(name_tok)); try p.syms.defineParam(p, interned_name, param_ty, name_tok); } } @@ -3268,7 +3268,7 @@ fn paramDecls(p: *Parser, d: *Declarator) Error!?[]Type.Func.Param { try param_decl_spec.validateParam(p, ¶m_ty); try p.param_buf.append(.{ - .name = if (name_tok == 0) .empty else try StrInt.intern(p.comp, p.tokSlice(name_tok)), + .name = if (name_tok == 0) .empty else try p.comp.internString(p.tokSlice(name_tok)), .name_tok = if (name_tok == 0) first_tok else name_tok, .ty = param_ty, }); @@ -3472,7 +3472,7 @@ fn initializerItem(p: *Parser, il: *InitList, init_ty: Type) Error!bool { } else if (p.eatToken(.period)) |period| { const field_tok = try p.expectIdentifier(); const field_str = p.tokSlice(field_tok); - const field_name = try StrInt.intern(p.comp, field_str); + const field_name = try p.comp.internString(field_str); cur_ty = cur_ty.canonicalize(.standard); if (!cur_ty.isRecord()) { try p.errStr(.invalid_field_designator, period, try p.typeStr(cur_ty)); @@ -4723,7 +4723,7 @@ fn compoundStmt(p: *Parser, is_fn_body: bool, stmt_expr_state: ?*StmtExprState) var return_zero = false; if (last_noreturn == .no and !ret_ty.is(.void) and !ret_ty.isFunc() and !ret_ty.isArray()) { const func_name = p.tokSlice(p.func.name); - const interned_name = try StrInt.intern(p.comp, func_name); + const interned_name = try p.comp.internString(func_name); if (interned_name == p.string_ids.main_id and ret_ty.is(.int)) { return_zero = true; } else { @@ -6980,7 +6980,7 @@ fn builtinOffsetof(p: *Parser, want_bits: bool) Error!Result { fn offsetofMemberDesignator(p: *Parser, base_ty: Type, want_bits: bool) Error!Result { errdefer p.skipTo(.r_paren); const base_field_name_tok = try p.expectIdentifier(); - const base_field_name = try StrInt.intern(p.comp, p.tokSlice(base_field_name_tok)); + const base_field_name = try p.comp.internString(p.tokSlice(base_field_name_tok)); const base_record_ty = base_ty.getRecord().?; try p.validateFieldAccess(base_record_ty, base_ty, base_field_name_tok, base_field_name); const base_node = try p.addNode(.{ .tag = .default_init_expr, .ty = base_ty, .data = undefined }); @@ -6993,7 +6993,7 @@ fn offsetofMemberDesignator(p: *Parser, base_ty: Type, want_bits: bool) Error!Re .period => { p.tok_i += 1; const field_name_tok = try p.expectIdentifier(); - const field_name = try StrInt.intern(p.comp, p.tokSlice(field_name_tok)); + const field_name = try p.comp.internString(p.tokSlice(field_name_tok)); const lhs_record_ty = lhs.ty.getRecord() orelse { try p.errStr(.offsetof_ty, field_name_tok, try p.typeStr(lhs.ty)); @@ -7598,7 +7598,7 @@ fn fieldAccess( if (is_arrow and !is_ptr) try p.errStr(.member_expr_not_ptr, field_name_tok, try p.typeStr(expr_ty)); if (!is_arrow and is_ptr) try p.errStr(.member_expr_ptr, field_name_tok, try p.typeStr(expr_ty)); - const field_name = try StrInt.intern(p.comp, p.tokSlice(field_name_tok)); + const field_name = try p.comp.internString(p.tokSlice(field_name_tok)); try p.validateFieldAccess(record_ty, expr_ty, field_name_tok, field_name); var discard: u64 = 0; return p.fieldAccessExtra(lhs.node, record_ty, field_name, is_arrow, &discard); @@ -7665,7 +7665,7 @@ fn checkVaStartArg(p: *Parser, builtin_tok: TokenIndex, first_after: TokenIndex, } const last_param_name = func_params[func_params.len - 1].name; const decl_ref = p.getNode(arg.node, .decl_ref_expr); - if (decl_ref == null or last_param_name != try StrInt.intern(p.comp, p.tokSlice(p.nodes.items(.data)[@intFromEnum(decl_ref.?)].decl_ref))) { + if (decl_ref == null or last_param_name != try p.comp.internString(p.tokSlice(p.nodes.items(.data)[@intFromEnum(decl_ref.?)].decl_ref))) { try p.errTok(.va_start_not_last_param, param_tok); } } @@ -7860,7 +7860,7 @@ fn primaryExpr(p: *Parser) Error!Result { .identifier, .extended_identifier => { const name_tok = try p.expectIdentifier(); const name = p.tokSlice(name_tok); - const interned_name = try StrInt.intern(p.comp, name); + const interned_name = try p.comp.internString(name); if (interned_name == p.auto_type_decl_name) { try p.errStr(.auto_type_self_initialized, name_tok, name); return error.ParsingFailed; diff --git a/src/aro/SymbolStack.zig b/src/aro/SymbolStack.zig index be2ee20c..f23c554e 100644 --- a/src/aro/SymbolStack.zig +++ b/src/aro/SymbolStack.zig @@ -9,7 +9,7 @@ const NodeIndex = Tree.NodeIndex; const Type = @import("Type.zig"); const Parser = @import("Parser.zig"); const Value = @import("Value.zig"); -const StringId = @import("StringInterner.zig").StringId; +const StringId = @import("backend").StringInterner.StringId; const SymbolStack = @This(); diff --git a/src/aro/Tree.zig b/src/aro/Tree.zig index 3bab9453..21640829 100644 --- a/src/aro/Tree.zig +++ b/src/aro/Tree.zig @@ -1,5 +1,7 @@ const std = @import("std"); -const Interner = @import("backend").Interner; +const backend = @import("backend"); +const Interner = backend.Interner; +const StringInterner = backend.StringInterner; const Attribute = @import("Attribute.zig"); const CodeGen = @import("CodeGen.zig"); const Compilation = @import("Compilation.zig"); @@ -8,7 +10,6 @@ const Source = @import("Source.zig"); const Tokenizer = @import("Tokenizer.zig"); const Type = @import("Type.zig"); const Value = @import("Value.zig"); -const StringInterner = @import("StringInterner.zig"); pub const Token = struct { id: Id, diff --git a/src/aro/Type.zig b/src/aro/Type.zig index 8ab2d316..a7b038f4 100644 --- a/src/aro/Type.zig +++ b/src/aro/Type.zig @@ -5,7 +5,7 @@ const NodeIndex = Tree.NodeIndex; const Parser = @import("Parser.zig"); const Compilation = @import("Compilation.zig"); const Attribute = @import("Attribute.zig"); -const StringInterner = @import("StringInterner.zig"); +const StringInterner = @import("backend").StringInterner; const StringId = StringInterner.StringId; const target_util = @import("target.zig"); const LangOpts = @import("LangOpts.zig"); diff --git a/src/backend.zig b/src/backend.zig index 24f4e27f..804a28af 100644 --- a/src/backend.zig +++ b/src/backend.zig @@ -2,6 +2,7 @@ pub const Interner = @import("backend/Interner.zig"); pub const Ir = @import("backend/Ir.zig"); pub const Object = @import("backend/Object.zig"); pub const CodeGenOptions = @import("backend/CodeGenOptions.zig"); +pub const StringInterner = @import("backend/StringInterner.zig"); pub const CallingConvention = enum { C, diff --git a/src/aro/StringInterner.zig b/src/backend/StringInterner.zig similarity index 93% rename from src/aro/StringInterner.zig rename to src/backend/StringInterner.zig index b6e0cd79..cf0270c4 100644 --- a/src/aro/StringInterner.zig +++ b/src/backend/StringInterner.zig @@ -1,6 +1,5 @@ const std = @import("std"); const mem = std.mem; -const Compilation = @import("Compilation.zig"); const StringToIdMap = std.StringHashMapUnmanaged(StringId); @@ -51,10 +50,6 @@ pub fn deinit(self: *StringInterner, allocator: mem.Allocator) void { self.string_table.deinit(allocator); } -pub fn intern(comp: *Compilation, str: []const u8) !StringId { - return comp.string_interner.internExtra(comp.gpa, str); -} - pub fn internExtra(self: *StringInterner, allocator: mem.Allocator, str: []const u8) !StringId { if (str.len == 0) return .empty;