Skip to content

Commit 8bce3a7

Browse files
committed
Moved all interners to a common type.
1 parent dd34e4e commit 8bce3a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+930
-910
lines changed

cilly/src/bin/asmedit.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ use std::{
77
};
88

99
use cilly::{
10+
asm::{encoded_stats, Assembly},
11+
bimap::{Interned, IntoBiMapIndex},
1012
c_exporter::CExporter,
11-
BasicBlock, CILNode, CILRoot, MethodDef,
12-
{
13-
asm::{encoded_stats, Assembly},
14-
cillyir_exporter::CillyIRExpoter,
15-
il_exporter::ILExporter,
16-
Access, CILIter, MethodImpl, MethodRefIdx,
17-
},
13+
cillyir_exporter::CillyIRExpoter,
14+
il_exporter::ILExporter,
15+
Access, BasicBlock, CILIter, CILNode, CILRoot, MethodDef, MethodImpl, MethodRef,
1816
};
1917
use fxhash::FxHashSet;
2018

@@ -323,7 +321,7 @@ fn is_valid_c(asm: &Assembly, id: u32) -> bool {
323321
true
324322
}
325323
}
326-
fn misolate(asm: &mut Assembly, isolate_id: MethodRefIdx) {
324+
fn misolate(asm: &mut Assembly, isolate_id: Interned<MethodRef>) {
327325
let externs: Vec<_> = asm
328326
.methods_with(|_, _, def| *def.access() == Access::Extern)
329327
.map(|(id, _)| id)
@@ -360,9 +358,9 @@ fn misolate(asm: &mut Assembly, isolate_id: MethodRefIdx) {
360358
*asm = asm.clone().link(Assembly::default());
361359
asm.remove_dead_statics();
362360
}
363-
fn parse_id(id: &str, asm: &Assembly) -> MethodRefIdx {
361+
fn parse_id(id: &str, asm: &Assembly) -> Interned<MethodRef> {
364362
if let Ok(id) = id.parse::<u32>() {
365-
unsafe { MethodRefIdx::from_raw(NonZeroU32::new(id).unwrap()) }
363+
unsafe { Interned::from_index(NonZeroU32::new(id).unwrap()) }
366364
} else {
367365
let Some(mut iter) = asm.find_methods_matching(id) else {
368366
panic!("{id:?} is neithier a method name nor a method id")

cilly/src/cil_iter.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{cil_node::CILNode, cil_root::CILRoot, MethodRefIdx};
1+
use crate::{bimap::Interned, cil_node::CILNode, cil_root::CILRoot, MethodRef};
22

33
#[derive(Debug, Clone, Copy)]
44
pub enum CILIterElem<'a> {
@@ -491,12 +491,12 @@ impl<'a> CILIter<'a> {
491491
}
492492
}
493493
pub trait CILIterTrait<'a> {
494-
fn call_sites(self) -> impl Iterator<Item = MethodRefIdx>;
494+
fn call_sites(self) -> impl Iterator<Item = Interned<MethodRef>>;
495495
fn nodes(self) -> impl Iterator<Item = &'a CILNode>;
496496
fn roots(self) -> impl Iterator<Item = &'a CILRoot>;
497497
}
498498
impl<'a, T: Iterator<Item = CILIterElem<'a>>> CILIterTrait<'a> for T {
499-
fn call_sites(self) -> impl Iterator<Item = MethodRefIdx> {
499+
fn call_sites(self) -> impl Iterator<Item = Interned<MethodRef>> {
500500
self.filter_map(|node| match node {
501501
CILIterElem::Node(
502502
CILNode::Call(call_op_args)

cilly/src/cil_node.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1+
use crate::bimap::Interned;
12
use crate::cilnode::IsPure;
23
use crate::cilnode::MethodKind;
34
use crate::v2::method::LocalDef;
4-
use crate::TypeIdx;
5+
6+
use crate::FieldDesc;
57
use crate::{
68
call,
79
cil_root::CILRoot,
810
hashable::{HashableF32, HashableF64},
911
IString,
1012
};
11-
use crate::{
12-
Assembly, ClassRef, ClassRefIdx, FieldIdx, FnSig, Int, MethodRef, MethodRefIdx,
13-
StaticFieldDesc, Type,
14-
};
13+
use crate::{Assembly, ClassRef, FnSig, Int, MethodRef, StaticFieldDesc, Type};
1514
use serde::{Deserialize, Serialize};
1615
/// A container for the arguments of a call, callvirt, or newobj instruction.
1716
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Hash, Debug)]
1817
pub struct CallOpArgs {
1918
pub args: Box<[CILNode]>,
20-
pub site: MethodRefIdx,
19+
pub site: Interned<MethodRef>,
2120
pub is_pure: IsPure,
2221
}
2322

2423
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug, Hash)]
2524
pub enum CILNode {
2625
/// A translated V2 node.
27-
V2(crate::NodeIdx),
26+
V2(Interned<crate::v2::CILNode>),
2827
/// Loads the value of local variable number `n`.
2928
LDLoc(u32),
3029
/// Loads the value of argument number `n`.
@@ -108,13 +107,13 @@ pub enum CILNode {
108107
LDFieldAdress {
109108
/// Address of the object
110109
addr: Box<Self>,
111-
field: FieldIdx,
110+
field: Interned<FieldDesc>,
112111
},
113112
/// Loads the value of `field` of the object `addr` points to
114113
LDField {
115114
/// Address of the object
116115
addr: Box<Self>,
117-
field: FieldIdx,
116+
field: Interned<FieldDesc>,
118117
},
119118
/// Adds 2 values together
120119
Add(Box<Self>, Box<Self>),
@@ -162,12 +161,12 @@ pub enum CILNode {
162161
Gt(Box<Self>, Box<Self>),
163162
/// Compares two operands, returning true if lhs < rhs. Unsigned for intigers, unordered(in respect to NaNs) for floats.
164163
GtUn(Box<Self>, Box<Self>),
165-
TemporaryLocal(Box<(TypeIdx, Box<[CILRoot]>, Self)>),
164+
TemporaryLocal(Box<(Interned<Type>, Box<[CILRoot]>, Self)>),
166165

167166
SubTrees(Box<(Box<[CILRoot]>, Box<Self>)>),
168167
LoadAddresOfTMPLocal,
169168
LoadTMPLocal,
170-
LDFtn(MethodRefIdx),
169+
LDFtn(Interned<MethodRef>),
171170
LDTypeToken(Box<Type>),
172171
NewObj(Box<CallOpArgs>),
173172
// 24 bytes - too big!
@@ -215,18 +214,18 @@ pub enum CILNode {
215214
/// Gets the exception. Can only be used in handlers, only once per handler.
216215
GetException,
217216
/// Checks if `lhs` is of type `rhs`. If not, throws.
218-
CheckedCast(Box<(CILNode, ClassRefIdx)>),
217+
CheckedCast(Box<(CILNode, Interned<ClassRef>)>),
219218
// Checks if `lhs` is of type `rhs`. Returns a boolean.
220-
IsInst(Box<(CILNode, ClassRefIdx)>),
219+
IsInst(Box<(CILNode, Interned<ClassRef>)>),
221220
/// Marks the inner pointer operation as volatile.
222221
Volatile(Box<Self>),
223222
UnboxAny(Box<Self>, Box<Type>),
224223
AddressOfStaticField(Box<StaticFieldDesc>),
225-
LdNull(ClassRefIdx),
224+
LdNull(Interned<ClassRef>),
226225
}
227226

228227
impl CILNode {
229-
pub fn stack_addr(val: Self, tpe_idx: TypeIdx, _asm: &mut Assembly) -> Self {
228+
pub fn stack_addr(val: Self, tpe_idx: Interned<Type>, _asm: &mut Assembly) -> Self {
230229
CILNode::TemporaryLocal(Box::new((
231230
tpe_idx,
232231
[CILRoot::SetTMPLocal { value: val }].into(),
@@ -235,7 +234,7 @@ impl CILNode {
235234
}
236235
pub fn ovf_check_tuple(
237236
asm: &mut Assembly,
238-
tuple: ClassRefIdx,
237+
tuple: Interned<ClassRef>,
239238
out_of_range: Self,
240239
val: Self,
241240
tpe: Type,
@@ -270,7 +269,7 @@ impl CILNode {
270269
)))*/
271270
}
272271
pub fn create_slice(
273-
slice_tpe: ClassRefIdx,
272+
slice_tpe: Interned<ClassRef>,
274273
asm: &mut Assembly,
275274
metadata: Self,
276275
ptr: Self,
@@ -360,7 +359,8 @@ impl CILNode {
360359
.into(),
361360
site: (asm.alloc_methodref(select)),
362361
is_pure: crate::cilnode::IsPure::PURE,
363-
})).cast_ptr(tpe)
362+
}))
363+
.cast_ptr(tpe)
364364
}
365365
_ => todo!(),
366366
}
@@ -399,8 +399,8 @@ impl CILNode {
399399
old_val: Self,
400400
expected: Self,
401401
destination_addr: Self,
402-
val_desc: FieldIdx,
403-
flag_desc: FieldIdx,
402+
val_desc: Interned<FieldDesc>,
403+
flag_desc: Interned<FieldDesc>,
404404
) -> CILRoot {
405405
// Set the value of the result.
406406
let set_val = CILRoot::SetField {

cilly/src/cil_root.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
use crate::bimap::Interned;
12
use crate::cilnode::IsPure;
23
use crate::cilnode::MethodKind;
34
use crate::v2::method::LocalDef;
4-
use crate::TypeIdx;
5+
use crate::FieldDesc;
56
use crate::{
67
call,
78
cil_node::{CILNode, CallOpArgs},
89
AsmString, IString,
910
};
10-
use crate::{Assembly, ClassRef, FieldIdx, FnSig, MethodRef, MethodRefIdx, StaticFieldDesc, Type};
11+
use crate::{Assembly, ClassRef, FnSig, MethodRef, StaticFieldDesc, Type};
1112
use serde::{Deserialize, Serialize};
1213
#[derive(Clone, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
1314
pub enum CILRoot {
@@ -79,13 +80,13 @@ pub enum CILRoot {
7980
},
8081

8182
Call {
82-
site: MethodRefIdx,
83+
site: Interned<MethodRef>,
8384
args: Box<[CILNode]>,
8485
},
8586
SetField {
8687
addr: Box<CILNode>,
8788
value: Box<CILNode>,
88-
desc: FieldIdx,
89+
desc: Interned<FieldDesc>,
8990
},
9091
SetTMPLocal {
9192
value: CILNode,
@@ -121,7 +122,7 @@ pub enum CILRoot {
121122
count: Box<CILNode>,
122123
},
123124
CallVirt {
124-
site: MethodRefIdx,
125+
site: Interned<MethodRef>,
125126
args: Box<[CILNode]>,
126127
},
127128
Ret {
@@ -150,8 +151,8 @@ pub enum CILRoot {
150151
OptimizedSourceFileInfo(std::ops::Range<u64>, std::ops::Range<u64>, AsmString),
151152
/// Marks the inner pointer operation as volatile.
152153
Volatile(Box<Self>),
153-
InitObj(CILNode, TypeIdx),
154-
V2(crate::RootIdx),
154+
InitObj(CILNode, Interned<Type>),
155+
V2(Interned<crate::v2::CILRoot>),
155156
}
156157
pub type SFI = Box<(std::ops::Range<u64>, std::ops::Range<u64>, IString)>;
157158
impl CILRoot {
@@ -385,7 +386,7 @@ impl CILRoot {
385386
Self::SourceFileInfo(Box::new((line, column, file.to_owned().into())))
386387
}
387388
#[must_use]
388-
pub fn set_field(addr: CILNode, value: CILNode, desc: FieldIdx) -> Self {
389+
pub fn set_field(addr: CILNode, value: CILNode, desc: Interned<FieldDesc>) -> Self {
389390
Self::SetField {
390391
addr: Box::new(addr),
391392
value: Box::new(value),

cilly/src/entrypoint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
{cilnode::MethodKind, Assembly, Int, MethodRef},
66
};
77

8-
/// Creates a wrapper method around entypoint represented by `MethodRefIdx`
8+
/// Creates a wrapper method around entypoint represented by `Interned<MethodRef>`
99
pub fn wrapper(entrypoint: MethodRef, asm: &mut Assembly) -> MethodDefIdx {
1010
let uint8_ptr = asm.nptr(Type::Int(Int::U8));
1111
let uint8_ptr_idx = asm.alloc_type(uint8_ptr);

cilly/src/method.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ use std::{
77

88
use crate::{
99
basic_block::BasicBlock,
10+
bimap::Interned,
1011
cil_iter::{CILIterElem, CILIterTrait},
1112
cil_node::CILNode,
1213
cil_tree::CILTree,
13-
Access, IString, IntoAsmIndex, StringIdx, Type, TypeIdx,
14-
{cilnode::MethodKind, v2::method::LocalDef, Assembly, FnSig, MethodRef, MethodRefIdx},
14+
cilnode::MethodKind,
15+
v2::method::LocalDef,
16+
Access, Assembly, FnSig, IString, IntoAsmIndex, MethodRef, Type,
1517
};
1618

1719
/// Represenation of a CIL method.
@@ -24,7 +26,7 @@ pub struct Method {
2426
locals: Vec<LocalDef>,
2527
blocks: Vec<BasicBlock>,
2628
attributes: Vec<Attribute>,
27-
arg_names: Vec<Option<StringIdx>>,
29+
arg_names: Vec<Option<Interned<IString>>>,
2830
}
2931

3032
impl Eq for Method {}
@@ -80,7 +82,7 @@ impl Method {
8082
name: &str,
8183
mut locals: Vec<LocalDef>,
8284
blocks: Vec<BasicBlock>,
83-
mut arg_names: Vec<Option<StringIdx>>,
85+
mut arg_names: Vec<Option<Interned<IString>>>,
8486
asm: &mut Assembly,
8587
) -> Self {
8688
let mut used_names = FxHashSet::with_hasher(FxBuildHasher::default());
@@ -126,8 +128,8 @@ impl Method {
126128
/// Adds a local variable of type `local`
127129
pub fn add_local(
128130
&mut self,
129-
local: impl IntoAsmIndex<TypeIdx>,
130-
name: Option<impl IntoAsmIndex<StringIdx>>,
131+
local: impl IntoAsmIndex<Interned<Type>>,
132+
name: Option<impl IntoAsmIndex<Interned<IString>>>,
131133
asm: &mut Assembly,
132134
) -> usize {
133135
let loc = self.locals.len();
@@ -136,7 +138,7 @@ impl Method {
136138
loc
137139
}
138140
/// Extends local variables by `iter`.
139-
pub fn extend_locals<'a>(&mut self, iter: impl Iterator<Item = &'a TypeIdx>) {
141+
pub fn extend_locals<'a>(&mut self, iter: impl Iterator<Item = &'a Interned<Type>>) {
140142
self.locals.extend(iter.map(|tpe| (None, *tpe)));
141143
}
142144
/// Checks if the method `self` is the entrypoint.
@@ -181,20 +183,20 @@ impl Method {
181183
}
182184
/// Returns the list of local types.
183185
#[must_use]
184-
pub fn locals(&self) -> &[(Option<StringIdx>, TypeIdx)] {
186+
pub fn locals(&self) -> &[(Option<Interned<IString>>, Interned<Type>)] {
185187
&self.locals
186188
}
187189
/// Returns the list of external calls this function preforms. Calls may repeat.
188190
// TODO: make this not call `into_ops`
189-
pub fn calls(&self) -> impl Iterator<Item = MethodRefIdx> + '_ {
191+
pub fn calls(&self) -> impl Iterator<Item = Interned<MethodRef>> + '_ {
190192
self.blocks
191193
.iter()
192194
.flat_map(|block| block.iter_cil())
193195
.call_sites()
194196
}
195197

196198
/// Returns a call site that describes this method.
197-
pub fn call_site(&self, asm: &mut crate::Assembly) -> MethodRefIdx {
199+
pub fn call_site(&self, asm: &mut crate::Assembly) -> Interned<MethodRef> {
198200
let mref = MethodRef::new(
199201
*asm.main_module(),
200202
asm.alloc_string(self.name()),
@@ -222,7 +224,10 @@ impl Method {
222224
self.attributes.push(attr);
223225
}
224226
/// Sets the list of locals of self to `locals`.
225-
pub fn set_locals(&mut self, locals: impl Into<Vec<(Option<StringIdx>, TypeIdx)>>) {
227+
pub fn set_locals(
228+
&mut self,
229+
locals: impl Into<Vec<(Option<Interned<IString>>, Interned<Type>)>>,
230+
) {
226231
self.locals = locals.into();
227232
}
228233
/// Returns the type of this method(static, instance or virtual)
@@ -241,7 +246,7 @@ impl Method {
241246
}
242247

243248
#[must_use]
244-
pub fn arg_names(&self) -> &[Option<StringIdx>] {
249+
pub fn arg_names(&self) -> &[Option<Interned<IString>>] {
245250
&self.arg_names
246251
}
247252

@@ -256,8 +261,8 @@ impl Method {
256261
}
257262
pub fn alloc_local(
258263
&mut self,
259-
tpe: impl IntoAsmIndex<TypeIdx>,
260-
name: Option<impl IntoAsmIndex<StringIdx>>,
264+
tpe: impl IntoAsmIndex<Interned<Type>>,
265+
name: Option<impl IntoAsmIndex<Interned<IString>>>,
261266
asm: &mut Assembly,
262267
) -> usize {
263268
let new_loc = self.locals.len();

0 commit comments

Comments
 (0)