Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: redesign the IR tree in order to allow its mutability #234

Merged
merged 12 commits into from
Oct 28, 2024
2 changes: 1 addition & 1 deletion lib/src/compiler/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ mod test {
base64_patterns(b"foobar", Some("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")),
vec![
(2, BString::from("mb29iYX")),
(1, BString::from("Zvb2Jhc")),
(1, BString::from("Zvb2Jhc")),
(0, BString::from("Zm9vYmFy"))
]
);
Expand Down
30 changes: 17 additions & 13 deletions lib/src/compiler/context.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
use itertools::Itertools;
use std::cell::Cell;
use std::mem::size_of;
use std::rc::Rc;

use yara_x_parser::ast::{Ident, WithSpan};

use crate::compiler::errors::{CompileError, UnknownPattern};
use crate::compiler::ir::PatternIdx;
use crate::compiler::ir::{PatternIdx, IR};
use crate::compiler::report::ReportBuilder;
use crate::compiler::{ir, Warnings};
use crate::symbols::{StackedSymbolTable, SymbolLookup};
use crate::types::Type;
use crate::wasm;

/// Structure that contains information and data structures required during the
/// current compilation process.
pub(in crate::compiler) struct CompileContext<'a, 'src, 'sym> {
/// compilation of a rule.
pub(crate) struct CompileContext<'a, 'src, 'sym> {
/// Builder for creating error and warning reports.
pub report_builder: &'a ReportBuilder,

/// IR tree for the rule's condition.
pub ir: &'a mut IR,

/// Symbol table that contains the currently defined identifiers, modules,
/// functions, etc.
pub symbol_table: &'a mut StackedSymbolTable<'sym>,
Expand Down Expand Up @@ -47,7 +51,7 @@ pub(in crate::compiler) struct CompileContext<'a, 'src, 'sym> {
pub error_on_slow_loop: bool,

/// Indicates how deep we are inside `for .. of` statements.
pub(crate) for_of_depth: usize,
pub for_of_depth: usize,
}

impl<'a, 'src, 'sym> CompileContext<'a, 'src, 'sym> {
Expand Down Expand Up @@ -118,7 +122,7 @@ impl VarStack {
panic!("variables stack overflow");
}

VarStackFrame { start, capacity, used: 0 }
VarStackFrame { start, capacity, used: Cell::new(0) }
}

/// Unwinds the stack freeing all frames that were allocated after the
Expand All @@ -137,9 +141,9 @@ impl VarStack {
/// allocated within a frame.
#[derive(Clone, Debug)]
pub(crate) struct VarStackFrame {
pub start: i32,
pub used: i32,
pub capacity: i32,
start: i32,
capacity: i32,
used: Cell<i32>,
}

impl VarStackFrame {
Expand All @@ -148,10 +152,10 @@ impl VarStackFrame {
/// # Panics
///
/// Panics if trying to allocate more variables than the frame capacity.
pub fn new_var(&mut self, ty: Type) -> Var {
let index = self.used + self.start;
self.used += 1;
if self.used > self.capacity {
pub fn new_var(&self, ty: Type) -> Var {
let index = self.used.get() + self.start;
self.used.replace(self.used.get() + 1);
if self.used.get() > self.capacity {
panic!("VarStack exceeding its capacity: {}", self.capacity);
}
Var { ty, index }
Expand All @@ -171,7 +175,7 @@ pub(crate) struct Var {

impl Var {
/// Returns the number of bytes that the variable occupies in memory.
pub(crate) const fn mem_size() -> i32 {
pub const fn mem_size() -> i32 {
size_of::<i64>() as i32
}
}
Loading
Loading