Skip to content

Commit 9c6545d

Browse files
authored
Merge pull request #57 from FractalFir/methodref
Merge changes to the method reference repr
2 parents ac20e7f + c0ed0a4 commit 9c6545d

Some content is hidden

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

47 files changed

+3742
-4524
lines changed

cilly/src/asm.rs

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ use serde::{Deserialize, Serialize};
77
use crate::{
88
access_modifier::AccessModifer,
99
basic_block::BasicBlock,
10-
call_site::CallSite,
1110
cil_root::CILRoot,
1211
method::{Method, MethodType},
13-
v2::{ClassDef, FnSig, Int},
12+
v2::{cilnode::MethodKind, ClassDef, FnSig, Int, MethodRef, MethodRefIdx},
1413
IString, Type,
1514
};
1615

@@ -36,9 +35,9 @@ pub type ExternFnDef = (IString, FnSig, bool);
3635
/// Representation of a .NET assembly.
3736
pub struct Assembly {
3837
/// List of functions defined within this assembly.
39-
functions: FxHashMap<CallSite, Method>,
40-
/// Callsite representing the entrypoint of this assebmly if any present.
41-
entrypoint: Option<CallSite>,
38+
functions: FxHashMap<MethodRefIdx, Method>,
39+
/// MethodRefIdx representing the entrypoint of this assebmly if any present.
40+
entrypoint: Option<MethodRefIdx>,
4241
/// List of references to external assemblies
4342
extern_refs: FxHashMap<IString, AssemblyExternRef>,
4443
extern_fns: FxHashMap<ExternFnDef, IString>,
@@ -100,33 +99,34 @@ impl Assembly {
10099

101100
/// Addds a per-thread static initailzer
102101
pub fn add_tcctor(&mut self) -> &mut Method {
103-
self.functions
104-
.entry(CallSite::new(
105-
None,
106-
".tcctor".into(),
102+
let mref = MethodRef::new(
103+
*self.main_module(),
104+
self.alloc_string(".tcctor"),
105+
self.alloc_sig(FnSig::new(Box::new([]), Type::Void)),
106+
MethodKind::Static,
107+
vec![].into(),
108+
);
109+
let mref = self.alloc_methodref(mref);
110+
self.functions.entry(mref).or_insert_with(|| {
111+
Method::new(
112+
AccessModifer::Extern,
113+
MethodType::Static,
107114
FnSig::new(Box::new([]), Type::Void),
108-
true,
109-
))
110-
.or_insert_with(|| {
111-
Method::new(
112-
AccessModifer::Extern,
113-
MethodType::Static,
114-
FnSig::new(Box::new([]), Type::Void),
115-
".tcctor",
116-
vec![
117-
(None, self.inner.nptr(Type::Int(Int::U8))),
118-
(None, self.inner.nptr(Type::Int(Int::U8))),
119-
],
120-
vec![BasicBlock::new(vec![CILRoot::VoidRet.into()], 0, None)],
121-
vec![],
122-
)
123-
})
115+
".tcctor",
116+
vec![
117+
(None, self.inner.nptr(Type::Int(Int::U8))),
118+
(None, self.inner.nptr(Type::Int(Int::U8))),
119+
],
120+
vec![BasicBlock::new(vec![CILRoot::VoidRet.into()], 0, None)],
121+
vec![],
122+
)
123+
})
124124
}
125125

126126
/// Returns true if assembly contains function named `name`
127127
#[must_use]
128-
pub fn contains_fn(&self, site: &CallSite) -> bool {
129-
self.functions.contains_key(site)
128+
pub fn contains_fn(&self, site: MethodRefIdx) -> bool {
129+
self.functions.contains_key(&site)
130130
}
131131
/// Adds a method to the assebmly.
132132
pub fn add_method(&mut self, method: Method) {
@@ -144,11 +144,11 @@ impl Assembly {
144144
}
145145
}
146146

147-
/// Sets the entrypoint of the assembly to the method behind `CallSite`.
148-
pub fn set_entrypoint(&mut self, entrypoint: &CallSite) {
147+
/// Sets the entrypoint of the assembly to the method behind `MethodRefIdx`.
148+
pub fn set_entrypoint(&mut self, entrypoint: MethodRefIdx) {
149149
assert!(self.entrypoint.is_none(), "ERROR: Multiple entrypoints");
150-
let wrapper = crate::entrypoint::wrapper(entrypoint, self.inner_mut());
151-
self.entrypoint = Some(wrapper.call_site());
150+
let wrapper = crate::entrypoint::wrapper(self[entrypoint].clone(), self.inner_mut());
151+
self.entrypoint = Some(wrapper.call_site(self));
152152
self.add_method(wrapper);
153153
}
154154

@@ -161,15 +161,18 @@ impl Assembly {
161161
self.initializers.push(root);
162162
}
163163
pub fn cctor_mut(&mut self) -> Option<&mut Method> {
164-
self.functions.get_mut(&CallSite::new(
165-
None,
166-
".cctor".into(),
167-
FnSig::new(Box::new([]), Type::Void),
168-
true,
169-
))
170-
}
171-
172-
pub(crate) fn functions(&self) -> &FxHashMap<CallSite, Method> {
164+
let mref = MethodRef::new(
165+
*self.main_module(),
166+
self.alloc_string(".cctor"),
167+
self.sig([], Type::Void),
168+
MethodKind::Static,
169+
vec![].into(),
170+
);
171+
let mref = self.alloc_methodref(mref);
172+
self.functions.get_mut(&mref)
173+
}
174+
175+
pub(crate) fn functions(&self) -> &FxHashMap<MethodRefIdx, Method> {
173176
&self.functions
174177
}
175178

cilly/src/bin/interpreter/main.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {}
55
use std::io::Write;
66
use cilly::{
77
asm::Assembly,
8-
call_site::CallSite,
8+
call_site::MethodRefIdx,
99
cil_node::CILNode,
1010
cil_root::{CILRoot, SFI},
1111
method::Method,
@@ -17,21 +17,21 @@ use fxhash::{FxBuildHasher, FxHashMap};
1717
use value::Value;
1818
#[derive(Debug)]
1919
enum Exception {
20-
MethodNotFound(CallSite),
20+
MethodNotFound(MethodRefIdx),
2121
LocalOutOfRange { loc: usize, lcount: usize },
2222
ArgOutOfRange { arg: usize, lcount: usize },
2323
AllocOffsetOutOfRange,
2424
}
2525
type AllocID = u32;
2626
struct InterpreterState<'asm> {
2727
asm: &'asm Assembly,
28-
call_stack: Vec<(&'asm CallSite, usize, usize, cilly::cil_root::SFI)>,
28+
call_stack: Vec<(&'asm MethodRefIdx, usize, usize, cilly::cil_root::SFI)>,
2929
locals: Vec<Box<[Value]>>,
3030
mem: FxHashMap<AllocID, Box<[u8]>>,
3131
last_alloc: AllocID,
3232
fields: FxHashMap<StaticFieldDesc, Value>,
33-
methods: FxHashMap<AllocID, CallSite>,
34-
inv_methods: FxHashMap<CallSite, AllocID>,
33+
methods: FxHashMap<AllocID, MethodRefIdx>,
34+
inv_methods: FxHashMap<MethodRefIdx, AllocID>,
3535
last_alloc_method: AllocID,
3636
}
3737
@@ -341,7 +341,7 @@ fn eval_node<'asm>(
341341
}
342342
}
343343
impl<'asm> InterpreterState<'asm> {
344-
pub fn get_fn_ptr_alloc(&mut self, site: &CallSite) -> AllocID {
344+
pub fn get_fn_ptr_alloc(&mut self, site: &MethodRefIdx) -> AllocID {
345345
*self.inv_methods.entry(site.clone()).or_insert_with(|| {
346346
let new_method = self.last_alloc_method;
347347
self.methods.insert(new_method, site.clone());
@@ -357,7 +357,7 @@ impl<'asm> InterpreterState<'asm> {
357357
}
358358
pub fn try_call_extern(
359359
&mut self,
360-
call: &'asm CallSite,
360+
call: &'asm MethodRefIdx,
361361
args: &mut Box<[Value]>,
362362
string_map: &AsmStringContainer,
363363
) -> Result<Value, Exception> {
@@ -412,7 +412,7 @@ impl<'asm> InterpreterState<'asm> {
412412
pub fn run_cctor(&mut self) -> Result<Value, Exception> {
413413
match self.asm.cctor() {
414414
Some(_) => self.run(
415-
Box::<CallSite>::leak(Box::new(CallSite::builtin(
415+
Box::<MethodRefIdx>::leak(Box::new(MethodRefIdx::builtin(
416416
".cctor".into(),
417417
FnSig::new(&[], Type::Void),
418418
true,
@@ -426,7 +426,7 @@ impl<'asm> InterpreterState<'asm> {
426426
let entry = self.asm.methods().find(|method| method.is_entrypoint());
427427
match entry {
428428
Some(entry) => self.run(
429-
Box::<CallSite>::leak(Box::new(entry.call_site())),
429+
Box::<MethodRefIdx>::leak(Box::new(entry.call_site())),
430430
&mut vec![Value::StringArray(
431431
std::env::args().map(|arg| arg.into()).collect(),
432432
)]
@@ -435,15 +435,15 @@ impl<'asm> InterpreterState<'asm> {
435435
None => Ok(Value::Undef),
436436
}
437437
}
438-
pub fn method(&self, site: &'asm CallSite) -> Result<&'asm Method, Exception> {
438+
pub fn method(&self, site: &'asm MethodRefIdx) -> Result<&'asm Method, Exception> {
439439
self.asm
440440
.functions()
441441
.get(site)
442442
.ok_or(Exception::MethodNotFound(site.clone()))
443443
}
444444
pub fn run(
445445
&mut self,
446-
call: &'asm CallSite,
446+
call: &'asm MethodRefIdx,
447447
args: &mut Box<[Value]>,
448448
) -> Result<Value, Exception> {
449449
assert_eq!(self.locals.len(), self.call_stack.len());

cilly/src/bin/linker/main.rs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#![allow(clippy::module_name_repetitions)]
33
use cilly::{
44
asm::DEAD_CODE_ELIMINATION,
5-
call_site::CallSite,
65
conv_usize,
76
libc_fns::{self, LIBC_FNS, LIBC_MODIFIES_ERRNO},
87
v2::{
@@ -11,6 +10,7 @@ use cilly::{
1110
Assembly, BasicBlock, CILNode, CILRoot, ClassDef, ClassRef, Const, FnSig, IlasmFlavour,
1211
Int, MethodImpl, Type,
1312
},
13+
MethodRef,
1414
};
1515
//use assembly::Assembly;
1616
use lazy_static::lazy_static;
@@ -258,6 +258,18 @@ fn main() {
258258
final_assembly.alloc_string("_Unwind_RaiseException"),
259259
Box::new(|_, asm| {
260260
let rust_exception = asm.alloc_string("RustException");
261+
let exception_class =
262+
asm.alloc_class_ref(ClassRef::new(rust_exception, None, false, [].into()));
263+
let exception_ctor = MethodRef::new(
264+
(asm.alloc_class_ref(ClassRef::new(rust_exception, None, false, [].into()))),
265+
asm.alloc_string(".ctor"),
266+
asm.sig(
267+
([Type::ClassRef(exception_class), Type::Int(Int::USize)]),
268+
Type::Void,
269+
),
270+
MethodKind::Constructor,
271+
vec![].into(),
272+
);
261273
MethodImpl::MethodBody {
262274
blocks: vec![cilly::v2::BasicBlock::from_v1(
263275
&cilly::basic_block::BasicBlock::new(
@@ -267,30 +279,7 @@ fn main() {
267279
args: Box::new([conv_usize!(
268280
cilly::cil_node::CILNode::LDArg(0)
269281
)]),
270-
site: Box::new(CallSite::new(
271-
Some(asm.alloc_class_ref(ClassRef::new(
272-
rust_exception,
273-
None,
274-
false,
275-
[].into(),
276-
))),
277-
".ctor".into(),
278-
FnSig::new(
279-
Box::new([
280-
Type::ClassRef(asm.alloc_class_ref(
281-
ClassRef::new(
282-
rust_exception,
283-
None,
284-
false,
285-
[].into(),
286-
),
287-
)),
288-
Type::Int(Int::USize),
289-
]),
290-
Type::Void,
291-
),
292-
false,
293-
)),
282+
site: asm.alloc_methodref(exception_ctor),
294283
},
295284
)),
296285
)
@@ -608,7 +597,7 @@ fn override_errno(asm: &mut Assembly) {
608597
vec![BasicBlock::new(
609598
vec![CILRoot::Ret {
610599
tree: cilly::call!(
611-
CallSite::new(
600+
MethodRefIdx::new(
612601
Some(ClassRef::marshal()),
613602
"GetLastWin32Error".into(),
614603
FnSig::new(&[], Type::Int(Int::I32)),

0 commit comments

Comments
 (0)