-
Notifications
You must be signed in to change notification settings - Fork 4
Description
This issue is a proposal to add byte-swap instructions to Glulx. Its purpose for now is just to take the temperature of maintainers. If there's interest in moving forward, I'll follow up with a pull request here to add an appropriate section to the specification, and with PRs to gluxe, quixe, and git to implement it.
This addition is motivated by a project I'm working on to translate WebAssembly into Glulx, making it possible to develop IF in any general-purpose language with a compiler that can target WASM, and have it run seamlessly on existing Glulx interpreters. Currently, an impediment to generating efficient code is that WebAssembly is little-endian while Glulx is big-endian, and swapping requires numerous instructions which would have to be executed before or after every main memory access.
I propose to add six new instructions to Glulx, and one new gestalt ID to indicate their availability.
swap L1 S1
Swap the bytes of L1
and store the result in S1
. 0x01020304
becomes 0x04030201
.
swaps L1 S1
Swap the two high bytes of L1
with each other, and the two low bytes with each other, and store the result in S1
. 0x01020304
becomes 0x02010403
.
astoreswap L1 L2 L3
Swap the bytes of L3
and then store it into the 32-bit field at main memory address (L1+4*L2)
. 0x01020304
is stored as 0x04030201
.
aloadswap L1 L2 S3
Load a 32-bit value from main memory address (L1+4*L2)
, and store it in S1
with its bytes swapped. 0x01020304
is stored as 0x04030201
.
astoreswaps L1 L2 L3
Swap the two low bytes of L3
and store them into the 16-bit field at main memory address (L1+2*L2)
. 0x0102
is stored as 0x0201
.
aloadswaps L1 L2 S3
Load an 16-bit value from main memory address (L1+2*L2)
, and store it in S1 with its bytes swapped. 0x0102
is stored as 0x0201
.
swap
, aloadswap
, and astoreswap
will occur frequently in generated code and should ideally have single-byte opcodes. The 16-bit versions are less important and should have two-byte opcodes to conserve numbering space.