aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-06-21 15:13:56 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-06-21 15:13:56 -0300
commitb69e712713785394ceefa11ab3e5f9636abea733 (patch)
tree0fa1b8b00ffbb5cfe0113d290b48b4fc528d2695
parentf51775950737eb4d097cda0bba143973b5385947 (diff)
downloadlua-b69e712713785394ceefa11ab3e5f9636abea733.tar.gz
lua-b69e712713785394ceefa11ab3e5f9636abea733.tar.bz2
lua-b69e712713785394ceefa11ab3e5f9636abea733.zip
new way to generate SETLINEs
-rw-r--r--lcode.c19
-rw-r--r--llex.c3
-rw-r--r--llex.h3
-rw-r--r--lparser.c70
-rw-r--r--lparser.h3
5 files changed, 41 insertions, 57 deletions
diff --git a/lcode.c b/lcode.c
index 6e43f499..50ed11e6 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 1.36 2000/06/16 17:51:40 roberto Exp roberto $ 2** $Id: lcode.c,v 1.37 2000/06/21 17:05:49 roberto Exp roberto $
3** Code generator for Lua 3** Code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -307,9 +307,8 @@ static void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
307 307
308 308
309static int code_label (FuncState *fs, OpCode op, int arg) { 309static int code_label (FuncState *fs, OpCode op, int arg) {
310 int j = luaK_getlabel(fs); 310 luaK_getlabel(fs); /* those instructions may be jump targets */
311 luaK_code1(fs, op, arg); 311 return luaK_code1(fs, op, arg);
312 return j;
313} 312}
314 313
315 314
@@ -624,9 +623,17 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
624 case iS: i = CREATE_S(o, arg1); break; 623 case iS: i = CREATE_S(o, arg1); break;
625 case iAB: i = CREATE_AB(o, arg1, arg2); break; 624 case iAB: i = CREATE_AB(o, arg1, arg2); break;
626 } 625 }
627 /* put new instruction in code array */ 626 /* check space for new instruction plus eventual SETLINE */
628 luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction, 627 luaM_growvector(fs->L, fs->f->code, fs->pc, 2, Instruction,
629 "code size overflow", MAX_INT); 628 "code size overflow", MAX_INT);
629 /* check the need for SETLINE */
630 if (fs->debug && fs->ls->lastline != fs->lastsetline) {
631 LexState *ls = fs->ls;
632 luaX_checklimit(ls, ls->lastline, MAXARG_U, "lines in a chunk");
633 fs->f->code[fs->pc++] = CREATE_U(OP_SETLINE, ls->lastline);
634 fs->lastsetline = ls->lastline;
635 }
636 /* put new instruction in code array */
630 fs->f->code[fs->pc] = i; 637 fs->f->code[fs->pc] = i;
631 return fs->pc++; 638 return fs->pc++;
632} 639}
diff --git a/llex.c b/llex.c
index f251ab9c..50ffc951 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 1.63 2000/06/12 13:52:05 roberto Exp roberto $ 2** $Id: llex.c,v 1.64 2000/06/19 18:05:14 roberto Exp roberto $
3** Lexical Analyzer 3** Lexical Analyzer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -138,6 +138,7 @@ void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
138 LS->z = z; 138 LS->z = z;
139 LS->fs = NULL; 139 LS->fs = NULL;
140 LS->linenumber = 1; 140 LS->linenumber = 1;
141 LS->lastline = 1;
141 LS->source = source; 142 LS->source = source;
142 next(LS); /* read first char */ 143 next(LS); /* read first char */
143 if (LS->current == '#') { 144 if (LS->current == '#') {
diff --git a/llex.h b/llex.h
index 8528dc20..604b8bf1 100644
--- a/llex.h
+++ b/llex.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.h,v 1.28 2000/05/26 14:04:04 roberto Exp roberto $ 2** $Id: llex.h,v 1.29 2000/06/19 18:05:14 roberto Exp roberto $
3** Lexical Analyzer 3** Lexical Analyzer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -51,6 +51,7 @@ typedef struct LexState {
51 struct lua_State *L; 51 struct lua_State *L;
52 struct zio *z; /* input stream */ 52 struct zio *z; /* input stream */
53 int linenumber; /* input line counter */ 53 int linenumber; /* input line counter */
54 int lastline; /* line of last token `consumed' */
54 TString *source; /* current source name */ 55 TString *source; /* current source name */
55} LexState; 56} LexState;
56 57
diff --git a/lparser.c b/lparser.c
index 7b46c0ea..e920af3e 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 1.96 2000/06/19 18:05:14 roberto Exp roberto $ 2** $Id: lparser.c,v 1.97 2000/06/19 18:26:23 roberto Exp roberto $
3** LL(1) Parser and code generator for Lua 3** LL(1) Parser and code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -56,6 +56,7 @@ static void exp1 (LexState *ls);
56 56
57 57
58static void next (LexState *ls) { 58static void next (LexState *ls) {
59 ls->lastline = ls->linenumber;
59 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ 60 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
60 ls->t = ls->lookahead; /* use this one */ 61 ls->t = ls->lookahead; /* use this one */
61 ls->lookahead.token = TK_EOS; /* and discharge it */ 62 ls->lookahead.token = TK_EOS; /* and discharge it */
@@ -91,16 +92,6 @@ static void check_condition (LexState *ls, int c, const char *msg) {
91} 92}
92 93
93 94
94static void setline (LexState *ls) {
95 FuncState *fs = ls->fs;
96 if (ls->L->debug && ls->linenumber != fs->lastsetline) {
97 luaX_checklimit(ls, ls->linenumber, MAXARG_U, "lines in a chunk");
98 luaK_code1(fs, OP_SETLINE, ls->linenumber);
99 fs->lastsetline = ls->linenumber;
100 }
101}
102
103
104static int optional (LexState *ls, int c) { 95static int optional (LexState *ls, int c) {
105 if (ls->t.token == c) { 96 if (ls->t.token == c) {
106 next(ls); 97 next(ls);
@@ -128,18 +119,6 @@ static void check_match (LexState *ls, int what, int who, int where) {
128} 119}
129 120
130 121
131static void setline_and_next (LexState *ls) {
132 setline(ls);
133 next(ls);
134}
135
136
137static void check_END (LexState *ls, int who, int where) {
138 setline(ls); /* setline for END */
139 check_match(ls, TK_END, who, where);
140}
141
142
143static int string_constant (FuncState *fs, TString *s) { 122static int string_constant (FuncState *fs, TString *s) {
144 Proto *f = fs->f; 123 Proto *f = fs->f;
145 int c = s->u.s.constindex; 124 int c = s->u.s.constindex;
@@ -175,9 +154,7 @@ static int checkname (LexState *ls) {
175 154
176static void luaI_registerlocalvar (LexState *ls, TString *varname, int line) { 155static void luaI_registerlocalvar (LexState *ls, TString *varname, int line) {
177 FuncState *fs = ls->fs; 156 FuncState *fs = ls->fs;
178 /* start debug only when there are no active local variables, 157 if (fs->debug) {
179 but keep going after starting */
180 if ((ls->L->debug && fs->nlocalvar == 0) || fs->nvars != 0) {
181 Proto *f = fs->f; 158 Proto *f = fs->f;
182 luaM_growvector(ls->L, f->locvars, fs->nvars, 1, LocVar, "", MAX_INT); 159 luaM_growvector(ls->L, f->locvars, fs->nvars, 1, LocVar, "", MAX_INT);
183 f->locvars[fs->nvars].varname = varname; 160 f->locvars[fs->nvars].varname = varname;
@@ -368,10 +345,8 @@ static void close_func (LexState *ls) {
368 luaM_reallocvector(L, f->kstr, f->nkstr, TString *); 345 luaM_reallocvector(L, f->kstr, f->nkstr, TString *);
369 luaM_reallocvector(L, f->knum, f->nknum, Number); 346 luaM_reallocvector(L, f->knum, f->nknum, Number);
370 luaM_reallocvector(L, f->kproto, f->nkproto, Proto *); 347 luaM_reallocvector(L, f->kproto, f->nkproto, Proto *);
371 if (f->locvars) { /* debug information? */ 348 luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */
372 luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */ 349 luaM_reallocvector(L, f->locvars, fs->nvars, LocVar);
373 luaM_reallocvector(L, f->locvars, fs->nvars, LocVar);
374 }
375 ls->fs = fs->prev; 350 ls->fs = fs->prev;
376 LUA_ASSERT(L, fs->bl == NULL, "wrong list end"); 351 LUA_ASSERT(L, fs->bl == NULL, "wrong list end");
377} 352}
@@ -383,6 +358,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z) {
383 luaX_setinput(L, &lexstate, z, luaS_new(L, zname(z))); 358 luaX_setinput(L, &lexstate, z, luaS_new(L, zname(z)));
384 open_func(&lexstate, &funcstate); 359 open_func(&lexstate, &funcstate);
385 next(&lexstate); /* read first token */ 360 next(&lexstate); /* read first token */
361 funcstate.debug = L->debug; /* previous `next' may scan a pragma */
386 chunk(&lexstate); 362 chunk(&lexstate);
387 check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected"); 363 check_condition(&lexstate, (lexstate.t.token == TK_EOS), "<eof> expected");
388 close_func(&lexstate); 364 close_func(&lexstate);
@@ -639,7 +615,6 @@ static void constructor (LexState *ls) {
639 615
640static void simpleexp (LexState *ls, expdesc *v) { 616static void simpleexp (LexState *ls, expdesc *v) {
641 FuncState *fs = ls->fs; 617 FuncState *fs = ls->fs;
642 setline(ls);
643 switch (ls->t.token) { 618 switch (ls->t.token) {
644 case TK_NUMBER: { /* simpleexp -> NUMBER */ 619 case TK_NUMBER: { /* simpleexp -> NUMBER */
645 Number r = ls->t.seminfo.r; 620 Number r = ls->t.seminfo.r;
@@ -819,13 +794,13 @@ static void whilestat (LexState *ls, int line) {
819 expdesc v; 794 expdesc v;
820 Breaklabel bl; 795 Breaklabel bl;
821 enterbreak(fs, &bl); 796 enterbreak(fs, &bl);
822 setline_and_next(ls); /* trace WHILE when looping */ 797 next(ls);
823 cond(ls, &v); 798 cond(ls, &v);
824 check(ls, TK_DO); 799 check(ls, TK_DO);
825 block(ls); 800 block(ls);
826 luaK_patchlist(fs, luaK_jump(fs), while_init); 801 luaK_patchlist(fs, luaK_jump(fs), while_init);
827 luaK_patchlist(fs, v.u.l.f, luaK_getlabel(fs)); 802 luaK_patchlist(fs, v.u.l.f, luaK_getlabel(fs));
828 check_END(ls, TK_WHILE, line); /* trace END when loop ends */ 803 check_match(ls, TK_END, TK_WHILE, line);
829 leavebreak(fs, &bl); 804 leavebreak(fs, &bl);
830} 805}
831 806
@@ -837,7 +812,7 @@ static void repeatstat (LexState *ls, int line) {
837 expdesc v; 812 expdesc v;
838 Breaklabel bl; 813 Breaklabel bl;
839 enterbreak(fs, &bl); 814 enterbreak(fs, &bl);
840 setline_and_next(ls); /* trace REPEAT when looping */ 815 next(ls);
841 block(ls); 816 block(ls);
842 check_match(ls, TK_UNTIL, TK_REPEAT, line); 817 check_match(ls, TK_UNTIL, TK_REPEAT, line);
843 cond(ls, &v); 818 cond(ls, &v);
@@ -905,23 +880,22 @@ static void forstat (LexState *ls, int line) {
905 TString *varname; 880 TString *varname;
906 Breaklabel bl; 881 Breaklabel bl;
907 enterbreak(fs, &bl); 882 enterbreak(fs, &bl);
908 setline_and_next(ls); /* skip `for' */ 883 next(ls); /* skip `for' */
909 varname = str_checkname(ls); /* first variable name */ 884 varname = str_checkname(ls); /* first variable name */
910 switch (ls->t.token) { 885 switch (ls->t.token) {
911 case '=': fornum(ls, varname); break; 886 case '=': fornum(ls, varname); break;
912 case ',': forlist(ls, varname); break; 887 case ',': forlist(ls, varname); break;
913 default: luaK_error(ls, "`=' or `,' expected"); 888 default: luaK_error(ls, "`=' or `,' expected");
914 } 889 }
915 check_END(ls, TK_FOR, line); 890 check_match(ls, TK_END, TK_FOR, line);
916 leavebreak(fs, &bl); 891 leavebreak(fs, &bl);
917} 892}
918 893
919 894
920static void test_then_block (LexState *ls, expdesc *v) { 895static void test_then_block (LexState *ls, expdesc *v) {
921 /* test_then_block -> [IF | ELSEIF] cond THEN block */ 896 /* test_then_block -> [IF | ELSEIF] cond THEN block */
922 setline_and_next(ls); /* skip IF or ELSEIF */ 897 next(ls); /* skip IF or ELSEIF */
923 cond(ls, v); 898 cond(ls, v);
924 setline(ls); /* to trace the THEN */
925 check(ls, TK_THEN); 899 check(ls, TK_THEN);
926 block(ls); /* `then' part */ 900 block(ls); /* `then' part */
927} 901}
@@ -941,13 +915,13 @@ static void ifstat (LexState *ls, int line) {
941 if (ls->t.token == TK_ELSE) { 915 if (ls->t.token == TK_ELSE) {
942 luaK_concat(fs, &escapelist, luaK_jump(fs)); 916 luaK_concat(fs, &escapelist, luaK_jump(fs));
943 luaK_patchlist(fs, v.u.l.f, luaK_getlabel(fs)); 917 luaK_patchlist(fs, v.u.l.f, luaK_getlabel(fs));
944 setline_and_next(ls); /* skip ELSE */ 918 next(ls); /* skip ELSE */
945 block(ls); /* `else' part */ 919 block(ls); /* `else' part */
946 } 920 }
947 else 921 else
948 luaK_concat(fs, &escapelist, v.u.l.f); 922 luaK_concat(fs, &escapelist, v.u.l.f);
949 luaK_patchlist(fs, escapelist, luaK_getlabel(fs)); 923 luaK_patchlist(fs, escapelist, luaK_getlabel(fs));
950 check_END(ls, TK_IF, line); 924 check_match(ls, TK_END, TK_IF, line);
951} 925}
952 926
953 927
@@ -956,7 +930,7 @@ static void localstat (LexState *ls) {
956 int nvars = 0; 930 int nvars = 0;
957 int nexps; 931 int nexps;
958 do { 932 do {
959 setline_and_next(ls); /* skip LOCAL or ',' */ 933 next(ls); /* skip LOCAL or ',' */
960 store_localvar(ls, str_checkname(ls), nvars++); 934 store_localvar(ls, str_checkname(ls), nvars++);
961 } while (ls->t.token == ','); 935 } while (ls->t.token == ',');
962 if (optional(ls, '=')) 936 if (optional(ls, '='))
@@ -989,7 +963,7 @@ static void funcstat (LexState *ls, int line) {
989 expdesc v; 963 expdesc v;
990 check_condition(ls, (ls->fs->prev == NULL), 964 check_condition(ls, (ls->fs->prev == NULL),
991 "cannot nest this kind of function declaration"); 965 "cannot nest this kind of function declaration");
992 setline_and_next(ls); /* skip FUNCTION */ 966 next(ls); /* skip FUNCTION */
993 needself = funcname(ls, &v); 967 needself = funcname(ls, &v);
994 body(ls, needself, line); 968 body(ls, needself, line);
995 luaK_storevar(ls, &v); 969 luaK_storevar(ls, &v);
@@ -1000,7 +974,6 @@ static void namestat (LexState *ls) {
1000 /* stat -> func | ['%'] NAME assignment */ 974 /* stat -> func | ['%'] NAME assignment */
1001 FuncState *fs = ls->fs; 975 FuncState *fs = ls->fs;
1002 expdesc v; 976 expdesc v;
1003 setline(ls);
1004 var_or_func(ls, &v); 977 var_or_func(ls, &v);
1005 if (v.k == VEXP) { /* stat -> func */ 978 if (v.k == VEXP) { /* stat -> func */
1006 check_condition(ls, luaK_lastisopen(fs), "syntax error"); /* an upvalue? */ 979 check_condition(ls, luaK_lastisopen(fs), "syntax error"); /* an upvalue? */
@@ -1016,7 +989,7 @@ static void namestat (LexState *ls) {
1016static void retstat (LexState *ls) { 989static void retstat (LexState *ls) {
1017 /* stat -> RETURN explist */ 990 /* stat -> RETURN explist */
1018 FuncState *fs = ls->fs; 991 FuncState *fs = ls->fs;
1019 setline_and_next(ls); /* skip RETURN */ 992 next(ls); /* skip RETURN */
1020 if (!block_follow(ls->t.token)) 993 if (!block_follow(ls->t.token))
1021 explist1(ls); /* optional return values */ 994 explist1(ls); /* optional return values */
1022 luaK_code1(fs, OP_RETURN, ls->fs->nlocalvar); 995 luaK_code1(fs, OP_RETURN, ls->fs->nlocalvar);
@@ -1031,7 +1004,7 @@ static void breakstat (LexState *ls) {
1031 Breaklabel *bl = fs->bl; 1004 Breaklabel *bl = fs->bl;
1032 if (!bl) 1005 if (!bl)
1033 luaK_error(ls, "no loop to break"); 1006 luaK_error(ls, "no loop to break");
1034 setline_and_next(ls); /* skip BREAK */ 1007 next(ls); /* skip BREAK */
1035 luaK_adjuststack(fs, currentlevel - bl->stacklevel); 1008 luaK_adjuststack(fs, currentlevel - bl->stacklevel);
1036 luaK_concat(fs, &bl->breaklist, luaK_jump(fs)); 1009 luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
1037 fs->stacklevel = currentlevel; 1010 fs->stacklevel = currentlevel;
@@ -1050,9 +1023,9 @@ static int stat (LexState *ls) {
1050 return 0; 1023 return 0;
1051 } 1024 }
1052 case TK_DO: { /* stat -> DO block END */ 1025 case TK_DO: { /* stat -> DO block END */
1053 setline_and_next(ls); /* skip DO */ 1026 next(ls); /* skip DO */
1054 block(ls); 1027 block(ls);
1055 check_END(ls, TK_DO, line); 1028 check_match(ls, TK_END, TK_DO, line);
1056 return 0; 1029 return 0;
1057 } 1030 }
1058 case TK_FOR: { /* stat -> forstat */ 1031 case TK_FOR: { /* stat -> forstat */
@@ -1113,13 +1086,14 @@ static void body (LexState *ls, int needself, int line) {
1113 FuncState new_fs; 1086 FuncState new_fs;
1114 open_func(ls, &new_fs); 1087 open_func(ls, &new_fs);
1115 new_fs.f->lineDefined = line; 1088 new_fs.f->lineDefined = line;
1089 new_fs.debug = ls->L->debug;
1116 check(ls, '('); 1090 check(ls, '(');
1117 if (needself) 1091 if (needself)
1118 add_localvar(ls, "self"); 1092 add_localvar(ls, "self");
1119 parlist(ls); 1093 parlist(ls);
1120 check(ls, ')'); 1094 check(ls, ')');
1121 chunk(ls); 1095 chunk(ls);
1122 check_END(ls, TK_FUNCTION, line); 1096 check_match(ls, TK_END, TK_FUNCTION, line);
1123 close_func(ls); 1097 close_func(ls);
1124 pushclosure(ls, &new_fs); 1098 pushclosure(ls, &new_fs);
1125} 1099}
diff --git a/lparser.h b/lparser.h
index 42f11d42..2fb8e000 100644
--- a/lparser.h
+++ b/lparser.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.h,v 1.16 2000/04/06 17:36:52 roberto Exp roberto $ 2** $Id: lparser.h,v 1.17 2000/05/25 18:26:42 roberto Exp roberto $
3** LL(1) Parser and code generator for Lua 3** LL(1) Parser and code generator for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -49,6 +49,7 @@ typedef struct FuncState {
49 int nupvalues; /* number of upvalues */ 49 int nupvalues; /* number of upvalues */
50 int nvars; /* number of entries in f->locvars */ 50 int nvars; /* number of entries in f->locvars */
51 int lastsetline; /* line where last SETLINE was issued */ 51 int lastsetline; /* line where last SETLINE was issued */
52 int debug; /* flag to generate debug information */
52 struct Breaklabel *bl; /* chain of breakable blocks */ 53 struct Breaklabel *bl; /* chain of breakable blocks */
53 expdesc upvalues[MAXUPVALUES]; /* upvalues */ 54 expdesc upvalues[MAXUPVALUES]; /* upvalues */
54 TString *localvar[MAXLOCALS]; /* store local variable names */ 55 TString *localvar[MAXLOCALS]; /* store local variable names */