| ... | @@ -18,14 +18,14 @@ pub const StackMachineOptions = struct { | ... | @@ -18,14 +18,14 @@ pub const StackMachineOptions = struct { |
| 18 | /// Expressions can be decoded for non-native address size and endianness, | 18 | /// Expressions can be decoded for non-native address size and endianness, |
| 19 | /// but can only be executed if the current target matches the configuration. | 19 | /// but can only be executed if the current target matches the configuration. |
| 20 | pub fn StackMachine(comptime options: StackMachineOptions) type { | 20 | pub fn StackMachine(comptime options: StackMachineOptions) type { |
| 21 | const addr_type = switch(options.addr_size) { | 21 | const addr_type = switch (options.addr_size) { |
| 22 | 2 => u16, | 22 | 2 => u16, |
| 23 | 4 => u32, | 23 | 4 => u32, |
| 24 | 8 => u64, | 24 | 8 => u64, |
| 25 | else => @compileError("Unsupported address size of " ++ options.addr_size), | 25 | else => @compileError("Unsupported address size of " ++ options.addr_size), |
| 26 | }; | 26 | }; |
| 27 | | 27 | |
| 28 | const addr_type_signed = switch(options.addr_size) { | 28 | const addr_type_signed = switch (options.addr_size) { |
| 29 | 2 => i16, | 29 | 2 => i16, |
| 30 | 4 => i32, | 30 | 4 => i32, |
| 31 | 8 => i64, | 31 | 8 => i64, |
| ... | @@ -61,19 +61,15 @@ pub fn StackMachine(comptime options: StackMachineOptions) type { | ... | @@ -61,19 +61,15 @@ pub fn StackMachine(comptime options: StackMachineOptions) type { |
| 61 | fn generic(value: anytype) Value { | 61 | fn generic(value: anytype) Value { |
| 62 | const int_info = @typeInfo(@TypeOf(value)).Int; | 62 | const int_info = @typeInfo(@TypeOf(value)).Int; |
| 63 | if (@sizeOf(@TypeOf(value)) > options.addr_size) { | 63 | if (@sizeOf(@TypeOf(value)) > options.addr_size) { |
| 64 | return .{ | 64 | return .{ .generic = switch (int_info.signedness) { |
| 65 | .generic = switch (int_info.signedness) { | 65 | .signed => @bitCast(addr_type, @truncate(addr_type_signed, value)), |
| 66 | .signed => @bitCast(addr_type, @truncate(addr_type_signed, value)), | 66 | .unsigned => @truncate(addr_type, value), |
| 67 | .unsigned => @truncate(addr_type, value), | 67 | } }; |
| 68 | } | | |
| 69 | }; | | |
| 70 | } else { | 68 | } else { |
| 71 | return .{ | 69 | return .{ .generic = switch (int_info.signedness) { |
| 72 | .generic = switch (int_info.signedness) { | 70 | .signed => @bitCast(addr_type, @intCast(addr_type_signed, value)), |
| 73 | .signed => @bitCast(addr_type, @intCast(addr_type_signed, value)), | 71 | .unsigned => @intCast(addr_type, value), |
| 74 | .unsigned => @intCast(addr_type, value), | 72 | } }; |
| 75 | } | | |
| 76 | }; | | |
| 77 | } | 73 | } |
| 78 | } | 74 | } |
| 79 | | 75 | |
| ... | @@ -113,20 +109,15 @@ pub fn StackMachine(comptime options: StackMachineOptions) type { | ... | @@ -113,20 +109,15 @@ pub fn StackMachine(comptime options: StackMachineOptions) type { |
| 113 | => generic(try leb.readILEB128(i64, reader)), | 109 | => generic(try leb.readILEB128(i64, reader)), |
| 114 | OP.lit0...OP.lit31 => |n| generic(n - OP.lit0), | 110 | OP.lit0...OP.lit31 => |n| generic(n - OP.lit0), |
| 115 | OP.reg0...OP.reg31 => |n| .{ .register = n - OP.reg0 }, | 111 | OP.reg0...OP.reg31 => |n| .{ .register = n - OP.reg0 }, |
| 116 | OP.breg0...OP.breg31 => |n| .{ | 112 | OP.breg0...OP.breg31 => |n| .{ .base_register = .{ |
| 117 | .base_register = .{ | 113 | .base_register = n - OP.breg0, |
| 118 | .base_register = n - OP.breg0, | 114 | .offset = try leb.readILEB128(i64, reader), |
| 119 | .offset = try leb.readILEB128(i64, reader), | 115 | } }, |
| 120 | } | | |
| 121 | }, | | |
| 122 | OP.regx => .{ .register = try leb.readULEB128(u8, reader) }, | 116 | OP.regx => .{ .register = try leb.readULEB128(u8, reader) }, |
| 123 | OP.bregx, | 117 | OP.bregx, OP.regval_type => .{ .base_register = .{ |
| 124 | OP.regval_type => .{ | 118 | .base_register = try leb.readULEB128(u8, reader), |
| 125 | .base_register = .{ | 119 | .offset = try leb.readILEB128(i64, reader), |
| 126 | .base_register = try leb.readULEB128(u8, reader), | 120 | } }, |
| 127 | .offset = try leb.readILEB128(i64, reader), | | |
| 128 | } | | |
| 129 | }, | | |
| 130 | OP.piece => .{ | 121 | OP.piece => .{ |
| 131 | .composite_location = .{ | 122 | .composite_location = .{ |
| 132 | .size = try leb.readULEB128(u8, reader), | 123 | .size = try leb.readULEB128(u8, reader), |
| ... | @@ -139,9 +130,7 @@ pub fn StackMachine(comptime options: StackMachineOptions) type { | ... | @@ -139,9 +130,7 @@ pub fn StackMachine(comptime options: StackMachineOptions) type { |
| 139 | .offset = try leb.readILEB128(i64, reader), | 130 | .offset = try leb.readILEB128(i64, reader), |
| 140 | }, | 131 | }, |
| 141 | }, | 132 | }, |
| 142 | OP.implicit_value, | 133 | OP.implicit_value, OP.entry_value => blk: { |
| 143 | OP.entry_value | | |
| 144 | => blk: { | | |
| 145 | const size = try leb.readULEB128(u8, reader); | 134 | const size = try leb.readULEB128(u8, reader); |
| 146 | if (stream.pos + size > stream.buffer.len) return error.InvalidExpression; | 135 | if (stream.pos + size > stream.buffer.len) return error.InvalidExpression; |
| 147 | const block = stream.buffer[stream.pos..][0..size]; | 136 | const block = stream.buffer[stream.pos..][0..size]; |
| ... | @@ -156,12 +145,10 @@ pub fn StackMachine(comptime options: StackMachineOptions) type { | ... | @@ -156,12 +145,10 @@ pub fn StackMachine(comptime options: StackMachineOptions) type { |
| 156 | if (stream.pos + size > stream.buffer.len) return error.InvalidExpression; | 145 | if (stream.pos + size > stream.buffer.len) return error.InvalidExpression; |
| 157 | const value_bytes = stream.buffer[stream.pos..][0..size]; | 146 | const value_bytes = stream.buffer[stream.pos..][0..size]; |
| 158 | stream.pos += size; | 147 | stream.pos += size; |
| 159 | break :blk .{ | 148 | break :blk .{ .base_type = .{ |
| 160 | .base_type = .{ | 149 | .type_offset = type_offset, |
| 161 | .type_offset = type_offset, | 150 | .value_bytes = value_bytes, |
| 162 | .value_bytes = value_bytes, | 151 | } }; |
| 163 | } | | |
| 164 | }; | | |
| 165 | }, | 152 | }, |
| 166 | OP.deref_type, | 153 | OP.deref_type, |
| 167 | OP.xderef_type, | 154 | OP.xderef_type, |