Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -148,10 +148,13 @@ objc_runtime="ObjFW runtime" AC_CHECK_HEADER(objc/objc.h) AC_MSG_CHECKING(which Objective C runtime to use) AC_ARG_ENABLE(runtime, AS_HELP_STRING([--enable-runtime], [use the included runtime])) +AC_ARG_ENABLE(seluid16, + AS_HELP_STRING([--enable-seluid16], + [use only 16 bit for selectors UIDs])) AS_IF([test x"$enable_runtime" != x"yes"], [ AS_IF([test x"$ac_cv_header_objc_objc_h" = x"yes"], [ dnl TODO: This is ugly. Let's think of a better check. AC_EGREP_CPP(yes, [ #import @@ -212,10 +215,15 @@ AC_SUBST(LOOKUP_S, lookup-x86-elf.S) AC_DEFINE(OF_ASM_LOOKUP, 1, [Whether to use lookup in assembly]) ]) ]) + + AS_IF([test x"$enable_seluid16" = x"yes"], [ + AC_DEFINE(OF_SELUID16, 1, + [Whether to use 16 bit selector UIDs]) + ]) ;; "Apple runtime") AC_DEFINE(OF_APPLE_RUNTIME, 1, [Whether we use the Apple ObjC runtime]) Index: src/runtime/lookup-amd64-elf.S ================================================================== --- src/runtime/lookup-amd64-elf.S +++ src/runtime/lookup-amd64-elf.S @@ -11,10 +11,12 @@ * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ + +#include "config.h" .globl objc_msg_lookup .globl objc_msg_lookup_super .section .text @@ -27,13 +29,15 @@ lookup: movq (%rsi), %rax movzbl %ah, %ecx movzbl %al, %edx +#ifndef OF_SELUID16 shrl $16, %eax movq (%r8,%rax,8), %r8 +#endif movq (%r8,%rcx,8), %r8 movq (%r8,%rdx,8), %rax testq %rax, %rax jz forward Index: src/runtime/lookup-x86-elf.S ================================================================== --- src/runtime/lookup-x86-elf.S +++ src/runtime/lookup-x86-elf.S @@ -11,10 +11,12 @@ * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ + +#include "config.h" .globl objc_msg_lookup .globl objc_msg_lookup_super .section .text @@ -27,12 +29,14 @@ movl 32(%edx), %edx lookup: movl 8(%esp), %eax +#ifndef OF_SELUID16 movzbl 2(%eax), %ecx movl (%edx,%ecx,4), %edx +#endif movzbl 1(%eax), %ecx movl (%edx,%ecx,4), %edx movzbl (%eax), %ecx movl (%edx,%ecx,4), %eax Index: src/runtime/runtime-private.h ================================================================== --- src/runtime/runtime-private.h +++ src/runtime/runtime-private.h @@ -104,19 +104,27 @@ struct objc_sparsearray { struct objc_sparsearray_level2 *buckets[256]; }; +#ifndef OF_SELUID16 struct objc_sparsearray_level2 { struct objc_sparsearray_level3 *buckets[256]; BOOL empty; }; struct objc_sparsearray_level3 { const void *buckets[256]; BOOL empty; }; +#else +struct objc_sparsearray_level2 { + const void *buckets[256]; + BOOL empty; +}; +#endif + enum objc_abi_class_info { OBJC_CLASS_INFO_CLASS = 0x001, OBJC_CLASS_INFO_METACLASS = 0x002, OBJC_CLASS_INFO_SETUP = 0x100, @@ -167,19 +175,26 @@ extern void objc_free_when_singlethreaded(void*); static inline const void* objc_sparsearray_get(const struct objc_sparsearray *s, uint32_t idx) { +#ifndef OF_SELUID16 uint8_t i = idx >> 16; uint8_t j = idx >> 8; uint8_t k = idx; return s->buckets[i]->buckets[j]->buckets[k]; +#else + uint8_t i = idx >> 8; + uint8_t j = idx; + + return s->buckets[i]->buckets[j]; +#endif } #define ERROR(...) \ { \ fprintf(stderr, "[objc @ " __FILE__ ":%d] ", __LINE__); \ fprintf(stderr, __VA_ARGS__); \ fputs("\n", stderr); \ abort(); \ } Index: src/runtime/selector.m ================================================================== --- src/runtime/selector.m +++ src/runtime/selector.m @@ -20,10 +20,16 @@ #include #include #import "runtime.h" #import "runtime-private.h" + +#ifndef OF_SELUID16 +# define SEL_MAX 0xFFFFFF +#else +# define SEL_MAX 0xFFFF +#endif static struct objc_sparsearray *selectors = NULL; void objc_register_selector(struct objc_abi_selector *sel) @@ -33,23 +39,23 @@ const char *name; if (selectors == NULL) selectors = objc_sparsearray_new(); - hash = objc_hash_string(sel->name) & 0xFFFFFF; + hash = objc_hash_string(sel->name) & SEL_MAX; - while (hash <= 0xFFFFFF && + while (hash <= SEL_MAX && (name = objc_sparsearray_get(selectors, hash)) != NULL) { if (!strcmp(name, sel->name)) { rsel->uid = hash; return; } hash++; } - if (hash > 0xFFFFFF) { + if (hash > SEL_MAX) { last = hash; hash = 0; while (hash < last && (name = objc_sparsearray_get(selectors, hash)) != NULL) { Index: src/runtime/sparsearray.m ================================================================== --- src/runtime/sparsearray.m +++ src/runtime/sparsearray.m @@ -21,40 +21,57 @@ #import "runtime.h" #import "runtime-private.h" static struct objc_sparsearray_level2 *empty_level2 = NULL; +#ifndef OF_SELUID16 static struct objc_sparsearray_level3 *empty_level3 = NULL; +#endif static void init(void) { uint_fast16_t i; empty_level2 = malloc(sizeof(struct objc_sparsearray_level2)); - empty_level3 = malloc(sizeof(struct objc_sparsearray_level3)); - - if (empty_level2 == NULL || empty_level3 == NULL) + if (empty_level2 == NULL) ERROR("Not enough memory to allocate sparse array!"); empty_level2->empty = YES; + +#ifndef OF_SELUID16 + empty_level3 = malloc(sizeof(struct objc_sparsearray_level3)); + if (empty_level3 == NULL) + ERROR("Not enough memory to allocate sparse array!"); + empty_level3->empty = YES; +#endif +#ifndef OF_SELUID16 for (i = 0; i < 256; i++) { empty_level2->buckets[i] = empty_level3; empty_level3->buckets[i] = NULL; } +#else + for (i = 0; i < 256; i++) + empty_level2->buckets[i] = NULL; +#endif } struct objc_sparsearray* objc_sparsearray_new(void) { struct objc_sparsearray *s; uint_fast16_t i; +#ifndef OF_SELUID16 if (empty_level2 == NULL || empty_level3 == NULL) init(); +#else + if (empty_level2 == NULL) + init(); +#endif if ((s = malloc(sizeof(struct objc_sparsearray))) == NULL) ERROR("Not enough memory to allocate sparse array!"); for (i = 0; i < 256; i++) @@ -65,17 +82,21 @@ void objc_sparsearray_copy(struct objc_sparsearray *dst, struct objc_sparsearray *src) { - uint_fast16_t i, j, k; + uint_fast16_t i, j; +#ifndef OF_SELUID16 + uint_fast16_t k; +#endif uint32_t idx; for (i = 0; i < 256; i++) { if (src->buckets[i]->empty) continue; +#ifndef OF_SELUID16 for (j = 0; j < 256; j++) { if (src->buckets[i]->buckets[j]->empty) continue; for (k = 0; k < 256; k++) { @@ -88,19 +109,37 @@ idx = (i << 16) | (j << 8) | k; objc_sparsearray_set(dst, idx, obj); } } +#else + for (j = 0; j < 256; j++) { + const void *obj; + + obj = src->buckets[i]->buckets[j]; + + if (obj == NULL) + continue; + + idx = (i << 8) | j; + objc_sparsearray_set(dst, idx, obj); + } +#endif } } void objc_sparsearray_set(struct objc_sparsearray *s, uint32_t idx, const void *obj) { +#ifndef OF_SELUID16 uint8_t i = idx >> 16; uint8_t j = idx >> 8; uint8_t k = idx; +#else + uint8_t i = idx >> 8; + uint8_t j = idx; +#endif if (s->buckets[i]->empty) { struct objc_sparsearray_level2 *t; uint_fast16_t l; @@ -110,15 +149,20 @@ ERROR("Not enough memory to insert into sparse array!"); t->empty = NO; for (l = 0; l < 256; l++) +#ifndef OF_SELUID16 t->buckets[l] = empty_level3; +#else + t->buckets[l] = NULL; +#endif s->buckets[i] = t; } +#ifndef OF_SELUID16 if (s->buckets[i]->buckets[j]->empty) { struct objc_sparsearray_level3 *t; uint_fast16_t l; t = malloc(sizeof(struct objc_sparsearray_level3)); @@ -133,24 +177,32 @@ s->buckets[i]->buckets[j] = t; } s->buckets[i]->buckets[j]->buckets[k] = obj; +#else + s->buckets[i]->buckets[j] = obj; +#endif } void objc_sparsearray_free(struct objc_sparsearray *s) { - uint_fast16_t i, j; + uint_fast16_t i; +#ifndef OF_SELUID16 + uint_fast16_t j; +#endif for (i = 0; i < 256; i++) { +#ifndef OF_SELUID16 if (s->buckets[i]->empty) continue; for (j = 0; j < 256; j++) if (!s->buckets[i]->buckets[j]->empty) free(s->buckets[i]->buckets[j]); +#endif free(s->buckets[i]); } free(s); @@ -159,11 +211,15 @@ void objc_sparsearray_cleanup(void) { if (empty_level2 != NULL) free(empty_level2); +#ifndef OF_SELUID16 if (empty_level3 != NULL) free(empty_level3); +#endif empty_level2 = NULL; +#ifndef OF_SELUID16 empty_level3 = NULL; +#endif }