test_swapstack.py 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
# Copyright 2017 The Australian National University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from util import execute, compile_bundle, load_bundle, get_function;
import pytest;
import ctypes;

def test_swapstack_simple():
    compile_bundle(
        """
        .funcdef new_func <()->()>
        {
            entry():
                CCALL #DEFAULT <exit_type exit_sig> exit(<int<32>>3) 
                RET
        }        
        .funcdef test_swapstack_simple <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                s = COMMINST uvm.new_stack<[()->()]>(new_func)
                SWAPSTACK s KILL_OLD PASS_VALUES<>()
        }
        """, "test_swapstack_simple");
    assert(execute("test_swapstack_simple", []) == 3);

def test_swapstack_swap_back():
    compile_bundle(
        """
        .funcdef new_func <(stackref)->()>
        {
            entry(<stackref>s):
43
                SWAPSTACK s KILL_OLD PASS_VALUES<>()
44 45 46 47
        }        
        .funcdef test_swapstack_swap_back <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
48 49
                cs =  COMMINST uvm.current_stack()
                s = COMMINST uvm.new_stack<[(stackref)->()]>(new_func)
50 51 52 53 54 55 56 57 58 59 60 61 62
                SWAPSTACK s RET_WITH<> PASS_VALUES<stackref>(cs)
                RET <int<32>>3
                
        }
        """, "test_swapstack_swap_back");
    assert(execute("test_swapstack_swap_back", []) == 3);

def test_swapstack_pass_vals():
    compile_bundle(
        """
        .funcdef new_func <(stackref)->()>
        {
            entry(<stackref>s):
63
                SWAPSTACK s KILL_OLD PASS_VALUES<int<32>>(<int<32>> 3) 
64 65 66 67
        }        
        .funcdef test_swapstack_pass_vals <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
68
                cs =  COMMINST uvm.current_stack()
69
                s = COMMINST uvm.new_stack<[(stackref)->()]>(new_func)
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
70
                r = SWAPSTACK s RET_WITH<int<32>> PASS_VALUES<stackref>(cs)
71 72 73 74 75 76 77 78 79 80 81
                RET r
        }
        """, "test_swapstack_pass_vals");
    assert(execute("test_swapstack_pass_vals", []) == 3);

def test_swapstack_throw():
    compile_bundle(
        """
        .funcdef new_func <(stackref)->()>
        {
            entry(<stackref>s):
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
82 83 84 85 86 87 88
                er = NEW <int<32>>
                eri = GETIREF <int<32>> er
                STORE <int<32>> eri <int<32>> 3
                ev = REFCAST <ref<int<32>> ref<void>> er
                SWAPSTACK s KILL_OLD THROW_EXC ev
        }
        .funcdef test_swapstack_throw <main_sig>
89 90
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
91 92 93
                cs =  COMMINST uvm.current_stack()
                s = COMMINST uvm.new_stack<[(stackref)->()]>(new_func)
                r = SWAPSTACK s RET_WITH<int<32>> PASS_VALUES<stackref>(cs) EXC(nor_dest(r) exc_dest())
94
                RET r
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
95
            nor_dest(<int<32>> r):
96 97
                RET <int<32>>0
            exc_dest()[exc_param]:
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
98 99 100 101
                e = REFCAST <ref<void> ref<int<32>>> exc_param
                evi = GETIREF <int<32>> e
                ev = LOAD <int<32>> evi
                RET ev
102
        }
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
103 104
        """, "test_swapstack_throw");
    assert(execute("test_swapstack_throw", []) == 3);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

def test_swapstack_throw_back():
    compile_bundle(
        """
        .funcdef new_func <(stackref)->()>
        {
            entry(<stackref>s):
                er = NEW <int<32>>
                eri = GETIREF <int<32>> er
                STORE <int<32>> eri <int<32>> 1
                ev = REFCAST <ref<int<32>> ref<void>> er
                r = SWAPSTACK s RET_WITH<int<32>> THROW_EXC ev EXC(nor_dest(r) exc_dest()) 
            
            nor_dest(<int<32>> r):
                CCALL #DEFAULT <exit_type exit_sig> exit(<int<32>>0)
                RET
            exc_dest()[exc_param]:
                e = REFCAST <ref<void> ref<int<32>>> exc_param
                evi = GETIREF <int<32>> e
                ev = LOAD <int<32>> evi
                CCALL #DEFAULT <exit_type exit_sig> exit(ev)
                RET
        }
        .funcdef test_swapstack_throw_back <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                cs =  COMMINST uvm.current_stack()
                s = COMMINST uvm.new_stack<[(stackref)->()]>(new_func)
                r = SWAPSTACK s RET_WITH<int<32>> PASS_VALUES<stackref>(cs) EXC(nor_dest(r) exc_dest(s))
                RET r
            nor_dest(<int<32>> r):
                RET <int<32>>0
            exc_dest(<stackref> s)[exc_param]:
                e = REFCAST <ref<void> ref<int<32>>> exc_param
                evi = GETIREF <int<32>> e
                ev = LOAD <int<32>> evi
                newv = ADD <int<32>> ev <int<32>> 2
                STORE <int<32>> evi newv
                // exc_param += 2
                
                // Throw back to new_func
                SWAPSTACK s KILL_OLD THROW_EXC exc_param
        }
        """, "test_swapstack_throw_back");
    assert(execute("test_swapstack_throw", []) == 3);