pub struct Offset(pub u16);
Expand description
Used to simplify calculation of label offsets.
Tuple Fields§
§0: u16
Implementations§
source§impl Offset
impl Offset
sourcepub fn width(&self) -> u16
pub fn width(&self) -> u16
Determine the width of this offset (in bytes).
Examples found in repository?
src/bytecode.rs (line 106)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
fn update_offsets(&self, offsets: &mut [Offset]) -> bool {
let mut changed = false;
let mut offset = 0u16;
// Calculate label offsets
for b in &self.bytecodes {
match b {
Instruction::JUMPDEST(lab) => {
// Extract old offset
let oldoff = offsets[*lab];
// Construct new offset
let newoff = Offset(offset);
// Check!
if oldoff != newoff {
// Offset has changed, but the key thing is
// whether or not its _width_ has changed.
changed |= oldoff.width() != newoff.width();
// Update new offset.
offsets[*lab] = newoff;
}
}
Instruction::PUSH(bs) => offset = offset + (bs.len() as u16),
Instruction::PUSHL(lab) => {
// This time calculate a more accurate figure.
offset = offset + offsets[*lab].width()
}
_ => {}
}
offset = offset + 1;
}
//
changed
}
More examples
src/instruction.rs (line 291)
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
pub fn opcode(&self, offsets: &[Offset]) -> Result<u8,Error> {
let op = match self {
// 0s: Stop and Arithmetic Operations
Instruction::STOP => 0x00,
Instruction::ADD => 0x01,
Instruction::MUL => 0x02,
Instruction::SUB => 0x03,
Instruction::DIV => 0x04,
Instruction::SDIV => 0x05,
Instruction::MOD => 0x06,
Instruction::SMOD => 0x07,
Instruction::ADDMOD => 0x08,
Instruction::MULMOD => 0x09,
Instruction::EXP => 0x0a,
Instruction::SIGNEXTEND => 0x0b,
// 10s: Comparison & Bitwise Logic Operations
Instruction::LT => 0x10,
Instruction::GT => 0x11,
Instruction::SLT => 0x12,
Instruction::SGT => 0x13,
Instruction::EQ => 0x14,
Instruction::ISZERO => 0x15,
Instruction::AND => 0x16,
Instruction::OR => 0x17,
Instruction::XOR => 0x18,
Instruction::NOT => 0x19,
Instruction::BYTE => 0x1a,
Instruction::SHL => 0x1b,
Instruction::SHR => 0x1c,
Instruction::SAR => 0x1d,
// 20s: Keccak256
Instruction::KECCAK256 => 0x20,
// 30s: Environmental Information
Instruction::ADDRESS => 0x30,
Instruction::BALANCE => 0x31,
Instruction::ORIGIN => 0x32,
Instruction::CALLER => 0x33,
Instruction::CALLVALUE => 0x34,
Instruction::CALLDATALOAD => 0x35,
Instruction::CALLDATASIZE => 0x36,
Instruction::CALLDATACOPY => 0x37,
Instruction::CODESIZE => 0x38,
Instruction::CODECOPY => 0x39,
Instruction::GASPRICE => 0x3a,
Instruction::EXTCODESIZE => 0x3b,
Instruction::EXTCODECOPY => 0x3c,
Instruction::RETURNDATASIZE => 0x3d,
Instruction::RETURNDATACOPY => 0x3e,
Instruction::EXTCODEHASH => 0x3f,
// 40s: Block Information
Instruction::BLOCKHASH => 0x40,
Instruction::COINBASE => 0x41,
Instruction::TIMESTAMP => 0x42,
Instruction::NUMBER => 0x43,
Instruction::DIFFICULTY => 0x44,
Instruction::GASLIMIT => 0x45,
Instruction::CHAINID => 0x46,
Instruction::SELFBALANCE => 0x47,
// 50s: Stack, Memory, Storage and Flow Operations
Instruction::POP => 0x50,
Instruction::MLOAD => 0x51,
Instruction::MSTORE => 0x52,
Instruction::MSTORE8 => 0x53,
Instruction::SLOAD => 0x54,
Instruction::SSTORE => 0x55,
Instruction::JUMP => 0x56,
Instruction::JUMPI => 0x57,
Instruction::PC => 0x58,
Instruction::MSIZE => 0x59,
Instruction::GAS => 0x5a,
Instruction::JUMPDEST(_) => 0x5b,
//
// 60s & 70s: Push Operations
Instruction::PUSH(bs) => {
if bs.len() == 0 || bs.len() > 32 {
return Err(Error::InvalidPush);
} else {
(0x5f + bs.len()) as u8
}
}
//
Instruction::PUSHL(lab) => {
let offset = &offsets[*lab];
if offset.width() == 2 { 0x61 }
else { 0x60 }
}
// 80s: Duplication Operations
Instruction::DUP(n) => {
if *n == 0 || *n > 32 {
return Err(Error::InvalidDup);
}
0x7f + n
}
// 90s: Swap Operations
Instruction::SWAP(n) => {
if *n == 0 || *n > 32 {
return Err(Error::InvalidDup);
}
0x8f + n
}
// a0s: Log Operations
Instruction::LOG(n) => {
if *n > 4 {
return Err(Error::InvalidDup);
}
0xa0 + n
}
// f0s: System Operations
Instruction::CREATE => 0xf0,
Instruction::CALL => 0xf1,
Instruction::CALLCODE => 0xf2,
Instruction::RETURN => 0xf3,
Instruction::DELEGATECALL => 0xf4,
Instruction::CREATE2 => 0xf5,
Instruction::STATICCALL => 0xfa,
Instruction::REVERT => 0xfd,
Instruction::INVALID => 0xfe,
Instruction::SELFDESTRUCT => 0xff,
//
Instruction::DATA(_) => {
panic!("Invalid instruction ({:?})",self);
}
};
//
Ok(op)
}
sourcepub fn to_bytes(&self) -> Vec<u8>
pub fn to_bytes(&self) -> Vec<u8>
Examples found in repository?
src/instruction.rs (line 178)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
pub fn encode(&self, offsets: &[Offset], bytes: &mut Vec<u8>) -> Result<(),Error> {
// Push opcode
bytes.push(self.opcode(&offsets)?);
// Push operands (if applicable)
match self {
Instruction::PUSH(args) => {
bytes.extend(args);
}
Instruction::PUSHL(idx) => {
bytes.extend(offsets[*idx].to_bytes());
}
_ => {
// All other instructions have no operands.
}
}
//
Ok(())
}