Discussion:
Conditional compilation in Modula-2
(too old to reply)
Chris Burrows
2010-08-13 00:00:59 UTC
Permalink
I have never been a fan of the system used in some Pascal / Modula-2
compilers where special comments e.g. (*$IFDEF TrialVersion *)are used in
the source code as a conditional compilation mechanism. It has always seemed
'clunky' and particularly non-portable to me.

During a recent discussion in the Oberon newgroup I pointed out that there
is actually no need, in simple cases, for an additional language feature. It
is feasible for a compiler to generate absolutely no code for statements
that could be statically evaluated to:

IF FALSE THEN .... END;

If you were using a compiler that eliminates such code you could implement a
simple module with your various conditions defined. For example:

MODULE Options;

CONST
TrialVersion = TRUE;
....
....
Then e.g. wherever you wanted to *eliminate all traces* from your executable
file of any features related to your full version you could write:

IF (Options.TrialVersion) THEN ....

At build-time you would link in the appropriate version of the Options
object file related to the version that is being built. (We use the
excellent FinalBuilder tool to automate this sort of process).

Purely coincidentally, I was pleasantly surprised a few days later to
discover that the code-generator for the original Lilith Modula-2 compiler
from the 1980's provides for conditional compilation in this way. Do other
Modula-2 compilers have this particular feature?

Regards,
Chris Burrows

CFB Software
http://www.cfbsoftware.com/modula2
Pascal J. Bourguignon
2010-08-13 02:42:36 UTC
Permalink
Post by Chris Burrows
I have never been a fan of the system used in some Pascal / Modula-2
compilers where special comments e.g. (*$IFDEF TrialVersion *)are used in
the source code as a conditional compilation mechanism. It has always seemed
'clunky' and particularly non-portable to me.
During a recent discussion in the Oberon newgroup I pointed out that there
is actually no need, in simple cases, for an additional language feature. It
is feasible for a compiler to generate absolutely no code for statements
IF FALSE THEN .... END;
If you were using a compiler that eliminates such code you could implement a
MODULE Options;
CONST
TrialVersion = TRUE;
....
....
Then e.g. wherever you wanted to *eliminate all traces* from your executable
IF (Options.TrialVersion) THEN ....
At build-time you would link in the appropriate version of the Options
object file related to the version that is being built. (We use the
excellent FinalBuilder tool to automate this sort of process).
Purely coincidentally, I was pleasantly surprised a few days later to
discover that the code-generator for the original Lilith Modula-2 compiler
from the 1980's provides for conditional compilation in this way. Do other
Modula-2 compilers have this particular feature?
Yes, most compilers (of most languages) do dead-code elimination,
since this is a trivial optimization.

However, one thing to consider, is that the dead code must still be
valid code for the current compiler. You couldn't use this technique
to avoid processing code using extensions. Even using undefined
functions (ie. function defined in other compilers than the current
one) could be signaled as errors, dead-code non-withstanding.
--
__Pascal Bourguignon__ http://www.informatimago.com/
Loading...