mq_lang/engine/
compiled_program.rs1#[cfg(not(feature = "debugger"))]
3use crate::SharedCell;
4#[cfg(any(not(feature = "debugger"), feature = "mqc"))]
5use crate::{Shared, tarn};
6
7#[derive(Debug, Clone)]
9pub struct CompiledProgram {
10 pub(crate) source: String,
11 pub(crate) body: ProgramBody,
12}
13
14#[derive(Debug, Clone)]
15pub(crate) enum ProgramBody {
16 #[cfg(feature = "debugger")]
18 Ast(crate::ast::Program),
19 #[cfg(not(feature = "debugger"))]
21 Cached {
22 program: crate::ast::Program,
23 cache: VmCache,
24 },
25 #[cfg(feature = "mqc")]
27 Precompiled(Shared<tarn::split_program::SplitProgram>),
28}
29
30#[cfg(not(feature = "debugger"))]
32#[derive(Debug, Clone, Default)]
33pub(crate) struct VmCache(Shared<SharedCell<Option<Shared<tarn::CachedProgram>>>>);
34
35#[cfg(not(feature = "debugger"))]
36impl VmCache {
37 pub(crate) fn get(&self) -> Option<Shared<tarn::CachedProgram>> {
38 #[cfg(feature = "sync")]
39 {
40 self.0.read().unwrap().clone()
41 }
42 #[cfg(not(feature = "sync"))]
43 {
44 self.0.borrow().clone()
45 }
46 }
47
48 pub(crate) fn set(&self, program: Shared<tarn::CachedProgram>) {
49 #[cfg(feature = "sync")]
50 {
51 *self.0.write().unwrap() = Some(program);
52 }
53 #[cfg(not(feature = "sync"))]
54 {
55 *self.0.borrow_mut() = Some(program);
56 }
57 }
58}
59
60impl CompiledProgram {
61 pub(super) fn cached(source: String, program: crate::ast::Program) -> Self {
63 #[cfg(not(feature = "debugger"))]
64 let body = ProgramBody::Cached {
65 program,
66 cache: VmCache::default(),
67 };
68 #[cfg(feature = "debugger")]
69 let body = ProgramBody::Ast(program);
70 Self { source, body }
71 }
72
73 pub fn source(&self) -> &str {
75 &self.source
76 }
77
78 pub fn program(&self) -> Option<&crate::ast::Program> {
80 match &self.body {
81 #[cfg(feature = "debugger")]
82 ProgramBody::Ast(program) => Some(program),
83 #[cfg(not(feature = "debugger"))]
84 ProgramBody::Cached { program, .. } => Some(program),
85 #[cfg(feature = "mqc")]
86 ProgramBody::Precompiled(_) => None,
87 }
88 }
89
90 #[cfg(feature = "mqc")]
91 pub(crate) fn from_precompiled(source: String, program: tarn::split_program::SplitProgram) -> Self {
92 Self {
93 source,
94 body: ProgramBody::Precompiled(Shared::new(program)),
95 }
96 }
97
98 #[cfg(all(test, not(feature = "debugger")))]
99 pub(crate) fn vm_cache(&self) -> Option<&VmCache> {
100 match &self.body {
101 ProgramBody::Cached { cache, .. } => Some(cache),
102 #[allow(unreachable_patterns)]
103 _ => None,
104 }
105 }
106}
107
108impl From<crate::ast::Program> for CompiledProgram {
109 fn from(program: crate::ast::Program) -> Self {
111 Self::cached(String::new(), program)
112 }
113}