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
// 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 crate::{Term};
use crate::instruction;
use crate::instruction::{Instruction,Offset};
use crate::compiler;
use crate::compiler::Compiler;
// ============================================================================
// Bytecode Programs
// ============================================================================
/// Represents a sequence of zero or more bytecodes which can be
/// turned, for example, into a hex string. Likewise, they can be
/// decompiled or further optimised.
pub struct Bytecode {
/// The underlying bytecode sequence.
bytecodes: Vec<Instruction>,
/// Counts the number of labels
labels: usize
}
impl Bytecode {
pub fn new() -> Self {
Bytecode{bytecodes:Vec::new(), labels:0}
}
pub fn push(&mut self, insn: Instruction) {
self.bytecodes.push(insn);
}
/// Get access to the raw sequence of instructions.
pub fn instructions(&self) -> &[Instruction] {
&self.bytecodes
}
/// Return the number of labels in the instruction sequence thus
/// far.
pub fn fresh_label(&mut self) -> usize {
let lab = self.labels;
self.labels = self.labels + 1;
lab
}
/// Translate this sequence of bytecode instructions into a
/// sequence of raw bytes. This can still fail in a number of
/// ways. For example, the target for a `PUSHL` does not match
/// any known `JUMPEST` label; Or, the stack size is exceeded,
/// etc.
pub fn to_bytes(&self) -> Result<Vec<u8>,instruction::Error> {
let offsets = self.determine_offsets();
let mut bytes = Vec::new();
//
for b in &self.bytecodes {
// Encode instruction
b.encode(&offsets,&mut bytes)?;
}
// Done
Ok(bytes)
}
/// Determine the offsets of all labels within the instruction
/// sequence. This is non-trivial because labels which are
/// further away affect the overall size of the bytecode sequence
/// (hence, a label can affect the offset of itself or other
/// labels).
fn determine_offsets(&self) -> Vec<Offset> {
// Construct initial set of empty offsets
let mut offsets = vec![Offset(0); self.labels];
// Iterate to a fixpoint.
while self.update_offsets(&mut offsets) {
// Keep going until no more changes!
}
//
offsets
}
/// Update the offset information, noting whether or not anything
/// actually changed. The key is that as we recalculate offsets
/// we may find the width has changed. If this happens, we have
/// to recalculate all offsets again assuming the larger width(s).
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
}
}
// ============================================================================
// Helpers
// ============================================================================
fn try_from(terms: &[Term]) -> Result<Bytecode,compiler::Error> {
let mut bytecode = Bytecode::new();
let mut compiler = Compiler::new(&mut bytecode);
// Translate statements one-by-one
for t in terms {
compiler.translate(t)?;
}
// Done
Ok(bytecode)
}
// ============================================================================
// Trait implementstions
// ============================================================================
/// Translate a sequence of IL statements into EVM bytecode, or fail
/// with an error.
impl TryFrom<&[Term]> for Bytecode {
type Error = compiler::Error;
fn try_from(terms: &[Term]) -> Result<Bytecode,Self::Error> {
try_from(terms)
}
}
/// Translate a sequence of IL statements into EVM bytecode, or fail
/// with an error.
impl<const N: usize> TryFrom<&[Term;N]> for Bytecode {
type Error = compiler::Error;
fn try_from(terms: &[Term;N]) -> Result<Bytecode,Self::Error> {
try_from(terms)
}
}
/// Try and translate a bytecode sequence into byte sequence. This
/// can fail for a number of reasons (e.g. dangling branches, stack
/// depth exceeded, etc).
impl TryInto<Vec<u8>> for Bytecode {
type Error = instruction::Error;
fn try_into(self) -> Result<Vec<u8>,Self::Error> {
self.to_bytes()
}
}