type_info bug fix

Fergus Henderson fjh at cs.mu.oz.au
Fri Feb 28 14:42:14 AEDT 1997


Tyson Richard DOWD, you wrote:
> 
> I'm working on this now - I am slowly replacing the various 
> `TYPE_..._OFFSET' with MR_TYPE..._GET_WHATEVER(T) style macros, 
> since I think this is a better solution (it's difficult to remember
> what the offsets are into, as you've noticed in the changes below).

I agree this is a better solution.

> The offsets will remain, but be used by the macros only.

I think the code would be clearer if you implement the above macros
by actually defining a C struct for the types, and just casting the
argument to a pointer to this struct.

It is a little bit tricky in the case of structs with two variable
sized bits, i.e. type_layout, but I think it would help even for
those cases.

	/*
	** IMPORTANT: the layout in memory of the following struct must
	** match the way that the Mercury compiler generates code for it.
	*/
	typedef struct {
		Word arity;
		Word arg_pseudo_type_infos[1]; /* variable-sized array */
			/* actualy length is `arity', not 1 */
	} MR_TypeLayout_part1;
	typedef struct {
		ConstString name;
		Word arg_layouts[1]; /* variable-sized array */
			/* actualy length is `arity', not 1 */
	} MR_TypeLayout_part2;
	typedef MR_TypeLayout_part1 MR_TypeLayout;

	#define MR_GET_TYPE_LAYOUT_ARITY(type_layout_ptr) \
		(((MR_TypeLayout_part1 *) type_layout_ptr)->name)
	#define MR_GET_TYPE_LAYOUT_ARG_PSEUDO_TYPE_INFOS(type_layout_ptr) \
	    (((MR_TypeLayout_part1 *) type_layout_ptr)->arg_pseduo_type_infos)
	#define MR_GET_TYPE_LAYOUT_PART2(type_layout_ptr) 		\
		((MR_TypeLayout_part2 *) (				\
			(char *)type_layout_ptr +			\
			offsetof(MR_TypeLayout_part1,			\
				arg_pseudo_type_infos) +		\
			MR_GET_TYPE_LAYOUT_ARITY * sizeof(Word *) 	\
		))
	#define MR_GET_TYPE_LAYOUT_NAME(p) \
		(MR_GET_TYPE_LAYOUT_PART2(type_layout_ptr)->name)
	#define MR_GET_TYPE_LAYOUT_ARG_LAYOUTS(p) \
		(MR_GET_TYPE_LAYOUT_PART2(type_layout_ptr)->arg_layouts)

It might perhaps be better to require a cast to MR_TypeLayout in the caller
rather than casting the argument to MR_TypeLayout_part1 in the macros --
I'm not sure whether it is better to have the cast in the macros or in
the caller.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list