| ... | ... | @@ -8,6 +8,8 @@ const OP = std.dwarf.OP; |
| 8 | 8 | const abi = std.debug.Dwarf.abi; |
| 9 | 9 | const mem = std.mem; |
| 10 | 10 | const assert = std.debug.assert; |
| 11 | const testing = std.testing; |
| 12 | const Writer = std.Io.Writer; |
| 11 | 13 | |
| 12 | 14 | /// Expressions can be evaluated in different contexts, each requiring its own set of inputs. |
| 13 | 15 | /// Callers should specify all the fields relevant to their context. If a field is required |
| ... | ... | @@ -782,7 +784,7 @@ pub fn Builder(comptime options: Options) type { |
| 782 | 784 | |
| 783 | 785 | return struct { |
| 784 | 786 | /// Zero-operand instructions |
| 785 | | pub fn writeOpcode(writer: anytype, comptime opcode: u8) !void { |
| 787 | pub fn writeOpcode(writer: *Writer, comptime opcode: u8) !void { |
| 786 | 788 | if (options.call_frame_context and !comptime isOpcodeValidInCFA(opcode)) return error.InvalidCFAOpcode; |
| 787 | 789 | switch (opcode) { |
| 788 | 790 | OP.dup, |
| ... | ... | @@ -823,14 +825,14 @@ pub fn Builder(comptime options: Options) type { |
| 823 | 825 | } |
| 824 | 826 | |
| 825 | 827 | // 2.5.1.1: Literal Encodings |
| 826 | | pub fn writeLiteral(writer: anytype, literal: u8) !void { |
| 828 | pub fn writeLiteral(writer: *Writer, literal: u8) !void { |
| 827 | 829 | switch (literal) { |
| 828 | 830 | 0...31 => |n| try writer.writeByte(n + OP.lit0), |
| 829 | 831 | else => return error.InvalidLiteral, |
| 830 | 832 | } |
| 831 | 833 | } |
| 832 | 834 | |
| 833 | | pub fn writeConst(writer: anytype, comptime T: type, value: T) !void { |
| 835 | pub fn writeConst(writer: *Writer, comptime T: type, value: T) !void { |
| 834 | 836 | if (@typeInfo(T) != .int) @compileError("Constants must be integers"); |
| 835 | 837 | |
| 836 | 838 | switch (T) { |
| ... | ... | @@ -852,7 +854,7 @@ pub fn Builder(comptime options: Options) type { |
| 852 | 854 | else => switch (@typeInfo(T).int.signedness) { |
| 853 | 855 | .unsigned => { |
| 854 | 856 | try writer.writeByte(OP.constu); |
| 855 | | try leb.writeUleb128(writer, value); |
| 857 | try writer.writeUleb128(value); |
| 856 | 858 | }, |
| 857 | 859 | .signed => { |
| 858 | 860 | try writer.writeByte(OP.consts); |
| ... | ... | @@ -862,105 +864,105 @@ pub fn Builder(comptime options: Options) type { |
| 862 | 864 | } |
| 863 | 865 | } |
| 864 | 866 | |
| 865 | | pub fn writeConstx(writer: anytype, debug_addr_offset: anytype) !void { |
| 867 | pub fn writeConstx(writer: *Writer, debug_addr_offset: anytype) !void { |
| 866 | 868 | try writer.writeByte(OP.constx); |
| 867 | | try leb.writeUleb128(writer, debug_addr_offset); |
| 869 | try writer.writeUleb128(debug_addr_offset); |
| 868 | 870 | } |
| 869 | 871 | |
| 870 | | pub fn writeConstType(writer: anytype, die_offset: anytype, value_bytes: []const u8) !void { |
| 872 | pub fn writeConstType(writer: *Writer, die_offset: anytype, value_bytes: []const u8) !void { |
| 871 | 873 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 872 | 874 | if (value_bytes.len > 0xff) return error.InvalidTypeLength; |
| 873 | 875 | try writer.writeByte(OP.const_type); |
| 874 | | try leb.writeUleb128(writer, die_offset); |
| 876 | try writer.writeUleb128(die_offset); |
| 875 | 877 | try writer.writeByte(@intCast(value_bytes.len)); |
| 876 | 878 | try writer.writeAll(value_bytes); |
| 877 | 879 | } |
| 878 | 880 | |
| 879 | | pub fn writeAddr(writer: anytype, value: addr_type) !void { |
| 881 | pub fn writeAddr(writer: *Writer, value: addr_type) !void { |
| 880 | 882 | try writer.writeByte(OP.addr); |
| 881 | 883 | try writer.writeInt(addr_type, value, options.endian); |
| 882 | 884 | } |
| 883 | 885 | |
| 884 | | pub fn writeAddrx(writer: anytype, debug_addr_offset: anytype) !void { |
| 886 | pub fn writeAddrx(writer: *Writer, debug_addr_offset: anytype) !void { |
| 885 | 887 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 886 | 888 | try writer.writeByte(OP.addrx); |
| 887 | | try leb.writeUleb128(writer, debug_addr_offset); |
| 889 | try writer.writeUleb128(debug_addr_offset); |
| 888 | 890 | } |
| 889 | 891 | |
| 890 | 892 | // 2.5.1.2: Register Values |
| 891 | | pub fn writeFbreg(writer: anytype, offset: anytype) !void { |
| 893 | pub fn writeFbreg(writer: *Writer, offset: anytype) !void { |
| 892 | 894 | try writer.writeByte(OP.fbreg); |
| 893 | 895 | try leb.writeIleb128(writer, offset); |
| 894 | 896 | } |
| 895 | 897 | |
| 896 | | pub fn writeBreg(writer: anytype, register: u8, offset: anytype) !void { |
| 898 | pub fn writeBreg(writer: *Writer, register: u8, offset: anytype) !void { |
| 897 | 899 | if (register > 31) return error.InvalidRegister; |
| 898 | 900 | try writer.writeByte(OP.breg0 + register); |
| 899 | 901 | try leb.writeIleb128(writer, offset); |
| 900 | 902 | } |
| 901 | 903 | |
| 902 | | pub fn writeBregx(writer: anytype, register: anytype, offset: anytype) !void { |
| 904 | pub fn writeBregx(writer: *Writer, register: anytype, offset: anytype) !void { |
| 903 | 905 | try writer.writeByte(OP.bregx); |
| 904 | | try leb.writeUleb128(writer, register); |
| 906 | try writer.writeUleb128(register); |
| 905 | 907 | try leb.writeIleb128(writer, offset); |
| 906 | 908 | } |
| 907 | 909 | |
| 908 | | pub fn writeRegvalType(writer: anytype, register: anytype, offset: anytype) !void { |
| 910 | pub fn writeRegvalType(writer: *Writer, register: anytype, offset: anytype) !void { |
| 909 | 911 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 910 | 912 | try writer.writeByte(OP.regval_type); |
| 911 | | try leb.writeUleb128(writer, register); |
| 912 | | try leb.writeUleb128(writer, offset); |
| 913 | try writer.writeUleb128(register); |
| 914 | try writer.writeUleb128(offset); |
| 913 | 915 | } |
| 914 | 916 | |
| 915 | 917 | // 2.5.1.3: Stack Operations |
| 916 | | pub fn writePick(writer: anytype, index: u8) !void { |
| 918 | pub fn writePick(writer: *Writer, index: u8) !void { |
| 917 | 919 | try writer.writeByte(OP.pick); |
| 918 | 920 | try writer.writeByte(index); |
| 919 | 921 | } |
| 920 | 922 | |
| 921 | | pub fn writeDerefSize(writer: anytype, size: u8) !void { |
| 923 | pub fn writeDerefSize(writer: *Writer, size: u8) !void { |
| 922 | 924 | try writer.writeByte(OP.deref_size); |
| 923 | 925 | try writer.writeByte(size); |
| 924 | 926 | } |
| 925 | 927 | |
| 926 | | pub fn writeXDerefSize(writer: anytype, size: u8) !void { |
| 928 | pub fn writeXDerefSize(writer: *Writer, size: u8) !void { |
| 927 | 929 | try writer.writeByte(OP.xderef_size); |
| 928 | 930 | try writer.writeByte(size); |
| 929 | 931 | } |
| 930 | 932 | |
| 931 | | pub fn writeDerefType(writer: anytype, size: u8, die_offset: anytype) !void { |
| 933 | pub fn writeDerefType(writer: *Writer, size: u8, die_offset: anytype) !void { |
| 932 | 934 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 933 | 935 | try writer.writeByte(OP.deref_type); |
| 934 | 936 | try writer.writeByte(size); |
| 935 | | try leb.writeUleb128(writer, die_offset); |
| 937 | try writer.writeUleb128(die_offset); |
| 936 | 938 | } |
| 937 | 939 | |
| 938 | | pub fn writeXDerefType(writer: anytype, size: u8, die_offset: anytype) !void { |
| 940 | pub fn writeXDerefType(writer: *Writer, size: u8, die_offset: anytype) !void { |
| 939 | 941 | try writer.writeByte(OP.xderef_type); |
| 940 | 942 | try writer.writeByte(size); |
| 941 | | try leb.writeUleb128(writer, die_offset); |
| 943 | try writer.writeUleb128(die_offset); |
| 942 | 944 | } |
| 943 | 945 | |
| 944 | 946 | // 2.5.1.4: Arithmetic and Logical Operations |
| 945 | 947 | |
| 946 | | pub fn writePlusUconst(writer: anytype, uint_value: anytype) !void { |
| 948 | pub fn writePlusUconst(writer: *Writer, uint_value: anytype) !void { |
| 947 | 949 | try writer.writeByte(OP.plus_uconst); |
| 948 | | try leb.writeUleb128(writer, uint_value); |
| 950 | try writer.writeUleb128(uint_value); |
| 949 | 951 | } |
| 950 | 952 | |
| 951 | 953 | // 2.5.1.5: Control Flow Operations |
| 952 | 954 | |
| 953 | | pub fn writeSkip(writer: anytype, offset: i16) !void { |
| 955 | pub fn writeSkip(writer: *Writer, offset: i16) !void { |
| 954 | 956 | try writer.writeByte(OP.skip); |
| 955 | 957 | try writer.writeInt(i16, offset, options.endian); |
| 956 | 958 | } |
| 957 | 959 | |
| 958 | | pub fn writeBra(writer: anytype, offset: i16) !void { |
| 960 | pub fn writeBra(writer: *Writer, offset: i16) !void { |
| 959 | 961 | try writer.writeByte(OP.bra); |
| 960 | 962 | try writer.writeInt(i16, offset, options.endian); |
| 961 | 963 | } |
| 962 | 964 | |
| 963 | | pub fn writeCall(writer: anytype, comptime T: type, offset: T) !void { |
| 965 | pub fn writeCall(writer: *Writer, comptime T: type, offset: T) !void { |
| 964 | 966 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 965 | 967 | switch (T) { |
| 966 | 968 | u16 => try writer.writeByte(OP.call2), |
| ... | ... | @@ -971,45 +973,45 @@ pub fn Builder(comptime options: Options) type { |
| 971 | 973 | try writer.writeInt(T, offset, options.endian); |
| 972 | 974 | } |
| 973 | 975 | |
| 974 | | pub fn writeCallRef(writer: anytype, comptime is_64: bool, value: if (is_64) u64 else u32) !void { |
| 976 | pub fn writeCallRef(writer: *Writer, comptime is_64: bool, value: if (is_64) u64 else u32) !void { |
| 975 | 977 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 976 | 978 | try writer.writeByte(OP.call_ref); |
| 977 | 979 | try writer.writeInt(if (is_64) u64 else u32, value, options.endian); |
| 978 | 980 | } |
| 979 | 981 | |
| 980 | | pub fn writeConvert(writer: anytype, die_offset: anytype) !void { |
| 982 | pub fn writeConvert(writer: *Writer, die_offset: anytype) !void { |
| 981 | 983 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 982 | 984 | try writer.writeByte(OP.convert); |
| 983 | | try leb.writeUleb128(writer, die_offset); |
| 985 | try writer.writeUleb128(die_offset); |
| 984 | 986 | } |
| 985 | 987 | |
| 986 | | pub fn writeReinterpret(writer: anytype, die_offset: anytype) !void { |
| 988 | pub fn writeReinterpret(writer: *Writer, die_offset: anytype) !void { |
| 987 | 989 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 988 | 990 | try writer.writeByte(OP.reinterpret); |
| 989 | | try leb.writeUleb128(writer, die_offset); |
| 991 | try writer.writeUleb128(die_offset); |
| 990 | 992 | } |
| 991 | 993 | |
| 992 | 994 | // 2.5.1.7: Special Operations |
| 993 | 995 | |
| 994 | | pub fn writeEntryValue(writer: anytype, expression: []const u8) !void { |
| 996 | pub fn writeEntryValue(writer: *Writer, expression: []const u8) !void { |
| 995 | 997 | try writer.writeByte(OP.entry_value); |
| 996 | | try leb.writeUleb128(writer, expression.len); |
| 998 | try writer.writeUleb128(expression.len); |
| 997 | 999 | try writer.writeAll(expression); |
| 998 | 1000 | } |
| 999 | 1001 | |
| 1000 | 1002 | // 2.6: Location Descriptions |
| 1001 | | pub fn writeReg(writer: anytype, register: u8) !void { |
| 1003 | pub fn writeReg(writer: *Writer, register: u8) !void { |
| 1002 | 1004 | try writer.writeByte(OP.reg0 + register); |
| 1003 | 1005 | } |
| 1004 | 1006 | |
| 1005 | | pub fn writeRegx(writer: anytype, register: anytype) !void { |
| 1007 | pub fn writeRegx(writer: *Writer, register: anytype) !void { |
| 1006 | 1008 | try writer.writeByte(OP.regx); |
| 1007 | | try leb.writeUleb128(writer, register); |
| 1009 | try writer.writeUleb128(register); |
| 1008 | 1010 | } |
| 1009 | 1011 | |
| 1010 | | pub fn writeImplicitValue(writer: anytype, value_bytes: []const u8) !void { |
| 1012 | pub fn writeImplicitValue(writer: *Writer, value_bytes: []const u8) !void { |
| 1011 | 1013 | try writer.writeByte(OP.implicit_value); |
| 1012 | | try leb.writeUleb128(writer, value_bytes.len); |
| 1014 | try writer.writeUleb128(value_bytes.len); |
| 1013 | 1015 | try writer.writeAll(value_bytes); |
| 1014 | 1016 | } |
| 1015 | 1017 | }; |
| ... | ... | @@ -1042,8 +1044,7 @@ fn isOpcodeRegisterLocation(opcode: u8) bool { |
| 1042 | 1044 | }; |
| 1043 | 1045 | } |
| 1044 | 1046 | |
| 1045 | | const testing = std.testing; |
| 1046 | | test "DWARF expressions" { |
| 1047 | test "basics" { |
| 1047 | 1048 | const allocator = std.testing.allocator; |
| 1048 | 1049 | |
| 1049 | 1050 | const options = Options{}; |
| ... | ... | @@ -1052,10 +1053,10 @@ test "DWARF expressions" { |
| 1052 | 1053 | |
| 1053 | 1054 | const b = Builder(options); |
| 1054 | 1055 | |
| 1055 | | var program = std.array_list.Managed(u8).init(allocator); |
| 1056 | var program: std.Io.Writer.Allocating = .init(allocator); |
| 1056 | 1057 | defer program.deinit(); |
| 1057 | 1058 | |
| 1058 | | const writer = program.writer(); |
| 1059 | const writer = &program.writer; |
| 1059 | 1060 | |
| 1060 | 1061 | // Literals |
| 1061 | 1062 | { |
| ... | ... | @@ -1064,7 +1065,7 @@ test "DWARF expressions" { |
| 1064 | 1065 | try b.writeLiteral(writer, @intCast(i)); |
| 1065 | 1066 | } |
| 1066 | 1067 | |
| 1067 | | _ = try stack_machine.run(program.items, allocator, context, 0); |
| 1068 | _ = try stack_machine.run(program.written(), allocator, context, 0); |
| 1068 | 1069 | |
| 1069 | 1070 | for (0..32) |i| { |
| 1070 | 1071 | const expected = 31 - i; |
| ... | ... | @@ -1108,16 +1109,16 @@ test "DWARF expressions" { |
| 1108 | 1109 | var mock_compile_unit: std.debug.Dwarf.CompileUnit = undefined; |
| 1109 | 1110 | mock_compile_unit.addr_base = 1; |
| 1110 | 1111 | |
| 1111 | | var mock_debug_addr = std.array_list.Managed(u8).init(allocator); |
| 1112 | var mock_debug_addr: std.Io.Writer.Allocating = .init(allocator); |
| 1112 | 1113 | defer mock_debug_addr.deinit(); |
| 1113 | 1114 | |
| 1114 | | try mock_debug_addr.writer().writeInt(u16, 0, native_endian); |
| 1115 | | try mock_debug_addr.writer().writeInt(usize, input[11], native_endian); |
| 1116 | | try mock_debug_addr.writer().writeInt(usize, input[12], native_endian); |
| 1115 | try mock_debug_addr.writer.writeInt(u16, 0, native_endian); |
| 1116 | try mock_debug_addr.writer.writeInt(usize, input[11], native_endian); |
| 1117 | try mock_debug_addr.writer.writeInt(usize, input[12], native_endian); |
| 1117 | 1118 | |
| 1118 | | const context = Context{ |
| 1119 | const context: Context = .{ |
| 1119 | 1120 | .compile_unit = &mock_compile_unit, |
| 1120 | | .debug_addr = mock_debug_addr.items, |
| 1121 | .debug_addr = mock_debug_addr.written(), |
| 1121 | 1122 | }; |
| 1122 | 1123 | |
| 1123 | 1124 | try b.writeConstx(writer, @as(usize, 1)); |
| ... | ... | @@ -1127,7 +1128,7 @@ test "DWARF expressions" { |
| 1127 | 1128 | const type_bytes: []const u8 = &.{ 1, 2, 3, 4 }; |
| 1128 | 1129 | try b.writeConstType(writer, die_offset, type_bytes); |
| 1129 | 1130 | |
| 1130 | | _ = try stack_machine.run(program.items, allocator, context, 0); |
| 1131 | _ = try stack_machine.run(program.written(), allocator, context, 0); |
| 1131 | 1132 | |
| 1132 | 1133 | const const_type = stack_machine.stack.pop().?.const_type; |
| 1133 | 1134 | try testing.expectEqual(die_offset, const_type.type_offset); |
| ... | ... | @@ -1185,7 +1186,7 @@ test "DWARF expressions" { |
| 1185 | 1186 | try b.writeBregx(writer, abi.ipRegNum(native_arch).?, @as(usize, 300)); |
| 1186 | 1187 | try b.writeRegvalType(writer, @as(u8, 0), @as(usize, 400)); |
| 1187 | 1188 | |
| 1188 | | _ = try stack_machine.run(program.items, allocator, context, 0); |
| 1189 | _ = try stack_machine.run(program.written(), allocator, context, 0); |
| 1189 | 1190 | |
| 1190 | 1191 | const regval_type = stack_machine.stack.pop().?.regval_type; |
| 1191 | 1192 | try testing.expectEqual(@as(usize, 400), regval_type.type_offset); |
| ... | ... | @@ -1214,7 +1215,7 @@ test "DWARF expressions" { |
| 1214 | 1215 | program.clearRetainingCapacity(); |
| 1215 | 1216 | try b.writeConst(writer, u8, 1); |
| 1216 | 1217 | try b.writeOpcode(writer, OP.dup); |
| 1217 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1218 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1218 | 1219 | try testing.expectEqual(@as(usize, 1), stack_machine.stack.pop().?.generic); |
| 1219 | 1220 | try testing.expectEqual(@as(usize, 1), stack_machine.stack.pop().?.generic); |
| 1220 | 1221 | |
| ... | ... | @@ -1222,7 +1223,7 @@ test "DWARF expressions" { |
| 1222 | 1223 | program.clearRetainingCapacity(); |
| 1223 | 1224 | try b.writeConst(writer, u8, 1); |
| 1224 | 1225 | try b.writeOpcode(writer, OP.drop); |
| 1225 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1226 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1226 | 1227 | try testing.expect(stack_machine.stack.pop() == null); |
| 1227 | 1228 | |
| 1228 | 1229 | stack_machine.reset(); |
| ... | ... | @@ -1231,7 +1232,7 @@ test "DWARF expressions" { |
| 1231 | 1232 | try b.writeConst(writer, u8, 5); |
| 1232 | 1233 | try b.writeConst(writer, u8, 6); |
| 1233 | 1234 | try b.writePick(writer, 2); |
| 1234 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1235 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1235 | 1236 | try testing.expectEqual(@as(usize, 4), stack_machine.stack.pop().?.generic); |
| 1236 | 1237 | |
| 1237 | 1238 | stack_machine.reset(); |
| ... | ... | @@ -1240,7 +1241,7 @@ test "DWARF expressions" { |
| 1240 | 1241 | try b.writeConst(writer, u8, 5); |
| 1241 | 1242 | try b.writeConst(writer, u8, 6); |
| 1242 | 1243 | try b.writeOpcode(writer, OP.over); |
| 1243 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1244 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1244 | 1245 | try testing.expectEqual(@as(usize, 5), stack_machine.stack.pop().?.generic); |
| 1245 | 1246 | |
| 1246 | 1247 | stack_machine.reset(); |
| ... | ... | @@ -1248,7 +1249,7 @@ test "DWARF expressions" { |
| 1248 | 1249 | try b.writeConst(writer, u8, 5); |
| 1249 | 1250 | try b.writeConst(writer, u8, 6); |
| 1250 | 1251 | try b.writeOpcode(writer, OP.swap); |
| 1251 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1252 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1252 | 1253 | try testing.expectEqual(@as(usize, 5), stack_machine.stack.pop().?.generic); |
| 1253 | 1254 | try testing.expectEqual(@as(usize, 6), stack_machine.stack.pop().?.generic); |
| 1254 | 1255 | |
| ... | ... | @@ -1258,7 +1259,7 @@ test "DWARF expressions" { |
| 1258 | 1259 | try b.writeConst(writer, u8, 5); |
| 1259 | 1260 | try b.writeConst(writer, u8, 6); |
| 1260 | 1261 | try b.writeOpcode(writer, OP.rot); |
| 1261 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1262 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1262 | 1263 | try testing.expectEqual(@as(usize, 5), stack_machine.stack.pop().?.generic); |
| 1263 | 1264 | try testing.expectEqual(@as(usize, 4), stack_machine.stack.pop().?.generic); |
| 1264 | 1265 | try testing.expectEqual(@as(usize, 6), stack_machine.stack.pop().?.generic); |
| ... | ... | @@ -1269,7 +1270,7 @@ test "DWARF expressions" { |
| 1269 | 1270 | program.clearRetainingCapacity(); |
| 1270 | 1271 | try b.writeAddr(writer, @intFromPtr(&deref_target)); |
| 1271 | 1272 | try b.writeOpcode(writer, OP.deref); |
| 1272 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1273 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1273 | 1274 | try testing.expectEqual(deref_target, stack_machine.stack.pop().?.generic); |
| 1274 | 1275 | |
| 1275 | 1276 | stack_machine.reset(); |
| ... | ... | @@ -1277,14 +1278,14 @@ test "DWARF expressions" { |
| 1277 | 1278 | try b.writeLiteral(writer, 0); |
| 1278 | 1279 | try b.writeAddr(writer, @intFromPtr(&deref_target)); |
| 1279 | 1280 | try b.writeOpcode(writer, OP.xderef); |
| 1280 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1281 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1281 | 1282 | try testing.expectEqual(deref_target, stack_machine.stack.pop().?.generic); |
| 1282 | 1283 | |
| 1283 | 1284 | stack_machine.reset(); |
| 1284 | 1285 | program.clearRetainingCapacity(); |
| 1285 | 1286 | try b.writeAddr(writer, @intFromPtr(&deref_target)); |
| 1286 | 1287 | try b.writeDerefSize(writer, 1); |
| 1287 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1288 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1288 | 1289 | try testing.expectEqual(@as(usize, @as(*const u8, @ptrCast(&deref_target)).*), stack_machine.stack.pop().?.generic); |
| 1289 | 1290 | |
| 1290 | 1291 | stack_machine.reset(); |
| ... | ... | @@ -1292,7 +1293,7 @@ test "DWARF expressions" { |
| 1292 | 1293 | try b.writeLiteral(writer, 0); |
| 1293 | 1294 | try b.writeAddr(writer, @intFromPtr(&deref_target)); |
| 1294 | 1295 | try b.writeXDerefSize(writer, 1); |
| 1295 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1296 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1296 | 1297 | try testing.expectEqual(@as(usize, @as(*const u8, @ptrCast(&deref_target)).*), stack_machine.stack.pop().?.generic); |
| 1297 | 1298 | |
| 1298 | 1299 | const type_offset: usize = @truncate(0xaabbaabb_aabbaabb); |
| ... | ... | @@ -1301,7 +1302,7 @@ test "DWARF expressions" { |
| 1301 | 1302 | program.clearRetainingCapacity(); |
| 1302 | 1303 | try b.writeAddr(writer, @intFromPtr(&deref_target)); |
| 1303 | 1304 | try b.writeDerefType(writer, 1, type_offset); |
| 1304 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1305 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1305 | 1306 | const deref_type = stack_machine.stack.pop().?.regval_type; |
| 1306 | 1307 | try testing.expectEqual(type_offset, deref_type.type_offset); |
| 1307 | 1308 | try testing.expectEqual(@as(u8, 1), deref_type.type_size); |
| ... | ... | @@ -1312,7 +1313,7 @@ test "DWARF expressions" { |
| 1312 | 1313 | try b.writeLiteral(writer, 0); |
| 1313 | 1314 | try b.writeAddr(writer, @intFromPtr(&deref_target)); |
| 1314 | 1315 | try b.writeXDerefType(writer, 1, type_offset); |
| 1315 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1316 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1316 | 1317 | const xderef_type = stack_machine.stack.pop().?.regval_type; |
| 1317 | 1318 | try testing.expectEqual(type_offset, xderef_type.type_offset); |
| 1318 | 1319 | try testing.expectEqual(@as(u8, 1), xderef_type.type_size); |
| ... | ... | @@ -1323,7 +1324,7 @@ test "DWARF expressions" { |
| 1323 | 1324 | stack_machine.reset(); |
| 1324 | 1325 | program.clearRetainingCapacity(); |
| 1325 | 1326 | try b.writeOpcode(writer, OP.push_object_address); |
| 1326 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1327 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1327 | 1328 | try testing.expectEqual(@as(usize, @intFromPtr(context.object_address.?)), stack_machine.stack.pop().?.generic); |
| 1328 | 1329 | |
| 1329 | 1330 | // TODO: Test OP.form_tls_address |
| ... | ... | @@ -1333,7 +1334,7 @@ test "DWARF expressions" { |
| 1333 | 1334 | stack_machine.reset(); |
| 1334 | 1335 | program.clearRetainingCapacity(); |
| 1335 | 1336 | try b.writeOpcode(writer, OP.call_frame_cfa); |
| 1336 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1337 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1337 | 1338 | try testing.expectEqual(context.cfa.?, stack_machine.stack.pop().?.generic); |
| 1338 | 1339 | } |
| 1339 | 1340 | |
| ... | ... | @@ -1345,7 +1346,7 @@ test "DWARF expressions" { |
| 1345 | 1346 | program.clearRetainingCapacity(); |
| 1346 | 1347 | try b.writeConst(writer, i16, -4096); |
| 1347 | 1348 | try b.writeOpcode(writer, OP.abs); |
| 1348 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1349 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1349 | 1350 | try testing.expectEqual(@as(usize, 4096), stack_machine.stack.pop().?.generic); |
| 1350 | 1351 | |
| 1351 | 1352 | stack_machine.reset(); |
| ... | ... | @@ -1353,7 +1354,7 @@ test "DWARF expressions" { |
| 1353 | 1354 | try b.writeConst(writer, u16, 0xff0f); |
| 1354 | 1355 | try b.writeConst(writer, u16, 0xf0ff); |
| 1355 | 1356 | try b.writeOpcode(writer, OP.@"and"); |
| 1356 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1357 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1357 | 1358 | try testing.expectEqual(@as(usize, 0xf00f), stack_machine.stack.pop().?.generic); |
| 1358 | 1359 | |
| 1359 | 1360 | stack_machine.reset(); |
| ... | ... | @@ -1361,7 +1362,7 @@ test "DWARF expressions" { |
| 1361 | 1362 | try b.writeConst(writer, i16, -404); |
| 1362 | 1363 | try b.writeConst(writer, i16, 100); |
| 1363 | 1364 | try b.writeOpcode(writer, OP.div); |
| 1364 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1365 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1365 | 1366 | try testing.expectEqual(@as(isize, -404 / 100), @as(isize, @bitCast(stack_machine.stack.pop().?.generic))); |
| 1366 | 1367 | |
| 1367 | 1368 | stack_machine.reset(); |
| ... | ... | @@ -1369,7 +1370,7 @@ test "DWARF expressions" { |
| 1369 | 1370 | try b.writeConst(writer, u16, 200); |
| 1370 | 1371 | try b.writeConst(writer, u16, 50); |
| 1371 | 1372 | try b.writeOpcode(writer, OP.minus); |
| 1372 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1373 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1373 | 1374 | try testing.expectEqual(@as(usize, 150), stack_machine.stack.pop().?.generic); |
| 1374 | 1375 | |
| 1375 | 1376 | stack_machine.reset(); |
| ... | ... | @@ -1377,7 +1378,7 @@ test "DWARF expressions" { |
| 1377 | 1378 | try b.writeConst(writer, u16, 123); |
| 1378 | 1379 | try b.writeConst(writer, u16, 100); |
| 1379 | 1380 | try b.writeOpcode(writer, OP.mod); |
| 1380 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1381 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1381 | 1382 | try testing.expectEqual(@as(usize, 23), stack_machine.stack.pop().?.generic); |
| 1382 | 1383 | |
| 1383 | 1384 | stack_machine.reset(); |
| ... | ... | @@ -1385,7 +1386,7 @@ test "DWARF expressions" { |
| 1385 | 1386 | try b.writeConst(writer, u16, 0xff); |
| 1386 | 1387 | try b.writeConst(writer, u16, 0xee); |
| 1387 | 1388 | try b.writeOpcode(writer, OP.mul); |
| 1388 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1389 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1389 | 1390 | try testing.expectEqual(@as(usize, 0xed12), stack_machine.stack.pop().?.generic); |
| 1390 | 1391 | |
| 1391 | 1392 | stack_machine.reset(); |
| ... | ... | @@ -1394,7 +1395,7 @@ test "DWARF expressions" { |
| 1394 | 1395 | try b.writeOpcode(writer, OP.neg); |
| 1395 | 1396 | try b.writeConst(writer, i16, -6); |
| 1396 | 1397 | try b.writeOpcode(writer, OP.neg); |
| 1397 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1398 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1398 | 1399 | try testing.expectEqual(@as(usize, 6), stack_machine.stack.pop().?.generic); |
| 1399 | 1400 | try testing.expectEqual(@as(isize, -5), @as(isize, @bitCast(stack_machine.stack.pop().?.generic))); |
| 1400 | 1401 | |
| ... | ... | @@ -1402,7 +1403,7 @@ test "DWARF expressions" { |
| 1402 | 1403 | program.clearRetainingCapacity(); |
| 1403 | 1404 | try b.writeConst(writer, u16, 0xff0f); |
| 1404 | 1405 | try b.writeOpcode(writer, OP.not); |
| 1405 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1406 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1406 | 1407 | try testing.expectEqual(~@as(usize, 0xff0f), stack_machine.stack.pop().?.generic); |
| 1407 | 1408 | |
| 1408 | 1409 | stack_machine.reset(); |
| ... | ... | @@ -1410,7 +1411,7 @@ test "DWARF expressions" { |
| 1410 | 1411 | try b.writeConst(writer, u16, 0xff0f); |
| 1411 | 1412 | try b.writeConst(writer, u16, 0xf0ff); |
| 1412 | 1413 | try b.writeOpcode(writer, OP.@"or"); |
| 1413 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1414 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1414 | 1415 | try testing.expectEqual(@as(usize, 0xffff), stack_machine.stack.pop().?.generic); |
| 1415 | 1416 | |
| 1416 | 1417 | stack_machine.reset(); |
| ... | ... | @@ -1418,14 +1419,14 @@ test "DWARF expressions" { |
| 1418 | 1419 | try b.writeConst(writer, i16, 402); |
| 1419 | 1420 | try b.writeConst(writer, i16, 100); |
| 1420 | 1421 | try b.writeOpcode(writer, OP.plus); |
| 1421 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1422 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1422 | 1423 | try testing.expectEqual(@as(usize, 502), stack_machine.stack.pop().?.generic); |
| 1423 | 1424 | |
| 1424 | 1425 | stack_machine.reset(); |
| 1425 | 1426 | program.clearRetainingCapacity(); |
| 1426 | 1427 | try b.writeConst(writer, u16, 4096); |
| 1427 | 1428 | try b.writePlusUconst(writer, @as(usize, 8192)); |
| 1428 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1429 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1429 | 1430 | try testing.expectEqual(@as(usize, 4096 + 8192), stack_machine.stack.pop().?.generic); |
| 1430 | 1431 | |
| 1431 | 1432 | stack_machine.reset(); |
| ... | ... | @@ -1433,7 +1434,7 @@ test "DWARF expressions" { |
| 1433 | 1434 | try b.writeConst(writer, u16, 0xfff); |
| 1434 | 1435 | try b.writeConst(writer, u16, 1); |
| 1435 | 1436 | try b.writeOpcode(writer, OP.shl); |
| 1436 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1437 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1437 | 1438 | try testing.expectEqual(@as(usize, 0xfff << 1), stack_machine.stack.pop().?.generic); |
| 1438 | 1439 | |
| 1439 | 1440 | stack_machine.reset(); |
| ... | ... | @@ -1441,7 +1442,7 @@ test "DWARF expressions" { |
| 1441 | 1442 | try b.writeConst(writer, u16, 0xfff); |
| 1442 | 1443 | try b.writeConst(writer, u16, 1); |
| 1443 | 1444 | try b.writeOpcode(writer, OP.shr); |
| 1444 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1445 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1445 | 1446 | try testing.expectEqual(@as(usize, 0xfff >> 1), stack_machine.stack.pop().?.generic); |
| 1446 | 1447 | |
| 1447 | 1448 | stack_machine.reset(); |
| ... | ... | @@ -1449,7 +1450,7 @@ test "DWARF expressions" { |
| 1449 | 1450 | try b.writeConst(writer, u16, 0xfff); |
| 1450 | 1451 | try b.writeConst(writer, u16, 1); |
| 1451 | 1452 | try b.writeOpcode(writer, OP.shr); |
| 1452 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1453 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1453 | 1454 | try testing.expectEqual(@as(usize, @bitCast(@as(isize, 0xfff) >> 1)), stack_machine.stack.pop().?.generic); |
| 1454 | 1455 | |
| 1455 | 1456 | stack_machine.reset(); |
| ... | ... | @@ -1457,7 +1458,7 @@ test "DWARF expressions" { |
| 1457 | 1458 | try b.writeConst(writer, u16, 0xf0ff); |
| 1458 | 1459 | try b.writeConst(writer, u16, 0xff0f); |
| 1459 | 1460 | try b.writeOpcode(writer, OP.xor); |
| 1460 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1461 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1461 | 1462 | try testing.expectEqual(@as(usize, 0x0ff0), stack_machine.stack.pop().?.generic); |
| 1462 | 1463 | } |
| 1463 | 1464 | |
| ... | ... | @@ -1486,7 +1487,7 @@ test "DWARF expressions" { |
| 1486 | 1487 | try b.writeConst(writer, u16, 1); |
| 1487 | 1488 | try b.writeConst(writer, u16, 0); |
| 1488 | 1489 | try b.writeOpcode(writer, e[0]); |
| 1489 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1490 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1490 | 1491 | try testing.expectEqual(@as(usize, e[3]), stack_machine.stack.pop().?.generic); |
| 1491 | 1492 | try testing.expectEqual(@as(usize, e[2]), stack_machine.stack.pop().?.generic); |
| 1492 | 1493 | try testing.expectEqual(@as(usize, e[1]), stack_machine.stack.pop().?.generic); |
| ... | ... | @@ -1497,7 +1498,7 @@ test "DWARF expressions" { |
| 1497 | 1498 | try b.writeLiteral(writer, 2); |
| 1498 | 1499 | try b.writeSkip(writer, 1); |
| 1499 | 1500 | try b.writeLiteral(writer, 3); |
| 1500 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1501 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1501 | 1502 | try testing.expectEqual(@as(usize, 2), stack_machine.stack.pop().?.generic); |
| 1502 | 1503 | |
| 1503 | 1504 | stack_machine.reset(); |
| ... | ... | @@ -1509,7 +1510,7 @@ test "DWARF expressions" { |
| 1509 | 1510 | try b.writeBra(writer, 1); |
| 1510 | 1511 | try b.writeLiteral(writer, 4); |
| 1511 | 1512 | try b.writeLiteral(writer, 5); |
| 1512 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1513 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1513 | 1514 | try testing.expectEqual(@as(usize, 5), stack_machine.stack.pop().?.generic); |
| 1514 | 1515 | try testing.expectEqual(@as(usize, 4), stack_machine.stack.pop().?.generic); |
| 1515 | 1516 | try testing.expect(stack_machine.stack.pop() == null); |
| ... | ... | @@ -1535,7 +1536,7 @@ test "DWARF expressions" { |
| 1535 | 1536 | program.clearRetainingCapacity(); |
| 1536 | 1537 | try b.writeConstType(writer, @as(usize, 0), &value_bytes); |
| 1537 | 1538 | try b.writeConvert(writer, @as(usize, 0)); |
| 1538 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1539 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1539 | 1540 | try testing.expectEqual(value, stack_machine.stack.pop().?.generic); |
| 1540 | 1541 | |
| 1541 | 1542 | // Reinterpret to generic type |
| ... | ... | @@ -1543,7 +1544,7 @@ test "DWARF expressions" { |
| 1543 | 1544 | program.clearRetainingCapacity(); |
| 1544 | 1545 | try b.writeConstType(writer, @as(usize, 0), &value_bytes); |
| 1545 | 1546 | try b.writeReinterpret(writer, @as(usize, 0)); |
| 1546 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1547 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1547 | 1548 | try testing.expectEqual(value, stack_machine.stack.pop().?.generic); |
| 1548 | 1549 | |
| 1549 | 1550 | // Reinterpret to new type |
| ... | ... | @@ -1553,7 +1554,7 @@ test "DWARF expressions" { |
| 1553 | 1554 | program.clearRetainingCapacity(); |
| 1554 | 1555 | try b.writeConstType(writer, @as(usize, 0), &value_bytes); |
| 1555 | 1556 | try b.writeReinterpret(writer, die_offset); |
| 1556 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1557 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1557 | 1558 | const const_type = stack_machine.stack.pop().?.const_type; |
| 1558 | 1559 | try testing.expectEqual(die_offset, const_type.type_offset); |
| 1559 | 1560 | |
| ... | ... | @@ -1561,7 +1562,7 @@ test "DWARF expressions" { |
| 1561 | 1562 | program.clearRetainingCapacity(); |
| 1562 | 1563 | try b.writeLiteral(writer, 0); |
| 1563 | 1564 | try b.writeReinterpret(writer, die_offset); |
| 1564 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1565 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1565 | 1566 | const regval_type = stack_machine.stack.pop().?.regval_type; |
| 1566 | 1567 | try testing.expectEqual(die_offset, regval_type.type_offset); |
| 1567 | 1568 | } |
| ... | ... | @@ -1573,20 +1574,20 @@ test "DWARF expressions" { |
| 1573 | 1574 | stack_machine.reset(); |
| 1574 | 1575 | program.clearRetainingCapacity(); |
| 1575 | 1576 | try b.writeOpcode(writer, OP.nop); |
| 1576 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1577 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1577 | 1578 | try testing.expect(stack_machine.stack.pop() == null); |
| 1578 | 1579 | |
| 1579 | 1580 | // Sub-expression |
| 1580 | 1581 | { |
| 1581 | | var sub_program = std.array_list.Managed(u8).init(allocator); |
| 1582 | var sub_program: std.Io.Writer.Allocating = .init(allocator); |
| 1582 | 1583 | defer sub_program.deinit(); |
| 1583 | | const sub_writer = sub_program.writer(); |
| 1584 | const sub_writer = &sub_program.writer; |
| 1584 | 1585 | try b.writeLiteral(sub_writer, 3); |
| 1585 | 1586 | |
| 1586 | 1587 | stack_machine.reset(); |
| 1587 | 1588 | program.clearRetainingCapacity(); |
| 1588 | | try b.writeEntryValue(writer, sub_program.items); |
| 1589 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1589 | try b.writeEntryValue(writer, sub_program.written()); |
| 1590 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1590 | 1591 | try testing.expectEqual(@as(usize, 3), stack_machine.stack.pop().?.generic); |
| 1591 | 1592 | } |
| 1592 | 1593 | |
| ... | ... | @@ -1605,15 +1606,15 @@ test "DWARF expressions" { |
| 1605 | 1606 | if (abi.regBytes(&thread_context, 0, reg_context)) |reg_bytes| { |
| 1606 | 1607 | mem.writeInt(usize, reg_bytes[0..@sizeOf(usize)], 0xee, native_endian); |
| 1607 | 1608 | |
| 1608 | | var sub_program = std.array_list.Managed(u8).init(allocator); |
| 1609 | var sub_program: std.Io.Writer.Allocating = .init(allocator); |
| 1609 | 1610 | defer sub_program.deinit(); |
| 1610 | | const sub_writer = sub_program.writer(); |
| 1611 | const sub_writer = &sub_program.writer; |
| 1611 | 1612 | try b.writeReg(sub_writer, 0); |
| 1612 | 1613 | |
| 1613 | 1614 | stack_machine.reset(); |
| 1614 | 1615 | program.clearRetainingCapacity(); |
| 1615 | | try b.writeEntryValue(writer, sub_program.items); |
| 1616 | | _ = try stack_machine.run(program.items, allocator, context, null); |
| 1616 | try b.writeEntryValue(writer, sub_program.written()); |
| 1617 | _ = try stack_machine.run(program.written(), allocator, context, null); |
| 1617 | 1618 | try testing.expectEqual(@as(usize, 0xee), stack_machine.stack.pop().?.generic); |
| 1618 | 1619 | } else |err| { |
| 1619 | 1620 | switch (err) { |