paul@353 | 1 | /* Common operations. |
paul@353 | 2 | |
paul@952 | 3 | Copyright (C) 2015-2018, 2021 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@952 | 30 | return (__ref) (attr.rawvalue & (~__TAG_MASK)); |
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@815 | 85 | int __store_via_class__(__ref obj, int pos, __attr value) |
paul@815 | 86 | { |
paul@815 | 87 | return __store_via_object__(__get_class(obj), pos, value); |
paul@815 | 88 | } |
paul@815 | 89 | |
paul@624 | 90 | int __get_class_and_store__(__ref obj, int pos, __attr value) |
paul@252 | 91 | { |
paul@252 | 92 | /* Forbid class-relative assignments. */ |
paul@252 | 93 | |
paul@252 | 94 | __raise_type_error(); |
paul@252 | 95 | return 0; |
paul@252 | 96 | } |
paul@252 | 97 | |
paul@126 | 98 | /* Introspection. */ |
paul@126 | 99 | |
paul@126 | 100 | int __is_instance(__ref obj) |
paul@126 | 101 | { |
paul@126 | 102 | return obj->pos == __INSTANCEPOS; |
paul@126 | 103 | } |
paul@126 | 104 | |
paul@655 | 105 | int __is_subclass(__ref obj, __attr cls) |
paul@655 | 106 | { |
paul@757 | 107 | return __HASATTR(obj, __TYPEPOS(__VALUE(cls)), __TYPECODE(__VALUE(cls))); |
paul@655 | 108 | } |
paul@655 | 109 | |
paul@655 | 110 | int __is_instance_subclass(__ref obj, __attr cls) |
paul@655 | 111 | { |
paul@757 | 112 | return __is_instance(obj) && __HASATTR(__get_class(obj), __TYPEPOS(__VALUE(cls)), __TYPECODE(__VALUE(cls))); |
paul@655 | 113 | } |
paul@655 | 114 | |
paul@274 | 115 | int __is_type_instance(__ref obj) |
paul@274 | 116 | { |
paul@274 | 117 | return __HASATTR(__get_class(obj), __TYPE_CLASS_POS, __TYPE_CLASS_CODE); |
paul@274 | 118 | } |
paul@274 | 119 | |
paul@126 | 120 | __ref __get_class(__ref obj) |
paul@126 | 121 | { |
paul@757 | 122 | return __VALUE(__load_via_object(obj, __class__)); |
paul@126 | 123 | } |
paul@126 | 124 | |
paul@231 | 125 | __attr __get_class_attr(__ref obj) |
paul@231 | 126 | { |
paul@624 | 127 | return __load_via_object(obj, __class__); |
paul@231 | 128 | } |
paul@231 | 129 | |
paul@126 | 130 | /* Attribute testing operations. */ |
paul@126 | 131 | |
paul@144 | 132 | __ref __test_specific_instance(__ref obj, __ref type) |
paul@126 | 133 | { |
paul@144 | 134 | return __get_class(obj) == type ? obj : 0; |
paul@126 | 135 | } |
paul@126 | 136 | |
paul@237 | 137 | __ref __test_specific_object(__ref obj, __ref type) |
paul@237 | 138 | { |
paul@237 | 139 | return __test_specific_type(obj, type) || __test_specific_instance(obj, type) ? obj : 0; |
paul@237 | 140 | } |
paul@237 | 141 | |
paul@237 | 142 | __ref __test_specific_type(__ref obj, __ref type) |
paul@237 | 143 | { |
paul@237 | 144 | return obj == type ? obj : 0; |
paul@237 | 145 | } |
paul@237 | 146 | |
paul@624 | 147 | __ref __test_common_instance__(__ref obj, int pos, int code) |
paul@144 | 148 | { |
paul@144 | 149 | return __HASATTR(__get_class(obj), pos, code) ? obj : 0; |
paul@144 | 150 | } |
paul@144 | 151 | |
paul@624 | 152 | __ref __test_common_object__(__ref obj, int pos, int code) |
paul@126 | 153 | { |
paul@624 | 154 | return __test_common_type__(obj, pos, code) || __test_common_instance__(obj, pos, code) ? obj : 0; |
paul@144 | 155 | } |
paul@144 | 156 | |
paul@624 | 157 | __ref __test_common_type__(__ref obj, int pos, int code) |
paul@144 | 158 | { |
paul@144 | 159 | return __HASATTR(obj, pos, code) ? obj : 0; |
paul@126 | 160 | } |
paul@126 | 161 | |
paul@126 | 162 | /* Attribute testing and retrieval operations. */ |
paul@126 | 163 | |
paul@487 | 164 | __attr __check_and_load_via_object_null(__ref obj, int pos, int code) |
paul@233 | 165 | { |
paul@233 | 166 | if (__HASATTR(obj, pos, code)) |
paul@624 | 167 | return __load_via_object__(obj, pos); |
paul@233 | 168 | else |
paul@233 | 169 | return __NULL; |
paul@233 | 170 | } |
paul@233 | 171 | |
paul@624 | 172 | __attr __check_and_load_via_class__(__ref obj, int pos, int code) |
paul@126 | 173 | { |
paul@624 | 174 | return __check_and_load_via_object__(__get_class(obj), pos, code); |
paul@126 | 175 | } |
paul@126 | 176 | |
paul@624 | 177 | __attr __check_and_load_via_object__(__ref obj, int pos, int code) |
paul@126 | 178 | { |
paul@233 | 179 | if (__HASATTR(obj, pos, code)) |
paul@624 | 180 | return __load_via_object__(obj, pos); |
paul@233 | 181 | |
paul@233 | 182 | __raise_type_error(); |
paul@233 | 183 | return __NULL; |
paul@126 | 184 | } |
paul@126 | 185 | |
paul@624 | 186 | __attr __check_and_load_via_any__(__ref obj, int pos, int code) |
paul@126 | 187 | { |
paul@233 | 188 | __attr out = __check_and_load_via_object_null(obj, pos, code); |
paul@757 | 189 | if (__ISNULL(out)) |
paul@624 | 190 | out = __check_and_load_via_class__(obj, pos, code); |
paul@126 | 191 | return out; |
paul@126 | 192 | } |
paul@126 | 193 | |
paul@126 | 194 | /* Attribute testing and storage operations. */ |
paul@126 | 195 | |
paul@624 | 196 | int __check_and_store_via_class__(__ref obj, int pos, int code, __attr value) |
paul@252 | 197 | { |
paul@252 | 198 | /* Forbid class-relative assignments. */ |
paul@252 | 199 | |
paul@252 | 200 | __raise_type_error(); |
paul@252 | 201 | return 0; |
paul@252 | 202 | } |
paul@252 | 203 | |
paul@624 | 204 | int __check_and_store_via_object__(__ref obj, int pos, int code, __attr value) |
paul@126 | 205 | { |
paul@126 | 206 | if (__HASATTR(obj, pos, code)) |
paul@126 | 207 | { |
paul@624 | 208 | __store_via_object__(obj, pos, value); |
paul@126 | 209 | return 1; |
paul@126 | 210 | } |
paul@252 | 211 | |
paul@252 | 212 | /* No suitable attribute. */ |
paul@252 | 213 | |
paul@252 | 214 | __raise_type_error(); |
paul@126 | 215 | return 0; |
paul@126 | 216 | } |
paul@126 | 217 | |
paul@624 | 218 | int __check_and_store_via_any__(__ref obj, int pos, int code, __attr value) |
paul@126 | 219 | { |
paul@624 | 220 | if (__check_and_store_via_object__(obj, pos, code, value)) |
paul@126 | 221 | return 1; |
paul@252 | 222 | |
paul@252 | 223 | /* Forbid class-relative assignments. */ |
paul@252 | 224 | |
paul@252 | 225 | __raise_type_error(); |
paul@252 | 226 | return 0; |
paul@126 | 227 | } |
paul@126 | 228 | |
paul@126 | 229 | /* Context-related operations. */ |
paul@126 | 230 | |
paul@852 | 231 | int __test_context_update(__attr context, __attr attr, int invoke) |
paul@126 | 232 | { |
paul@594 | 233 | /* Return whether the context should be updated for the attribute. */ |
paul@594 | 234 | |
paul@757 | 235 | __attr attrcontext = __CONTEXT_AS_VALUE(attr); |
paul@757 | 236 | __ref attrcontextvalue = __VALUE(attrcontext); |
paul@577 | 237 | |
paul@267 | 238 | /* Preserve any existing null or instance context. */ |
paul@230 | 239 | |
paul@757 | 240 | if (__ISNULL(attrcontext) || __is_instance(attrcontextvalue)) |
paul@594 | 241 | return 0; |
paul@235 | 242 | |
paul@235 | 243 | /* Test any instance context against the context employed by the |
paul@235 | 244 | attribute. */ |
paul@126 | 245 | |
paul@757 | 246 | if (__is_instance(__VALUE(context))) |
paul@477 | 247 | { |
paul@601 | 248 | /* Obtain the special class attribute position and code identifying the |
paul@601 | 249 | attribute context's class, inspecting the context instance for |
paul@601 | 250 | compatibility. */ |
paul@601 | 251 | |
paul@757 | 252 | if (__test_common_instance__(__VALUE(context), __TYPEPOS(attrcontextvalue), __TYPECODE(attrcontextvalue))) |
paul@594 | 253 | return 1; |
paul@235 | 254 | else |
paul@235 | 255 | __raise_type_error(); |
paul@477 | 256 | } |
paul@235 | 257 | |
paul@852 | 258 | /* Without a null or instance context, an invocation cannot be performed. */ |
paul@852 | 259 | |
paul@852 | 260 | if (invoke) |
paul@852 | 261 | __raise_unbound_method_error(); |
paul@852 | 262 | |
paul@274 | 263 | /* Test for access to a type class attribute using a type instance. */ |
paul@274 | 264 | |
paul@757 | 265 | if (__test_specific_type(attrcontextvalue, &__TYPE_CLASS_TYPE) && __is_type_instance(__VALUE(context))) |
paul@594 | 266 | return 1; |
paul@274 | 267 | |
paul@235 | 268 | /* Otherwise, preserve the attribute as retrieved. */ |
paul@235 | 269 | |
paul@594 | 270 | return 0; |
paul@594 | 271 | } |
paul@594 | 272 | |
paul@757 | 273 | __attr __test_context(__attr context, __attr attr) |
paul@594 | 274 | { |
paul@594 | 275 | /* Update the context or return the unchanged attribute. */ |
paul@594 | 276 | |
paul@852 | 277 | if (__test_context_update(context, attr, 0)) |
paul@594 | 278 | return __update_context(context, attr); |
paul@594 | 279 | else |
paul@594 | 280 | return attr; |
paul@126 | 281 | } |
paul@126 | 282 | |
paul@757 | 283 | __attr __update_context(__attr context, __attr attr) |
paul@126 | 284 | { |
paul@577 | 285 | return __new_wrapper(context, attr); |
paul@126 | 286 | } |
paul@126 | 287 | |
paul@757 | 288 | __attr __test_context_revert(int target, __attr context, __attr attr, __attr contexts[]) |
paul@602 | 289 | { |
paul@602 | 290 | /* Revert the local context to that employed by the attribute if the |
paul@602 | 291 | supplied context is not appropriate. */ |
paul@602 | 292 | |
paul@852 | 293 | if (!__test_context_update(context, attr, 1)) |
paul@757 | 294 | contexts[target] = __CONTEXT_AS_VALUE(attr); |
paul@602 | 295 | return attr; |
paul@602 | 296 | } |
paul@602 | 297 | |
paul@757 | 298 | __attr __test_context_static(int target, __attr context, __ref value, __attr contexts[]) |
paul@602 | 299 | { |
paul@602 | 300 | /* Set the local context to the specified context if appropriate. */ |
paul@602 | 301 | |
paul@852 | 302 | if (__test_context_update(context, __ATTRVALUE(value), 1)) |
paul@602 | 303 | contexts[target] = context; |
paul@713 | 304 | return __ATTRVALUE(value); |
paul@602 | 305 | } |
paul@602 | 306 | |
paul@523 | 307 | /* Context testing for invocations. */ |
paul@523 | 308 | |
paul@757 | 309 | int __type_method_invocation(__attr context, __attr target) |
paul@523 | 310 | { |
paul@757 | 311 | __attr targetcontext = __CONTEXT_AS_VALUE(target); |
paul@523 | 312 | |
paul@523 | 313 | /* Require instances, not classes, where methods are function instances. */ |
paul@523 | 314 | |
paul@757 | 315 | if (!__is_instance(__VALUE(target))) |
paul@523 | 316 | return 0; |
paul@523 | 317 | |
paul@577 | 318 | /* Access the context of the callable and test if it is the type object. */ |
paul@523 | 319 | |
paul@757 | 320 | return (!__ISNULL(targetcontext) && __test_specific_type(__VALUE(targetcontext), &__TYPE_CLASS_TYPE) && __is_type_instance(__VALUE(context))); |
paul@523 | 321 | } |
paul@523 | 322 | |
paul@577 | 323 | __attr __unwrap_callable(__attr callable) |
paul@523 | 324 | { |
paul@757 | 325 | __attr value = __check_and_load_via_object_null(__VALUE(callable), __ATTRPOS(__value__), __ATTRCODE(__value__)); |
paul@757 | 326 | return __VALUE(value) ? value : callable; |
paul@577 | 327 | } |
paul@577 | 328 | |
paul@763 | 329 | __attr (*__get_function_unchecked(__attr target))() |
paul@746 | 330 | { |
paul@757 | 331 | return __load_via_object(__VALUE(__unwrap_callable(target)), __fn__).fn; |
paul@746 | 332 | } |
paul@746 | 333 | |
paul@763 | 334 | __attr (*__get_function(__attr context, __attr target))() |
paul@577 | 335 | { |
paul@759 | 336 | return __get_function_unwrapped(context, __unwrap_callable(target)); |
paul@759 | 337 | } |
paul@663 | 338 | |
paul@768 | 339 | __attr (*__get_function_unwrapped(__attr context, __attr target))() |
paul@759 | 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@776 | 345 | return __get_function_member(target); |
paul@523 | 346 | else |
paul@577 | 347 | return __unbound_method; |
paul@523 | 348 | } |
paul@523 | 349 | |
paul@776 | 350 | __attr (*__get_function_member(__attr target))() |
paul@776 | 351 | { |
paul@776 | 352 | return __load_via_object(__VALUE(target), __fn__).fn; |
paul@776 | 353 | } |
paul@776 | 354 | |
paul@763 | 355 | __attr (*__check_and_get_function(__attr context, __attr target))() |
paul@523 | 356 | { |
paul@759 | 357 | return __check_and_get_function_unwrapped(context, __unwrap_callable(target)); |
paul@759 | 358 | } |
paul@663 | 359 | |
paul@768 | 360 | __attr (*__check_and_get_function_unwrapped(__attr context, __attr target))() |
paul@759 | 361 | { |
paul@523 | 362 | /* Require null or instance contexts for functions and methods respectively, |
paul@523 | 363 | or type instance contexts for type methods. */ |
paul@523 | 364 | |
paul@757 | 365 | if (__ISNULL(context) || __is_instance(__VALUE(context)) || __type_method_invocation(context, target)) |
paul@757 | 366 | return __check_and_load_via_object__(__VALUE(target), __ATTRPOS(__fn__), __ATTRCODE(__fn__)).fn; |
paul@523 | 367 | else |
paul@577 | 368 | return __unbound_method; |
paul@523 | 369 | } |
paul@523 | 370 | |
paul@126 | 371 | /* Parameter position operations. */ |
paul@126 | 372 | |
paul@126 | 373 | int __HASPARAM(const __ptable *ptable, int ppos, int pcode) |
paul@126 | 374 | { |
paul@126 | 375 | __param param; |
paul@126 | 376 | |
paul@126 | 377 | if (ppos < ptable->size) |
paul@126 | 378 | { |
paul@126 | 379 | param = ptable->params[ppos]; |
paul@126 | 380 | if (param.code == pcode) |
paul@126 | 381 | return param.pos; |
paul@126 | 382 | } |
paul@126 | 383 | |
paul@126 | 384 | return -1; |
paul@126 | 385 | } |
paul@126 | 386 | |
paul@126 | 387 | /* Conversions. */ |
paul@126 | 388 | |
paul@126 | 389 | __attr __CONTEXT_AS_VALUE(__attr attr) |
paul@126 | 390 | { |
paul@757 | 391 | return __check_and_load_via_object_null(__VALUE(attr), __ATTRPOS(__context__), __ATTRCODE(__context__)); |
paul@126 | 392 | } |
paul@126 | 393 | |
paul@126 | 394 | /* Type testing. */ |
paul@126 | 395 | |
paul@144 | 396 | __ref __ISFUNC(__ref obj) |
paul@126 | 397 | { |
paul@126 | 398 | return __test_specific_instance(obj, &__FUNCTION_TYPE); |
paul@126 | 399 | } |
paul@126 | 400 | |
paul@126 | 401 | /* Attribute codes and positions for type objects. */ |
paul@126 | 402 | |
paul@126 | 403 | unsigned int __TYPECODE(__ref obj) |
paul@126 | 404 | { |
paul@126 | 405 | return obj->table->attrs[obj->pos]; |
paul@126 | 406 | } |
paul@126 | 407 | |
paul@126 | 408 | unsigned int __TYPEPOS(__ref obj) |
paul@126 | 409 | { |
paul@126 | 410 | return obj->pos; |
paul@126 | 411 | } |
paul@151 | 412 | |
paul@260 | 413 | /* Memory allocation. */ |
paul@260 | 414 | |
paul@260 | 415 | void *__ALLOCATE(size_t nmemb, size_t size) |
paul@260 | 416 | { |
paul@433 | 417 | void *ptr = GC_MALLOC(nmemb * size); /* sets memory to zero */ |
paul@260 | 418 | if (ptr == NULL) |
paul@260 | 419 | __raise_memory_error(); |
paul@260 | 420 | return ptr; |
paul@260 | 421 | } |
paul@260 | 422 | |
paul@756 | 423 | void *__ALLOCATEIM(size_t nmemb, size_t size) |
paul@756 | 424 | { |
paul@756 | 425 | void *ptr = GC_MALLOC_ATOMIC(nmemb * size); /* sets memory to zero */ |
paul@756 | 426 | if (ptr == NULL) |
paul@756 | 427 | __raise_memory_error(); |
paul@756 | 428 | return ptr; |
paul@756 | 429 | } |
paul@756 | 430 | |
paul@260 | 431 | void *__REALLOCATE(void *ptr, size_t size) |
paul@260 | 432 | { |
paul@433 | 433 | void *nptr = GC_REALLOC(ptr, size); |
paul@260 | 434 | if (nptr == NULL) |
paul@260 | 435 | __raise_memory_error(); |
paul@260 | 436 | return nptr; |
paul@260 | 437 | } |
paul@260 | 438 | |
paul@151 | 439 | /* Copying of structures. */ |
paul@151 | 440 | |
paul@151 | 441 | __ref __COPY(__ref obj, int size) |
paul@151 | 442 | { |
paul@260 | 443 | __ref copy = (__ref) __ALLOCATE(1, size); |
paul@151 | 444 | memcpy(copy, obj, size); |
paul@151 | 445 | return copy; |
paul@151 | 446 | } |