This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
hir_to_rustspec.rs
467 lines (456 loc) · 19.9 KB
/
hir_to_rustspec.rs
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use im::HashMap;
use rustc_ast::ast::{IntTy, UintTy};
use rustc_hir::{definitions::DefPathData, AssocItemKind, ItemKind};
use rustc_metadata::creader::CStore;
use rustc_middle::mir::interpret::{ConstValue, Scalar};
use rustc_middle::mir::terminator::Mutability;
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::{self, ConstKind, PolyFnSig, RegionKind, TyCtxt, TyKind};
use rustc_session::Session;
use rustc_span::{
def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE},
DUMMY_SP,
};
use std::sync::atomic::Ordering;
use crate::rustspec::*;
use crate::typechecker::{FnKey, ID_COUNTER};
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
enum ParamType {
ImplParam,
FnParam,
}
type TypVarContext = HashMap<(ParamType, usize), BaseTyp>;
fn fresh_type_var(
rust_id: usize,
p: ParamType,
typ_ctx: &TypVarContext,
) -> (BaseTyp, TypVarContext) {
let t = BaseTyp::Variable(HacspecId(ID_COUNTER.fetch_add(1, Ordering::SeqCst)));
(t.clone(), typ_ctx.update((p, rust_id), t))
}
fn translate_base_typ(
tcx: &TyCtxt,
ty: &ty::Ty,
typ_ctx: &TypVarContext,
) -> Result<(BaseTyp, TypVarContext), ()> {
match ty.kind() {
TyKind::Bool => Ok((BaseTyp::Bool, typ_ctx.clone())),
TyKind::Int(IntTy::Isize) => Ok((BaseTyp::Isize, typ_ctx.clone())),
TyKind::Int(IntTy::I8) => Ok((BaseTyp::Int8, typ_ctx.clone())),
TyKind::Int(IntTy::I16) => Ok((BaseTyp::Int16, typ_ctx.clone())),
TyKind::Int(IntTy::I32) => Ok((BaseTyp::Int32, typ_ctx.clone())),
TyKind::Int(IntTy::I64) => Ok((BaseTyp::Int64, typ_ctx.clone())),
TyKind::Int(IntTy::I128) => Ok((BaseTyp::Int128, typ_ctx.clone())),
TyKind::Uint(UintTy::Usize) => Ok((BaseTyp::Usize, typ_ctx.clone())),
TyKind::Uint(UintTy::U8) => Ok((BaseTyp::UInt8, typ_ctx.clone())),
TyKind::Uint(UintTy::U16) => Ok((BaseTyp::UInt16, typ_ctx.clone())),
TyKind::Uint(UintTy::U32) => Ok((BaseTyp::UInt32, typ_ctx.clone())),
TyKind::Uint(UintTy::U64) => Ok((BaseTyp::UInt64, typ_ctx.clone())),
TyKind::Uint(UintTy::U128) => Ok((BaseTyp::UInt128, typ_ctx.clone())),
TyKind::Ref(region, inner_ty, mutability) => match region {
RegionKind::ReStatic => match mutability {
Mutability::Not => match inner_ty.kind() {
TyKind::Str => Ok((BaseTyp::Str, typ_ctx.clone())),
_ => Err(()),
},
_ => Err(()),
},
_ => Err(()),
},
TyKind::Adt(adt, substs) => {
let adt_id = adt.did;
let adt_def_path = tcx.def_path(adt_id);
// We're looking at types from imported crates that can only be imported
// with * blobs so the types have to be re-exported from inner modules,
// which is why we only consider the last path segment (there should not
// be ambiguities)
match adt_def_path.data.last().unwrap().data {
DefPathData::TypeNs(name) => match tcx
.original_crate_name(adt_def_path.krate)
.to_ident_string()
.as_str()
{
"hacspec_lib" => match name.to_ident_string().as_str() {
"Seq" | "PublicSeq" | "SecretSeq" => {
let (param_typ, typ_ctx) = if substs.len() == 1 {
match substs.first().unwrap().unpack() {
GenericArgKind::Type(arg_ty) => {
match translate_base_typ(tcx, &arg_ty, typ_ctx) {
Ok((t, typ_ctx)) => (t, typ_ctx),
Err(()) => return Err(()),
}
}
_ => return Err(()),
}
} else {
return Err(());
};
Ok((BaseTyp::Seq(Box::new((param_typ, DUMMY_SP))), typ_ctx))
}
// We accept all named types from hacspec_lib because of the predefined
// array types like U32Word, etc.
_ => Ok((
BaseTyp::Named(
(Ident::Original(name.to_ident_string()), DUMMY_SP),
None,
),
typ_ctx.clone(),
)),
},
"core" => match name.to_ident_string().as_str() {
"Range" => {
let (param_typ, typ_ctx) = if substs.len() == 1 {
match substs.first().unwrap().unpack() {
GenericArgKind::Type(arg_ty) => {
match translate_base_typ(tcx, &arg_ty, typ_ctx) {
Ok((t, typ_ctx)) => (t, typ_ctx),
Err(()) => return Err(()),
}
}
_ => return Err(()),
}
} else {
return Err(());
};
Ok((
BaseTyp::Tuple(vec![
(param_typ.clone(), DUMMY_SP),
(param_typ, DUMMY_SP),
]),
typ_ctx,
))
}
_ => Err(()),
},
_ => Ok((
BaseTyp::Named((Ident::Original(name.to_ident_string()), DUMMY_SP), None),
typ_ctx.clone(),
)),
},
_ => Err(()),
}
}
TyKind::Param(p) => match typ_ctx.get(&(ParamType::ImplParam, p.index as usize)) {
None => {
let (id_typ, typ_ctx) =
fresh_type_var(p.index as usize, ParamType::ImplParam, typ_ctx);
Ok((id_typ, typ_ctx))
}
Some(id_typ) => Ok((id_typ.clone(), typ_ctx.clone())),
},
TyKind::Bound(rust_id, _) => match typ_ctx.get(&(ParamType::FnParam, rust_id.index())) {
None => {
let (id_typ, typ_ctx) =
fresh_type_var(rust_id.index(), ParamType::FnParam, typ_ctx);
Ok((id_typ, typ_ctx))
}
Some(id_typ) => Ok((id_typ.clone(), typ_ctx.clone())),
},
TyKind::Tuple(args) => {
let mut new_args = Vec::new();
let typ_ctx = args.types().fold(Ok(typ_ctx.clone()), |typ_ctx, ty| {
let (new_ty, typ_ctx) = translate_base_typ(tcx, &ty, &typ_ctx?)?;
new_args.push((new_ty, DUMMY_SP));
Ok(typ_ctx)
})?;
Ok((BaseTyp::Tuple(new_args), typ_ctx))
}
_ => Err(()),
}
}
fn translate_ty(
tcx: &TyCtxt,
ty: &ty::Ty,
typ_ctx: &TypVarContext,
) -> Result<(Typ, TypVarContext), ()> {
match ty.kind() {
TyKind::Ref(_, ref_ty, Mutability::Not) => {
let (ty, typ_ctx) = translate_base_typ(tcx, &ref_ty, typ_ctx)?;
Ok((((Borrowing::Borrowed, DUMMY_SP), (ty, DUMMY_SP)), typ_ctx))
}
_ => {
let (ty, typ_ctx) = translate_base_typ(tcx, ty, typ_ctx)?;
Ok((((Borrowing::Consumed, DUMMY_SP), (ty, DUMMY_SP)), typ_ctx))
}
}
}
fn translate_polyfnsig(
tcx: &TyCtxt,
sig: &PolyFnSig,
typ_ctx: &TypVarContext,
) -> Result<ExternalFuncSig, ()> {
// The type context maps De Bruijn indexed types in the signature
// to Hacspec type variables
let mut new_args = Vec::new();
let typ_ctx = sig
.inputs()
.skip_binder()
.iter()
.fold(Ok(typ_ctx.clone()), |typ_ctx, ty| {
let (new_ty, typ_ctx) = translate_ty(tcx, ty, &typ_ctx?)?;
new_args.push(new_ty);
Ok(typ_ctx)
})?;
let (ret, _) = translate_base_typ(tcx, &sig.output().skip_binder(), &typ_ctx)?;
Ok(ExternalFuncSig {
args: new_args,
ret,
})
}
fn insert_extern_func(
extern_funcs: &mut HashMap<FnKey, Result<ExternalFuncSig, String>>,
fn_key: FnKey,
sig: Result<ExternalFuncSig, String>,
) {
// Here we can deal with function name clashes
// When two functions have the same name, we can only keep one of them
// If one of the two is not in hacspec then we keep the other
// If the two are in hacspec then we decide in an ad hoc way
match extern_funcs.get(&fn_key) {
None => {
extern_funcs.insert(fn_key, sig);
}
Some(old_sig) => match (old_sig, &sig) {
(Ok(_), Err(_)) => (),
_ => {
extern_funcs.insert(fn_key, sig);
}
},
}
}
fn process_fn_id(
_sess: &Session,
tcx: &TyCtxt,
id: &DefId,
krate_num: &CrateNum,
extern_funcs: &mut HashMap<FnKey, Result<ExternalFuncSig, String>>,
) {
match tcx.type_of(*id).kind() {
TyKind::FnDef(_, _) => {
let def_path = tcx.def_path(*id);
if def_path.krate == *krate_num {
if def_path.data.len() == 1
|| (match def_path.data[def_path.data.len() - 2].data {
DefPathData::Impl | DefPathData::ImplTrait => false,
_ => true,
})
{
// Function not within impl block
let export_sig = tcx.fn_sig(*id);
let sig = match translate_polyfnsig(tcx, &export_sig, &HashMap::new()) {
Ok(sig) => Ok(sig),
Err(()) => Err(format!("{}", export_sig)),
};
let name_segment = def_path.data.last().unwrap();
match name_segment.data {
DefPathData::ValueNs(name) => {
let fn_key =
FnKey::Independent(Ident::Original(name.to_ident_string()));
insert_extern_func(extern_funcs, fn_key, sig);
}
_ => (),
}
} else {
// Function inside an impl block
let impl_segment = def_path.data[def_path.data.len() - 2];
let name_segment = def_path.data.last().unwrap();
match (impl_segment.data, name_segment.data) {
(DefPathData::Impl, DefPathData::ValueNs(name)) => {
let impl_id = tcx.impl_of_method(*id).unwrap();
let impl_type = &tcx.type_of(impl_id);
let impl_type = translate_base_typ(tcx, impl_type, &HashMap::new());
// TODO: distinguish between methods and static for types
match impl_type {
Ok((impl_type, typ_ctx)) => {
let fn_key = FnKey::Impl(
impl_type,
Ident::Original(name.to_ident_string()),
);
let export_sig = tcx.fn_sig(*id);
let sig = match translate_polyfnsig(tcx, &export_sig, &typ_ctx)
{
Ok(sig) => Ok(sig),
Err(()) => Err(format!("{}", export_sig)),
};
insert_extern_func(extern_funcs, fn_key, sig);
}
Err(()) => (),
}
}
_ => (),
}
};
}
}
_ => (),
};
}
fn add_array_type_from_ctor_sig(
tcx: &TyCtxt,
sig: &PolyFnSig,
external_arrays: &mut HashMap<String, BaseTyp>,
) {
let ret = sig.output().skip_binder();
match ret.kind() {
TyKind::Adt(adt, substs) => {
if substs.len() > 0 {
return ();
}
if adt.variants.len() != 1 {
return ();
}
for variant in adt.variants.iter() {
if variant.fields.len() != 1 {
return ();
}
for field in variant.fields.iter() {
match tcx.type_of(field.did).kind() {
TyKind::Array(cell_t, size) => {
let (new_cell_t, _) =
match translate_base_typ(tcx, cell_t, &HashMap::new()) {
Ok(x) => x,
Err(()) => return (),
};
let new_size = match &size.val {
ConstKind::Value(value) => match value {
ConstValue::Scalar(scalar) => match scalar {
Scalar::Int(s) => s.to_bits(s.size()).unwrap() as usize,
_ => return (),
},
_ => return (),
},
_ => return (),
};
let array_typ = BaseTyp::Array(
(ArraySize::Integer(new_size), DUMMY_SP),
Box::new((new_cell_t, DUMMY_SP)),
);
let array_name =
tcx.def_path(adt.did).data.last().unwrap().data.to_string();
external_arrays.insert(array_name, array_typ);
}
_ => return (),
}
}
}
()
}
_ => (),
}
}
pub fn retrieve_external_functions(
sess: &Session,
tcx: &TyCtxt,
imported_crates: &Vec<Spanned<String>>,
) -> (
HashMap<FnKey, Result<ExternalFuncSig, String>>,
HashMap<String, BaseTyp>,
) {
let mut krates: Vec<_> = tcx.crates().iter().collect();
krates.push(&LOCAL_CRATE);
let mut extern_funcs = HashMap::new();
let mut extern_arrays = HashMap::new();
let crate_store = tcx.cstore_as_any().downcast_ref::<CStore>().unwrap();
let mut imported_crates = imported_crates.clone();
// You normally only import hacspec_lib which then reexports the definitions
// from abstract_integers and secret_integers. But we do have to fetch those
// reexported definitions here and thus need to examine the original crates
// containing them
imported_crates.push(("core".to_string(), DUMMY_SP));
imported_crates.push(("abstract_integers".to_string(), DUMMY_SP));
imported_crates.push(("secret_integers".to_string(), DUMMY_SP));
for krate_num in krates {
let original_crate_name = tcx.original_crate_name(*krate_num);
if imported_crates
.iter()
.filter(|(imported_crate, _)| {
*imported_crate == original_crate_name.to_ident_string()
|| original_crate_name.to_ident_string()
== tcx.crate_name(LOCAL_CRATE).to_ident_string()
})
.collect::<Vec<_>>()
.len()
> 0
{
if *krate_num != LOCAL_CRATE {
let num_def_ids = crate_store.num_def_ids(*krate_num);
let def_ids = (0..num_def_ids).into_iter().map(|id| DefId {
krate: *krate_num,
index: DefIndex::from_usize(id),
});
for def_id in def_ids {
let def_path = tcx.def_path(def_id);
match &def_path.data.last() {
Some(x) => {
// We only import things really defined in the crate
if tcx.original_crate_name(def_path.krate).to_ident_string()
== original_crate_name.to_ident_string()
{
match x.data {
DefPathData::ValueNs(_) => process_fn_id(
sess,
tcx,
&def_id,
krate_num,
&mut extern_funcs,
),
DefPathData::Ctor => {
// Some weird constructor inside std crashes so we disable them for std
if &original_crate_name.to_ident_string() != "core" {
let export_sig = tcx.fn_sig(def_id);
let sig = match translate_polyfnsig(
tcx,
&export_sig,
&HashMap::new(),
) {
Ok(sig) => Ok(sig),
Err(()) => Err(format!("{}", export_sig)),
};
add_array_type_from_ctor_sig(
tcx,
&export_sig,
&mut extern_arrays,
);
let name_segment =
def_path.data[def_path.data.len() - 2];
match name_segment.data {
DefPathData::TypeNs(name) => {
let fn_key = FnKey::Independent(
Ident::Original(name.to_ident_string()),
);
extern_funcs.insert(fn_key, sig);
}
_ => (),
}
}
}
_ => (),
}
}
}
_ => (),
}
}
}
}
}
let items = &tcx.hir_crate(LOCAL_CRATE).items;
for (item_id, item) in items {
let item_id = tcx.hir().local_def_id(*item_id).to_def_id();
match item.kind {
ItemKind::Fn(_, _, _) => {
process_fn_id(sess, tcx, &item_id, &LOCAL_CRATE, &mut extern_funcs)
}
ItemKind::Impl { items, .. } => {
for item in items {
let item_id = tcx.hir().local_def_id(item.id.hir_id).to_def_id();
if let AssocItemKind::Fn { .. } = item.kind {
process_fn_id(sess, tcx, &item_id, &LOCAL_CRATE, &mut extern_funcs)
}
}
}
_ => (),
}
}
(extern_funcs, extern_arrays)
}