History

Lua 5.3.0-rc0

Lua Team committed 11 years ago

Commit 17ba7311 parent 10f306357 files · +1476 −1139Browse files

Showing the changes to src/lcode.c (1 of 57 files). Show all

Unified · Split

src/lcode.c+44−29
@@ -1,16 +1,18 @@
11 /*
2−** $Id: lcode.c,v 2.90 2014/05/08 18:58:46 roberto Exp $
2+** $Id: lcode.c,v 2.97 2014/11/24 14:59:22 roberto Exp $
33 ** Code generator for Lua
44 ** See Copyright Notice in lua.h
55 */
66
7+#define lcode_c
8+#define LUA_CORE
9+
10+#include "lprefix.h"
11+
712
813 #include <math.h>
914 #include <stdlib.h>
1015
11−#define lcode_c
12−#define LUA_CORE
13−
1416 #include "lua.h"
1517
1618 #include "lcode.h"
@@ -27,7 +29,11 @@ · 10 unmodified lines
2729 #include "lvm.h"
2830
2931
30−/* test for x == -0 */
32+/* Maximum number of registers in a Lua function */
33+#define MAXREGS 250
34+
35+
36+/* test for x == -0 ('signbit' needs 'math.h') */
3137 #if defined(signbit)
3238 #define isminuszero(x) ((x) == 0.0 && signbit(x))
3339 #else
@@ -107,7 +113,7 @@ static void fixjump (FuncState *fs, int pc, int dest) { · 73 unmodified lines
107113
108114
109115 /*
110−** returns current `pc' and marks it as a jump target (to avoid wrong
116+** returns current 'pc' and marks it as a jump target (to avoid wrong
111117 ** optimizations with consecutive instructions not in the same basic block).
112118 */
113119 int luaK_getlabel (FuncState *fs) {
@@ -230,7 +236,7 @@ void luaK_concat (FuncState *fs, int *l1, int l2) { · 116 unmodified lines
230236
231237 static int luaK_code (FuncState *fs, Instruction i) {
232238 Proto *f = fs->f;
233− dischargejpc(fs); /* `pc' will change */
239+ dischargejpc(fs); /* 'pc' will change */
234240 /* put new instruction in code array */
235241 luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
236242 MAX_INT, "opcodes");
@@ -280,7 +286,7 @@ int luaK_codek (FuncState *fs, int reg, int k) { · 43 unmodified lines
280286 void luaK_checkstack (FuncState *fs, int n) {
281287 int newstack = fs->freereg + n;
282288 if (newstack > fs->f->maxstacksize) {
283− if (newstack >= MAXSTACK)
289+ if (newstack >= MAXREGS)
284290 luaX_syntaxerror(fs->ls, "function or expression too complex");
285291 fs->f->maxstacksize = cast_byte(newstack);
286292 }
@@ -503,7 +509,7 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) { · 216 unmodified lines
503509 static void exp2reg (FuncState *fs, expdesc *e, int reg) {
504510 discharge2reg(fs, e, reg);
505511 if (e->k == VJMP)
506− luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */
512+ luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
507513 if (hasjumps(e)) {
508514 int final; /* position after whole expression */
509515 int p_f = NO_JUMP; /* position of an eventual LOAD false */
@@ -585,7 +591,7 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) { · 75 unmodified lines
585591 }
586592 case VK: {
587593 vk:
588− if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */
594+ if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */
589595 return RKASK(e->u.info);
590596 else break;
591597 }
@@ -677,7 +683,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { · 85 unmodified lines
677683 break;
678684 }
679685 }
680− luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
686+ luaK_concat(fs, &e->f, pc); /* insert last jump in 'f' list */
681687 luaK_patchtohere(fs, e->t);
682688 e->t = NO_JUMP;
683689 }
@@ -700,7 +706,7 @@ void luaK_goiffalse (FuncState *fs, expdesc *e) { · 16 unmodified lines
700706 break;
701707 }
702708 }
703− luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
709+ luaK_concat(fs, &e->t, pc); /* insert last jump in 't' list */
704710 luaK_patchtohere(fs, e->f);
705711 e->f = NO_JUMP;
706712 }
@@ -762,12 +768,10 @@ static int validop (int op, TValue *v1, TValue *v2) { · 55 unmodified lines
762768 (cast_void(tonumber(v2, &b)), b)))
763769 return 0;
764770 switch (op) {
765− case LUA_OPIDIV: /* division by 0 and conversion errors */
766− return (tointeger(v1, &i) && tointeger(v2, &i) && i != 0);
767771 case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
768772 case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: /* conversion errors */
769773 return (tointeger(v1, &i) && tointeger(v2, &i));
770− case LUA_OPMOD: /* integer module by 0 */
774+ case LUA_OPIDIV: case LUA_OPMOD: /* integer division by 0 */
771775 return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0);
772776 default: return 1; /* everything else is valid */
773777 }
@@ -797,19 +801,30 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { · 23 unmodified lines
797801 }
798802
799803
800−static void codearith (FuncState *fs, OpCode op,
801− expdesc *e1, expdesc *e2, int line) {
802− if (!constfolding(fs, op - OP_ADD + LUA_OPADD, e1, e2)) {
804+/*
805+** Code for binary and unary expressions that "produce values"
806+** (arithmetic operations, bitwise operations, concat, length). First
807+** try to do constant folding (only for numeric [arithmetic and
808+** bitwise] operations, which is what 'lua_arith' accepts).
809+** Expression to produce final result will be encoded in 'e1'.
810+*/
811+static void codeexpval (FuncState *fs, OpCode op,
812+ expdesc *e1, expdesc *e2, int line) {
813+ lua_assert(op >= OP_ADD);
814+ if (op <= OP_BNOT && constfolding(fs, op - OP_ADD + LUA_OPADD, e1, e2))
815+ return; /* result has been folded */
816+ else {
803817 int o1, o2;
804− if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) {
805− o2 = 0;
818+ /* move operands to registers (if needed) */
819+ if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) { /* unary op? */
820+ o2 = 0; /* no second expression */
806821 o1 = luaK_exp2anyreg(fs, e1); /* cannot operate on constants */
807822 }
808823 else { /* regular case (binary operators) */
809− o2 = luaK_exp2RK(fs, e2);
824+ o2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */
810825 o1 = luaK_exp2RK(fs, e1);
811826 }
812− if (o1 > o2) {
827+ if (o1 > o2) { /* free registers in proper order */
813828 freeexp(fs, e1);
814829 freeexp(fs, e2);
815830 }
@@ -817,8 +832,8 @@ static void codearith (FuncState *fs, OpCode op, · 1 unmodified line
817832 freeexp(fs, e2);
818833 freeexp(fs, e1);
819834 }
820− e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);
821− e1->k = VRELOCABLE;
835+ e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); /* generate opcode */
836+ e1->k = VRELOCABLE; /* all those operations are relocable */
822837 luaK_fixline(fs, line);
823838 }
824839 }
@@ -831,7 +846,7 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, · 6 unmodified lines
831846 freeexp(fs, e2);
832847 freeexp(fs, e1);
833848 if (cond == 0 && op != OP_EQ) {
834− int temp; /* exchange args to replace by `<' or `<=' */
849+ int temp; /* exchange args to replace by '<' or '<=' */
835850 temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */
836851 cond = 1;
837852 }
@@ -845,7 +860,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { · 7 unmodified lines
845860 e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0;
846861 switch (op) {
847862 case OPR_MINUS: case OPR_BNOT: case OPR_LEN: {
848− codearith(fs, cast(OpCode, (op - OPR_MINUS) + OP_UNM), e, &e2, line);
863+ codeexpval(fs, cast(OpCode, (op - OPR_MINUS) + OP_UNM), e, &e2, line);
849864 break;
850865 }
851866 case OPR_NOT: codenot(fs, e); break;
@@ -865,7 +880,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { · 13 unmodified lines
865880 break;
866881 }
867882 case OPR_CONCAT: {
868− luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
883+ luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */
869884 break;
870885 }
871886 case OPR_ADD: case OPR_SUB:
@@ -911,7 +926,7 @@ void luaK_posfix (FuncState *fs, BinOpr op, · 39 unmodified lines
911926 }
912927 else {
913928 luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
914− codearith(fs, OP_CONCAT, e1, e2, line);
929+ codeexpval(fs, OP_CONCAT, e1, e2, line);
915930 }
916931 break;
917932 }
@@ -919,7 +934,7 @@ void luaK_posfix (FuncState *fs, BinOpr op, · 1 unmodified line
919934 case OPR_IDIV: case OPR_MOD: case OPR_POW:
920935 case OPR_BAND: case OPR_BOR: case OPR_BXOR:
921936 case OPR_SHL: case OPR_SHR: {
922− codearith(fs, cast(OpCode, (op - OPR_ADD) + OP_ADD), e1, e2, line);
937+ codeexpval(fs, cast(OpCode, (op - OPR_ADD) + OP_ADD), e1, e2, line);
923938 break;
924939 }
925940 case OPR_EQ: case OPR_LT: case OPR_LE: {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t