paul@353 | 1 | /* Common operations. |
paul@353 | 2 | |
paul@523 | 3 | Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@353 | 4 | |
paul@353 | 5 | This program is free software; you can redistribute it and/or modify it under |
paul@353 | 6 | the terms of the GNU General Public License as published by the Free Software |
paul@353 | 7 | Foundation; either version 3 of the License, or (at your option) any later |
paul@353 | 8 | version. |
paul@353 | 9 | |
paul@353 | 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@353 | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@353 | 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@353 | 13 | details. |
paul@353 | 14 | |
paul@353 | 15 | You should have received a copy of the GNU General Public License along with |
paul@353 | 16 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@353 | 17 | */ |
paul@126 | 18 | |
paul@433 | 19 | #include "gc.h" /* GC_MALLOC, GC_REALLOC */ |
paul@649 | 20 | #include "types.h" |
paul@126 | 21 | #include "ops.h" |
paul@284 | 22 | #include "progops.h" /* for raising errors */ |
paul@126 | 23 | #include "progconsts.h" |
paul@126 | 24 | #include "progtypes.h" |
paul@126 | 25 | |
paul@757 | 26 | /* Get object reference from attribute. */ |
paul@757 | 27 | |
paul@757 | 28 | __ref __VALUE(__attr attr) |
paul@757 | 29 | { |
paul@757 | 30 | return attr.value; |
paul@757 | 31 | } |
paul@757 | 32 | |
paul@655 | 33 | /* Basic structure tests. */ |
paul@655 | 34 | |
paul@655 | 35 | static inline int __HASATTR(__ref obj, int pos, int code) |
paul@655 | 36 | { |
paul@662 | 37 | return (pos < obj->table->size) && (obj->table->attrs[pos] == code); |
paul@655 | 38 | } |
paul@655 | 39 | |
paul@126 | 40 | /* Direct access and manipulation of static objects. */ |
paul@126 | 41 | |
paul@577 | 42 | __attr __load_static_ignore(__ref obj) |
paul@577 | 43 | { |
paul@713 | 44 | return __ATTRVALUE(obj); |
paul@577 | 45 | } |
paul@577 | 46 | |
paul@757 | 47 | __attr __load_static_replace(__attr context, __ref obj) |
paul@126 | 48 | { |
paul@713 | 49 | return __update_context(context, __ATTRVALUE(obj)); |
paul@577 | 50 | } |
paul@577 | 51 | |
paul@757 | 52 | __attr __load_static_test(__attr context, __ref obj) |
paul@577 | 53 | { |
paul@713 | 54 | return __test_context(context, __ATTRVALUE(obj)); |
paul@126 | 55 | } |
paul@126 | 56 | |
paul@126 | 57 | /* Direct retrieval operations, returning and setting attributes. */ |
paul@126 | 58 | |
paul@624 | 59 | __attr __load_via_object__(__ref obj, int pos) |
paul@126 | 60 | { |
paul@126 | 61 | return obj->attrs[pos]; |
paul@126 | 62 | } |
paul@126 | 63 | |
paul@624 | 64 | __attr __load_via_class__(__ref obj, int pos) |
paul@126 | 65 | { |
paul@624 | 66 | return __load_via_object__(__get_class(obj), pos); |
paul@126 | 67 | } |
paul@126 | 68 | |
paul@624 | 69 | __attr __get_class_and_load__(__ref obj, int pos) |
paul@126 | 70 | { |
paul@153 | 71 | if (__is_instance(obj)) |
paul@624 | 72 | return __load_via_class__(obj, pos); |
paul@153 | 73 | else |
paul@624 | 74 | return __load_via_object__(obj, pos); |
paul@126 | 75 | } |
paul@126 | 76 | |
paul@126 | 77 | /* Direct storage operations. */ |
paul@126 | 78 | |
paul@624 | 79 | int __store_via_object__(__ref obj, int pos, __attr value) |
paul@126 | 80 | { |
paul@126 | 81 | obj->attrs[pos] = value; |
paul@126 | 82 | return 1; |
paul@126 | 83 | } |
paul@126 | 84 | |
paul@624 | 85 | int __get_class_and_store__(__ref obj, int pos, __attr value) |
paul@252 | 86 | { |
paul@252 | 87 | /* Forbid class-relative assignments. */ |
paul@252 | 88 | |
paul@252 | 89 | __raise_type_error(); |
paul@252 | 90 | return 0; |
paul@252 | 91 | } |
paul@252 | 92 | |
paul@126 | 93 | /* Introspection. */ |
paul@126 | 94 | |
paul@126 | 95 | int __is_instance(__ref obj) |
paul@126 | 96 | { |
paul@126 | 97 | return obj->pos == __INSTANCEPOS; |
paul@126 | 98 | } |
paul@126 | 99 | |
paul@655 | 100 | int __is_subclass(__ref obj, __attr cls) |
paul@655 | 101 | { |
paul@757 | 102 | return __HASATTR(obj, __TYPEPOS(__VALUE(cls)), __TYPECODE(__VALUE(cls))); |
paul@655 | 103 | } |
paul@655 | 104 | |
paul@655 | 105 | int __is_instance_subclass(__ref obj, __attr cls) |
paul@655 | 106 | { |
paul@757 | 107 | return __is_instance(obj) && __HASATTR(__get_class(obj), __TYPEPOS(__VALUE(cls)), __TYPECODE(__VALUE(cls))); |
paul@655 | 108 | } |
paul@655 | 109 | |
paul@274 | 110 | int __is_type_instance(__ref obj) |
paul@274 | 111 | { |
paul@274 | 112 | return __HASATTR(__get_class(obj), __TYPE_CLASS_POS, __TYPE_CLASS_CODE); |
paul@274 | 113 | } |
paul@274 | 114 | |
paul@126 | 115 | __ref __get_class(__ref obj) |
paul@126 | 116 | { |
paul@757 | 117 | return __VALUE(__load_via_object(obj, __class__)); |
paul@126 | 118 | } |
paul@126 | 119 | |
paul@231 | 120 | __attr __get_class_attr(__ref obj) |
paul@231 | 121 | { |
paul@624 | 122 | return __load_via_object(obj, __class__); |
paul@231 | 123 | } |
paul@231 | 124 | |
paul@126 | 125 | /* Attribute testing operations. */ |
paul@126 | 126 | |
paul@144 | 127 | __ref __test_specific_instance(__ref obj, __ref type) |
paul@126 | 128 | { |
paul@144 | 129 | return __get_class(obj) == type ? obj : 0; |
paul@126 | 130 | } |
paul@126 | 131 | |
paul@237 | 132 | __ref __test_specific_object(__ref obj, __ref type) |
paul@237 | 133 | { |
paul@237 | 134 | return __test_specific_type(obj, type) || __test_specific_instance(obj, type) ? obj : 0; |
paul@237 | 135 | } |
paul@237 | 136 | |
paul@237 | 137 | __ref __test_specific_type(__ref obj, __ref type) |
paul@237 | 138 | { |
paul@237 | 139 | return obj == type ? obj : 0; |
paul@237 | 140 | } |
paul@237 | 141 | |
paul@624 | 142 | __ref __test_common_instance__(__ref obj, int pos, int code) |
paul@144 | 143 | { |
paul@144 | 144 | return __HASATTR(__get_class(obj), pos, code) ? obj : 0; |
paul@144 | 145 | } |
paul@144 | 146 | |
paul@624 | 147 | __ref __test_common_object__(__ref obj, int pos, int code) |
paul@126 | 148 | { |
paul@624 | 149 | return __test_common_type__(obj, pos, code) || __test_common_instance__(obj, pos, code) ? obj : 0; |
paul@144 | 150 | } |
paul@144 | 151 | |
paul@624 | 152 | __ref __test_common_type__(__ref obj, int pos, int code) |
paul@144 | 153 | { |
paul@144 | 154 | return __HASATTR(obj, pos, code) ? obj : 0; |
paul@126 | 155 | } |
paul@126 | 156 | |
paul@126 | 157 | /* Attribute testing and retrieval operations. */ |
paul@126 | 158 | |
paul@487 | 159 | __attr __check_and_load_via_object_null(__ref obj, int pos, int code) |
paul@233 | 160 | { |
paul@233 | 161 | if (__HASATTR(obj, pos, code)) |
paul@624 | 162 | return __load_via_object__(obj, pos); |
paul@233 | 163 | else |
paul@233 | 164 | return __NULL; |
paul@233 | 165 | } |
paul@233 | 166 | |
paul@624 | 167 | __attr __check_and_load_via_class__(__ref obj, int pos, int code) |
paul@126 | 168 | { |
paul@624 | 169 | return __check_and_load_via_object__(__get_class(obj), pos, code); |
paul@126 | 170 | } |
paul@126 | 171 | |
paul@624 | 172 | __attr __check_and_load_via_object__(__ref obj, int pos, int code) |
paul@126 | 173 | { |
paul@233 | 174 | if (__HASATTR(obj, pos, code)) |
paul@624 | 175 | return __load_via_object__(obj, pos); |
paul@233 | 176 | |
paul@233 | 177 | __raise_type_error(); |
paul@233 | 178 | return __NULL; |
paul@126 | 179 | } |
paul@126 | 180 | |
paul@624 | 181 | __attr __check_and_load_via_any__(__ref obj, int pos, int code) |
paul@126 | 182 | { |
paul@233 | 183 | __attr out = __check_and_load_via_object_null(obj, pos, code); |
paul@757 | 184 | if (__ISNULL(out)) |
paul@624 | 185 | out = __check_and_load_via_class__(obj, pos, code); |
paul@126 | 186 | return out; |
paul@126 | 187 | } |
paul@126 | 188 | |
paul@126 | 189 | /* Attribute testing and storage operations. */ |
paul@126 | 190 | |
paul@624 | 191 | int __check_and_store_via_class__(__ref obj, int pos, int code, __attr value) |
paul@252 | 192 | { |
paul@252 | 193 | /* Forbid class-relative assignments. */ |
paul@252 | 194 | |
paul@252 | 195 | __raise_type_error(); |
paul@252 | 196 | return 0; |
paul@252 | 197 | } |
paul@252 | 198 | |
paul@624 | 199 | int __check_and_store_via_object__(__ref obj, int pos, int code, __attr value) |
paul@126 | 200 | { |
paul@126 | 201 | if (__HASATTR(obj, pos, code)) |
paul@126 | 202 | { |
paul@624 | 203 | __store_via_object__(obj, pos, value); |
paul@126 | 204 | return 1; |
paul@126 | 205 | } |
paul@252 | 206 | |
paul@252 | 207 | /* No suitable attribute. */ |
paul@252 | 208 | |
paul@252 | 209 | __raise_type_error(); |
paul@126 | 210 | return 0; |
paul@126 | 211 | } |
paul@126 | 212 | |
paul@624 | 213 | int __check_and_store_via_any__(__ref obj, int pos, int code, __attr value) |
paul@126 | 214 | { |
paul@624 | 215 | if (__check_and_store_via_object__(obj, pos, code, value)) |
paul@126 | 216 | return 1; |
paul@252 | 217 | |
paul@252 | 218 | /* Forbid class-relative assignments. */ |
paul@252 | 219 | |
paul@252 | 220 | __raise_type_error(); |
paul@252 | 221 | return 0; |
paul@126 | 222 | } |
paul@126 | 223 | |
paul@126 | 224 | /* Context-related operations. */ |
paul@126 | 225 | |
paul@757 | 226 | int __test_context_update(__attr context, __attr attr) |
paul@126 | 227 | { |
paul@594 | 228 | /* Return whether the context should be updated for the attribute. */ |
paul@594 | 229 | |
paul@757 | 230 | __attr attrcontext = __CONTEXT_AS_VALUE(attr); |
paul@757 | 231 | __ref attrcontextvalue = __VALUE(attrcontext); |
paul@577 | 232 | |
paul@267 | 233 | /* Preserve any existing null or instance context. */ |
paul@230 | 234 | |
paul@757 | 235 | if (__ISNULL(attrcontext) || __is_instance(attrcontextvalue)) |
paul@594 | 236 | return 0; |
paul@235 | 237 | |
paul@235 | 238 | /* Test any instance context against the context employed by the |
paul@235 | 239 | attribute. */ |
paul@126 | 240 | |
paul@757 | 241 | if (__is_instance(__VALUE(context))) |
paul@477 | 242 | { |
paul@601 | 243 | /* Obtain the special class attribute position and code identifying the |
paul@601 | 244 | attribute context's class, inspecting the context instance for |
paul@601 | 245 | compatibility. */ |
paul@601 | 246 | |
paul@757 | 247 | if (__test_common_instance__(__VALUE(context), __TYPEPOS(attrcontextvalue), __TYPECODE(attrcontextvalue))) |
paul@594 | 248 | return 1; |
paul@235 | 249 | else |
paul@235 | 250 | __raise_type_error(); |
paul@477 | 251 | } |
paul@235 | 252 | |
paul@274 | 253 | /* Test for access to a type class attribute using a type instance. */ |
paul@274 | 254 | |
paul@757 | 255 | if (__test_specific_type(attrcontextvalue, &__TYPE_CLASS_TYPE) && __is_type_instance(__VALUE(context))) |
paul@594 | 256 | return 1; |
paul@274 | 257 | |
paul@235 | 258 | /* Otherwise, preserve the attribute as retrieved. */ |
paul@235 | 259 | |
paul@594 | 260 | return 0; |
paul@594 | 261 | } |
paul@594 | 262 | |
paul@757 | 263 | __attr __test_context(__attr context, __attr attr) |
paul@594 | 264 | { |
paul@594 | 265 | /* Update the context or return the unchanged attribute. */ |
paul@594 | 266 | |
paul@594 | 267 | if (__test_context_update(context, attr)) |
paul@594 | 268 | return __update_context(context, attr); |
paul@594 | 269 | else |
paul@594 | 270 | return attr; |
paul@126 | 271 | } |
paul@126 | 272 | |
paul@757 | 273 | __attr __update_context(__attr context, __attr attr) |
paul@126 | 274 | { |
paul@577 | 275 | return __new_wrapper(context, attr); |
paul@126 | 276 | } |
paul@126 | 277 | |
paul@757 | 278 | __attr __test_context_revert(int target, __attr context, __attr attr, __attr contexts[]) |
paul@602 | 279 | { |
paul@602 | 280 | /* Revert the local context to that employed by the attribute if the |
paul@602 | 281 | supplied context is not appropriate. */ |
paul@602 | 282 | |
paul@602 | 283 | if (!__test_context_update(context, attr)) |
paul@757 | 284 | contexts[target] = __CONTEXT_AS_VALUE(attr); |
paul@602 | 285 | return attr; |
paul@602 | 286 | } |
paul@602 | 287 | |
paul@757 | 288 | __attr __test_context_static(int target, __attr context, __ref value, __attr contexts[]) |
paul@602 | 289 | { |
paul@602 | 290 | /* Set the local context to the specified context if appropriate. */ |
paul@602 | 291 | |
paul@713 | 292 | if (__test_context_update(context, __ATTRVALUE(value))) |
paul@602 | 293 | contexts[target] = context; |
paul@713 | 294 | return __ATTRVALUE(value); |
paul@602 | 295 | } |
paul@602 | 296 | |
paul@523 | 297 | /* Context testing for invocations. */ |
paul@523 | 298 | |
paul@757 | 299 | int __type_method_invocation(__attr context, __attr target) |
paul@523 | 300 | { |
paul@757 | 301 | __attr targetcontext = __CONTEXT_AS_VALUE(target); |
paul@523 | 302 | |
paul@523 | 303 | /* Require instances, not classes, where methods are function instances. */ |
paul@523 | 304 | |
paul@757 | 305 | if (!__is_instance(__VALUE(target))) |
paul@523 | 306 | return 0; |
paul@523 | 307 | |
paul@577 | 308 | /* Access the context of the callable and test if it is the type object. */ |
paul@523 | 309 | |
paul@757 | 310 | return (!__ISNULL(targetcontext) && __test_specific_type(__VALUE(targetcontext), &__TYPE_CLASS_TYPE) && __is_type_instance(__VALUE(context))); |
paul@523 | 311 | } |
paul@523 | 312 | |
paul@577 | 313 | __attr __unwrap_callable(__attr callable) |
paul@523 | 314 | { |
paul@757 | 315 | __attr value = __check_and_load_via_object_null(__VALUE(callable), __ATTRPOS(__value__), __ATTRCODE(__value__)); |
paul@757 | 316 | return __VALUE(value) ? value : callable; |
paul@577 | 317 | } |
paul@577 | 318 | |
paul@746 | 319 | __attr (*__get_function_unchecked(__attr target))(__attr[]) |
paul@746 | 320 | { |
paul@757 | 321 | return __load_via_object(__VALUE(__unwrap_callable(target)), __fn__).fn; |
paul@746 | 322 | } |
paul@746 | 323 | |
paul@757 | 324 | __attr (*__get_function(__attr context, __attr target))(__attr[]) |
paul@577 | 325 | { |
paul@663 | 326 | target = __unwrap_callable(target); |
paul@663 | 327 | |
paul@523 | 328 | /* Require null or instance contexts for functions and methods respectively, |
paul@523 | 329 | or type instance contexts for type methods. */ |
paul@523 | 330 | |
paul@757 | 331 | if (__ISNULL(context) || __is_instance(__VALUE(context)) || __type_method_invocation(context, target)) |
paul@757 | 332 | return __load_via_object(__VALUE(target), __fn__).fn; |
paul@523 | 333 | else |
paul@577 | 334 | return __unbound_method; |
paul@523 | 335 | } |
paul@523 | 336 | |
paul@757 | 337 | __attr (*__check_and_get_function(__attr context, __attr target))(__attr[]) |
paul@523 | 338 | { |
paul@663 | 339 | target = __unwrap_callable(target); |
paul@663 | 340 | |
paul@523 | 341 | /* Require null or instance contexts for functions and methods respectively, |
paul@523 | 342 | or type instance contexts for type methods. */ |
paul@523 | 343 | |
paul@757 | 344 | if (__ISNULL(context) || __is_instance(__VALUE(context)) || __type_method_invocation(context, target)) |
paul@757 | 345 | return __check_and_load_via_object__(__VALUE(target), __ATTRPOS(__fn__), __ATTRCODE(__fn__)).fn; |
paul@523 | 346 | else |
paul@577 | 347 | return __unbound_method; |
paul@523 | 348 | } |
paul@523 | 349 | |
paul@126 | 350 | /* Parameter position operations. */ |
paul@126 | 351 | |
paul@126 | 352 | int __HASPARAM(const __ptable *ptable, int ppos, int pcode) |
paul@126 | 353 | { |
paul@126 | 354 | __param param; |
paul@126 | 355 | |
paul@126 | 356 | if (ppos < ptable->size) |
paul@126 | 357 | { |
paul@126 | 358 | param = ptable->params[ppos]; |
paul@126 | 359 | if (param.code == pcode) |
paul@126 | 360 | return param.pos; |
paul@126 | 361 | } |
paul@126 | 362 | |
paul@126 | 363 | return -1; |
paul@126 | 364 | } |
paul@126 | 365 | |
paul@126 | 366 | /* Conversions. */ |
paul@126 | 367 | |
paul@126 | 368 | __attr __CONTEXT_AS_VALUE(__attr attr) |
paul@126 | 369 | { |
paul@757 | 370 | return __check_and_load_via_object_null(__VALUE(attr), __ATTRPOS(__context__), __ATTRCODE(__context__)); |
paul@126 | 371 | } |
paul@126 | 372 | |
paul@126 | 373 | /* Type testing. */ |
paul@126 | 374 | |
paul@144 | 375 | __ref __ISFUNC(__ref obj) |
paul@126 | 376 | { |
paul@126 | 377 | return __test_specific_instance(obj, &__FUNCTION_TYPE); |
paul@126 | 378 | } |
paul@126 | 379 | |
paul@126 | 380 | int __ISNULL(__attr value) |
paul@126 | 381 | { |
paul@143 | 382 | return (value.value == 0); /* __NULL.value */ |
paul@126 | 383 | } |
paul@126 | 384 | |
paul@126 | 385 | /* Attribute codes and positions for type objects. */ |
paul@126 | 386 | |
paul@126 | 387 | unsigned int __TYPECODE(__ref obj) |
paul@126 | 388 | { |
paul@126 | 389 | return obj->table->attrs[obj->pos]; |
paul@126 | 390 | } |
paul@126 | 391 | |
paul@126 | 392 | unsigned int __TYPEPOS(__ref obj) |
paul@126 | 393 | { |
paul@126 | 394 | return obj->pos; |
paul@126 | 395 | } |
paul@151 | 396 | |
paul@260 | 397 | /* Memory allocation. */ |
paul@260 | 398 | |
paul@260 | 399 | void *__ALLOCATE(size_t nmemb, size_t size) |
paul@260 | 400 | { |
paul@433 | 401 | void *ptr = GC_MALLOC(nmemb * size); /* sets memory to zero */ |
paul@260 | 402 | if (ptr == NULL) |
paul@260 | 403 | __raise_memory_error(); |
paul@260 | 404 | return ptr; |
paul@260 | 405 | } |
paul@260 | 406 | |
paul@756 | 407 | void *__ALLOCATEIM(size_t nmemb, size_t size) |
paul@756 | 408 | { |
paul@756 | 409 | void *ptr = GC_MALLOC_ATOMIC(nmemb * size); /* sets memory to zero */ |
paul@756 | 410 | if (ptr == NULL) |
paul@756 | 411 | __raise_memory_error(); |
paul@756 | 412 | return ptr; |
paul@756 | 413 | } |
paul@756 | 414 | |
paul@260 | 415 | void *__REALLOCATE(void *ptr, size_t size) |
paul@260 | 416 | { |
paul@433 | 417 | void *nptr = GC_REALLOC(ptr, size); |
paul@260 | 418 | if (nptr == NULL) |
paul@260 | 419 | __raise_memory_error(); |
paul@260 | 420 | return nptr; |
paul@260 | 421 | } |
paul@260 | 422 | |
paul@151 | 423 | /* Copying of structures. */ |
paul@151 | 424 | |
paul@151 | 425 | __ref __COPY(__ref obj, int size) |
paul@151 | 426 | { |
paul@260 | 427 | __ref copy = (__ref) __ALLOCATE(1, size); |
paul@151 | 428 | memcpy(copy, obj, size); |
paul@151 | 429 | return copy; |
paul@151 | 430 | } |