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