test_address_mod.py 5.07 KB
Newer Older
qinsoon's avatar
qinsoon committed
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 150
# 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;

# all the tests try to add 1 with 42, with a result of 43
# but the constants may be in the memory
# these tests are designed to test most of the cases for addressing mode on x86

def test_add_load_val():
    compile_bundle(
        """
        .funcdef test_add_load_val <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                ref_y = NEW <int<64>>
                iref_y = GETIREF <int<64>> ref_y
                STORE <int<64>> iref_y <int<64>> 42
                BRANCH body(<int<64>> 1 ref_y)
            
            body(<int<64>> x <ref<int<64>>> ref_y):
                iref_y = GETIREF <int<64>> ref_y
                y = LOAD <int<64>> iref_y
                sum = ADD <int<64>> x y
                BRANCH exit(sum)
            
            exit(<int<64>> res):
                res32 = TRUNC <int<64> int<32>> res
                RET res32
        }
        """, "test_add_load_val"
    )
    assert(execute("test_add_load_val") == 43)

def test_add_load_cast():
    compile_bundle(
        """
        .funcdef test_add_load_cast <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                ref_y = NEW <int<64>>
                iref_y = GETIREF <int<64>> ref_y
                STORE <int<64>> iref_y <int<64>> 42
                ref32_y = REFCAST <ref<int<64>> ref<int<32>>> ref_y
                BRANCH body(<int<64>> 1 ref32_y)
                
            body(<int<64>> x <ref<int<32>>> ref32_y):
                ref_y = REFCAST <ref<int<32>> ref<int<64>>> ref32_y
                iref_y = GETIREF <int<64>> ref_y
                y = LOAD <int<64>> iref_y
                sum = ADD <int<64>> x y
                BRANCH exit(sum)
            
            exit(<int<64>> res):
                res32 = TRUNC <int<64> int<32>> res
                RET res32
        }
        """, "test_add_load_cast"
    )
    assert(execute("test_add_load_cast") == 43)

def test_add_load_load():
    compile_bundle(
        """
        .funcdef test_add_load_load <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                ref_y = NEW <int<64>>
                iref_y = GETIREF <int<64>> ref_y
                STORE <int<64>> iref_y <int<64>> 42
                
                ref_cell = NEW <ref<int<64>>>
                iref_cell = GETIREF <ref<int<64>>> ref_cell
                STORE <ref<int<64>>> iref_cell ref_y
                BRANCH body(<int<64>> 1 ref_cell)
                
            body(<int<64>> x <ref<ref<int<64>>>> ref_cell):
                iref_cell = GETIREF <ref<int<64>>> ref_cell
                ref_y = LOAD <ref<int<64>>> iref_cell
                iref_y = GETIREF <int<64>> ref_y
                y = LOAD <int<64>> iref_y
                sum = ADD <int<64>> x y
                BRANCH exit(sum)
            
            exit(<int<64>> res):
                res32 = TRUNC <int<64> int<32>> res
                RET res32
        }
        """, "test_add_load_load"
    )
    assert(execute("test_add_load_load") == 43)

def test_add_load_global():
    compile_bundle(
        """
        .global global_y <int<64>>
        .funcdef test_add_load_global <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                STORE <int<64>> global_y <int<64>> 42
                BRANCH body(<int<64>> 1)
                
            body(<int<64>> x):
                y = LOAD <int<64>> global_y
                sum = ADD <int<64>> x y
                BRANCH exit(sum)
            
            exit(<int<64>> res):
                res32 = TRUNC <int<64> int<32>> res
                RET res32
        }
        """, "test_add_load_global"
    )
    assert(execute("test_add_load_global") == 43)

def test_add_load_cast_global():
    compile_bundle(
        """
        .global global_y <int<64>>
        .funcdef test_add_load_cast_global <main_sig>
        {
            entry(<int<32>>argc <uptr<uptr<char>>>argv):
                STORE <int<64>> global_y <int<64>> 42
                BRANCH body(<int<32>> 1)
                
            body(<int<64>> x):
                global32_y = REFCAST <iref<int<64>> iref<int<32>>> global_y
                y = LOAD <int<32>> global32_y
                sum = ADD <int<32>> x y
                BRANCH exit(sum)
            
            exit(<int<32>> res):
                RET res
        }
        """, "test_add_load_cast_global"
    )
    assert(execute("test_add_load_cast_global") == 43)