forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.rs
More file actions
180 lines (163 loc) · 5.68 KB
/
Copy pathruntime.rs
File metadata and controls
180 lines (163 loc) · 5.68 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
use hir::analysis::semantic::{SemanticInstance, check_semantic_borrows, check_semantic_noesc};
use salsa::Update;
use crate::{
db::MirDb,
runtime::{
LowerError, LoweredRuntimeBody, RuntimeBody, RuntimeCallEdge, RuntimeClass,
RuntimeExitBehavior, RuntimeInterfaceSignature, RuntimeSyntheticSpec,
lower::{
abi::runtime_abi_plan,
body::lower_to_rmir,
call::{
collect_referenced_code_regions, collect_referenced_const_regions,
collect_runtime_calls as collect_runtime_calls_lowered,
},
returns::runtime_exit_behavior,
},
synthetic::lower_synthetic_runtime_body,
},
};
#[salsa::interned]
#[derive(Debug)]
pub struct RuntimeSyntheticInstance<'db> {
pub spec: RuntimeSyntheticSpec<'db>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Update)]
pub enum RuntimeInstanceSource<'db> {
Semantic(SemanticInstance<'db>),
Synthetic(RuntimeSyntheticInstance<'db>),
}
#[salsa::interned]
#[derive(Debug)]
pub struct RuntimeInstanceKey<'db> {
pub source: RuntimeInstanceSource<'db>,
#[return_ref]
pub params: Vec<RuntimeClass<'db>>,
}
impl<'db> RuntimeInstanceKey<'db> {
pub fn semantic(self, db: &'db dyn MirDb) -> Option<SemanticInstance<'db>> {
match self.source(db) {
RuntimeInstanceSource::Semantic(semantic) => Some(semantic),
RuntimeInstanceSource::Synthetic(_) => None,
}
}
}
#[salsa::tracked]
#[derive(Debug)]
pub struct RuntimeInstance<'db> {
pub key: RuntimeInstanceKey<'db>,
}
#[salsa::tracked]
impl<'db> RuntimeInstance<'db> {
#[salsa::tracked]
pub fn interface_signature(self, db: &'db dyn MirDb) -> RuntimeInterfaceSignature<'db> {
runtime_interface_signature_for_key(db, self.key(db))
}
#[salsa::tracked]
pub fn exit_behavior(self, db: &'db dyn MirDb) -> RuntimeExitBehavior {
runtime_exit_behavior(db, self.key(db))
}
#[salsa::tracked]
pub fn body(self, db: &'db dyn MirDb) -> RuntimeBody<'db> {
expect_lowered_runtime_body(db, self).body(db)
}
#[salsa::tracked(return_ref)]
pub fn calls(self, db: &'db dyn MirDb) -> Vec<RuntimeCallEdge<'db>> {
expect_lowered_runtime_body(db, self).direct_callees(db)
}
#[salsa::tracked(return_ref)]
pub fn referenced_const_regions(
self,
db: &'db dyn MirDb,
) -> Vec<crate::runtime::ConstRegionId<'db>> {
expect_lowered_runtime_body(db, self).referenced_const_regions(db)
}
#[salsa::tracked(return_ref)]
pub fn referenced_code_regions(
self,
db: &'db dyn MirDb,
) -> Vec<crate::runtime::RuntimeCodeRegion<'db>> {
expect_lowered_runtime_body(db, self).referenced_code_regions(db)
}
}
pub(crate) fn runtime_interface_signature_for_key<'db>(
db: &'db dyn MirDb,
key: RuntimeInstanceKey<'db>,
) -> RuntimeInterfaceSignature<'db> {
runtime_abi_plan(db, key).signature()
}
#[salsa::tracked]
pub fn get_or_build_runtime_instance<'db>(
db: &'db dyn MirDb,
key: RuntimeInstanceKey<'db>,
) -> RuntimeInstance<'db> {
RuntimeInstance::new(db, key)
}
#[salsa::tracked]
fn lower_runtime_body<'db>(
db: &'db dyn MirDb,
instance: RuntimeInstance<'db>,
) -> Result<LoweredRuntimeBody<'db>, LowerError> {
let body = match instance.key(db).source(db) {
RuntimeInstanceSource::Semantic(semantic) => {
if let Err(diag) = check_semantic_borrows(db, semantic) {
return Err(LowerError::Unsupported(format!(
"semantic borrow checking failed for {:?}: {}",
semantic.key(db),
diag.message
)));
}
if let Err(diag) = check_semantic_noesc(db, semantic) {
return Err(LowerError::Unsupported(format!(
"semantic noesc checking failed for {:?}: {}",
semantic.key(db),
diag.message
)));
}
lower_to_rmir(db, instance)?
}
RuntimeInstanceSource::Synthetic(synthetic) => {
lower_synthetic_runtime_body(db, instance, synthetic.spec(db).clone())
}
};
// Anchor lowering to the canonical class discipline: in debug builds,
// verify each body as it is produced so a divergence is attributed to
// the instance being lowered instead of surfacing later at package
// assembly. Release builds rely on the unconditional package-level
// verification.
#[cfg(debug_assertions)]
if let Err(failure) = crate::verify::verify_runtime_body_detailed(db, &db, &body) {
panic!(
"lowering produced an invalid runtime body for {:?}:\n{}",
instance.key(db).source(db),
crate::runtime::format_runtime_verify_failure(db, &body, &failure),
);
}
let direct_callees = collect_runtime_calls_lowered(&body);
let referenced_const_regions = collect_referenced_const_regions(&body);
let referenced_code_regions = collect_referenced_code_regions(&body);
Ok(LoweredRuntimeBody::new(
db,
body,
direct_callees,
referenced_const_regions,
referenced_code_regions,
))
}
pub(crate) fn runtime_instance_lowered_body<'db>(
db: &'db dyn MirDb,
instance: RuntimeInstance<'db>,
) -> Result<LoweredRuntimeBody<'db>, LowerError> {
lower_runtime_body(db, instance)
}
fn expect_lowered_runtime_body<'db>(
db: &'db dyn MirDb,
instance: RuntimeInstance<'db>,
) -> LoweredRuntimeBody<'db> {
lower_runtime_body(db, instance).unwrap_or_else(|err| {
panic!(
"runtime lowering failed for {:?}: {err}",
instance.key(db).source(db)
)
})
}