From 780f056e61323a41abcaf0dd53a44f99bcac197c Mon Sep 17 00:00:00 2001 From: Wojciech Kosior Date: Wed, 16 Sep 2020 14:42:32 +0200 Subject: add function calling (call, ret and drop instructions) with a testbench + bugfix in stack machine --- tests/stack_machine_function_call/Makefile | 1 + .../stack_machine_function_call/instructions.s.tcl | 88 ++++++++++++++++++++++ tests/stack_machine_function_call/test.v | 1 + .../words_to_verify.mem | 3 + 4 files changed, 93 insertions(+) create mode 120000 tests/stack_machine_function_call/Makefile create mode 100644 tests/stack_machine_function_call/instructions.s.tcl create mode 120000 tests/stack_machine_function_call/test.v create mode 100644 tests/stack_machine_function_call/words_to_verify.mem (limited to 'tests') diff --git a/tests/stack_machine_function_call/Makefile b/tests/stack_machine_function_call/Makefile new file mode 120000 index 0000000..2c3c770 --- /dev/null +++ b/tests/stack_machine_function_call/Makefile @@ -0,0 +1 @@ +../stack_machine_store/Makefile \ No newline at end of file diff --git a/tests/stack_machine_function_call/instructions.s.tcl b/tests/stack_machine_function_call/instructions.s.tcl new file mode 100644 index 0000000..450b669 --- /dev/null +++ b/tests/stack_machine_function_call/instructions.s.tcl @@ -0,0 +1,88 @@ +### call a simple function, that sums its 2 arguments + +## store sth at h1000 to later check if calee restored it properly +# 1 16-bit instruction +const h23 +# 2 16-bit instructions +store h1000 + +# function arguments - each loaded with 1 16-bit instruction +const h32 +const h14 +# also 1 16-bit instruction +call 18 + +## this executes after return - store the computed difference at h400 +# this takes 2 16-bit instructions +store h400 +# and this takes 1 16-bit instruction +halt + +# address 18 here + +## function frame will look like this: +## *****higher addresses***** +## argument1 +## argument2 +## ... +## return address +## local1 +## local2 +## ... +## previous frame address +## *****lower addresses***** + +## push the arguments out of r0 and r1 to memory (+ claim space for +## backed stack frame address and backed return address); +## if we had n local variables, we'd repeat that n more times +const 0 +const 0 +const 0 + +## with this we get the address to reference our locals from into r1 +get_frame +tee + +## we get our previous frame and back it up as first local +load h1000 +store+ h0 + +## we now store our new function frame +store h1000 + +## here the actual function body starts + +## fetch the first argument +load h1000 +load+ hC + +## fetch the second one +load h1000 +load+ h8 + +sub + +## function exit - store the result at the the top of current frame +load h1000 +swap +store+ hC + +## put the return address just below result +load h1000 +tee +tee +load+ h4 +store+ h8 + +## restore old frame +load+ 0 +store h1000 + +## discard current frame +drop +drop +drop +drop + +## return +ret diff --git a/tests/stack_machine_function_call/test.v b/tests/stack_machine_function_call/test.v new file mode 120000 index 0000000..f5b6a59 --- /dev/null +++ b/tests/stack_machine_function_call/test.v @@ -0,0 +1 @@ +../stack_machine_store/test.v \ No newline at end of file diff --git a/tests/stack_machine_function_call/words_to_verify.mem b/tests/stack_machine_function_call/words_to_verify.mem new file mode 100644 index 0000000..8048d6f --- /dev/null +++ b/tests/stack_machine_function_call/words_to_verify.mem @@ -0,0 +1,3 @@ +// address value + 1000 23 + 400 1E -- cgit v1.2.3