Syntax simplification utility for classic Modula-2 source code.
M2-Redux is a feature-reduction and syntax-simplification utility for classic Modula-2 source code. It performs lexical substitutions and syntax transformations on classic Modula-2 source files following the guidelines in the paper On the Maintenance of Classic Modula-2 Compilers.
The conversion of Modula-2 source code with this utility allows downstream classic Modula-2 compilers to remove support for outdated, redundant and non-essential features altogether. The converted source code can still be compiled on such a feature reduced compiler with no or very little manual modification.
M2-Redux is released under the General Public License 2.0 (GPLv2).
M2-Redux will replace all occurences of synonym operators & and ~ with their keyword equivalents and all occurences of <> with #.
IF ~foo & (bar <> baz) THEN ...will be replaced by
IF NOT foo AND (bar # baz) THEN ...M2-Redux will replace all occurences of octal literals with their decimal equivalents.
CONST Foo = 377B; (* octal representation *)will be replaced by
CONST Foo = 255; (* decimal representation *)M2-Redux will replace all occurences of character code literals with their decimal equivalents wrapped in a function call to the built-in CHR() function.
CONST Space = 40C; (* octal representation *)will be replaced by
CONST Space = CHR(32); (* decimal representation *)M2-Redux may replace all occurences of H suffixed base-16 literals with their 0x prefixed equivalents. This option requires the use of command line option --prefix-literals.
CONST Foo = 0DEADBEEFH; (* suffix representation *)may be replaced by
CONST Foo = 0xDEADBEEF; (* prefix representation *)M2-Redux will insert parentheses into multi-term expressions with unary minus for disambiguation. This requires the use of command line option --ebnf-conform-unary-minus or --math-conform-unary-minus.
i := -a + b;will be replaced by
i := -(a + b); (* --ebnf-conform-unary-minus *)respectively
i := (-a) + b; (* --math-conform-unary-minus *)depending on the command line option passed.
M2-Redux will replace all occurences of classic Modula-2 type cast syntax with a function call to the SYSTEM.CAST() function adopted from ISO Modula-2.
int := INTEGER(card); (* legacy syntax *)will be replaced by
int := SYSTEM.CAST(INTEGER, card); (* ISO M2 syntax *)M2-Redux will replace calls to built-in type conversion functions INT(), CARD(), TRUNC(), FLOAT() and LFLOAT() with equivalent calls to built-in function VAL().
int := INT(card);
card := CARD(int);
int := TRUNC(real);
real := FLOAT(int);
longReal := LFLOAT(longInt);will be replaced by
int := VAL(INTEGER, card);
card := VAL(CARDINAL, int);
int := VAL(INTEGER, real);
real := VAL(REAL, int);
longReal := VAL(LONGREAL, longInt);M2-Redux will replace all occurences of long form array type definitions with their short form equivalents.
TYPE A = ARRAY [0..9] OF ARRAY [0..9] OF B; (* long form *)will be replaced by
TYPE A = ARRAY [0..9], [0..9] OF B; (* short form *)M2-Redux will remove all occurences of local modules and create an equivalent standalone library.
IMPLEMENTATION MODULE Foo;
...
MODULE Bar;
...
END Bar;
...
END Foo.will be replaced by
IMPLEMENTATION MODULE Foo;
IMPORT Bar;
...
END Foo.and a standalone library will be created with a definition module that marks the library private with a PRIVATETO pragma
DEFINITION MODULE Bar (*$PRIVATETO=Foo*);
<< interface for module Bar >>
END Bar.where the implementation module contains the body of the formerly local module.
IMPLEMENTATION MODULE Bar;
<< body of local module Bar >>
END Bar.M2-Redux will replace all L-value occurences of imported variables with procedure calls to a setter procedure.
FROM Foo IMPORT foo;
foo := expr;will be replaced by
FROM Foo IMPORT foo, setFoo;
SetFoo(expr);M2-Redux will replace all occurences of sub-expression terms that use DIV and MOD on operands of type INTEGER and LONGINT with equivalent library function calls to tdiv(), tmod(), fdiv() and fmod(). This requires the use of command line options --pim2, --pim3 or --pim4.
quotient := i DIV j;
modulus := i MOD j;will be replaced by
(* --pim2 and --pim3 *)
quotient := tdiv(i, j); (* truncated integer division *)
modulus := tmod(i, j);respectively
(* --pim4 *)
quotient := fdiv(i, j); (* floored integer division *)
modulus := fmod(i, j);depending on the command line option passed.
M2-Redux will remove all occurences of module priority.
M2-Redux will remove all occurences of directives EXPORT and EXPORT QUALIFIED.
M2-Redux will remove all occurences of semicolons before ), |, ELSE, ELSIF, END and UNTIL.
M2-Redux will remove any import of function SYSTEM.SIZE() and replace any call to function SYSTEM.SIZE(), qualified or unqualified, that has an argument of a variant record type with an equivalent call to pervasive function SIZE().
size := SYSTEM.SIZE(variantRecord);will be replaced by
size := SIZE(variantRecord);M2-Redux will further replace any call to function SYSTEM.SIZE(), qualified or unqualified, or to pervasive function SIZE() that has an argument that is not of a variant record type with an equivalent call to pervasive function TSIZE().
TYPE FooType = << any non-variant type >>;
VAR fooVar : FooType;
size := SYSTEM.SIZE(fooVar);will be replaced by
TYPE FooType = << any non-variant type >>;
VAR fooVar : FooType;
size := TSIZE(FooType);M2-Redux will remove any import of function SYSTEM.TSIZE() and replace any call to function SYSTEM.TSIZE(), qualified or unqualified, with an equivalent call to pervasive function TSIZE().
size := SYSTEM.TSIZE(Type);will be replaced by
size := TSIZE(Type);M2-Redux will replace any call to pervasive function MIN() with an equivalent call to pervasive function TMIN() and any call to pervasive function MAX() with an equivalent call to pervasive function TMAX().
min := MIN(Type);
max := MAX(Type);will be replaced by
min := TMIN(Type);
max := TMAX(Type);M2-Redux will replace non-standard foreign definition module headers with a standard definition module header that marks the interface with an FFI pragma.
FOREIGN DEFINITION MODULE Foobar; (* Logitech, GPM, MOCKA *)and
DEFINITION MODULE FOR "C" Foobar; (* GNU *)will be replaced by
DEFINITION MODULE Foobar (*$FFI="C"*);M2-Redux will further replace non-standard procedure headers with a foreign definition module that use or map to foreign identifiers with standard procedure headers that map to foreign identifiers via an FFIDENT pragma.
PROCEDURE foo_bar ( baz : Bam ); (* Logitech, MOCKA *)and
PROCEDURE ["C"] / foo_bar ( baz : Bam ); (* TopSpeed *)and
(*$FOREIGN="foo_bar"*) PROCEDURE FooBar ( baz : Bam ); (* ACK *)will be replaced by
PROCEDURE FooBar ( baz : Bam ) (*$FFIDENT="foo_bar"*);Where modules contain both standard and foreign function declarations, M2-Redux will remove all foreign function declarations and place them in a separate foreign function interface module.
DEFINITION MODULE Foobar; (* Standard definition module *)
PROCEDURE BarBaz ( bam : Boo ); (* standard procedure declaration *)
PROCEDURE ["C"] / baz_bam ( boo : Bee ); (* foreign procedure declaration *)
END Foobar.will be replaced by
DEFINITION MODULE Foobar0 (*$FFI="C"; PRIVATETO=Foobar*);
PROCEDURE BazBam ( boo : Bee ) (*$FFIDENT="baz_bam"*);
END Foobar0.and
DEFINITION MODULE Foobar;
PROCEDURE BarBaz ( bam : Boo );
TYPE BazBamFn = PROCEDURE ( Bee );
VAR BazBam : BazBamFn;
END Foobar.and
IMPLEMENTATION MODULE Foobar;
IMPORT Foobar0;
...
BEGIN
BazBam := Foobar0.BazBam;
END Foobar0.M2-Redux will replace all occurences of sub-expression terms that use bitwise logical 16- and 32-bit operations NOT, AND, OR and XOR with equivalent 32- and 64-bit function calls to SYSTEM.BWNOT(), SYSTEM.BWAND(), SYSTEM.BWOR() and SYSTEM.BWXOR() respectively.
The following syntax, qualified and unqualified, is recognised in the input:
| Dialect | (1) bitwise not | (2) bitwise and | (3) bitwise or | (4) bitwise xor |
|---|---|---|---|---|
| TopSpeed | NOT a |
a AND b |
a OR b |
a XOR b |
| Stony Brook | BNOT a |
a BAND b |
a BOR b |
a BXOR b |
| FST (16-bit) | BitOp.Not(a) |
BitOp.And(a, b) |
BitOp.Or(a, b) |
BitOp.Xor(a, b) |
| FST (32-bit) | BitOp.NotLong(a) |
BitOp.AndLong(a, b) |
BitOp.OrLong(a, b) |
BitOp.XorLong(a, b) |
| Logitech | BitBlockOps.Not(a) |
BitBlockOps.And(a, b) |
BitBlockOps.Or(a, b) |
BitBlockOps.Xor(a, b) |
| ACK | SYSTEM.NOT(a) |
SYSTEM.AND(a, b) |
SYSTEM.OR(a, b) |
SYSTEM.XOR(a, b) |
| MOCKA | SYSTEM.NOT(a) |
SYSTEM.AND(a, b) |
SYSTEM.OR(a, b) |
SYSTEM.XOR(a, b) |
| GPM | SYSTEM.BITNOT(a) |
SYSTEM.BITAND(a,b) |
SYSTEM.BITOR(a,b) |
SYSTEM.BITXOR(a,b) |
where a and b are of types WORD, CARDINAL, INTEGER, LONGINT or LONGCARD.
Types CARDINAL and INTEGER are always assumed to be of the same size as WORD. By default M2-Redux assumes a WORD size of 32 bits and a LONGCARD and LONGINT size of 64 bits. If the source code was written for targets with a WORD size of 16 bits, the use of command line option --assume-16bit-wordsize is required, except for source code that is written for FST or GPM.
The following transformations are carried out in the output:
| Operation | 32-bit (default) | 16-bit (--assume-16bit-wordsize) |
|---|---|---|
| (1) bitwise not | SYSTEM.BWNOT(a) |
SYSTEM.BWAND(SYSTEM.BWNOT(a), 0FFFFH) |
| (2) bitwise and | SYSTEM.BWAND(a, b) |
SYSTEM.BWAND(a, b) |
| (3) bitwise or | SYSTEM.BWOR(a, b) |
SYSTEM.BWAND(SYSTEM.BWOR(a, b), 0FFFFH) |
| (4) bitwise xor | SYSTEM.BWXOR(a, b) |
SYSTEM.BWAND(SYSTEM.BWXOR(a, b), 0FFFFH) |
M2-Redux will replace occurences of bi-directioal bit-shifting sub-expression terms with equivalent function calls to SYSTEM.SHL() and SYSTEM.SHR() for constant shift factors, and BitShifts.shiftCardinal(), BitShifts.shiftInteger(), BitShifts.shiftLongInt() and BitShifts.shiftLongCard() for non-constant shift factors.
Terms of the form
SYSTEM.BITSHIFT(val, shiftFactor) (* GPM *)
SYSTEM.SHIFT(val, shiftFactor) (* ACK, GM2, Logitech, MOCKA *)where val is of type WORD, INTEGER, CARDINAL, LONGINT or LONGCARD, and shiftFactor is of type INTEGER
are replaced as follows
| shift factor | 32 bit operand | 16 bit operand |
|---|---|---|
| constant > 0 | SYSTEM.BWAND(SYSTEM.SHL(val, shiftFactor), 0FFFFFFFFH) |
SYSTEM.BWAND(SYSTEM.SHL(val, shiftFactor), 0FFFFH) |
| constant < 0 | SYSTEM.SHR(val, shiftFactor) |
SYSTEM.SHR(val, shiftFactor) |
| non-constant | BitShift.shiftLongInt(val, shiftFactor)BitShift.shiftLongCard(val, shiftFactor) |
BitShift.shiftInteger(val, shiftFactor)BitShift.shiftCardinal(val, shiftFactor) |
M2-Redux will replace occurences of uni-directioal bit-shifting sub-expression terms with equivalent function calls to SYSTEM.SHL() and SYSTEM.SHR() as follows
| source term | 32 bit operand | 16 bit operand |
|---|---|---|
LSH(val, shiftFactor) |
SYSTEM.BWAND(SYSTEM.SHL(val, shiftFactor), 0FFFFFFFFH) |
SYSTEM.BWAND(SYSTEM.SHL(val, shiftFactor), 0FFFFH) |
RSH(val, shiftFactor) |
SYSTEM.SHR(val, shiftFactor) |
SYSTEM.SHR(val, shiftFactor) |
BitOp.SHLW(val, shiftFactor) |
SYSTEM.BWAND(SYSTEM.SHL(val, shiftFactor), 0FFFFFFFFH) |
SYSTEM.BWAND(SYSTEM.SHL(val, shiftFactor), 0FFFFH) |
BitOp.SHRW(val, shiftFactor) |
SYSTEM.SHR(val, shiftFactor) |
SYSTEM.SHR(val, shiftFactor) |
BitOp.SHLL(val, shiftFactor) |
SYSTEM.BWAND(SYSTEM.SHL(val, shiftFactor), 0FFFFFFFFH) |
n/a |
BitOp.SHRL(val, shiftFactor) |
SYSTEM.SHR(val, shiftFactor) |
n/a |
M2-Redux provides libraries with replacement functions for the non-portable DIV and MOD operations and bi-directional bit shift functions for non-constant argument use cases. The libraries are released under the Library General Public License 2.0 (LGPLv2).
DEFINITION MODULE IntMath;
(* truncated integer division *)
PROCEDURE tdiv ( i, j : INTEGER ) : INTEGER;
(* modulus of truncated integer division *)
PROCEDURE tmod ( i, j : INTEGER ) : INTEGER;
(* floored integer division *)
PROCEDURE fdiv ( i, j : INTEGER ) : INTEGER;
(* modulus of floored integer division *)
PROCEDURE fmod ( i, j : INTEGER ) : INTEGER;
END IntMath.DEFINITION MODULE LongIntMath;
(* truncated integer division *)
PROCEDURE tdiv ( i, j : LONGINT ) : LONGINT;
(* modulus of truncated integer division *)
PROCEDURE tmod ( i, j : LONGINT ) : LONGINT;
(* floored integer division *)
PROCEDURE fdiv ( i, j : LONGINT ) : LONGINT;
(* modulus of floored integer division *)
PROCEDURE fmod ( i, j : LONGINT ) : LONGINT;
END LongIntMath.DEFINITION MODULE BitShifts;
(* shift integer *)
PROCEDURE shiftInteger ( i, shiftFactor : INTEGER ) : INTEGER;
(* shift cardinal *)
PROCEDURE shiftCardinal ( n : CARDINAL; shiftFactor : INTEGER ) : CARDINAL;
(* shift long integer *)
PROCEDURE shiftLongInt ( i : LONGINT; shiftFactor : INTEGER ) : LONGINT;
(* shift long cardinal *)
PROCEDURE shiftLongCard ( n : LONGCARD; shiftFactor : INTEGER ) : LONGCARD;
END BitShifts.+++