diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-12-20 19:20:36 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1994-12-20 19:20:36 -0200 |
| commit | 8cb8594a3bcfdc1447aebfcd0ac85db9af5ca490 (patch) | |
| tree | 13d09f704662cafa2597e77c92611b468e4741c9 /opcode.c | |
| parent | fe8338335dfb4bf37e6b164cb55bfcc94ec6563d (diff) | |
| download | lua-8cb8594a3bcfdc1447aebfcd0ac85db9af5ca490.tar.gz lua-8cb8594a3bcfdc1447aebfcd0ac85db9af5ca490.tar.bz2 lua-8cb8594a3bcfdc1447aebfcd0ac85db9af5ca490.zip | |
better control of integer types and their limits
Diffstat (limited to 'opcode.c')
| -rw-r--r-- | opcode.c | 49 |
1 files changed, 25 insertions, 24 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.26 1994/12/16 15:56:45 roberto Exp roberto $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 3.27 1994/12/16 16:08:34 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <setjmp.h> | 8 | #include <setjmp.h> |
| 9 | #include <stdio.h> | 9 | #include <stdio.h> |
| @@ -24,6 +24,8 @@ char *rcs_opcode="$Id: opcode.c,v 3.26 1994/12/16 15:56:45 roberto Exp roberto $ | |||
| 24 | 24 | ||
| 25 | #define STACK_BUFFER (STACKGAP+128) | 25 | #define STACK_BUFFER (STACKGAP+128) |
| 26 | 26 | ||
| 27 | typedef unsigned int StkId; /* index to stack elements */ | ||
| 28 | |||
| 27 | static Long maxstack = 0L; | 29 | static Long maxstack = 0L; |
| 28 | static Object *stack = NULL; | 30 | static Object *stack = NULL; |
| 29 | static Object *top = NULL; | 31 | static Object *top = NULL; |
| @@ -35,16 +37,16 @@ static Object *top = NULL; | |||
| 35 | #define Ref(st) ((st)-stack+1) | 37 | #define Ref(st) ((st)-stack+1) |
| 36 | 38 | ||
| 37 | 39 | ||
| 38 | static int CBase = 0; /* when Lua calls C or C calls Lua, points to the */ | 40 | static StkId CBase = 0; /* when Lua calls C or C calls Lua, points to */ |
| 39 | /* first slot after the last parameter. */ | 41 | /* the first slot after the last parameter. */ |
| 40 | static int CnResults = 0; /* when Lua calls C, has the number of parameters; */ | 42 | static int CnResults = 0; /* when Lua calls C, has the number of parameters; */ |
| 41 | /* when C calls Lua, has the number of results. */ | 43 | /* when C calls Lua, has the number of results. */ |
| 42 | 44 | ||
| 43 | static jmp_buf *errorJmp = NULL; /* current error recover point */ | 45 | static jmp_buf *errorJmp = NULL; /* current error recover point */ |
| 44 | 46 | ||
| 45 | 47 | ||
| 46 | static int lua_execute (Byte *pc, int base); | 48 | static StkId lua_execute (Byte *pc, StkId base); |
| 47 | static void do_call (Object *func, int base, int nResults, int whereRes); | 49 | static void do_call (Object *func, StkId base, int nResults, StkId whereRes); |
| 48 | 50 | ||
| 49 | 51 | ||
| 50 | 52 | ||
| @@ -94,11 +96,11 @@ static void lua_initstack (void) | |||
| 94 | /* | 96 | /* |
| 95 | ** Check stack overflow and, if necessary, realloc vector | 97 | ** Check stack overflow and, if necessary, realloc vector |
| 96 | */ | 98 | */ |
| 97 | static void lua_checkstack (Word n) | 99 | static void lua_checkstack (StkId n) |
| 98 | { | 100 | { |
| 99 | if ((Long)n > maxstack) | 101 | if ((Long)n > maxstack) |
| 100 | { | 102 | { |
| 101 | int t; | 103 | StkId t; |
| 102 | if (stack == NULL) | 104 | if (stack == NULL) |
| 103 | lua_initstack(); | 105 | lua_initstack(); |
| 104 | t = top-stack; | 106 | t = top-stack; |
| @@ -176,7 +178,7 @@ static int lua_tostring (Object *obj) | |||
| 176 | /* | 178 | /* |
| 177 | ** Adjust stack. Set top to the given value, pushing NILs if needed. | 179 | ** Adjust stack. Set top to the given value, pushing NILs if needed. |
| 178 | */ | 180 | */ |
| 179 | static void adjust_top (int newtop) | 181 | static void adjust_top (StkId newtop) |
| 180 | { | 182 | { |
| 181 | Object *nt = stack+newtop; | 183 | Object *nt = stack+newtop; |
| 182 | while (top < nt) tag(top++) = LUA_T_NIL; | 184 | while (top < nt) tag(top++) = LUA_T_NIL; |
| @@ -195,11 +197,11 @@ static void adjustC (int nParams) | |||
| 195 | ** and CnResults is the number of parameters. Returns an index | 197 | ** and CnResults is the number of parameters. Returns an index |
| 196 | ** to the first result from C. | 198 | ** to the first result from C. |
| 197 | */ | 199 | */ |
| 198 | static int callC (lua_CFunction func, int base) | 200 | static StkId callC (lua_CFunction func, StkId base) |
| 199 | { | 201 | { |
| 200 | int oldBase = CBase; | 202 | StkId oldBase = CBase; |
| 201 | int oldCnResults = CnResults; | 203 | int oldCnResults = CnResults; |
| 202 | int firstResult; | 204 | StkId firstResult; |
| 203 | CnResults = (top-stack) - base; | 205 | CnResults = (top-stack) - base; |
| 204 | /* incorporate parameters on the stack */ | 206 | /* incorporate parameters on the stack */ |
| 205 | CBase = base+CnResults; | 207 | CBase = base+CnResults; |
| @@ -213,9 +215,9 @@ static int callC (lua_CFunction func, int base) | |||
| 213 | /* | 215 | /* |
| 214 | ** Call the fallback for invalid functions (see do_call) | 216 | ** Call the fallback for invalid functions (see do_call) |
| 215 | */ | 217 | */ |
| 216 | static void call_funcFB (Object *func, int base, int nResults, int whereRes) | 218 | static void call_funcFB (Object *func, StkId base, int nResults, StkId whereRes) |
| 217 | { | 219 | { |
| 218 | int i; | 220 | StkId i; |
| 219 | /* open space for first parameter (func) */ | 221 | /* open space for first parameter (func) */ |
| 220 | for (i=top-stack; i>base; i--) | 222 | for (i=top-stack; i>base; i--) |
| 221 | stack[i] = stack[i-1]; | 223 | stack[i] = stack[i-1]; |
| @@ -231,9 +233,9 @@ static void call_funcFB (Object *func, int base, int nResults, int whereRes) | |||
| 231 | ** between [stack+whereRes,top). The number of results is nResults, unless | 233 | ** between [stack+whereRes,top). The number of results is nResults, unless |
| 232 | ** nResults=MULT_RET. | 234 | ** nResults=MULT_RET. |
| 233 | */ | 235 | */ |
| 234 | static void do_call (Object *func, int base, int nResults, int whereRes) | 236 | static void do_call (Object *func, StkId base, int nResults, StkId whereRes) |
| 235 | { | 237 | { |
| 236 | int firstResult; | 238 | StkId firstResult; |
| 237 | if (tag(func) == LUA_T_CFUNCTION) | 239 | if (tag(func) == LUA_T_CFUNCTION) |
| 238 | firstResult = callC(fvalue(func), base); | 240 | firstResult = callC(fvalue(func), base); |
| 239 | else if (tag(func) == LUA_T_FUNCTION) | 241 | else if (tag(func) == LUA_T_FUNCTION) |
| @@ -315,7 +317,7 @@ static int do_protectedrun (Object *function, int nResults) | |||
| 315 | { | 317 | { |
| 316 | jmp_buf myErrorJmp; | 318 | jmp_buf myErrorJmp; |
| 317 | int status; | 319 | int status; |
| 318 | int oldCBase = CBase; | 320 | StkId oldCBase = CBase; |
| 319 | jmp_buf *oldErr = errorJmp; | 321 | jmp_buf *oldErr = errorJmp; |
| 320 | errorJmp = &myErrorJmp; | 322 | errorJmp = &myErrorJmp; |
| 321 | if (setjmp(myErrorJmp) == 0) | 323 | if (setjmp(myErrorJmp) == 0) |
| @@ -340,7 +342,7 @@ static int do_protectedmain (void) | |||
| 340 | { | 342 | { |
| 341 | Byte *code = NULL; | 343 | Byte *code = NULL; |
| 342 | int status; | 344 | int status; |
| 343 | int oldCBase = CBase; | 345 | StkId oldCBase = CBase; |
| 344 | jmp_buf myErrorJmp; | 346 | jmp_buf myErrorJmp; |
| 345 | jmp_buf *oldErr = errorJmp; | 347 | jmp_buf *oldErr = errorJmp; |
| 346 | errorJmp = &myErrorJmp; | 348 | errorJmp = &myErrorJmp; |
| @@ -377,7 +379,7 @@ int lua_callfunction (lua_Object function) | |||
| 377 | 379 | ||
| 378 | int lua_call (char *funcname) | 380 | int lua_call (char *funcname) |
| 379 | { | 381 | { |
| 380 | int n = luaI_findsymbolbyname(funcname); | 382 | Word n = luaI_findsymbolbyname(funcname); |
| 381 | return do_protectedrun(&s_object(n), MULT_RET); | 383 | return do_protectedrun(&s_object(n), MULT_RET); |
| 382 | } | 384 | } |
| 383 | 385 | ||
| @@ -455,7 +457,7 @@ lua_Object lua_getsubscript (void) | |||
| 455 | #define MAX_C_BLOCKS 10 | 457 | #define MAX_C_BLOCKS 10 |
| 456 | 458 | ||
| 457 | static int numCblocks = 0; | 459 | static int numCblocks = 0; |
| 458 | static int Cblocks[MAX_C_BLOCKS]; | 460 | static StkId Cblocks[MAX_C_BLOCKS]; |
| 459 | 461 | ||
| 460 | /* | 462 | /* |
| 461 | ** API: starts a new block | 463 | ** API: starts a new block |
| @@ -580,7 +582,7 @@ int lua_lock (void) | |||
| 580 | */ | 582 | */ |
| 581 | lua_Object lua_getglobal (char *name) | 583 | lua_Object lua_getglobal (char *name) |
| 582 | { | 584 | { |
| 583 | int n = luaI_findsymbolbyname(name); | 585 | Word n = luaI_findsymbolbyname(name); |
| 584 | adjustC(0); | 586 | adjustC(0); |
| 585 | *top = s_object(n); | 587 | *top = s_object(n); |
| 586 | top++; | 588 | top++; |
| @@ -594,8 +596,7 @@ lua_Object lua_getglobal (char *name) | |||
| 594 | */ | 596 | */ |
| 595 | int lua_storeglobal (char *name) | 597 | int lua_storeglobal (char *name) |
| 596 | { | 598 | { |
| 597 | int n = luaI_findsymbolbyname(name); | 599 | Word n = luaI_findsymbolbyname(name); |
| 598 | if (n < 0) return 1; | ||
| 599 | adjustC(1); | 600 | adjustC(1); |
| 600 | s_object(n) = *(--top); | 601 | s_object(n) = *(--top); |
| 601 | return 0; | 602 | return 0; |
| @@ -736,7 +737,7 @@ static void comparison (lua_Type tag_less, lua_Type tag_equal, | |||
| 736 | ** [stack+base,top). Returns n such that the the results are between | 737 | ** [stack+base,top). Returns n such that the the results are between |
| 737 | ** [stack+n,top). | 738 | ** [stack+n,top). |
| 738 | */ | 739 | */ |
| 739 | static int lua_execute (Byte *pc, int base) | 740 | static StkId lua_execute (Byte *pc, StkId base) |
| 740 | { | 741 | { |
| 741 | lua_checkstack(STACKGAP+MAX_TEMPS+base); | 742 | lua_checkstack(STACKGAP+MAX_TEMPS+base); |
| 742 | while (1) | 743 | while (1) |
| @@ -1080,7 +1081,7 @@ static int lua_execute (Byte *pc, int base) | |||
| 1080 | int nParams = *(pc++); | 1081 | int nParams = *(pc++); |
| 1081 | int nResults = *(pc++); | 1082 | int nResults = *(pc++); |
| 1082 | Object *func = top-1-nParams; /* function is below parameters */ | 1083 | Object *func = top-1-nParams; /* function is below parameters */ |
| 1083 | int newBase = (top-stack)-nParams; | 1084 | StkId newBase = (top-stack)-nParams; |
| 1084 | do_call(func, newBase, nResults, newBase-1); | 1085 | do_call(func, newBase, nResults, newBase-1); |
| 1085 | } | 1086 | } |
| 1086 | break; | 1087 | break; |
