Skip to content

Commit 38748f7

Browse files
committed
frontend: Emit bytecode as part of Emitter
1 parent 60ccffd commit 38748f7

File tree

5 files changed

+53
-54
lines changed

5 files changed

+53
-54
lines changed

dora-frontend/src/generator.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use self::expr::{
1010
gen_expr, gen_expr_bin_cmp, gen_expr_condition, gen_fatal_error, gen_intrinsic_bin,
1111
gen_method_bin,
1212
};
13+
use crate::program_emitter::Emitter;
1314
use crate::sema::{
1415
emit_as_bytecode_operation, new_identity_type_params, AnalysisData, CallType,
1516
ClassDefinitionId, ConstDefinitionId, ContextFieldId, Element, EnumDefinitionId, FctDefinition,
@@ -42,16 +43,22 @@ impl LoopLabels {
4243
}
4344
}
4445

45-
pub fn generate_fct_id(sa: &Sema, id: FctDefinitionId) -> BytecodeFunction {
46+
pub fn generate_fct_id(sa: &Sema, emitter: &mut Emitter, id: FctDefinitionId) -> BytecodeFunction {
4647
let fct = sa.fct(id);
4748
let analysis = fct.analysis();
4849

49-
generate_fct(sa, &fct, analysis)
50+
generate_fct(sa, emitter, &fct, analysis)
5051
}
5152

52-
pub fn generate_fct(sa: &Sema, fct: &FctDefinition, src: &AnalysisData) -> BytecodeFunction {
53+
pub fn generate_fct(
54+
sa: &Sema,
55+
emitter: &mut Emitter,
56+
fct: &FctDefinition,
57+
src: &AnalysisData,
58+
) -> BytecodeFunction {
5359
let ast_bytecode_generator = AstBytecodeGen {
5460
sa,
61+
emitter,
5562
type_params_len: fct.type_param_definition().type_param_count(),
5663
is_lambda: fct.is_lambda(),
5764
return_type: fct.return_type(),
@@ -70,11 +77,13 @@ pub fn generate_fct(sa: &Sema, fct: &FctDefinition, src: &AnalysisData) -> Bytec
7077

7178
pub fn generate_global_initializer(
7279
sa: &Sema,
80+
emitter: &mut Emitter,
7381
global: &GlobalDefinition,
7482
src: &AnalysisData,
7583
) -> BytecodeFunction {
7684
let ast_bytecode_generator = AstBytecodeGen {
7785
sa,
86+
emitter,
7887
type_params_len: 0,
7988
is_lambda: false,
8089
return_type: global.ty(),
@@ -101,6 +110,8 @@ struct EnteredContext {
101110

102111
struct AstBytecodeGen<'a> {
103112
sa: &'a Sema,
113+
#[allow(unused)]
114+
emitter: &'a mut Emitter,
104115
type_params_len: usize,
105116
is_lambda: bool,
106117
return_type: SourceType,

dora-frontend/src/generator/tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::mem;
33

44
use self::Bytecode::*;
55
use crate::generator::{bty_from_ty, generate_fct_id};
6+
use crate::program_emitter::Emitter;
67
use crate::sema::{create_tuple, Sema};
78
use crate::sema::{ClassDefinitionId, FieldId, SemaFlags};
89
use crate::stdlib_lookup::{lookup_fct, resolve_path};
@@ -36,7 +37,8 @@ fn sema(code: &'static str) -> Sema {
3637

3738
fn bc(sa: &Sema, path: &str) -> (BytecodeFunction, Vec<Bytecode>) {
3839
let fct_id = lookup_fct(sa, path);
39-
let bc_fct = generate_fct_id(sa, fct_id);
40+
let mut emitter = Emitter::new();
41+
let bc_fct = generate_fct_id(sa, &mut emitter, fct_id);
4042
let code = build(&bc_fct);
4143

4244
(bc_fct, code)

dora-frontend/src/lib.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -116,32 +116,6 @@ pub fn emit_ast(sa: &Sema, filter: &str) {
116116
}
117117
}
118118

119-
pub fn generate_bytecode(sa: &Sema) {
120-
for (_id, fct) in sa.fcts.iter() {
121-
let bc = {
122-
if !fct.has_body() {
123-
continue;
124-
}
125-
126-
let analysis = fct.analysis();
127-
generator::generate_fct(sa, &*fct, analysis)
128-
};
129-
130-
assert!(fct.bytecode.set(bc).is_ok());
131-
}
132-
133-
for (_id, global) in sa.globals.iter() {
134-
if !global.has_initial_value() {
135-
continue;
136-
}
137-
138-
let analysis = global.analysis();
139-
let bc = generator::generate_global_initializer(sa, global, analysis);
140-
141-
assert!(global.bytecode.set(bc).is_ok());
142-
}
143-
}
144-
145119
pub fn emit_bytecode(prog: &Program, filter: &str) {
146120
for (id, fct) in prog.functions.iter().enumerate() {
147121
let id = FunctionId(id.try_into().expect("overflow"));

dora-frontend/src/program_emitter.rs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use dora_bytecode::{
99
TypeParamBound, TypeParamData,
1010
};
1111

12-
use crate::generator::{bty_from_ty, convert_trait_type};
12+
use crate::generator::{
13+
bty_from_ty, convert_trait_type, generate_fct, generate_global_initializer,
14+
};
1315

1416
use crate::sema::{
1517
self, AliasDefinitionId, ClassDefinition, Element, EnumDefinition, FctDefinitionId, FctParent,
@@ -21,22 +23,7 @@ use crate::sema::{
2123
};
2224

2325
pub fn emit_program(sa: Sema) -> Program {
24-
let mut emitter = Emitter {
25-
global_initializer: HashMap::new(),
26-
map_functions: HashMap::new(),
27-
functions: Vec::new(),
28-
globals: Vec::new(),
29-
packages: Vec::new(),
30-
modules: Vec::new(),
31-
classes: Vec::new(),
32-
structs: Vec::new(),
33-
enums: Default::default(),
34-
traits: Default::default(),
35-
source_files: Default::default(),
36-
extensions: Default::default(),
37-
impls: Default::default(),
38-
aliases: Default::default(),
39-
};
26+
let mut emitter = Emitter::new();
4027

4128
emitter.create_packages(&sa);
4229
emitter.create_modules(&sa);
@@ -76,7 +63,7 @@ pub fn emit_program(sa: Sema) -> Program {
7663
}
7764
}
7865

79-
struct Emitter {
66+
pub struct Emitter {
8067
global_initializer: HashMap<GlobalDefinitionId, FunctionId>,
8168
map_functions: HashMap<FctDefinitionId, FunctionId>,
8269
functions: Vec<FunctionData>,
@@ -94,6 +81,25 @@ struct Emitter {
9481
}
9582

9683
impl Emitter {
84+
pub fn new() -> Emitter {
85+
Emitter {
86+
global_initializer: HashMap::new(),
87+
map_functions: HashMap::new(),
88+
functions: Vec::new(),
89+
globals: Vec::new(),
90+
packages: Vec::new(),
91+
modules: Vec::new(),
92+
classes: Vec::new(),
93+
structs: Vec::new(),
94+
enums: Default::default(),
95+
traits: Default::default(),
96+
source_files: Default::default(),
97+
extensions: Default::default(),
98+
impls: Default::default(),
99+
aliases: Default::default(),
100+
}
101+
}
102+
97103
fn create_packages(&mut self, sa: &Sema) {
98104
for (_id, pkg) in sa.packages.iter() {
99105
let name = match pkg.name {
@@ -244,6 +250,13 @@ impl Emitter {
244250
FctParent::None => FunctionKind::Function,
245251
};
246252

253+
let bc_fct = if fct.has_body() {
254+
let analysis = fct.analysis();
255+
Some(generate_fct(sa, self, &*fct, analysis))
256+
} else {
257+
None
258+
};
259+
247260
let function_id = FunctionId(self.functions.len().try_into().expect("overflow"));
248261
self.functions.push(FunctionData {
249262
name,
@@ -267,7 +280,7 @@ impl Emitter {
267280
is_force_inline: fct.is_force_inline,
268281
is_never_inline: fct.is_never_inline,
269282
is_trait_object_ignore: fct.is_trait_object_ignore,
270-
bytecode: fct.bytecode.get().cloned(),
283+
bytecode: bc_fct,
271284
trait_method_impl: fct
272285
.trait_method_impl
273286
.get()
@@ -286,6 +299,9 @@ impl Emitter {
286299
let fct_id = FunctionId(self.functions.len().try_into().expect("overflow"));
287300
let name = sa.interner.str(global.name).to_string();
288301

302+
let analysis = global.analysis();
303+
let bc_fct = generate_global_initializer(sa, self, global, analysis);
304+
289305
self.functions.push(FunctionData {
290306
name,
291307
loc: sa.compute_loc(global.file_id, global.span),
@@ -304,7 +320,7 @@ impl Emitter {
304320
is_force_inline: false,
305321
is_never_inline: false,
306322
is_trait_object_ignore: false,
307-
bytecode: Some(global.bytecode().clone()),
323+
bytecode: Some(bc_fct),
308324
trait_method_impl: None,
309325
});
310326

dora/src/driver/start.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ fn compile_std_library(flags: &DriverFlags) -> Result<Program, ()> {
141141
language::emit_ast(&sa, filter);
142142
}
143143

144-
language::generate_bytecode(&sa);
145-
146144
// Create a serializable data structure from bytecode and metadata.
147145
// Here we drop the generated AST.
148146
let prog = language::emit_program(sa);
@@ -191,8 +189,6 @@ fn compile_into_program(flags: &DriverFlags, file: String) -> Result<Program, ()
191189
language::emit_ast(&sa, filter);
192190
}
193191

194-
language::generate_bytecode(&sa);
195-
196192
// Create a serializable data structure from bytecode and metadata.
197193
// Here we drop the generated AST.
198194
let prog = language::emit_program(sa);

0 commit comments

Comments
 (0)