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