Skip to content

Commit dd34e4e

Browse files
committed
Ported place handling to v2 nodes
1 parent 7d201ae commit dd34e4e

File tree

20 files changed

+233
-309
lines changed

20 files changed

+233
-309
lines changed

cilly/src/v2/asm.rs

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,63 @@ impl Index<FieldIdx> for Assembly {
117117
}
118118
}
119119
impl Assembly {
120+
/// Offsets `addr` by `index` * sizeof(`tpe`)
121+
pub fn offset(
122+
&mut self,
123+
addr: impl IntoAsmIndex<NodeIdx>,
124+
index: impl IntoAsmIndex<NodeIdx>,
125+
tpe: impl IntoAsmIndex<TypeIdx>,
126+
) -> NodeIdx {
127+
let index = index.into_idx(self);
128+
let stride = self.size_of(tpe);
129+
let stride = self.int_cast(stride, Int::USize, ExtendKind::ZeroExtend);
130+
let offset = self.biop(index, stride, BinOp::Mul);
131+
self.biop(addr, offset, BinOp::Add)
132+
}
133+
/// Dereferences `addr`, loading data of type `tpe`
134+
pub fn load(
135+
&mut self,
136+
addr: impl IntoAsmIndex<NodeIdx>,
137+
tpe: impl IntoAsmIndex<TypeIdx>,
138+
) -> NodeIdx {
139+
let addr = addr.into_idx(self);
140+
let tpe = tpe.into_idx(self);
141+
self.alloc_node(CILNode::LdInd {
142+
addr,
143+
tpe,
144+
volatile: false,
145+
})
146+
}
147+
/// Gets the field of a valuetype / pointer `addr`.
148+
pub fn ld_field(
149+
&mut self,
150+
addr: impl IntoAsmIndex<NodeIdx>,
151+
field: impl IntoAsmIndex<FieldIdx>,
152+
) -> NodeIdx {
153+
let addr = addr.into_idx(self);
154+
let field = field.into_idx(self);
155+
self.alloc_node(CILNode::LdField { addr, field })
156+
}
157+
/// Casts a pointer / usize / isize (`addr`) to a pointer to `tpe`.
158+
pub fn cast_ptr(
159+
&mut self,
160+
addr: impl IntoAsmIndex<NodeIdx>,
161+
tpe: impl IntoAsmIndex<TypeIdx>,
162+
) -> NodeIdx {
163+
let addr = addr.into_idx(self);
164+
let tpe = tpe.into_idx(self);
165+
self.alloc_node(CILNode::PtrCast(addr, Box::new(PtrCastRes::Ptr(tpe))))
166+
}
167+
/// Gets the addres of a field of a pointer to valuetype `addr`.
168+
pub fn ld_field_addr(
169+
&mut self,
170+
addr: impl IntoAsmIndex<NodeIdx>,
171+
field: impl IntoAsmIndex<FieldIdx>,
172+
) -> NodeIdx {
173+
let addr = addr.into_idx(self);
174+
let field = field.into_idx(self);
175+
self.alloc_node(CILNode::LdFieldAdress { addr, field })
176+
}
120177
pub fn typecheck(&mut self) {
121178
let method_def_idxs: Box<[_]> = self.method_defs.keys().copied().collect();
122179
for method in method_def_idxs {
@@ -276,20 +333,20 @@ impl Assembly {
276333
pub fn get_root(&self, root: RootIdx) -> &CILRoot {
277334
self.roots.get(root)
278335
}
279-
pub fn size_of(&mut self, tpe: impl IntoAsmIndex<TypeIdx>) -> CILNode {
336+
pub fn size_of(&mut self, tpe: impl IntoAsmIndex<TypeIdx>) -> NodeIdx {
280337
let idx = tpe.into_idx(self);
281338
assert_ne!(self[idx], Type::Void);
282-
CILNode::SizeOf(idx)
339+
self.alloc_node(CILNode::SizeOf(idx))
283340
}
284341
pub fn biop(
285342
&mut self,
286343
lhs: impl IntoAsmIndex<NodeIdx>,
287344
rhs: impl IntoAsmIndex<NodeIdx>,
288345
op: BinOp,
289-
) -> CILNode {
346+
) -> NodeIdx {
290347
let lhs = lhs.into_idx(self);
291348
let rhs = rhs.into_idx(self);
292-
CILNode::BinOp(lhs, rhs, op)
349+
self.alloc_node(CILNode::BinOp(lhs, rhs, op))
293350
}
294351
pub fn unop(&mut self, val: impl Into<CILNode>, op: UnOp) -> CILNode {
295352
let val = self.nodes.alloc(val.into());
@@ -967,7 +1024,7 @@ impl Assembly {
9671024
self.new_method(mref.into_def(implementation, Access::Private, self));
9681025
continue;
9691026
}
970-
1027+
9711028
// Check if this method is in the extern list
9721029
if let Some(lib) = externs.get(&mref.name()) {
9731030
let arg_names = (0..(self[mref.sig()].inputs().len()))
@@ -995,7 +1052,7 @@ impl Assembly {
9951052
continue;
9961053
}
9971054
// Create a replacement method.
998-
1055+
9991056
let arg_names = (0..(self[mref.sig()].inputs().len()))
10001057
.map(|_| None)
10011058
.collect();

cilly/src/v2/builtins/f16/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ fn op_direct(
1313
let name = asm.alloc_string(format!("{op}_{lhs}", op = op.name(), lhs = lhs.name()));
1414
let generator = move |_, asm: &mut Assembly| {
1515
let op = asm.biop(CILNode::LdArg(0), CILNode::LdArg(1), op);
16-
let op = asm.alloc_node(op);
1716
let ret = asm.alloc_root(CILRoot::Ret(op));
1817
MethodImpl::MethodBody {
1918
blocks: vec![BasicBlock::new(vec![ret], 0, None)],

cilly/src/v2/builtins/int128/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ fn op_direct(
1313
let name = asm.alloc_string(format!("{op}_{lhs}", op = op.name(), lhs = lhs.name()));
1414
let generator = move |_, asm: &mut Assembly| {
1515
let op = asm.biop(CILNode::LdArg(0), CILNode::LdArg(1), op);
16-
let op = asm.alloc_node(op);
1716
let ret = asm.alloc_root(CILRoot::Ret(op));
1817
MethodImpl::MethodBody {
1918
blocks: vec![BasicBlock::new(vec![ret], 0, None)],

cilly/src/v2/builtins/thread.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ fn insert_pthread_attr_setstacksize(asm: &mut Assembly, patcher: &mut MissingMet
6464
let usize_tpe = asm.alloc_type(Int::USize);
6565
let usize_size = asm.alloc_node(CILNode::SizeOf(usize_tpe));
6666
let offset = asm.alloc_node(CILNode::BinOp(usize_size, three, BinOp::Mul));
67-
6867
let addr = asm.biop(
6968
CILNode::IntCast {
7069
input: offset,
@@ -74,7 +73,6 @@ fn insert_pthread_attr_setstacksize(asm: &mut Assembly, patcher: &mut MissingMet
7473
CILNode::LdArg(0),
7574
BinOp::Add,
7675
);
77-
let addr = asm.alloc_node(addr);
7876
let init = asm.alloc_root(CILRoot::StInd(Box::new((
7977
addr,
8078
ldarg_1,

cilly/src/v2/c_exporter/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -638,11 +638,20 @@ impl CExporter {
638638
volatile,
639639
} => {
640640
if volatile {
641-
format!(
642-
"*(volatile {tpe}*)({addr})",
643-
tpe = c_tpe(asm[tpe], asm),
644-
addr = Self::node_to_string(asm[addr].clone(), asm, locals, inputs, sig)?
645-
)
641+
if matches!(asm[tpe], Type::Ptr(_) | Type::Ref(_)){
642+
format!(
643+
"(({tpe})*(volatile size_t*)({addr}))",
644+
tpe = c_tpe(asm[tpe], asm),
645+
addr = Self::node_to_string(asm[addr].clone(), asm, locals, inputs, sig)?
646+
)
647+
}
648+
else{
649+
format!(
650+
"*(volatile {tpe}*)({addr})",
651+
tpe = c_tpe(asm[tpe], asm),
652+
addr = Self::node_to_string(asm[addr].clone(), asm, locals, inputs, sig)?
653+
)
654+
}
646655
} else {
647656
format!(
648657
"*({addr})",

cilly/src/v2/field.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use serde::{Deserialize, Serialize};
22

33
use super::bimap::BiMapIndex;
4-
use super::Int;
54
use super::{bimap::IntoBiMapIndex, ClassRefIdx, StringIdx, Type};
5+
use super::{Int, IntoAsmIndex};
66

77
#[derive(Hash, PartialEq, Eq, Clone, Debug, Copy, Serialize, Deserialize)]
88
pub struct FieldIdx(BiMapIndex);
@@ -100,3 +100,8 @@ impl StaticFieldDesc {
100100
self.tpe
101101
}
102102
}
103+
impl IntoAsmIndex<FieldIdx> for FieldDesc {
104+
fn into_idx(self, asm: &mut super::Assembly) -> FieldIdx {
105+
asm.alloc_field(self)
106+
}
107+
}

cilly/src/v2/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ impl<'this, T: Iterator<Item = CILIterElem> + 'this> TpeIter<'this> for T {
554554
pub fn nodes() {
555555
use super::{BinOp, Const};
556556
let mut asm = Assembly::default();
557-
let mut add = asm.biop(Const::I8(2), Const::I8(1), BinOp::Add);
557+
let mut add = asm[asm.biop(Const::I8(2), Const::I8(1), BinOp::Add)].clone();
558558
let mut iter = CILIterMut::new(&mut add, &mut asm);
559559
assert!(matches!(
560560
iter.next(),

cilly/src/v2/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn types() {
6363
pub fn nodes() {
6464
let mut asm = Assembly::default();
6565
let add = asm.biop(Const::I8(2), Const::I8(1), BinOp::Add);
66-
let mut iter = CILIter::new(add, &asm);
66+
let mut iter = CILIter::new(asm[add].clone(), &asm);
6767
assert!(matches!(
6868
iter.next(),
6969
Some(CILIterElem::Node(CILNode::BinOp(_, _, BinOp::Add)))

cilly/src/v2/opt/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ impl MethodDef {
803803
*curr = asm.alloc_root(CILRoot::Ret(tree));
804804
}
805805
// FIXME: **why** does this cause issues?
806-
/*
806+
/*
807807
(CILRoot::Branch(info), CILRoot::Branch(info2))
808808
if is_branch_unconditional(info2) =>
809809
{
@@ -835,7 +835,7 @@ impl MethodDef {
835835
if self.implementation().blocks().is_none() {
836836
return;
837837
}
838-
838+
839839
// TODO: this is a hack, which makes root inlining optimizations not consume fuel.
840840
let fuel = std::sync::Mutex::new(&mut *fuel);
841841
let locals = self.locals().map(|locs| locs.to_vec()).unwrap();
@@ -973,7 +973,6 @@ fn local_prop() {
973973
let stloc_0 = asm.alloc_root(CILRoot::StLoc(0, arg0));
974974
let loc0 = CILNode::LdLoc(0);
975975
let sum = asm.biop(loc0.clone(), loc0, BinOp::Add);
976-
let sum = asm.alloc_node(sum);
977976
let ret = asm.alloc_root(CILRoot::Ret(sum));
978977
let mut block = BasicBlock::new(vec![stloc_0, ret], 0, None);
979978
let mut cache = SideEffectInfoCache::default();
@@ -985,7 +984,6 @@ fn local_prop() {
985984
assert!(iter.next().is_some());
986985
let opt_ret = iter.next().unwrap();
987986
let sum_arg0 = asm.biop(CILNode::LdArg(0), CILNode::LdArg(0), BinOp::Add);
988-
let sum_arg0 = asm.alloc_node(sum_arg0);
989987
let ret = asm.alloc_root(CILRoot::Ret(sum_arg0));
990988
assert_eq!(ret, *opt_ret);
991989
}
@@ -997,7 +995,6 @@ fn remove_nops() {
997995
let stloc_0 = asm.alloc_root(CILRoot::StLoc(0, arg0));
998996
let loc0 = CILNode::LdLoc(0);
999997
let sum = asm.biop(loc0.clone(), loc0, BinOp::Add);
1000-
let sum = asm.alloc_node(sum);
1001998
let ret = asm.alloc_root(CILRoot::Ret(sum));
1002999
let nop = asm.alloc_root(CILRoot::Nop);
10031000
let block = BasicBlock::new(vec![nop, stloc_0, nop, ret, nop], 0, None);

cilly/src/v2/opt/side_effect.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,10 @@ fn const_no_side_effect() {
9090
let node = asm.alloc_node(cst);
9191
assert!(!cache.has_side_effects(node, &asm));
9292
let node = asm.biop(cst, cst, crate::BinOp::Add);
93-
let node = asm.alloc_node(node);
9493
assert!(!cache.has_side_effects(node, &asm));
9594
let node = asm.biop(CILNode::LocAlloc { size: node }, cst, crate::BinOp::Add);
96-
let node = asm.alloc_node(node);
9795
assert!(cache.has_side_effects(node, &asm));
9896
let node = asm.biop(cst, CILNode::LocAlloc { size: node }, crate::BinOp::Add);
99-
let node = asm.alloc_node(node);
10097
assert!(cache.has_side_effects(node, &asm));
10198
}
10299
}

0 commit comments

Comments
 (0)