[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: how does lua arrange vmcase?
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Tue, 4 Apr 2017 13:10:05 -0300
> AFAIK, cases are sensitive of orders in C language. switch (?) case
> XXX:break; case YYY:break;
> vmcase(OP_MOVE) is before vmcase(OP_LOADK) before vmcase(OP_LOADKX)
> then vmcase(OP_LOADBOOL) followed?
> why such order? frequency order? or any other considerations?
It's mostly the order the opcodes are listed in the enum defined in lopcodes.h.
A switch whose cases are in increasing order should be easily handled by
the C compiler, which can generated a simple jump table. More generally,
if the cases form an interval [1,n] with n small, then a simple jump
table works, even if the cases are not ordered.
Frequency of execution is handled by the branch predictions in the CPU.
The order in lopcodes.h and lvm.c does differ in a few places:
where < is lopcodes.h and > is lvm.c.