pub mod cldb;
pub mod cldb_hierarchy;
pub mod clvm;
mod codegen;
#[allow(clippy::module_inception)]
pub mod compiler;
pub mod comptypes;
pub mod debug;
pub mod dialect;
pub mod evaluate;
pub mod frontend;
#[cfg(any(test, feature = "fuzz"))]
pub mod fuzz;
pub mod gensym;
mod inline;
mod lambda;
pub mod optimize;
pub mod preprocessor;
pub mod prims;
pub mod rename;
pub mod repl;
pub mod runtypes;
pub mod sexp;
pub mod srcloc;
pub mod stackvisit;
pub mod usecheck;
use clvmr::allocator::Allocator;
use std::collections::HashMap;
use std::mem::swap;
use std::rc::Rc;
use crate::classic::clvm_tools::stages::stage_0::TRunProgram;
use crate::compiler::comptypes::{
BodyForm, CompileErr, CompileForm, CompilerOpts, DefunData, HelperForm, PrimaryCodegen,
};
use crate::compiler::optimize::Optimization;
use crate::compiler::sexp::SExp;
pub struct BasicCompileContext {
pub allocator: Allocator,
pub runner: Rc<dyn TRunProgram>,
pub symbols: HashMap<String, String>,
pub optimizer: Box<dyn Optimization>,
}
impl BasicCompileContext {
fn allocator(&mut self) -> &mut Allocator {
&mut self.allocator
}
fn runner(&self) -> Rc<dyn TRunProgram> {
self.runner.clone()
}
fn symbols(&mut self) -> &mut HashMap<String, String> {
&mut self.symbols
}
fn frontend_optimization(
&mut self,
opts: Rc<dyn CompilerOpts>,
cf: CompileForm,
) -> Result<CompileForm, CompileErr> {
let runner = self.runner.clone();
self.optimizer
.frontend_optimization(&mut self.allocator, runner, opts, cf)
}
fn post_desugar_optimization(
&mut self,
opts: Rc<dyn CompilerOpts>,
cf: CompileForm,
) -> Result<CompileForm, CompileErr> {
let runner = self.runner.clone();
self.optimizer
.post_desugar_optimization(&mut self.allocator, runner, opts, cf)
}
fn start_of_codegen_optimization(
&mut self,
opts: Rc<dyn CompilerOpts>,
to_optimize: StartOfCodegenOptimization,
) -> Result<StartOfCodegenOptimization, CompileErr> {
let runner = self.runner.clone();
self.optimizer
.start_of_codegen_optimization(&mut self.allocator, runner, opts, to_optimize)
}
fn post_codegen_output_optimize(
&mut self,
opts: Rc<dyn CompilerOpts>,
generated: SExp,
) -> Result<SExp, CompileErr> {
self.optimizer.post_codegen_output_optimize(opts, generated)
}
fn macro_optimization(
&mut self,
opts: Rc<dyn CompilerOpts>,
code: Rc<SExp>,
) -> Result<Rc<SExp>, CompileErr> {
self.optimizer
.macro_optimization(&mut self.allocator, self.runner.clone(), opts, code)
}
fn pre_codegen_function_optimize(
&mut self,
opts: Rc<dyn CompilerOpts>,
codegen: &PrimaryCodegen,
defun: &DefunData,
) -> Result<Rc<BodyForm>, CompileErr> {
self.optimizer.defun_body_optimization(
&mut self.allocator,
self.runner.clone(),
opts,
codegen,
defun,
)
}
fn post_codegen_function_optimize(
&mut self,
opts: Rc<dyn CompilerOpts>,
helper: Option<&HelperForm>,
code: Rc<SExp>,
) -> Result<Rc<SExp>, CompileErr> {
self.optimizer.post_codegen_function_optimize(
&mut self.allocator,
self.runner.clone(),
opts,
helper,
code,
)
}
fn pre_final_codegen_optimize(
&mut self,
opts: Rc<dyn CompilerOpts>,
codegen: &PrimaryCodegen,
) -> Result<Rc<BodyForm>, CompileErr> {
self.optimizer.pre_final_codegen_optimize(
&mut self.allocator,
self.runner.clone(),
opts,
codegen,
)
}
pub fn new(
allocator: Allocator,
runner: Rc<dyn TRunProgram>,
symbols: HashMap<String, String>,
optimizer: Box<dyn Optimization>,
) -> Self {
BasicCompileContext {
allocator,
runner,
symbols,
optimizer,
}
}
}
pub struct CompileContextWrapper<'a> {
pub allocator: &'a mut Allocator,
pub symbols: &'a mut HashMap<String, String>,
pub context: BasicCompileContext,
}
impl<'a> CompileContextWrapper<'a> {
pub fn new(
allocator: &'a mut Allocator,
runner: Rc<dyn TRunProgram>,
symbols: &'a mut HashMap<String, String>,
optimizer: Box<dyn Optimization>,
) -> Self {
let bcc = BasicCompileContext {
allocator: Allocator::new(),
runner,
symbols: HashMap::new(),
optimizer,
};
let mut wrapper = CompileContextWrapper {
allocator,
symbols,
context: bcc,
};
wrapper.switch();
wrapper
}
fn switch(&mut self) {
swap(self.allocator, &mut self.context.allocator);
swap(self.symbols, &mut self.context.symbols);
}
}
impl Drop for CompileContextWrapper<'_> {
fn drop(&mut self) {
self.switch();
}
}
#[derive(Debug, Clone)]
pub struct StartOfCodegenOptimization {
program: CompileForm,
code_generator: PrimaryCodegen,
}