MCPcopy Create free account
hub / github.com/chc4/lineiform / function_use_stack

Function function_use_stack

tangle/src/ir.rs:618–673  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

616
617 #[test]
618 pub fn function_use_stack() {
619 let mut ir = IR::new();
620 let mut f = Node::function::<1, 1>(&mut ir);
621 let input = f.add_argument(&mut ir);
622 let output = f.add_return(&mut ir);
623 // We create a stack slot
624 let mut ss1 = f.add_stack_slot(&mut ir);
625
626 // ...and push, which should resolve to a use of ss1
627 let mut push = Node::simple(Operation::StoreStack);
628 ss1 = f.add_body(push, &mut ir, |i, r| {
629 i.connect_input(0, ss1, r);
630 i.connect_input(1, input, r);
631 i.sinks()[0]
632 }).1;
633
634 // get the value
635 let mut pop = Node::simple(Operation::LoadStack);
636 let ss_val = f.add_body(pop, &mut ir, |i, r| {
637 i.connect_input(0, ss1, r);
638 i.sinks()[0]
639 }).1;
640
641 // then add to it via ss1
642 let mut two = Node::constant(2);
643 let mut add = Node::simple(Operation::Add);
644 let mut two_const = f.add_body(two, &mut ir, |two, r| {
645 two.sinks()[0]
646 }).1;
647 let added_val = f.add_body(add, &mut ir, |add, r| {
648 println!("add closure {}", r.ports[ss1].storage);
649 add.connect_input(0, ss_val, r);
650 add.connect_input(1, two_const, r);
651 add.sinks()[0]
652 }).1;
653
654 // and store it again
655 let mut push = Node::simple(Operation::StoreStack);
656 f.add_body(push, &mut ir, |i, r| {
657 i.connect_input(0, ss1, r);
658 i.connect_input(1, added_val, r);
659 ss1 = i.sinks()[0];
660 });
661
662 // ...then pop it into the result register
663 let mut pop = Node::simple(Operation::LoadStack);
664 f.add_body(pop, &mut ir, |i, r| {
665 i.connect_input(0, ss1, r);
666 i.connect_output(0, output, r);
667 });
668
669 ir.set_body(f);
670 let hello_fn: extern "C" fn(usize) -> usize = ir.compile_fn().unwrap().0;
671 assert_eq!(hello_fn(0), 2);
672 assert_eq!(hello_fn(1), 3);
673 }
674
675 #[test]

Callers

nothing calls this directly

Calls 9

add_argumentMethod · 0.80
add_returnMethod · 0.80
add_stack_slotMethod · 0.80
add_bodyMethod · 0.80
connect_inputMethod · 0.80
sinksMethod · 0.80
connect_outputMethod · 0.80
set_bodyMethod · 0.80
compile_fnMethod · 0.80

Tested by

no test coverage detected