Skip to content

Commit cb70ae8

Browse files
committed
Started reimplementing projection. Struct fields can now, once again, be read.
1 parent b4e656f commit cb70ae8

19 files changed

+659
-319
lines changed

src/assembly.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,25 @@ use serde::{Deserialize, Serialize};
1212
pub struct Assembly {
1313
types: HashSet<TypeDef>,
1414
functions: HashSet<Method>,
15-
entrypoint:Option<CallSite>,
15+
entrypoint: Option<CallSite>,
1616
}
1717
impl Assembly {
1818
pub fn empty() -> Self {
1919
Self {
2020
types: HashSet::new(),
2121
functions: HashSet::new(),
22-
entrypoint:None,
22+
entrypoint: None,
2323
}
2424
}
2525
pub fn join(self, other: Self) -> Self {
2626
let types = self.types.union(&other.types).cloned().collect();
2727
let functions = self.functions.union(&other.functions).cloned().collect();
2828
let entrypoint = self.entrypoint.or(other.entrypoint);
29-
Self { types, functions,entrypoint }
29+
Self {
30+
types,
31+
functions,
32+
entrypoint,
33+
}
3034
}
3135
pub fn add_fn<'tcx>(
3236
&mut self,
@@ -74,7 +78,7 @@ impl Assembly {
7478
Ok(())
7579
//todo!("Can't add function")
7680
}
77-
pub fn add_method(&mut self,method:Method){
81+
pub fn add_method(&mut self, method: Method) {
7882
self.functions.insert(method);
7983
}
8084
pub fn methods(&self) -> impl Iterator<Item = &Method> {
@@ -83,12 +87,12 @@ impl Assembly {
8387
pub fn types(&self) -> impl Iterator<Item = &TypeDef> {
8488
(&self.types).iter()
8589
}
86-
pub fn add_type<'ctx>(&mut self,ty:rustc_middle::ty::Ty<'ctx>,tyctx: TyCtxt<'ctx>){
87-
for type_def in TypeDef::from_ty(ty,tyctx){
90+
pub fn add_type<'ctx>(&mut self, ty: rustc_middle::ty::Ty<'ctx>, tyctx: TyCtxt<'ctx>) {
91+
for type_def in TypeDef::from_ty(ty, tyctx) {
8892
self.types.insert(type_def);
8993
}
9094
}
91-
pub fn add_typedef<'ctx>(&mut self,type_def:TypeDef){
95+
pub fn add_typedef<'ctx>(&mut self, type_def: TypeDef) {
9296
self.types.insert(type_def);
9397
}
9498
pub fn add_item<'tcx>(
@@ -105,9 +109,10 @@ impl Assembly {
105109
_ => todo!("Unsupported item:\"{item:?}\"!"),
106110
}
107111
}
108-
pub fn set_entrypoint(&mut self,entrypoint:CallSite){
109-
assert!(self.entrypoint.is_none(),"ERROR: Multiple entrypoints");
110-
self.functions.insert(crate::entrypoint::wrapper(&entrypoint));
112+
pub fn set_entrypoint(&mut self, entrypoint: CallSite) {
113+
assert!(self.entrypoint.is_none(), "ERROR: Multiple entrypoints");
114+
self.functions
115+
.insert(crate::entrypoint::wrapper(&entrypoint));
111116
self.entrypoint = Some(entrypoint);
112117
}
113118
}

src/assembly_exporter/ilasm_exporter.rs

Lines changed: 101 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl AssemblyExporter for ILASMExporter {
2828
Self { encoded_asm }
2929
}
3030
fn add_type(&mut self, tpe: &TypeDef) {
31-
type_def_cli(&mut self.encoded_asm,tpe).expect("Error");
31+
type_def_cli(&mut self.encoded_asm, tpe).expect("Error");
3232
//let _ = self.types.push(tpe.clone());
3333
}
3434
fn add_method(&mut self, method: &Method) {
@@ -84,33 +84,36 @@ impl AssemblyExporter for ILASMExporter {
8484
}
8585
}
8686

87-
fn type_def_cli(mut w: impl Write,tpe: &TypeDef) -> Result<(), super::AssemblyExportError> {
87+
fn type_def_cli(mut w: impl Write, tpe: &TypeDef) -> Result<(), super::AssemblyExportError> {
8888
let name = tpe.name();
8989
let mut generics = String::new();
90-
if tpe.gargc() != 0{
90+
if tpe.gargc() != 0 {
9191
generics.push('<');
9292
}
93-
for i in 0..tpe.gargc(){
94-
if i != 0{
93+
for i in 0..tpe.gargc() {
94+
if i != 0 {
9595
generics.push(',');
9696
}
9797
generics.push_str(&format!("G{i}"));
9898
}
99-
if tpe.gargc() != 0{
99+
if tpe.gargc() != 0 {
100100
generics.push('>');
101101
}
102-
let extended = if let Some(extended) = tpe.extends(){
102+
let extended = if let Some(extended) = tpe.extends() {
103103
todo!("Can't handle inheretence yet!");
104-
}
105-
else{
104+
} else {
106105
"[System.Runtime]System.ValueType"
107106
};
108-
writeln!(w,"\n.class {name}{generics} extends {extended}{{");
107+
writeln!(w, "\n.class {name}{generics} extends {extended}{{");
109108
let mut field_string = String::new();
110-
for (field_name,field_type) in tpe.fields().iter(){
111-
writeln!(w,"\t.field {field_type_name} {field_name}",field_type_name = prefixed_type_cli(field_type));
109+
for (field_name, field_type) in tpe.fields().iter() {
110+
writeln!(
111+
w,
112+
"\t.field {field_type_name} {field_name}",
113+
field_type_name = prefixed_type_cli(field_type)
114+
);
112115
}
113-
writeln!(w,"}}");
116+
writeln!(w, "}}");
114117
Ok(())
115118
}
116119
fn absolute_path(path: &std::path::Path) -> std::io::Result<std::path::PathBuf> {
@@ -133,16 +136,24 @@ fn method_cil(mut w: impl Write, method: &Method) -> std::io::Result<()> {
133136
write!(w, ".method {access} static hidebysig {output} {name}")?;
134137
args_cli(&mut w, method.sig().inputs())?;
135138
writeln!(w, "{{")?;
136-
if method.is_entrypoint(){
137-
writeln!(w,".entrypoint")?;
139+
if method.is_entrypoint() {
140+
writeln!(w, ".entrypoint")?;
138141
}
139142
writeln!(w, "\t.locals (")?;
140143
let mut locals_iter = method.locals().iter().enumerate();
141-
if let Some((local_id, local) ) = locals_iter.next(){
142-
write!(w, "\t\t[{local_id}] {escaped_type}", escaped_type = arg_type_cli(local))?;
144+
if let Some((local_id, local)) = locals_iter.next() {
145+
write!(
146+
w,
147+
"\t\t[{local_id}] {escaped_type}",
148+
escaped_type = arg_type_cli(local)
149+
)?;
143150
}
144-
for (local_id, local) in locals_iter{
145-
write!(w, ",\n\t\t[{local_id}] {escaped_type}", escaped_type = arg_type_cli(local))?;
151+
for (local_id, local) in locals_iter {
152+
write!(
153+
w,
154+
",\n\t\t[{local_id}] {escaped_type}",
155+
escaped_type = arg_type_cli(local)
156+
)?;
146157
}
147158
writeln!(w, "\n\t)")?;
148159
for op in method.get_ops() {
@@ -289,95 +300,95 @@ fn op_cli(op: &crate::cil_op::CILOp) -> Cow<'static, str> {
289300
//Debug
290301
CILOp::Comment(comment) => format!("//{comment}").into(),
291302
//Convertions
292-
CILOp::ConvISize(checked) =>
293-
if *checked{
303+
CILOp::ConvISize(checked) => {
304+
if *checked {
294305
"conv.ovf.i".into()
295-
}
296-
else{
306+
} else {
297307
"conv.i".into()
298308
}
299-
CILOp::ConvI8(checked) =>
300-
if *checked{
309+
}
310+
CILOp::ConvI8(checked) => {
311+
if *checked {
301312
"conv.ovf.i1".into()
302-
}
303-
else{
313+
} else {
304314
"conv.i1".into()
305-
}
306-
CILOp::ConvI16(checked) =>
307-
if *checked{
308-
"conv.ovf.i2".into()
309315
}
310-
else{
316+
}
317+
CILOp::ConvI16(checked) => {
318+
if *checked {
319+
"conv.ovf.i2".into()
320+
} else {
311321
"conv.i2".into()
312-
}
313-
CILOp::ConvI32(checked) =>
314-
if *checked{
315-
"conv.ovf.i4".into()
316322
}
317-
else{
323+
}
324+
CILOp::ConvI32(checked) => {
325+
if *checked {
326+
"conv.ovf.i4".into()
327+
} else {
318328
"conv.i4".into()
319-
}
320-
CILOp::ConvI64(checked) =>
321-
if *checked{
322-
"conv.ovf.i8".into()
323329
}
324-
else{
330+
}
331+
CILOp::ConvI64(checked) => {
332+
if *checked {
333+
"conv.ovf.i8".into()
334+
} else {
325335
"conv.i8".into()
326-
}
327-
CILOp::ConvUSize(checked) =>
328-
if *checked{
329-
"conv.ovf.u".into()
330336
}
331-
else{
337+
}
338+
CILOp::ConvUSize(checked) => {
339+
if *checked {
340+
"conv.ovf.u".into()
341+
} else {
332342
"conv.u".into()
333343
}
334-
CILOp::ConvU8(checked) =>
335-
if *checked{
344+
}
345+
CILOp::ConvU8(checked) => {
346+
if *checked {
336347
"conv.ovf.u1".into()
337-
}
338-
else{
348+
} else {
339349
"conv.u1".into()
340-
}
341-
CILOp::ConvU16(checked) =>
342-
if *checked{
343-
"conv.ovf.u2".into()
344350
}
345-
else{
351+
}
352+
CILOp::ConvU16(checked) => {
353+
if *checked {
354+
"conv.ovf.u2".into()
355+
} else {
346356
"conv.u2".into()
347-
}
348-
CILOp::ConvU32(checked) =>
349-
if *checked{
350-
"conv.ovf.u4".into()
351357
}
352-
else{
358+
}
359+
CILOp::ConvU32(checked) => {
360+
if *checked {
361+
"conv.ovf.u4".into()
362+
} else {
353363
"conv.u4".into()
354-
}
355-
CILOp::ConvU64(checked) =>
356-
if *checked{
357-
"conv.ovf.u8".into()
358364
}
359-
else{
365+
}
366+
CILOp::ConvU64(checked) => {
367+
if *checked {
368+
"conv.ovf.u8".into()
369+
} else {
360370
"conv.u8".into()
361-
}
362-
CILOp::ConvF32(checked) =>
363-
if *checked{
364-
"conv.ovf.r4".into()
365371
}
366-
else{
372+
}
373+
CILOp::ConvF32(checked) => {
374+
if *checked {
375+
"conv.ovf.r4".into()
376+
} else {
367377
"conv.r4".into()
368-
}
369-
CILOp::ConvU64(checked) =>
370-
if *checked{
371-
"conv.ovf.r8".into()
372378
}
373-
else{
374-
"conv.r8".into()
375-
}
379+
}
376380
// Pointer stuff
377381
CILOp::LDIndI8 => "ldind.i1".into(),
378-
CILOp::Pop=>"pop".into(),
379-
CILOp::Throw=>"throw".into(),
380-
CILOp::LdStr(str)=>format!("ldstr {str:?}").into(),
382+
CILOp::Pop => "pop".into(),
383+
CILOp::Throw => "throw".into(),
384+
CILOp::LdStr(str) => format!("ldstr {str:?}").into(),
385+
CILOp::LDField(descr) => format!(
386+
"ldfld {prefixed_type} {owner}::{field_name}",
387+
prefixed_type = prefixed_type_cli(descr.tpe()),
388+
owner = dotnet_type_ref_cli(descr.owner()),
389+
field_name = descr.name()
390+
)
391+
.into(),
381392
CILOp::NewObj(call_site) => {
382393
if call_site.is_nop() {
383394
"".into()
@@ -460,7 +471,7 @@ fn type_cli(tpe: &Type) -> Cow<'static, str> {
460471
Type::Unresolved => "Unresolved".into(),
461472
Type::Bool => "bool".into(),
462473
Type::DotnetChar => "char".into(),
463-
Type::GenericArg(idx)=>format!("G{idx}").into(),
474+
Type::GenericArg(idx) => format!("!G{idx}").into(),
464475
Type::Foreign => "valuetype Foreign".into(),
465476
//_ => todo!("Unsuported type {tpe:?}"),
466477
}
@@ -483,13 +494,15 @@ fn prefixed_type_cli(tpe: &Type) -> Cow<'static, str> {
483494
Type::ISize => "native int".into(),
484495
Type::USize => "native uint".into(),
485496
Type::Ptr(inner) => format!("{inner}*", inner = prefixed_type_cli(inner)).into(),
486-
Type::DotnetType(dotnet_type) => format!("valuetype {}",dotnet_type_ref_cli(dotnet_type)).into(),
497+
Type::DotnetType(dotnet_type) => {
498+
format!("valuetype {}", dotnet_type_ref_cli(dotnet_type)).into()
499+
}
487500
//Special type
488501
Type::Unresolved => "valuetype Unresolved".into(),
489502
Type::Foreign => "valuetype Foreign".into(),
490503
Type::Bool => "bool".into(),
491504
Type::DotnetChar => "char".into(),
492-
Type::GenericArg(idx)=>format!("G{idx}").into(),
505+
Type::GenericArg(idx) => format!("!G{idx}").into(),
493506
//_ => todo!("Unsuported type {tpe:?}"),
494507
}
495508
}
@@ -512,7 +525,10 @@ fn generics_str(generics: &[Type]) -> Cow<'static, str> {
512525
let mut garg_string = String::new();
513526
let mut generic_iter = generics.iter();
514527
if let Some(first_generic) = generic_iter.next() {
515-
garg_string.push_str(&format!("{type_cli}", type_cli = prefixed_type_cli(&first_generic)));
528+
garg_string.push_str(&format!(
529+
"{type_cli}",
530+
type_cli = prefixed_type_cli(&first_generic)
531+
));
516532
}
517533
for arg in generic_iter {
518534
garg_string.push_str(&format!(",{type_cli}", type_cli = prefixed_type_cli(&arg)));

0 commit comments

Comments
 (0)