1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::fmt;
use std::fmt::{Debug};
use crate::util::{ToHexString};
use super::opcode;
/// Instructions correspond (roughly speaking) to EVM bytecodes.
/// There are a few points to make about this:
///
/// 1. A single instruction can represent an entire _class_ of related
/// bytecodes. For example, the `PUSH(bytes)` instruction corresponds
/// to the various push bytecodes (e.g. `PUSH1`, `PUSH2`, etc).
///
/// 2. Instructions do not necessarily represent actual EVM bytecodes.
/// For example, the `LABEL` instruction has no concrete bytecode
/// representation.[^label_note] Such instructions are typically
/// relevant only at a higher level (e.g. for assembly language).
///
/// 3. Instructions are parameterised over the type of _control flow_
/// they support (i.e. their `Operand`s). This allows
/// `Instruction<T>` to be reused for representing actual bytecodes as
/// well as assembly language instructions.
///
/// 4. All concrete bytecodes (past and present) are represented by an
/// instruction. Thus, depending on the fork, some instructions may
/// not be considered valid in a given context.
///
/// The intention is that all known instructions are represented here
/// in one place, rather than e.g. being separated (somehow) by fork.
///
/// [^label_note]: **NOTE:** One might consider `JUMPDEST` as the
/// appropriate representation. However, since this is a no-operation
/// under the EVM Object Format, it is represented by its own
/// instruction.
#[derive(Clone, Debug, PartialEq)]
pub enum Instruction {
// ===============================================================
// 0s: Stop and Arithmetic Operations
// ===============================================================
/// Halt execution successfully with empty return data.
STOP,
/// Arithmetic addition modulo 2<sup>256</sup>.
ADD,
/// Arithmetic multiplication modulo 2<sup>256</sup>.
MUL,
/// Arithmetic subtraction modulo 2<sup>256</sup>.
SUB,
/// Arithmetic division which rounds towards zero and where
/// _division by zero_ returns zero. For example, `3 / 2` gives
/// `1`.
DIV,
/// Signed arithmetic division which rounds towards zero (i.e. it
/// is
/// [non-Euclidian](https://en.wikipedia.org/wiki/Euclidean_division))
/// and where _division by zero_ returns zero. For example, `-1 /
/// 2` gives `0`.
SDIV,
/// Unsigned arithmetic _modulus_ operator. Thus, for example, `3
/// % 2` gives `1`.
MOD,
/// Signed arithmetic _remainder_ operator. Thus, for example,
/// `-1 % 2` gives `-1`.
SMOD,
/// Arithmetic addition modulo a given value `n`. This is
/// typically used as a cryptographic primitive, where `n` is the
/// order of a given [prime
/// field](https://en.wikipedia.org/wiki/Finite_field).
ADDMOD,
/// Arithmetic multiplication modulo a given value `n`. This is
/// typically used as a cryptographic primitive, where `n` is the
/// order of a given [prime
/// field](https://en.wikipedia.org/wiki/Finite_field).
MULMOD,
/// Arithmetic exponentiation modulo 2<sup>256</sup>.
EXP,
/// Sign extend a value using the _most significant bit (msb)_ of
/// its _kth_ byte. Consider this example for v:
///
/// ```text
/// 23 16 15 8 7 0
/// +--------+--------+--------+
/// ... |10111010|10010101|01000101|
/// +--------+--------+--------+
/// ```
///
/// Then, perfoming a sign extend with `k=0` gives:
///
/// ```text
/// 23 16 15 8 7 0
/// +--------+--------+--------+
/// ... |00000000|00000000|01000101|
/// +--------+--------+--------+
/// ```
///
/// Since the msb of byte 0 is 0, everything above that is set to
/// zero. In contrast, performing a sign extend of our original
/// input with `k=1` gives:
///
/// ```text
/// 23 16 15 8 7 0
/// +--------+--------+--------+
/// ... |11111111|10010101|01000101|
/// +--------+--------+--------+
/// ```
///
/// Since, in this case, the msb of byte 1 is 1.
SIGNEXTEND,
// 10s: Comparison & Bitwise Logic Operations
LT,
GT,
SLT,
SGT,
EQ,
ISZERO,
AND,
OR,
XOR,
NOT,
BYTE,
SHL,
SHR,
SAR,
// 20s: Keccak256
KECCAK256,
// 30s: Environmental Information
ADDRESS,
BALANCE,
ORIGIN,
CALLER,
CALLVALUE,
CALLDATALOAD,
CALLDATASIZE,
CALLDATACOPY,
CODESIZE,
CODECOPY,
GASPRICE,
EXTCODESIZE,
EXTCODECOPY,
RETURNDATASIZE,
RETURNDATACOPY,
EXTCODEHASH,
// 40s: Block Information
BLOCKHASH,
COINBASE,
TIMESTAMP,
NUMBER,
DIFFICULTY,
GASLIMIT,
CHAINID,
SELFBALANCE,
// 50s: Stack, Memory, Storage and Flow Operations
POP,
MLOAD,
MSTORE,
MSTORE8,
SLOAD,
SSTORE,
JUMP,
JUMPI,
PC,
MSIZE,
GAS,
JUMPDEST,
RJUMP(usize), // EIP4200
RJUMPI(usize), // EIP4200
PUSH0, // EIP3855
// 60 & 70s: Push Operations
PUSH(Vec<u8>),
// 80s: Duplicate Operations
DUP(u8),
// 90s: Exchange Operations
SWAP(u8),
// a0s: Logging Operations
LOG(u8),
// f0s: System Operations
CREATE,
CALL,
CALLCODE,
RETURN,
DELEGATECALL,
CREATE2,
STATICCALL,
REVERT,
INVALID,
SELFDESTRUCT,
// Signals arbitrary data in the contract, rather than bytecode
// instructions.
DATA(Vec<u8>),
// (Virtual) Indicates a specific location on the stack should be
// sent to *havoc*. Here, `0` represents the top of the stack.
HAVOC(usize)
}
use Instruction::*;
impl Instruction {
/// Determine whether or not control can continue to the next
/// instruction.
pub fn fallthru(&self) -> bool {
match self {
DATA(_) => false,
INVALID => false,
JUMP => false,
RJUMP(_) => false,
STOP => false,
RETURN => false,
REVERT => false,
SELFDESTRUCT => false,
_ => true,
}
}
/// Determine whether or not this instruction can branch. That
/// is, whether or not it is a `JUMP` or `JUMPI` instruction.
pub fn can_branch(&self) -> bool {
matches!(self, JUMP|JUMPI|RJUMP(_)|RJUMPI(_))
}
/// Encode an instruction into a byte sequence, assuming a given
/// set of label offsets.
pub fn encode(&self, pc: usize, bytes: &mut Vec<u8>) {
// Push operands (if applicable)
match self {
DATA(args) => {
// Push operands
bytes.extend(args);
}
RJUMP(byte_offset)|RJUMPI(byte_offset) => {
// Convert absolute byte offset into relative offset.
let rel_offset = to_rel_offset(pc,*byte_offset);
// Push opcode
bytes.push(self.opcode());
// Push operands
bytes.extend(&rel_offset.to_be_bytes());
}
PUSH(args) => {
// Push opcode
bytes.push(self.opcode());
// Push operands
bytes.extend(args);
}
HAVOC(_) => {
// Virtial instruction, so ignore
}
_ => {
// All other instructions have no operands.
bytes.push(self.opcode());
}
}
}
/// Determine the length of this instruction (in bytes).
pub fn length(&self) -> usize {
match self {
DATA(bytes) => bytes.len(),
// Static jumps
RJUMP(_) => 3,
RJUMPI(_) => 3,
// Push instructions
PUSH(bs) => 1 + bs.len(),
// Virtual instructions
HAVOC(_) => 0,
// Default case
_ => 1,
}
}
/// Determine how many stack operands this instruction consumes.
pub fn operands(&self) -> usize {
match self {
STOP => 0,
ADD|MUL|SUB|DIV|SDIV|MOD|SMOD|EXP|SIGNEXTEND => 2,
ADDMOD|MULMOD => 3,
LT|GT|SLT|SGT|EQ|AND|OR|XOR => 2,
ISZERO|NOT => 1,
BYTE|SHL|SHR|SAR|KECCAK256 => 2,
// 30s: Environmental Information
ADDRESS|ORIGIN|CALLER|CALLVALUE|CALLDATASIZE|CODESIZE|RETURNDATASIZE|GASPRICE => 0,
BALANCE|CALLDATALOAD|EXTCODESIZE|EXTCODEHASH => 1,
CALLDATACOPY|CODECOPY|RETURNDATACOPY => 3,
EXTCODECOPY => 4,
// 40s: Block Information
BLOCKHASH => 1,
COINBASE|TIMESTAMP|NUMBER|DIFFICULTY|GASLIMIT|CHAINID|SELFBALANCE => 0,
// 50s: Stack, Memory, Storage and Flow Operations
MSIZE|PC|GAS|JUMPDEST|RJUMP(_) => 0,
MLOAD|SLOAD|JUMP|POP|RJUMPI(_) => 1,
MSTORE|MSTORE8|SSTORE|JUMPI => 2,
// 60s & 70s: Push Operations
PUSH0|PUSH(_) => 0,
// 80s: Duplication Operations
DUP(_) => 0,
// 90s: Swap Operations
SWAP(_) => 0,
// a0s: Log Operations
LOG(n) => (2+n) as usize,
// f0s: System Operations
INVALID => 0,
SELFDESTRUCT => 1,
RETURN|REVERT => 2,
CREATE => 3,
CREATE2 => 4,
DELEGATECALL|STATICCALL => 6,
CALL|CALLCODE => 7,
// Virtual instructions
HAVOC(_) => 0,
DATA(_) => 0,
_ => { unreachable!("{:?}",self); }
}
}
/// Determine the opcode for a given instruction. In many cases,
/// this is a straightforward mapping. However, in other cases,
/// its slightly more involved as a calculation involving the
/// operands is required.
pub fn opcode(&self) -> u8 {
match self {
// 0s: Stop and Arithmetic Operations
STOP => opcode::STOP,
ADD => opcode::ADD,
MUL => opcode::MUL,
SUB => opcode::SUB,
DIV => opcode::DIV,
SDIV => opcode::SDIV,
MOD => opcode::MOD,
SMOD => opcode::SMOD,
ADDMOD => opcode::ADDMOD,
MULMOD => opcode::MULMOD,
EXP => opcode::EXP,
SIGNEXTEND => opcode::SIGNEXTEND,
// 10s: Comparison & Bitwise Logic Operations
LT => opcode::LT,
GT => opcode::GT,
SLT => opcode::SLT,
SGT => opcode::SGT,
EQ => opcode::EQ,
ISZERO => opcode::ISZERO,
AND => opcode::AND,
OR => opcode::OR,
XOR => opcode::XOR,
NOT => opcode::NOT,
BYTE => opcode::BYTE,
SHL => opcode::SHL,
SHR => opcode::SHR,
SAR => opcode::SAR,
// 20s: Keccak256
KECCAK256 => opcode::KECCAK256,
// 30s: Environmental Information
ADDRESS => opcode::ADDRESS,
BALANCE => opcode::BALANCE,
ORIGIN => opcode::ORIGIN,
CALLER => opcode::CALLER,
CALLVALUE => opcode::CALLVALUE,
CALLDATALOAD => opcode::CALLDATALOAD,
CALLDATASIZE => opcode::CALLDATASIZE,
CALLDATACOPY => opcode::CALLDATACOPY,
CODESIZE => opcode::CODESIZE,
CODECOPY => opcode::CODECOPY,
GASPRICE => opcode::GASPRICE,
EXTCODESIZE => opcode::EXTCODESIZE,
EXTCODECOPY => opcode::EXTCODECOPY,
RETURNDATASIZE => opcode::RETURNDATASIZE,
RETURNDATACOPY => opcode::RETURNDATACOPY,
EXTCODEHASH => opcode::EXTCODEHASH,
// 40s: Block Information
BLOCKHASH => opcode::BLOCKHASH,
COINBASE => opcode::COINBASE,
TIMESTAMP => opcode::TIMESTAMP,
NUMBER => opcode::NUMBER,
DIFFICULTY => opcode::DIFFICULTY,
GASLIMIT => opcode::GASLIMIT,
CHAINID => opcode::CHAINID,
SELFBALANCE => opcode::SELFBALANCE,
// 50s: Stack, Memory, Storage and Flow Operations
POP => opcode::POP,
MLOAD => opcode::MLOAD,
MSTORE => opcode::MSTORE,
MSTORE8 => opcode::MSTORE8,
SLOAD => opcode::SLOAD,
SSTORE => opcode::SSTORE,
JUMP => opcode::JUMP,
JUMPI => opcode::JUMPI,
PC => opcode::PC,
MSIZE => opcode::MSIZE,
GAS => opcode::GAS,
JUMPDEST => opcode::JUMPDEST,
RJUMP(_) => opcode::RJUMP,
RJUMPI(_) => opcode::RJUMPI,
PUSH0 => opcode::PUSH0,
// 60s & 70s: Push Operations
PUSH(bs) => {
if bs.is_empty() || bs.len() > 32 {
panic!("invalid push");
} else {
let n = (bs.len() as u8) - 1;
opcode::PUSH1 + n
}
}
// 80s: Duplication Operations
DUP(n) => {
if *n == 0 || *n > 32 { panic!("invalid dup"); }
opcode::DUP1 + (n-1)
}
// 90s: Swap Operations
SWAP(n) => {
if *n == 0 || *n > 32 { panic!("invalid swap"); }
opcode::SWAP1 + (n-1)
}
// a0s: Log Operations
LOG(n) => {
if *n > 4 { panic!("invalid log"); }
opcode::LOG0 + n
}
// f0s: System Operations
CREATE => opcode::CREATE,
CALL => opcode::CALL,
CALLCODE => opcode::CALLCODE,
RETURN => opcode::RETURN,
DELEGATECALL => opcode::DELEGATECALL,
CREATE2 => opcode::CREATE2,
STATICCALL => opcode::STATICCALL,
REVERT => opcode::REVERT,
INVALID => opcode::INVALID,
SELFDESTRUCT => opcode::SELFDESTRUCT,
//
_ => {
panic!("Invalid instruction ({:?})", self);
}
}
}
/// Decode the next instruction in a given sequence of bytes.
pub fn decode(pc: usize, bytes: &[u8]) -> Instruction {
let opcode = if pc < bytes.len() { bytes[pc] } else { 0x00 };
//
match opcode {
// 0s: Stop and Arithmetic Operations
opcode::STOP => STOP,
opcode::ADD => ADD,
opcode::MUL => MUL,
opcode::SUB => SUB,
opcode::DIV => DIV,
opcode::SDIV => SDIV,
opcode::MOD => MOD,
opcode::SMOD => SMOD,
opcode::ADDMOD => ADDMOD,
opcode::MULMOD => MULMOD,
opcode::EXP => EXP,
opcode::SIGNEXTEND => SIGNEXTEND,
// 10s: Comparison & Bitwise Logic Operations
opcode::LT => LT,
opcode::GT => GT,
opcode::SLT => SLT,
opcode::SGT => SGT,
opcode::EQ => EQ,
opcode::ISZERO => ISZERO,
opcode::AND => AND,
opcode::OR => OR,
opcode::XOR => XOR,
opcode::NOT => NOT,
opcode::BYTE => BYTE,
opcode::SHL => SHL,
opcode::SHR => SHR,
opcode::SAR => SAR,
// 20s: SHA3
opcode::KECCAK256 => KECCAK256,
// 30s: Environmental Information
opcode::ADDRESS => ADDRESS,
opcode::BALANCE => BALANCE,
opcode::ORIGIN => ORIGIN,
opcode::CALLER => CALLER,
opcode::CALLVALUE => CALLVALUE,
opcode::CALLDATALOAD => CALLDATALOAD,
opcode::CALLDATASIZE => CALLDATASIZE,
opcode::CALLDATACOPY => CALLDATACOPY,
opcode::CODESIZE => CODESIZE,
opcode::CODECOPY => CODECOPY,
opcode::GASPRICE => GASPRICE,
opcode::EXTCODESIZE => EXTCODESIZE,
opcode::EXTCODECOPY => EXTCODECOPY,
opcode::RETURNDATASIZE => RETURNDATASIZE,
opcode::RETURNDATACOPY => RETURNDATACOPY,
opcode::EXTCODEHASH => EXTCODEHASH,
// 40s: Block Information
opcode::BLOCKHASH => BLOCKHASH,
opcode::COINBASE => COINBASE,
opcode::TIMESTAMP => TIMESTAMP,
opcode::NUMBER => NUMBER,
opcode::DIFFICULTY => DIFFICULTY,
opcode::GASLIMIT => GASLIMIT,
opcode::CHAINID => CHAINID,
opcode::SELFBALANCE => SELFBALANCE,
// 50s: Stack, Memory, Storage and Flow Operations
opcode::POP => POP,
opcode::MLOAD => MLOAD,
opcode::MSTORE => MSTORE,
opcode::MSTORE8 => MSTORE8,
opcode::SLOAD => SLOAD,
opcode::SSTORE => SSTORE,
opcode::JUMP => JUMP,
opcode::JUMPI => JUMPI,
opcode::PC => PC,
opcode::MSIZE => MSIZE,
opcode::GAS => GAS,
opcode::JUMPDEST => JUMPDEST,
// opcode::RJUMP => {
// // NOTE: these instructions are not permitted to
// // overflow, and therefore don't require padding.
// let arg = [bytes[pc+1],bytes[pc+2]];
// RJUMP(i16::from_be_bytes(arg))
// }
// opcode::RJUMPI => {
// // NOTE: these instructions are not permitted to
// // overflow, and therefore don't require padding.
// let arg = [bytes[pc+1],bytes[pc+2]];
// RJUMPI(i16::from_be_bytes(arg))
// }
opcode::PUSH0 => PUSH0,
// 60s & 70s: Push Operations
opcode::PUSH1..=opcode::PUSH32 => {
let m = pc + 1;
let n = pc + 2 + ((opcode - opcode::PUSH1) as usize);
if n <= bytes.len() {
// Simple case: does not overflow
PUSH(bytes[m..n].to_vec())
} else {
// Harder case: does overflow code.
let mut bs = bytes[m..].to_vec();
// Pad out with zeros
for _i in 0..(n - bytes.len()) {
bs.push(0);
}
// Done
PUSH(bs)
}
}
// 80s: Duplicate Operations
opcode::DUP1..=opcode::DUP16 => DUP(opcode - 0x7f),
// 90s: Swap Operations
opcode::SWAP1..=opcode::SWAP16 => SWAP(opcode - 0x8f),
// a0s: Log Operations
opcode::LOG0..=opcode::LOG4 => LOG(opcode - 0xa0),
// f0s: System Operations
opcode::CREATE => CREATE,
opcode::CALL => CALL,
opcode::CALLCODE => CALLCODE,
opcode::RETURN => RETURN,
opcode::DELEGATECALL => DELEGATECALL,
opcode::CREATE2 => CREATE2,
opcode::STATICCALL => STATICCALL,
opcode::REVERT => REVERT,
opcode::INVALID => INVALID,
opcode::SELFDESTRUCT => SELFDESTRUCT,
// Unknown
_ => DATA(vec![opcode]),
}
}
}
impl fmt::Display for Instruction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Use the default (debug) formatter. Its only for certain
// instructions that we need to do anything different.
match self {
DATA(bytes) => {
// Print bytes as hex string
write!(f, "db {}", bytes.to_hex_string())
}
DUP(n) => {
write!(f, "dup{}",n)
}
LOG(n) => {
write!(f, "log{n}")
}
JUMPDEST => {
write!(f, "jumpdest")
}
PUSH(bytes) => {
// Convert bytes into hex string
let hex = bytes.to_hex_string();
// Print!
write!(f, "push {}", hex)
}
RJUMP(offset) => {
write!(f, "rjump {offset}")
}
RJUMPI(offset) => {
write!(f, "rjumpi {offset}")
}
SWAP(n) => {
write!(f, "swap{n}")
}
HAVOC(n) => {
write!(f, "havoc {n}")
}
_ => {
let s = format!("{:?}",self).to_lowercase();
write!(f, "{s}")
}
}
}
}
// ============================================================================
// Disassemble
// ============================================================================
/// A trait for converting something (e.g. a byte sequence) into a
/// vector of instructions.
pub trait Disassemble {
fn disassemble(&self) -> Vec<Instruction>;
}
impl Disassemble for [u8] {
fn disassemble(&self) -> Vec<Instruction> {
// Initialise instruction offsets
let mut insns = Vec::new();
let mut byte_offset = 0;
//
while byte_offset < self.len() {
let insn = Instruction::decode(byte_offset,self);
byte_offset += insn.length();
insns.push(insn);
}
// Done
insns
}
}
// ============================================================================
// Assemble
// ============================================================================
/// A trait for converting zero or more instructions into vector of
/// bytes.
pub trait Assemble {
fn assemble(&self) -> Vec<u8>;
}
impl Assemble for [Instruction] {
fn assemble(&self) -> Vec<u8> {
// Encode instructions
let mut bytes : Vec<u8> = Vec::new();
let mut pc = 0;
//
for i in self {
i.encode(pc, &mut bytes);
pc += i.length();
}
// Done
bytes
}
}
// ============================================================================
// Utilities
// ============================================================================
/// Calculate the relative offset for a given branch target expressed
/// as an _abstolute byte offset_ from the program counter position
/// where the instruction in question is being instantiated.
fn to_rel_offset(pc: usize, target: usize) -> i16 {
let mut n = target as isize;
n -= pc as isize;
// Following should always be true!
n as i16
}
/// Calculate the variable bytes for an absolute branch target.
fn to_abs_bytes(large: bool, target: usize) -> Vec<u8> {
if large || target > 255 {
vec![(target / 256) as u8, (target % 256) as u8]
} else {
vec![target as u8]
}
}