Skip to content

Commit

Permalink
refactor: redesign the IR tree in order to allow its mutability (#234)
Browse files Browse the repository at this point in the history
With this change the IR tree is represented by a vector of `Expr`. References to other expressions in the tree are represented by the index of the expression in the vector.
  • Loading branch information
plusvic committed Oct 28, 2024
1 parent a2f18d6 commit 0d8b713
Show file tree
Hide file tree
Showing 16 changed files with 1,941 additions and 1,443 deletions.
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,24 +1,28 @@
use itertools::Itertools;
use rustc_hash::FxHashSet;
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 @@ -51,7 +55,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 @@ -122,7 +126,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 @@ -141,9 +145,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 @@ -152,10 +156,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 @@ -175,7 +179,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

0 comments on commit 0d8b713

Please # to comment.