lua-users home
lua-l archive

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


> 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:

	       17,18d16
	       < OP_MOD
	       < OP_POW

	       20d17
	       < OP_IDIV

	       25a23,25
	       > OP_MOD
	       > OP_IDIV
	       > OP_POW

where < is lopcodes.h and > is lvm.c.