1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! # bcgen_lang
//!
//! Compiles HIR (the representation every LexerSketch language lowers into)
//! to LSB, the bytecode `bvm-lang` executes.
//!
//! ## The lazy path
//!
//! Build or lower a resolved unit, [`compile`] it, and hand the module to a
//! VM:
//!
//! ```
//! use hir_lang::{Builder, Name, OpKind};
//! use intern_lang::Interner;
//!
//! // fn add(a, b) { a + b }
//! let mut names = Interner::new();
//! let mut b = Builder::new();
//! let (pa, a) = b.local_param(Name::new(names.intern("a")));
//! let (pb, bb) = b.local_param(Name::new(names.intern("b")));
//! let (ua, ub) = (b.use_binder(a), b.use_binder(bb));
//! let sum = b.op(OpKind::Add, &[ua, ub]);
//! let body = b.block(&[], Some(sum));
//! let add = b.func(Name::new(names.intern("add")), &[pa, pb], body);
//! let root = b.module(None, &[add]);
//! let hir = b.finish(root)?;
//!
//! let out = bcgen_lang::compile(&hir, &names)?;
//! let module = out.module();
//! assert!(module.function(out.function(add).unwrap()).is_some());
//! assert!(!bytecode_lang::encode(module).is_empty());
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! Running it (with `bvm-lang`): `Program::load(out.into_module(), &host)`,
//! then `vm.run(func, &[Value::Int(2), Value::Int(3)])` returns `Int(5)`.
//!
//! ## What it compiles
//!
//! HIR spec v3 (hir-lang 0.4) to LSB format 2 (bytecode-lang 0.3), in the
//! **dynamic profile**: every value is an LSB `dyn`. Functions with defaults
//! (per call or once), named, variadic, and by-reference parameters, each
//! carrying its LSB parameter list so dynamic calls bind in the VM; locals in
//! registers, shared variables in LSB references; closures with explicit,
//! implicit, and recursive captures; generators (keyed `yield`, `yield
//! from`), `await`, and `spawn`; `if`, the loop primitive with its step,
//! labeled `break`/`continue`, `return`; `match` by a sequential decision
//! procedure, raising `NoMatch` when no arm matches; direct, dynamic, host,
//! and method calls; maps and arrays with PHP value semantics, separation
//! that copies only what is shared, and key coercion (or shared references);
//! `let_place` and PHP references into array elements; every OPS v2
//! operation with its policy (`pow`, `promote`, `shift = saturate`);
//! union and intersection type tests and casts; `throw` and
//! `try`/`catch`/`finally` by LSB's canonical lowering, `defer`; globals,
//! constants, statics, and the module body; named intrinsics through an
//! [`Intrinsics`] table ([`Intrinsics::mox`] maps every Mox intrinsic to an
//! inline fast path or a host function). Every instruction maps back to the
//! HIR node it came from ([`Compiled::origin`]) and to a line-table row.
//!
//! Classes (0.4): classes, interfaces, and mixins (PHP traits) compile to LSB
//! structs with PHP's semantics: single inheritance, constructors,
//! properties with defaults, instance and static methods, `self::`,
//! `parent::`, and late-bound `static::`, static properties, constants,
//! `new`/`instanceof`/`clone` as [`ClassOp`]s of the intrinsic table,
//! visibility checked where PHP checks it (raised as `TypeError` with PHP's
//! message), objects thrown and caught by class or interface, and the
//! destructor's name for the VM ([`Compiled::destructor`]).
//!
//! Not yet: declared records and sums, and typed fast paths (0.5); each is a
//! precise [`CompileError::Unsupported`], as is every class form HIR or LSB
//! cannot express (multiple inheritance, `impl` blocks).
//!
//! ## Guarantees
//!
//! - **Loads.** Every module it emits passes `bvm-lang`'s load-time checks:
//! registers, windows, constants, branch targets, and handlers in range.
//! - **Semantics.** Differential tests run random programs through the
//! compiler and the VM, and through a reference interpreter written in
//! the tests, comparing results, error kinds, and error locations.
//! - **No recursion over the input.** Generation is an explicit task stack;
//! any nesting depth compiles in bounded native stack.
//! - **Budgeted and deterministic.** Output is bounded by
//! [`Options::with_max_instructions`]; the same unit compiles to the same
//! bytes.
//!
//! ## `no_std`
//!
//! The crate needs only `alloc`. The default `std` feature is additive.
extern crate alloc;
pub use ;
pub use ;
pub use ;
pub use Compiled;
/// Compiles and runs the `rust` code blocks in `README.md`, `docs/API.md`,
/// and the current release notes as part of `cargo test`, so the published
/// examples cannot drift from the API.
;
use Hir;
use Lookup;
/// Compiles a resolved unit with the default [`Options`] (the PHP profile).
///
/// `names` is the interner the unit's names were interned in; function,
/// import, property, and debug names are read from it.
///
/// # Errors
///
/// A [`CompileError`] naming the first form that does not compile, in the
/// unit's canonical order.
///
/// # Examples
///
/// ```
/// use hir_lang::Builder;
/// use intern_lang::Interner;
///
/// let mut b = Builder::new();
/// let root = b.module(None, &[]);
/// let hir = b.finish(root)?;
/// let out = bcgen_lang::compile(&hir, &Interner::new())?;
/// assert_eq!(out.module().functions().len(), 1); // the entry function
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
/// A configured compiler: [`Options`] and, optionally, a [`Locate`] for the
/// line tables.
///
/// # Examples
///
/// ```
/// use bcgen_lang::{Compiler, LineIndex, Options};
/// use hir_lang::Builder;
/// use intern_lang::Interner;
///
/// let lines = LineIndex::new("main.mox", 0, "");
/// let mut b = Builder::new();
/// let root = b.module(None, &[]);
/// let hir = b.finish(root)?;
/// let out = Compiler::new(Options::new())
/// .with_locator(&lines)
/// .compile(&hir, &Interner::new())?;
/// assert!(out.start().is_none());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```