[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: VM compilation question
- From: Norman Ramsey <nr@...>
- Date: Tue, 20 Jul 2021 10:54:36 -0400
I've been poking around at the VM and its compiler, trying to learn
how constants are handled. It appears that
1. A typical infix operator can take B and C operands that might be
constant, so for each operand, distinguishing a constant from a
register requires a conditional test (e.g., whether a particular
bit is set).
2. Every function has its own pool of constants.
3. As long as the number of constants in a function is small, source
n = n + 1 -- assume `n` is local here
will compile to a single VM instruction.
4. If the number of constants in a function gets large, to the point
where there aren't enough bits to pack them all into a B or C
operand, *from that point onward* no more constants are packed
into a B or C operand. In other words, if the assignment `n = n + 1`
is compiled *after* the compiler has seen a large number of
constants, then it compiles to two VM instructions: one to load
the literal 1 into a register, and one to do the addition.
Does this summary seem accurate? It's the last point that I'm really
P.S. Why I want to know: last year I taught a course in which students
built a compiler and VM inspired by Lua's VM. Performance was quite
good except around the matter of literals; my students' compilers
always compiled code like `n = n + 1` into two VM instructions.
Their codes were otherwise competitive with Lua. So I'm trying to
figure out if they can eliminate that extra instruction without making
things too complicated.