WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

test_swapstack.py 10.7 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
                RET r
        }
        """, "test_swapstack_pass_vals");
    assert(execute("test_swapstack_pass_vals", []) == 3);

Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
def test_swapstack_save_vals():
    compile_bundle(
        """
        .funcdef new_func <(stackref)->()>
        {
            entry(<stackref>s):
                SWAPSTACK s KILL_OLD PASS_VALUES<int<32>>(<int<32>> 2) 
        }        
        .funcdef test_swapstack_save_vals <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)
                rv = ADD <int<32>> argc r
                RET rv
        }
        """, "test_swapstack_save_vals");
    assert(execute("test_swapstack_save_vals", []) == 3);


97 98 99 100 101 102 103 104
def test_swapstack_pass_stack():
    compile_bundle(
        """
        .funcsig stack_sig = (stackref double double double double double double double double double double)->()
        .funcdef new_func <stack_sig>
        {
            entry(<stackref>s <double>d0 <double>d1 <double>d2 <double>d3 <double>d4 <double>d5 <double>d6 <double>d7 <double> d8 <double> d9):
                SWAPSTACK s KILL_OLD PASS_VALUES<double double double double double double double double double double>(d0 d1 d2 d3 d4 d5 d6 d7 d8 d9) 
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
105
        }
106 107 108 109 110
        .funcdef test_swapstack_pass_stack <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                cs =  COMMINST uvm.current_stack()
                s = COMMINST uvm.new_stack<[stack_sig]>(new_func)
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
111
                (d0 d1 d2 d3 d4 d5 d6 d7 d8 d9) = SWAPSTACK s RET_WITH<double double double double double double double double double double> PASS_VALUES<stackref double double double double double double double double double double>(cs <double>0.0 d <double>1.0 d <double>2.0 d <double>3.0 d <double>4.0 d <double>5.0 d <double>6.0 d <double>7.0 d <double>8.0 d <double>9.0 d)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
                s1 = FADD <double> d0 d1
                s2 = FADD <double> s1 d2
                s3 = FADD <double> s2 d3
                s4 = FADD <double> s3 d4
                s5 = FADD <double> s4 d5
                s6 = FADD <double> s5 d6
                s7 = FADD <double> s6 d7
                s8 = FADD <double> s7 d8
                s9 = FADD <double> s8 d9
                r = FPTOSI <double int<32>> s9
                RET r
        }
        """, "test_swapstack_pass_stack");
    assert(execute("test_swapstack_pass_stack", []) == 45);

127 128 129 130 131 132
def test_swapstack_throw():
    compile_bundle(
        """
        .funcdef new_func <(stackref)->()>
        {
            entry(<stackref>s):
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
133 134 135 136 137 138 139
                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>
140 141
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
142 143 144 145
                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())
            nor_dest(<int<32>> r):
146 147
                RET <int<32>>0
            exc_dest()[exc_param]:
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
148 149 150 151
                e = REFCAST <ref<void> ref<int<32>>> exc_param
                evi = GETIREF <int<32>> e
                ev = LOAD <int<32>> evi
                RET ev
152
        }
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
153 154
        """, "test_swapstack_throw");
    assert(execute("test_swapstack_throw", []) == 3);
155

Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
def test_swapstack_throw_back_simple():
    compile_bundle(
        """
        .funcdef new_func <(stackref)->()>
        {
            entry(<stackref>s):
                cs =  COMMINST uvm.current_stack()

                er = NEW <stackref>
                eri = GETIREF <stackref> er
                STORE <stackref> eri cs
                ev = REFCAST <ref<stackref> 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_simple <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())
            nor_dest(<int<32>> r):
                RET <int<32>>0
            exc_dest()[exc_param]:
                e = REFCAST <ref<void> ref<stackref>> exc_param
                evi = GETIREF <stackref> e
                ev = LOAD <stackref> evi
                               
                eer = NEW <int<32>>
                eeri = GETIREF <int<32>> eer
                STORE <int<32>> eeri <int<32>> 3
                eev = REFCAST <ref<int<32>> ref<void>> eer
                
                // Throw back to new_func
                SWAPSTACK ev KILL_OLD THROW_EXC eev
        }
        """, "test_swapstack_throw_back_simple");
    assert(execute("test_swapstack_throw_back_simple", []) == 3);

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
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))
            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");
246
    assert(execute("test_swapstack_throw_back", []) == 3);
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

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