lua-users home
lua-l archive

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

2015-01-17 21:24 GMT-08:00 chuang <>:
In the function patchlistaux:

static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
                          int dtarget) {
  while (list != NO_JUMP) {
    int next = getjump(fs, list);
    if (patchtestreg(fs, list, reg))
      fixjump(fs, list, vtarget);
      fixjump(fs, list, dtarget);  /* jump to default target */
    list = next;

what dose the param vtarget and dtarget actual mean?

Another question is, if patchtestreg return true,means list is an OP_TESTSET or OP_TEST instruction,but their format is not the same as OP_JMP,which is iAsBx,but in function fixjump it use SETARG_sBx macro to set sBx param.How could SETARG_sBx  being used in OP_TESTSET or OP_TEST instruction? 

When it generates code for a logical _expression_, which is consisted of a few expressions (comparison exp and/or non-comparison exp) connected by logic operators, the Lua compiler generates the last two instruction as follows:
A logical _expression_ yields one of the three possible values: TRUE, FALSE, or the value yield by a non-boolean _expression_. Then for all the comparison sub-exa, its JMP should jump to either EXP_F or EXP_T. For a non-comparison _expression_, its JMP should jump to the instruction following the EXP_T.

The location of the instruction following EXP_T is "varget", meaning "value target". It is named because the a non-boolean "value" is already in the target reg. On the other hand, EXP_T and EXP_F are called dtarget, meaning "default target".

In patchtestreg(), the Lua compiler checks if a JMP instruction follows a TESTSET. TESTSET is what Lua uses to deal with non-comparison _expression_ within a logical _expression_. That is the reason why the if-branch after the patchtestreg() check uses vtarget.