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@964 | 40 | /* Generic load wrapper employing temporary stack storage. */ |
paul@964 | 41 | |
paul@964 | 42 | __attr __load(__attr value) |
paul@964 | 43 | { |
paul@964 | 44 | /* Copy values where appropriate. */ |
paul@964 | 45 | |
paul@964 | 46 | if (__COPYABLE(value)) |
paul@964 | 47 | { |
paul@964 | 48 | size_t value_size = __INSTANCE_SIZE(__VALUE(value)); |
paul@964 | 49 | __attr target = __stack_allocate(__stack, value_size); |
paul@964 | 50 | |
paul@964 | 51 | __COPY_TO(__VALUE(value), __VALUE(target), value_size); |
paul@964 | 52 | return __TO_MUTABLE(target); |
paul@964 | 53 | } |
paul@964 | 54 | else |
paul@964 | 55 | return value; |
paul@964 | 56 | } |
paul@964 | 57 | |
paul@126 | 58 | /* Direct access and manipulation of static objects. */ |
paul@126 | 59 | |
paul@577 | 60 | __attr __load_static_ignore(__ref obj) |
paul@577 | 61 | { |
paul@713 | 62 | return __ATTRVALUE(obj); |
paul@577 | 63 | } |
paul@577 | 64 | |
paul@757 | 65 | __attr __load_static_replace(__attr context, __ref obj) |
paul@126 | 66 | { |
paul@713 | 67 | return __update_context(context, __ATTRVALUE(obj)); |
paul@577 | 68 | } |
paul@577 | 69 | |
paul@757 | 70 | __attr __load_static_test(__attr context, __ref obj) |
paul@577 | 71 | { |
paul@713 | 72 | return __test_context(context, __ATTRVALUE(obj)); |
paul@126 | 73 | } |
paul@126 | 74 | |
paul@126 | 75 | /* Direct retrieval operations, returning and setting attributes. */ |
paul@126 | 76 | |
paul@624 | 77 | __attr __load_via_object__(__ref obj, int pos) |
paul@126 | 78 | { |
paul@126 | 79 | return obj->attrs[pos]; |
paul@126 | 80 | } |
paul@126 | 81 | |
paul@624 | 82 | __attr __load_via_class__(__ref obj, int pos) |
paul@126 | 83 | { |
paul@624 | 84 | return __load_via_object__(__get_class(obj), pos); |
paul@126 | 85 | } |
paul@126 | 86 | |
paul@624 | 87 | __attr __get_class_and_load__(__ref obj, int pos) |
paul@126 | 88 | { |
paul@153 | 89 | if (__is_instance(obj)) |
paul@624 | 90 | return __load_via_class__(obj, pos); |
paul@153 | 91 | else |
paul@624 | 92 | return __load_via_object__(obj, pos); |
paul@126 | 93 | } |
paul@126 | 94 | |
paul@126 | 95 | /* Direct storage operations. */ |
paul@126 | 96 | |
paul@964 | 97 | __attr __store_local(__attr target, __attr value) |
paul@964 | 98 | { |
paul@964 | 99 | /* Copy values where appropriate. */ |
paul@964 | 100 | |
paul@964 | 101 | if (__COPYABLE(value)) |
paul@964 | 102 | { |
paul@964 | 103 | size_t value_size = __INSTANCE_SIZE(__VALUE(value)); |
paul@964 | 104 | |
paul@964 | 105 | /* Allocate new space for values if the target is not a mutable value |
paul@964 | 106 | or refers to an object that is too small for the value. */ |
paul@964 | 107 | |
paul@964 | 108 | if ((__VALUE(target) == NULL) || !__MUTABLE(target) || (value_size > __INSTANCE_SIZE(__VALUE(target)))) |
paul@964 | 109 | target = __stack_allocate(__stack, value_size); |
paul@964 | 110 | |
paul@964 | 111 | __COPY_TO(__VALUE(value), __VALUE(target), value_size); |
paul@964 | 112 | return __TO_MUTABLE(target); |
paul@964 | 113 | } |
paul@964 | 114 | else |
paul@964 | 115 | return value; |
paul@964 | 116 | } |
paul@964 | 117 | |
paul@964 | 118 | void __store_target(__attr *target, __attr value) |
paul@964 | 119 | { |
paul@964 | 120 | /* Copy values where appropriate. */ |
paul@964 | 121 | |
paul@964 | 122 | if (__COPYABLE(value)) |
paul@964 | 123 | { |
paul@964 | 124 | size_t value_size = __INSTANCE_SIZE(__VALUE(value)); |
paul@964 | 125 | |
paul@964 | 126 | if ((__VALUE(*target) == NULL) || (!__MUTABLE(*target)) || (value_size > __INSTANCE_SIZE(__VALUE(*target)))) |
paul@964 | 127 | *target = __ATTRVALUE(__ALLOCATEIM(1, value_size)); |
paul@964 | 128 | |
paul@964 | 129 | __COPY_TO(__VALUE(value), __VALUE(*target), value_size); |
paul@964 | 130 | *target = __TO_MUTABLE(*target); |
paul@964 | 131 | } |
paul@964 | 132 | else |
paul@964 | 133 | *target = value; |
paul@964 | 134 | } |
paul@964 | 135 | |
paul@624 | 136 | int __store_via_object__(__ref obj, int pos, __attr value) |
paul@126 | 137 | { |
paul@964 | 138 | /* Copy values where appropriate. */ |
paul@964 | 139 | |
paul@964 | 140 | if (__COPYABLE(value)) |
paul@964 | 141 | { |
paul@964 | 142 | size_t value_size = __INSTANCE_SIZE(__VALUE(value)); |
paul@964 | 143 | __attr target = obj->attrs[pos]; |
paul@964 | 144 | |
paul@964 | 145 | /* Allocate new space for values if the target is not a mutable value |
paul@964 | 146 | or refers to an object that is too small for the value. */ |
paul@964 | 147 | |
paul@964 | 148 | if ((__VALUE(target) == NULL) || (!__MUTABLE(target)) || (value_size > __INSTANCE_SIZE(__VALUE(target)))) |
paul@964 | 149 | target = __ATTRVALUE(__ALLOCATEIM(1, value_size)); |
paul@964 | 150 | |
paul@964 | 151 | __COPY_TO(__VALUE(value), __VALUE(target), value_size); |
paul@964 | 152 | obj->attrs[pos] = __TO_MUTABLE(target); |
paul@964 | 153 | } |
paul@964 | 154 | else |
paul@964 | 155 | obj->attrs[pos] = value; |
paul@964 | 156 | |
paul@964 | 157 | return 1; |
paul@964 | 158 | } |
paul@964 | 159 | |
paul@964 | 160 | int __store_via_object_internal__(__ref obj, int pos, __attr value) |
paul@964 | 161 | { |
paul@126 | 162 | obj->attrs[pos] = value; |
paul@126 | 163 | return 1; |
paul@126 | 164 | } |
paul@126 | 165 | |
paul@815 | 166 | int __store_via_class__(__ref obj, int pos, __attr value) |
paul@815 | 167 | { |
paul@815 | 168 | return __store_via_object__(__get_class(obj), pos, value); |
paul@815 | 169 | } |
paul@815 | 170 | |
paul@624 | 171 | int __get_class_and_store__(__ref obj, int pos, __attr value) |
paul@252 | 172 | { |
paul@252 | 173 | /* Forbid class-relative assignments. */ |
paul@252 | 174 | |
paul@252 | 175 | __raise_type_error(); |
paul@252 | 176 | return 0; |
paul@252 | 177 | } |
paul@252 | 178 | |
paul@126 | 179 | /* Introspection. */ |
paul@126 | 180 | |
paul@126 | 181 | int __is_instance(__ref obj) |
paul@126 | 182 | { |
paul@126 | 183 | return obj->pos == __INSTANCEPOS; |
paul@126 | 184 | } |
paul@126 | 185 | |
paul@655 | 186 | int __is_subclass(__ref obj, __attr cls) |
paul@655 | 187 | { |
paul@757 | 188 | return __HASATTR(obj, __TYPEPOS(__VALUE(cls)), __TYPECODE(__VALUE(cls))); |
paul@655 | 189 | } |
paul@655 | 190 | |
paul@655 | 191 | int __is_instance_subclass(__ref obj, __attr cls) |
paul@655 | 192 | { |
paul@757 | 193 | return __is_instance(obj) && __HASATTR(__get_class(obj), __TYPEPOS(__VALUE(cls)), __TYPECODE(__VALUE(cls))); |
paul@655 | 194 | } |
paul@655 | 195 | |
paul@274 | 196 | int __is_type_instance(__ref obj) |
paul@274 | 197 | { |
paul@274 | 198 | return __HASATTR(__get_class(obj), __TYPE_CLASS_POS, __TYPE_CLASS_CODE); |
paul@274 | 199 | } |
paul@274 | 200 | |
paul@126 | 201 | __ref __get_class(__ref obj) |
paul@126 | 202 | { |
paul@757 | 203 | return __VALUE(__load_via_object(obj, __class__)); |
paul@126 | 204 | } |
paul@126 | 205 | |
paul@231 | 206 | __attr __get_class_attr(__ref obj) |
paul@231 | 207 | { |
paul@624 | 208 | return __load_via_object(obj, __class__); |
paul@231 | 209 | } |
paul@231 | 210 | |
paul@126 | 211 | /* Attribute testing operations. */ |
paul@126 | 212 | |
paul@144 | 213 | __ref __test_specific_instance(__ref obj, __ref type) |
paul@126 | 214 | { |
paul@144 | 215 | return __get_class(obj) == type ? obj : 0; |
paul@126 | 216 | } |
paul@126 | 217 | |
paul@237 | 218 | __ref __test_specific_object(__ref obj, __ref type) |
paul@237 | 219 | { |
paul@237 | 220 | return __test_specific_type(obj, type) || __test_specific_instance(obj, type) ? obj : 0; |
paul@237 | 221 | } |
paul@237 | 222 | |
paul@237 | 223 | __ref __test_specific_type(__ref obj, __ref type) |
paul@237 | 224 | { |
paul@237 | 225 | return obj == type ? obj : 0; |
paul@237 | 226 | } |
paul@237 | 227 | |
paul@624 | 228 | __ref __test_common_instance__(__ref obj, int pos, int code) |
paul@144 | 229 | { |
paul@144 | 230 | return __HASATTR(__get_class(obj), pos, code) ? obj : 0; |
paul@144 | 231 | } |
paul@144 | 232 | |
paul@624 | 233 | __ref __test_common_object__(__ref obj, int pos, int code) |
paul@126 | 234 | { |
paul@624 | 235 | return __test_common_type__(obj, pos, code) || __test_common_instance__(obj, pos, code) ? obj : 0; |
paul@144 | 236 | } |
paul@144 | 237 | |
paul@624 | 238 | __ref __test_common_type__(__ref obj, int pos, int code) |
paul@144 | 239 | { |
paul@144 | 240 | return __HASATTR(obj, pos, code) ? obj : 0; |
paul@126 | 241 | } |
paul@126 | 242 | |
paul@126 | 243 | /* Attribute testing and retrieval operations. */ |
paul@126 | 244 | |
paul@487 | 245 | __attr __check_and_load_via_object_null(__ref obj, int pos, int code) |
paul@233 | 246 | { |
paul@233 | 247 | if (__HASATTR(obj, pos, code)) |
paul@624 | 248 | return __load_via_object__(obj, pos); |
paul@233 | 249 | else |
paul@233 | 250 | return __NULL; |
paul@233 | 251 | } |
paul@233 | 252 | |
paul@624 | 253 | __attr __check_and_load_via_class__(__ref obj, int pos, int code) |
paul@126 | 254 | { |
paul@624 | 255 | return __check_and_load_via_object__(__get_class(obj), pos, code); |
paul@126 | 256 | } |
paul@126 | 257 | |
paul@624 | 258 | __attr __check_and_load_via_object__(__ref obj, int pos, int code) |
paul@126 | 259 | { |
paul@233 | 260 | if (__HASATTR(obj, pos, code)) |
paul@624 | 261 | return __load_via_object__(obj, pos); |
paul@233 | 262 | |
paul@233 | 263 | __raise_type_error(); |
paul@233 | 264 | return __NULL; |
paul@126 | 265 | } |
paul@126 | 266 | |
paul@624 | 267 | __attr __check_and_load_via_any__(__ref obj, int pos, int code) |
paul@126 | 268 | { |
paul@233 | 269 | __attr out = __check_and_load_via_object_null(obj, pos, code); |
paul@757 | 270 | if (__ISNULL(out)) |
paul@624 | 271 | out = __check_and_load_via_class__(obj, pos, code); |
paul@126 | 272 | return out; |
paul@126 | 273 | } |
paul@126 | 274 | |
paul@126 | 275 | /* Attribute testing and storage operations. */ |
paul@126 | 276 | |
paul@624 | 277 | int __check_and_store_via_class__(__ref obj, int pos, int code, __attr value) |
paul@252 | 278 | { |
paul@252 | 279 | /* Forbid class-relative assignments. */ |
paul@252 | 280 | |
paul@252 | 281 | __raise_type_error(); |
paul@252 | 282 | return 0; |
paul@252 | 283 | } |
paul@252 | 284 | |
paul@624 | 285 | int __check_and_store_via_object__(__ref obj, int pos, int code, __attr value) |
paul@126 | 286 | { |
paul@126 | 287 | if (__HASATTR(obj, pos, code)) |
paul@126 | 288 | { |
paul@624 | 289 | __store_via_object__(obj, pos, value); |
paul@126 | 290 | return 1; |
paul@126 | 291 | } |
paul@252 | 292 | |
paul@252 | 293 | /* No suitable attribute. */ |
paul@252 | 294 | |
paul@252 | 295 | __raise_type_error(); |
paul@126 | 296 | return 0; |
paul@126 | 297 | } |
paul@126 | 298 | |
paul@964 | 299 | int __check_and_store_via_object_internal__(__ref obj, int pos, int code, __attr value) |
paul@964 | 300 | { |
paul@964 | 301 | if (__HASATTR(obj, pos, code)) |
paul@964 | 302 | { |
paul@964 | 303 | __store_via_object_internal__(obj, pos, value); |
paul@964 | 304 | return 1; |
paul@964 | 305 | } |
paul@964 | 306 | |
paul@964 | 307 | /* No suitable attribute. */ |
paul@964 | 308 | |
paul@964 | 309 | __raise_type_error(); |
paul@964 | 310 | return 0; |
paul@964 | 311 | } |
paul@964 | 312 | |
paul@624 | 313 | int __check_and_store_via_any__(__ref obj, int pos, int code, __attr value) |
paul@126 | 314 | { |
paul@624 | 315 | if (__check_and_store_via_object__(obj, pos, code, value)) |
paul@126 | 316 | return 1; |
paul@252 | 317 | |
paul@252 | 318 | /* Forbid class-relative assignments. */ |
paul@252 | 319 | |
paul@252 | 320 | __raise_type_error(); |
paul@252 | 321 | return 0; |
paul@126 | 322 | } |
paul@126 | 323 | |
paul@126 | 324 | /* Context-related operations. */ |
paul@126 | 325 | |
paul@852 | 326 | int __test_context_update(__attr context, __attr attr, int invoke) |
paul@126 | 327 | { |
paul@594 | 328 | /* Return whether the context should be updated for the attribute. */ |
paul@594 | 329 | |
paul@757 | 330 | __attr attrcontext = __CONTEXT_AS_VALUE(attr); |
paul@757 | 331 | __ref attrcontextvalue = __VALUE(attrcontext); |
paul@577 | 332 | |
paul@267 | 333 | /* Preserve any existing null or instance context. */ |
paul@230 | 334 | |
paul@757 | 335 | if (__ISNULL(attrcontext) || __is_instance(attrcontextvalue)) |
paul@594 | 336 | return 0; |
paul@235 | 337 | |
paul@235 | 338 | /* Test any instance context against the context employed by the |
paul@235 | 339 | attribute. */ |
paul@126 | 340 | |
paul@757 | 341 | if (__is_instance(__VALUE(context))) |
paul@477 | 342 | { |
paul@601 | 343 | /* Obtain the special class attribute position and code identifying the |
paul@601 | 344 | attribute context's class, inspecting the context instance for |
paul@601 | 345 | compatibility. */ |
paul@601 | 346 | |
paul@757 | 347 | if (__test_common_instance__(__VALUE(context), __TYPEPOS(attrcontextvalue), __TYPECODE(attrcontextvalue))) |
paul@594 | 348 | return 1; |
paul@235 | 349 | else |
paul@235 | 350 | __raise_type_error(); |
paul@477 | 351 | } |
paul@235 | 352 | |
paul@852 | 353 | /* Without a null or instance context, an invocation cannot be performed. */ |
paul@852 | 354 | |
paul@852 | 355 | if (invoke) |
paul@852 | 356 | __raise_unbound_method_error(); |
paul@852 | 357 | |
paul@274 | 358 | /* Test for access to a type class attribute using a type instance. */ |
paul@274 | 359 | |
paul@757 | 360 | if (__test_specific_type(attrcontextvalue, &__TYPE_CLASS_TYPE) && __is_type_instance(__VALUE(context))) |
paul@594 | 361 | return 1; |
paul@274 | 362 | |
paul@235 | 363 | /* Otherwise, preserve the attribute as retrieved. */ |
paul@235 | 364 | |
paul@594 | 365 | return 0; |
paul@594 | 366 | } |
paul@594 | 367 | |
paul@757 | 368 | __attr __test_context(__attr context, __attr attr) |
paul@594 | 369 | { |
paul@594 | 370 | /* Update the context or return the unchanged attribute. */ |
paul@594 | 371 | |
paul@852 | 372 | if (__test_context_update(context, attr, 0)) |
paul@594 | 373 | return __update_context(context, attr); |
paul@594 | 374 | else |
paul@594 | 375 | return attr; |
paul@126 | 376 | } |
paul@126 | 377 | |
paul@757 | 378 | __attr __update_context(__attr context, __attr attr) |
paul@126 | 379 | { |
paul@577 | 380 | return __new_wrapper(context, attr); |
paul@126 | 381 | } |
paul@126 | 382 | |
paul@757 | 383 | __attr __test_context_revert(int target, __attr context, __attr attr, __attr contexts[]) |
paul@602 | 384 | { |
paul@602 | 385 | /* Revert the local context to that employed by the attribute if the |
paul@602 | 386 | supplied context is not appropriate. */ |
paul@602 | 387 | |
paul@852 | 388 | if (!__test_context_update(context, attr, 1)) |
paul@757 | 389 | contexts[target] = __CONTEXT_AS_VALUE(attr); |
paul@602 | 390 | return attr; |
paul@602 | 391 | } |
paul@602 | 392 | |
paul@757 | 393 | __attr __test_context_static(int target, __attr context, __ref value, __attr contexts[]) |
paul@602 | 394 | { |
paul@602 | 395 | /* Set the local context to the specified context if appropriate. */ |
paul@602 | 396 | |
paul@852 | 397 | if (__test_context_update(context, __ATTRVALUE(value), 1)) |
paul@602 | 398 | contexts[target] = context; |
paul@713 | 399 | return __ATTRVALUE(value); |
paul@602 | 400 | } |
paul@602 | 401 | |
paul@523 | 402 | /* Context testing for invocations. */ |
paul@523 | 403 | |
paul@757 | 404 | int __type_method_invocation(__attr context, __attr target) |
paul@523 | 405 | { |
paul@757 | 406 | __attr targetcontext = __CONTEXT_AS_VALUE(target); |
paul@523 | 407 | |
paul@523 | 408 | /* Require instances, not classes, where methods are function instances. */ |
paul@523 | 409 | |
paul@757 | 410 | if (!__is_instance(__VALUE(target))) |
paul@523 | 411 | return 0; |
paul@523 | 412 | |
paul@577 | 413 | /* Access the context of the callable and test if it is the type object. */ |
paul@523 | 414 | |
paul@757 | 415 | return (!__ISNULL(targetcontext) && __test_specific_type(__VALUE(targetcontext), &__TYPE_CLASS_TYPE) && __is_type_instance(__VALUE(context))); |
paul@523 | 416 | } |
paul@523 | 417 | |
paul@577 | 418 | __attr __unwrap_callable(__attr callable) |
paul@523 | 419 | { |
paul@757 | 420 | __attr value = __check_and_load_via_object_null(__VALUE(callable), __ATTRPOS(__value__), __ATTRCODE(__value__)); |
paul@757 | 421 | return __VALUE(value) ? value : callable; |
paul@577 | 422 | } |
paul@577 | 423 | |
paul@763 | 424 | __attr (*__get_function_unchecked(__attr target))() |
paul@746 | 425 | { |
paul@757 | 426 | return __load_via_object(__VALUE(__unwrap_callable(target)), __fn__).fn; |
paul@746 | 427 | } |
paul@746 | 428 | |
paul@763 | 429 | __attr (*__get_function(__attr context, __attr target))() |
paul@577 | 430 | { |
paul@759 | 431 | return __get_function_unwrapped(context, __unwrap_callable(target)); |
paul@759 | 432 | } |
paul@663 | 433 | |
paul@768 | 434 | __attr (*__get_function_unwrapped(__attr context, __attr target))() |
paul@759 | 435 | { |
paul@523 | 436 | /* Require null or instance contexts for functions and methods respectively, |
paul@523 | 437 | or type instance contexts for type methods. */ |
paul@523 | 438 | |
paul@757 | 439 | if (__ISNULL(context) || __is_instance(__VALUE(context)) || __type_method_invocation(context, target)) |
paul@776 | 440 | return __get_function_member(target); |
paul@523 | 441 | else |
paul@577 | 442 | return __unbound_method; |
paul@523 | 443 | } |
paul@523 | 444 | |
paul@776 | 445 | __attr (*__get_function_member(__attr target))() |
paul@776 | 446 | { |
paul@776 | 447 | return __load_via_object(__VALUE(target), __fn__).fn; |
paul@776 | 448 | } |
paul@776 | 449 | |
paul@763 | 450 | __attr (*__check_and_get_function(__attr context, __attr target))() |
paul@523 | 451 | { |
paul@759 | 452 | return __check_and_get_function_unwrapped(context, __unwrap_callable(target)); |
paul@759 | 453 | } |
paul@663 | 454 | |
paul@768 | 455 | __attr (*__check_and_get_function_unwrapped(__attr context, __attr target))() |
paul@759 | 456 | { |
paul@523 | 457 | /* Require null or instance contexts for functions and methods respectively, |
paul@523 | 458 | or type instance contexts for type methods. */ |
paul@523 | 459 | |
paul@757 | 460 | if (__ISNULL(context) || __is_instance(__VALUE(context)) || __type_method_invocation(context, target)) |
paul@757 | 461 | return __check_and_load_via_object__(__VALUE(target), __ATTRPOS(__fn__), __ATTRCODE(__fn__)).fn; |
paul@523 | 462 | else |
paul@577 | 463 | return __unbound_method; |
paul@523 | 464 | } |
paul@523 | 465 | |
paul@126 | 466 | /* Parameter position operations. */ |
paul@126 | 467 | |
paul@126 | 468 | int __HASPARAM(const __ptable *ptable, int ppos, int pcode) |
paul@126 | 469 | { |
paul@126 | 470 | __param param; |
paul@126 | 471 | |
paul@126 | 472 | if (ppos < ptable->size) |
paul@126 | 473 | { |
paul@126 | 474 | param = ptable->params[ppos]; |
paul@126 | 475 | if (param.code == pcode) |
paul@126 | 476 | return param.pos; |
paul@126 | 477 | } |
paul@126 | 478 | |
paul@126 | 479 | return -1; |
paul@126 | 480 | } |
paul@126 | 481 | |
paul@126 | 482 | /* Conversions. */ |
paul@126 | 483 | |
paul@126 | 484 | __attr __CONTEXT_AS_VALUE(__attr attr) |
paul@126 | 485 | { |
paul@757 | 486 | return __check_and_load_via_object_null(__VALUE(attr), __ATTRPOS(__context__), __ATTRCODE(__context__)); |
paul@126 | 487 | } |
paul@126 | 488 | |
paul@126 | 489 | /* Type testing. */ |
paul@126 | 490 | |
paul@144 | 491 | __ref __ISFUNC(__ref obj) |
paul@126 | 492 | { |
paul@126 | 493 | return __test_specific_instance(obj, &__FUNCTION_TYPE); |
paul@126 | 494 | } |
paul@126 | 495 | |
paul@126 | 496 | /* Attribute codes and positions for type objects. */ |
paul@126 | 497 | |
paul@126 | 498 | unsigned int __TYPECODE(__ref obj) |
paul@126 | 499 | { |
paul@126 | 500 | return obj->table->attrs[obj->pos]; |
paul@126 | 501 | } |
paul@126 | 502 | |
paul@126 | 503 | unsigned int __TYPEPOS(__ref obj) |
paul@126 | 504 | { |
paul@126 | 505 | return obj->pos; |
paul@126 | 506 | } |
paul@151 | 507 | |
paul@260 | 508 | /* Memory allocation. */ |
paul@260 | 509 | |
paul@260 | 510 | void *__ALLOCATE(size_t nmemb, size_t size) |
paul@260 | 511 | { |
paul@433 | 512 | void *ptr = GC_MALLOC(nmemb * size); /* sets memory to zero */ |
paul@260 | 513 | if (ptr == NULL) |
paul@260 | 514 | __raise_memory_error(); |
paul@260 | 515 | return ptr; |
paul@260 | 516 | } |
paul@260 | 517 | |
paul@756 | 518 | void *__ALLOCATEIM(size_t nmemb, size_t size) |
paul@756 | 519 | { |
paul@756 | 520 | void *ptr = GC_MALLOC_ATOMIC(nmemb * size); /* sets memory to zero */ |
paul@756 | 521 | if (ptr == NULL) |
paul@756 | 522 | __raise_memory_error(); |
paul@756 | 523 | return ptr; |
paul@756 | 524 | } |
paul@756 | 525 | |
paul@260 | 526 | void *__REALLOCATE(void *ptr, size_t size) |
paul@260 | 527 | { |
paul@433 | 528 | void *nptr = GC_REALLOC(ptr, size); |
paul@260 | 529 | if (nptr == NULL) |
paul@260 | 530 | __raise_memory_error(); |
paul@260 | 531 | return nptr; |
paul@260 | 532 | } |
paul@260 | 533 | |
paul@151 | 534 | /* Copying of structures. */ |
paul@151 | 535 | |
paul@964 | 536 | __ref __COPY(__ref obj, size_t size) |
paul@151 | 537 | { |
paul@260 | 538 | __ref copy = (__ref) __ALLOCATE(1, size); |
paul@151 | 539 | memcpy(copy, obj, size); |
paul@151 | 540 | return copy; |
paul@151 | 541 | } |
paul@964 | 542 | |
paul@964 | 543 | void __COPY_TO(__ref source, __ref target, size_t size) |
paul@964 | 544 | { |
paul@964 | 545 | memcpy(target, source, size); |
paul@964 | 546 | } |
paul@964 | 547 | |
paul@964 | 548 | /* Stack management. */ |
paul@964 | 549 | |
paul@964 | 550 | __attr __stack_init() |
paul@964 | 551 | { |
paul@964 | 552 | __attr __stack; |
paul@964 | 553 | |
paul@964 | 554 | __stack.stackdesc = (__stackdesc *) __ALLOCATE(1, sizeof(__stackdesc)); |
paul@964 | 555 | __stack.stackdesc->current = NULL; |
paul@964 | 556 | __stack_expand(__stack); |
paul@964 | 557 | |
paul@964 | 558 | return __stack; |
paul@964 | 559 | } |
paul@964 | 560 | |
paul@964 | 561 | __attr __stack_allocate(__attr __stack, size_t size) |
paul@964 | 562 | { |
paul@964 | 563 | char *result; |
paul@964 | 564 | __section *section = __stack.stackdesc->current; |
paul@964 | 565 | |
paul@964 | 566 | if (section->limit - section->level < size) |
paul@964 | 567 | { |
paul@964 | 568 | __stack_expand(__stack); |
paul@964 | 569 | section = __stack.stackdesc->current; |
paul@964 | 570 | } |
paul@964 | 571 | |
paul@964 | 572 | result = section->level; |
paul@964 | 573 | section->level += size; |
paul@964 | 574 | |
paul@964 | 575 | return __ATTRVALUE(result); |
paul@964 | 576 | } |
paul@964 | 577 | |
paul@964 | 578 | void __stack_expand(__attr __stack) |
paul@964 | 579 | { |
paul@964 | 580 | __section *current = __stack.stackdesc->current; |
paul@964 | 581 | __section *section = (__section *) __ALLOCATE(1, sizeof(__section)); |
paul@964 | 582 | char *base = (char *) __ALLOCATEIM(1, __STACK_SECTION_SIZE); |
paul@964 | 583 | |
paul@964 | 584 | section->base = base; |
paul@964 | 585 | section->level = base; |
paul@964 | 586 | section->limit = base + __STACK_SECTION_SIZE; |
paul@964 | 587 | section->previous = current; |
paul@964 | 588 | |
paul@964 | 589 | __stack.stackdesc->current = section; |
paul@964 | 590 | } |
paul@964 | 591 | |
paul@964 | 592 | __attr __return(__attr result, __section *section, char *level) |
paul@964 | 593 | { |
paul@964 | 594 | __ref obj = __VALUE(result); |
paul@964 | 595 | char *target = level; |
paul@964 | 596 | size_t size; |
paul@964 | 597 | |
paul@964 | 598 | if (__COPYABLE(result)) |
paul@964 | 599 | { |
paul@964 | 600 | size = __INSTANCE_SIZE(obj); |
paul@964 | 601 | |
paul@964 | 602 | /* Test for space in the section. */ |
paul@964 | 603 | |
paul@964 | 604 | if (size > section->limit - level) |
paul@964 | 605 | { |
paul@964 | 606 | __stack_expand(__stack); |
paul@964 | 607 | section = __stack.stackdesc->current; |
paul@964 | 608 | target = section->base; |
paul@964 | 609 | } |
paul@964 | 610 | |
paul@964 | 611 | /* Copy into the stack and adjust the level. */ |
paul@964 | 612 | |
paul@964 | 613 | __COPY_TO(obj, (__ref) target, size); |
paul@964 | 614 | level = target + size; |
paul@964 | 615 | |
paul@964 | 616 | /* Reference the new location of the object. */ |
paul@964 | 617 | |
paul@964 | 618 | result = __MUTABLEVALUE(target); |
paul@964 | 619 | } |
paul@964 | 620 | |
paul@964 | 621 | /* Set the level and current section.*/ |
paul@964 | 622 | |
paul@964 | 623 | section->level = level; |
paul@964 | 624 | __stack.stackdesc->current = section; |
paul@964 | 625 | |
paul@964 | 626 | return result; |
paul@964 | 627 | } |