diff options
| author | Luuk de Gram <luuk@degram.dev> | 2024-02-09 16:36:09 +0100 |
|---|---|---|
| committer | Luuk de Gram <luuk@degram.dev> | 2024-02-29 15:24:07 +0100 |
| commit | 5ef832133895fd69fc8378463b86759eaab6913a (patch) | |
| tree | 08bc27cd8f20de4c928ca0bdc411b6c8d59be465 /src/arch | |
| parent | c99ef23862573269ae4052bd2236f9803f9e36a2 (diff) | |
| download | zig-5ef832133895fd69fc8378463b86759eaab6913a.tar.gz zig-5ef832133895fd69fc8378463b86759eaab6913a.zip | |
wasm: make symbol indexes a non-exhaustive enum
This introduces some type safety so we cannot accidently give an atom
index as a symbol index. This also means we do not have to store any
optionals and therefore allow for memory optimizations. Lastly, we can
now always simply access the symbol index of an atom, rather than having
to call `getSymbolIndex` as it is easy to forget.
Diffstat (limited to 'src/arch')
| -rw-r--r-- | src/arch/wasm/CodeGen.zig | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index 5440147296..361fd96374 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -1286,8 +1286,9 @@ fn genFunc(func: *CodeGen) InnerError!void { var prologue = std.ArrayList(Mir.Inst).init(func.gpa); defer prologue.deinit(); + const sp = @intFromEnum(func.bin_file.zigObjectPtr().?.stack_pointer_sym); // load stack pointer - try prologue.append(.{ .tag = .global_get, .data = .{ .label = 0 } }); + try prologue.append(.{ .tag = .global_get, .data = .{ .label = sp } }); // store stack pointer so we can restore it when we return from the function try prologue.append(.{ .tag = .local_tee, .data = .{ .label = func.initial_stack_value.local.value } }); // get the total stack size @@ -1303,7 +1304,7 @@ fn genFunc(func: *CodeGen) InnerError!void { try prologue.append(.{ .tag = .local_tee, .data = .{ .label = func.bottom_stack_value.local.value } }); // Store the current stack pointer value into the global stack pointer so other function calls will // start from this value instead and not overwrite the current stack. - try prologue.append(.{ .tag = .global_set, .data = .{ .label = 0 } }); + try prologue.append(.{ .tag = .global_set, .data = .{ .label = sp } }); // reserve space and insert all prologue instructions at the front of the instruction list // We insert them in reserve order as there is no insertSlice in multiArrayList. @@ -1502,7 +1503,7 @@ fn restoreStackPointer(func: *CodeGen) !void { try func.emitWValue(func.initial_stack_value); // save its value in the global stack pointer - try func.addLabel(.global_set, 0); + try func.addLabel(.global_set, @intFromEnum(func.bin_file.zigObjectPtr().?.stack_pointer_sym)); } /// From a given type, will create space on the virtual stack to store the value of such type. @@ -2205,7 +2206,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif const type_index = try func.bin_file.storeDeclType(extern_func.decl, func_type); try func.bin_file.addOrUpdateImport( mod.intern_pool.stringToSlice(ext_decl.name), - atom.getSymbolIndex().?, + atom.sym_index, mod.intern_pool.stringToSliceUnwrap(ext_decl.getOwnedExternFunc(mod).?.lib_name), type_index, ); @@ -2240,7 +2241,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif if (callee) |direct| { const atom_index = func.bin_file.zigObjectPtr().?.decls_map.get(direct).?.atom; - try func.addLabel(.call, func.bin_file.getAtom(atom_index).sym_index); + try func.addLabel(.call, @intFromEnum(func.bin_file.getAtom(atom_index).sym_index)); } else { // in this case we call a function pointer // so load its value onto the stack @@ -3158,7 +3159,7 @@ fn lowerAnonDeclRef( }, } const target_atom_index = func.bin_file.zigObjectPtr().?.anon_decls.get(decl_val).?; - const target_sym_index = func.bin_file.getAtom(target_atom_index).getSymbolIndex().?; + const target_sym_index = @intFromEnum(func.bin_file.getAtom(target_atom_index).sym_index); if (is_fn_body) { return WValue{ .function_index = target_sym_index }; } else if (offset == 0) { @@ -3189,7 +3190,7 @@ fn lowerDeclRefValue(func: *CodeGen, tv: TypedValue, decl_index: InternPool.Decl const atom_index = try func.bin_file.getOrCreateAtomForDecl(decl_index); const atom = func.bin_file.getAtom(atom_index); - const target_sym_index = atom.sym_index; + const target_sym_index = @intFromEnum(atom.sym_index); if (decl.ty.zigTypeTag(mod) == .Fn) { return WValue{ .function_index = target_sym_index }; } else if (offset == 0) { @@ -3711,7 +3712,7 @@ fn airCmpLtErrorsLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; const operand = try func.resolveInst(un_op); const sym_index = try func.bin_file.getGlobalSymbol("__zig_errors_len", null); - const errors_len = WValue{ .memory = sym_index }; + const errors_len = WValue{ .memory = @intFromEnum(sym_index) }; try func.emitWValue(operand); const mod = func.bin_file.base.comp.module.?; @@ -7153,7 +7154,7 @@ fn callIntrinsic( args: []const WValue, ) InnerError!WValue { assert(param_types.len == args.len); - const symbol_index = func.bin_file.base.getGlobalSymbol(name, null) catch |err| { + const symbol_index = func.bin_file.getGlobalSymbol(name, null) catch |err| { return func.fail("Could not find or create global symbol '{s}'", .{@errorName(err)}); }; @@ -7181,7 +7182,7 @@ fn callIntrinsic( } // Actually call our intrinsic - try func.addLabel(.call, symbol_index); + try func.addLabel(.call, @intFromEnum(symbol_index)); if (!return_type.hasRuntimeBitsIgnoreComptime(mod)) { return WValue.none; @@ -7224,7 +7225,7 @@ fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 { // check if we already generated code for this. if (func.bin_file.findGlobalSymbol(func_name)) |loc| { - return loc.index; + return @intFromEnum(loc.index); } const int_tag_ty = enum_ty.intTagType(mod); @@ -7364,7 +7365,8 @@ fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 { const slice_ty = Type.slice_const_u8_sentinel_0; const func_type = try genFunctype(arena, .Unspecified, &.{int_tag_ty.ip_index}, slice_ty, mod); - return func.bin_file.createFunction(func_name, func_type, &body_list, &relocs); + const sym_index = try func.bin_file.createFunction(func_name, func_type, &body_list, &relocs); + return @intFromEnum(sym_index); } fn airErrorSetHasValue(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
