making the Call node less special

return value is now a separate node pinned to the call

Signed-off-by: Jakub Doka <jakub.doka2@gmail.com>
This commit is contained in:
Jakub Doka 2024-12-19 10:48:05 +01:00
parent f05c61a99e
commit a8aba7e7c2
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
17 changed files with 155 additions and 136 deletions

View file

@ -434,12 +434,15 @@ impl HbvmBackend {
self.emit(instrs::st(r, atr(*node.inputs.last().unwrap()), 0, size));
}
match ret {
Some(PLoc::WideReg(..)) => {}
Some(PLoc::Reg(..)) if node.ty.loc(tys) == Loc::Stack => {}
Some(PLoc::Reg(r, ..)) => self.emit_cp(atr(nid), r),
None | Some(PLoc::Ref(..)) => {}
//match ret {
// Some(PLoc::WideReg(..)) => {}
// Some(PLoc::Reg(..)) if node.ty.loc(tys) == Loc::Stack => {}
// Some(PLoc::Reg(r, ..)) => self.emit_cp(atr(nid), r),
// None | Some(PLoc::Ref(..)) => {}
//}
}
Kind::RetVal => {
self.emit_cp(atr(nid), reg::RET);
}
Kind::Global { global } => {
let reloc = Reloc::new(self.code.len(), 3, 4);
@ -696,6 +699,7 @@ impl<'a> FunctionBuilder<'a> {
| Kind::Global { .. }
| Kind::Load { .. }
| Kind::Stre
| Kind::RetVal
| Kind::Stck => self.add_instr(nid),
Kind::End | Kind::Phi | Kind::Arg | Kind::Mem | Kind::Loops | Kind::Join => {}
Kind::Assert { .. } => unreachable!(),

View file

@ -169,14 +169,10 @@ impl Nodes {
let mut deepest = self[node].inputs[0];
for &inp in self[node].inputs[1..].iter() {
if self.idepth(inp, Some(scheds)) > self.idepth(deepest, Some(scheds)) {
if self[inp].kind.is_call() {
deepest = inp;
} else {
debug_assert!(!self.is_cfg(inp));
deepest = self.idom(inp, Some(scheds));
}
}
}
scheds[node as usize] = deepest;
}
@ -199,8 +195,8 @@ impl Nodes {
for &node in rpo.iter().rev() {
self.loop_depth(node, Some(scheds));
for i in 0..self[node].inputs.len() {
self.push_up_impl(self[node].inputs[i], visited, scheds);
for &i in self[node].inputs.iter() {
self.push_up_impl(i, visited, scheds);
}
if matches!(self[node].kind, Kind::Loop | Kind::Region) {
@ -217,13 +213,13 @@ impl Nodes {
self.iter()
.map(|(n, _)| n)
.filter(|&n| !visited.get(n)
&& !matches!(self[n].kind, Kind::Arg | Kind::Mem | Kind::Loops))
&& !matches!(self[n].kind, Kind::Arg | Kind::Mem | Kind::Loops | Kind::RetVal))
.collect::<Vec<_>>(),
vec![],
"{:?}",
self.iter()
.filter(|&(n, nod)| !visited.get(n)
&& !matches!(nod.kind, Kind::Arg | Kind::Mem | Kind::Loops))
&& !matches!(nod.kind, Kind::Arg | Kind::Mem | Kind::Loops | Kind::RetVal))
.collect::<Vec<_>>()
);
@ -288,6 +284,7 @@ impl Nodes {
let cfg_idx = outputs.iter().position(|&n| self.is_cfg(n)).unwrap();
outputs.swap(cfg_idx, 0);
for &o in outputs.iter() {
if (!self.is_cfg(o)
&& self[o].outputs.iter().any(|&oi| {
@ -1566,6 +1563,7 @@ impl Nodes {
K::Start => {}
_ if self.is_cfg(target) && self.idom(target, None) == NEVER => panic!(),
K::Entry
| K::RetVal
| K::Mem
| K::Loops
| K::End
@ -1728,6 +1726,7 @@ impl Nodes {
Kind::Mem => write!(out, " mem: "),
Kind::Loops => write!(out, "loops: "),
Kind::Join => write!(out, "join: "),
Kind::RetVal => write!(out, "rval: "),
}?;
if self[node].kind != Kind::Loop && self[node].kind != Kind::Region {
@ -2051,8 +2050,7 @@ impl Node {
}
pub fn has_no_value(&self) -> bool {
(self.kind.is_cfg() && (!self.kind.is_call() || self.ty == ty::Id::VOID))
|| matches!(self.kind, Kind::Stre)
self.kind.is_cfg() || matches!(self.kind, Kind::Stre)
}
}
@ -2087,6 +2085,12 @@ pub enum Kind {
Return {
file: ty::Module,
},
// [ctrl, ...args]
Call {
unreachable: bool,
func: ty::Func,
args: ty::Tuple,
},
// [ctrl]
Die,
// [ctrl]
@ -2108,12 +2112,7 @@ pub enum Kind {
Global {
global: ty::Global,
},
// [ctrl, ...args]
Call {
unreachable: bool,
func: ty::Func,
args: ty::Tuple,
},
RetVal,
// [ctrl, cond, value]
Assert {
kind: AssertKind,
@ -2139,7 +2138,9 @@ impl Kind {
}
fn is_pinned(&self) -> bool {
self.is_cfg() || self.is_at_start() || matches!(self, Self::Phi | Kind::Assert { .. })
self.is_cfg()
|| self.is_at_start()
|| matches!(self, Self::Phi | Self::Assert { .. } | Self::RetVal)
}
fn is_at_start(&self) -> bool {

View file

@ -1520,7 +1520,14 @@ impl<'a> Codegen<'a> {
None
}
Expr::Directive { pos, name: "eca", args } => {
inference!(ty, ctx, self, pos, "return type", "@as(<return_ty>, @eca(<expr>...))");
inference!(
ret_ty,
ctx,
self,
pos,
"return type",
"@as(<return_ty>, @eca(<expr>...))"
);
let mut inps = Vc::from([NEVER]);
let arg_base = self.tys.tmp.args.len();
@ -1542,19 +1549,19 @@ impl<'a> Codegen<'a> {
self.append_clobbers(&mut inps, &mut clobbered_aliases);
let alt_value = match ty.loc(self.tys) {
let alt_value = match ret_ty.loc(self.tys) {
Loc::Reg => None,
Loc::Stack => {
let stck = self.new_stack(pos, ty);
let stck = self.new_stack(pos, ret_ty);
inps.push(stck);
Some(Value::ptr(stck).ty(ty))
Some(Value::ptr(stck).ty(ret_ty))
}
};
inps[0] = self.ci.ctrl.get();
self.ci.ctrl.set(
self.ci.nodes.new_node_nop(
ty,
ret_ty,
Kind::Call { func: ty::Func::ECA, args, unreachable: false },
inps,
),
@ -1563,7 +1570,11 @@ impl<'a> Codegen<'a> {
self.add_clobber_stores(clobbered_aliases);
alt_value.or(Some(Value::new(self.ci.ctrl.get()).ty(ty)))
Some(alt_value.unwrap_or_else(|| {
let ret =
self.ci.nodes.new_node_nop(ret_ty, Kind::RetVal, [self.ci.ctrl.get()]);
Value::new(ret).ty(ret_ty)
}))
}
Expr::Call { func, args, .. } => self.gen_call(func, args, false),
Expr::Directive { name: "inline", args: [func, args @ ..], .. } => {
@ -2713,7 +2724,10 @@ impl<'a> Codegen<'a> {
return None;
}
alt_value.or(Some(Value::new(self.ci.ctrl.get()).ty(sig.ret)))
Some(alt_value.unwrap_or_else(|| {
let ret = self.ci.nodes.new_node_nop(sig.ret, Kind::RetVal, [self.ci.ctrl.get()]);
Value::new(ret).ty(sig.ret)
}))
}
}

View file

@ -5,8 +5,8 @@ main:
ADDI64 r254, r254, -24d
ST r31, r254, 0a, 24h
JAL r31, r0, :cond
CP r33, r1
CP r32, r0
CP r33, r1
JNE r33, r32, :0
JMP :1
0: LI64 r32, 2d

View file

@ -37,41 +37,41 @@ main:
ST r31, r254, 0a, 40h
CP r2, r0
JAL r31, r0, :multiple_breaks
CP r32, r1
LI64 r33, 3d
JEQ r32, r33, :0
LI64 r32, 3d
CP r33, r1
JEQ r33, r32, :0
LI64 r32, 1d
CP r1, r32
JMP :1
0: LI64 r32, 4d
CP r2, r32
0: LI64 r33, 4d
CP r2, r33
JAL r31, r0, :multiple_breaks
CP r34, r1
LI64 r35, 10d
JEQ r34, r35, :2
LI64 r34, 10d
CP r35, r1
JEQ r35, r34, :2
LI64 r32, 2d
CP r1, r32
JMP :1
2: CP r2, r0
JAL r31, r0, :state_change_in_break
CP r34, r1
JEQ r34, r0, :3
CP r1, r33
JMP :1
3: CP r2, r32
JAL r31, r0, :state_change_in_break
CP r34, r1
JEQ r34, r35, :4
CP r35, r1
JEQ r35, r0, :3
CP r1, r32
JMP :1
4: CP r2, r35
3: CP r2, r33
JAL r31, r0, :state_change_in_break
CP r35, r1
JEQ r35, r34, :4
CP r1, r33
JMP :1
4: CP r2, r34
JAL r31, r0, :continue_and_state_change
CP r32, r1
JEQ r32, r35, :5
CP r33, r1
JEQ r33, r34, :5
LI64 r32, 5d
CP r1, r32
JMP :1
5: CP r2, r33
5: CP r2, r32
JAL r31, r0, :continue_and_state_change
CP r32, r1
JEQ r32, r0, :6

View file

@ -42,10 +42,11 @@ free:
CP r4, r14
CP r5, r15
ECA
CP r13, r1
JALA r0, r31, 0a
main:
ADDI64 r254, r254, -96d
ST r31, r254, 48a, 48h
ADDI64 r254, r254, -88d
ST r31, r254, 48a, 40h
ADDI64 r32, r254, 24d
CP r1, r32
JAL r31, r0, :new
@ -60,20 +61,19 @@ main:
CP r2, r33
CP r3, r34
JAL r31, r0, :push
CP r34, r1
LD r35, r254, 0a, 8h
LD r35, r35, 0a, 1h
LD r36, r254, 24a, 8h
LD r34, r36, 0a, 8h
LD r34, r254, 0a, 8h
LD r34, r34, 0a, 1h
LD r35, r254, 24a, 8h
LD r35, r35, 0a, 8h
CP r2, r33
JAL r31, r0, :deinit
CP r2, r32
JAL r31, r0, :deinit
ANDI r32, r35, 255d
ADD64 r32, r34, r32
ANDI r32, r34, 255d
ADD64 r32, r35, r32
CP r1, r32
LD r31, r254, 48a, 48h
ADDI64 r254, r254, 96d
LD r31, r254, 48a, 40h
ADDI64 r254, r254, 88d
JALA r0, r31, 0a
malloc:
CP r13, r2
@ -125,8 +125,8 @@ push:
2: CP r2, r32
CP r3, r37
JAL r31, r0, :malloc
CP r35, r1
ST r32, r38, 16a, 8h
CP r35, r1
JNE r35, r0, :3
CP r1, r0
JMP :4
@ -179,8 +179,8 @@ push:
CP r2, r34
CP r3, r40
JAL r31, r0, :malloc
CP r35, r1
ST r32, r38, 16a, 8h
CP r35, r1
JNE r35, r0, :3
CP r1, r0
JMP :4

View file

@ -2,8 +2,8 @@ inb:
CP r1, r0
JALA r0, r31, 0a
main:
ADDI64 r254, r254, -32d
ST r31, r254, 0a, 32h
ADDI64 r254, r254, -24d
ST r31, r254, 0a, 24h
LRA r32, r0, :ports
LD r33, r32, 0a, 1h
ANDI r33, r33, 255d
@ -11,12 +11,12 @@ main:
JMP :1
0: JAL r31, r0, :inb
CP r33, r1
CMPU r34, r33, r0
CMPUI r34, r34, 0d
NOT r34, r34
ST r34, r32, 0a, 1h
1: LD r31, r254, 0a, 32h
ADDI64 r254, r254, 32d
CMPU r33, r33, r0
CMPUI r33, r33, 0d
NOT r33, r33
ST r33, r32, 0a, 1h
1: LD r31, r254, 0a, 24h
ADDI64 r254, r254, 24d
JALA r0, r31, 0a
code size: 164
ret: 0

View file

@ -6,9 +6,9 @@ integer_range:
CP r2, r16
CP r3, r15
ECA
CP r15, r1
SUB64 r14, r14, r13
ADDI64 r14, r14, 1d
CP r15, r1
DIRU64 r0, r14, r15, r14
ADD64 r13, r14, r13
CP r1, r13

View file

@ -10,26 +10,26 @@ decide:
ADDI64 r254, r254, 24d
JALA r0, r31, 0a
main:
ADDI64 r254, r254, -120d
ST r31, r254, 72a, 48h
ADDI64 r254, r254, -104d
ST r31, r254, 72a, 32h
ADDI64 r32, r254, 48d
CP r1, r32
CP r2, r0
JAL r31, r0, :decide
ADDI64 r34, r254, 24d
BMC r32, r34, 24h
LI64 r35, 1d
CP r1, r34
CP r2, r35
ADDI64 r33, r254, 24d
BMC r32, r33, 24h
LI64 r34, 1d
CP r1, r33
CP r2, r34
JAL r31, r0, :decide
ADDI64 r36, r254, 0d
BMC r32, r36, 24h
ADDI64 r34, r254, 0d
BMC r32, r34, 24h
LD r32, r254, 24a, 8h
LD r33, r254, 0a, 8h
ADD64 r32, r33, r32
CP r1, r32
LD r31, r254, 72a, 48h
ADDI64 r254, r254, 120d
LD r31, r254, 72a, 32h
ADDI64 r254, r254, 104d
JALA r0, r31, 0a
code size: 273
ret: 1

View file

@ -1,6 +1,6 @@
main:
ADDI64 r254, r254, -66d
ST r31, r254, 26a, 40h
ADDI64 r254, r254, -58d
ST r31, r254, 26a, 32h
JAL r31, r0, :returner_fn
CP r32, r1
ADDI64 r33, r254, 2d
@ -25,8 +25,8 @@ main:
JMP :1
0: LI64 r32, 1d
CP r1, r32
1: LD r31, r254, 26a, 40h
ADDI64 r254, r254, 66d
1: LD r31, r254, 26a, 32h
ADDI64 r254, r254, 58d
JALA r0, r31, 0a
returner_bn:
ADDI64 r254, r254, -24d

View file

@ -6,9 +6,9 @@ main:
ADDI64 r254, r254, -128d
ST r31, r254, 80a, 48h
JAL r31, r0, :decide
CP r32, r1
CP r33, r0
ADDI64 r34, r254, 72d
CP r32, r1
ANDI r32, r32, 255d
JNE r32, r0, :0
CP r32, r33
@ -37,8 +37,8 @@ main:
CP r1, r32
JMP :3
6: JAL r31, r0, :decide
CP r32, r1
CP r33, r0
CP r32, r1
ANDI r32, r32, 255d
JNE r32, r0, :7
CP r32, r33

View file

@ -1,6 +1,6 @@
main:
ADDI64 r254, r254, -64d
ST r31, r254, 24a, 40h
ADDI64 r254, r254, -56d
ST r31, r254, 24a, 32h
ADDI64 r32, r254, 0d
LI64 r33, 1d
ST r33, r254, 16a, 8h
@ -9,14 +9,14 @@ main:
ST r33, r254, 8a, 8h
JAL r31, r0, :opaque
ST r1, r32, 0a, 16h
LD r34, r254, 8a, 8h
LD r35, r254, 16a, 8h
ADD64 r34, r35, r34
LD r33, r254, 8a, 8h
LD r34, r254, 16a, 8h
ADD64 r33, r34, r33
LD r32, r254, 0a, 8h
SUB64 r32, r32, r34
SUB64 r32, r32, r33
CP r1, r32
LD r31, r254, 24a, 40h
ADDI64 r254, r254, 64d
LD r31, r254, 24a, 32h
ADDI64 r254, r254, 56d
JALA r0, r31, 0a
opaque:
ADDI64 r254, r254, -16d

View file

@ -12,8 +12,8 @@ create_back_buffer:
0: LI8 r34, 255b
CP r2, r34
JAL r31, r0, :request_page
CP r35, r1
SUB64 r32, r32, r33
CP r35, r1
5: JGTS r32, r0, :2
CP r1, r35
JMP :1

View file

@ -1,23 +1,23 @@
main:
ADDI64 r254, r254, -52d
ST r31, r254, 4a, 48h
ADDI64 r254, r254, -44d
ST r31, r254, 4a, 40h
ADDI64 r32, r254, 0d
JAL r31, r0, :random_color
ST r1, r32, 0a, 4h
LD r34, r254, 0a, 1h
LD r35, r254, 1a, 1h
LD r36, r254, 2a, 1h
LD r33, r254, 0a, 1h
LD r34, r254, 1a, 1h
LD r35, r254, 2a, 1h
ANDI r33, r33, 255d
ANDI r34, r34, 255d
ANDI r35, r35, 255d
LD r32, r254, 3a, 1h
ANDI r33, r36, 255d
ADD64 r34, r35, r34
ANDI r32, r32, 255d
ANDI r35, r35, 255d
ADD64 r33, r34, r33
ANDI r32, r32, 255d
ADD64 r33, r33, r35
ADD64 r32, r33, r32
CP r1, r32
LD r31, r254, 4a, 48h
ADDI64 r254, r254, 52d
LD r31, r254, 4a, 40h
ADDI64 r254, r254, 44d
JALA r0, r31, 0a
random_color:
LRA r13, r0, :white

View file

@ -5,8 +5,8 @@ do_stuff:
just_read:
JALA r0, r31, 0a
main:
ADDI64 r254, r254, -80d
ST r31, r254, 48a, 32h
ADDI64 r254, r254, -72d
ST r31, r254, 48a, 24h
ADDI64 r32, r254, 16d
CP r1, r32
JAL r31, r0, :optionala
@ -37,8 +37,8 @@ main:
CP r33, r1
ADD64 r32, r33, r32
CP r1, r32
1: LD r31, r254, 48a, 32h
ADDI64 r254, r254, 80d
1: LD r31, r254, 48a, 24h
ADDI64 r254, r254, 72d
JALA r0, r31, 0a
optional:
ADDI64 r254, r254, -16d

View file

@ -9,8 +9,8 @@ foo:
ADDI64 r254, r254, 16d
JALA r0, r31, 0a
main:
ADDI64 r254, r254, -88d
ST r31, r254, 48a, 40h
ADDI64 r254, r254, -80d
ST r31, r254, 48a, 32h
ADDI64 r32, r254, 32d
JAL r31, r0, :foo
ST r1, r32, 0a, 16h
@ -30,8 +30,8 @@ main:
LI64 r33, 7d
SUB64 r32, r33, r32
CP r1, r32
LD r31, r254, 48a, 40h
ADDI64 r254, r254, 88d
LD r31, r254, 48a, 32h
ADDI64 r254, r254, 80d
JALA r0, r31, 0a
code size: 347
ret: 0

View file

@ -1,46 +1,46 @@
main:
ADDI64 r254, r254, -48d
ST r31, r254, 16a, 32h
ADDI64 r254, r254, -40d
ST r31, r254, 16a, 24h
ADDI64 r32, r254, 0d
CP r3, r0
CP r4, r0
JAL r31, r0, :maina
ST r1, r32, 0a, 16h
LD r34, r254, 12a, 1h
LD r33, r254, 12a, 1h
LD r32, r254, 3a, 1h
SUB8 r32, r32, r34
SUB8 r32, r32, r33
ANDI r32, r32, 255d
CP r1, r32
LD r31, r254, 16a, 32h
ADDI64 r254, r254, 48d
LD r31, r254, 16a, 24h
ADDI64 r254, r254, 40d
JALA r0, r31, 0a
maina:
ADDI64 r254, r254, -52d
ST r31, r254, 20a, 32h
ADDI64 r254, r254, -44d
ST r31, r254, 20a, 24h
ADDI64 r32, r254, 16d
JAL r31, r0, :small_struct
ST r1, r32, 0a, 4h
ST r0, r254, 0a, 1h
ST r0, r254, 1a, 1h
ST r0, r254, 2a, 1h
LI8 r33, 3b
ST r33, r254, 3a, 1h
LI8 r34, 1b
ST r34, r254, 4a, 1h
LI8 r32, 3b
ST r32, r254, 3a, 1h
LI8 r33, 1b
ST r33, r254, 4a, 1h
ST r0, r254, 5a, 1h
ST r0, r254, 6a, 1h
ST r0, r254, 7a, 1h
ST r0, r254, 8a, 1h
ST r0, r254, 9a, 1h
ST r0, r254, 10a, 1h
ST r33, r254, 11a, 1h
ST r34, r254, 12a, 1h
ST r32, r254, 11a, 1h
ST r33, r254, 12a, 1h
ST r0, r254, 13a, 1h
ST r0, r254, 14a, 1h
ST r0, r254, 15a, 1h
LD r1, r254, 0a, 16h
LD r31, r254, 20a, 32h
ADDI64 r254, r254, 52d
LD r31, r254, 20a, 24h
ADDI64 r254, r254, 44d
JALA r0, r31, 0a
small_struct:
ADDI64 r254, r254, -4d