Bitwise operators
Currently, the strategoxt standard library supports arithmetic operations (add, sub, mul, div), but no bitwise operators. For the Jasmin project, we tried to translate lists of ‘access flags’ to integers and required bitwise OR. We implemented a temporary solution using a rewrite-rule, but adding this operator to the standard library would be useful.
Submitted by Reinier Hartog on 28 February 2014 at 10:57
Issue Log
What is the temporary solution?
I wrote the rewrite-rules for one bit situations and used the
mod
operator together withdiv
andmul
to recursively extract the least significant byte and rewrite it. It works, but I imagine adding it to the standard library would be simpler and more efficient.See the code below:
bitwise-or: (0, 0) -> 0 bitwise-or: (x, 0) -> x bitwise-or: (0, x) -> x bitwise-or: (1, 1) -> 1 bitwise-or: (a, b) -> result where lsa := <mod> (a, 2) ; lsb := <mod> (b, 2) ; ls := <bitwise-or> (lsa, lsb) ; resta := <divi> (a, 2) ; restb := <divi> (b, 2) ; restor := <bitwise-or> (resta, restb) ; restors := <muli> (restor, 2) ; result := <addi> (restors, ls)
Nice workaround, but indeed these should be primitives and added to the Stratego library.
If you (or anyone) would like to add them, have a look at the https://github.com/metaborg/mb-exec/blob/master/org.spoofax.interpreter.core/src/main/java/org/spoofax/interpreter/library/ssl/SSL_addi.java primitive, and add similar primitives for bitwise operations. Then add a strategy in https://github.com/metaborg/strategoxt/blob/java-bootstrap/strategoxt/stratego-libraries/lib/spec/term/integer.str that calls into the primitives (see addi example). If you make a pull request I can merge it into our repository.
Reinier added the primitives in https://github.com/metaborg/mb-exec/pull/1 and https://github.com/metaborg/strategoxt/pull/4, thanks!
Log in to post comments