summaryrefslogtreecommitdiff
path: root/gcc/objc
diff options
context:
space:
mode:
authorlaw <law@138bc75d-0d04-0410-961f-82ee72b054a4>1998-09-02 23:48:01 +0000
committerlaw <law@138bc75d-0d04-0410-961f-82ee72b054a4>1998-09-02 23:48:01 +0000
commit5c87243081ce5580df8701a3754bc00eee43e050 (patch)
tree1c3b607110323b831606c2ec27d39a0f292a1a15 /gcc/objc
parentff79b0cf7660dfd068a1b0a08a865c881d55259d (diff)
downloadgcc-5c87243081ce5580df8701a3754bc00eee43e050.tar.gz
Boehm GC support for ObjC from Ovidiu Predescu.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@22199 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/objc')
-rw-r--r--gcc/objc/Make-lang.in94
-rw-r--r--gcc/objc/Makefile.in17
-rw-r--r--gcc/objc/encoding.c445
-rw-r--r--gcc/objc/encoding.h50
-rw-r--r--gcc/objc/gc.c458
-rw-r--r--gcc/objc/misc.c30
-rw-r--r--gcc/objc/objc-act.c112
-rw-r--r--gcc/objc/objc-api.h13
-rw-r--r--gcc/objc/objc-features.texi392
-rw-r--r--gcc/objc/objc.h1
-rw-r--r--gcc/objc/objects.c13
-rw-r--r--gcc/objc/sendmsg.c51
12 files changed, 1582 insertions, 94 deletions
diff --git a/gcc/objc/Make-lang.in b/gcc/objc/Make-lang.in
index 3774e22e564..f12992b8390 100644
--- a/gcc/objc/Make-lang.in
+++ b/gcc/objc/Make-lang.in
@@ -119,6 +119,7 @@ OBJC_O = objc/hash.o objc/sarray.o \
objc/NXConstStr.o objc/Object.o \
objc/Protocol.o objc/nil_method.o \
objc/thr.o objc/linking.o \
+ objc/gc.o \
objc/$(OBJC_THREAD_FILE).o
objc/hash.o: $(srcdir)/objc/hash.c $(GCC_PASSES)
@@ -172,11 +173,74 @@ objc/nil_method.o: $(srcdir)/objc/nil_method.c $(GCC_PASSES)
objc/linking.o: $(srcdir)/objc/linking.m $(GCC_PASSES)
$(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
-fgnu-runtime -c $(srcdir)/objc/linking.m -o $@
-
+objc/gc.o: $(srcdir)/objc/gc.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -fgnu-runtime -c $(srcdir)/objc/gc.c -o $@
objc/libobjc_entry.o: $(srcdir)/objc/libobjc_entry.c $(GCC_PASSES)
$(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
-c $(srcdir)/objc/libobjc_entry.c -o $@
+
+
+objc/hash_gc.o: $(srcdir)/objc/hash.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/hash.c -o $@
+objc/sarray_gc.o: $(srcdir)/objc/sarray.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/sarray.c -o $@
+objc/class_gc.o: $(srcdir)/objc/class.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/class.c -o $@
+objc/sendmsg_gc.o: $(srcdir)/objc/sendmsg.c $(GCC_PASSES) objc/runtime-info.h
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) -Iobjc \
+ -c $(srcdir)/objc/sendmsg.c -o $@
+objc/init_gc.o: $(srcdir)/objc/init.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/init.c -o $@
+objc/archive_gc.o: $(srcdir)/objc/archive.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/archive.c -o $@
+objc/encoding_gc.o: $(srcdir)/objc/encoding.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/encoding.c -o $@
+objc/selector_gc.o: $(srcdir)/objc/selector.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/selector.c -o $@
+objc/objects_gc.o: $(srcdir)/objc/objects.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/objects.c -o $@
+objc/misc_gc.o: $(srcdir)/objc/misc.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/misc.c -o $@
+objc/NXConstStr_gc.o: $(srcdir)/objc/NXConstStr.m $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -fgnu-runtime -c $(srcdir)/objc/NXConstStr.m -o $@
+objc/Object_gc.o: $(srcdir)/objc/Object.m $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -fgnu-runtime -c $(srcdir)/objc/Object.m -o $@
+objc/Protocol_gc.o: $(srcdir)/objc/Protocol.m $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -fgnu-runtime -c $(srcdir)/objc/Protocol.m -o $@
+objc/thr_gc.o: $(srcdir)/objc/thr.h $(srcdir)/objc/thr.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/thr.c -o $@
+objc/$(OBJC_THREAD_FILE)_gc.o: $(srcdir)/objc/$(OBJC_THREAD_FILE).c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/$(OBJC_THREAD_FILE).c -o $@
+objc/nil_method_gc.o: $(srcdir)/objc/nil_method.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/nil_method.c -o $@
+objc/linking_gc.o: $(srcdir)/objc/linking.m $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -fgnu-runtime -c $(srcdir)/objc/linking.m -o $@
+objc/gc_gc.o: $(srcdir)/objc/gc.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -fgnu-runtime -c $(srcdir)/objc/gc.c -o $@
+objc/libobjc_entry_gc.o: $(srcdir)/objc/libobjc_entry.c $(GCC_PASSES)
+ $(GCC_FOR_TARGET) $(GCC_CFLAGS) $(INCLUDES) \
+ -c $(srcdir)/objc/libobjc_entry.c -o $@
+
+
$(OBJC_O): $(GCC_PASSES) cc1obj$(exeext)
# Build the Objective C runtime library.
@@ -185,6 +249,17 @@ libobjc.a: cc1obj$(exeext) specs stmp-int-hdrs libgcc2.ready \
-rm -f libobjc.a
$(AR) $(AR_FLAGS) libobjc.a $(OBJC_O)
-if $(RANLIB_TEST) ; then $(RANLIB) libobjc.a; else true; fi
+ -if [ "$(OBJC_BOEHM_GC)" != "" ]; then \
+ make libobjc_gc.a GCC_CFLAGS="$(GCC_CFLAGS) -DOBJC_WITH_GC=1" \
+ OBJC_O=`echo $(OBJC_O) | sed 's/.o /_gc.o /g'`; \
+ fi
+
+# Build the garbage collected Objective C runtime library.
+libobjc_gc.a: cc1obj specs stmp-int-hdrs libgcc2.ready \
+ $(USE_COLLECT2) $(EXTRA_PARTS) $(OBJC_O)
+ -rm -f libobjc_gc.a
+ $(AR) $(AR_FLAGS) libobjc_gc.a $(OBJC_O)
+ -if $(RANLIB_TEST) ; then $(RANLIB) libobjc_gc.a; else true; fi
libobjc_s.a: libobjc.a
mv libobjc.a libobjc_s.a
@@ -236,6 +311,13 @@ objc.install-normal: installdirs
(cd $(libsubdir); $(RANLIB) libobjc.a); else true; fi; \
chmod a-x $(libsubdir)/libobjc.a; \
else true; fi
+ -if [ -f libobjc_gc.a ] ; then \
+ rm -f $(libsubdir)/libobjc_gc.a; \
+ $(INSTALL_DATA) libobjc_gc.a $(libsubdir)/libobjc_gc.a; \
+ if $(RANLIB_TEST) ; then \
+ (cd $(libsubdir); $(RANLIB) libobjc_gc.a); else true; fi; \
+ chmod a-x $(libsubdir)/libobjc_gc.a; \
+ else true; fi
-if [ -f libobjc_s.a ] ; then \
rm -f $(libsubdir)/libobjc_s.a; \
$(INSTALL_DATA) libobjc_s.a $(libsubdir)/libobjc_s.a; \
@@ -263,7 +345,7 @@ objc.mostlyclean:
-rm -f tmp-objc-prs.y
-rm -f objc/*$(objext) objc/xforward objc/fflags
-rm -f objc/runtime-info.h
- -rm -f libobjc.a libobjc_s.a libobjc.dll
+ -rm -f libobjc.a libobjc_gc.a libobjc_s.a libobjc.dll
-rm -f libobjc.base libobjc.exp
objc.clean: objc.mostlyclean
-rm -rf objc-headers
@@ -282,19 +364,19 @@ objc.maintainer-clean:
objc.stage1: stage1-start
-mv objc/*$(objext) stage1/objc
-mv cc1obj$(exeext) stage1
- -mv libobjc.a stage1
+ -mv libobjc.a libobjc_gc.a stage1
objc.stage2: stage2-start
-mv objc/*$(objext) stage2/objc
-mv cc1obj$(exeext) stage2
- -mv libobjc.a stage2
+ -mv libobjc.a libobjc_gc.a stage2
objc.stage3: stage3-start
-mv objc/*$(objext) stage3/objc
-mv cc1obj$(exeext) stage3
- -mv libobjc.a stage3
+ -mv libobjc.a libobjc_gc.a stage3
objc.stage4: stage4-start
-mv objc/*$(objext) stage4/objc
-mv cc1obj$(exeext) stage4
- -mv libobjc.a stage4
+ -mv libobjc.a libobjc_gc.a stage4
#
# Maintenance hooks:
diff --git a/gcc/objc/Makefile.in b/gcc/objc/Makefile.in
index 5f1bc8811eb..b152363fca7 100644
--- a/gcc/objc/Makefile.in
+++ b/gcc/objc/Makefile.in
@@ -81,8 +81,23 @@ copy-headers:
Makefile: $(srcdir)/Makefile.in $(srcdir)/../configure
cd ..; $(SHELL) config.status
+doc: info dvi html
+info: objc-features.info
+dvi: objc-features.dvi
+html: objc-features_toc.html
+
+objc-features.info: $(srcdir)/objc-features.texi
+ makeinfo $(srcdir)/objc-features.texi
+
+objc-features.dvi: $(srcdir)/objc-features.texi
+ texi2dvi $(srcdir)/objc-features.texi
+
+objc-features_toc.html: objc-features.texi
+ texi2html -split_node $(srcdir)/objc-features.texi
+
mostlyclean:
- -rm -f *.o libobjc.a xforward fflags
+ -rm -f *.o libobjc* xforward fflags *.aux *.cp *.dvi *.fn *.info \
+ *.ky *.log *.pg *.toc *.tp *.vr *.html
clean: mostlyclean
distclean: mostlyclean
extraclean: mostlyclean
diff --git a/gcc/objc/encoding.c b/gcc/objc/encoding.c
index c90e914f501..ab47e9c4bf4 100644
--- a/gcc/objc/encoding.c
+++ b/gcc/objc/encoding.c
@@ -1,6 +1,7 @@
/* Encoding of types for Objective C.
- Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc.
+ Copyright (C) 1993, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
Contributed by Kresten Krab Thorup
+ Bitfield support by Ovidiu Predescu
This file is part of GNU CC.
@@ -25,6 +26,8 @@ Boston, MA 02111-1307, USA. */
This exception does not however invalidate any other reasons why
the executable file might be covered by the GNU General Public License. */
+#include "tconfig.h"
+#include "objc-api.h"
#include "encoding.h"
#define MAX(X, Y) \
@@ -40,6 +43,22 @@ Boston, MA 02111-1307, USA. */
__a*((__v+__a-1)/__a); })
+/* Various hacks for objc_layout_record. These are used by the target
+ macros. */
+
+#define TREE_CODE(TYPE) *TYPE
+#define RECORD_TYPE _C_STRUCT_B
+#define UNION_TYPE _C_UNION_B
+#define QUAL_UNION_TYPE _C_UNION_B
+
+#define TYPE_FIELDS(TYPE) objc_skip_typespec (TYPE)
+
+#define DECL_MODE(TYPE) *(TYPE)
+
+#define DFmode _C_DBL
+
+
+
static inline int
atoi (const char* str)
{
@@ -56,8 +75,15 @@ atoi (const char* str)
*/
int
-objc_sizeof_type(const char* type)
+objc_sizeof_type (const char* type)
{
+ /* Skip the variable name if any */
+ if (*type == '"')
+ {
+ for (type++; *type++ != '"';)
+ /* do nothing */;
+ }
+
switch(*type) {
case _C_ID:
return sizeof(id);
@@ -103,6 +129,14 @@ objc_sizeof_type(const char* type)
return sizeof(unsigned long);
break;
+ case _C_LNG_LNG:
+ return sizeof(long long);
+ break;
+
+ case _C_ULNG_LNG:
+ return sizeof(unsigned long long);
+ break;
+
case _C_FLT:
return sizeof(float);
break;
@@ -111,6 +145,9 @@ objc_sizeof_type(const char* type)
return sizeof(double);
break;
+ case _C_VOID:
+ return sizeof(void);
+ break;
case _C_PTR:
case _C_ATOM:
case _C_CHARPTR:
@@ -125,19 +162,32 @@ objc_sizeof_type(const char* type)
}
break;
+ case _C_BFLD:
+ {
+ /* The new encoding of bitfields is: b 'position' 'type' 'size' */
+ int position, size;
+ int startByte, endByte;
+
+ position = atoi (type + 1);
+ while (isdigit (*++type));
+ size = atoi (type + 1);
+
+ startByte = position / BITS_PER_UNIT;
+ endByte = (position + size) / BITS_PER_UNIT;
+ return endByte - startByte;
+ }
+
case _C_STRUCT_B:
{
- int acc_size = 0;
- int align;
- while (*type != _C_STRUCT_E && *type++ != '='); /* skip "<name>=" */
- while (*type != _C_STRUCT_E)
- {
- align = objc_alignof_type (type); /* padd to alignment */
- acc_size = ROUND (acc_size, align);
- acc_size += objc_sizeof_type (type); /* add component size */
- type = objc_skip_typespec (type); /* skip component */
- }
- return acc_size;
+ struct objc_struct_layout layout;
+ unsigned int size;
+
+ objc_layout_structure (type, &layout);
+ while (objc_layout_structure_next_member (&layout))
+ /* do nothing */ ;
+ objc_layout_finish_structure (&layout, &size, NULL);
+
+ return size;
}
case _C_UNION_B:
@@ -146,6 +196,12 @@ objc_sizeof_type(const char* type)
while (*type != _C_UNION_E && *type++ != '=') /* do nothing */;
while (*type != _C_UNION_E)
{
+ /* Skip the variable name if any */
+ if (*type == '"')
+ {
+ for (type++; *type++ != '"';)
+ /* do nothing */;
+ }
max_size = MAX (max_size, objc_sizeof_type (type));
type = objc_skip_typespec (type);
}
@@ -153,7 +209,10 @@ objc_sizeof_type(const char* type)
}
default:
- objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
+ {
+ objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
+ return 0;
+ }
}
}
@@ -165,6 +224,12 @@ objc_sizeof_type(const char* type)
int
objc_alignof_type(const char* type)
{
+ /* Skip the variable name if any */
+ if (*type == '"')
+ {
+ for (type++; *type++ != '"';)
+ /* do nothing */;
+ }
switch(*type) {
case _C_ID:
return __alignof__(id);
@@ -210,6 +275,14 @@ objc_alignof_type(const char* type)
return __alignof__(unsigned long);
break;
+ case _C_LNG_LNG:
+ return __alignof__(long long);
+ break;
+
+ case _C_ULNG_LNG:
+ return __alignof__(unsigned long long);
+ break;
+
case _C_FLT:
return __alignof__(float);
break;
@@ -227,15 +300,18 @@ objc_alignof_type(const char* type)
case _C_ARY_B:
while (isdigit(*++type)) /* do nothing */;
return objc_alignof_type (type);
-
+
case _C_STRUCT_B:
{
- struct { int x; double y; } fooalign;
- while(*type != _C_STRUCT_E && *type++ != '=') /* do nothing */;
- if (*type != _C_STRUCT_E)
- return MAX (objc_alignof_type (type), __alignof__ (fooalign));
- else
- return __alignof__ (fooalign);
+ struct objc_struct_layout layout;
+ unsigned int align;
+
+ objc_layout_structure (type, &layout);
+ while (objc_layout_structure_next_member (&layout))
+ /* do nothing */;
+ objc_layout_finish_structure (&layout, NULL, &align);
+
+ return align;
}
case _C_UNION_B:
@@ -244,14 +320,23 @@ objc_alignof_type(const char* type)
while (*type != _C_UNION_E && *type++ != '=') /* do nothing */;
while (*type != _C_UNION_E)
{
+ /* Skip the variable name if any */
+ if (*type == '"')
+ {
+ for (type++; *type++ != '"';)
+ /* do nothing */;
+ }
maxalign = MAX (maxalign, objc_alignof_type (type));
type = objc_skip_typespec (type);
}
return maxalign;
}
-
+
default:
- objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
+ {
+ objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
+ return 0;
+ }
}
}
@@ -262,8 +347,18 @@ objc_alignof_type(const char* type)
int
objc_aligned_size (const char* type)
{
- int size = objc_sizeof_type (type);
- int align = objc_alignof_type (type);
+ int size, align;
+
+ /* Skip the variable name */
+ if (*type == '"')
+ {
+ for (type++; *type++ != '"';)
+ /* do nothing */;
+ }
+
+ size = objc_sizeof_type (type);
+ align = objc_alignof_type (type);
+
return ROUND (size, align);
}
@@ -275,8 +370,17 @@ objc_aligned_size (const char* type)
int
objc_promoted_size (const char* type)
{
- int size = objc_sizeof_type (type);
- int wordsize = sizeof (void*);
+ int size, wordsize;
+
+ /* Skip the variable name */
+ if (*type == '"')
+ {
+ for (type++; *type++ != '"';)
+ /* do nothing */;
+ }
+
+ size = objc_sizeof_type (type);
+ wordsize = sizeof (void*);
return ROUND (size, wordsize);
}
@@ -294,7 +398,8 @@ objc_skip_type_qualifiers (const char* type)
|| *type == _C_INOUT
|| *type == _C_OUT
|| *type == _C_BYCOPY
- || *type == _C_ONEWAY)
+ || *type == _C_ONEWAY
+ || *type == _C_GCINVISIBLE)
{
type += 1;
}
@@ -310,6 +415,13 @@ objc_skip_type_qualifiers (const char* type)
const char*
objc_skip_typespec (const char* type)
{
+ /* Skip the variable name if any */
+ if (*type == '"')
+ {
+ for (type++; *type++ != '"';)
+ /* do nothing */;
+ }
+
type = objc_skip_type_qualifiers (type);
switch (*type) {
@@ -339,6 +451,8 @@ objc_skip_typespec (const char* type)
case _C_UINT:
case _C_LNG:
case _C_ULNG:
+ case _C_LNG_LNG:
+ case _C_ULNG_LNG:
case _C_FLT:
case _C_DBL:
case _C_VOID:
@@ -354,7 +468,16 @@ objc_skip_typespec (const char* type)
if (*type == _C_ARY_E)
return ++type;
else
- objc_error(nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type);
+ {
+ objc_error(nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type);
+ return 0;
+ }
+
+ case _C_BFLD:
+ /* The new encoding of bitfields is: b 'position' 'type' 'size' */
+ while (isdigit (*++type)); /* skip position */
+ while (isdigit (*++type)); /* skip type and size */
+ return type;
case _C_STRUCT_B:
/* skip name, and elements until closing '}' */
@@ -376,7 +499,10 @@ objc_skip_typespec (const char* type)
return objc_skip_typespec (++type);
default:
- objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
+ {
+ objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
+ return 0;
+ }
}
}
@@ -526,14 +652,261 @@ objc_get_type_qualifiers (const char* type)
while (flag)
switch (*type++)
{
- case _C_CONST: res |= _F_CONST; break;
- case _C_IN: res |= _F_IN; break;
- case _C_INOUT: res |= _F_INOUT; break;
- case _C_OUT: res |= _F_OUT; break;
- case _C_BYCOPY: res |= _F_BYCOPY; break;
- case _C_ONEWAY: res |= _F_ONEWAY; break;
+ case _C_CONST: res |= _F_CONST; break;
+ case _C_IN: res |= _F_IN; break;
+ case _C_INOUT: res |= _F_INOUT; break;
+ case _C_OUT: res |= _F_OUT; break;
+ case _C_BYCOPY: res |= _F_BYCOPY; break;
+ case _C_ONEWAY: res |= _F_ONEWAY; break;
+ case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
default: flag = NO;
}
return res;
}
+
+
+/* The following three functions can be used to determine how a
+ structure is laid out by the compiler. For example:
+
+ struct objc_struct_layout layout;
+ int i;
+
+ objc_layout_structure (type, &layout);
+ while (objc_layout_structure_next_member (&layout))
+ {
+ int position, align;
+ const char *type;
+
+ objc_layout_structure_get_info (&layout, &position, &align, &type);
+ printf ("element %d has offset %d, alignment %d\n",
+ i++, position, align);
+ }
+
+ These functions are used by objc_sizeof_type and objc_alignof_type
+ functions to compute the size and alignment of structures. The
+ previous method of computing the size and alignment of a structure
+ was not working on some architectures, particulary on AIX, and in
+ the presence of bitfields inside the structure. */
+void
+objc_layout_structure (const char *type,
+ struct objc_struct_layout *layout)
+{
+ const char *ntype;
+
+ if (*type++ != _C_STRUCT_B)
+ {
+ objc_error(nil, OBJC_ERR_BAD_TYPE,
+ "record type expected in objc_layout_structure, got %s\n",
+ type);
+ }
+
+ layout->original_type = type;
+
+ /* Skip "<name>=" if any. Avoid embedded structures and unions. */
+ ntype = type;
+ while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
+ && *ntype++ != '=')
+ /* do nothing */;
+
+ /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
+ if (*(ntype - 1) == '=')
+ type = ntype;
+
+ layout->type = type;
+ layout->prev_type = NULL;
+ layout->record_size = 0;
+ layout->record_align = BITS_PER_UNIT;
+
+#ifdef STRUCTURE_SIZE_BOUNDARY
+ layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
+#endif
+}
+
+
+BOOL
+objc_layout_structure_next_member (struct objc_struct_layout *layout)
+{
+ register int known_align = layout->record_size;
+ register int desired_align = 0;
+
+ /* The following are used only if the field is a bitfield */
+ register const char *bfld_type;
+ register int bfld_type_size, bfld_type_align, bfld_field_size;
+
+ /* The current type without the type qualifiers */
+ const char *type;
+
+#if 1
+ if (layout->prev_type == NULL)
+ {
+ layout->prev_type = layout->type;
+ layout->type = objc_skip_typespec (layout->prev_type);
+ return YES;
+ }
+#endif
+
+ /* Add the size of the previous field to the size of the record. */
+ if (layout->prev_type)
+ {
+ type = objc_skip_type_qualifiers (layout->prev_type);
+
+ if (*type != _C_BFLD)
+ layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
+ else {
+ desired_align = 1;
+ /* Get the bitfield's type */
+ for (bfld_type = type + 1;
+ isdigit(*bfld_type);
+ bfld_type++)
+ /* do nothing */;
+
+ bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
+ bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
+ bfld_field_size = atoi (objc_skip_typespec (bfld_type));
+ layout->record_size += bfld_field_size;
+ }
+ }
+
+ if (*layout->type == _C_STRUCT_E)
+ return NO;
+
+ /* Skip the variable name if any */
+ if (*layout->type == '"')
+ {
+ for (layout->type++; *layout->type++ != '"';)
+ /* do nothing */;
+ }
+
+ type = objc_skip_type_qualifiers (layout->type);
+
+ if (*type != _C_BFLD)
+ desired_align = objc_alignof_type(type) * BITS_PER_UNIT;
+ else
+ {
+ desired_align = 1;
+ /* Skip the bitfield's offset */
+ for (bfld_type = type + 1; isdigit(*bfld_type); bfld_type++)
+ /* do nothing */;
+
+ bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
+ bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
+ bfld_field_size = atoi (objc_skip_typespec (bfld_type));
+ }
+
+#ifdef BIGGEST_FIELD_ALIGNMENT
+ desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
+#endif
+#ifdef ADJUST_FIELD_ALIGN
+ desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
+#endif
+
+ /* Record must have at least as much alignment as any field.
+ Otherwise, the alignment of the field within the record
+ is meaningless. */
+#ifndef PCC_BITFIELD_TYPE_MATTERS
+ layout->record_align = MAX (layout->record_align, desired_align);
+#else
+ if (*type == _C_BFLD)
+ {
+ /* For these machines, a zero-length field does not
+ affect the alignment of the structure as a whole.
+ It does, however, affect the alignment of the next field
+ within the structure. */
+ if (bfld_field_size)
+ layout->record_align = MAX (layout->record_align, desired_align);
+ else
+ desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
+
+ /* A named bit field of declared type `int'
+ forces the entire structure to have `int' alignment.
+ Q1: How is encoded this thing and how to check for it?
+ Q2: How to determine maximum_field_alignment at runtime? */
+
+/* if (DECL_NAME (field) != 0) */
+ {
+ int type_align = bfld_type_align;
+#if 0
+ if (maximum_field_alignment != 0)
+ type_align = MIN (type_align, maximum_field_alignment);
+ else if (DECL_PACKED (field))
+ type_align = MIN (type_align, BITS_PER_UNIT);
+#endif
+
+ layout->record_align = MAX (layout->record_align, type_align);
+ }
+ }
+ else
+ layout->record_align = MAX (layout->record_align, desired_align);
+#endif
+
+ /* Does this field automatically have alignment it needs
+ by virtue of the fields that precede it and the record's
+ own alignment? */
+
+ if (*type == _C_BFLD)
+ layout->record_size = atoi (type + 1);
+ else if (layout->record_size % desired_align != 0)
+ {
+ /* No, we need to skip space before this field.
+ Bump the cumulative size to multiple of field alignment. */
+ layout->record_size = ROUND (layout->record_size, desired_align);
+ }
+
+ /* Jump to the next field in record. */
+
+ layout->prev_type = layout->type;
+ layout->type = objc_skip_typespec (layout->type); /* skip component */
+
+ return YES;
+}
+
+
+void objc_layout_finish_structure (struct objc_struct_layout *layout,
+ unsigned int *size,
+ unsigned int *align)
+{
+ if (layout->type && *layout->type == _C_STRUCT_E)
+ {
+ /* Work out the alignment of the record as one expression and store
+ in the record type. Round it up to a multiple of the record's
+ alignment. */
+
+#ifdef ROUND_TYPE_ALIGN
+ layout->record_align = ROUND_TYPE_ALIGN (layout->original_type,
+ 1,
+ layout->record_align);
+#else
+ layout->record_align = MAX (1, layout->record_align);
+#endif
+
+#ifdef ROUND_TYPE_SIZE
+ layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
+ layout->record_size,
+ layout->record_align);
+#else
+ /* Round the size up to be a multiple of the required alignment */
+ layout->record_size = ROUND (layout->record_size, layout->record_align);
+#endif
+
+ layout->type = NULL;
+ }
+ if (size)
+ *size = layout->record_size / BITS_PER_UNIT;
+ if (align)
+ *align = layout->record_align / BITS_PER_UNIT;
+}
+
+
+void objc_layout_structure_get_info (struct objc_struct_layout *layout,
+ unsigned int *offset,
+ unsigned int *align,
+ const char **type)
+{
+ if (offset)
+ *offset = layout->record_size / BITS_PER_UNIT;
+ if (align)
+ *align = layout->record_align / BITS_PER_UNIT;
+ if (type)
+ *type = layout->prev_type;
+}
diff --git a/gcc/objc/encoding.h b/gcc/objc/encoding.h
index 141e9fe9e97..cbbc8de7ef5 100644
--- a/gcc/objc/encoding.h
+++ b/gcc/objc/encoding.h
@@ -32,26 +32,28 @@ Boston, MA 02111-1307, USA. */
#include <ctype.h>
#include "objc/objc-api.h"
-#define _C_CONST 'r'
-#define _C_IN 'n'
-#define _C_INOUT 'N'
-#define _C_OUT 'o'
-#define _C_BYCOPY 'O'
-#define _C_ONEWAY 'V'
-
-#define _F_CONST 0x01
-#define _F_IN 0x01
-#define _F_OUT 0x02
-#define _F_INOUT 0x03
-#define _F_BYCOPY 0x04
-#define _F_ONEWAY 0x08
-
+#define _C_CONST 'r'
+#define _C_IN 'n'
+#define _C_INOUT 'N'
+#define _C_OUT 'o'
+#define _C_BYCOPY 'O'
+#define _C_ONEWAY 'V'
+#define _C_GCINVISIBLE '!'
+
+#define _F_CONST 0x01
+#define _F_IN 0x01
+#define _F_OUT 0x02
+#define _F_INOUT 0x03
+#define _F_BYCOPY 0x04
+#define _F_ONEWAY 0x08
+#define _F_GCINVISIBLE 0x10
int objc_aligned_size (const char* type);
int objc_sizeof_type (const char* type);
int objc_alignof_type (const char* type);
int objc_aligned_size (const char* type);
int objc_promoted_size (const char* type);
+
const char* objc_skip_type_qualifiers (const char* type);
const char* objc_skip_typespec (const char* type);
const char* objc_skip_offset (const char* type);
@@ -72,4 +74,24 @@ char* method_get_nth_argument (struct objc_method* m,
unsigned objc_get_type_qualifiers (const char* type);
+struct objc_struct_layout
+{
+ const char *original_type;
+ const char *type;
+ const char *prev_type;
+ unsigned int record_size;
+ unsigned int record_align;
+};
+
+void objc_layout_structure (const char *type,
+ struct objc_struct_layout *layout);
+BOOL objc_layout_structure_next_member (struct objc_struct_layout *layout);
+void objc_layout_finish_structure (struct objc_struct_layout *layout,
+ unsigned int *size,
+ unsigned int *align);
+void objc_layout_structure_get_info (struct objc_struct_layout *layout,
+ unsigned int *offset,
+ unsigned int *align,
+ const char **type);
+
#endif /* __encoding_INCLUDE_GNU */
diff --git a/gcc/objc/gc.c b/gcc/objc/gc.c
new file mode 100644
index 00000000000..8abf493e20a
--- /dev/null
+++ b/gcc/objc/gc.c
@@ -0,0 +1,458 @@
+/* Basic data types for Objective C.
+ Copyright (C) 1998 Free Software Foundation, Inc.
+ Contributed by Ovidiu Predescu.
+
+This file is part of GNU CC.
+
+GNU CC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU CC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU CC; see the file COPYING. If not, write to
+the Free Software Foundation, 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA. */
+
+/* As a special exception, if you link this library with files
+ compiled with GCC to produce an executable, this does not cause
+ the resulting executable to be covered by the GNU General Public License.
+ This exception does not however invalidate any other reasons why
+ the executable file might be covered by the GNU General Public License. */
+
+#include "../tconfig.h"
+#include "objc.h"
+#include "encoding.h"
+
+#include <assert.h>
+#include <string.h>
+
+#if OBJC_WITH_GC
+
+#include <gc.h>
+
+/* gc_typed.h uses the following but doesn't declare them */
+typedef GC_word word;
+typedef GC_signed_word signed_word;
+
+#if BITS_PER_WORD == 32
+# define LOGWL 5
+# define modWORDSZ(n) ((n) & 0x1f) /* n mod size of word */
+#endif
+
+#if BITS_PER_WORD == 64
+# define LOGWL 6
+# define modWORDSZ(n) ((n) & 0x3f) /* n mod size of word */
+#endif
+
+#define divWORDSZ(n) ((n) >> LOGWL) /* divide n by size of word */
+
+#include <gc_typed.h>
+
+/* The following functions set up in `mask` the corresponding pointers.
+ The offset is incremented with the size of the type. */
+
+#define ROUND(V, A) \
+ ({ typeof(V) __v=(V); typeof(A) __a=(A); \
+ __a*((__v+__a-1)/__a); })
+
+#define SET_BIT_FOR_OFFSET(mask, offset) \
+ GC_set_bit(mask, offset / sizeof (void*))
+
+/* Some prototypes */
+static void
+__objc_gc_setup_struct (GC_bitmap mask, const char *type, int offset);
+static void
+__objc_gc_setup_union (GC_bitmap mask, const char *type, int offset);
+
+
+static void
+__objc_gc_setup_array (GC_bitmap mask, const char *type, int offset)
+{
+ int i, len = atoi(type + 1);
+
+ while (isdigit(*++type))
+ /* do nothing */; /* skip the size of the array */
+
+ switch (*type) {
+ case _C_ARY_B:
+ for (i = 0; i < len; i++)
+ __objc_gc_setup_array (mask, type, offset);
+ break;
+
+ case _C_STRUCT_B:
+ for (i = 0; i < len; i++)
+ __objc_gc_setup_struct (mask, type, offset);
+ break;
+
+ case _C_UNION_B:
+ for (i = 0; i < len; i++)
+ __objc_gc_setup_union (mask, type, offset);
+ break;
+
+ default:
+ break;
+ }
+}
+
+static void
+__objc_gc_setup_struct (GC_bitmap mask, const char *type, int offset)
+{
+ struct objc_struct_layout layout;
+ unsigned int position;
+ const char *mtype;
+
+ objc_layout_structure (type, &layout);
+
+ while (objc_layout_structure_next_member (&layout))
+ {
+ BOOL gc_invisible = NO;
+
+ objc_layout_structure_get_info (&layout, &position, NULL, &mtype);
+
+ /* Skip the variable name */
+ if (*mtype == '"')
+ {
+ for (mtype++; *mtype++ != '"';)
+ /* do nothing */;
+ }
+
+ if (*mtype == _C_GCINVISIBLE)
+ {
+ gc_invisible = YES;
+ mtype++;
+ }
+
+ /* Add to position the offset of this structure */
+ position += offset;
+
+ switch (*mtype) {
+ case _C_ID:
+ case _C_CLASS:
+ case _C_SEL:
+ case _C_PTR:
+ case _C_CHARPTR:
+ case _C_ATOM:
+ if (!gc_invisible)
+ SET_BIT_FOR_OFFSET(mask, position);
+ break;
+
+ case _C_ARY_B:
+ __objc_gc_setup_array (mask, mtype, position);
+ break;
+
+ case _C_STRUCT_B:
+ __objc_gc_setup_struct (mask, mtype, position);
+ break;
+
+ case _C_UNION_B:
+ __objc_gc_setup_union (mask, mtype, position);
+ break;
+
+ default:
+ break;
+ }
+ }
+}
+
+static void
+__objc_gc_setup_union (GC_bitmap mask, const char *type, int offset)
+{
+ /* Sub-optimal, quick implementation: assume the union is made of
+ pointers, set up the mask accordingly. */
+
+ int i, size, align;
+
+ /* Skip the variable name */
+ if (*type == '"')
+ {
+ for (type++; *type++ != '"';)
+ /* do nothing */;
+ }
+
+ size = objc_sizeof_type (type);
+ align = objc_alignof_type (type);
+
+ offset = ROUND(offset, align);
+ for (i = 0; i < size; i += sizeof (void*))
+ {
+ SET_BIT_FOR_OFFSET(mask, offset);
+ offset += sizeof (void*);
+ }
+}
+
+
+/* Iterates over the types in the structure that represents the class
+ encoding and sets the bits in mask according to each ivar type. */
+static void
+__objc_gc_type_description_from_type (GC_bitmap mask, const char *type)
+{
+ struct objc_struct_layout layout;
+ unsigned int offset, align;
+ const char *ivar_type;
+
+ objc_layout_structure (type, &layout);
+
+ while (objc_layout_structure_next_member (&layout))
+ {
+ BOOL gc_invisible = NO;
+
+ objc_layout_structure_get_info (&layout, &offset, &align, &ivar_type);
+
+ /* Skip the variable name */
+ if (*ivar_type == '"')
+ {
+ for (ivar_type++; *ivar_type++ != '"';)
+ /* do nothing */;
+ }
+
+ if (*ivar_type == _C_GCINVISIBLE)
+ {
+ gc_invisible = YES;
+ ivar_type++;
+ }
+
+ switch (*ivar_type) {
+ case _C_ID:
+ case _C_CLASS:
+ case _C_SEL:
+ case _C_PTR:
+ case _C_CHARPTR:
+ if (!gc_invisible)
+ SET_BIT_FOR_OFFSET(mask, offset);
+ break;
+
+ case _C_ARY_B:
+ __objc_gc_setup_array (mask, ivar_type, offset);
+ break;
+
+ case _C_STRUCT_B:
+ __objc_gc_setup_struct (mask, ivar_type, offset);
+ break;
+
+ case _C_UNION_B:
+ __objc_gc_setup_union (mask, ivar_type, offset);
+ break;
+
+ default:
+ break;
+ }
+ }
+}
+
+/* Computes in *type the full type encoding of this class including
+ its super classes. '*size' gives the total number of bytes allocated
+ into *type, '*current' the number of bytes used so far by the
+ encoding. */
+static void
+__objc_class_structure_encoding (Class class, char **type, int *size,
+ int *current)
+{
+ int i, ivar_count;
+ struct objc_ivar_list* ivars;
+
+ if (!class)
+ {
+ strcat (*type, "{");
+ *current++;
+ return;
+ }
+
+ /* Add the type encodings of the super classes */
+ __objc_class_structure_encoding (class->super_class, type, size, current);
+
+ ivars = class->ivars;
+ if (!ivars)
+ return;
+
+ ivar_count = ivars->ivar_count;
+
+ for (i = 0; i < ivar_count; i++)
+ {
+ struct objc_ivar *ivar = &(ivars->ivar_list[i]);
+ const char *ivar_type = ivar->ivar_type;
+ int len = strlen (ivar_type);
+
+ if (*current + len + 1 >= *size)
+ {
+ /* Increase the size of the encoding string so that it
+ contains this ivar's type. */
+ *size = ROUND(*current + len + 1, 10);
+ *type = objc_realloc (*type, *size);
+ }
+ strcat (*type + *current, ivar_type);
+ *current += len;
+ }
+}
+
+
+/* Allocates the memory that will hold the type description for class
+ and calls the __objc_class_structure_encoding that generates this
+ value. */
+void
+__objc_generate_gc_type_description (Class class)
+{
+ GC_bitmap mask;
+ int bits_no, size;
+ int type_size = 10, current;
+ char *class_structure_type;
+
+ if (!CLS_ISCLASS(class))
+ return;
+
+ /* We have to create a mask in which each bit counts for a pointer member.
+ We take into consideration all the non-pointer instance variables and we
+ round them up to the alignment. */
+
+ /* The number of bits in the mask is the size of an instance in bytes divided
+ by the size of a pointer. */
+ bits_no = (ROUND(class_get_instance_size (class), sizeof(void*))
+ / sizeof (void*));
+ size = ROUND(bits_no, BITS_PER_WORD) / BITS_PER_WORD;
+ mask = objc_atomic_malloc (size * sizeof (int));
+ memset (mask, 0, size * sizeof (int));
+
+ class_structure_type = objc_atomic_malloc (type_size);
+ *class_structure_type = current = 0;
+ __objc_class_structure_encoding (class, &class_structure_type,
+ &type_size, &current);
+ if (current + 1 == type_size)
+ class_structure_type = objc_realloc (class_structure_type, ++type_size);
+ strcat (class_structure_type + current, "}");
+// printf ("type description for '%s' is %s\n", class->name, class_structure_type);
+
+ __objc_gc_type_description_from_type (mask, class_structure_type);
+ objc_free (class_structure_type);
+
+#define DEBUG 1
+#ifdef DEBUG
+ printf (" mask for '%s', type '%s' (bits %d, mask size %d) is:",
+ class_structure_type, class->name, bits_no, size);
+ {
+ int i;
+ for (i = 0; i < size; i++)
+ printf (" %lx", mask[i]);
+ }
+ puts ("");
+#endif
+
+ class->gc_object_type = (void*)GC_make_descriptor (mask, bits_no);
+}
+
+
+/* Returns YES if type denotes a pointer type, NO otherwise */
+static inline BOOL
+__objc_ivar_pointer (const char *type)
+{
+ type = objc_skip_type_qualifiers (type);
+
+ return (*type == _C_ID
+ || *type == _C_CLASS
+ || *type == _C_SEL
+ || *type == _C_PTR
+ || *type == _C_CHARPTR
+ || *type == _C_ATOM);
+}
+
+
+/* Mark the instance variable whose name is given by ivarname as a
+ weak pointer (a pointer hidden to the garbage collector) if
+ gc_invisible is true. If gc_invisible is false it unmarks the
+ instance variable and makes it a normal pointer, visible to the
+ garbage collector.
+
+ This operation only makes sense on instance variables that are
+ pointers. */
+void
+class_ivar_set_gcinvisible (Class class, const char* ivarname,
+ BOOL gc_invisible)
+{
+ int i, ivar_count;
+ struct objc_ivar_list* ivars;
+
+ if (!class || !ivarname)
+ return;
+
+ ivars = class->ivars;
+ if (!ivars)
+ return;
+
+ ivar_count = ivars->ivar_count;
+
+ for (i = 0; i < ivar_count; i++)
+ {
+ struct objc_ivar *ivar = &(ivars->ivar_list[i]);
+ const char *type;
+
+ if (!ivar->ivar_name || strcmp (ivar->ivar_name, ivarname))
+ continue;
+
+ assert (ivar->ivar_type);
+ type = ivar->ivar_type;
+
+ /* Skip the variable name */
+ if (*type == '"')
+ {
+ for (type++; *type++ != '"';)
+ /* do nothing */;
+ }
+
+ if (*type == _C_GCINVISIBLE)
+ {
+ char *new_type;
+
+ if (gc_invisible || !__objc_ivar_pointer (type))
+ return; /* The type of the variable already matches the
+ requested gc_invisible type */
+
+ /* The variable is gc_invisible and we have to reverse it */
+ new_type = objc_atomic_malloc (strlen (ivar->ivar_type));
+ strncpy (new_type, ivar->ivar_type,
+ (size_t)(type - ivar->ivar_type));
+ strcat (new_type, type + 1);
+ ivar->ivar_type = new_type;
+ }
+ else
+ {
+ char *new_type;
+
+ if (!gc_invisible || !__objc_ivar_pointer (type))
+ return; /* The type of the variable already matches the
+ requested gc_invisible type */
+
+ /* The variable is gc visible and we have to make it gc_invisible */
+ new_type = objc_malloc (strlen (ivar->ivar_type) + 2);
+ strncpy (new_type, ivar->ivar_type,
+ (size_t)(type - ivar->ivar_type));
+ strcat (new_type, "!");
+ strcat (new_type, type);
+ ivar->ivar_type = new_type;
+ }
+
+ __objc_generate_gc_type_description (class);
+ return;
+ }
+
+ /* Search the instance variable in the superclasses */
+ class_ivar_set_gcinvisible (class->super_class, ivarname, gc_invisible);
+}
+
+#else /* !OBJC_WITH_GC */
+
+void
+__objc_generate_gc_type_description (Class class)
+{
+}
+
+void class_ivar_set_gcinvisible (Class class,
+ const char* ivarname,
+ BOOL gc_invisible)
+{
+}
+
+#endif /* OBJC_WITH_GC */
diff --git a/gcc/objc/misc.c b/gcc/objc/misc.c
index 8fc8a3fa6c8..7339888dc28 100644
--- a/gcc/objc/misc.c
+++ b/gcc/objc/misc.c
@@ -1,6 +1,6 @@
/* GNU Objective C Runtime Miscellaneous
Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
- Contrbuted by Kresten Krab Thorup
+ Contributed by Kresten Krab Thorup
This file is part of GNU CC.
@@ -144,9 +144,37 @@ objc_free(void *mem)
** Users should call the normal objc routines above for
** memory allocation and disposal within their programs.
*/
+
+#if OBJC_WITH_GC
+#include <gc.h>
+
+static void *GC_calloc (size_t nelem, size_t size)
+{
+ void* p = GC_malloc (nelem * size);
+ if (!p)
+ objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted!\n");
+
+ memset (p, 0, nelem * size);
+ return p;
+}
+
+static void noFree (void* p) {}
+
+void *(*_objc_malloc)(size_t) = GC_malloc;
+void *(*_objc_atomic_malloc)(size_t) = GC_malloc_atomic;
+void *(*_objc_valloc)(size_t) = GC_malloc;
+void *(*_objc_realloc)(void *, size_t) = GC_realloc;
+void *(*_objc_calloc)(size_t, size_t) = GC_calloc;
+void (*_objc_free)(void *) = noFree;
+
+#else
+
void *(*_objc_malloc)(size_t) = malloc;
void *(*_objc_atomic_malloc)(size_t) = malloc;
void *(*_objc_valloc)(size_t) = malloc;
void *(*_objc_realloc)(void *, size_t) = realloc;
void *(*_objc_calloc)(size_t, size_t) = calloc;
void (*_objc_free)(void *) = free;
+
+
+#endif
diff --git a/gcc/objc/objc-act.c b/gcc/objc/objc-act.c
index 0f4058b2b5c..0dc5ef05398 100644
--- a/gcc/objc/objc-act.c
+++ b/gcc/objc/objc-act.c
@@ -3380,6 +3380,7 @@ build_selector_template ()
struct objc_class *sibling_class;
}
struct objc_protocol_list *protocols;
+ void *gc_object_type;
}; */
static void
@@ -3515,6 +3516,21 @@ build_class_template ()
decl_specs, NULL_TREE);
chainon (field_decl_chain, field_decl);
+ /* void *sel_id; */
+
+ decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_VOID]);
+ field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("sel_id"));
+ field_decl
+ = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
+ chainon (field_decl_chain, field_decl);
+
+ /* void *gc_object_type; */
+
+ decl_specs = build_tree_list (NULL_TREE, ridpointers[(int) RID_VOID]);
+ field_decl = build1 (INDIRECT_REF, NULL_TREE, get_identifier ("gc_object_type"));
+ field_decl
+ = grokfield (input_filename, lineno, field_decl, decl_specs, NULL_TREE);
+ chainon (field_decl_chain, field_decl);
finish_struct (objc_class_template, field_decl_chain, NULL_TREE);
}
@@ -4287,6 +4303,7 @@ build_category_initializer (type, cat_name, class_name,
struct objc_class *sibling_class;
}
struct objc_protocol_list *protocols;
+ void *gc_object_type;
}; */
static tree
@@ -4377,6 +4394,9 @@ build_shared_structure_initializer (type, isa, super, name, size, status,
initlist = tree_cons (NULL_TREE, expr, initlist);
}
+ /* gc_object_type = NULL */
+ initlist = tree_cons (NULL_TREE, build_int_2 (0, 0), initlist);
+
return build_constructor (type, nreverse (initlist));
}
@@ -6819,6 +6839,62 @@ encode_type (type, curtype, format)
}
static void
+encode_complete_bitfield (int position, tree type, int size)
+{
+ enum tree_code code = TREE_CODE (type);
+ char buffer[40];
+ char charType = '?';
+
+ if (code == INTEGER_TYPE)
+ {
+ if (TREE_INT_CST_LOW (TYPE_MIN_VALUE (type)) == 0
+ && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) == 0)
+ {
+ /* Unsigned integer types. */
+
+ if (TYPE_MODE (type) == QImode)
+ charType = 'C';
+ else if (TYPE_MODE (type) == HImode)
+ charType = 'S';
+ else if (TYPE_MODE (type) == SImode)
+ {
+ if (type == long_unsigned_type_node)
+ charType = 'L';
+ else
+ charType = 'I';
+ }
+ else if (TYPE_MODE (type) == DImode)
+ charType = 'Q';
+ }
+
+ else
+ /* Signed integer types. */
+ {
+ if (TYPE_MODE (type) == QImode)
+ charType = 'c';
+ else if (TYPE_MODE (type) == HImode)
+ charType = 's';
+ else if (TYPE_MODE (type) == SImode)
+ {
+ if (type == long_integer_type_node)
+ charType = 'l';
+ else
+ charType = 'i';
+ }
+
+ else if (TYPE_MODE (type) == DImode)
+ charType = 'q';
+ }
+ }
+
+ else
+ abort ();
+
+ sprintf (buffer, "b%d%c%d", position, charType, size);
+ obstack_grow (&util_obstack, buffer, strlen (buffer));
+}
+
+static void
encode_field_decl (field_decl, curtype, format)
tree field_decl;
int curtype;
@@ -6826,18 +6902,36 @@ encode_field_decl (field_decl, curtype, format)
{
tree type;
- /* If this field is obviously a bitfield, or is a bitfield that has been
+ type = TREE_TYPE (field_decl);
+
+ /* If this field is obviously a bitfield, or is a bitfield that has been
clobbered to look like a ordinary integer mode, go ahead and generate
the bitfield typing information. */
- type = TREE_TYPE (field_decl);
- if (DECL_BIT_FIELD (field_decl))
- encode_bitfield (DECL_FIELD_SIZE (field_decl), format);
- else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
- && DECL_FIELD_SIZE (field_decl)
- && TYPE_MODE (type) > DECL_MODE (field_decl))
- encode_bitfield (DECL_FIELD_SIZE (field_decl), format);
+ if (flag_next_runtime)
+ {
+ if (DECL_BIT_FIELD (field_decl))
+ encode_bitfield (DECL_FIELD_SIZE (field_decl), format);
+ else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
+ && DECL_FIELD_SIZE (field_decl)
+ && TYPE_MODE (type) > DECL_MODE (field_decl))
+ encode_bitfield (DECL_FIELD_SIZE (field_decl), format);
+ else
+ encode_type (TREE_TYPE (field_decl), curtype, format);
+ }
else
- encode_type (TREE_TYPE (field_decl), curtype, format);
+ {
+ if (DECL_BIT_FIELD (field_decl)
+ || (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
+ && DECL_FIELD_SIZE (field_decl)
+ && TYPE_MODE (type) > DECL_MODE (field_decl)))
+ {
+ encode_complete_bitfield (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field_decl)),
+ DECL_BIT_FIELD_TYPE (field_decl),
+ DECL_FIELD_SIZE (field_decl));
+ }
+ else
+ encode_type (TREE_TYPE (field_decl), curtype, format);
+ }
}
static tree
diff --git a/gcc/objc/objc-api.h b/gcc/objc/objc-api.h
index 9eb000b5b39..fe34dafb895 100644
--- a/gcc/objc/objc-api.h
+++ b/gcc/objc/objc-api.h
@@ -59,6 +59,8 @@ struct objc_method_description
#define _C_UINT 'I'
#define _C_LNG 'l'
#define _C_ULNG 'L'
+#define _C_LNG_LNG 'q'
+#define _C_ULNG_LNG 'Q'
#define _C_FLT 'f'
#define _C_DBL 'd'
#define _C_BFLD 'b'
@@ -495,6 +497,17 @@ class_set_version(Class class, long version)
class->version = version;
}
+static inline void *
+class_get_gc_object_type (Class class)
+{
+ return CLS_ISCLASS(class) ? class->gc_object_type : NULL;
+}
+
+/* Mark the instance variable as innaccessible to the garbage collector */
+extern void class_ivar_set_gcinvisible (Class class,
+ const char* ivarname,
+ BOOL gcInvisible);
+
static inline IMP
method_get_imp(Method_t method)
{
diff --git a/gcc/objc/objc-features.texi b/gcc/objc/objc-features.texi
new file mode 100644
index 00000000000..ec3b5a6284d
--- /dev/null
+++ b/gcc/objc/objc-features.texi
@@ -0,0 +1,392 @@
+\input texinfo @c -*-texinfo-*-
+@c %**start of header
+@setfilename objc-features.info
+@settitle GNU Objective-C runtime features
+@setchapternewpage odd
+@c %**end of header
+
+@node Top, Executing code before main, , (dir), (dir)
+@comment node-name, next, previous, up
+
+@chapter GNU Objective-C runtime features
+
+This document is meant to describe some of the GNU Objective-C runtime
+features. It is not intended to teach you Objective-C, there are several
+resources on the Internet that present the language. Questions and
+comments about this document to Ovidiu Predescu
+@code{<ovidiu@@aracnet.com>}.
+
+@menu
+* Executing code before main::
+* Type encoding::
+* Garbage Collection::
+@end menu
+
+
+@node Executing code before main, What you can and what you cannot do in +load, Top, Top
+@section @code{+load}: Executing code before main
+
+
+The GNU Objective-C runtime provides a way that allows you to execute
+code before the execution of the program enters the @code{main}
+function. The code is executed on a per-class and a per-category basis,
+through a special class method @code{+load}.
+
+This facility is very useful if you want to initialize global variables
+which can be accessed by the program directly, without sending a message
+to the class first. The usual way to initialize global variables, in the
+@code{+initialize} method, might not be useful because
+@code{+initialize} is only called when the first message is sent to a
+class object, which in some cases could be too late.
+
+Suppose for example you have a @code{FileStream} class that declares
+@code{Stdin}, @code{Stdout} and @code{Stderr} as global variables, like
+below:
+
+@example
+
+FileStream *Stdin = nil;
+FileStream *Stdout = nil;
+FileStream *Stderr = nil;
+
+@@implementation FileStream
+
++ (void)initialize
+@{
+ Stdin = [[FileStream new] initWithFd:0];
+ Stdout = [[FileStream new] initWithFd:1];
+ Stderr = [[FileStream new] initWithFd:2];
+@}
+
+/* Other methods here */
+@@end
+
+@end example
+
+In this example, the initialization of @code{Stdin}, @code{Stdout} and
+@code{Stderr} in @code{+initialize} occurs too late. The programmer can
+send a message to one of these objects before the variables are actually
+initialized, thus sending messages to the @code{nil} object. The
+@code{+initialize} method which actually initializes the global
+variables is not invoked until the first message is sent to the class
+object. The solution would require these variables to be initialized
+just before entering @code{main}.
+
+The correct solution of the above problem is to use the @code{+load}
+method instead of @code{+initialize}:
+
+@example
+
+@@implementation FileStream
+
++ (void)load
+@{
+ Stdin = [[FileStream new] initWithFd:0];
+ Stdout = [[FileStream new] initWithFd:1];
+ Stderr = [[FileStream new] initWithFd:2];
+@}
+
+/* Other methods here */
+@@end
+
+@end example
+
+The @code{+load} is a method that is not overridden by categories. If a
+class and a category of it both implement @code{+load}, both methods are
+invoked. This allows some additional initializations to be performed in
+a category.
+
+This mechanism is not intended to be a replacement for @code{+initialize}.
+You should be aware of its limitations when you decide to use it
+instead of @code{+initialize}.
+
+@menu
+* What you can and what you cannot do in +load::
+@end menu
+
+
+@node What you can and what you cannot do in +load, Type encoding, Executing code before main, Executing code before main
+@subsection What you can and what you cannot do in @code{+load}
+
+The +load implementation in the GNU runtime guarantees you the following
+things:
+
+@itemize @bullet
+
+@item
+you can write whatever C code you like;
+
+@item
+you can send messages to Objective-C constant strings (@@"this is a
+constant string");
+
+@item
+you can allocate and send messages to objects whose class is implemented
+in the same file;
+
+@item
+the @code{+load} implementation of all super classes of a class are executed before the @code{+load} of that class is executed;
+
+@item
+the @code{+load} implementation of a class is executed before the
+@code{+load} implementation of any category.
+
+@end itemize
+
+In particular, the following things, even if they can work in a
+particular case, are not guaranteed:
+
+@itemize @bullet
+
+@item
+allocation of or sending messages to arbitrary objects;
+
+@item
+allocation of or sending messages to objects whose classes have a
+category implemented in the same file;
+
+@end itemize
+
+You should make no assumptions about receiving @code{+load} in sibling
+classes when you write @code{+load} of a class. The order in which
+sibling classes receive @code{+load} is not guaranteed.
+
+The order in which @code{+load} and @code{+initialize} are called could
+be problematic if this matters. If you don't allocate objects inside
+@code{+load}, it is guaranteed that @code{+load} is called before
+@code{+initialize}. If you create an object inside @code{+load} the
+@code{+initialize} method of object's class is invoked even if
+@code{+load} was not invoked. Note if you explicitly call @code{+load}
+on a class, @code{+initialize} will be called first. To avoid possible
+problems try to implement only one of these methods.
+
+The @code{+load} method is also invoked when a bundle is dynamically
+loaded into your running program. This happens automatically without any
+intervening operation from you. When you write bundles and you need to
+write @code{+load} you can safely create and send messages to objects whose
+classes already exist in the running program. The same restrictions as
+above apply to classes defined in bundle.
+
+
+
+@node Type encoding, Garbage Collection, What you can and what you cannot do in +load, Top
+@section Type encoding
+
+The Objective-C compiler generates type encodings for all the
+types. These type encodings are used at runtime to find out information
+about selectors and methods and about objects and classes.
+
+The types are encoded in the following way:
+
+@c @sp 1
+
+@multitable @columnfractions .25 .75
+@item @code{char}
+@tab @code{c}
+@item @code{unsigned char}
+@tab @code{C}
+@item @code{short}
+@tab @code{s}
+@item @code{unsigned short}
+@tab @code{S}
+@item @code{int}
+@tab @code{i}
+@item @code{unsigned int}
+@tab @code{I}
+@item @code{long}
+@tab @code{l}
+@item @code{unsigned long}
+@tab @code{L}
+@item @code{long long}
+@tab @code{q}
+@item @code{unsigned long long}
+@tab @code{Q}
+@item @code{float}
+@tab @code{f}
+@item @code{double}
+@tab @code{d}
+@item @code{void}
+@tab @code{v}
+@item @code{id}
+@tab @code{@@}
+@item @code{Class}
+@tab @code{#}
+@item @code{SEL}
+@tab @code{:}
+@item @code{char*}
+@tab @code{*}
+@item unknown type
+@tab @code{?}
+@item bitfields
+@tab @code{b} followed by the starting position of the bitfield, the type of the bitfield and the size of the bitfield (the bitfields encoding was changed from the NeXT's compiler encoding, see below)
+@end multitable
+
+@c @sp 1
+
+The encoding of bitfields has changed to allow bitfields to be properly
+handled by the runtime functions that compute sizes and alignments of
+types that contain bitfields. The previous encoding contained only the
+size of the bitfield. Using only this information it is not possible to
+reliably compute the size occupied by the bitfield. This is very
+important in the presence of the Boehm's garbage collector because the
+objects are allocated using the typed memory facility available in this
+collector. The typed memory allocation requires information about where
+the pointers are located inside the object.
+
+The position in the bitfield is the position, counting in bits, of the
+bit closest to the beginning of the structure.
+
+The non-atomic types are encoded as follows:
+
+@c @sp 1
+
+@multitable @columnfractions .2 .8
+@item pointers
+@tab @code{'^'} followed by the pointed type.
+@item arrays
+@tab @code{'['} followed by the number of elements in the array followed by the type of the elements followed by @code{']'}
+@item structures
+@tab @code{'@{'} followed by the name of the structure (or '?' if the structure is unnamed), the '=' sign, the type of the members and by @code{'@}'}
+@item unions
+@tab @code{'('} followed by the name of the structure (or '?' if the union is unnamed), the '=' sign, the type of the members followed by @code{')'}
+@end multitable
+
+Here are some types and their encodings, as they are generated by the
+compiler on a i386 machine:
+
+@sp 1
+
+@multitable @columnfractions .25 .75
+@item Objective-C type
+@tab Compiler encoding
+@item
+@example
+int a[10];
+@end example
+@tab @code{[10i]}
+@item
+@example
+struct @{
+ int i;
+ float f[3];
+ int a:3;
+ int b:2;
+ char c;
+@}
+@end example
+@tab @code{@{?=i[3f]b128i3b131i2c@}}
+@end multitable
+
+@sp 1
+
+In addition to the types the compiler also encodes the type
+specifiers. The table below describes the encoding of the current
+Objective-C type specifiers:
+
+@sp 1
+
+@multitable @columnfractions .25 .75
+@item Specifier
+@tab Encoding
+@item @code{const}
+@tab @code{r}
+@item @code{in}
+@tab @code{n}
+@item @code{inout}
+@tab @code{N}
+@item @code{out}
+@tab @code{o}
+@item @code{bycopy}
+@tab @code{O}
+@item @code{oneway}
+@tab @code{V}
+@end multitable
+
+@sp 1
+
+The type specifiers are encoded just before the type. Unlike types
+however, the type specifiers are only encoded when they appear in method
+argument types.
+
+
+@node Garbage Collection, , Type encoding, Top
+
+@page
+@section Garbage Collection
+
+Support for a new memory management policy has been added by using a
+powerful conservative garbage collector, known as the
+Boehm-Demers-Weiser conservative garbage collector. It is available from
+@w{@url{http://reality.sgi.com/employees/boehm_mti/gc.html}}.
+
+To enable the support for it you have to configure the compiler using an
+additional argument, @w{@kbd{--enable-objc-gc}}. You need to have
+garbage collector installed before building the compiler. This will
+build an additional runtime library which has several enhancements to
+support the garbage collector. The new library has a new name,
+@kbd{libobjc_gc.a} to not conflict with the non-garbage-collected
+library.
+
+When the garbage collector is used, the objects are allocated using the
+so-called typed memory allocation mechanism available in the
+Boehm-Demers-Weiser collector. This mode requires precise information on
+where pointers are located inside objects. This information is computed
+once per class, immediately after the class has been initialized.
+
+There is a new runtime function @code{class_ivar_set_gcinvisible()}
+which can be used to declare a so-called @strong{weak pointer}
+reference. Such a pointer is basically hidden for the garbage collector;
+this can be useful in certain situations, especially when you want to
+keep track of the allocated objects, yet allow them to be
+collected. This kind of pointers can only be members of objects, you
+cannot declare a global pointer as a weak reference. Every type which is
+a pointer type can be declared a weak pointer, including @code{id},
+@code{Class} and @code{SEL}.
+
+Here is an example of how to use this feature. Suppose you want to
+implement a class whose instances hold a weak pointer reference; the
+following class does this:
+
+@example
+
+@@interface WeakPointer : Object
+@{
+ const void* weakPointer;
+@}
+
+- initWithPointer:(const void*)p;
+- (const void*)weakPointer;
+@@end
+
+
+@@implementation WeakPointer
+
++ (void)initialize
+@{
+ class_ivar_set_gcinvisible (self, "weakPointer", YES);
+@}
+
+- initWithPointer:(const void*)p
+@{
+ weakPointer = p;
+ return self;
+@}
+
+- (const void*)weakPointer
+@{
+ return weakPointer;
+@}
+
+@@end
+
+@end example
+
+Weak pointers are supported through a new type character specifier
+represented by the @code{'!'} character. The
+@code{class_ivar_set_gcinvisible()} function adds or removes this
+specifier to the string type description of the instance variable named
+as argument.
+
+
+@bye
+
diff --git a/gcc/objc/objc.h b/gcc/objc/objc.h
index e48b0fd5bfb..79b2519e576 100644
--- a/gcc/objc/objc.h
+++ b/gcc/objc/objc.h
@@ -126,6 +126,7 @@ struct objc_class {
struct objc_class* sibling_class;
struct objc_protocol_list *protocols; /* Protocols conformed to */
+ void* gc_object_type;
};
#ifndef __OBJC__
diff --git a/gcc/objc/objects.c b/gcc/objc/objects.c
index 3e68334c924..d079c5d4ec7 100644
--- a/gcc/objc/objects.c
+++ b/gcc/objc/objects.c
@@ -25,8 +25,13 @@ Boston, MA 02111-1307, USA. */
covered by the GNU General Public License. */
#include "../tconfig.h" /* include defs of bzero for target */
+#include "objc.h"
#include "runtime.h" /* the kitchen sink */
+#if OBJC_WITH_GC
+# include <gc.h>
+#endif
+
id __objc_object_alloc(Class);
id __objc_object_dispose(id);
id __objc_object_copy(id);
@@ -39,8 +44,16 @@ id
class_create_instance(Class class)
{
id new = nil;
+
+#if OBJC_WITH_GC
+ if (CLS_ISCLASS(class))
+ new = (id)GC_malloc_explicitly_typed (class->instance_size,
+ class->gc_object_type);
+#else
if (CLS_ISCLASS(class))
new = (*_objc_object_alloc)(class);
+#endif
+
if (new!=nil)
{
memset (new, 0, class->instance_size);
diff --git a/gcc/objc/sendmsg.c b/gcc/objc/sendmsg.c
index 245b8b9eaf7..17f8b974013 100644
--- a/gcc/objc/sendmsg.c
+++ b/gcc/objc/sendmsg.c
@@ -244,10 +244,7 @@ __objc_init_install_dtable(id receiver, SEL op)
/* Install real dtable for factory methods */
__objc_install_dispatch_table_for_class (receiver->class_pointer);
- if (strcmp (sel_get_name (op), "initialize"))
- __objc_send_initialize((Class)receiver);
- else
- CLS_SETINITIALIZED((Class)receiver);
+ __objc_send_initialize((Class)receiver);
}
objc_mutex_unlock(__objc_runtime_mutex);
}
@@ -273,36 +270,36 @@ __objc_send_initialize(Class class)
{
CLS_SETINITIALIZED(class);
CLS_SETINITIALIZED(class->class_pointer);
-
+
+ /* Create the garbage collector type memory description */
+ __objc_generate_gc_type_description (class);
+
if(class->super_class)
__objc_send_initialize(class->super_class);
{
- SEL op = sel_register_name ("initialize");
- Class tmpclass = class;
- IMP imp = 0;
-
- while (!imp && tmpclass) {
- MethodList_t method_list = tmpclass->class_pointer->methods;
-
- while(!imp && method_list) {
- int i;
- Method_t method;
-
- for (i=0;i<method_list->method_count;i++) {
- method = &(method_list->method_list[i]);
- if (method->method_name
- && method->method_name->sel_id == op->sel_id) {
- imp = method->method_imp;
- break;
- }
- }
+ SEL op = sel_register_name ("initialize");
+ IMP imp = 0;
+ MethodList_t method_list = class->class_pointer->methods;
+
+ while (method_list) {
+ int i;
+ Method_t method;
+
+ for (i = 0; i< method_list->method_count; i++) {
+ method = &(method_list->method_list[i]);
+ if (method->method_name
+ && method->method_name->sel_id == op->sel_id) {
+ imp = method->method_imp;
+ break;
+ }
+ }
- method_list = method_list->method_next;
+ if (imp)
+ break;
- }
+ method_list = method_list->method_next;
- tmpclass = tmpclass->super_class;
}
if (imp)
(*imp)((id)class, op);