lcode.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  1. /*
  2. ** $Id: lcode.c $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lcode_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <limits.h>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include "lua.h"
  13. #include "lcode.h"
  14. #include "ldebug.h"
  15. #include "ldo.h"
  16. #include "lgc.h"
  17. #include "llex.h"
  18. #include "lmem.h"
  19. #include "lobject.h"
  20. #include "lopcodes.h"
  21. #include "lparser.h"
  22. #include "lstring.h"
  23. #include "ltable.h"
  24. #include "lvm.h"
  25. /* Maximum number of registers in a Lua function (must fit in 8 bits) */
  26. #define MAXREGS 255
  27. #define hasjumps(e) ((e)->t != (e)->f)
  28. static int codesJ (FuncState *fs, OpCode o, int sj, int k);
  29. /* semantic error */
  30. l_noret luaK_semerror (LexState *ls, const char *msg) {
  31. ls->t.token = 0; /* remove "near <token>" from final message */
  32. luaX_syntaxerror(ls, msg);
  33. }
  34. /*
  35. ** If expression is a numeric constant, fills 'v' with its value
  36. ** and returns 1. Otherwise, returns 0.
  37. */
  38. static int tonumeral (const expdesc *e, TValue *v) {
  39. if (hasjumps(e))
  40. return 0; /* not a numeral */
  41. switch (e->k) {
  42. case VKINT:
  43. if (v) setivalue(v, e->u.ival);
  44. return 1;
  45. case VKFLT:
  46. if (v) setfltvalue(v, e->u.nval);
  47. return 1;
  48. default: return 0;
  49. }
  50. }
  51. /*
  52. ** Get the constant value from a constant expression
  53. */
  54. static TValue *const2val (FuncState *fs, const expdesc *e) {
  55. lua_assert(e->k == VCONST);
  56. return &fs->ls->dyd->actvar.arr[e->u.info].k;
  57. }
  58. /*
  59. ** If expression is a constant, fills 'v' with its value
  60. ** and returns 1. Otherwise, returns 0.
  61. */
  62. int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
  63. if (hasjumps(e))
  64. return 0; /* not a constant */
  65. switch (e->k) {
  66. case VFALSE: case VTRUE:
  67. setbvalue(v, e->k == VTRUE);
  68. return 1;
  69. case VNIL:
  70. setnilvalue(v);
  71. return 1;
  72. case VKSTR: {
  73. setsvalue(fs->ls->L, v, e->u.strval);
  74. return 1;
  75. }
  76. case VCONST: {
  77. setobj(fs->ls->L, v, const2val(fs, e));
  78. return 1;
  79. }
  80. default: return tonumeral(e, v);
  81. }
  82. }
  83. /*
  84. ** Return the previous instruction of the current code. If there
  85. ** may be a jump target between the current instruction and the
  86. ** previous one, return an invalid instruction (to avoid wrong
  87. ** optimizations).
  88. */
  89. static Instruction *previousinstruction (FuncState *fs) {
  90. static const Instruction invalidinstruction = -1;
  91. if (fs->pc > fs->lasttarget)
  92. return &fs->f->code[fs->pc - 1]; /* previous instruction */
  93. else
  94. return cast(Instruction*, &invalidinstruction);
  95. }
  96. /*
  97. ** Create a OP_LOADNIL instruction, but try to optimize: if the previous
  98. ** instruction is also OP_LOADNIL and ranges are compatible, adjust
  99. ** range of previous instruction instead of emitting a new one. (For
  100. ** instance, 'local a; local b' will generate a single opcode.)
  101. */
  102. void luaK_nil (FuncState *fs, int from, int n) {
  103. int l = from + n - 1; /* last register to set nil */
  104. Instruction *previous = previousinstruction(fs);
  105. if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */
  106. int pfrom = GETARG_A(*previous); /* get previous range */
  107. int pl = pfrom + GETARG_B(*previous);
  108. if ((pfrom <= from && from <= pl + 1) ||
  109. (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
  110. if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
  111. if (pl > l) l = pl; /* l = max(l, pl) */
  112. SETARG_A(*previous, from);
  113. SETARG_B(*previous, l - from);
  114. return;
  115. } /* else go through */
  116. }
  117. luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
  118. }
  119. /*
  120. ** Gets the destination address of a jump instruction. Used to traverse
  121. ** a list of jumps.
  122. */
  123. static int getjump (FuncState *fs, int pc) {
  124. int offset = GETARG_sJ(fs->f->code[pc]);
  125. if (offset == NO_JUMP) /* point to itself represents end of list */
  126. return NO_JUMP; /* end of list */
  127. else
  128. return (pc+1)+offset; /* turn offset into absolute position */
  129. }
  130. /*
  131. ** Fix jump instruction at position 'pc' to jump to 'dest'.
  132. ** (Jump addresses are relative in Lua)
  133. */
  134. static void fixjump (FuncState *fs, int pc, int dest) {
  135. Instruction *jmp = &fs->f->code[pc];
  136. int offset = dest - (pc + 1);
  137. lua_assert(dest != NO_JUMP);
  138. if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ))
  139. luaX_syntaxerror(fs->ls, "control structure too long");
  140. lua_assert(GET_OPCODE(*jmp) == OP_JMP);
  141. SETARG_sJ(*jmp, offset);
  142. }
  143. /*
  144. ** Concatenate jump-list 'l2' into jump-list 'l1'
  145. */
  146. void luaK_concat (FuncState *fs, int *l1, int l2) {
  147. if (l2 == NO_JUMP) return; /* nothing to concatenate? */
  148. else if (*l1 == NO_JUMP) /* no original list? */
  149. *l1 = l2; /* 'l1' points to 'l2' */
  150. else {
  151. int list = *l1;
  152. int next;
  153. while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
  154. list = next;
  155. fixjump(fs, list, l2); /* last element links to 'l2' */
  156. }
  157. }
  158. /*
  159. ** Create a jump instruction and return its position, so its destination
  160. ** can be fixed later (with 'fixjump').
  161. */
  162. int luaK_jump (FuncState *fs) {
  163. return codesJ(fs, OP_JMP, NO_JUMP, 0);
  164. }
  165. /*
  166. ** Code a 'return' instruction
  167. */
  168. void luaK_ret (FuncState *fs, int first, int nret) {
  169. OpCode op;
  170. switch (nret) {
  171. case 0: op = OP_RETURN0; break;
  172. case 1: op = OP_RETURN1; break;
  173. default: op = OP_RETURN; break;
  174. }
  175. luaK_codeABC(fs, op, first, nret + 1, 0);
  176. }
  177. /*
  178. ** Code a "conditional jump", that is, a test or comparison opcode
  179. ** followed by a jump. Return jump position.
  180. */
  181. static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) {
  182. luaK_codeABCk(fs, op, A, B, C, k);
  183. return luaK_jump(fs);
  184. }
  185. /*
  186. ** returns current 'pc' and marks it as a jump target (to avoid wrong
  187. ** optimizations with consecutive instructions not in the same basic block).
  188. */
  189. int luaK_getlabel (FuncState *fs) {
  190. fs->lasttarget = fs->pc;
  191. return fs->pc;
  192. }
  193. /*
  194. ** Returns the position of the instruction "controlling" a given
  195. ** jump (that is, its condition), or the jump itself if it is
  196. ** unconditional.
  197. */
  198. static Instruction *getjumpcontrol (FuncState *fs, int pc) {
  199. Instruction *pi = &fs->f->code[pc];
  200. if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
  201. return pi-1;
  202. else
  203. return pi;
  204. }
  205. /*
  206. ** Patch destination register for a TESTSET instruction.
  207. ** If instruction in position 'node' is not a TESTSET, return 0 ("fails").
  208. ** Otherwise, if 'reg' is not 'NO_REG', set it as the destination
  209. ** register. Otherwise, change instruction to a simple 'TEST' (produces
  210. ** no register value)
  211. */
  212. static int patchtestreg (FuncState *fs, int node, int reg) {
  213. Instruction *i = getjumpcontrol(fs, node);
  214. if (GET_OPCODE(*i) != OP_TESTSET)
  215. return 0; /* cannot patch other instructions */
  216. if (reg != NO_REG && reg != GETARG_B(*i))
  217. SETARG_A(*i, reg);
  218. else {
  219. /* no register to put value or register already has the value;
  220. change instruction to simple test */
  221. *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i));
  222. }
  223. return 1;
  224. }
  225. /*
  226. ** Traverse a list of tests ensuring no one produces a value
  227. */
  228. static void removevalues (FuncState *fs, int list) {
  229. for (; list != NO_JUMP; list = getjump(fs, list))
  230. patchtestreg(fs, list, NO_REG);
  231. }
  232. /*
  233. ** Traverse a list of tests, patching their destination address and
  234. ** registers: tests producing values jump to 'vtarget' (and put their
  235. ** values in 'reg'), other tests jump to 'dtarget'.
  236. */
  237. static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
  238. int dtarget) {
  239. while (list != NO_JUMP) {
  240. int next = getjump(fs, list);
  241. if (patchtestreg(fs, list, reg))
  242. fixjump(fs, list, vtarget);
  243. else
  244. fixjump(fs, list, dtarget); /* jump to default target */
  245. list = next;
  246. }
  247. }
  248. /*
  249. ** Path all jumps in 'list' to jump to 'target'.
  250. ** (The assert means that we cannot fix a jump to a forward address
  251. ** because we only know addresses once code is generated.)
  252. */
  253. void luaK_patchlist (FuncState *fs, int list, int target) {
  254. lua_assert(target <= fs->pc);
  255. patchlistaux(fs, list, target, NO_REG, target);
  256. }
  257. void luaK_patchtohere (FuncState *fs, int list) {
  258. int hr = luaK_getlabel(fs); /* mark "here" as a jump target */
  259. luaK_patchlist(fs, list, hr);
  260. }
  261. /*
  262. ** MAXimum number of successive Instructions WiTHout ABSolute line
  263. ** information.
  264. */
  265. #if !defined(MAXIWTHABS)
  266. #define MAXIWTHABS 120
  267. #endif
  268. /* limit for difference between lines in relative line info. */
  269. #define LIMLINEDIFF 0x80
  270. /*
  271. ** Save line info for a new instruction. If difference from last line
  272. ** does not fit in a byte, of after that many instructions, save a new
  273. ** absolute line info; (in that case, the special value 'ABSLINEINFO'
  274. ** in 'lineinfo' signals the existence of this absolute information.)
  275. ** Otherwise, store the difference from last line in 'lineinfo'.
  276. */
  277. static void savelineinfo (FuncState *fs, Proto *f, int line) {
  278. int linedif = line - fs->previousline;
  279. int pc = fs->pc - 1; /* last instruction coded */
  280. if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ > MAXIWTHABS) {
  281. luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
  282. f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines");
  283. f->abslineinfo[fs->nabslineinfo].pc = pc;
  284. f->abslineinfo[fs->nabslineinfo++].line = line;
  285. linedif = ABSLINEINFO; /* signal that there is absolute information */
  286. fs->iwthabs = 0; /* restart counter */
  287. }
  288. luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
  289. MAX_INT, "opcodes");
  290. f->lineinfo[pc] = linedif;
  291. fs->previousline = line; /* last line saved */
  292. }
  293. /*
  294. ** Remove line information from the last instruction.
  295. ** If line information for that instruction is absolute, set 'iwthabs'
  296. ** above its max to force the new (replacing) instruction to have
  297. ** absolute line info, too.
  298. */
  299. static void removelastlineinfo (FuncState *fs) {
  300. Proto *f = fs->f;
  301. int pc = fs->pc - 1; /* last instruction coded */
  302. if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */
  303. fs->previousline -= f->lineinfo[pc]; /* last line saved */
  304. fs->iwthabs--;
  305. }
  306. else { /* absolute line information */
  307. fs->nabslineinfo--; /* remove it */
  308. lua_assert(f->abslineinfo[fs->nabslineinfo].pc = pc);
  309. fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */
  310. }
  311. }
  312. /*
  313. ** Remove the last instruction created, correcting line information
  314. ** accordingly.
  315. */
  316. static void removelastinstruction (FuncState *fs) {
  317. removelastlineinfo(fs);
  318. fs->pc--;
  319. }
  320. /*
  321. ** Emit instruction 'i', checking for array sizes and saving also its
  322. ** line information. Return 'i' position.
  323. */
  324. int luaK_code (FuncState *fs, Instruction i) {
  325. Proto *f = fs->f;
  326. /* put new instruction in code array */
  327. luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
  328. MAX_INT, "opcodes");
  329. f->code[fs->pc++] = i;
  330. savelineinfo(fs, f, fs->ls->lastline);
  331. return fs->pc - 1; /* index of new instruction */
  332. }
  333. /*
  334. ** Format and emit an 'iABC' instruction. (Assertions check consistency
  335. ** of parameters versus opcode.)
  336. */
  337. int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) {
  338. lua_assert(getOpMode(o) == iABC);
  339. lua_assert(a <= MAXARG_A && b <= MAXARG_B &&
  340. c <= MAXARG_C && (k & ~1) == 0);
  341. return luaK_code(fs, CREATE_ABCk(o, a, b, c, k));
  342. }
  343. /*
  344. ** Format and emit an 'iABx' instruction.
  345. */
  346. int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
  347. lua_assert(getOpMode(o) == iABx);
  348. lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
  349. return luaK_code(fs, CREATE_ABx(o, a, bc));
  350. }
  351. /*
  352. ** Format and emit an 'iAsBx' instruction.
  353. */
  354. int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
  355. unsigned int b = bc + OFFSET_sBx;
  356. lua_assert(getOpMode(o) == iAsBx);
  357. lua_assert(a <= MAXARG_A && b <= MAXARG_Bx);
  358. return luaK_code(fs, CREATE_ABx(o, a, b));
  359. }
  360. /*
  361. ** Format and emit an 'isJ' instruction.
  362. */
  363. static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
  364. unsigned int j = sj + OFFSET_sJ;
  365. lua_assert(getOpMode(o) == isJ);
  366. lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
  367. return luaK_code(fs, CREATE_sJ(o, j, k));
  368. }
  369. /*
  370. ** Emit an "extra argument" instruction (format 'iAx')
  371. */
  372. static int codeextraarg (FuncState *fs, int a) {
  373. lua_assert(a <= MAXARG_Ax);
  374. return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
  375. }
  376. /*
  377. ** Emit a "load constant" instruction, using either 'OP_LOADK'
  378. ** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
  379. ** instruction with "extra argument".
  380. */
  381. static int luaK_codek (FuncState *fs, int reg, int k) {
  382. if (k <= MAXARG_Bx)
  383. return luaK_codeABx(fs, OP_LOADK, reg, k);
  384. else {
  385. int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
  386. codeextraarg(fs, k);
  387. return p;
  388. }
  389. }
  390. /*
  391. ** Check register-stack level, keeping track of its maximum size
  392. ** in field 'maxstacksize'
  393. */
  394. void luaK_checkstack (FuncState *fs, int n) {
  395. int newstack = fs->freereg + n;
  396. if (newstack > fs->f->maxstacksize) {
  397. if (newstack >= MAXREGS)
  398. luaX_syntaxerror(fs->ls,
  399. "function or expression needs too many registers");
  400. fs->f->maxstacksize = cast_byte(newstack);
  401. }
  402. }
  403. /*
  404. ** Reserve 'n' registers in register stack
  405. */
  406. void luaK_reserveregs (FuncState *fs, int n) {
  407. luaK_checkstack(fs, n);
  408. fs->freereg += n;
  409. }
  410. /*
  411. ** Free register 'reg', if it is neither a constant index nor
  412. ** a local variable.
  413. )
  414. */
  415. static void freereg (FuncState *fs, int reg) {
  416. if (reg >= luaY_nvarstack(fs)) {
  417. fs->freereg--;
  418. lua_assert(reg == fs->freereg);
  419. }
  420. }
  421. /*
  422. ** Free two registers in proper order
  423. */
  424. static void freeregs (FuncState *fs, int r1, int r2) {
  425. if (r1 > r2) {
  426. freereg(fs, r1);
  427. freereg(fs, r2);
  428. }
  429. else {
  430. freereg(fs, r2);
  431. freereg(fs, r1);
  432. }
  433. }
  434. /*
  435. ** Free register used by expression 'e' (if any)
  436. */
  437. static void freeexp (FuncState *fs, expdesc *e) {
  438. if (e->k == VNONRELOC)
  439. freereg(fs, e->u.info);
  440. }
  441. /*
  442. ** Free registers used by expressions 'e1' and 'e2' (if any) in proper
  443. ** order.
  444. */
  445. static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
  446. int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
  447. int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
  448. freeregs(fs, r1, r2);
  449. }
  450. /*
  451. ** Add constant 'v' to prototype's list of constants (field 'k').
  452. ** Use scanner's table to cache position of constants in constant list
  453. ** and try to reuse constants. Because some values should not be used
  454. ** as keys (nil cannot be a key, integer keys can collapse with float
  455. ** keys), the caller must provide a useful 'key' for indexing the cache.
  456. */
  457. static int addk (FuncState *fs, TValue *key, TValue *v) {
  458. lua_State *L = fs->ls->L;
  459. Proto *f = fs->f;
  460. TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */
  461. int k, oldsize;
  462. if (ttisinteger(idx)) { /* is there an index there? */
  463. k = cast_int(ivalue(idx));
  464. /* correct value? (warning: must distinguish floats from integers!) */
  465. if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) &&
  466. luaV_rawequalobj(&f->k[k], v))
  467. return k; /* reuse index */
  468. }
  469. /* constant not found; create a new entry */
  470. oldsize = f->sizek;
  471. k = fs->nk;
  472. /* numerical value does not need GC barrier;
  473. table has no metatable, so it does not need to invalidate cache */
  474. setivalue(idx, k);
  475. luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
  476. while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
  477. setobj(L, &f->k[k], v);
  478. fs->nk++;
  479. luaC_barrier(L, f, v);
  480. return k;
  481. }
  482. /*
  483. ** Add a string to list of constants and return its index.
  484. */
  485. static int stringK (FuncState *fs, TString *s) {
  486. TValue o;
  487. setsvalue(fs->ls->L, &o, s);
  488. return addk(fs, &o, &o); /* use string itself as key */
  489. }
  490. /*
  491. ** Add an integer to list of constants and return its index.
  492. ** Integers use userdata as keys to avoid collision with floats with
  493. ** same value; conversion to 'void*' is used only for hashing, so there
  494. ** are no "precision" problems.
  495. */
  496. static int luaK_intK (FuncState *fs, lua_Integer n) {
  497. TValue k, o;
  498. setpvalue(&k, cast_voidp(cast_sizet(n)));
  499. setivalue(&o, n);
  500. return addk(fs, &k, &o);
  501. }
  502. /*
  503. ** Add a float to list of constants and return its index.
  504. */
  505. static int luaK_numberK (FuncState *fs, lua_Number r) {
  506. TValue o;
  507. setfltvalue(&o, r);
  508. return addk(fs, &o, &o); /* use number itself as key */
  509. }
  510. /*
  511. ** Add a boolean to list of constants and return its index.
  512. */
  513. static int boolK (FuncState *fs, int b) {
  514. TValue o;
  515. setbvalue(&o, b);
  516. return addk(fs, &o, &o); /* use boolean itself as key */
  517. }
  518. /*
  519. ** Add nil to list of constants and return its index.
  520. */
  521. static int nilK (FuncState *fs) {
  522. TValue k, v;
  523. setnilvalue(&v);
  524. /* cannot use nil as key; instead use table itself to represent nil */
  525. sethvalue(fs->ls->L, &k, fs->ls->h);
  526. return addk(fs, &k, &v);
  527. }
  528. /*
  529. ** Check whether 'i' can be stored in an 'sC' operand.
  530. ** Equivalent to (0 <= i + OFFSET_sC && i + OFFSET_sC <= MAXARG_C)
  531. ** but without risk of overflows in the addition.
  532. */
  533. static int fitsC (lua_Integer i) {
  534. return (-OFFSET_sC <= i && i <= MAXARG_C - OFFSET_sC);
  535. }
  536. /*
  537. ** Check whether 'i' can be stored in an 'sBx' operand.
  538. */
  539. static int fitsBx (lua_Integer i) {
  540. return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx);
  541. }
  542. void luaK_int (FuncState *fs, int reg, lua_Integer i) {
  543. if (fitsBx(i))
  544. luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i));
  545. else
  546. luaK_codek(fs, reg, luaK_intK(fs, i));
  547. }
  548. static int floatI (lua_Number f, lua_Integer *fi) {
  549. return (luaV_flttointeger(f, fi, 0) && fitsBx(*fi));
  550. }
  551. static void luaK_float (FuncState *fs, int reg, lua_Number f) {
  552. lua_Integer fi;
  553. if (floatI(f, &fi))
  554. luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
  555. else
  556. luaK_codek(fs, reg, luaK_numberK(fs, f));
  557. }
  558. /*
  559. ** Convert a constant in 'v' into an expression description 'e'
  560. */
  561. static void const2exp (TValue *v, expdesc *e) {
  562. switch (ttypetag(v)) {
  563. case LUA_TNUMINT:
  564. e->k = VKINT; e->u.ival = ivalue(v);
  565. break;
  566. case LUA_TNUMFLT:
  567. e->k = VKFLT; e->u.nval = fltvalue(v);
  568. break;
  569. case LUA_TBOOLEAN:
  570. e->k = bvalue(v) ? VTRUE : VFALSE;
  571. break;
  572. case LUA_TNIL:
  573. e->k = VNIL;
  574. break;
  575. case LUA_TSHRSTR: case LUA_TLNGSTR:
  576. e->k = VKSTR; e->u.strval = tsvalue(v);
  577. break;
  578. default: lua_assert(0);
  579. }
  580. }
  581. /*
  582. ** Fix an expression to return the number of results 'nresults'.
  583. ** Either 'e' is a multi-ret expression (function call or vararg)
  584. ** or 'nresults' is LUA_MULTRET (as any expression can satisfy that).
  585. */
  586. void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
  587. Instruction *pc = &getinstruction(fs, e);
  588. if (e->k == VCALL) /* expression is an open function call? */
  589. SETARG_C(*pc, nresults + 1);
  590. else if (e->k == VVARARG) {
  591. SETARG_C(*pc, nresults + 1);
  592. SETARG_A(*pc, fs->freereg);
  593. luaK_reserveregs(fs, 1);
  594. }
  595. else lua_assert(nresults == LUA_MULTRET);
  596. }
  597. /*
  598. ** Convert a VKSTR to a VK
  599. */
  600. static void str2K (FuncState *fs, expdesc *e) {
  601. lua_assert(e->k == VKSTR);
  602. e->u.info = stringK(fs, e->u.strval);
  603. e->k = VK;
  604. }
  605. /*
  606. ** Fix an expression to return one result.
  607. ** If expression is not a multi-ret expression (function call or
  608. ** vararg), it already returns one result, so nothing needs to be done.
  609. ** Function calls become VNONRELOC expressions (as its result comes
  610. ** fixed in the base register of the call), while vararg expressions
  611. ** become VRELOC (as OP_VARARG puts its results where it wants).
  612. ** (Calls are created returning one result, so that does not need
  613. ** to be fixed.)
  614. */
  615. void luaK_setoneret (FuncState *fs, expdesc *e) {
  616. if (e->k == VCALL) { /* expression is an open function call? */
  617. /* already returns 1 value */
  618. lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
  619. e->k = VNONRELOC; /* result has fixed position */
  620. e->u.info = GETARG_A(getinstruction(fs, e));
  621. }
  622. else if (e->k == VVARARG) {
  623. SETARG_C(getinstruction(fs, e), 2);
  624. e->k = VRELOC; /* can relocate its simple result */
  625. }
  626. }
  627. /*
  628. ** Ensure that expression 'e' is not a variable (nor a constant).
  629. ** (Expression still may have jump lists.)
  630. */
  631. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  632. switch (e->k) {
  633. case VCONST: {
  634. const2exp(const2val(fs, e), e);
  635. break;
  636. }
  637. case VLOCAL: { /* already in a register */
  638. e->u.info = e->u.var.sidx;
  639. e->k = VNONRELOC; /* becomes a non-relocatable value */
  640. break;
  641. }
  642. case VUPVAL: { /* move value to some (pending) register */
  643. e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
  644. e->k = VRELOC;
  645. break;
  646. }
  647. case VINDEXUP: {
  648. e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx);
  649. e->k = VRELOC;
  650. break;
  651. }
  652. case VINDEXI: {
  653. freereg(fs, e->u.ind.t);
  654. e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx);
  655. e->k = VRELOC;
  656. break;
  657. }
  658. case VINDEXSTR: {
  659. freereg(fs, e->u.ind.t);
  660. e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx);
  661. e->k = VRELOC;
  662. break;
  663. }
  664. case VINDEXED: {
  665. freeregs(fs, e->u.ind.t, e->u.ind.idx);
  666. e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx);
  667. e->k = VRELOC;
  668. break;
  669. }
  670. case VVARARG: case VCALL: {
  671. luaK_setoneret(fs, e);
  672. break;
  673. }
  674. default: break; /* there is one value available (somewhere) */
  675. }
  676. }
  677. /*
  678. ** Ensures expression value is in register 'reg' (and therefore
  679. ** 'e' will become a non-relocatable expression).
  680. ** (Expression still may have jump lists.)
  681. */
  682. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  683. luaK_dischargevars(fs, e);
  684. switch (e->k) {
  685. case VNIL: {
  686. luaK_nil(fs, reg, 1);
  687. break;
  688. }
  689. case VFALSE: case VTRUE: {
  690. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  691. break;
  692. }
  693. case VKSTR: {
  694. str2K(fs, e);
  695. } /* FALLTHROUGH */
  696. case VK: {
  697. luaK_codek(fs, reg, e->u.info);
  698. break;
  699. }
  700. case VKFLT: {
  701. luaK_float(fs, reg, e->u.nval);
  702. break;
  703. }
  704. case VKINT: {
  705. luaK_int(fs, reg, e->u.ival);
  706. break;
  707. }
  708. case VRELOC: {
  709. Instruction *pc = &getinstruction(fs, e);
  710. SETARG_A(*pc, reg); /* instruction will put result in 'reg' */
  711. break;
  712. }
  713. case VNONRELOC: {
  714. if (reg != e->u.info)
  715. luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
  716. break;
  717. }
  718. default: {
  719. lua_assert(e->k == VJMP);
  720. return; /* nothing to do... */
  721. }
  722. }
  723. e->u.info = reg;
  724. e->k = VNONRELOC;
  725. }
  726. /*
  727. ** Ensures expression value is in any register.
  728. ** (Expression still may have jump lists.)
  729. */
  730. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  731. if (e->k != VNONRELOC) { /* no fixed register yet? */
  732. luaK_reserveregs(fs, 1); /* get a register */
  733. discharge2reg(fs, e, fs->freereg-1); /* put value there */
  734. }
  735. }
  736. static int code_loadbool (FuncState *fs, int A, int b, int jump) {
  737. luaK_getlabel(fs); /* those instructions may be jump targets */
  738. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  739. }
  740. /*
  741. ** check whether list has any jump that do not produce a value
  742. ** or produce an inverted value
  743. */
  744. static int need_value (FuncState *fs, int list) {
  745. for (; list != NO_JUMP; list = getjump(fs, list)) {
  746. Instruction i = *getjumpcontrol(fs, list);
  747. if (GET_OPCODE(i) != OP_TESTSET) return 1;
  748. }
  749. return 0; /* not found */
  750. }
  751. /*
  752. ** Ensures final expression result (which includes results from its
  753. ** jump lists) is in register 'reg'.
  754. ** If expression has jumps, need to patch these jumps either to
  755. ** its final position or to "load" instructions (for those tests
  756. ** that do not produce values).
  757. */
  758. static void exp2reg (FuncState *fs, expdesc *e, int reg) {
  759. discharge2reg(fs, e, reg);
  760. if (e->k == VJMP) /* expression itself is a test? */
  761. luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
  762. if (hasjumps(e)) {
  763. int final; /* position after whole expression */
  764. int p_f = NO_JUMP; /* position of an eventual LOAD false */
  765. int p_t = NO_JUMP; /* position of an eventual LOAD true */
  766. if (need_value(fs, e->t) || need_value(fs, e->f)) {
  767. int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
  768. p_f = code_loadbool(fs, reg, 0, 1); /* load false and skip next i. */
  769. p_t = code_loadbool(fs, reg, 1, 0); /* load true */
  770. /* jump around these booleans if 'e' is not a test */
  771. luaK_patchtohere(fs, fj);
  772. }
  773. final = luaK_getlabel(fs);
  774. patchlistaux(fs, e->f, final, reg, p_f);
  775. patchlistaux(fs, e->t, final, reg, p_t);
  776. }
  777. e->f = e->t = NO_JUMP;
  778. e->u.info = reg;
  779. e->k = VNONRELOC;
  780. }
  781. /*
  782. ** Ensures final expression result is in next available register.
  783. */
  784. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  785. luaK_dischargevars(fs, e);
  786. freeexp(fs, e);
  787. luaK_reserveregs(fs, 1);
  788. exp2reg(fs, e, fs->freereg - 1);
  789. }
  790. /*
  791. ** Ensures final expression result is in some (any) register
  792. ** and return that register.
  793. */
  794. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  795. luaK_dischargevars(fs, e);
  796. if (e->k == VNONRELOC) { /* expression already has a register? */
  797. if (!hasjumps(e)) /* no jumps? */
  798. return e->u.info; /* result is already in a register */
  799. if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */
  800. exp2reg(fs, e, e->u.info); /* put final result in it */
  801. return e->u.info;
  802. }
  803. }
  804. luaK_exp2nextreg(fs, e); /* otherwise, use next available register */
  805. return e->u.info;
  806. }
  807. /*
  808. ** Ensures final expression result is either in a register
  809. ** or in an upvalue.
  810. */
  811. void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
  812. if (e->k != VUPVAL || hasjumps(e))
  813. luaK_exp2anyreg(fs, e);
  814. }
  815. /*
  816. ** Ensures final expression result is either in a register
  817. ** or it is a constant.
  818. */
  819. void luaK_exp2val (FuncState *fs, expdesc *e) {
  820. if (hasjumps(e))
  821. luaK_exp2anyreg(fs, e);
  822. else
  823. luaK_dischargevars(fs, e);
  824. }
  825. /*
  826. ** Try to make 'e' a K expression with an index in the range of R/K
  827. ** indices. Return true iff succeeded.
  828. */
  829. static int luaK_exp2K (FuncState *fs, expdesc *e) {
  830. if (!hasjumps(e)) {
  831. int info;
  832. switch (e->k) { /* move constants to 'k' */
  833. case VTRUE: info = boolK(fs, 1); break;
  834. case VFALSE: info = boolK(fs, 0); break;
  835. case VNIL: info = nilK(fs); break;
  836. case VKINT: info = luaK_intK(fs, e->u.ival); break;
  837. case VKFLT: info = luaK_numberK(fs, e->u.nval); break;
  838. case VKSTR: info = stringK(fs, e->u.strval); break;
  839. case VK: info = e->u.info; break;
  840. default: return 0; /* not a constant */
  841. }
  842. if (info <= MAXINDEXRK) { /* does constant fit in 'argC'? */
  843. e->k = VK; /* make expression a 'K' expression */
  844. e->u.info = info;
  845. return 1;
  846. }
  847. }
  848. /* else, expression doesn't fit; leave it unchanged */
  849. return 0;
  850. }
  851. /*
  852. ** Ensures final expression result is in a valid R/K index
  853. ** (that is, it is either in a register or in 'k' with an index
  854. ** in the range of R/K indices).
  855. ** Returns 1 iff expression is K.
  856. */
  857. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  858. if (luaK_exp2K(fs, e))
  859. return 1;
  860. else { /* not a constant in the right range: put it in a register */
  861. luaK_exp2anyreg(fs, e);
  862. return 0;
  863. }
  864. }
  865. static void codeABRK (FuncState *fs, OpCode o, int a, int b,
  866. expdesc *ec) {
  867. int k = luaK_exp2RK(fs, ec);
  868. luaK_codeABCk(fs, o, a, b, ec->u.info, k);
  869. }
  870. /*
  871. ** Generate code to store result of expression 'ex' into variable 'var'.
  872. */
  873. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
  874. switch (var->k) {
  875. case VLOCAL: {
  876. freeexp(fs, ex);
  877. exp2reg(fs, ex, var->u.var.sidx); /* compute 'ex' into proper place */
  878. return;
  879. }
  880. case VUPVAL: {
  881. int e = luaK_exp2anyreg(fs, ex);
  882. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
  883. break;
  884. }
  885. case VINDEXUP: {
  886. codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex);
  887. break;
  888. }
  889. case VINDEXI: {
  890. codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex);
  891. break;
  892. }
  893. case VINDEXSTR: {
  894. codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
  895. break;
  896. }
  897. case VINDEXED: {
  898. codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
  899. break;
  900. }
  901. default: lua_assert(0); /* invalid var kind to store */
  902. }
  903. freeexp(fs, ex);
  904. }
  905. /*
  906. ** Emit SELF instruction (convert expression 'e' into 'e:key(e,').
  907. */
  908. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  909. int ereg;
  910. luaK_exp2anyreg(fs, e);
  911. ereg = e->u.info; /* register where 'e' was placed */
  912. freeexp(fs, e);
  913. e->u.info = fs->freereg; /* base register for op_self */
  914. e->k = VNONRELOC; /* self expression has a fixed register */
  915. luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
  916. codeABRK(fs, OP_SELF, e->u.info, ereg, key);
  917. freeexp(fs, key);
  918. }
  919. /*
  920. ** Negate condition 'e' (where 'e' is a comparison).
  921. */
  922. static void negatecondition (FuncState *fs, expdesc *e) {
  923. Instruction *pc = getjumpcontrol(fs, e->u.info);
  924. lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
  925. GET_OPCODE(*pc) != OP_TEST);
  926. SETARG_k(*pc, (GETARG_k(*pc) ^ 1));
  927. }
  928. /*
  929. ** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
  930. ** is true, code will jump if 'e' is true.) Return jump position.
  931. ** Optimize when 'e' is 'not' something, inverting the condition
  932. ** and removing the 'not'.
  933. */
  934. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  935. if (e->k == VRELOC) {
  936. Instruction ie = getinstruction(fs, e);
  937. if (GET_OPCODE(ie) == OP_NOT) {
  938. removelastinstruction(fs); /* remove previous OP_NOT */
  939. return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond);
  940. }
  941. /* else go through */
  942. }
  943. discharge2anyreg(fs, e);
  944. freeexp(fs, e);
  945. return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond);
  946. }
  947. /*
  948. ** Emit code to go through if 'e' is true, jump otherwise.
  949. */
  950. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  951. int pc; /* pc of new jump */
  952. luaK_dischargevars(fs, e);
  953. switch (e->k) {
  954. case VJMP: { /* condition? */
  955. negatecondition(fs, e); /* jump when it is false */
  956. pc = e->u.info; /* save jump position */
  957. break;
  958. }
  959. case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
  960. pc = NO_JUMP; /* always true; do nothing */
  961. break;
  962. }
  963. default: {
  964. pc = jumponcond(fs, e, 0); /* jump when false */
  965. break;
  966. }
  967. }
  968. luaK_concat(fs, &e->f, pc); /* insert new jump in false list */
  969. luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */
  970. e->t = NO_JUMP;
  971. }
  972. /*
  973. ** Emit code to go through if 'e' is false, jump otherwise.
  974. */
  975. void luaK_goiffalse (FuncState *fs, expdesc *e) {
  976. int pc; /* pc of new jump */
  977. luaK_dischargevars(fs, e);
  978. switch (e->k) {
  979. case VJMP: {
  980. pc = e->u.info; /* already jump if true */
  981. break;
  982. }
  983. case VNIL: case VFALSE: {
  984. pc = NO_JUMP; /* always false; do nothing */
  985. break;
  986. }
  987. default: {
  988. pc = jumponcond(fs, e, 1); /* jump if true */
  989. break;
  990. }
  991. }
  992. luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */
  993. luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */
  994. e->f = NO_JUMP;
  995. }
  996. /*
  997. ** Code 'not e', doing constant folding.
  998. */
  999. static void codenot (FuncState *fs, expdesc *e) {
  1000. switch (e->k) {
  1001. case VNIL: case VFALSE: {
  1002. e->k = VTRUE; /* true == not nil == not false */
  1003. break;
  1004. }
  1005. case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
  1006. e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */
  1007. break;
  1008. }
  1009. case VJMP: {
  1010. negatecondition(fs, e);
  1011. break;
  1012. }
  1013. case VRELOC:
  1014. case VNONRELOC: {
  1015. discharge2anyreg(fs, e);
  1016. freeexp(fs, e);
  1017. e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
  1018. e->k = VRELOC;
  1019. break;
  1020. }
  1021. default: lua_assert(0); /* cannot happen */
  1022. }
  1023. /* interchange true and false lists */
  1024. { int temp = e->f; e->f = e->t; e->t = temp; }
  1025. removevalues(fs, e->f); /* values are useless when negated */
  1026. removevalues(fs, e->t);
  1027. }
  1028. /*
  1029. ** Check whether expression 'e' is a small literal string
  1030. */
  1031. static int isKstr (FuncState *fs, expdesc *e) {
  1032. return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B &&
  1033. ttisshrstring(&fs->f->k[e->u.info]));
  1034. }
  1035. /*
  1036. ** Check whether expression 'e' is a literal integer.
  1037. */
  1038. int luaK_isKint (expdesc *e) {
  1039. return (e->k == VKINT && !hasjumps(e));
  1040. }
  1041. /*
  1042. ** Check whether expression 'e' is a literal integer in
  1043. ** proper range to fit in register C
  1044. */
  1045. static int isCint (expdesc *e) {
  1046. return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
  1047. }
  1048. /*
  1049. ** Check whether expression 'e' is a literal integer in
  1050. ** proper range to fit in register sC
  1051. */
  1052. static int isSCint (expdesc *e) {
  1053. return luaK_isKint(e) && fitsC(e->u.ival);
  1054. }
  1055. /*
  1056. ** Check whether expression 'e' is a literal integer or float in
  1057. ** proper range to fit in a register (sB or sC).
  1058. */
  1059. static int isSCnumber (expdesc *e, lua_Integer *i, int *isfloat) {
  1060. if (e->k == VKINT)
  1061. *i = e->u.ival;
  1062. else if (!(e->k == VKFLT && floatI(e->u.nval, i)))
  1063. return 0; /* not a number */
  1064. else
  1065. *isfloat = 1;
  1066. if (!hasjumps(e) && fitsC(*i)) {
  1067. *i += OFFSET_sC;
  1068. return 1;
  1069. }
  1070. else
  1071. return 0;
  1072. }
  1073. /*
  1074. ** Create expression 't[k]'. 't' must have its final result already in a
  1075. ** register or upvalue. Upvalues can only be indexed by literal strings.
  1076. ** Keys can be literal strings in the constant table or arbitrary
  1077. ** values in registers.
  1078. */
  1079. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  1080. if (k->k == VKSTR)
  1081. str2K(fs, k);
  1082. lua_assert(!hasjumps(t) &&
  1083. (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL));
  1084. if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
  1085. luaK_exp2anyreg(fs, t); /* put it in a register */
  1086. if (t->k == VUPVAL) {
  1087. t->u.ind.t = t->u.info; /* upvalue index */
  1088. t->u.ind.idx = k->u.info; /* literal string */
  1089. t->k = VINDEXUP;
  1090. }
  1091. else {
  1092. /* register index of the table */
  1093. t->u.ind.t = (t->k == VLOCAL) ? t->u.var.sidx: t->u.info;
  1094. if (isKstr(fs, k)) {
  1095. t->u.ind.idx = k->u.info; /* literal string */
  1096. t->k = VINDEXSTR;
  1097. }
  1098. else if (isCint(k)) {
  1099. t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */
  1100. t->k = VINDEXI;
  1101. }
  1102. else {
  1103. t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */
  1104. t->k = VINDEXED;
  1105. }
  1106. }
  1107. }
  1108. /*
  1109. ** Return false if folding can raise an error.
  1110. ** Bitwise operations need operands convertible to integers; division
  1111. ** operations cannot have 0 as divisor.
  1112. */
  1113. static int validop (int op, TValue *v1, TValue *v2) {
  1114. switch (op) {
  1115. case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
  1116. case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
  1117. lua_Integer i;
  1118. return (tointegerns(v1, &i) && tointegerns(v2, &i));
  1119. }
  1120. case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
  1121. return (nvalue(v2) != 0);
  1122. default: return 1; /* everything else is valid */
  1123. }
  1124. }
  1125. /*
  1126. ** Try to "constant-fold" an operation; return 1 iff successful.
  1127. ** (In this case, 'e1' has the final result.)
  1128. */
  1129. static int constfolding (FuncState *fs, int op, expdesc *e1,
  1130. const expdesc *e2) {
  1131. TValue v1, v2, res;
  1132. if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
  1133. return 0; /* non-numeric operands or not safe to fold */
  1134. luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */
  1135. if (ttisinteger(&res)) {
  1136. e1->k = VKINT;
  1137. e1->u.ival = ivalue(&res);
  1138. }
  1139. else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
  1140. lua_Number n = fltvalue(&res);
  1141. if (luai_numisnan(n) || n == 0)
  1142. return 0;
  1143. e1->k = VKFLT;
  1144. e1->u.nval = n;
  1145. }
  1146. return 1;
  1147. }
  1148. /*
  1149. ** Emit code for unary expressions that "produce values"
  1150. ** (everything but 'not').
  1151. ** Expression to produce final result will be encoded in 'e'.
  1152. */
  1153. static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
  1154. int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */
  1155. freeexp(fs, e);
  1156. e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */
  1157. e->k = VRELOC; /* all those operations are relocatable */
  1158. luaK_fixline(fs, line);
  1159. }
  1160. /*
  1161. ** Emit code for binary expressions that "produce values"
  1162. ** (everything but logical operators 'and'/'or' and comparison
  1163. ** operators).
  1164. ** Expression to produce final result will be encoded in 'e1'.
  1165. */
  1166. static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
  1167. OpCode op, int v2, int flip, int line,
  1168. OpCode mmop, TMS event) {
  1169. int v1 = luaK_exp2anyreg(fs, e1);
  1170. int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0);
  1171. freeexps(fs, e1, e2);
  1172. e1->u.info = pc;
  1173. e1->k = VRELOC; /* all those operations are relocatable */
  1174. luaK_fixline(fs, line);
  1175. if (op == OP_SHRI && flip) {
  1176. /* For the metamethod, undo the "changedir" did by 'codeshift' */
  1177. event = TM_SHL;
  1178. v2 = -(v2 - OFFSET_sC) + OFFSET_sC;
  1179. flip = 0;
  1180. }
  1181. luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */
  1182. luaK_fixline(fs, line);
  1183. }
  1184. /*
  1185. ** Emit code for binary expressions that "produce values" over
  1186. ** two registers.
  1187. */
  1188. static void codebinexpval (FuncState *fs, OpCode op,
  1189. expdesc *e1, expdesc *e2, int line) {
  1190. int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */
  1191. lua_assert(OP_ADD <= op && op <= OP_SHR);
  1192. finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN,
  1193. cast(TMS, (op - OP_ADD) + TM_ADD));
  1194. }
  1195. /*
  1196. ** Code binary operators with immediate operands.
  1197. */
  1198. static void codebini (FuncState *fs, OpCode op,
  1199. expdesc *e1, expdesc *e2, int flip, int line,
  1200. TMS event) {
  1201. int v2 = cast_int(e2->u.ival) + OFFSET_sC; /* immediate operand */
  1202. finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event);
  1203. }
  1204. static void swapexps (expdesc *e1, expdesc *e2) {
  1205. expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */
  1206. }
  1207. /*
  1208. ** Code arithmetic operators ('+', '-', ...). If second operand is a
  1209. ** constant in the proper range, use variant opcodes with K operands.
  1210. */
  1211. static void codearith (FuncState *fs, BinOpr opr,
  1212. expdesc *e1, expdesc *e2, int flip, int line) {
  1213. TMS event = cast(TMS, opr + TM_ADD);
  1214. if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */
  1215. int v2 = e2->u.info; /* K index */
  1216. OpCode op = cast(OpCode, opr + OP_ADDK);
  1217. finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
  1218. }
  1219. else { /* 'e2' is neither an immediate nor a K operand */
  1220. OpCode op = cast(OpCode, opr + OP_ADD);
  1221. if (flip)
  1222. swapexps(e1, e2); /* back to original order */
  1223. codebinexpval(fs, op, e1, e2, line); /* use standard operators */
  1224. }
  1225. }
  1226. /*
  1227. ** Code commutative operators ('+', '*'). If first operand is a
  1228. ** numeric constant, change order of operands to try to use an
  1229. ** immediate or K operator.
  1230. */
  1231. static void codecommutative (FuncState *fs, BinOpr op,
  1232. expdesc *e1, expdesc *e2, int line) {
  1233. int flip = 0;
  1234. if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */
  1235. swapexps(e1, e2); /* change order */
  1236. flip = 1;
  1237. }
  1238. if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */
  1239. codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD);
  1240. else
  1241. codearith(fs, op, e1, e2, flip, line);
  1242. }
  1243. /*
  1244. ** Code bitwise operations; they are all associative, so the function
  1245. ** tries to put an integer constant as the 2nd operand (a K operand).
  1246. */
  1247. static void codebitwise (FuncState *fs, BinOpr opr,
  1248. expdesc *e1, expdesc *e2, int line) {
  1249. int flip = 0;
  1250. int v2;
  1251. OpCode op;
  1252. if (e1->k == VKINT && luaK_exp2RK(fs, e1)) {
  1253. swapexps(e1, e2); /* 'e2' will be the constant operand */
  1254. flip = 1;
  1255. }
  1256. else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */
  1257. op = cast(OpCode, opr + OP_ADD);
  1258. codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */
  1259. return;
  1260. }
  1261. v2 = e2->u.info; /* index in K array */
  1262. op = cast(OpCode, opr + OP_ADDK);
  1263. lua_assert(ttisinteger(&fs->f->k[v2]));
  1264. finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK,
  1265. cast(TMS, opr + TM_ADD));
  1266. }
  1267. /*
  1268. ** Code shift operators. If second operand is constant, use immediate
  1269. ** operand (negating it if shift is in the other direction).
  1270. */
  1271. static void codeshift (FuncState *fs, OpCode op,
  1272. expdesc *e1, expdesc *e2, int line) {
  1273. if (isSCint(e2)) {
  1274. int changedir = 0;
  1275. if (op == OP_SHL) {
  1276. changedir = 1;
  1277. e2->u.ival = -(e2->u.ival);
  1278. }
  1279. codebini(fs, OP_SHRI, e1, e2, changedir, line, TM_SHR);
  1280. }
  1281. else
  1282. codebinexpval(fs, op, e1, e2, line);
  1283. }
  1284. /*
  1285. ** Emit code for order comparisons. When using an immediate operand,
  1286. ** 'isfloat' tells whether the original value was a float.
  1287. */
  1288. static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
  1289. int r1, r2;
  1290. lua_Integer im;
  1291. int isfloat = 0;
  1292. if (isSCnumber(e2, &im, &isfloat)) {
  1293. /* use immediate operand */
  1294. r1 = luaK_exp2anyreg(fs, e1);
  1295. r2 = cast_int(im);
  1296. op = cast(OpCode, (op - OP_LT) + OP_LTI);
  1297. }
  1298. else if (isSCnumber(e1, &im, &isfloat)) {
  1299. /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
  1300. r1 = luaK_exp2anyreg(fs, e2);
  1301. r2 = cast_int(im);
  1302. op = (op == OP_LT) ? OP_GTI : OP_GEI;
  1303. }
  1304. else { /* regular case, compare two registers */
  1305. r1 = luaK_exp2anyreg(fs, e1);
  1306. r2 = luaK_exp2anyreg(fs, e2);
  1307. }
  1308. freeexps(fs, e1, e2);
  1309. e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
  1310. e1->k = VJMP;
  1311. }
  1312. /*
  1313. ** Emit code for equality comparisons ('==', '~=').
  1314. ** 'e1' was already put as RK by 'luaK_infix'.
  1315. */
  1316. static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
  1317. int r1, r2;
  1318. lua_Integer im;
  1319. int isfloat = 0; /* not needed here, but kept for symmetry */
  1320. OpCode op;
  1321. if (e1->k != VNONRELOC) {
  1322. lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
  1323. swapexps(e1, e2);
  1324. }
  1325. r1 = luaK_exp2anyreg(fs, e1); /* 1nd expression must be in register */
  1326. if (isSCnumber(e2, &im, &isfloat)) {
  1327. op = OP_EQI;
  1328. r2 = cast_int(im); /* immediate operand */
  1329. }
  1330. else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */
  1331. op = OP_EQK;
  1332. r2 = e2->u.info; /* constant index */
  1333. }
  1334. else {
  1335. op = OP_EQ; /* will compare two registers */
  1336. r2 = luaK_exp2anyreg(fs, e2);
  1337. }
  1338. freeexps(fs, e1, e2);
  1339. e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ));
  1340. e1->k = VJMP;
  1341. }
  1342. /*
  1343. ** Apply prefix operation 'op' to expression 'e'.
  1344. */
  1345. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
  1346. static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
  1347. luaK_dischargevars(fs, e);
  1348. switch (op) {
  1349. case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
  1350. if (constfolding(fs, op + LUA_OPUNM, e, &ef))
  1351. break;
  1352. /* else */ /* FALLTHROUGH */
  1353. case OPR_LEN:
  1354. codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line);
  1355. break;
  1356. case OPR_NOT: codenot(fs, e); break;
  1357. default: lua_assert(0);
  1358. }
  1359. }
  1360. /*
  1361. ** Process 1st operand 'v' of binary operation 'op' before reading
  1362. ** 2nd operand.
  1363. */
  1364. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  1365. luaK_dischargevars(fs, v);
  1366. switch (op) {
  1367. case OPR_AND: {
  1368. luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */
  1369. break;
  1370. }
  1371. case OPR_OR: {
  1372. luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */
  1373. break;
  1374. }
  1375. case OPR_CONCAT: {
  1376. luaK_exp2nextreg(fs, v); /* operand must be on the stack */
  1377. break;
  1378. }
  1379. case OPR_ADD: case OPR_SUB:
  1380. case OPR_MUL: case OPR_DIV: case OPR_IDIV:
  1381. case OPR_MOD: case OPR_POW:
  1382. case OPR_BAND: case OPR_BOR: case OPR_BXOR:
  1383. case OPR_SHL: case OPR_SHR: {
  1384. if (!tonumeral(v, NULL))
  1385. luaK_exp2anyreg(fs, v);
  1386. /* else keep numeral, which may be folded with 2nd operand */
  1387. break;
  1388. }
  1389. case OPR_EQ: case OPR_NE: {
  1390. if (!tonumeral(v, NULL))
  1391. luaK_exp2RK(fs, v);
  1392. /* else keep numeral, which may be an immediate operand */
  1393. break;
  1394. }
  1395. case OPR_LT: case OPR_LE:
  1396. case OPR_GT: case OPR_GE: {
  1397. lua_Integer dummy;
  1398. int dummy2;
  1399. if (!isSCnumber(v, &dummy, &dummy2))
  1400. luaK_exp2anyreg(fs, v);
  1401. /* else keep numeral, which may be an immediate operand */
  1402. break;
  1403. }
  1404. default: lua_assert(0);
  1405. }
  1406. }
  1407. /*
  1408. ** Create code for '(e1 .. e2)'.
  1409. ** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))',
  1410. ** because concatenation is right associative), merge both CONCATs.
  1411. */
  1412. static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
  1413. Instruction *ie2 = previousinstruction(fs);
  1414. if (GET_OPCODE(*ie2) == OP_CONCAT) { /* is 'e2' a concatenation? */
  1415. int n = GETARG_B(*ie2); /* # of elements concatenated in 'e2' */
  1416. lua_assert(e1->u.info + 1 == GETARG_A(*ie2));
  1417. freeexp(fs, e2);
  1418. SETARG_A(*ie2, e1->u.info); /* correct first element ('e1') */
  1419. SETARG_B(*ie2, n + 1); /* will concatenate one more element */
  1420. }
  1421. else { /* 'e2' is not a concatenation */
  1422. luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0); /* new concat opcode */
  1423. freeexp(fs, e2);
  1424. luaK_fixline(fs, line);
  1425. }
  1426. }
  1427. /*
  1428. ** Finalize code for binary operation, after reading 2nd operand.
  1429. */
  1430. void luaK_posfix (FuncState *fs, BinOpr opr,
  1431. expdesc *e1, expdesc *e2, int line) {
  1432. luaK_dischargevars(fs, e2);
  1433. if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2))
  1434. return; /* done by folding */
  1435. switch (opr) {
  1436. case OPR_AND: {
  1437. lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */
  1438. luaK_concat(fs, &e2->f, e1->f);
  1439. *e1 = *e2;
  1440. break;
  1441. }
  1442. case OPR_OR: {
  1443. lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */
  1444. luaK_concat(fs, &e2->t, e1->t);
  1445. *e1 = *e2;
  1446. break;
  1447. }
  1448. case OPR_CONCAT: { /* e1 .. e2 */
  1449. luaK_exp2nextreg(fs, e2);
  1450. codeconcat(fs, e1, e2, line);
  1451. break;
  1452. }
  1453. case OPR_ADD: case OPR_MUL: {
  1454. codecommutative(fs, opr, e1, e2, line);
  1455. break;
  1456. }
  1457. case OPR_SUB: case OPR_DIV:
  1458. case OPR_IDIV: case OPR_MOD: case OPR_POW: {
  1459. codearith(fs, opr, e1, e2, 0, line);
  1460. break;
  1461. }
  1462. case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
  1463. codebitwise(fs, opr, e1, e2, line);
  1464. break;
  1465. }
  1466. case OPR_SHL: {
  1467. if (isSCint(e1)) {
  1468. swapexps(e1, e2);
  1469. codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL);
  1470. }
  1471. else
  1472. codeshift(fs, OP_SHL, e1, e2, line);
  1473. break;
  1474. }
  1475. case OPR_SHR: {
  1476. codeshift(fs, OP_SHR, e1, e2, line);
  1477. break;
  1478. }
  1479. case OPR_EQ: case OPR_NE: {
  1480. codeeq(fs, opr, e1, e2);
  1481. break;
  1482. }
  1483. case OPR_LT: case OPR_LE: {
  1484. OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ);
  1485. codeorder(fs, op, e1, e2);
  1486. break;
  1487. }
  1488. case OPR_GT: case OPR_GE: {
  1489. /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */
  1490. OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ);
  1491. swapexps(e1, e2);
  1492. codeorder(fs, op, e1, e2);
  1493. break;
  1494. }
  1495. default: lua_assert(0);
  1496. }
  1497. }
  1498. /*
  1499. ** Change line information associated with current position, by removing
  1500. ** previous info and adding it again with new line.
  1501. */
  1502. void luaK_fixline (FuncState *fs, int line) {
  1503. removelastlineinfo(fs);
  1504. savelineinfo(fs, fs->f, line);
  1505. }
  1506. void luaK_settablesize (FuncState *fs, int pc, int ra, int rc, int rb) {
  1507. Instruction *inst = &fs->f->code[pc];
  1508. int extra = 0;
  1509. int k = 0;
  1510. if (rb != 0)
  1511. rb = luaO_ceillog2(rb) + 1; /* hash size */
  1512. if (rc > MAXARG_C) { /* does it need the extra argument? */
  1513. extra = rc / (MAXARG_C + 1);
  1514. rc %= (MAXARG_C + 1);
  1515. k = 1;
  1516. }
  1517. *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k);
  1518. *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
  1519. }
  1520. /*
  1521. ** Emit a SETLIST instruction.
  1522. ** 'base' is register that keeps table;
  1523. ** 'nelems' is #table plus those to be stored now;
  1524. ** 'tostore' is number of values (in registers 'base + 1',...) to add to
  1525. ** table (or LUA_MULTRET to add up to stack top).
  1526. */
  1527. void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
  1528. lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
  1529. if (tostore == LUA_MULTRET)
  1530. tostore = 0;
  1531. if (nelems <= MAXARG_C)
  1532. luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems);
  1533. else {
  1534. int extra = nelems / (MAXARG_C + 1);
  1535. nelems %= (MAXARG_C + 1);
  1536. luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
  1537. codeextraarg(fs, extra);
  1538. }
  1539. fs->freereg = base + 1; /* free registers with list values */
  1540. }
  1541. /*
  1542. ** return the final target of a jump (skipping jumps to jumps)
  1543. */
  1544. static int finaltarget (Instruction *code, int i) {
  1545. int count;
  1546. for (count = 0; count < 100; count++) { /* avoid infinite loops */
  1547. Instruction pc = code[i];
  1548. if (GET_OPCODE(pc) != OP_JMP)
  1549. break;
  1550. else
  1551. i += GETARG_sJ(pc) + 1;
  1552. }
  1553. return i;
  1554. }
  1555. /*
  1556. ** Do a final pass over the code of a function, doing small peephole
  1557. ** optimizations and adjustments.
  1558. */
  1559. void luaK_finish (FuncState *fs) {
  1560. int i;
  1561. Proto *p = fs->f;
  1562. for (i = 0; i < fs->pc; i++) {
  1563. Instruction *pc = &p->code[i];
  1564. lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc));
  1565. switch (GET_OPCODE(*pc)) {
  1566. case OP_RETURN0: case OP_RETURN1: {
  1567. if (!(fs->needclose || p->is_vararg))
  1568. break; /* no extra work */
  1569. /* else use OP_RETURN to do the extra work */
  1570. SET_OPCODE(*pc, OP_RETURN);
  1571. } /* FALLTHROUGH */
  1572. case OP_RETURN: case OP_TAILCALL: {
  1573. if (fs->needclose)
  1574. SETARG_k(*pc, 1); /* signal that it needs to close */
  1575. if (p->is_vararg)
  1576. SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */
  1577. break;
  1578. }
  1579. case OP_JMP: {
  1580. int target = finaltarget(p->code, i);
  1581. fixjump(fs, i, target);
  1582. break;
  1583. }
  1584. default: break;
  1585. }
  1586. }
  1587. }