lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This isn't an anomaly or bug. It's how Lua is able to index more than 256 constants - look at the bit field, it's only 9 bits with one reserved to mean "is constant" (from memory).

It's definitely possible a future version of Lua may not solve the problem this way - there's nothing to stop the authors changing the VM entirely, to a stack-based architecture even. But it's not a bug, and it's definitely not something that needs to be fixed.

Tracking which constants I can see being a pain though. I'd mark all jump locations (and both targets of a skip instruction) and from each jump location downwards keep track of which registers are holding which constants. Mark them as unknown if another instruction writes to them (or multirets potentially over them). A very rudimentary single-block constant propagation routine effectively.

If you're writing a code analysis program though, this metadata may help you in other ways.

Regards,
- Alex

-----Original Message----- From: Patrick Donnelly
Sent: Monday, April 02, 2012 12:20 PM
To: Lua mailing list
Subject: luac [GS]ETTABUP "anomoly"

Hi list,

I'm writing a code analysis program for Lua 5.2 that needs to know
global variable gets/sets. Apparently when you exceed 256 constants,
lua needs to begin using 3 op codes:

       513     [257]   LOADK           0 -257  ; "foo256"
       514     [257]   CLOSURE         1 256   ; 0xc1a3a0
       515     [257]   SETTABUP        0 0 1   ; _ENV

versus 2 when you use constants in the constant table below 256.

       511     [256]   CLOSURE         0 255   ; 0xc27730
       512     [256]   SETTABUP        0 -256 0        ; _ENV "foo255"

[To generate a simple example, run this:

$ for ((i = 0; i <= 256; i++)); do echo "function foo$i() end "; done
| luac -l -p - | less

]

The "anomaly" is that the listing output no longer says what key is
being set (I imagine it's the same GETTABUP) on the SETTABUP line.
This is mildly annoying as now I have to keep track of what constants
are loaded into registers to naively keep track of what SETTABUP is
referring to. Is it possible a future bug fix release could correct
this easily?

--
- Patrick Donnelly