Custom lua instruction format

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom lua instruction format

Baozeng
Hi all, 

As we know, Lua instruction format is the opcode with 6 bits,  `A` with 8 bits, `B` and `C` with 9 bits (together 32 bits)

I custom opcode with 8 bits, `A` with 8 bits,  `B` and `C` with 8 bits.  and use the lua test-suite (https://www.lua.org/tests/) to test it and It have passed all the test.    I want to know does it  affect user's program? 
I only know that with 8 bits for `B` and `C`, the max jump offset is shorter than before. will it affect the normal user? Thank you.



--
     Best Regards,
     Baozeng Ding
                                                                
Reply | Threaded
Open this post in threaded view
|

Re: Custom lua instruction format

Egor Skriptunoff-2
On Sat, Jun 6, 2020 at 4:09 PM Baozeng wrote:
As we know, Lua instruction format is the opcode with 6 bits,  `A` with 8 bits, `B` and `C` with 9 bits (together 32 bits)
I custom opcode with 8 bits, `A` with 8 bits,  `B` and `C` with 8 bits.


Does this mean your VM has 127 registers instead of 255?
A function containing 30 local variables and a matrix constructor 50x50 will fail to compile.
Reply | Threaded
Open this post in threaded view
|

Re: Custom lua instruction format

Baozeng
Hello Egor,

The register is represented  by `A`,  which is still 8 bits.  I have not changed it.  Only `B` is changed from 9 bits to 8 bits. and `C` is changed from 9 bits to 8 bits. 
So I think it will not affect the registers. Am I right?  Thank you in advance.


Egor Skriptunoff <[hidden email]> 于2020年6月7日周日 上午3:10写道:
On Sat, Jun 6, 2020 at 4:09 PM Baozeng wrote:
As we know, Lua instruction format is the opcode with 6 bits,  `A` with 8 bits, `B` and `C` with 9 bits (together 32 bits)
I custom opcode with 8 bits, `A` with 8 bits,  `B` and `C` with 8 bits.


Does this mean your VM has 127 registers instead of 255?
A function containing 30 local variables and a matrix constructor 50x50 will fail to compile.


--
     Best Regards,
     Baozeng Ding
                                                                
Reply | Threaded
Open this post in threaded view
|

Re: Custom lua instruction format

Jonathan Goble
In reply to this post by Baozeng
On Sat, Jun 6, 2020 at 9:09 AM Baozeng <[hidden email]> wrote:
Hi all, 

As we know, Lua instruction format is the opcode with 6 bits,  `A` with 8 bits, `B` and `C` with 9 bits (together 32 bits)

I custom opcode with 8 bits, `A` with 8 bits,  `B` and `C` with 8 bits.  and use the lua test-suite (https://www.lua.org/tests/) to test it and It have passed all the test.    I want to know does it  affect user's program? 
I only know that with 8 bits for `B` and `C`, the max jump offset is shorter than before. will it affect the normal user? Thank you.

Lua 5.4 (currently in release candidate stage) has changed the format. Now for instructions in ABC format, the opcode is the lowest 7 bits (increased from 6 to accommodate new opcodes), 8 bits for A, 1 bit for "k" (used as a boolean in some compare/test instructions), 8 bits for B, and the highest 8 bits for C. You can view the layout at the top of lopcodes.h in the Lua 5.4 source. Note that I'm looking at 5.4rc1, and we're now up to 5.4rc4, but I don't think there's been any changes.

If you reorder that so that the "k" is bit 7, between the opcode and A, then the new ABC format neatly divides into 8-8-8-8 like yours. The only rub, if you truly want to separate the bytes so that each byte serves one purpose, is that you lose one bit from the larger arguments (Bx, sBx, Ax, and sJ) in the other instruction formats.
Reply | Threaded
Open this post in threaded view
|

Re: Custom lua instruction format

Egor Skriptunoff-2
In reply to this post by Baozeng
On Sun, Jun 7, 2020 at 5:49 AM Baozeng wrote:
The register is represented  by `A`,  which is still 8 bits.  I have not changed it.  Only `B` is changed from 9 bits to 8 bits. and `C` is changed from 9 bits to 8 bits. 


RK(B) encodes either register or constant (256 registers and 256 constants, 9 bits in total)
If your B is 8 bits then only 128 registers are addressable?
Reply | Threaded
Open this post in threaded view
|

Re: Custom lua instruction format

Jonathan Goble
On Sun, Jun 7, 2020, 12:58 AM Egor Skriptunoff <[hidden email]> wrote:
On Sun, Jun 7, 2020 at 5:49 AM Baozeng wrote:
The register is represented  by `A`,  which is still 8 bits.  I have not changed it.  Only `B` is changed from 9 bits to 8 bits. and `C` is changed from 9 bits to 8 bits.

RK(B) encodes either register or constant (256 registers and 256 constants, 9 bits in total)
If your B is 8 bits then only 128 registers are addressable?

As I described, Lua 5.4 has only 8 bits for B. It can still address 256 registers because it uses separate instructions for constants. 
Reply | Threaded
Open this post in threaded view
|

Re: Custom lua instruction format

Philippe Verdy-2
Or equivalently it can still use 9 bits for RK(B), one of them (k) being part of the instruction code (so there's two instruction codes allocated for the same "virtual" instruction).
You could as well still build a Lua VM using even more registers by allocating an additional instruction code that encodes the register in a additional extension code, And then pack the instructions with shorter codes than 32-bit, but a variable number of 16-bit entries; this has an interest only to compress the total size of the bytecode, to reduce the memory footprint (this is what is done in many CISC ISA, because it allows more code to fit the internal cache: the compact CISC code is expanded to a wide RISC instruction within the internal instruction decoder, then the wide instructions can be fed into multiple parallel pipelines).
RISC processors are disappearing, they are not really needed if you have integrated the instruction decoder in the CPU (or the VM), because CISC is then faster to reduce accesses to external memory.
So I'm convinced that LUa could (like also other bytecode VMs, including the JVM for Java) use an ISA with variable instruction sizes (in multiples of 8-bit, with extension codes).
And I think I could even make Lua not jsut better performing, but also fitting with smaller devices (with low internal memory, or slow external memory on Flash or ROM); to give access to at least 256 registers, you'd need extension codes for extra registers, but some registers are very frequently used in the bytecode, and a few of them could fit in the size of the initial instruction byte without needed any extension code (like in the x86/x64 ISA).

Le dim. 7 juin 2020 à 18:30, Jonathan Goble <[hidden email]> a écrit :

As I described, Lua 5.4 has only 8 bits for B. It can still address 256 registers because it uses separate instructions for constants.