|
1 | 1 | use super::{Instr, Reset};
|
2 |
| -use crate::ir::Instruction; |
| 2 | +use crate::{ |
| 3 | + core::FuelCostsProvider, |
| 4 | + engine::translator::{utils::FuelInfo, BumpFuelConsumption}, |
| 5 | + ir::Instruction, |
| 6 | + Error, |
| 7 | +}; |
3 | 8 | use alloc::vec::{self, Vec};
|
4 | 9 |
|
5 | 10 | /// Creates and encodes the list of [`Instruction`]s for a function.
|
@@ -47,6 +52,33 @@ impl InstrEncoder {
|
47 | 52 | &self.instrs[instr.into_usize()]
|
48 | 53 | }
|
49 | 54 |
|
| 55 | + /// Returns an exclusive reference to the [`Instruction`] associated to [`Instr`]. |
| 56 | + /// |
| 57 | + /// # Panics |
| 58 | + /// |
| 59 | + /// If `instr` is out of bounds for `self`. |
| 60 | + pub fn get_mut(&mut self, instr: Instr) -> &mut Instruction { |
| 61 | + &mut self.instrs[instr.into_usize()] |
| 62 | + } |
| 63 | + |
| 64 | + /// Bumps consumed fuel for [`Instruction::ConsumeFuel`] of `instr` by `delta`. |
| 65 | + /// |
| 66 | + /// # Errors |
| 67 | + /// |
| 68 | + /// If consumed fuel is out of bounds after this operation. |
| 69 | + pub fn bump_fuel_consumption<F>(&mut self, fuel_info: &FuelInfo, f: F) -> Result<(), Error> |
| 70 | + where |
| 71 | + F: FnOnce(&FuelCostsProvider) -> u64, |
| 72 | + { |
| 73 | + let FuelInfo::Some { costs, instr } = fuel_info else { |
| 74 | + // Fuel metering is disabled so we can bail out. |
| 75 | + return Ok(()); |
| 76 | + }; |
| 77 | + let fuel_consumed = f(costs); |
| 78 | + self.get_mut(*instr).bump_fuel_consumption(fuel_consumed)?; |
| 79 | + Ok(()) |
| 80 | + } |
| 81 | + |
50 | 82 | /// Returns an iterator yielding all [`Instruction`]s of the [`InstrEncoder`].
|
51 | 83 | ///
|
52 | 84 | /// # Note
|
|
0 commit comments