unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* proposal: stricter type-checking for macros
@ 2004-03-25 12:23 Han-Wen Nienhuys
  2004-03-25 15:27 ` Paul Jarc
  0 siblings, 1 reply; 7+ messages in thread
From: Han-Wen Nienhuys @ 2004-03-25 12:23 UTC (permalink / raw)




Hi there,

after being bitten by a unnoticed type-error using SCM_CDRLOC, for the
umpteenth time, I submit a proposal that would make typechecking
macros stricter without the overhead of the union type
(SCM_DEBUG_TYPING_STRICTNESS == 2).

Here are a couple of tests. I'm not sure about the development
requirements for such code (one hack requires GCC, for instance), but
I think it should be switched on at all times. My preference is for

  /*
  works, but warning message sucks.
  */
  #define TYPECHECK(x)  (((x) - global_object) + global_object)

or

  /*
   works, GCC specific
  */
  #define TYPECHECK(x)  ({ SCM ___y = x; ___y; })

I am not sure of the performance penalties when compiling without -O2.
Maybe assembler gurus could comment?

It would also be nice if scm_unused_struct of the current code would
be changed in 

	struct scm_word {
	  long car;
	  long cdr; 
	};

or somesuch - it makes debugging a little easier.

****
struct scm_word {
  long car;
  long cdr; 
};

typedef struct scm_word * SCM;
SCM global_object;

inline SCM scm_identity (SCM x)
{
  return x;
}

/*
  ugh: evaluates X twice.
  */

// #define TYPECHECK(x)  (x == global_object) ? x : x;


/*
  works, but warning message sucks.
 */
#define TYPECHECK(x)  (((x) - global_object) + global_object)


/*
  works, performance hit (even with -O2) 
 */
// #define TYPECHECK(x)  (global_object = (x))

/*
  works, performance hit (without -O2)
 */
//#define TYPECHECK(x) scm_identity(x)

/*
  works, GCC specific
*/
//#define TYPECHECK(x)  ({ SCM ___y = x; ___y; })



SCM
f(SCM x, SCM y)
{
  SCM d = 0;
  d = TYPECHECK(x);
  d = TYPECHECK(&x);
  return d;
}



-- 

 Han-Wen Nienhuys   |   hanwen@xs4all.nl   |   http://www.xs4all.nl/~hanwen 



_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: proposal: stricter type-checking for macros
  2004-03-25 12:23 proposal: stricter type-checking for macros Han-Wen Nienhuys
@ 2004-03-25 15:27 ` Paul Jarc
  2004-03-25 15:34   ` Han-Wen Nienhuys
  2004-03-31 23:08   ` Paul Jarc
  0 siblings, 2 replies; 7+ messages in thread
From: Paul Jarc @ 2004-03-25 15:27 UTC (permalink / raw)
  Cc: guile-devel

Han-Wen Nienhuys  <hanwen@xs4all.nl> wrote:
> // #define TYPECHECK(x)  (global_object = (x))

#define TYPECHECK(x) (0? (void)(*(SCM*)0=(x)): (void)0)

Casting to void ensures that the resulting "value" won't accidentally
be used for anything.  "0?" ensures that the null pointer won't
actually be dereferenced.  I'd expect dead-code elimination to compile
this into nothing, but I haven't checked which optimization level this
happens for.


paul


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: proposal: stricter type-checking for macros
  2004-03-25 15:27 ` Paul Jarc
@ 2004-03-25 15:34   ` Han-Wen Nienhuys
  2004-04-24 22:09     ` Marius Vollmer
  2004-03-31 23:08   ` Paul Jarc
  1 sibling, 1 reply; 7+ messages in thread
From: Han-Wen Nienhuys @ 2004-03-25 15:34 UTC (permalink / raw)
  Cc: guile-devel

prj@po.cwru.edu writes:
> Han-Wen Nienhuys  <hanwen@xs4all.nl> wrote:
> > // #define TYPECHECK(x)  (global_object = (x))
> 
> #define TYPECHECK(x) (0? (void)(*(SCM*)0=(x)): (void)0)
> 
> Casting to void ensures that the resulting "value" won't accidentally
> be used for anything.  "0?" ensures that the null pointer won't
> actually be dereferenced.  I'd expect dead-code elimination to compile
> this into nothing, but I haven't checked which optimization level this
> happens for.


Very clever!

I vote for

  #define TYPECHECKED(x) (0? (*(SCM*)0=(x)): x)

which returns x, and obviates the void casting.

-- 

 Han-Wen Nienhuys   |   hanwen@xs4all.nl   |   http://www.xs4all.nl/~hanwen 



_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: proposal: stricter type-checking for macros
  2004-03-25 15:27 ` Paul Jarc
  2004-03-25 15:34   ` Han-Wen Nienhuys
@ 2004-03-31 23:08   ` Paul Jarc
  1 sibling, 0 replies; 7+ messages in thread
From: Paul Jarc @ 2004-03-31 23:08 UTC (permalink / raw)
  Cc: guile-devel

I wrote:
> #define TYPECHECK(x) (0? (void)(*(SCM*)0=(x)): (void)0)

BTW, does anyone know of a similar trick for arrays?  I can do this:

#define ARRAYCHECK(type, arr) \
  (0? (void)(*(type (**)[sizeof (arr)/sizeof (type)])0=&(arr)): (void)0)

But this requires the caller to know not just the element type of the
array, but also whether the array is const.  (Pointer to array of
const T is not compatible with pointer to array of non-const T.)  I'd
like to find a way to check that the expression is an array of const
or non-const T, giving a warning iff it is neither.  (That way,
ARRAYCHECK(char, "foo") would work with or without -Wwrite-strings.)


paul


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: proposal: stricter type-checking for macros
  2004-03-25 15:34   ` Han-Wen Nienhuys
@ 2004-04-24 22:09     ` Marius Vollmer
  2004-04-24 22:54       ` Han-Wen Nienhuys
  0 siblings, 1 reply; 7+ messages in thread
From: Marius Vollmer @ 2004-04-24 22:09 UTC (permalink / raw)
  Cc: guile-devel

Han-Wen Nienhuys  <hanwen@xs4all.nl> writes:

> I vote for
>
>   #define TYPECHECKED(x) (0? (*(SCM*)0=(x)): x)
>
> which returns x, and obviates the void casting.

How would you want to install this into Guile?  Would it be part of
the public API or would we just use it in a lot of places ourselves?

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: proposal: stricter type-checking for macros
  2004-04-24 22:09     ` Marius Vollmer
@ 2004-04-24 22:54       ` Han-Wen Nienhuys
  2004-04-24 23:42         ` Marius Vollmer
  0 siblings, 1 reply; 7+ messages in thread
From: Han-Wen Nienhuys @ 2004-04-24 22:54 UTC (permalink / raw)
  Cc: guile-devel

mvo@zagadka.de writes:
> Han-Wen Nienhuys  <hanwen@xs4all.nl> writes:
> 
> > I vote for
> >
> >   #define TYPECHECKED(x) (0? (*(SCM*)0=(x)): x)
> >
> > which returns x, and obviates the void casting.
> 
> How would you want to install this into Guile?  Would it be part of
> the public API or would we just use it in a lot of places ourselves?

Huh?

See tags.h , #define SCM_UNPACK . I've put this into GUILE on 28/3.


-- 

 Han-Wen Nienhuys   |   hanwen@xs4all.nl   |   http://www.xs4all.nl/~hanwen 



_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: proposal: stricter type-checking for macros
  2004-04-24 22:54       ` Han-Wen Nienhuys
@ 2004-04-24 23:42         ` Marius Vollmer
  0 siblings, 0 replies; 7+ messages in thread
From: Marius Vollmer @ 2004-04-24 23:42 UTC (permalink / raw)
  Cc: guile-devel

Han-Wen Nienhuys <hanwen@xs4all.nl> writes:

> mvo@zagadka.de writes:
>> Han-Wen Nienhuys  <hanwen@xs4all.nl> writes:
>> 
>> > I vote for
>> >
>> >   #define TYPECHECKED(x) (0? (*(SCM*)0=(x)): x)
>> >
>> > which returns x, and obviates the void casting.
>> 
>> How would you want to install this into Guile?  Would it be part of
>> the public API or would we just use it in a lot of places ourselves?
>
> Huh?
>
> See tags.h , #define SCM_UNPACK . I've put this into GUILE on 28/3.

:-)  Yes, I noticed, but only after I wrote that mail...

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-04-24 23:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-25 12:23 proposal: stricter type-checking for macros Han-Wen Nienhuys
2004-03-25 15:27 ` Paul Jarc
2004-03-25 15:34   ` Han-Wen Nienhuys
2004-04-24 22:09     ` Marius Vollmer
2004-04-24 22:54       ` Han-Wen Nienhuys
2004-04-24 23:42         ` Marius Vollmer
2004-03-31 23:08   ` Paul Jarc

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).