test_thread_and_stack.py 13.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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;

19
def test_swapstack_kill_old():
20 21
    compile_bundle(
        """
22
        .funcdef test_swapstack_kill_old_swapee <()->()>
23 24 25 26 27
        {
            entry():
                CCALL #DEFAULT <exit_type exit_sig> exit(<int<32>>3) 
                RET
        }        
28
        .funcdef test_swapstack_kill_old <main_sig>
29 30
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
31
                s = COMMINST uvm.new_stack<[()->()]>(test_swapstack_kill_old_swapee)
32 33
                SWAPSTACK s KILL_OLD PASS_VALUES<>()
        }
34 35
        """, "test_swapstack_kill_old");
    assert(execute("test_swapstack_kill_old", []) == 3);
36 37 38 39

def test_swapstack_swap_back():
    compile_bundle(
        """
40
        .funcdef test_swapstack_swap_back_swapee <(stackref)->()>
41 42
        {
            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
                cs =  COMMINST uvm.current_stack()
49
                s = COMMINST uvm.new_stack<[(stackref)->()]>(test_swapstack_swap_back_swapee)
50 51 52 53 54 55 56
                SWAPSTACK s RET_WITH<> PASS_VALUES<stackref>(cs)
                RET <int<32>>3
                
        }
        """, "test_swapstack_swap_back");
    assert(execute("test_swapstack_swap_back", []) == 3);

57
def test_swapstack_ret_values():
58 59
    compile_bundle(
        """
60
        .funcdef test_swapstack_ret_values_swapee <(stackref)->()>
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
61 62 63 64
        {
            entry(<stackref>s):
                SWAPSTACK s KILL_OLD PASS_VALUES<int<32>>(<int<32>> 2) 
        }        
65
        .funcdef test_swapstack_ret_values <main_sig>
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
66 67 68
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                cs =  COMMINST uvm.current_stack()
69
                s = COMMINST uvm.new_stack<[(stackref)->()]>(test_swapstack_ret_values_swapee)
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
70 71 72 73
                r = SWAPSTACK s RET_WITH<int<32>> PASS_VALUES<stackref>(cs)
                rv = ADD <int<32>> argc r
                RET rv
        }
74 75
        """, "test_swapstack_ret_values");
    assert(execute("test_swapstack_ret_values", []) == 3);
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
76

77
def test_swapstack_pass_stack_args():
78 79 80
    compile_bundle(
        """
        .funcsig stack_sig = (stackref double double double double double double double double double double)->()
81
        .funcdef test_swapstack_pass_stack_args_swapee <stack_sig>
82 83 84
        {
            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
85
        }
86
        .funcdef test_swapstack_pass_stack_args <main_sig>
87 88 89
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                cs =  COMMINST uvm.current_stack()
90
                s = COMMINST uvm.new_stack<[stack_sig]>(test_swapstack_pass_stack_args_swapee)
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
91
                (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)
92 93 94 95 96 97 98 99 100 101 102 103
                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
        }
104 105
        """, "test_swapstack_pass_stack_args");
    assert(execute("test_swapstack_pass_stack_args", []) == 45);
106

107 108 109
def test_swapstack_throw():
    compile_bundle(
        """
110
        .funcdef test_swapstack_throw_swapee <(stackref)->()>
111 112
        {
            entry(<stackref>s):
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
113 114 115 116 117 118 119
                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>
120 121
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
122
                cs =  COMMINST uvm.current_stack()
123
                s = COMMINST uvm.new_stack<[(stackref)->()]>(test_swapstack_throw_swapee)
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
124 125
                r = SWAPSTACK s RET_WITH<int<32>> PASS_VALUES<stackref>(cs) EXC(nor_dest(r) exc_dest())
            nor_dest(<int<32>> r):
126 127
                RET <int<32>>0
            exc_dest()[exc_param]:
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
128 129 130 131
                e = REFCAST <ref<void> ref<int<32>>> exc_param
                evi = GETIREF <int<32>> e
                ev = LOAD <int<32>> evi
                RET ev
132
        }
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
133
        """, "test_swapstack_throw");
134
    assert(execute("test_swapstack_throw", []) == 3);
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
135

136 137 138
def test_swapstack_throw_back():
    compile_bundle(
        """
139
        .funcdef test_swapstack_throw_back_swapee <(stackref)->()>
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        {
            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()
162
                s = COMMINST uvm.new_stack<[(stackref)->()]>(test_swapstack_throw_back_swapee)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
                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");
178
    assert(execute("test_swapstack_throw_back", []) == 3);
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
179 180 181 182

def test_kill_stack():
    compile_bundle(
        """
183
        .funcdef test_kill_stack_swapee <(stackref)->()>
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
184 185 186 187 188 189 190 191 192
        {
            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):
193
                cs = COMMINST uvm.current_stack()
194
                s = COMMINST uvm.new_stack<[(stackref)->()]>(test_kill_stack_swapee)
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
195 196 197 198 199
                SWAPSTACK s RET_WITH<> PASS_VALUES<stackref>(cs)
                RET <int<32>>0
                
        }
        """, "test_kill_stack");
200 201 202 203 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    assert(execute("test_kill_stack", []) == 3);

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

def test_newthread_swapstack():
    compile_bundle(
        """
        .funcdef test_newthread_swapstack_thread <(stackref)->()>
        {
            entry(<stackref>s):
                t = NEWTHREAD s PASS_VALUES<int<32>>(<int<32>> 2)
                BRANCH loop()
            loop():
                a = ADD <int<1>> <int<1>>0 <int<1>>0 // needed due to issue #82
                BRANCH loop()
        }        
        .funcdef test_newthread_swapstack <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                cs =  COMMINST uvm.current_stack()
                s = COMMINST uvm.new_stack<[(stackref)->()]>(test_newthread_swapstack_thread)
                r = SWAPSTACK s RET_WITH<int<32>> PASS_VALUES<stackref>(cs)
                rv = ADD <int<32>> argc r
                RET rv
                // argc = 1
        }
        """, "test_newthread_swapstack");
    assert(execute("test_newthread_swapstack", []) == 3);

def test_newthread_throw():
    compile_bundle(
        """
        .funcdef test_newthread_throw_thread <(stackref)->()>
        {
            entry(<stackref>s):
                er = NEW <int<32>>
                eri = GETIREF <int<32>> er
                STORE <int<32>> eri <int<32>> 3
                ev = REFCAST <ref<int<32>> ref<void>> er
                t = NEWTHREAD s THROW_EXC ev
                COMMINST uvm.thread_exit()
        }
        .funcdef test_newthread_throw <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                cs =  COMMINST uvm.current_stack()
                s = COMMINST uvm.new_stack<[(stackref)->()]>(test_newthread_throw_thread)
                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<int<32>>> exc_param
                evi = GETIREF <int<32>> e
                ev = LOAD <int<32>> evi
                RET ev
        }
        """, "test_newthread_throw");
    assert(execute("test_newthread_throw", []) == 3);

def test_newthread_threadlocal():
    compile_bundle(
        """
        .funcdef test_newthread_threadlocal_thread <()->()>
        {
            entry():
                tv = COMMINST uvm.get_threadlocal()
                tr = REFCAST <ref<void> ref<int<32>>> tv
                tvi = GETIREF <int<32>> tr
                tv = LOAD <int<32>> tvi
                CCALL #DEFAULT <exit_type exit_sig> exit(tv)
                RET 
        }
        .funcdef test_newthread_threadlocal <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                cs =  COMMINST uvm.current_stack()
                s = COMMINST uvm.new_stack<[()->()]>(test_newthread_threadlocal_thread)
                
                tr = NEW <int<32>>
                tri = GETIREF <int<32>> tr
                STORE <int<32>> tri <int<32>> 3
                tl = REFCAST <ref<int<32>> ref<void>> tr
                t = NEWTHREAD s THREADLOCAL (tl) PASS_VALUES<>()
                COMMINST uvm.thread_exit()
        }
        """, "test_newthread_threadlocal");
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    assert(execute("test_newthread_threadlocal", []) == 3);

def test_newthread_stack_args():
    compile_bundle(
        """
        .funcsig stack_sig = (stackref double double double double double double double double double double)->()
        .funcdef test_newthread_stack_args_thread <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):
                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
                CCALL #DEFAULT <exit_type exit_sig> exit(r)
                RET
        }
        .funcdef test_newthread_stack_args <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                cs =  COMMINST uvm.current_stack()
                s = COMMINST uvm.new_stack<[stack_sig]>(test_newthread_stack_args_thread)
                t = NEWTHREAD s 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)
                COMMINST uvm.thread_exit()
        }
        """, "test_newthread_stack_args");
    assert(execute("test_newthread_stack_args", []) == 45);