diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-05-28 18:07:32 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-05-28 18:07:32 -0300 |
commit | 9863223fbf512d903a1677c861e4beb4f8feda4d (patch) | |
tree | 964f1b36a5a7c9aff238d454fa4bffe88dacbe2d /opcode.c | |
parent | 9a1948e67d940d988260e738f2a251087835a318 (diff) | |
download | lua-9863223fbf512d903a1677c861e4beb4f8feda4d.tar.gz lua-9863223fbf512d903a1677c861e4beb4f8feda4d.tar.bz2 lua-9863223fbf512d903a1677c861e4beb4f8feda4d.zip |
first version of vararg facility (plus new function "call").
Diffstat (limited to 'opcode.c')
-rw-r--r-- | opcode.c | 140 |
1 files changed, 89 insertions, 51 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 3.67 1996/04/22 18:00:37 roberto Exp roberto $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 3.68 1996/04/25 14:10:00 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <setjmp.h> | 8 | #include <setjmp.h> |
9 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -117,7 +117,7 @@ static void growstack (void) | |||
117 | */ | 117 | */ |
118 | static char *lua_strconc (char *l, char *r) | 118 | static char *lua_strconc (char *l, char *r) |
119 | { | 119 | { |
120 | int nl = strlen(l); | 120 | size_t nl = strlen(l); |
121 | char *buffer = luaI_buffer(nl+strlen(r)+1); | 121 | char *buffer = luaI_buffer(nl+strlen(r)+1); |
122 | strcpy(buffer, l); | 122 | strcpy(buffer, l); |
123 | strcpy(buffer+nl, r); | 123 | strcpy(buffer+nl, r); |
@@ -886,6 +886,40 @@ static void comparison (lua_Type tag_less, lua_Type tag_equal, | |||
886 | } | 886 | } |
887 | 887 | ||
888 | 888 | ||
889 | void luaI_packarg (Object *firstelem, Object *arg) | ||
890 | { | ||
891 | int nvararg = (firstelem != NULL) ? top-firstelem : 0; | ||
892 | int i; | ||
893 | if (nvararg < 0) nvararg = 0; | ||
894 | avalue(arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */ | ||
895 | tag(arg) = LUA_T_ARRAY; | ||
896 | for (i=0; i<nvararg; i++) | ||
897 | { | ||
898 | Object index; | ||
899 | tag(&index) = LUA_T_NUMBER; | ||
900 | nvalue(&index) = i+1; | ||
901 | *(lua_hashdefine(avalue(arg), &index)) = *(firstelem+i); | ||
902 | } | ||
903 | /* store counter in field "n" */ | ||
904 | { | ||
905 | Object index, extra; | ||
906 | tag(&index) = LUA_T_STRING; | ||
907 | tsvalue(&index) = lua_createstring("n"); | ||
908 | tag(&extra) = LUA_T_NUMBER; | ||
909 | nvalue(&extra) = nvararg; | ||
910 | *(lua_hashdefine(avalue(arg), &index)) = extra; | ||
911 | } | ||
912 | } | ||
913 | |||
914 | |||
915 | static void adjust_varargs (StkId first_extra_arg) | ||
916 | { | ||
917 | Object arg; | ||
918 | luaI_packarg(stack+first_extra_arg, &arg); | ||
919 | adjust_top(first_extra_arg); | ||
920 | *top = arg; incr_top; | ||
921 | } | ||
922 | |||
889 | 923 | ||
890 | /* | 924 | /* |
891 | ** Execute the given opcode, until a RET. Parameters are between | 925 | ** Execute the given opcode, until a RET. Parameters are between |
@@ -914,38 +948,38 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
914 | 948 | ||
915 | case PUSHWORD: | 949 | case PUSHWORD: |
916 | { | 950 | { |
917 | CodeWord code; | 951 | Word w; |
918 | get_word(code,pc); | 952 | get_word(w,pc); |
919 | tag(top) = LUA_T_NUMBER; nvalue(top) = code.w; | 953 | tag(top) = LUA_T_NUMBER; nvalue(top) = w; |
920 | incr_top; | 954 | incr_top; |
921 | } | 955 | } |
922 | break; | 956 | break; |
923 | 957 | ||
924 | case PUSHFLOAT: | 958 | case PUSHFLOAT: |
925 | { | 959 | { |
926 | CodeFloat code; | 960 | real num; |
927 | get_float(code,pc); | 961 | get_float(num,pc); |
928 | tag(top) = LUA_T_NUMBER; nvalue(top) = code.f; | 962 | tag(top) = LUA_T_NUMBER; nvalue(top) = num; |
929 | incr_top; | 963 | incr_top; |
930 | } | 964 | } |
931 | break; | 965 | break; |
932 | 966 | ||
933 | case PUSHSTRING: | 967 | case PUSHSTRING: |
934 | { | 968 | { |
935 | CodeWord code; | 969 | Word w; |
936 | get_word(code,pc); | 970 | get_word(w,pc); |
937 | tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w]; | 971 | tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w]; |
938 | incr_top; | 972 | incr_top; |
939 | } | 973 | } |
940 | break; | 974 | break; |
941 | 975 | ||
942 | case PUSHFUNCTION: | 976 | case PUSHFUNCTION: |
943 | { | 977 | { |
944 | CodeCode code; | 978 | TFunc *f; |
945 | get_code(code,pc); | 979 | get_code(f,pc); |
946 | luaI_insertfunction(code.tf); /* may take part in GC */ | 980 | luaI_insertfunction(f); /* may take part in GC */ |
947 | top->tag = LUA_T_FUNCTION; | 981 | top->tag = LUA_T_FUNCTION; |
948 | top->value.tf = code.tf; | 982 | top->value.tf = f; |
949 | incr_top; | 983 | incr_top; |
950 | } | 984 | } |
951 | break; | 985 | break; |
@@ -960,9 +994,9 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
960 | 994 | ||
961 | case PUSHGLOBAL: | 995 | case PUSHGLOBAL: |
962 | { | 996 | { |
963 | CodeWord code; | 997 | Word w; |
964 | get_word(code,pc); | 998 | get_word(w,pc); |
965 | getglobal(code.w); | 999 | getglobal(w); |
966 | } | 1000 | } |
967 | break; | 1001 | break; |
968 | 1002 | ||
@@ -973,9 +1007,9 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
973 | case PUSHSELF: | 1007 | case PUSHSELF: |
974 | { | 1008 | { |
975 | Object receiver = *(top-1); | 1009 | Object receiver = *(top-1); |
976 | CodeWord code; | 1010 | Word w; |
977 | get_word(code,pc); | 1011 | get_word(w,pc); |
978 | tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w]; | 1012 | tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w]; |
979 | incr_top; | 1013 | incr_top; |
980 | pushsubscript(); | 1014 | pushsubscript(); |
981 | *top = receiver; | 1015 | *top = receiver; |
@@ -994,9 +1028,9 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
994 | 1028 | ||
995 | case STOREGLOBAL: | 1029 | case STOREGLOBAL: |
996 | { | 1030 | { |
997 | CodeWord code; | 1031 | Word w; |
998 | get_word(code,pc); | 1032 | get_word(w,pc); |
999 | s_object(code.w) = *(--top); | 1033 | s_object(w) = *(--top); |
1000 | } | 1034 | } |
1001 | break; | 1035 | break; |
1002 | 1036 | ||
@@ -1050,9 +1084,9 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
1050 | Object *arr = top-n-1; | 1084 | Object *arr = top-n-1; |
1051 | while (n) | 1085 | while (n) |
1052 | { | 1086 | { |
1053 | CodeWord code; | 1087 | Word w; |
1054 | get_word(code,pc); | 1088 | get_word(w,pc); |
1055 | tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w]; | 1089 | tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w]; |
1056 | *(lua_hashdefine (avalue(arr), top)) = *(top-1); | 1090 | *(lua_hashdefine (avalue(arr), top)) = *(top-1); |
1057 | top--; | 1091 | top--; |
1058 | n--; | 1092 | n--; |
@@ -1068,11 +1102,15 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
1068 | adjust_top(base + *(pc++)); | 1102 | adjust_top(base + *(pc++)); |
1069 | break; | 1103 | break; |
1070 | 1104 | ||
1105 | case VARARGS: | ||
1106 | adjust_varargs(base + *(pc++)); | ||
1107 | break; | ||
1108 | |||
1071 | case CREATEARRAY: | 1109 | case CREATEARRAY: |
1072 | { | 1110 | { |
1073 | CodeWord size; | 1111 | Word size; |
1074 | get_word(size,pc); | 1112 | get_word(size,pc); |
1075 | avalue(top) = lua_createarray(size.w); | 1113 | avalue(top) = lua_createarray(size); |
1076 | tag(top) = LUA_T_ARRAY; | 1114 | tag(top) = LUA_T_ARRAY; |
1077 | incr_top; | 1115 | incr_top; |
1078 | } | 1116 | } |
@@ -1195,51 +1233,51 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
1195 | 1233 | ||
1196 | case ONTJMP: | 1234 | case ONTJMP: |
1197 | { | 1235 | { |
1198 | CodeWord code; | 1236 | Word w; |
1199 | get_word(code,pc); | 1237 | get_word(w,pc); |
1200 | if (tag(top-1) != LUA_T_NIL) pc += code.w; | 1238 | if (tag(top-1) != LUA_T_NIL) pc += w; |
1201 | } | 1239 | } |
1202 | break; | 1240 | break; |
1203 | 1241 | ||
1204 | case ONFJMP: | 1242 | case ONFJMP: |
1205 | { | 1243 | { |
1206 | CodeWord code; | 1244 | Word w; |
1207 | get_word(code,pc); | 1245 | get_word(w,pc); |
1208 | if (tag(top-1) == LUA_T_NIL) pc += code.w; | 1246 | if (tag(top-1) == LUA_T_NIL) pc += w; |
1209 | } | 1247 | } |
1210 | break; | 1248 | break; |
1211 | 1249 | ||
1212 | case JMP: | 1250 | case JMP: |
1213 | { | 1251 | { |
1214 | CodeWord code; | 1252 | Word w; |
1215 | get_word(code,pc); | 1253 | get_word(w,pc); |
1216 | pc += code.w; | 1254 | pc += w; |
1217 | } | 1255 | } |
1218 | break; | 1256 | break; |
1219 | 1257 | ||
1220 | case UPJMP: | 1258 | case UPJMP: |
1221 | { | 1259 | { |
1222 | CodeWord code; | 1260 | Word w; |
1223 | get_word(code,pc); | 1261 | get_word(w,pc); |
1224 | pc -= code.w; | 1262 | pc -= w; |
1225 | } | 1263 | } |
1226 | break; | 1264 | break; |
1227 | 1265 | ||
1228 | case IFFJMP: | 1266 | case IFFJMP: |
1229 | { | 1267 | { |
1230 | CodeWord code; | 1268 | Word w; |
1231 | get_word(code,pc); | 1269 | get_word(w,pc); |
1232 | top--; | 1270 | top--; |
1233 | if (tag(top) == LUA_T_NIL) pc += code.w; | 1271 | if (tag(top) == LUA_T_NIL) pc += w; |
1234 | } | 1272 | } |
1235 | break; | 1273 | break; |
1236 | 1274 | ||
1237 | case IFFUPJMP: | 1275 | case IFFUPJMP: |
1238 | { | 1276 | { |
1239 | CodeWord code; | 1277 | Word w; |
1240 | get_word(code,pc); | 1278 | get_word(w,pc); |
1241 | top--; | 1279 | top--; |
1242 | if (tag(top) == LUA_T_NIL) pc -= code.w; | 1280 | if (tag(top) == LUA_T_NIL) pc -= w; |
1243 | } | 1281 | } |
1244 | break; | 1282 | break; |
1245 | 1283 | ||
@@ -1262,8 +1300,8 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
1262 | 1300 | ||
1263 | case SETLINE: | 1301 | case SETLINE: |
1264 | { | 1302 | { |
1265 | CodeWord code; | 1303 | Word line; |
1266 | get_word(code,pc); | 1304 | get_word(line,pc); |
1267 | if ((stack+base-1)->tag != LUA_T_LINE) | 1305 | if ((stack+base-1)->tag != LUA_T_LINE) |
1268 | { | 1306 | { |
1269 | /* open space for LINE value */ | 1307 | /* open space for LINE value */ |
@@ -1271,9 +1309,9 @@ static StkId lua_execute (Byte *pc, StkId base) | |||
1271 | base++; | 1309 | base++; |
1272 | (stack+base-1)->tag = LUA_T_LINE; | 1310 | (stack+base-1)->tag = LUA_T_LINE; |
1273 | } | 1311 | } |
1274 | (stack+base-1)->value.i = code.w; | 1312 | (stack+base-1)->value.i = line; |
1275 | if (lua_linehook) | 1313 | if (lua_linehook) |
1276 | lineHook (code.w); | 1314 | lineHook (line); |
1277 | break; | 1315 | break; |
1278 | } | 1316 | } |
1279 | 1317 | ||