paul@354 | 1 | /* Native functions for integer operations. |
paul@354 | 2 | |
paul@354 | 3 | Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> |
paul@354 | 4 | |
paul@354 | 5 | This program is free software; you can redistribute it and/or modify it under |
paul@354 | 6 | the terms of the GNU General Public License as published by the Free Software |
paul@354 | 7 | Foundation; either version 3 of the License, or (at your option) any later |
paul@354 | 8 | version. |
paul@354 | 9 | |
paul@354 | 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@354 | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@354 | 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@354 | 13 | details. |
paul@354 | 14 | |
paul@354 | 15 | You should have received a copy of the GNU General Public License along with |
paul@354 | 16 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@354 | 17 | */ |
paul@354 | 18 | |
paul@354 | 19 | #include <limits.h> /* INT_MAX, INT_MIN */ |
paul@354 | 20 | #include <math.h> /* ceil, log10, pow */ |
paul@354 | 21 | #include <stdio.h> /* fdopen, snprintf */ |
paul@354 | 22 | #include <errno.h> /* errno */ |
paul@354 | 23 | #include "native/common.h" |
paul@354 | 24 | #include "types.h" |
paul@354 | 25 | #include "exceptions.h" |
paul@354 | 26 | #include "ops.h" |
paul@354 | 27 | #include "progconsts.h" |
paul@354 | 28 | #include "progops.h" |
paul@354 | 29 | #include "progtypes.h" |
paul@354 | 30 | #include "main.h" |
paul@354 | 31 | |
paul@354 | 32 | /* Integer operations. */ |
paul@354 | 33 | |
paul@354 | 34 | __attr __fn_native_int_int_add(__attr __args[]) |
paul@354 | 35 | { |
paul@354 | 36 | __attr * const _data = &__args[1]; |
paul@354 | 37 | __attr * const other = &__args[2]; |
paul@354 | 38 | /* _data and other interpreted as int */ |
paul@354 | 39 | int i = _data->intvalue; |
paul@354 | 40 | int j = other->intvalue; |
paul@354 | 41 | |
paul@354 | 42 | /* Test for overflow. */ |
paul@354 | 43 | if (((i > 0) && (j > 0) && (i > INT_MAX - j)) || |
paul@354 | 44 | ((i < 0) && (j < 0) && (i < INT_MIN - j))) |
paul@354 | 45 | |
paul@354 | 46 | __raise_overflow_error(); |
paul@354 | 47 | |
paul@354 | 48 | /* Return the new integer. */ |
paul@354 | 49 | return __new_int(i + j); |
paul@354 | 50 | } |
paul@354 | 51 | |
paul@354 | 52 | __attr __fn_native_int_int_sub(__attr __args[]) |
paul@354 | 53 | { |
paul@354 | 54 | __attr * const _data = &__args[1]; |
paul@354 | 55 | __attr * const other = &__args[2]; |
paul@354 | 56 | /* _data and other interpreted as int */ |
paul@354 | 57 | int i = _data->intvalue; |
paul@354 | 58 | int j = other->intvalue; |
paul@354 | 59 | |
paul@354 | 60 | /* Test for overflow. */ |
paul@354 | 61 | if (((i < 0) && (j > 0) && (i < INT_MIN + j)) || |
paul@354 | 62 | ((i > 0) && (j < 0) && (i > INT_MAX + j))) |
paul@354 | 63 | |
paul@354 | 64 | __raise_overflow_error(); |
paul@354 | 65 | |
paul@354 | 66 | /* Return the new integer. */ |
paul@354 | 67 | return __new_int(i - j); |
paul@354 | 68 | } |
paul@354 | 69 | |
paul@354 | 70 | __attr __fn_native_int_int_mul(__attr __args[]) |
paul@354 | 71 | { |
paul@354 | 72 | __attr * const _data = &__args[1]; |
paul@354 | 73 | __attr * const other = &__args[2]; |
paul@354 | 74 | /* _data and other interpreted as int */ |
paul@354 | 75 | int i = _data->intvalue; |
paul@354 | 76 | int j = other->intvalue; |
paul@354 | 77 | |
paul@354 | 78 | /* Test for overflow. */ |
paul@354 | 79 | if (((i > 0) && (j > 0) && (i > INT_MAX / j)) || |
paul@354 | 80 | ((i < 0) && (j < 0) && (i > INT_MAX / j)) || |
paul@354 | 81 | ((i < 0) && (j > 0) && (i < INT_MIN / j)) || |
paul@354 | 82 | ((i > 0) && (j < 0) && (j < INT_MIN / i))) |
paul@354 | 83 | |
paul@354 | 84 | __raise_overflow_error(); |
paul@354 | 85 | |
paul@354 | 86 | /* Return the new integer. */ |
paul@354 | 87 | return __new_int(i * j); |
paul@354 | 88 | } |
paul@354 | 89 | |
paul@354 | 90 | __attr __fn_native_int_int_div(__attr __args[]) |
paul@354 | 91 | { |
paul@354 | 92 | __attr * const _data = &__args[1]; |
paul@354 | 93 | __attr * const other = &__args[2]; |
paul@354 | 94 | /* _data and other interpreted as int */ |
paul@354 | 95 | int i = _data->intvalue; |
paul@354 | 96 | int j = other->intvalue; |
paul@354 | 97 | |
paul@354 | 98 | /* Test for division by zero or overflow. */ |
paul@354 | 99 | if (j == 0) |
paul@354 | 100 | __raise_zero_division_error(); |
paul@354 | 101 | else if ((j == -1) && (i == INT_MIN)) |
paul@354 | 102 | __raise_overflow_error(); |
paul@354 | 103 | |
paul@354 | 104 | /* Return the new integer. */ |
paul@354 | 105 | return __new_int(i / j); |
paul@354 | 106 | } |
paul@354 | 107 | |
paul@354 | 108 | __attr __fn_native_int_int_mod(__attr __args[]) |
paul@354 | 109 | { |
paul@354 | 110 | __attr * const _data = &__args[1]; |
paul@354 | 111 | __attr * const other = &__args[2]; |
paul@354 | 112 | /* _data and other interpreted as int */ |
paul@354 | 113 | int i = _data->intvalue; |
paul@354 | 114 | int j = other->intvalue; |
paul@354 | 115 | |
paul@354 | 116 | /* Test for division by zero or overflow. */ |
paul@354 | 117 | if (j == 0) |
paul@354 | 118 | __raise_zero_division_error(); |
paul@354 | 119 | else if ((j == -1) && (i == INT_MIN)) |
paul@354 | 120 | __raise_overflow_error(); |
paul@354 | 121 | |
paul@354 | 122 | /* Return the new integer. */ |
paul@354 | 123 | return __new_int(i % j); |
paul@354 | 124 | } |
paul@354 | 125 | |
paul@354 | 126 | __attr __fn_native_int_int_neg(__attr __args[]) |
paul@354 | 127 | { |
paul@354 | 128 | __attr * const _data = &__args[1]; |
paul@354 | 129 | /* _data interpreted as int */ |
paul@354 | 130 | int i = _data->intvalue; |
paul@354 | 131 | |
paul@354 | 132 | /* Test for overflow. */ |
paul@354 | 133 | if (i == INT_MIN) |
paul@354 | 134 | __raise_overflow_error(); |
paul@354 | 135 | |
paul@354 | 136 | /* Return the new integer. */ |
paul@354 | 137 | return __new_int(-i); |
paul@354 | 138 | } |
paul@354 | 139 | |
paul@354 | 140 | __attr __fn_native_int_int_pow(__attr __args[]) |
paul@354 | 141 | { |
paul@354 | 142 | __attr * const _data = &__args[1]; |
paul@354 | 143 | __attr * const other = &__args[2]; |
paul@354 | 144 | /* _data and other interpreted as int */ |
paul@354 | 145 | int i = _data->intvalue; |
paul@354 | 146 | int j = other->intvalue; |
paul@354 | 147 | int k; |
paul@354 | 148 | |
paul@354 | 149 | errno = 0; |
paul@354 | 150 | k = (int) pow(i, j); |
paul@354 | 151 | |
paul@354 | 152 | /* Test for overflow. */ |
paul@354 | 153 | |
paul@354 | 154 | if (errno == ERANGE) |
paul@354 | 155 | __raise_overflow_error(); |
paul@354 | 156 | |
paul@354 | 157 | /* Return the new integer. */ |
paul@354 | 158 | return __new_int(k); |
paul@354 | 159 | } |
paul@354 | 160 | |
paul@354 | 161 | __attr __fn_native_int_int_and(__attr __args[]) |
paul@354 | 162 | { |
paul@354 | 163 | __attr * const _data = &__args[1]; |
paul@354 | 164 | __attr * const other = &__args[2]; |
paul@354 | 165 | /* _data and other interpreted as int */ |
paul@354 | 166 | int i = _data->intvalue; |
paul@354 | 167 | int j = other->intvalue; |
paul@354 | 168 | |
paul@354 | 169 | /* Return the new integer. */ |
paul@354 | 170 | /* NOTE: No overflow test applied. */ |
paul@354 | 171 | return __new_int(i & j); |
paul@354 | 172 | } |
paul@354 | 173 | |
paul@354 | 174 | __attr __fn_native_int_int_not(__attr __args[]) |
paul@354 | 175 | { |
paul@354 | 176 | __attr * const _data = &__args[1]; |
paul@354 | 177 | /* _data interpreted as int */ |
paul@354 | 178 | int i = _data->intvalue; |
paul@354 | 179 | |
paul@354 | 180 | /* Return the new integer. */ |
paul@354 | 181 | return __new_int(~i); |
paul@354 | 182 | } |
paul@354 | 183 | |
paul@354 | 184 | __attr __fn_native_int_int_or(__attr __args[]) |
paul@354 | 185 | { |
paul@354 | 186 | __attr * const _data = &__args[1]; |
paul@354 | 187 | __attr * const other = &__args[2]; |
paul@354 | 188 | /* _data and other interpreted as int */ |
paul@354 | 189 | int i = _data->intvalue; |
paul@354 | 190 | int j = other->intvalue; |
paul@354 | 191 | |
paul@354 | 192 | /* Return the new integer. */ |
paul@354 | 193 | /* NOTE: No overflow test applied. */ |
paul@354 | 194 | return __new_int(i | j); |
paul@354 | 195 | } |
paul@354 | 196 | |
paul@354 | 197 | __attr __fn_native_int_int_xor(__attr __args[]) |
paul@354 | 198 | { |
paul@354 | 199 | __attr * const _data = &__args[1]; |
paul@354 | 200 | __attr * const other = &__args[2]; |
paul@354 | 201 | /* _data and other interpreted as int */ |
paul@354 | 202 | int i = _data->intvalue; |
paul@354 | 203 | int j = other->intvalue; |
paul@354 | 204 | |
paul@354 | 205 | /* Return the new integer. */ |
paul@354 | 206 | /* NOTE: No overflow test applied. */ |
paul@354 | 207 | return __new_int(i ^ j); |
paul@354 | 208 | } |
paul@354 | 209 | |
paul@354 | 210 | __attr __fn_native_int_int_lt(__attr __args[]) |
paul@354 | 211 | { |
paul@354 | 212 | __attr * const _data = &__args[1]; |
paul@354 | 213 | __attr * const other = &__args[2]; |
paul@354 | 214 | /* _data and other interpreted as int */ |
paul@354 | 215 | int i = _data->intvalue; |
paul@354 | 216 | int j = other->intvalue; |
paul@354 | 217 | |
paul@354 | 218 | /* Return a boolean result. */ |
paul@354 | 219 | return i < j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 220 | } |
paul@354 | 221 | |
paul@354 | 222 | __attr __fn_native_int_int_gt(__attr __args[]) |
paul@354 | 223 | { |
paul@354 | 224 | __attr * const _data = &__args[1]; |
paul@354 | 225 | __attr * const other = &__args[2]; |
paul@354 | 226 | /* _data and other interpreted as int */ |
paul@354 | 227 | int i = _data->intvalue; |
paul@354 | 228 | int j = other->intvalue; |
paul@354 | 229 | |
paul@354 | 230 | /* Return a boolean result. */ |
paul@354 | 231 | return i > j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 232 | } |
paul@354 | 233 | |
paul@354 | 234 | __attr __fn_native_int_int_eq(__attr __args[]) |
paul@354 | 235 | { |
paul@354 | 236 | __attr * const _data = &__args[1]; |
paul@354 | 237 | __attr * const other = &__args[2]; |
paul@354 | 238 | /* _data and other interpreted as int */ |
paul@354 | 239 | int i = _data->intvalue; |
paul@354 | 240 | int j = other->intvalue; |
paul@354 | 241 | |
paul@354 | 242 | /* Return a boolean result. */ |
paul@354 | 243 | return i == j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 244 | } |
paul@354 | 245 | |
paul@354 | 246 | __attr __fn_native_int_int_ne(__attr __args[]) |
paul@354 | 247 | { |
paul@354 | 248 | __attr * const _data = &__args[1]; |
paul@354 | 249 | __attr * const other = &__args[2]; |
paul@354 | 250 | /* _data and other interpreted as int */ |
paul@354 | 251 | int i = _data->intvalue; |
paul@354 | 252 | int j = other->intvalue; |
paul@354 | 253 | |
paul@354 | 254 | /* Return a boolean result. */ |
paul@354 | 255 | return i != j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 256 | } |
paul@354 | 257 | |
paul@354 | 258 | __attr __fn_native_int_int_str(__attr __args[]) |
paul@354 | 259 | { |
paul@354 | 260 | __attr * const _data = &__args[1]; |
paul@354 | 261 | /* _data interpreted as int */ |
paul@354 | 262 | int i = _data->intvalue; |
paul@354 | 263 | /* Employ a buffer big enough to fit the largest integer plus an extra |
paul@354 | 264 | character, a minus sign, and the null terminator. */ |
paul@354 | 265 | unsigned int n = (int) log10(INT_MAX) + 3; |
paul@354 | 266 | char *s = (char *) __ALLOCATE(n, sizeof(char)); |
paul@354 | 267 | |
paul@354 | 268 | snprintf(s, n, "%d", i); |
paul@354 | 269 | |
paul@354 | 270 | /* Return a new string. */ |
paul@354 | 271 | return __new_str(s); |
paul@354 | 272 | } |
paul@354 | 273 | |
paul@354 | 274 | /* Module initialisation. */ |
paul@354 | 275 | |
paul@354 | 276 | void __main_native_int() |
paul@354 | 277 | { |
paul@354 | 278 | } |