Xavid (kihou) wrote,
Xavid
kihou

Compile-time Asserts [Geek Alert!]

Recently I came across the idea of compile-time asserts (PDF warning) while doing a bit of complicated hackery*. This made me think: why don't compile-time asserts get as much attention as runtime ones? Built-in language support for them would be easy to implement, and could generate an error if an expression is undeterminable at compile time. Actually, you could extend this into making representation-independent base types: why can't you say you want an integer from 0 to 17 and let the compiler decide the actual representation and do the worrying about sign and overflow for you, and run invariants to make sure it never exceeded its range, sort of thing. Maybe I'll put it into the dialect of Forth I'm creating because Sussman couldn't think of a language with names for parameters but not local variables. ^_^

* 8051-type microcontrollers have 4 memory spaces, two of which can use 16-bit addressing; thus, a pointer to arbitrary memory takes 3 bytes and a function call to dereference (at least in the sdcc implementation). However, on my R31-JP in 6.115, code memory and external RAM are wired to actually use the same space. I have a series of functions that I want to be able to take 16-bit pointers to this shared space. However, compile-time constants and inline string constants need to be declared code for the compiler's sake, while things that get modified need to be declared xdata. I wanted to make a macro to do the cast safely without accidentally casting another type of pointer and getting garbage. Alas, I can't use C++. I totally got it to work using a nonstandard __typeof operator and dividing by 0.
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 5 comments