| ... | ... | @@ -8,6 +8,7 @@ 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 Allocator = std.mem.Allocator; |
| 11 | 12 | |
| 12 | 13 | /// Expressions can be evaluated in different contexts, each requiring its own set of inputs. |
| 13 | 14 | /// Callers should specify all the fields relevant to their context. If a field is required |
| ... | ... | @@ -41,8 +42,7 @@ pub const Options = struct { |
| 41 | 42 | call_frame_context: bool = false, |
| 42 | 43 | }; |
| 43 | 44 | |
| 44 | | // Explicitly defined to support executing sub-expressions |
| 45 | | pub const Error = error{ |
| 45 | pub const RunError = error{ |
| 46 | 46 | UnimplementedExpressionCall, |
| 47 | 47 | UnimplementedOpcode, |
| 48 | 48 | UnimplementedUserOpcode, |
| ... | ... | @@ -62,7 +62,12 @@ pub const Error = error{ |
| 62 | 62 | InvalidTypeLength, |
| 63 | 63 | |
| 64 | 64 | TruncatedIntegralType, |
| 65 | | } || abi.RegBytesError || error{ EndOfStream, Overflow, OutOfMemory, DivisionByZero, ReadFailed }; |
| 65 | |
| 66 | OutOfMemory, |
| 67 | EndOfStream, |
| 68 | Overflow, |
| 69 | DivisionByZero, |
| 70 | } || abi.RegBytesError; |
| 66 | 71 | |
| 67 | 72 | /// A stack machine that can decode and run DWARF expressions. |
| 68 | 73 | /// Expressions can be decoded for non-native address size and endianness, |
| ... | ... | @@ -74,44 +79,16 @@ pub fn StackMachine(comptime options: Options) type { |
| 74 | 79 | 8 => u64, |
| 75 | 80 | else => @compileError("Unsupported address size of " ++ options.addr_size), |
| 76 | 81 | }; |
| 77 | | |
| 78 | 82 | const SignedAddress = switch (options.addr_size) { |
| 79 | 83 | 2 => i16, |
| 80 | 84 | 4 => i32, |
| 81 | 85 | 8 => i64, |
| 82 | 86 | else => @compileError("Unsupported address size of " ++ options.addr_size), |
| 83 | 87 | }; |
| 84 | | |
| 85 | 88 | return struct { |
| 86 | | const Self = @This(); |
| 89 | stack: std.ArrayListUnmanaged(Value) = .empty, |
| 87 | 90 | |
| 88 | | const Operand = union(enum) { |
| 89 | | generic: Address, |
| 90 | | register: u8, |
| 91 | | type_size: u8, |
| 92 | | branch_offset: i16, |
| 93 | | base_register: struct { |
| 94 | | base_register: u8, |
| 95 | | offset: i64, |
| 96 | | }, |
| 97 | | composite_location: struct { |
| 98 | | size: u64, |
| 99 | | offset: i64, |
| 100 | | }, |
| 101 | | block: []const u8, |
| 102 | | register_type: struct { |
| 103 | | register: u8, |
| 104 | | type_offset: Address, |
| 105 | | }, |
| 106 | | const_type: struct { |
| 107 | | type_offset: Address, |
| 108 | | value_bytes: []const u8, |
| 109 | | }, |
| 110 | | deref_type: struct { |
| 111 | | size: u8, |
| 112 | | type_offset: Address, |
| 113 | | }, |
| 114 | | }; |
| 91 | const Self = @This(); |
| 115 | 92 | |
| 116 | 93 | const Value = union(enum) { |
| 117 | 94 | generic: Address, |
| ... | ... | @@ -132,7 +109,7 @@ pub fn StackMachine(comptime options: Options) type { |
| 132 | 109 | value_bytes: []const u8, |
| 133 | 110 | }, |
| 134 | 111 | |
| 135 | | pub fn asIntegral(self: Value) !Address { |
| 112 | fn asIntegral(self: Value) !Address { |
| 136 | 113 | return switch (self) { |
| 137 | 114 | .generic => |v| v, |
| 138 | 115 | |
| ... | ... | @@ -151,185 +128,131 @@ pub fn StackMachine(comptime options: Options) type { |
| 151 | 128 | }, |
| 152 | 129 | }; |
| 153 | 130 | } |
| 154 | | }; |
| 155 | 131 | |
| 156 | | stack: std.ArrayListUnmanaged(Value) = .empty, |
| 132 | fn fromInt(int: anytype) Value { |
| 133 | const info = @typeInfo(@TypeOf(int)).int; |
| 134 | if (@sizeOf(@TypeOf(int)) > options.addr_size) { |
| 135 | return .{ .generic = switch (info.signedness) { |
| 136 | .signed => @bitCast(@as(SignedAddress, @truncate(int))), |
| 137 | .unsigned => @truncate(int), |
| 138 | } }; |
| 139 | } else { |
| 140 | return .{ .generic = switch (info.signedness) { |
| 141 | .signed => @bitCast(@as(SignedAddress, @intCast(int))), |
| 142 | .unsigned => @intCast(int), |
| 143 | } }; |
| 144 | } |
| 145 | } |
| 146 | }; |
| 157 | 147 | |
| 158 | 148 | pub fn reset(self: *Self) void { |
| 159 | 149 | self.stack.clearRetainingCapacity(); |
| 160 | 150 | } |
| 161 | 151 | |
| 162 | | pub fn deinit(self: *Self, allocator: std.mem.Allocator) void { |
| 152 | pub fn deinit(self: *Self, allocator: Allocator) void { |
| 163 | 153 | self.stack.deinit(allocator); |
| 164 | 154 | } |
| 165 | 155 | |
| 166 | | fn generic(value: anytype) Operand { |
| 167 | | const int_info = @typeInfo(@TypeOf(value)).int; |
| 168 | | if (@sizeOf(@TypeOf(value)) > options.addr_size) { |
| 169 | | return .{ .generic = switch (int_info.signedness) { |
| 170 | | .signed => @bitCast(@as(SignedAddress, @truncate(value))), |
| 171 | | .unsigned => @truncate(value), |
| 172 | | } }; |
| 173 | | } else { |
| 174 | | return .{ .generic = switch (int_info.signedness) { |
| 175 | | .signed => @bitCast(@as(SignedAddress, @intCast(value))), |
| 176 | | .unsigned => @intCast(value), |
| 177 | | } }; |
| 178 | | } |
| 179 | | } |
| 180 | | |
| 181 | | pub fn readOperand(reader: *std.io.BufferedReader, opcode: u8, context: Context) !?Operand { |
| 182 | | return switch (opcode) { |
| 183 | | OP.addr => generic(try reader.takeInt(Address, options.endian)), |
| 184 | | OP.call_ref => switch (context.format) { |
| 185 | | .@"32" => generic(try reader.takeInt(u32, options.endian)), |
| 186 | | .@"64" => generic(try reader.takeInt(u64, options.endian)), |
| 187 | | }, |
| 188 | | OP.const1u, |
| 189 | | OP.pick, |
| 190 | | => generic(try reader.takeByte()), |
| 191 | | OP.deref_size, |
| 192 | | OP.xderef_size, |
| 193 | | => .{ .type_size = try reader.takeByte() }, |
| 194 | | OP.const1s => generic(try reader.takeByteSigned()), |
| 195 | | OP.const2u, |
| 196 | | OP.call2, |
| 197 | | => generic(try reader.takeInt(u16, options.endian)), |
| 198 | | OP.call4 => generic(try reader.takeInt(u32, options.endian)), |
| 199 | | OP.const2s => generic(try reader.takeInt(i16, options.endian)), |
| 200 | | OP.bra, |
| 201 | | OP.skip, |
| 202 | | => .{ .branch_offset = try reader.takeInt(i16, options.endian) }, |
| 203 | | OP.const4u => generic(try reader.takeInt(u32, options.endian)), |
| 204 | | OP.const4s => generic(try reader.takeInt(i32, options.endian)), |
| 205 | | OP.const8u => generic(try reader.takeInt(u64, options.endian)), |
| 206 | | OP.const8s => generic(try reader.takeInt(i64, options.endian)), |
| 207 | | OP.constu, |
| 208 | | OP.plus_uconst, |
| 209 | | OP.addrx, |
| 210 | | OP.constx, |
| 211 | | OP.convert, |
| 212 | | OP.reinterpret, |
| 213 | | => generic(try reader.takeLeb128(u64)), |
| 214 | | OP.consts, |
| 215 | | OP.fbreg, |
| 216 | | => generic(try reader.takeLeb128(i64)), |
| 217 | | OP.lit0...OP.lit31 => |n| generic(n - OP.lit0), |
| 218 | | OP.reg0...OP.reg31 => |n| .{ .register = n - OP.reg0 }, |
| 219 | | OP.breg0...OP.breg31 => |n| .{ .base_register = .{ |
| 220 | | .base_register = n - OP.breg0, |
| 221 | | .offset = try reader.takeLeb128(i64), |
| 222 | | } }, |
| 223 | | OP.regx => .{ .register = try reader.takeLeb128(u8) }, |
| 224 | | OP.bregx => .{ .base_register = .{ |
| 225 | | .base_register = try reader.takeLeb128(u8), |
| 226 | | .offset = try reader.takeLeb128(i64), |
| 227 | | } }, |
| 228 | | OP.regval_type => .{ .register_type = .{ |
| 229 | | .register = try reader.takeLeb128(u8), |
| 230 | | .type_offset = try reader.takeLeb128(Address), |
| 231 | | } }, |
| 232 | | OP.piece => .{ .composite_location = .{ |
| 233 | | .size = try reader.takeLeb128(u64), |
| 234 | | .offset = 0, |
| 235 | | } }, |
| 236 | | OP.bit_piece => .{ .composite_location = .{ |
| 237 | | .size = try reader.takeLeb128(u64), |
| 238 | | .offset = try reader.takeLeb128(i64), |
| 239 | | } }, |
| 240 | | OP.implicit_value, OP.entry_value => .{ |
| 241 | | .block = try reader.take(try reader.takeLeb128(usize)), |
| 242 | | }, |
| 243 | | OP.const_type => .{ .const_type = .{ |
| 244 | | .type_offset = try reader.takeLeb128(Address), |
| 245 | | .value_bytes = try reader.take(try reader.takeByte()), |
| 246 | | } }, |
| 247 | | OP.deref_type, OP.xderef_type => .{ .deref_type = .{ |
| 248 | | .size = try reader.takeByte(), |
| 249 | | .type_offset = try reader.takeLeb128(Address), |
| 250 | | } }, |
| 251 | | OP.lo_user...OP.hi_user => return error.UnimplementedUserOpcode, |
| 252 | | else => null, |
| 253 | | }; |
| 254 | | } |
| 255 | | |
| 256 | 156 | pub fn run( |
| 257 | 157 | self: *Self, |
| 258 | 158 | expression: []const u8, |
| 259 | | allocator: std.mem.Allocator, |
| 159 | gpa: Allocator, |
| 260 | 160 | context: Context, |
| 261 | 161 | initial_value: ?usize, |
| 262 | | ) Error!?Value { |
| 263 | | if (initial_value) |i| try self.stack.append(allocator, .{ .generic = i }); |
| 264 | | var reader: std.io.BufferedReader = undefined; |
| 265 | | reader.initFixed(@constCast(expression)); |
| 266 | | while (try self.step(&reader, allocator, context)) {} |
| 267 | | if (self.stack.items.len == 0) return null; |
| 268 | | return self.stack.items[self.stack.items.len - 1]; |
| 269 | | } |
| 270 | | |
| 271 | | /// Reads an opcode and its operands from `stream`, then executes it |
| 272 | | pub fn step( |
| 273 | | self: *Self, |
| 274 | | reader: *std.io.BufferedReader, |
| 275 | | allocator: std.mem.Allocator, |
| 276 | | context: Context, |
| 277 | | ) Error!bool { |
| 278 | | if (@sizeOf(usize) != @sizeOf(Address) or options.endian != native_endian) |
| 162 | ) RunError!?Value { |
| 163 | if (@sizeOf(usize) != @sizeOf(Address) or options.endian != native_endian) { |
| 164 | // This restriction can be removed when the `@ptrFromInt` calls are removed. |
| 279 | 165 | @compileError("Execution of non-native address sizes / endianness is not supported"); |
| 166 | } |
| 280 | 167 | |
| 281 | | const opcode = reader.takeByte() catch |err| switch (err) { |
| 282 | | error.EndOfStream => return false, |
| 283 | | error.ReadFailed => return error.ReadFailed, |
| 284 | | }; |
| 285 | | if (options.call_frame_context and !isOpcodeValidInCFA(opcode)) return error.InvalidCFAOpcode; |
| 286 | | const operand = try readOperand(reader, opcode, context); |
| 287 | | switch (opcode) { |
| 168 | const stack = &self.stack; |
| 169 | if (initial_value) |i| try stack.append(gpa, .{ .generic = i }); |
| 288 | 170 | |
| 171 | var i: usize = 0; |
| 172 | // TODO: https://github.com/ziglang/zig/issues/15556 |
| 173 | op: switch (nextOpcode(expression, &i)) { |
| 289 | 174 | // 2.5.1.1: Literal Encodings |
| 290 | | OP.lit0...OP.lit31, |
| 291 | | OP.addr, |
| 292 | | OP.const1u, |
| 293 | | OP.const2u, |
| 294 | | OP.const4u, |
| 295 | | OP.const8u, |
| 296 | | OP.const1s, |
| 297 | | OP.const2s, |
| 298 | | OP.const4s, |
| 299 | | OP.const8s, |
| 300 | | OP.constu, |
| 301 | | OP.consts, |
| 302 | | => try self.stack.append(allocator, .{ .generic = operand.?.generic }), |
| 303 | | |
| 304 | | OP.const_type => { |
| 305 | | const const_type = operand.?.const_type; |
| 306 | | try self.stack.append(allocator, .{ .const_type = .{ |
| 307 | | .type_offset = const_type.type_offset, |
| 308 | | .value_bytes = const_type.value_bytes, |
| 175 | @intFromEnum(OP.lit0)...@intFromEnum(OP.lit31) => |n| { |
| 176 | try stack.append(gpa, .{ .generic = n - @intFromEnum(OP.lit0) }); |
| 177 | continue :op nextOpcode(expression, &i); |
| 178 | }, |
| 179 | @intFromEnum(OP.addr) => { |
| 180 | try stack.append(gpa, .fromInt(try nextInt(expression, &i, Address))); |
| 181 | continue :op nextOpcode(expression, &i); |
| 182 | }, |
| 183 | @intFromEnum(OP.const1u) => { |
| 184 | try stack.append(gpa, .fromInt(try nextInt(expression, &i, u8))); |
| 185 | continue :op nextOpcode(expression, &i); |
| 186 | }, |
| 187 | @intFromEnum(OP.const2u) => { |
| 188 | try stack.append(gpa, .fromInt(try nextInt(expression, &i, u16))); |
| 189 | continue :op nextOpcode(expression, &i); |
| 190 | }, |
| 191 | @intFromEnum(OP.const4u) => { |
| 192 | try stack.append(gpa, .fromInt(try nextInt(expression, &i, u32))); |
| 193 | continue :op nextOpcode(expression, &i); |
| 194 | }, |
| 195 | @intFromEnum(OP.const8u) => { |
| 196 | try stack.append(gpa, .fromInt(try nextInt(expression, &i, u64))); |
| 197 | continue :op nextOpcode(expression, &i); |
| 198 | }, |
| 199 | @intFromEnum(OP.const1s) => { |
| 200 | try stack.append(gpa, .fromInt(try nextInt(expression, &i, i8))); |
| 201 | continue :op nextOpcode(expression, &i); |
| 202 | }, |
| 203 | @intFromEnum(OP.const2s) => { |
| 204 | try stack.append(gpa, .fromInt(try nextInt(expression, &i, i16))); |
| 205 | continue :op nextOpcode(expression, &i); |
| 206 | }, |
| 207 | @intFromEnum(OP.const4s) => { |
| 208 | try stack.append(gpa, .fromInt(try nextInt(expression, &i, i32))); |
| 209 | continue :op nextOpcode(expression, &i); |
| 210 | }, |
| 211 | @intFromEnum(OP.const8s) => { |
| 212 | try stack.append(gpa, .fromInt(try nextInt(expression, &i, i64))); |
| 213 | continue :op nextOpcode(expression, &i); |
| 214 | }, |
| 215 | @intFromEnum(OP.constu) => { |
| 216 | try stack.append(gpa, .fromInt(try nextLeb128(expression, &i, u64))); |
| 217 | continue :op nextOpcode(expression, &i); |
| 218 | }, |
| 219 | @intFromEnum(OP.consts) => { |
| 220 | try stack.append(gpa, .fromInt(try nextLeb128(expression, &i, i64))); |
| 221 | continue :op nextOpcode(expression, &i); |
| 222 | }, |
| 223 | |
| 224 | @intFromEnum(OP.const_type) => { |
| 225 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 226 | try stack.append(gpa, .{ .const_type = .{ |
| 227 | .type_offset = try nextLeb128(expression, &i, Address), |
| 228 | .value_bytes = try nextSlice(expression, &i, try nextInt(expression, &i, u8)), |
| 309 | 229 | } }); |
| 230 | continue :op nextOpcode(expression, &i); |
| 310 | 231 | }, |
| 311 | 232 | |
| 312 | | OP.addrx, |
| 313 | | OP.constx, |
| 233 | @intFromEnum(OP.addrx), |
| 234 | @intFromEnum(OP.constx), |
| 314 | 235 | => { |
| 315 | | if (context.compile_unit == null) return error.IncompleteExpressionContext; |
| 316 | | if (context.debug_addr == null) return error.IncompleteExpressionContext; |
| 317 | | const debug_addr_index = operand.?.generic; |
| 318 | | const offset = context.compile_unit.?.addr_base + debug_addr_index; |
| 319 | | if (offset >= context.debug_addr.?.len) return error.InvalidExpression; |
| 320 | | const value = mem.readInt(usize, context.debug_addr.?[offset..][0..@sizeOf(usize)], native_endian); |
| 321 | | try self.stack.append(allocator, .{ .generic = value }); |
| 236 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 237 | const compile_unit = context.compile_unit orelse return error.IncompleteExpressionContext; |
| 238 | const debug_addr = context.debug_addr orelse return error.IncompleteExpressionContext; |
| 239 | const debug_addr_index = try nextLeb128(expression, &i, u64); |
| 240 | const offset = compile_unit.addr_base + debug_addr_index; |
| 241 | if (offset >= debug_addr.len) return error.InvalidExpression; |
| 242 | const value = mem.readInt(Address, debug_addr[offset..][0..@sizeOf(Address)], options.endian); |
| 243 | try stack.append(gpa, .fromInt(value)); |
| 244 | continue :op nextOpcode(expression, &i); |
| 322 | 245 | }, |
| 323 | 246 | |
| 324 | 247 | // 2.5.1.2: Register Values |
| 325 | | OP.fbreg => { |
| 326 | | if (context.compile_unit == null) return error.IncompleteExpressionContext; |
| 327 | | if (context.compile_unit.?.frame_base == null) return error.IncompleteExpressionContext; |
| 248 | @intFromEnum(OP.fbreg) => { |
| 249 | const compile_unit = context.compile_unit orelse return error.IncompleteExpressionContext; |
| 250 | const frame_base = compile_unit.frame_base orelse return error.IncompleteExpressionContext; |
| 328 | 251 | |
| 329 | | const offset: i64 = @intCast(operand.?.generic); |
| 252 | const offset = try nextLeb128(expression, &i, i64); |
| 330 | 253 | _ = offset; |
| 331 | 254 | |
| 332 | | switch (context.compile_unit.?.frame_base.?.*) { |
| 255 | switch (frame_base.*) { |
| 333 | 256 | .exprloc => { |
| 334 | 257 | // TODO: Run this expression in a nested stack machine |
| 335 | 258 | return error.UnimplementedOpcode; |
| ... | ... | @@ -345,344 +268,407 @@ pub fn StackMachine(comptime options: Options) type { |
| 345 | 268 | else => return error.InvalidFrameBase, |
| 346 | 269 | } |
| 347 | 270 | }, |
| 348 | | OP.breg0...OP.breg31, |
| 349 | | OP.bregx, |
| 350 | | => { |
| 351 | | if (context.thread_context == null) return error.IncompleteExpressionContext; |
| 352 | | |
| 353 | | const base_register = operand.?.base_register; |
| 354 | | var value: i64 = @intCast(mem.readInt(usize, (try abi.regBytes( |
| 355 | | context.thread_context.?, |
| 356 | | base_register.base_register, |
| 357 | | context.reg_context, |
| 358 | | ))[0..@sizeOf(usize)], native_endian)); |
| 359 | | value += base_register.offset; |
| 360 | | try self.stack.append(allocator, .{ .generic = @intCast(value) }); |
| 361 | | }, |
| 362 | | OP.regval_type => { |
| 363 | | const register_type = operand.?.register_type; |
| 364 | | const value = mem.readInt(usize, (try abi.regBytes( |
| 365 | | context.thread_context.?, |
| 366 | | register_type.register, |
| 367 | | context.reg_context, |
| 368 | | ))[0..@sizeOf(usize)], native_endian); |
| 369 | | try self.stack.append(allocator, .{ |
| 271 | @intFromEnum(OP.breg0)...@intFromEnum(OP.breg31) => |n| { |
| 272 | const thread_context = context.thread_context orelse return error.IncompleteExpressionContext; |
| 273 | const base_register = n - @intFromEnum(OP.breg0); |
| 274 | const offset = try nextLeb128(expression, &i, i64); |
| 275 | const reg_bytes = try abi.regBytes(thread_context, base_register, context.reg_context); |
| 276 | const start_addr = mem.readInt(Address, reg_bytes[0..@sizeOf(Address)], options.endian); |
| 277 | try stack.append(gpa, .{ |
| 278 | .generic = std.math.addAny(Address, start_addr, offset) orelse |
| 279 | return error.InvalidExpression, |
| 280 | }); |
| 281 | continue :op nextOpcode(expression, &i); |
| 282 | }, |
| 283 | @intFromEnum(OP.bregx) => { |
| 284 | const thread_context = context.thread_context orelse return error.IncompleteExpressionContext; |
| 285 | const base_register = try nextLeb128(expression, &i, u8); |
| 286 | const offset = try nextLeb128(expression, &i, i64); |
| 287 | const reg_bytes = try abi.regBytes(thread_context, base_register, context.reg_context); |
| 288 | const start_addr = mem.readInt(Address, reg_bytes[0..@sizeOf(Address)], options.endian); |
| 289 | try stack.append(gpa, .{ |
| 290 | .generic = std.math.addAny(Address, start_addr, offset) orelse |
| 291 | return error.InvalidExpression, |
| 292 | }); |
| 293 | continue :op nextOpcode(expression, &i); |
| 294 | }, |
| 295 | @intFromEnum(OP.regval_type) => { |
| 296 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 297 | const thread_context = context.thread_context orelse return error.IncompleteExpressionContext; |
| 298 | const register = try nextLeb128(expression, &i, u8); |
| 299 | const type_offset = try nextLeb128(expression, &i, Address); |
| 300 | const reg_bytes = try abi.regBytes(thread_context, register, context.reg_context); |
| 301 | const value = mem.readInt(Address, reg_bytes[0..@sizeOf(Address)], options.endian); |
| 302 | try stack.append(gpa, .{ |
| 370 | 303 | .regval_type = .{ |
| 371 | | .type_offset = register_type.type_offset, |
| 304 | .type_offset = type_offset, |
| 372 | 305 | .type_size = @sizeOf(Address), |
| 373 | 306 | .value = value, |
| 374 | 307 | }, |
| 375 | 308 | }); |
| 309 | continue :op nextOpcode(expression, &i); |
| 376 | 310 | }, |
| 377 | 311 | |
| 378 | 312 | // 2.5.1.3: Stack Operations |
| 379 | | OP.dup => { |
| 380 | | if (self.stack.items.len == 0) return error.InvalidExpression; |
| 381 | | try self.stack.append(allocator, self.stack.items[self.stack.items.len - 1]); |
| 313 | @intFromEnum(OP.dup) => { |
| 314 | if (stack.items.len == 0) return error.InvalidExpression; |
| 315 | try stack.append(gpa, stack.items[stack.items.len - 1]); |
| 316 | continue :op nextOpcode(expression, &i); |
| 382 | 317 | }, |
| 383 | | OP.drop => { |
| 384 | | _ = self.stack.pop(); |
| 318 | @intFromEnum(OP.drop) => { |
| 319 | _ = stack.pop(); |
| 320 | continue :op nextOpcode(expression, &i); |
| 385 | 321 | }, |
| 386 | | OP.pick, OP.over => { |
| 387 | | const stack_index = if (opcode == OP.over) 1 else operand.?.generic; |
| 388 | | if (stack_index >= self.stack.items.len) return error.InvalidExpression; |
| 389 | | try self.stack.append(allocator, self.stack.items[self.stack.items.len - 1 - stack_index]); |
| 322 | @intFromEnum(OP.pick) => { |
| 323 | const stack_index = try nextInt(expression, &i, u8); |
| 324 | if (stack_index >= stack.items.len) return error.InvalidExpression; |
| 325 | try stack.append(gpa, stack.items[stack.items.len - 1 - stack_index]); |
| 326 | continue :op nextOpcode(expression, &i); |
| 390 | 327 | }, |
| 391 | | OP.swap => { |
| 392 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 393 | | mem.swap(Value, &self.stack.items[self.stack.items.len - 1], &self.stack.items[self.stack.items.len - 2]); |
| 328 | @intFromEnum(OP.over) => { |
| 329 | if (stack.items.len < 2) return error.InvalidExpression; |
| 330 | try stack.append(gpa, stack.items[stack.items.len - 2]); |
| 331 | continue :op nextOpcode(expression, &i); |
| 394 | 332 | }, |
| 395 | | OP.rot => { |
| 396 | | if (self.stack.items.len < 3) return error.InvalidExpression; |
| 397 | | const first = self.stack.items[self.stack.items.len - 1]; |
| 398 | | self.stack.items[self.stack.items.len - 1] = self.stack.items[self.stack.items.len - 2]; |
| 399 | | self.stack.items[self.stack.items.len - 2] = self.stack.items[self.stack.items.len - 3]; |
| 400 | | self.stack.items[self.stack.items.len - 3] = first; |
| 333 | @intFromEnum(OP.swap) => { |
| 334 | if (stack.items.len < 2) return error.InvalidExpression; |
| 335 | mem.swap(Value, &stack.items[stack.items.len - 1], &stack.items[stack.items.len - 2]); |
| 336 | continue :op nextOpcode(expression, &i); |
| 401 | 337 | }, |
| 402 | | OP.deref, |
| 403 | | OP.xderef, |
| 404 | | OP.deref_size, |
| 405 | | OP.xderef_size, |
| 406 | | OP.deref_type, |
| 407 | | OP.xderef_type, |
| 408 | | => { |
| 409 | | if (self.stack.items.len == 0) return error.InvalidExpression; |
| 410 | | const addr = try self.stack.items[self.stack.items.len - 1].asIntegral(); |
| 411 | | const addr_space_identifier: ?usize = switch (opcode) { |
| 412 | | OP.xderef, |
| 413 | | OP.xderef_size, |
| 414 | | OP.xderef_type, |
| 415 | | => blk: { |
| 416 | | _ = self.stack.pop(); |
| 417 | | if (self.stack.items.len == 0) return error.InvalidExpression; |
| 418 | | break :blk try self.stack.items[self.stack.items.len - 1].asIntegral(); |
| 338 | @intFromEnum(OP.rot) => { |
| 339 | if (stack.items.len < 3) return error.InvalidExpression; |
| 340 | const first = stack.items[stack.items.len - 1]; |
| 341 | stack.items[stack.items.len - 1] = stack.items[stack.items.len - 2]; |
| 342 | stack.items[stack.items.len - 2] = stack.items[stack.items.len - 3]; |
| 343 | stack.items[stack.items.len - 3] = first; |
| 344 | continue :op nextOpcode(expression, &i); |
| 345 | }, |
| 346 | |
| 347 | @intFromEnum(OP.deref_type) => { |
| 348 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 349 | if (stack.items.len == 0) return error.InvalidExpression; |
| 350 | const size = try nextInt(expression, &i, u8); |
| 351 | const type_offset = try nextLeb128(expression, &i, Address); |
| 352 | const addr = try stack.items[stack.items.len - 1].asIntegral(); |
| 353 | const loaded = try accessAddress(size, addr, context.memory_accessor); |
| 354 | stack.items[stack.items.len - 1] = .{ |
| 355 | .regval_type = .{ |
| 356 | .type_offset = type_offset, |
| 357 | .type_size = size, |
| 358 | .value = loaded, |
| 419 | 359 | }, |
| 420 | | else => null, |
| 421 | 360 | }; |
| 422 | | |
| 361 | continue :op nextOpcode(expression, &i); |
| 362 | }, |
| 363 | @intFromEnum(OP.deref_size) => { |
| 364 | if (stack.items.len == 0) return error.InvalidExpression; |
| 365 | const addr = try stack.items[stack.items.len - 1].asIntegral(); |
| 366 | const type_size = try nextInt(expression, &i, u8); |
| 367 | const loaded = try accessAddress(type_size, addr, context.memory_accessor); |
| 368 | stack.items[stack.items.len - 1] = .{ .generic = loaded }; |
| 369 | continue :op nextOpcode(expression, &i); |
| 370 | }, |
| 371 | @intFromEnum(OP.xderef_size) => { |
| 372 | if (stack.items.len < 2) return error.InvalidExpression; |
| 373 | const type_size = try nextInt(expression, &i, u8); |
| 374 | const addr = try stack.pop().?.asIntegral(); |
| 375 | const addr_space_identifier = try stack.items[stack.items.len - 1].asIntegral(); |
| 423 | 376 | // Usage of addr_space_identifier in the address calculation is implementation defined. |
| 424 | 377 | // This code will need to be updated to handle any architectures that utilize this. |
| 425 | 378 | _ = addr_space_identifier; |
| 379 | const loaded = try accessAddress(type_size, addr, context.memory_accessor); |
| 380 | stack.items[stack.items.len - 1] = .{ .generic = loaded }; |
| 381 | continue :op nextOpcode(expression, &i); |
| 382 | }, |
| 383 | @intFromEnum(OP.deref) => { |
| 384 | if (stack.items.len == 0) return error.InvalidExpression; |
| 385 | const addr = try stack.items[stack.items.len - 1].asIntegral(); |
| 386 | const loaded = try accessAddress(@sizeOf(Address), addr, context.memory_accessor); |
| 387 | stack.items[stack.items.len - 1] = .{ .generic = loaded }; |
| 388 | continue :op nextOpcode(expression, &i); |
| 389 | }, |
| 390 | @intFromEnum(OP.xderef) => { |
| 391 | if (stack.items.len < 2) return error.InvalidExpression; |
| 392 | const addr = try stack.pop().?.asIntegral(); |
| 393 | const addr_space_identifier = try stack.items[stack.items.len - 1].asIntegral(); |
| 394 | // Usage of addr_space_identifier in the address calculation is implementation defined. |
| 395 | // This code will need to be updated to handle any architectures that utilize this. |
| 396 | _ = addr_space_identifier; |
| 397 | const loaded = try accessAddress(@sizeOf(Address), addr, context.memory_accessor); |
| 398 | stack.items[stack.items.len - 1] = .{ .generic = loaded }; |
| 399 | continue :op nextOpcode(expression, &i); |
| 400 | }, |
| 426 | 401 | |
| 427 | | const size = switch (opcode) { |
| 428 | | OP.deref, |
| 429 | | OP.xderef, |
| 430 | | => @sizeOf(Address), |
| 431 | | OP.deref_size, |
| 432 | | OP.xderef_size, |
| 433 | | => operand.?.type_size, |
| 434 | | OP.deref_type, |
| 435 | | OP.xderef_type, |
| 436 | | => operand.?.deref_type.size, |
| 437 | | else => unreachable, |
| 438 | | }; |
| 439 | | |
| 440 | | if (context.memory_accessor) |memory_accessor| { |
| 441 | | if (!switch (size) { |
| 442 | | 1 => memory_accessor.load(u8, addr) != null, |
| 443 | | 2 => memory_accessor.load(u16, addr) != null, |
| 444 | | 4 => memory_accessor.load(u32, addr) != null, |
| 445 | | 8 => memory_accessor.load(u64, addr) != null, |
| 446 | | else => return error.InvalidExpression, |
| 447 | | }) return error.InvalidExpression; |
| 448 | | } |
| 449 | | |
| 450 | | const value: Address = std.math.cast(Address, @as(u64, switch (size) { |
| 451 | | 1 => @as(*const u8, @ptrFromInt(addr)).*, |
| 452 | | 2 => @as(*const u16, @ptrFromInt(addr)).*, |
| 453 | | 4 => @as(*const u32, @ptrFromInt(addr)).*, |
| 454 | | 8 => @as(*const u64, @ptrFromInt(addr)).*, |
| 455 | | else => return error.InvalidExpression, |
| 456 | | })) orelse return error.InvalidExpression; |
| 457 | | |
| 458 | | switch (opcode) { |
| 459 | | OP.deref_type, |
| 460 | | OP.xderef_type, |
| 461 | | => { |
| 462 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 463 | | .regval_type = .{ |
| 464 | | .type_offset = operand.?.deref_type.type_offset, |
| 465 | | .type_size = operand.?.deref_type.size, |
| 466 | | .value = value, |
| 467 | | }, |
| 468 | | }; |
| 469 | | }, |
| 470 | | else => { |
| 471 | | self.stack.items[self.stack.items.len - 1] = .{ .generic = value }; |
| 402 | @intFromEnum(OP.xderef_type), |
| 403 | => { |
| 404 | if (stack.items.len < 2) return error.InvalidExpression; |
| 405 | const addr = try stack.pop().?.asIntegral(); |
| 406 | const addr_space_identifier = try stack.items[stack.items.len - 1].asIntegral(); |
| 407 | // Usage of addr_space_identifier in the address calculation is implementation defined. |
| 408 | // This code will need to be updated to handle any architectures that utilize this. |
| 409 | _ = addr_space_identifier; |
| 410 | const size = try nextInt(expression, &i, u8); |
| 411 | const type_offset = try nextLeb128(expression, &i, Address); |
| 412 | const loaded = try accessAddress(size, addr, context.memory_accessor); |
| 413 | stack.items[stack.items.len - 1] = .{ |
| 414 | .regval_type = .{ |
| 415 | .type_offset = type_offset, |
| 416 | .type_size = size, |
| 417 | .value = loaded, |
| 472 | 418 | }, |
| 473 | | } |
| 419 | }; |
| 420 | continue :op nextOpcode(expression, &i); |
| 474 | 421 | }, |
| 475 | | OP.push_object_address => { |
| 422 | @intFromEnum(OP.push_object_address) => { |
| 423 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 476 | 424 | // In sub-expressions, `push_object_address` is not meaningful (as per the |
| 477 | 425 | // spec), so treat it like a nop |
| 478 | 426 | if (!context.entry_value_context) { |
| 479 | 427 | if (context.object_address == null) return error.IncompleteExpressionContext; |
| 480 | | try self.stack.append(allocator, .{ .generic = @intFromPtr(context.object_address.?) }); |
| 428 | try stack.append(gpa, .{ .generic = @intFromPtr(context.object_address.?) }); |
| 481 | 429 | } |
| 482 | 430 | }, |
| 483 | | OP.form_tls_address => { |
| 431 | @intFromEnum(OP.form_tls_address) => { |
| 484 | 432 | return error.UnimplementedOpcode; |
| 485 | 433 | }, |
| 486 | | OP.call_frame_cfa => { |
| 434 | @intFromEnum(OP.call_frame_cfa) => { |
| 435 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 487 | 436 | if (context.cfa) |cfa| { |
| 488 | | try self.stack.append(allocator, .{ .generic = cfa }); |
| 437 | try stack.append(gpa, .{ .generic = cfa }); |
| 489 | 438 | } else return error.IncompleteExpressionContext; |
| 439 | continue :op nextOpcode(expression, &i); |
| 490 | 440 | }, |
| 491 | 441 | |
| 492 | 442 | // 2.5.1.4: Arithmetic and Logical Operations |
| 493 | | OP.abs => { |
| 494 | | if (self.stack.items.len == 0) return error.InvalidExpression; |
| 495 | | const value: isize = @bitCast(try self.stack.items[self.stack.items.len - 1].asIntegral()); |
| 496 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 443 | @intFromEnum(OP.abs) => { |
| 444 | if (stack.items.len == 0) return error.InvalidExpression; |
| 445 | const value: isize = @bitCast(try stack.items[stack.items.len - 1].asIntegral()); |
| 446 | stack.items[stack.items.len - 1] = .{ |
| 497 | 447 | .generic = @abs(value), |
| 498 | 448 | }; |
| 449 | continue :op nextOpcode(expression, &i); |
| 499 | 450 | }, |
| 500 | | OP.@"and" => { |
| 501 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 502 | | const a = try self.stack.pop().?.asIntegral(); |
| 503 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 504 | | .generic = a & try self.stack.items[self.stack.items.len - 1].asIntegral(), |
| 451 | @intFromEnum(OP.@"and") => { |
| 452 | if (stack.items.len < 2) return error.InvalidExpression; |
| 453 | const a = try stack.pop().?.asIntegral(); |
| 454 | stack.items[stack.items.len - 1] = .{ |
| 455 | .generic = a & try stack.items[stack.items.len - 1].asIntegral(), |
| 505 | 456 | }; |
| 457 | continue :op nextOpcode(expression, &i); |
| 506 | 458 | }, |
| 507 | | OP.div => { |
| 508 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 509 | | const a: isize = @bitCast(try self.stack.pop().?.asIntegral()); |
| 510 | | const b: isize = @bitCast(try self.stack.items[self.stack.items.len - 1].asIntegral()); |
| 511 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 459 | @intFromEnum(OP.div) => { |
| 460 | if (stack.items.len < 2) return error.InvalidExpression; |
| 461 | const a: isize = @bitCast(try stack.pop().?.asIntegral()); |
| 462 | const b: isize = @bitCast(try stack.items[stack.items.len - 1].asIntegral()); |
| 463 | stack.items[stack.items.len - 1] = .{ |
| 512 | 464 | .generic = @bitCast(try std.math.divTrunc(isize, b, a)), |
| 513 | 465 | }; |
| 466 | continue :op nextOpcode(expression, &i); |
| 514 | 467 | }, |
| 515 | | OP.minus => { |
| 516 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 517 | | const b = try self.stack.pop().?.asIntegral(); |
| 518 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 519 | | .generic = try std.math.sub(Address, try self.stack.items[self.stack.items.len - 1].asIntegral(), b), |
| 468 | @intFromEnum(OP.minus) => { |
| 469 | if (stack.items.len < 2) return error.InvalidExpression; |
| 470 | const b = try stack.pop().?.asIntegral(); |
| 471 | stack.items[stack.items.len - 1] = .{ |
| 472 | .generic = try std.math.sub(Address, try stack.items[stack.items.len - 1].asIntegral(), b), |
| 520 | 473 | }; |
| 474 | continue :op nextOpcode(expression, &i); |
| 521 | 475 | }, |
| 522 | | OP.mod => { |
| 523 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 524 | | const a: isize = @bitCast(try self.stack.pop().?.asIntegral()); |
| 525 | | const b: isize = @bitCast(try self.stack.items[self.stack.items.len - 1].asIntegral()); |
| 526 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 476 | @intFromEnum(OP.mod) => { |
| 477 | if (stack.items.len < 2) return error.InvalidExpression; |
| 478 | const a: isize = @bitCast(try stack.pop().?.asIntegral()); |
| 479 | const b: isize = @bitCast(try stack.items[stack.items.len - 1].asIntegral()); |
| 480 | stack.items[stack.items.len - 1] = .{ |
| 527 | 481 | .generic = @bitCast(@mod(b, a)), |
| 528 | 482 | }; |
| 483 | continue :op nextOpcode(expression, &i); |
| 529 | 484 | }, |
| 530 | | OP.mul => { |
| 531 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 532 | | const a: isize = @bitCast(try self.stack.pop().?.asIntegral()); |
| 533 | | const b: isize = @bitCast(try self.stack.items[self.stack.items.len - 1].asIntegral()); |
| 534 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 485 | @intFromEnum(OP.mul) => { |
| 486 | if (stack.items.len < 2) return error.InvalidExpression; |
| 487 | const a: isize = @bitCast(try stack.pop().?.asIntegral()); |
| 488 | const b: isize = @bitCast(try stack.items[stack.items.len - 1].asIntegral()); |
| 489 | stack.items[stack.items.len - 1] = .{ |
| 535 | 490 | .generic = @bitCast(@mulWithOverflow(a, b)[0]), |
| 536 | 491 | }; |
| 492 | continue :op nextOpcode(expression, &i); |
| 537 | 493 | }, |
| 538 | | OP.neg => { |
| 539 | | if (self.stack.items.len == 0) return error.InvalidExpression; |
| 540 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 494 | @intFromEnum(OP.neg) => { |
| 495 | if (stack.items.len == 0) return error.InvalidExpression; |
| 496 | stack.items[stack.items.len - 1] = .{ |
| 541 | 497 | .generic = @bitCast( |
| 542 | 498 | try std.math.negate( |
| 543 | | @as(isize, @bitCast(try self.stack.items[self.stack.items.len - 1].asIntegral())), |
| 499 | @as(isize, @bitCast(try stack.items[stack.items.len - 1].asIntegral())), |
| 544 | 500 | ), |
| 545 | 501 | ), |
| 546 | 502 | }; |
| 503 | continue :op nextOpcode(expression, &i); |
| 547 | 504 | }, |
| 548 | | OP.not => { |
| 549 | | if (self.stack.items.len == 0) return error.InvalidExpression; |
| 550 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 551 | | .generic = ~try self.stack.items[self.stack.items.len - 1].asIntegral(), |
| 505 | @intFromEnum(OP.not) => { |
| 506 | if (stack.items.len == 0) return error.InvalidExpression; |
| 507 | stack.items[stack.items.len - 1] = .{ |
| 508 | .generic = ~try stack.items[stack.items.len - 1].asIntegral(), |
| 552 | 509 | }; |
| 510 | continue :op nextOpcode(expression, &i); |
| 553 | 511 | }, |
| 554 | | OP.@"or" => { |
| 555 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 556 | | const a = try self.stack.pop().?.asIntegral(); |
| 557 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 558 | | .generic = a | try self.stack.items[self.stack.items.len - 1].asIntegral(), |
| 512 | @intFromEnum(OP.@"or") => { |
| 513 | if (stack.items.len < 2) return error.InvalidExpression; |
| 514 | const a = try stack.pop().?.asIntegral(); |
| 515 | stack.items[stack.items.len - 1] = .{ |
| 516 | .generic = a | try stack.items[stack.items.len - 1].asIntegral(), |
| 559 | 517 | }; |
| 518 | continue :op nextOpcode(expression, &i); |
| 560 | 519 | }, |
| 561 | | OP.plus => { |
| 562 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 563 | | const b = try self.stack.pop().?.asIntegral(); |
| 564 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 565 | | .generic = try std.math.add(Address, try self.stack.items[self.stack.items.len - 1].asIntegral(), b), |
| 520 | @intFromEnum(OP.plus) => { |
| 521 | if (stack.items.len < 2) return error.InvalidExpression; |
| 522 | const b = try stack.pop().?.asIntegral(); |
| 523 | stack.items[stack.items.len - 1] = .{ |
| 524 | .generic = try std.math.add(Address, try stack.items[stack.items.len - 1].asIntegral(), b), |
| 566 | 525 | }; |
| 526 | continue :op nextOpcode(expression, &i); |
| 567 | 527 | }, |
| 568 | | OP.plus_uconst => { |
| 569 | | if (self.stack.items.len == 0) return error.InvalidExpression; |
| 570 | | const constant = operand.?.generic; |
| 571 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 572 | | .generic = try std.math.add(Address, try self.stack.items[self.stack.items.len - 1].asIntegral(), constant), |
| 573 | | }; |
| 528 | @intFromEnum(OP.plus_uconst) => { |
| 529 | if (stack.items.len == 0) return error.InvalidExpression; |
| 530 | stack.items[stack.items.len - 1] = .{ .generic = std.math.addAny( |
| 531 | Address, |
| 532 | try nextLeb128(expression, &i, u64), |
| 533 | try stack.items[stack.items.len - 1].asIntegral(), |
| 534 | ) orelse return error.Overflow }; |
| 535 | continue :op nextOpcode(expression, &i); |
| 574 | 536 | }, |
| 575 | | OP.shl => { |
| 576 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 577 | | const a = try self.stack.pop().?.asIntegral(); |
| 578 | | const b = try self.stack.items[self.stack.items.len - 1].asIntegral(); |
| 579 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 537 | @intFromEnum(OP.shl) => { |
| 538 | if (stack.items.len < 2) return error.InvalidExpression; |
| 539 | const a = try stack.pop().?.asIntegral(); |
| 540 | const b = try stack.items[stack.items.len - 1].asIntegral(); |
| 541 | stack.items[stack.items.len - 1] = .{ |
| 580 | 542 | .generic = std.math.shl(usize, b, a), |
| 581 | 543 | }; |
| 544 | continue :op nextOpcode(expression, &i); |
| 582 | 545 | }, |
| 583 | | OP.shr => { |
| 584 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 585 | | const a = try self.stack.pop().?.asIntegral(); |
| 586 | | const b = try self.stack.items[self.stack.items.len - 1].asIntegral(); |
| 587 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 546 | @intFromEnum(OP.shr) => { |
| 547 | if (stack.items.len < 2) return error.InvalidExpression; |
| 548 | const a = try stack.pop().?.asIntegral(); |
| 549 | const b = try stack.items[stack.items.len - 1].asIntegral(); |
| 550 | stack.items[stack.items.len - 1] = .{ |
| 588 | 551 | .generic = std.math.shr(usize, b, a), |
| 589 | 552 | }; |
| 553 | continue :op nextOpcode(expression, &i); |
| 590 | 554 | }, |
| 591 | | OP.shra => { |
| 592 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 593 | | const a = try self.stack.pop().?.asIntegral(); |
| 594 | | const b: isize = @bitCast(try self.stack.items[self.stack.items.len - 1].asIntegral()); |
| 595 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 555 | @intFromEnum(OP.shra) => { |
| 556 | if (stack.items.len < 2) return error.InvalidExpression; |
| 557 | const a = try stack.pop().?.asIntegral(); |
| 558 | const b: isize = @bitCast(try stack.items[stack.items.len - 1].asIntegral()); |
| 559 | stack.items[stack.items.len - 1] = .{ |
| 596 | 560 | .generic = @bitCast(std.math.shr(isize, b, a)), |
| 597 | 561 | }; |
| 562 | continue :op nextOpcode(expression, &i); |
| 598 | 563 | }, |
| 599 | | OP.xor => { |
| 600 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 601 | | const a = try self.stack.pop().?.asIntegral(); |
| 602 | | self.stack.items[self.stack.items.len - 1] = .{ |
| 603 | | .generic = a ^ try self.stack.items[self.stack.items.len - 1].asIntegral(), |
| 564 | @intFromEnum(OP.xor) => { |
| 565 | if (stack.items.len < 2) return error.InvalidExpression; |
| 566 | const a = try stack.pop().?.asIntegral(); |
| 567 | stack.items[stack.items.len - 1] = .{ |
| 568 | .generic = a ^ try stack.items[stack.items.len - 1].asIntegral(), |
| 604 | 569 | }; |
| 570 | continue :op nextOpcode(expression, &i); |
| 605 | 571 | }, |
| 606 | 572 | |
| 607 | 573 | // 2.5.1.5: Control Flow Operations |
| 608 | | OP.le, |
| 609 | | OP.ge, |
| 610 | | OP.eq, |
| 611 | | OP.lt, |
| 612 | | OP.gt, |
| 613 | | OP.ne, |
| 614 | | => { |
| 615 | | if (self.stack.items.len < 2) return error.InvalidExpression; |
| 616 | | const a = self.stack.pop().?; |
| 617 | | const b = self.stack.items[self.stack.items.len - 1]; |
| 618 | | |
| 619 | | if (a == .generic and b == .generic) { |
| 620 | | const a_int: isize = @bitCast(a.asIntegral() catch unreachable); |
| 621 | | const b_int: isize = @bitCast(b.asIntegral() catch unreachable); |
| 622 | | const result = @intFromBool(switch (opcode) { |
| 623 | | OP.le => b_int <= a_int, |
| 624 | | OP.ge => b_int >= a_int, |
| 625 | | OP.eq => b_int == a_int, |
| 626 | | OP.lt => b_int < a_int, |
| 627 | | OP.gt => b_int > a_int, |
| 628 | | OP.ne => b_int != a_int, |
| 629 | | else => unreachable, |
| 630 | | }); |
| 631 | | |
| 632 | | self.stack.items[self.stack.items.len - 1] = .{ .generic = result }; |
| 633 | | } else { |
| 634 | | // TODO: Load the types referenced by these values, find their comparison operator, and run it |
| 635 | | return error.UnimplementedTypedComparison; |
| 636 | | } |
| 574 | @intFromEnum(OP.le) => { |
| 575 | try cmpOp(stack, .lte); |
| 576 | continue :op nextOpcode(expression, &i); |
| 577 | }, |
| 578 | @intFromEnum(OP.ge) => { |
| 579 | try cmpOp(stack, .gte); |
| 580 | continue :op nextOpcode(expression, &i); |
| 581 | }, |
| 582 | @intFromEnum(OP.eq) => { |
| 583 | try cmpOp(stack, .eq); |
| 584 | continue :op nextOpcode(expression, &i); |
| 585 | }, |
| 586 | @intFromEnum(OP.lt) => { |
| 587 | try cmpOp(stack, .lt); |
| 588 | continue :op nextOpcode(expression, &i); |
| 589 | }, |
| 590 | @intFromEnum(OP.gt) => { |
| 591 | try cmpOp(stack, .gt); |
| 592 | continue :op nextOpcode(expression, &i); |
| 593 | }, |
| 594 | @intFromEnum(OP.ne) => { |
| 595 | try cmpOp(stack, .neq); |
| 596 | continue :op nextOpcode(expression, &i); |
| 637 | 597 | }, |
| 638 | | OP.skip, OP.bra => { |
| 639 | | const branch_offset = operand.?.branch_offset; |
| 640 | | const condition = if (opcode == OP.bra) blk: { |
| 641 | | if (self.stack.items.len == 0) return error.InvalidExpression; |
| 642 | | break :blk try self.stack.pop().?.asIntegral() != 0; |
| 643 | | } else true; |
| 644 | 598 | |
| 645 | | if (condition) reader.seekBy(branch_offset) catch return error.InvalidExpression; |
| 599 | @intFromEnum(OP.skip) => { |
| 600 | const branch_offset = try nextInt(expression, &i, i16); |
| 601 | i = std.math.addAny(usize, i, branch_offset) orelse return error.InvalidExpression; |
| 602 | continue :op nextOpcode(expression, &i); |
| 646 | 603 | }, |
| 647 | | OP.call2, |
| 648 | | OP.call4, |
| 649 | | OP.call_ref, |
| 650 | | => { |
| 651 | | const debug_info_offset = operand.?.generic; |
| 604 | @intFromEnum(OP.bra) => { |
| 605 | const branch_offset = try nextInt(expression, &i, i16); |
| 606 | const condition = try (stack.pop() orelse return error.InvalidExpression).asIntegral(); |
| 607 | if (condition != 0) { |
| 608 | i = std.math.addAny(usize, i, branch_offset) orelse return error.InvalidExpression; |
| 609 | } |
| 610 | continue :op nextOpcode(expression, &i); |
| 611 | }, |
| 612 | @intFromEnum(OP.call2) => { |
| 613 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 614 | const debug_info_offset = nextInt(expression, &i, u16); |
| 615 | _ = debug_info_offset; |
| 616 | // TODO: Load a DIE entry at debug_info_offset in a .debug_info section (the spec says that it |
| 617 | // can be in a separate exe / shared object from the one containing this expression). |
| 618 | // Transfer control to the DW_AT_location attribute, with the current stack as input. |
| 619 | return error.UnimplementedExpressionCall; |
| 620 | }, |
| 621 | @intFromEnum(OP.call4) => { |
| 622 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 623 | const debug_info_offset = nextInt(expression, &i, u32); |
| 624 | _ = debug_info_offset; |
| 625 | // TODO: Load a DIE entry at debug_info_offset in a .debug_info section (the spec says that it |
| 626 | // can be in a separate exe / shared object from the one containing this expression). |
| 627 | // Transfer control to the DW_AT_location attribute, with the current stack as input. |
| 628 | return error.UnimplementedExpressionCall; |
| 629 | }, |
| 630 | @intFromEnum(OP.call_ref) => { |
| 631 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 632 | const debug_info_offset: u64 = switch (context.format) { |
| 633 | .@"32" => nextInt(expression, &i, u32), |
| 634 | .@"64" => nextInt(expression, &i, u64), |
| 635 | }; |
| 652 | 636 | _ = debug_info_offset; |
| 653 | | |
| 654 | 637 | // TODO: Load a DIE entry at debug_info_offset in a .debug_info section (the spec says that it |
| 655 | 638 | // can be in a separate exe / shared object from the one containing this expression). |
| 656 | 639 | // Transfer control to the DW_AT_location attribute, with the current stack as input. |
| 657 | | |
| 658 | 640 | return error.UnimplementedExpressionCall; |
| 659 | 641 | }, |
| 660 | 642 | |
| 661 | 643 | // 2.5.1.6: Type Conversions |
| 662 | | OP.convert => { |
| 663 | | if (self.stack.items.len == 0) return error.InvalidExpression; |
| 664 | | const type_offset = operand.?.generic; |
| 644 | @intFromEnum(OP.convert) => { |
| 645 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 646 | |
| 647 | if (stack.items.len == 0) return error.InvalidExpression; |
| 648 | const type_offset = try nextLeb128(expression, &i, u64); |
| 665 | 649 | |
| 666 | 650 | // TODO: Load the DW_TAG_base_type entries in context.compile_unit and verify both types are the same size |
| 667 | | const value = self.stack.items[self.stack.items.len - 1]; |
| 651 | const value = stack.items[stack.items.len - 1]; |
| 668 | 652 | if (type_offset == 0) { |
| 669 | | self.stack.items[self.stack.items.len - 1] = .{ .generic = try value.asIntegral() }; |
| 653 | stack.items[stack.items.len - 1] = .{ .generic = try value.asIntegral() }; |
| 670 | 654 | } else { |
| 671 | 655 | // TODO: Load the DW_TAG_base_type entry in context.compile_unit, find a conversion operator |
| 672 | 656 | // from the old type to the new type, run it. |
| 673 | 657 | return error.UnimplementedTypeConversion; |
| 674 | 658 | } |
| 659 | continue :op nextOpcode(expression, &i); |
| 675 | 660 | }, |
| 676 | | OP.reinterpret => { |
| 677 | | if (self.stack.items.len == 0) return error.InvalidExpression; |
| 678 | | const type_offset = operand.?.generic; |
| 661 | @intFromEnum(OP.reinterpret) => { |
| 662 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 663 | if (stack.items.len == 0) return error.InvalidExpression; |
| 664 | const type_offset = try nextLeb128(expression, &i, u64); |
| 679 | 665 | |
| 680 | 666 | // TODO: Load the DW_TAG_base_type entries in context.compile_unit and verify both types are the same size |
| 681 | | const value = self.stack.items[self.stack.items.len - 1]; |
| 667 | const value = stack.items[stack.items.len - 1]; |
| 682 | 668 | if (type_offset == 0) { |
| 683 | | self.stack.items[self.stack.items.len - 1] = .{ .generic = try value.asIntegral() }; |
| 669 | stack.items[stack.items.len - 1] = .{ .generic = try value.asIntegral() }; |
| 684 | 670 | } else { |
| 685 | | self.stack.items[self.stack.items.len - 1] = switch (value) { |
| 671 | stack.items[stack.items.len - 1] = switch (value) { |
| 686 | 672 | .generic => |v| .{ |
| 687 | 673 | .regval_type = .{ |
| 688 | 674 | .type_offset = type_offset, |
| ... | ... | @@ -705,45 +691,128 @@ pub fn StackMachine(comptime options: Options) type { |
| 705 | 691 | }, |
| 706 | 692 | }; |
| 707 | 693 | } |
| 694 | continue :op nextOpcode(expression, &i); |
| 708 | 695 | }, |
| 709 | 696 | |
| 710 | 697 | // 2.5.1.7: Special Operations |
| 711 | | OP.nop => {}, |
| 712 | | OP.entry_value => { |
| 713 | | const block = operand.?.block; |
| 714 | | if (block.len == 0) return error.InvalidSubExpression; |
| 698 | @intFromEnum(OP.nop) => continue :op nextOpcode(expression, &i), |
| 699 | @intFromEnum(OP.entry_value) => { |
| 700 | const block_len = try nextLeb128(expression, &i, usize); |
| 701 | if (block_len == 0) return error.InvalidSubExpression; |
| 702 | const block = try nextSlice(expression, &i, block_len); |
| 715 | 703 | |
| 716 | 704 | // TODO: The spec states that this sub-expression needs to observe the state (ie. registers) |
| 717 | 705 | // as it was upon entering the current subprogram. If this isn't being called at the |
| 718 | 706 | // end of a frame unwind operation, an additional ThreadContext with this state will be needed. |
| 719 | 707 | |
| 720 | | if (isOpcodeRegisterLocation(block[0])) { |
| 721 | | if (context.thread_context == null) return error.IncompleteExpressionContext; |
| 708 | switch (block[0]) { |
| 709 | @intFromEnum(OP.reg0)...@intFromEnum(OP.reg31) => |n| { |
| 710 | const thread_context = context.thread_context orelse |
| 711 | return error.IncompleteExpressionContext; |
| 712 | const register = n - @intFromEnum(OP.reg0); |
| 713 | const reg_bytes = try abi.regBytes(thread_context, register, context.reg_context); |
| 714 | const value = mem.readInt(usize, reg_bytes[0..@sizeOf(usize)], options.endian); |
| 715 | try stack.append(gpa, .{ .generic = value }); |
| 716 | continue :op nextOpcode(expression, &i); |
| 717 | }, |
| 718 | @intFromEnum(OP.regx) => { |
| 719 | const thread_context = context.thread_context orelse |
| 720 | return error.IncompleteExpressionContext; |
| 721 | const register = try nextLeb128(expression, &i, u8); |
| 722 | const reg_bytes = try abi.regBytes(thread_context, register, context.reg_context); |
| 723 | const value = mem.readInt(usize, reg_bytes[0..@sizeOf(usize)], options.endian); |
| 724 | try stack.append(gpa, .{ .generic = value }); |
| 725 | continue :op nextOpcode(expression, &i); |
| 726 | }, |
| 727 | else => { |
| 728 | var stack_machine: Self = .{}; |
| 729 | defer stack_machine.deinit(gpa); |
| 730 | |
| 731 | var sub_context = context; |
| 732 | sub_context.entry_value_context = true; |
| 733 | const result = try stack_machine.run(block, gpa, sub_context, null); |
| 734 | try stack.append(gpa, result orelse return error.InvalidSubExpression); |
| 735 | continue :op nextOpcode(expression, &i); |
| 736 | }, |
| 737 | } |
| 738 | }, |
| 722 | 739 | |
| 723 | | var block_reader: std.io.BufferedReader = undefined; |
| 724 | | block_reader.initFixed(@constCast(block)); |
| 725 | | const register = (try readOperand(&block_reader, block[0], context)).?.register; |
| 726 | | const value = mem.readInt(usize, (try abi.regBytes(context.thread_context.?, register, context.reg_context))[0..@sizeOf(usize)], native_endian); |
| 727 | | try self.stack.append(allocator, .{ .generic = value }); |
| 728 | | } else { |
| 729 | | var stack_machine: Self = .{}; |
| 730 | | defer stack_machine.deinit(allocator); |
| 740 | @intFromEnum(OP.lo_user)...@intFromEnum(OP.hi_user) - 1 => return error.UnimplementedUserOpcode, |
| 731 | 741 | |
| 732 | | var sub_context = context; |
| 733 | | sub_context.entry_value_context = true; |
| 734 | | const result = try stack_machine.run(block, allocator, sub_context, null); |
| 735 | | try self.stack.append(allocator, result orelse return error.InvalidSubExpression); |
| 736 | | } |
| 742 | // Repurposed for exiting the loop. |
| 743 | @intFromEnum(OP.hi_user) => { |
| 744 | if (stack.items.len == 0) return null; |
| 745 | return stack.items[stack.items.len - 1]; |
| 737 | 746 | }, |
| 738 | 747 | |
| 739 | | // These have already been handled by readOperand |
| 740 | | OP.lo_user...OP.hi_user => unreachable, |
| 741 | 748 | else => { |
| 742 | 749 | //std.debug.print("Unknown DWARF expression opcode: {x}\n", .{opcode}); |
| 743 | 750 | return error.UnknownExpressionOpcode; |
| 744 | 751 | }, |
| 745 | 752 | } |
| 746 | | return true; |
| 753 | comptime unreachable; |
| 754 | } |
| 755 | |
| 756 | fn nextOpcode(expression: []const u8, i: *usize) u8 { |
| 757 | const index = i.*; |
| 758 | if (expression.len - index == 0) return @intFromEnum(OP.hi_user); // repurposed to indicate end |
| 759 | i.* = index + 1; |
| 760 | return expression[index]; |
| 761 | } |
| 762 | |
| 763 | fn nextInt(expression: []const u8, i: *usize, comptime I: type) !I { |
| 764 | const n = @divExact(@bitSizeOf(I), 8); |
| 765 | const slice = try nextSlice(expression, i, n); |
| 766 | return mem.readInt(I, slice[0..n], options.endian); |
| 767 | } |
| 768 | |
| 769 | fn nextSlice(expression: []const u8, i: *usize, len: usize) ![]const u8 { |
| 770 | const index = i.*; |
| 771 | if (expression.len - index < len) return error.EndOfStream; |
| 772 | i.* = index + len; |
| 773 | return expression[index..][0..len]; |
| 774 | } |
| 775 | |
| 776 | fn nextLeb128(expression: []const u8, i: *usize, comptime I: type) !I { |
| 777 | var br: std.io.BufferedReader = undefined; |
| 778 | br.initFixed(@constCast(expression)); |
| 779 | br.seek = i.*; |
| 780 | assert(br.seek <= br.end); |
| 781 | const result = br.takeLeb128(I) catch |err| switch (err) { |
| 782 | error.ReadFailed => unreachable, |
| 783 | else => |e| return e, |
| 784 | }; |
| 785 | i.* = br.seek; |
| 786 | return result; |
| 787 | } |
| 788 | |
| 789 | fn accessAddress(size: u8, addr: Address, accessor: ?*std.debug.MemoryAccessor) !Address { |
| 790 | if (accessor) |memory_accessor| { |
| 791 | switch (size) { |
| 792 | 1 => if (memory_accessor.load(u8, addr) == null) return error.InvalidExpression, |
| 793 | 2 => if (memory_accessor.load(u16, addr) == null) return error.InvalidExpression, |
| 794 | 4 => if (memory_accessor.load(u32, addr) == null) return error.InvalidExpression, |
| 795 | 8 => if (memory_accessor.load(u64, addr) == null) return error.InvalidExpression, |
| 796 | else => return error.InvalidExpression, |
| 797 | } |
| 798 | } |
| 799 | return switch (size) { |
| 800 | 1 => std.math.cast(Address, @as(*const u8, @ptrFromInt(addr)).*), |
| 801 | 2 => std.math.cast(Address, @as(*const u16, @ptrFromInt(addr)).*), |
| 802 | 4 => std.math.cast(Address, @as(*const u32, @ptrFromInt(addr)).*), |
| 803 | 8 => std.math.cast(Address, @as(*const u64, @ptrFromInt(addr)).*), |
| 804 | else => return error.InvalidExpression, |
| 805 | } orelse return error.InvalidExpression; |
| 806 | } |
| 807 | |
| 808 | fn cmpOp(stack: *std.ArrayListUnmanaged(Value), op: std.math.CompareOperator) !void { |
| 809 | if (stack.items.len < 2) return error.InvalidExpression; |
| 810 | const a = stack.pop().?; |
| 811 | const b = stack.items[stack.items.len - 1]; |
| 812 | |
| 813 | const a_int = try a.asIntegral(); |
| 814 | const b_int = try b.asIntegral(); |
| 815 | stack.items[stack.items.len - 1] = .{ .generic = @intFromBool(std.math.compare(a_int, op, b_int)) }; |
| 747 | 816 | } |
| 748 | 817 | }; |
| 749 | 818 | } |
| ... | ... | @@ -758,7 +827,7 @@ pub fn Builder(comptime options: Options) type { |
| 758 | 827 | |
| 759 | 828 | return struct { |
| 760 | 829 | /// Zero-operand instructions |
| 761 | | pub fn writeOpcode(writer: anytype, comptime opcode: u8) !void { |
| 830 | pub fn writeOpcode(writer: *std.io.BufferedWriter, comptime opcode: u8) !void { |
| 762 | 831 | if (options.call_frame_context and !comptime isOpcodeValidInCFA(opcode)) return error.InvalidCFAOpcode; |
| 763 | 832 | switch (opcode) { |
| 764 | 833 | OP.dup, |
| ... | ... | @@ -799,14 +868,14 @@ pub fn Builder(comptime options: Options) type { |
| 799 | 868 | } |
| 800 | 869 | |
| 801 | 870 | // 2.5.1.1: Literal Encodings |
| 802 | | pub fn writeLiteral(writer: anytype, literal: u8) !void { |
| 871 | pub fn writeLiteral(writer: *std.io.BufferedWriter, literal: u8) !void { |
| 803 | 872 | switch (literal) { |
| 804 | 873 | 0...31 => |n| try writer.writeByte(n + OP.lit0), |
| 805 | 874 | else => return error.InvalidLiteral, |
| 806 | 875 | } |
| 807 | 876 | } |
| 808 | 877 | |
| 809 | | pub fn writeConst(writer: anytype, comptime T: type, value: T) !void { |
| 878 | pub fn writeConst(writer: *std.io.BufferedWriter, comptime T: type, value: T) !void { |
| 810 | 879 | if (@typeInfo(T) != .int) @compileError("Constants must be integers"); |
| 811 | 880 | |
| 812 | 881 | switch (T) { |
| ... | ... | @@ -838,12 +907,12 @@ pub fn Builder(comptime options: Options) type { |
| 838 | 907 | } |
| 839 | 908 | } |
| 840 | 909 | |
| 841 | | pub fn writeConstx(writer: anytype, debug_addr_offset: anytype) !void { |
| 910 | pub fn writeConstx(writer: *std.io.BufferedWriter, debug_addr_offset: anytype) !void { |
| 842 | 911 | try writer.writeByte(OP.constx); |
| 843 | 912 | try leb.writeUleb128(writer, debug_addr_offset); |
| 844 | 913 | } |
| 845 | 914 | |
| 846 | | pub fn writeConstType(writer: anytype, die_offset: anytype, value_bytes: []const u8) !void { |
| 915 | pub fn writeConstType(writer: *std.io.BufferedWriter, die_offset: anytype, value_bytes: []const u8) !void { |
| 847 | 916 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 848 | 917 | if (value_bytes.len > 0xff) return error.InvalidTypeLength; |
| 849 | 918 | try writer.writeByte(OP.const_type); |
| ... | ... | @@ -852,36 +921,36 @@ pub fn Builder(comptime options: Options) type { |
| 852 | 921 | try writer.writeAll(value_bytes); |
| 853 | 922 | } |
| 854 | 923 | |
| 855 | | pub fn writeAddr(writer: anytype, value: Address) !void { |
| 924 | pub fn writeAddr(writer: *std.io.BufferedWriter, value: Address) !void { |
| 856 | 925 | try writer.writeByte(OP.addr); |
| 857 | 926 | try writer.writeInt(Address, value, options.endian); |
| 858 | 927 | } |
| 859 | 928 | |
| 860 | | pub fn writeAddrx(writer: anytype, debug_addr_offset: anytype) !void { |
| 929 | pub fn writeAddrx(writer: *std.io.BufferedWriter, debug_addr_offset: anytype) !void { |
| 861 | 930 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 862 | 931 | try writer.writeByte(OP.addrx); |
| 863 | 932 | try leb.writeUleb128(writer, debug_addr_offset); |
| 864 | 933 | } |
| 865 | 934 | |
| 866 | 935 | // 2.5.1.2: Register Values |
| 867 | | pub fn writeFbreg(writer: anytype, offset: anytype) !void { |
| 936 | pub fn writeFbreg(writer: *std.io.BufferedWriter, offset: anytype) !void { |
| 868 | 937 | try writer.writeByte(OP.fbreg); |
| 869 | 938 | try leb.writeIleb128(writer, offset); |
| 870 | 939 | } |
| 871 | 940 | |
| 872 | | pub fn writeBreg(writer: anytype, register: u8, offset: anytype) !void { |
| 941 | pub fn writeBreg(writer: *std.io.BufferedWriter, register: u8, offset: anytype) !void { |
| 873 | 942 | if (register > 31) return error.InvalidRegister; |
| 874 | 943 | try writer.writeByte(OP.breg0 + register); |
| 875 | 944 | try leb.writeIleb128(writer, offset); |
| 876 | 945 | } |
| 877 | 946 | |
| 878 | | pub fn writeBregx(writer: anytype, register: anytype, offset: anytype) !void { |
| 947 | pub fn writeBregx(writer: *std.io.BufferedWriter, register: anytype, offset: anytype) !void { |
| 879 | 948 | try writer.writeByte(OP.bregx); |
| 880 | 949 | try leb.writeUleb128(writer, register); |
| 881 | 950 | try leb.writeIleb128(writer, offset); |
| 882 | 951 | } |
| 883 | 952 | |
| 884 | | pub fn writeRegvalType(writer: anytype, register: anytype, offset: anytype) !void { |
| 953 | pub fn writeRegvalType(writer: *std.io.BufferedWriter, register: anytype, offset: anytype) !void { |
| 885 | 954 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 886 | 955 | try writer.writeByte(OP.regval_type); |
| 887 | 956 | try leb.writeUleb128(writer, register); |
| ... | ... | @@ -889,29 +958,29 @@ pub fn Builder(comptime options: Options) type { |
| 889 | 958 | } |
| 890 | 959 | |
| 891 | 960 | // 2.5.1.3: Stack Operations |
| 892 | | pub fn writePick(writer: anytype, index: u8) !void { |
| 961 | pub fn writePick(writer: *std.io.BufferedWriter, index: u8) !void { |
| 893 | 962 | try writer.writeByte(OP.pick); |
| 894 | 963 | try writer.writeByte(index); |
| 895 | 964 | } |
| 896 | 965 | |
| 897 | | pub fn writeDerefSize(writer: anytype, size: u8) !void { |
| 966 | pub fn writeDerefSize(writer: *std.io.BufferedWriter, size: u8) !void { |
| 898 | 967 | try writer.writeByte(OP.deref_size); |
| 899 | 968 | try writer.writeByte(size); |
| 900 | 969 | } |
| 901 | 970 | |
| 902 | | pub fn writeXDerefSize(writer: anytype, size: u8) !void { |
| 971 | pub fn writeXDerefSize(writer: *std.io.BufferedWriter, size: u8) !void { |
| 903 | 972 | try writer.writeByte(OP.xderef_size); |
| 904 | 973 | try writer.writeByte(size); |
| 905 | 974 | } |
| 906 | 975 | |
| 907 | | pub fn writeDerefType(writer: anytype, size: u8, die_offset: anytype) !void { |
| 976 | pub fn writeDerefType(writer: *std.io.BufferedWriter, size: u8, die_offset: anytype) !void { |
| 908 | 977 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 909 | 978 | try writer.writeByte(OP.deref_type); |
| 910 | 979 | try writer.writeByte(size); |
| 911 | 980 | try leb.writeUleb128(writer, die_offset); |
| 912 | 981 | } |
| 913 | 982 | |
| 914 | | pub fn writeXDerefType(writer: anytype, size: u8, die_offset: anytype) !void { |
| 983 | pub fn writeXDerefType(writer: *std.io.BufferedWriter, size: u8, die_offset: anytype) !void { |
| 915 | 984 | try writer.writeByte(OP.xderef_type); |
| 916 | 985 | try writer.writeByte(size); |
| 917 | 986 | try leb.writeUleb128(writer, die_offset); |
| ... | ... | @@ -919,24 +988,24 @@ pub fn Builder(comptime options: Options) type { |
| 919 | 988 | |
| 920 | 989 | // 2.5.1.4: Arithmetic and Logical Operations |
| 921 | 990 | |
| 922 | | pub fn writePlusUconst(writer: anytype, uint_value: anytype) !void { |
| 991 | pub fn writePlusUconst(writer: *std.io.BufferedWriter, uint_value: anytype) !void { |
| 923 | 992 | try writer.writeByte(OP.plus_uconst); |
| 924 | 993 | try leb.writeUleb128(writer, uint_value); |
| 925 | 994 | } |
| 926 | 995 | |
| 927 | 996 | // 2.5.1.5: Control Flow Operations |
| 928 | 997 | |
| 929 | | pub fn writeSkip(writer: anytype, offset: i16) !void { |
| 998 | pub fn writeSkip(writer: *std.io.BufferedWriter, offset: i16) !void { |
| 930 | 999 | try writer.writeByte(OP.skip); |
| 931 | 1000 | try writer.writeInt(i16, offset, options.endian); |
| 932 | 1001 | } |
| 933 | 1002 | |
| 934 | | pub fn writeBra(writer: anytype, offset: i16) !void { |
| 1003 | pub fn writeBra(writer: *std.io.BufferedWriter, offset: i16) !void { |
| 935 | 1004 | try writer.writeByte(OP.bra); |
| 936 | 1005 | try writer.writeInt(i16, offset, options.endian); |
| 937 | 1006 | } |
| 938 | 1007 | |
| 939 | | pub fn writeCall(writer: anytype, comptime T: type, offset: T) !void { |
| 1008 | pub fn writeCall(writer: *std.io.BufferedWriter, comptime T: type, offset: T) !void { |
| 940 | 1009 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 941 | 1010 | switch (T) { |
| 942 | 1011 | u16 => try writer.writeByte(OP.call2), |
| ... | ... | @@ -947,19 +1016,19 @@ pub fn Builder(comptime options: Options) type { |
| 947 | 1016 | try writer.writeInt(T, offset, options.endian); |
| 948 | 1017 | } |
| 949 | 1018 | |
| 950 | | pub fn writeCallRef(writer: anytype, comptime is_64: bool, value: if (is_64) u64 else u32) !void { |
| 1019 | pub fn writeCallRef(writer: *std.io.BufferedWriter, comptime is_64: bool, value: if (is_64) u64 else u32) !void { |
| 951 | 1020 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 952 | 1021 | try writer.writeByte(OP.call_ref); |
| 953 | 1022 | try writer.writeInt(if (is_64) u64 else u32, value, options.endian); |
| 954 | 1023 | } |
| 955 | 1024 | |
| 956 | | pub fn writeConvert(writer: anytype, die_offset: anytype) !void { |
| 1025 | pub fn writeConvert(writer: *std.io.BufferedWriter, die_offset: anytype) !void { |
| 957 | 1026 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 958 | 1027 | try writer.writeByte(OP.convert); |
| 959 | 1028 | try leb.writeUleb128(writer, die_offset); |
| 960 | 1029 | } |
| 961 | 1030 | |
| 962 | | pub fn writeReinterpret(writer: anytype, die_offset: anytype) !void { |
| 1031 | pub fn writeReinterpret(writer: *std.io.BufferedWriter, die_offset: anytype) !void { |
| 963 | 1032 | if (options.call_frame_context) return error.InvalidCFAOpcode; |
| 964 | 1033 | try writer.writeByte(OP.reinterpret); |
| 965 | 1034 | try leb.writeUleb128(writer, die_offset); |
| ... | ... | @@ -967,23 +1036,23 @@ pub fn Builder(comptime options: Options) type { |
| 967 | 1036 | |
| 968 | 1037 | // 2.5.1.7: Special Operations |
| 969 | 1038 | |
| 970 | | pub fn writeEntryValue(writer: anytype, expression: []const u8) !void { |
| 1039 | pub fn writeEntryValue(writer: *std.io.BufferedWriter, expression: []const u8) !void { |
| 971 | 1040 | try writer.writeByte(OP.entry_value); |
| 972 | 1041 | try leb.writeUleb128(writer, expression.len); |
| 973 | 1042 | try writer.writeAll(expression); |
| 974 | 1043 | } |
| 975 | 1044 | |
| 976 | 1045 | // 2.6: Location Descriptions |
| 977 | | pub fn writeReg(writer: anytype, register: u8) !void { |
| 1046 | pub fn writeReg(writer: *std.io.BufferedWriter, register: u8) !void { |
| 978 | 1047 | try writer.writeByte(OP.reg0 + register); |
| 979 | 1048 | } |
| 980 | 1049 | |
| 981 | | pub fn writeRegx(writer: anytype, register: anytype) !void { |
| 1050 | pub fn writeRegx(writer: *std.io.BufferedWriter, register: anytype) !void { |
| 982 | 1051 | try writer.writeByte(OP.regx); |
| 983 | 1052 | try leb.writeUleb128(writer, register); |
| 984 | 1053 | } |
| 985 | 1054 | |
| 986 | | pub fn writeImplicitValue(writer: anytype, value_bytes: []const u8) !void { |
| 1055 | pub fn writeImplicitValue(writer: *std.io.BufferedWriter, value_bytes: []const u8) !void { |
| 987 | 1056 | try writer.writeByte(OP.implicit_value); |
| 988 | 1057 | try leb.writeUleb128(writer, value_bytes.len); |
| 989 | 1058 | try writer.writeAll(value_bytes); |
| ... | ... | @@ -991,21 +1060,21 @@ pub fn Builder(comptime options: Options) type { |
| 991 | 1060 | }; |
| 992 | 1061 | } |
| 993 | 1062 | |
| 994 | | // Certain opcodes are not allowed in a CFA context, see 6.4.2 |
| 995 | | fn isOpcodeValidInCFA(opcode: u8) bool { |
| 1063 | /// Certain opcodes are not allowed in a CFA context, see 6.4.2 |
| 1064 | fn isOpcodeValidInCFA(opcode: OP) bool { |
| 996 | 1065 | return switch (opcode) { |
| 997 | | OP.addrx, |
| 998 | | OP.call2, |
| 999 | | OP.call4, |
| 1000 | | OP.call_ref, |
| 1001 | | OP.const_type, |
| 1002 | | OP.constx, |
| 1003 | | OP.convert, |
| 1004 | | OP.deref_type, |
| 1005 | | OP.regval_type, |
| 1006 | | OP.reinterpret, |
| 1007 | | OP.push_object_address, |
| 1008 | | OP.call_frame_cfa, |
| 1066 | .addrx, |
| 1067 | .call2, |
| 1068 | .call4, |
| 1069 | .call_ref, |
| 1070 | .const_type, |
| 1071 | .constx, |
| 1072 | .convert, |
| 1073 | .deref_type, |
| 1074 | .regval_type, |
| 1075 | .reinterpret, |
| 1076 | .push_object_address, |
| 1077 | .call_frame_cfa, |
| 1009 | 1078 | => false, |
| 1010 | 1079 | else => true, |
| 1011 | 1080 | }; |