forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.rs
More file actions
389 lines (342 loc) · 11.6 KB
/
backend.rs
File metadata and controls
389 lines (342 loc) · 11.6 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//! Backend abstraction for multi-target code generation.
//!
//! This module defines the [`Backend`] trait that all code generation backends implement.
//! Fe supports multiple backends:
//! - [`YulBackend`]: Emits Yul text for compilation via solc (default)
//! - [`SonatinaBackend`]: Direct EVM bytecode generation via Sonatina IR (WIP)
use driver::DriverDataBase;
use hir::hir_def::TopLevelMod;
use mir::layout::TargetDataLayout;
use std::fmt;
/// Optimization level for code generation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OptLevel {
/// No optimization — maximum debuggability.
O0,
/// Size-oriented optimization.
Os,
/// Speed-oriented optimization (default).
#[default]
O2,
}
impl std::str::FromStr for OptLevel {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"0" => Ok(OptLevel::O0),
"s" => Ok(OptLevel::Os),
"1" => Ok(OptLevel::O2),
"2" => Ok(OptLevel::O2),
_ => Err(format!(
"unknown optimization level: {s} (expected '0', '1', '2', or 's')"
)),
}
}
}
impl OptLevel {
/// Whether the solc Yul optimizer should be enabled for this level.
pub fn yul_optimize(&self) -> bool {
!matches!(self, OptLevel::O0)
}
}
impl fmt::Display for OptLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
OptLevel::O0 => write!(f, "0"),
OptLevel::Os => write!(f, "s"),
OptLevel::O2 => write!(f, "2"),
}
}
}
/// Output produced by a backend compilation.
#[derive(Debug, Clone)]
pub enum BackendOutput {
/// Yul text output (to be compiled by solc).
Yul { source: String, solc_optimize: bool },
/// Raw EVM bytecode.
Bytecode(Vec<u8>),
}
impl BackendOutput {
/// Returns the Yul text if this is a Yul output.
pub fn as_yul(&self) -> Option<&str> {
match self {
BackendOutput::Yul { source, .. } => Some(source),
_ => None,
}
}
/// Returns whether the solc optimizer should be enabled for this Yul output.
pub fn yul_solc_optimize(&self) -> Option<bool> {
match self {
BackendOutput::Yul { solc_optimize, .. } => Some(*solc_optimize),
_ => None,
}
}
/// Returns the bytecode if this is a Bytecode output.
pub fn as_bytecode(&self) -> Option<&[u8]> {
match self {
BackendOutput::Bytecode(b) => Some(b),
_ => None,
}
}
/// Consumes self and returns the Yul text if this is a Yul output.
pub fn into_yul(self) -> Option<String> {
match self {
BackendOutput::Yul { source, .. } => Some(source),
_ => None,
}
}
/// Consumes self and returns the bytecode if this is a Bytecode output.
pub fn into_bytecode(self) -> Option<Vec<u8>> {
match self {
BackendOutput::Bytecode(b) => Some(b),
_ => None,
}
}
}
/// Error type for backend compilation failures.
#[derive(Debug)]
pub enum BackendError {
/// MIR lowering failed.
MirLower(mir::MirLowerError),
/// Yul emission failed.
Yul(crate::yul::YulError),
/// Sonatina compilation failed.
Sonatina(String),
}
impl fmt::Display for BackendError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BackendError::MirLower(err) => write!(f, "{err}"),
BackendError::Yul(err) => write!(f, "{err}"),
BackendError::Sonatina(msg) => write!(f, "sonatina error: {msg}"),
}
}
}
impl std::error::Error for BackendError {}
impl From<mir::MirLowerError> for BackendError {
fn from(err: mir::MirLowerError) -> Self {
BackendError::MirLower(err)
}
}
impl From<crate::yul::YulError> for BackendError {
fn from(err: crate::yul::YulError) -> Self {
BackendError::Yul(err)
}
}
impl From<crate::EmitModuleError> for BackendError {
fn from(err: crate::EmitModuleError) -> Self {
match err {
crate::EmitModuleError::MirLower(e) => BackendError::MirLower(e),
crate::EmitModuleError::Yul(e) => BackendError::Yul(e),
}
}
}
/// A code generation backend that transforms HIR modules to target output.
///
/// Backends are responsible for:
/// 1. Lowering the HIR module to MIR
/// 2. Translating MIR to target-specific IR (if any)
/// 3. Producing the final output (Yul text or bytecode)
pub trait Backend {
/// Returns the human-readable name of this backend.
fn name(&self) -> &'static str;
/// Compiles a top-level module to backend output.
///
/// # Arguments
/// * `db` - Driver database for compiler queries
/// * `top_mod` - The HIR module to compile
/// * `layout` - Target data layout for type sizing
/// * `opt_level` - Optimization level for code generation
///
/// # Returns
/// The compiled output or an error.
fn compile(
&self,
db: &DriverDataBase,
top_mod: TopLevelMod<'_>,
layout: TargetDataLayout,
opt_level: OptLevel,
) -> Result<BackendOutput, BackendError>;
}
/// Available backend implementations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BackendKind {
/// Yul backend (emits Yul text for solc).
Yul,
/// Sonatina backend (direct EVM bytecode generation).
#[default]
Sonatina,
}
impl BackendKind {
/// Returns the name of this backend kind.
pub fn name(&self) -> &'static str {
match self {
BackendKind::Yul => "yul",
BackendKind::Sonatina => "sonatina",
}
}
/// Creates a boxed backend instance for this kind.
pub fn create(&self) -> Box<dyn Backend> {
match self {
BackendKind::Yul => Box::new(YulBackend),
BackendKind::Sonatina => Box::new(SonatinaBackend),
}
}
}
impl std::str::FromStr for BackendKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"yul" => Ok(BackendKind::Yul),
"sonatina" => Ok(BackendKind::Sonatina),
_ => Err(format!(
"unknown backend: {s} (expected 'yul' or 'sonatina')"
)),
}
}
}
/// Yul backend implementation.
///
/// This wraps the existing Yul emitter to implement the [`Backend`] trait.
/// Output is Yul text that can be compiled by solc.
#[derive(Debug, Clone, Copy, Default)]
pub struct YulBackend;
impl Backend for YulBackend {
fn name(&self) -> &'static str {
"yul"
}
fn compile(
&self,
db: &DriverDataBase,
top_mod: TopLevelMod<'_>,
layout: TargetDataLayout,
opt_level: OptLevel,
) -> Result<BackendOutput, BackendError> {
let yul = crate::emit_module_yul_with_layout(db, top_mod, layout)?;
Ok(BackendOutput::Yul {
source: yul,
solc_optimize: opt_level.yul_optimize(),
})
}
}
/// Sonatina backend implementation.
///
/// This backend produces EVM bytecode directly via Sonatina IR,
/// bypassing the need for solc.
#[derive(Debug, Clone, Copy, Default)]
pub struct SonatinaBackend;
impl Backend for SonatinaBackend {
fn name(&self) -> &'static str {
"sonatina"
}
fn compile(
&self,
db: &DriverDataBase,
top_mod: TopLevelMod<'_>,
layout: TargetDataLayout,
opt_level: OptLevel,
) -> Result<BackendOutput, BackendError> {
use sonatina_codegen::isa::evm::EvmBackend;
use sonatina_codegen::object::{CompileOptions, compile_object};
use sonatina_ir::object::{Directive, SectionRef};
// Lower to Sonatina IR
let mut module = crate::sonatina::compile_module(db, top_mod, layout)?;
crate::sonatina::ensure_module_sonatina_ir_valid(&module)?;
// Run the optimization pipeline based on opt_level.
match opt_level {
OptLevel::O0 => { /* no optimization */ }
OptLevel::Os => sonatina_codegen::optim::Pipeline::size().run(&mut module),
OptLevel::O2 => sonatina_codegen::optim::Pipeline::speed().run(&mut module),
}
if opt_level != OptLevel::O0 {
crate::sonatina::ensure_module_sonatina_ir_valid(&module)?;
}
// Check if there are any objects to compile
if module.objects.is_empty() {
return Err(BackendError::Sonatina(
"no objects to compile (module has no functions?)".to_string(),
));
}
// Create the EVM backend for codegen
let isa = crate::sonatina::create_evm_isa();
let evm_backend = EvmBackend::new(isa);
// Find root objects (not referenced by any other object's Embed directives).
let mut referenced_objects = std::collections::BTreeSet::new();
for object in module.objects.values() {
for section in &object.sections {
for directive in §ion.directives {
let Directive::Embed(embed) = directive else {
continue;
};
let SectionRef::External { object, .. } = &embed.source else {
continue;
};
referenced_objects.insert(object.0.as_str().to_string());
}
}
}
let mut roots: Vec<String> = module
.objects
.keys()
.filter(|name| !referenced_objects.contains(*name))
.cloned()
.collect();
roots.sort();
if roots.is_empty() {
return Err(BackendError::Sonatina(
"failed to select root object (all objects are referenced)".to_string(),
));
}
// Pick the primary root for output: prefer "Contract", otherwise first root.
let object_name = if roots.iter().any(|n| n == "Contract") {
"Contract".to_string()
} else {
roots[0].clone()
};
// Compile all root objects so codegen errors in any root are caught.
let opts: CompileOptions<_> = CompileOptions::default();
let mut primary_artifact = None;
for root in &roots {
let artifact =
compile_object(&module, &evm_backend, root, &opts).map_err(|errors| {
let msg = errors
.iter()
.map(|e| format!("{:?}", e))
.collect::<Vec<_>>()
.join("; ");
BackendError::Sonatina(msg)
})?;
if *root == object_name {
primary_artifact = Some(artifact);
}
}
let artifact = primary_artifact.expect("primary root was in roots list");
// Extract bytecode from the runtime section
let section_name = sonatina_ir::object::SectionName::from("runtime");
let runtime_section = artifact.sections.get(§ion_name).ok_or_else(|| {
BackendError::Sonatina("compiled object has no runtime section".to_string())
})?;
Ok(BackendOutput::Bytecode(runtime_section.bytes.clone()))
}
}
#[cfg(test)]
mod tests {
use super::OptLevel;
use std::str::FromStr;
#[test]
fn opt_level_parses_os() {
assert_eq!(OptLevel::from_str("s"), Ok(OptLevel::Os));
}
#[test]
fn opt_level_parses_o1_as_o2() {
assert_eq!(OptLevel::from_str("1"), Ok(OptLevel::O2));
}
#[test]
fn opt_level_default_is_o2() {
assert_eq!(OptLevel::default(), OptLevel::O2);
}
#[test]
fn opt_level_display_uses_os() {
assert_eq!(OptLevel::Os.to_string(), "s");
}
}