A C++ version of the Lola-2 compiler and an Oberon to C++compiler
Thank you all again for your support. In case you're interested I updated the Oberon to C++ compiler so DIV and MOD are now mapped to the corresponding functions which can be implemented in the _Root class (in _Global.h). The following default implementation is provided: static int MOD(int a, int b) { return ( a % b + b ) % b; } static int DIV(int a, int b) { return ( a - MOD(a, b) ) / b; } The C++ generator is part of this repository: https://github.com/rochus-keller/Oberon The OberonViewer was updated to include the latest generator. It can be downloaded from here: http://software.rochus-keller.info/OberonViewer_win32.zip With the updated generator and the fixes of the Oberon files provided from this forum I was able to re-generate the Lola-2 compiler: https://github.com/rochus-keller/Lolac The C++ version of the Lola-2 compiler now generates identical files as the Vishap Oberon version. See https://github.com/rochus-keller/Lolac/tree/master/Examples/RISC5_translated https://github.com/rochus-keller/Lolac/tree/master/Examples/RISC5_translated... Best R.K.
-----Original Message----- From: Oberon [mailto:oberon-bounces@lists.inf.ethz.ch] On Behalf Of rochus.keller@bluewin.ch Sent: Wednesday, 17 April 2019 7:35 PM To: oberon@lists.inf.ethz.ch Subject: [Oberon] A C++ version of the Lola-2 compiler and an Oberon to C++compiler
Thank you all again for your support.
In case you're interested I updated the Oberon to C++ compiler so DIV and MOD are now mapped to the corresponding functions which can be implemented in the _Root class (in _Global.h). The following default implementation is provided: static int MOD(int a, int b) { return ( a % b + b ) % b; } static int DIV(int a, int b) { return ( a - MOD(a, b) ) / b; }
While this may well work it is quite inefficient. Depending on the target processor, the % and / operations can be significantly more expensive than + and -. MOD uses two of these and DIV uses three. Here is an alternative version which only requires one % or / operation for both DIV and MOD. It also makes it clear that it is only in cases where the dividend is negative that C++ differs from Oberon. static int DIV(int a, int b) { if (a < 0) return (a - b + 1) / b; else return a / b; } static int MOD(int a, int b) { if (a < 0) return (b - 1) + ((a - b + 1)) % b; else return a % b; } It might be possible to simplify the formula for the negative dividend case of MOD but I can't immediately see it. Regards, Chris Burrows CFB Software http://www.astrobe.com/RISC5
participants (2)
-
Chris Burrows -
rochus.keller@bluewin.ch