-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathmod.rs
More file actions
109 lines (96 loc) · 3.09 KB
/
mod.rs
File metadata and controls
109 lines (96 loc) · 3.09 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
// SPDX-License-Identifier: Apache-2.0
use crate::codegen::cfg::Instr;
use crate::codegen::Expression;
use crate::codegen::{cfg::ControlFlowGraph, Options};
use crate::{emit::Binary, sema::ast};
use inkwell::{context::Context, module::Module};
pub(super) mod target;
pub struct Midentarget;
impl Midentarget {
pub fn build<'a>(
context: &'a Context,
std_lib: &Module<'a>,
contract: &'a ast::Contract,
ns: &'a ast::Namespace,
opt: &'a Options,
_contract_no: usize,
) -> Binary<'a> {
let filename = ns.files[contract.loc.file_no()].file_name();
let mut bin = Binary::new(
context,
ns,
&contract.id.name,
&filename,
opt,
std_lib,
None,
);
// for each cfg, wrap its instructions in a function and copy it to the binary
for cfg in &contract.cfg {
if cfg.name == "storage_initializer" {
Self::emit_storage_initializer(cfg, &mut bin);
continue;
}
if cfg.name.contains("constructor") {
continue;
}
let name = cfg.name.split("::").last().unwrap();
bin.miden_instrs
.as_ref()
.unwrap()
.borrow_mut()
.push(format!("export.{}", name));
for miden_instr in cfg.miden_instrs.iter() {
bin.miden_instrs
.as_ref()
.unwrap()
.borrow_mut()
.push(miden_instr.to_string());
}
bin.miden_instrs
.as_ref()
.unwrap()
.borrow_mut()
.push("end".to_string());
}
bin
}
pub fn emit_storage_initializer(cfg: &ControlFlowGraph, bin: &mut Binary) {
let use_miden_account = "use.miden::account";
let use_miden_sys = "use.std::sys";
bin.miden_instrs
.as_ref()
.unwrap()
.borrow_mut()
.insert(0, use_miden_account.to_string());
bin.miden_instrs
.as_ref()
.unwrap()
.borrow_mut()
.insert(1, use_miden_sys.to_string());
for instr in cfg.blocks[0].instr.iter().enumerate() {
if let Instr::SetStorage {
ty: _ty,
value: _value,
storage,
storage_type: _storage_type,
} = instr.1
{
if let Expression::NumberLiteral {
loc: _loc,
ty: _ty,
value,
} = storage
{
println!("Set storage at slot: {}", value);
let miden_instr = format!("const.STORAGE_SLOT_{}={}", value, value);
bin.miden_instrs
.as_ref()
.unwrap()
.borrow_mut()
.insert(instr.0 + 2, miden_instr);
}
}
}
}
}