diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-07-30 19:00:50 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-07-30 19:00:50 -0300 |
commit | 0892f0e5b75c51f1fee07276a3ba13301b83409e (patch) | |
tree | 9f3b9ec92f26c05a85c826ffc5f803fda2f48867 /opcode.c | |
parent | 1d7857bc635c0bfe7c5b1f325d31feb7660e9a5a (diff) | |
download | lua-0892f0e5b75c51f1fee07276a3ba13301b83409e.tar.gz lua-0892f0e5b75c51f1fee07276a3ba13301b83409e.tar.bz2 lua-0892f0e5b75c51f1fee07276a3ba13301b83409e.zip |
BIG CHANGE: functions have their own "constant table".
Diffstat (limited to 'opcode.c')
-rw-r--r-- | opcode.c | 96 |
1 files changed, 36 insertions, 60 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_opcode="$Id: opcode.c,v 4.18 1997/07/29 13:35:06 roberto Exp roberto $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 4.19 1997/07/29 21:11:10 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <setjmp.h> | 8 | #include <setjmp.h> |
9 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -26,6 +26,9 @@ char *rcs_opcode="$Id: opcode.c,v 4.18 1997/07/29 13:35:06 roberto Exp roberto $ | |||
26 | #define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(o) != 0)) | 26 | #define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(o) != 0)) |
27 | 27 | ||
28 | 28 | ||
29 | #define get_word(w,pc) {w=*pc+(*(pc+1)<<8); pc+=2;} | ||
30 | |||
31 | |||
29 | #define STACK_SIZE 128 | 32 | #define STACK_SIZE 128 |
30 | 33 | ||
31 | #ifndef STACK_LIMIT | 34 | #ifndef STACK_LIMIT |
@@ -69,7 +72,7 @@ lua_LHFunction lua_linehook = NULL; | |||
69 | lua_CHFunction lua_callhook = NULL; | 72 | lua_CHFunction lua_callhook = NULL; |
70 | 73 | ||
71 | 74 | ||
72 | static StkId lua_execute (Byte *pc, StkId base); | 75 | static StkId lua_execute (TFunc *func, StkId base); |
73 | static void do_call (StkId base, int nResults); | 76 | static void do_call (StkId base, int nResults); |
74 | 77 | ||
75 | 78 | ||
@@ -169,7 +172,7 @@ static int lua_tostring (TObject *obj) | |||
169 | sprintf (s, "%d", i); | 172 | sprintf (s, "%d", i); |
170 | else | 173 | else |
171 | sprintf (s, "%g", nvalue(obj)); | 174 | sprintf (s, "%g", nvalue(obj)); |
172 | tsvalue(obj) = lua_createstring(s); | 175 | tsvalue(obj) = luaI_createstring(s); |
173 | ttype(obj) = LUA_T_STRING; | 176 | ttype(obj) = LUA_T_STRING; |
174 | return 0; | 177 | return 0; |
175 | } | 178 | } |
@@ -267,7 +270,8 @@ static void callHook (StkId base, lua_Type type, int isreturn) | |||
267 | { | 270 | { |
268 | TObject *f = stack+base-1; | 271 | TObject *f = stack+base-1; |
269 | if (type == LUA_T_MARK) | 272 | if (type == LUA_T_MARK) |
270 | (*lua_callhook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined); | 273 | (*lua_callhook)(Ref(f), f->value.tf->fileName->str, |
274 | f->value.tf->lineDefined); | ||
271 | else | 275 | else |
272 | (*lua_callhook)(Ref(f), "(C)", -1); | 276 | (*lua_callhook)(Ref(f), "(C)", -1); |
273 | } | 277 | } |
@@ -324,7 +328,7 @@ static void do_call (StkId base, int nResults) | |||
324 | } | 328 | } |
325 | else if (ttype(func) == LUA_T_FUNCTION) { | 329 | else if (ttype(func) == LUA_T_FUNCTION) { |
326 | ttype(func) = LUA_T_MARK; | 330 | ttype(func) = LUA_T_MARK; |
327 | firstResult = lua_execute(func->value.tf->code, base); | 331 | firstResult = lua_execute(func->value.tf, base); |
328 | } | 332 | } |
329 | else { /* func is not a function */ | 333 | else { /* func is not a function */ |
330 | /* Check the tag method for invalid functions */ | 334 | /* Check the tag method for invalid functions */ |
@@ -630,14 +634,17 @@ int luaI_dorun (TFunc *tf) | |||
630 | 634 | ||
631 | int lua_domain (void) | 635 | int lua_domain (void) |
632 | { | 636 | { |
633 | TFunc tf; | ||
634 | int status; | 637 | int status; |
638 | TFunc *tf = new(TFunc); | ||
635 | jmp_buf myErrorJmp; | 639 | jmp_buf myErrorJmp; |
636 | jmp_buf *oldErr = errorJmp; | 640 | jmp_buf *oldErr = errorJmp; |
637 | errorJmp = &myErrorJmp; | 641 | errorJmp = &myErrorJmp; |
638 | luaI_initTFunc(&tf); | 642 | luaI_initTFunc(tf); |
643 | adjustC(1); /* one slot for the pseudo-function */ | ||
644 | stack[CLS_current.base].ttype = LUA_T_FUNCTION; | ||
645 | stack[CLS_current.base].value.tf = tf; | ||
639 | if (setjmp(myErrorJmp) == 0) { | 646 | if (setjmp(myErrorJmp) == 0) { |
640 | lua_parse(&tf); | 647 | lua_parse(tf); |
641 | status = 0; | 648 | status = 0; |
642 | } | 649 | } |
643 | else { | 650 | else { |
@@ -645,9 +652,8 @@ int lua_domain (void) | |||
645 | status = 1; | 652 | status = 1; |
646 | } | 653 | } |
647 | if (status == 0) | 654 | if (status == 0) |
648 | status = luaI_dorun(&tf); | 655 | status = do_protectedrun(MULT_RET); |
649 | errorJmp = oldErr; | 656 | errorJmp = oldErr; |
650 | luaI_free(tf.code); | ||
651 | return status; | 657 | return status; |
652 | } | 658 | } |
653 | 659 | ||
@@ -952,7 +958,7 @@ void lua_pushstring (char *s) | |||
952 | ttype(top) = LUA_T_NIL; | 958 | ttype(top) = LUA_T_NIL; |
953 | else | 959 | else |
954 | { | 960 | { |
955 | tsvalue(top) = lua_createstring(s); | 961 | tsvalue(top) = luaI_createstring(s); |
956 | ttype(top) = LUA_T_STRING; | 962 | ttype(top) = LUA_T_STRING; |
957 | } | 963 | } |
958 | incr_top; | 964 | incr_top; |
@@ -1088,7 +1094,7 @@ static void adjust_varargs (StkId first_extra_arg) | |||
1088 | /* store counter in field "n" */ { | 1094 | /* store counter in field "n" */ { |
1089 | TObject index, extra; | 1095 | TObject index, extra; |
1090 | ttype(&index) = LUA_T_STRING; | 1096 | ttype(&index) = LUA_T_STRING; |
1091 | tsvalue(&index) = lua_createstring("n"); | 1097 | tsvalue(&index) = luaI_createstring("n"); |
1092 | ttype(&extra) = LUA_T_NUMBER; | 1098 | ttype(&extra) = LUA_T_NUMBER; |
1093 | nvalue(&extra) = nvararg; | 1099 | nvalue(&extra) = nvararg; |
1094 | *(lua_hashdefine(avalue(&arg), &index)) = extra; | 1100 | *(lua_hashdefine(avalue(&arg), &index)) = extra; |
@@ -1104,8 +1110,9 @@ static void adjust_varargs (StkId first_extra_arg) | |||
1104 | ** [stack+base,top). Returns n such that the the results are between | 1110 | ** [stack+base,top). Returns n such that the the results are between |
1105 | ** [stack+n,top). | 1111 | ** [stack+n,top). |
1106 | */ | 1112 | */ |
1107 | static StkId lua_execute (Byte *pc, StkId base) | 1113 | static StkId lua_execute (TFunc *func, StkId base) |
1108 | { | 1114 | { |
1115 | Byte *pc = func->code; | ||
1109 | if (lua_callhook) | 1116 | if (lua_callhook) |
1110 | callHook (base, LUA_T_MARK, 0); | 1117 | callHook (base, LUA_T_MARK, 0); |
1111 | while (1) | 1118 | while (1) |
@@ -1133,35 +1140,6 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
1133 | } | 1140 | } |
1134 | break; | 1141 | break; |
1135 | 1142 | ||
1136 | case PUSHFLOAT: | ||
1137 | { | ||
1138 | real num; | ||
1139 | get_float(num,pc); | ||
1140 | ttype(top) = LUA_T_NUMBER; nvalue(top) = num; | ||
1141 | incr_top; | ||
1142 | } | ||
1143 | break; | ||
1144 | |||
1145 | case PUSHSTRING: | ||
1146 | { | ||
1147 | Word w; | ||
1148 | get_word(w,pc); | ||
1149 | ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w]; | ||
1150 | incr_top; | ||
1151 | } | ||
1152 | break; | ||
1153 | |||
1154 | case PUSHFUNCTION: | ||
1155 | { | ||
1156 | TFunc *f; | ||
1157 | get_code(f,pc); | ||
1158 | luaI_insertfunction(f); /* may take part in GC */ | ||
1159 | top->ttype = LUA_T_FUNCTION; | ||
1160 | top->value.tf = f; | ||
1161 | incr_top; | ||
1162 | } | ||
1163 | break; | ||
1164 | |||
1165 | case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: | 1143 | case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: |
1166 | case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5: | 1144 | case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5: |
1167 | case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8: | 1145 | case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8: |
@@ -1187,7 +1165,7 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
1187 | TObject receiver = *(top-1); | 1165 | TObject receiver = *(top-1); |
1188 | Word w; | 1166 | Word w; |
1189 | get_word(w,pc); | 1167 | get_word(w,pc); |
1190 | ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w]; | 1168 | *top = func->consts[w]; |
1191 | incr_top; | 1169 | incr_top; |
1192 | pushsubscript(); | 1170 | pushsubscript(); |
1193 | *top = receiver; | 1171 | *top = receiver; |
@@ -1195,6 +1173,20 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
1195 | break; | 1173 | break; |
1196 | } | 1174 | } |
1197 | 1175 | ||
1176 | case PUSHCONSTANTB: { | ||
1177 | *top = func->consts[*pc++]; | ||
1178 | incr_top; | ||
1179 | break; | ||
1180 | } | ||
1181 | |||
1182 | case PUSHCONSTANT: { | ||
1183 | Word w; | ||
1184 | get_word(w,pc); | ||
1185 | *top = func->consts[w]; | ||
1186 | incr_top; | ||
1187 | break; | ||
1188 | } | ||
1189 | |||
1198 | case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: | 1190 | case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: |
1199 | case STORELOCAL3: case STORELOCAL4: case STORELOCAL5: | 1191 | case STORELOCAL3: case STORELOCAL4: case STORELOCAL5: |
1200 | case STORELOCAL6: case STORELOCAL7: case STORELOCAL8: | 1192 | case STORELOCAL6: case STORELOCAL7: case STORELOCAL8: |
@@ -1241,22 +1233,6 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
1241 | } | 1233 | } |
1242 | break; | 1234 | break; |
1243 | 1235 | ||
1244 | case STORERECORD: /* opcode obsolete: supersed by STOREMAP */ | ||
1245 | { | ||
1246 | int n = *(pc++); | ||
1247 | TObject *arr = top-n-1; | ||
1248 | while (n) | ||
1249 | { | ||
1250 | Word w; | ||
1251 | get_word(w,pc); | ||
1252 | ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w]; | ||
1253 | *(lua_hashdefine (avalue(arr), top)) = *(top-1); | ||
1254 | top--; | ||
1255 | n--; | ||
1256 | } | ||
1257 | } | ||
1258 | break; | ||
1259 | |||
1260 | case STOREMAP: { | 1236 | case STOREMAP: { |
1261 | int n = *(pc++); | 1237 | int n = *(pc++); |
1262 | TObject *arr = top-(2*n)-1; | 1238 | TObject *arr = top-(2*n)-1; |
@@ -1382,7 +1358,7 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
1382 | if (tostring(l) || tostring(r)) | 1358 | if (tostring(l) || tostring(r)) |
1383 | call_binTM(IM_CONCAT, "unexpected type for concatenation"); | 1359 | call_binTM(IM_CONCAT, "unexpected type for concatenation"); |
1384 | else { | 1360 | else { |
1385 | tsvalue(l) = lua_createstring(lua_strconc(svalue(l),svalue(r))); | 1361 | tsvalue(l) = luaI_createstring(lua_strconc(svalue(l),svalue(r))); |
1386 | --top; | 1362 | --top; |
1387 | } | 1363 | } |
1388 | } | 1364 | } |