diff options
| -rw-r--r-- | func.c | 46 | ||||
| -rw-r--r-- | func.h | 10 | ||||
| -rw-r--r-- | inout.c | 8 | ||||
| -rw-r--r-- | inout.h | 5 | ||||
| -rw-r--r-- | lex.c | 17 | ||||
| -rw-r--r-- | lua.stx | 281 | ||||
| -rw-r--r-- | opcode.c | 96 | ||||
| -rw-r--r-- | opcode.h | 12 | ||||
| -rw-r--r-- | table.c | 60 | ||||
| -rw-r--r-- | table.h | 10 | ||||
| -rw-r--r-- | tree.c | 4 | ||||
| -rw-r--r-- | tree.h | 6 |
12 files changed, 278 insertions, 277 deletions
| @@ -11,6 +11,14 @@ | |||
| 11 | static TFunc *function_root = NULL; | 11 | static TFunc *function_root = NULL; |
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | static void luaI_insertfunction (TFunc *f) | ||
| 15 | { | ||
| 16 | lua_pack(); | ||
| 17 | f->next = function_root; | ||
| 18 | function_root = f; | ||
| 19 | f->marked = 0; | ||
| 20 | } | ||
| 21 | |||
| 14 | /* | 22 | /* |
| 15 | ** Initialize TFunc struct | 23 | ** Initialize TFunc struct |
| 16 | */ | 24 | */ |
| @@ -21,29 +29,23 @@ void luaI_initTFunc (TFunc *f) | |||
| 21 | f->code = NULL; | 29 | f->code = NULL; |
| 22 | f->lineDefined = 0; | 30 | f->lineDefined = 0; |
| 23 | f->fileName = lua_parsedfile; | 31 | f->fileName = lua_parsedfile; |
| 32 | f->consts = NULL; | ||
| 33 | f->nconsts = 0; | ||
| 24 | f->locvars = NULL; | 34 | f->locvars = NULL; |
| 35 | luaI_insertfunction(f); | ||
| 25 | } | 36 | } |
| 26 | 37 | ||
| 27 | /* | ||
| 28 | ** Insert function in list for GC | ||
| 29 | */ | ||
| 30 | void luaI_insertfunction (TFunc *f) | ||
| 31 | { | ||
| 32 | lua_pack(); | ||
| 33 | f->next = function_root; | ||
| 34 | function_root = f; | ||
| 35 | f->marked = 0; | ||
| 36 | } | ||
| 37 | 38 | ||
| 38 | 39 | ||
| 39 | /* | 40 | /* |
| 40 | ** Free function | 41 | ** Free function |
| 41 | */ | 42 | */ |
| 42 | void luaI_freefunc (TFunc *f) | 43 | static void luaI_freefunc (TFunc *f) |
| 43 | { | 44 | { |
| 44 | luaI_free (f->code); | 45 | luaI_free(f->code); |
| 45 | luaI_free (f->locvars); | 46 | luaI_free(f->locvars); |
| 46 | luaI_free (f); | 47 | luaI_free(f->consts); |
| 48 | luaI_free(f); | ||
| 47 | } | 49 | } |
| 48 | 50 | ||
| 49 | 51 | ||
| @@ -56,6 +58,20 @@ void luaI_funcfree (TFunc *l) | |||
| 56 | } | 58 | } |
| 57 | } | 59 | } |
| 58 | 60 | ||
| 61 | |||
| 62 | void luaI_funcmark (TFunc *f) | ||
| 63 | { | ||
| 64 | f->marked = 1; | ||
| 65 | if (!f->fileName->marked) | ||
| 66 | f->fileName->marked = 1; | ||
| 67 | if (f->consts) { | ||
| 68 | int i; | ||
| 69 | for (i=0; i<f->nconsts; i++) | ||
| 70 | lua_markobject(&f->consts[i]); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 59 | /* | 75 | /* |
| 60 | ** Garbage collection function. | 76 | ** Garbage collection function. |
| 61 | */ | 77 | */ |
| @@ -92,7 +108,7 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined) | |||
| 92 | TObject *f = luaI_Address(func); | 108 | TObject *f = luaI_Address(func); |
| 93 | if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION) | 109 | if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION) |
| 94 | { | 110 | { |
| 95 | *filename = f->value.tf->fileName; | 111 | *filename = f->value.tf->fileName->str; |
| 96 | *linedefined = f->value.tf->lineDefined; | 112 | *linedefined = f->value.tf->lineDefined; |
| 97 | } | 113 | } |
| 98 | else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION) | 114 | else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION) |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: func.h,v 1.10 1997/07/29 19:44:02 roberto Exp roberto $ | 2 | ** $Id: func.h,v 1.11 1997/07/29 20:38:45 roberto Exp roberto $ |
| 3 | */ | 3 | */ |
| 4 | 4 | ||
| 5 | #ifndef func_h | 5 | #ifndef func_h |
| @@ -25,16 +25,16 @@ typedef struct TFunc | |||
| 25 | int marked; | 25 | int marked; |
| 26 | Byte *code; | 26 | Byte *code; |
| 27 | int lineDefined; | 27 | int lineDefined; |
| 28 | char *fileName; | 28 | TaggedString *fileName; |
| 29 | struct TObject *consts; | ||
| 30 | int nconsts; | ||
| 29 | LocVar *locvars; | 31 | LocVar *locvars; |
| 30 | } TFunc; | 32 | } TFunc; |
| 31 | 33 | ||
| 32 | TFunc *luaI_funccollector (long *cont); | 34 | TFunc *luaI_funccollector (long *cont); |
| 33 | void luaI_funcfree (TFunc *l); | 35 | void luaI_funcfree (TFunc *l); |
| 34 | void luaI_insertfunction (TFunc *f); | 36 | void luaI_funcmark (TFunc *f); |
| 35 | |||
| 36 | void luaI_initTFunc (TFunc *f); | 37 | void luaI_initTFunc (TFunc *f); |
| 37 | void luaI_freefunc (TFunc *f); | ||
| 38 | 38 | ||
| 39 | char *luaI_getlocalname (TFunc *func, int local_number, int line); | 39 | char *luaI_getlocalname (TFunc *func, int local_number, int line); |
| 40 | 40 | ||
| @@ -5,7 +5,7 @@ | |||
| 5 | ** Also provides some predefined lua functions. | 5 | ** Also provides some predefined lua functions. |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | char *rcs_inout="$Id: inout.c,v 2.70 1997/07/07 21:05:51 roberto Exp roberto $"; | 8 | char *rcs_inout="$Id: inout.c,v 2.71 1997/07/29 13:33:15 roberto Exp roberto $"; |
| 9 | 9 | ||
| 10 | #include <stdio.h> | 10 | #include <stdio.h> |
| 11 | #include <string.h> | 11 | #include <string.h> |
| @@ -27,7 +27,7 @@ char *rcs_inout="$Id: inout.c,v 2.70 1997/07/07 21:05:51 roberto Exp roberto $"; | |||
| 27 | 27 | ||
| 28 | /* Exported variables */ | 28 | /* Exported variables */ |
| 29 | Word lua_linenumber; | 29 | Word lua_linenumber; |
| 30 | char *lua_parsedfile; | 30 | TaggedString *lua_parsedfile; |
| 31 | 31 | ||
| 32 | 32 | ||
| 33 | char *luaI_typenames[] = { /* ORDER LUA_T */ | 33 | char *luaI_typenames[] = { /* ORDER LUA_T */ |
| @@ -40,7 +40,7 @@ char *luaI_typenames[] = { /* ORDER LUA_T */ | |||
| 40 | 40 | ||
| 41 | void luaI_setparsedfile (char *name) | 41 | void luaI_setparsedfile (char *name) |
| 42 | { | 42 | { |
| 43 | lua_parsedfile = luaI_createfixedstring(name)->str; | 43 | lua_parsedfile = luaI_createstring(name); |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | 46 | ||
| @@ -399,7 +399,7 @@ void luaI_predefine (void) | |||
| 399 | s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func; | 399 | s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func; |
| 400 | } | 400 | } |
| 401 | n = luaI_findsymbolbyname("_VERSION"); | 401 | n = luaI_findsymbolbyname("_VERSION"); |
| 402 | s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION); | 402 | s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = luaI_createstring(LUA_VERSION); |
| 403 | } | 403 | } |
| 404 | 404 | ||
| 405 | 405 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: inout.h,v 1.19 1997/06/18 20:35:49 roberto Exp roberto $ | 2 | ** $Id: inout.h,v 1.20 1997/06/19 18:04:34 roberto Exp roberto $ |
| 3 | */ | 3 | */ |
| 4 | 4 | ||
| 5 | 5 | ||
| @@ -7,12 +7,13 @@ | |||
| 7 | #define inout_h | 7 | #define inout_h |
| 8 | 8 | ||
| 9 | #include "types.h" | 9 | #include "types.h" |
| 10 | #include "tree.h" | ||
| 10 | #include <stdio.h> | 11 | #include <stdio.h> |
| 11 | 12 | ||
| 12 | 13 | ||
| 13 | extern Word lua_linenumber; | 14 | extern Word lua_linenumber; |
| 14 | extern Word lua_debugline; | 15 | extern Word lua_debugline; |
| 15 | extern char *lua_parsedfile; | 16 | extern TaggedString *lua_parsedfile; |
| 16 | 17 | ||
| 17 | void luaI_setparsedfile (char *name); | 18 | void luaI_setparsedfile (char *name); |
| 18 | 19 | ||
| @@ -1,4 +1,4 @@ | |||
| 1 | char *rcs_lex = "$Id: lex.c,v 3.6 1997/07/01 19:32:41 roberto Exp roberto $"; | 1 | char *rcs_lex = "$Id: lex.c,v 3.7 1997/07/29 13:33:15 roberto Exp roberto $"; |
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | #include <ctype.h> | 4 | #include <ctype.h> |
| @@ -62,7 +62,7 @@ void lua_setinput (ZIO *z) | |||
| 62 | static void luaI_auxsyntaxerror (char *s) | 62 | static void luaI_auxsyntaxerror (char *s) |
| 63 | { | 63 | { |
| 64 | luaL_verror("%s;\n> at line %d in file %s", | 64 | luaL_verror("%s;\n> at line %d in file %s", |
| 65 | s, lua_linenumber, lua_parsedfile); | 65 | s, lua_linenumber, lua_parsedfile->str); |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | static void luaI_auxsynterrbf (char *s, char *token) | 68 | static void luaI_auxsynterrbf (char *s, char *token) |
| @@ -70,7 +70,7 @@ static void luaI_auxsynterrbf (char *s, char *token) | |||
| 70 | if (token[0] == 0) | 70 | if (token[0] == 0) |
| 71 | token = "<eof>"; | 71 | token = "<eof>"; |
| 72 | luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s", | 72 | luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s", |
| 73 | s, token, lua_linenumber, lua_parsedfile); | 73 | s, token, lua_linenumber, lua_parsedfile->str); |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | void luaI_syntaxerror (char *s) | 76 | void luaI_syntaxerror (char *s) |
| @@ -110,7 +110,7 @@ void luaI_addReserved (void) | |||
| 110 | int i; | 110 | int i; |
| 111 | for (i=0; i<RESERVEDSIZE; i++) | 111 | for (i=0; i<RESERVEDSIZE; i++) |
| 112 | { | 112 | { |
| 113 | TaggedString *ts = lua_createstring(reserved[i].name); | 113 | TaggedString *ts = luaI_createstring(reserved[i].name); |
| 114 | ts->marked = reserved[i].token; /* reserved word (always > 255) */ | 114 | ts->marked = reserved[i].token; /* reserved word (always > 255) */ |
| 115 | } | 115 | } |
| 116 | } | 116 | } |
| @@ -273,7 +273,7 @@ static int read_long_string (char *yytext, int buffsize) | |||
| 273 | } endloop: | 273 | } endloop: |
| 274 | save_and_next(); /* pass the second ']' */ | 274 | save_and_next(); /* pass the second ']' */ |
| 275 | yytext[tokensize-2] = 0; /* erases ']]' */ | 275 | yytext[tokensize-2] = 0; /* erases ']]' */ |
| 276 | luaY_lval.vWord = luaI_findconstantbyname(yytext+2); | 276 | luaY_lval.pTStr = luaI_createtempstring(yytext+2); |
| 277 | yytext[tokensize-2] = ']'; /* restores ']]' */ | 277 | yytext[tokensize-2] = ']'; /* restores ']]' */ |
| 278 | save(0); | 278 | save(0); |
| 279 | return STRING; | 279 | return STRING; |
| @@ -368,7 +368,7 @@ int luaY_lex (void) | |||
| 368 | } | 368 | } |
| 369 | next(); /* skip delimiter */ | 369 | next(); /* skip delimiter */ |
| 370 | save(0); | 370 | save(0); |
| 371 | luaY_lval.vWord = luaI_findconstantbyname(yytext+1); | 371 | luaY_lval.pTStr = luaI_createtempstring(yytext+1); |
| 372 | tokensize--; | 372 | tokensize--; |
| 373 | save(del); save(0); /* restore delimiter */ | 373 | save(del); save(0); /* restore delimiter */ |
| 374 | return STRING; | 374 | return STRING; |
| @@ -454,11 +454,10 @@ int luaY_lex (void) | |||
| 454 | save_and_next(); | 454 | save_and_next(); |
| 455 | } while (isalnum((unsigned char)current) || current == '_'); | 455 | } while (isalnum((unsigned char)current) || current == '_'); |
| 456 | save(0); | 456 | save(0); |
| 457 | ts = lua_createstring(yytext); | 457 | ts = luaI_createtempstring(yytext); |
| 458 | if (ts->marked > 2) | 458 | if (ts->marked > 255) |
| 459 | return ts->marked; /* reserved word */ | 459 | return ts->marked; /* reserved word */ |
| 460 | luaY_lval.pTStr = ts; | 460 | luaY_lval.pTStr = ts; |
| 461 | ts->marked = 2; /* avoid GC */ | ||
| 462 | return NAME; | 461 | return NAME; |
| 463 | } | 462 | } |
| 464 | } | 463 | } |
| @@ -1,10 +1,8 @@ | |||
| 1 | %{ | 1 | %{ |
| 2 | 2 | ||
| 3 | char *rcs_luastx = "$Id: lua.stx,v 3.47 1997/06/19 17:46:12 roberto Exp roberto $"; | 3 | char *rcs_luastx = "$Id: lua.stx,v 3.48 1997/07/29 20:38:45 roberto Exp roberto $"; |
| 4 | 4 | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | 5 | #include <stdlib.h> |
| 7 | #include <string.h> | ||
| 8 | 6 | ||
| 9 | #include "luadebug.h" | 7 | #include "luadebug.h" |
| 10 | #include "luamem.h" | 8 | #include "luamem.h" |
| @@ -40,6 +38,7 @@ struct State { | |||
| 40 | int pc; /* next position to code */ | 38 | int pc; /* next position to code */ |
| 41 | TaggedString *localvar[MAXLOCALS]; /* store local variable names */ | 39 | TaggedString *localvar[MAXLOCALS]; /* store local variable names */ |
| 42 | int nlocalvar; /* number of active local variables */ | 40 | int nlocalvar; /* number of active local variables */ |
| 41 | int maxconsts; /* size of consts vector */ | ||
| 43 | int nvars; /* total number of local variables (for debugging information) */ | 42 | int nvars; /* total number of local variables (for debugging information) */ |
| 44 | int maxvars; /* = -1 if no debug information */ | 43 | int maxvars; /* = -1 if no debug information */ |
| 45 | } stateMain, stateFunc, *currState; | 44 | } stateMain, stateFunc, *currState; |
| @@ -69,41 +68,106 @@ static void check_space (int i) | |||
| 69 | Byte, codeEM, MAX_INT); | 68 | Byte, codeEM, MAX_INT); |
| 70 | } | 69 | } |
| 71 | 70 | ||
| 71 | |||
| 72 | static void code_byte (Byte c) | 72 | static void code_byte (Byte c) |
| 73 | { | 73 | { |
| 74 | check_space(1); | 74 | check_space(1); |
| 75 | currState->f->code[currState->pc++] = c; | 75 | currState->f->code[currState->pc++] = c; |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | static void code_float (real n) | 78 | |
| 79 | static void code_word_at (int pc, int n) | ||
| 79 | { | 80 | { |
| 80 | check_space(sizeof(real)); | 81 | Word w = n; |
| 81 | memcpy(currState->f->code+currState->pc, &n, sizeof(real)); | 82 | if (w != n) |
| 82 | currState->pc += sizeof(real); | 83 | yyerror("block too big"); |
| 84 | currState->f->code[pc] = n&0xFF; | ||
| 85 | currState->f->code[pc+1] = n>>8; | ||
| 83 | } | 86 | } |
| 84 | 87 | ||
| 85 | static void code_code (TFunc *tf) | 88 | static void code_word (int n) |
| 86 | { | 89 | { |
| 87 | check_space(sizeof(TFunc *)); | 90 | code_byte(n&0xFF); |
| 88 | memcpy(currState->f->code+currState->pc, &tf, sizeof(TFunc *)); | 91 | code_byte(n>>8); |
| 89 | currState->pc += sizeof(TFunc *); | ||
| 90 | } | 92 | } |
| 91 | 93 | ||
| 92 | static void code_word_at (int pc, int n) | 94 | static void code_constant (int c) |
| 93 | { | 95 | { |
| 94 | Word w = n; | 96 | if (c <= 255) { |
| 95 | if (w != n) | 97 | code_byte(PUSHCONSTANTB); |
| 96 | yyerror("block too big"); | 98 | code_byte(c); |
| 97 | memcpy(currState->f->code+pc, &w, sizeof(Word)); | 99 | } |
| 100 | else { | ||
| 101 | code_byte(PUSHCONSTANT); | ||
| 102 | code_word(c); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | static int next_constant (void) | ||
| 108 | { | ||
| 109 | if (currState->f->nconsts >= currState->maxconsts) { | ||
| 110 | currState->maxconsts = | ||
| 111 | growvector(&currState->f->consts, currState->maxconsts, | ||
| 112 | TObject, constantEM, MAX_WORD); | ||
| 113 | } | ||
| 114 | return currState->f->nconsts++; | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | static int string_constant (TaggedString *s) | ||
| 119 | { | ||
| 120 | int c = s->u.s.constindex; | ||
| 121 | if (!(0 <= c && c < currState->f->nconsts && | ||
| 122 | ttype(&currState->f->consts[c]) == LUA_T_STRING && | ||
| 123 | tsvalue(&currState->f->consts[c]) == s)) { | ||
| 124 | c = next_constant(); | ||
| 125 | ttype(&currState->f->consts[c]) = LUA_T_STRING; | ||
| 126 | tsvalue(&currState->f->consts[c]) = s; | ||
| 127 | s->u.s.constindex = c; /* hint for next time */ | ||
| 128 | luaI_releasestring(s); | ||
| 129 | } | ||
| 130 | return c; | ||
| 98 | } | 131 | } |
| 99 | 132 | ||
| 100 | static void code_word (Word n) | 133 | |
| 134 | static void code_string (TaggedString *s) | ||
| 101 | { | 135 | { |
| 102 | check_space(sizeof(Word)); | 136 | int c = string_constant(s); |
| 103 | memcpy(currState->f->code+currState->pc, &n, sizeof(Word)); | 137 | code_constant(c); |
| 104 | currState->pc += sizeof(Word); | ||
| 105 | } | 138 | } |
| 106 | 139 | ||
| 140 | static void code_float (real n) | ||
| 141 | { | ||
| 142 | int c = next_constant(); | ||
| 143 | ttype(&currState->f->consts[c]) = LUA_T_NUMBER; | ||
| 144 | nvalue(&currState->f->consts[c]) = n; | ||
| 145 | code_constant(c); | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | static void code_number (float f) | ||
| 150 | { | ||
| 151 | Word i; | ||
| 152 | if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) { | ||
| 153 | /* f has an (short) integer value */ | ||
| 154 | if (i <= 2) code_byte(PUSH0 + i); | ||
| 155 | else if (i <= 255) | ||
| 156 | { | ||
| 157 | code_byte(PUSHBYTE); | ||
| 158 | code_byte(i); | ||
| 159 | } | ||
| 160 | else | ||
| 161 | { | ||
| 162 | code_byte(PUSHWORD); | ||
| 163 | code_word(i); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | else | ||
| 167 | code_float(f); | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 107 | static void flush_record (int n) | 171 | static void flush_record (int n) |
| 108 | { | 172 | { |
| 109 | if (n == 0) return; | 173 | if (n == 0) return; |
| @@ -149,6 +213,7 @@ static void luaI_unregisterlocalvar (int line) | |||
| 149 | 213 | ||
| 150 | static void store_localvar (TaggedString *name, int n) | 214 | static void store_localvar (TaggedString *name, int n) |
| 151 | { | 215 | { |
| 216 | luaI_fixstring(name); /* local var names cannot be GC */ | ||
| 152 | if (currState->nlocalvar+n < MAXLOCALS) | 217 | if (currState->nlocalvar+n < MAXLOCALS) |
| 153 | currState->localvar[currState->nlocalvar+n] = name; | 218 | currState->localvar[currState->nlocalvar+n] = name; |
| 154 | else | 219 | else |
| @@ -170,40 +235,6 @@ static void add_varbuffer (Long var) | |||
| 170 | yyerror ("variable buffer overflow"); | 235 | yyerror ("variable buffer overflow"); |
| 171 | } | 236 | } |
| 172 | 237 | ||
| 173 | static void code_string (Word w) | ||
| 174 | { | ||
| 175 | code_byte(PUSHSTRING); | ||
| 176 | code_word(w); | ||
| 177 | } | ||
| 178 | |||
| 179 | static void code_constant (TaggedString *s) | ||
| 180 | { | ||
| 181 | code_string(luaI_findconstant(s)); | ||
| 182 | } | ||
| 183 | |||
| 184 | static void code_number (float f) | ||
| 185 | { | ||
| 186 | Word i; | ||
| 187 | if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) { | ||
| 188 | /* f has an (short) integer value */ | ||
| 189 | if (i <= 2) code_byte(PUSH0 + i); | ||
| 190 | else if (i <= 255) | ||
| 191 | { | ||
| 192 | code_byte(PUSHBYTE); | ||
| 193 | code_byte(i); | ||
| 194 | } | ||
| 195 | else | ||
| 196 | { | ||
| 197 | code_byte(PUSHWORD); | ||
| 198 | code_word(i); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | else | ||
| 202 | { | ||
| 203 | code_byte(PUSHFLOAT); | ||
| 204 | code_float(f); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | 238 | ||
| 208 | /* | 239 | /* |
| 209 | ** Search a local name and if find return its index. If do not find return -1 | 240 | ** Search a local name and if find return its index. If do not find return -1 |
| @@ -256,55 +287,6 @@ static void lua_codeadjust (int n) | |||
| 256 | } | 287 | } |
| 257 | 288 | ||
| 258 | 289 | ||
| 259 | static void init_state (TFunc *f) | ||
| 260 | { | ||
| 261 | luaI_initTFunc(f); | ||
| 262 | currState->nlocalvar = 0; | ||
| 263 | currState->f = f; | ||
| 264 | currState->pc = 0; | ||
| 265 | currState->codesize = CODE_BLOCK; | ||
| 266 | f->code = newvector(CODE_BLOCK, Byte); | ||
| 267 | if (lua_debug) { | ||
| 268 | currState->nvars = 0; | ||
| 269 | currState->maxvars = 0; | ||
| 270 | } | ||
| 271 | else | ||
| 272 | currState->maxvars = -1; /* flag no debug information */ | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | static void init_func (void) | ||
| 277 | { | ||
| 278 | currState = &stateFunc; | ||
| 279 | init_state(new(TFunc)); | ||
| 280 | luaI_codedebugline(lua_linenumber); | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | static void codereturn (void) | ||
| 285 | { | ||
| 286 | if (currState->nlocalvar == 0) | ||
| 287 | code_byte(RETCODE0); | ||
| 288 | else | ||
| 289 | { | ||
| 290 | code_byte(RETCODE); | ||
| 291 | code_byte(currState->nlocalvar); | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | |||
| 296 | static void close_func (void) | ||
| 297 | { | ||
| 298 | codereturn(); | ||
| 299 | code_byte(ENDCODE); | ||
| 300 | currState->f->code = shrinkvector(currState->f->code, currState->pc, Byte); | ||
| 301 | if (currState->maxvars != -1) { /* debug information? */ | ||
| 302 | luaI_registerlocalvar(NULL, -1); /* flag end of vector */ | ||
| 303 | currState->f->locvars = shrinkvector(currState->f->locvars, | ||
| 304 | currState->nvars, LocVar); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | 290 | ||
| 309 | void luaI_codedebugline (int line) | 291 | void luaI_codedebugline (int line) |
| 310 | { | 292 | { |
| @@ -350,7 +332,7 @@ static int close_parlist (int dots) | |||
| 350 | else { | 332 | else { |
| 351 | code_byte(VARARGS); | 333 | code_byte(VARARGS); |
| 352 | code_byte(currState->nlocalvar); | 334 | code_byte(currState->nlocalvar); |
| 353 | add_localvar(luaI_createfixedstring("arg")); | 335 | add_localvar(luaI_createstring("arg")); |
| 354 | } | 336 | } |
| 355 | return lua_linenumber; | 337 | return lua_linenumber; |
| 356 | } | 338 | } |
| @@ -423,6 +405,65 @@ static void code_shortcircuit (int pc, Byte jmp) | |||
| 423 | } | 405 | } |
| 424 | 406 | ||
| 425 | 407 | ||
| 408 | static void init_state (TFunc *f) | ||
| 409 | { | ||
| 410 | currState->nlocalvar = 0; | ||
| 411 | currState->f = f; | ||
| 412 | currState->pc = 0; | ||
| 413 | currState->codesize = CODE_BLOCK; | ||
| 414 | f->code = newvector(CODE_BLOCK, Byte); | ||
| 415 | currState->maxconsts = 0; | ||
| 416 | if (lua_debug) { | ||
| 417 | currState->nvars = 0; | ||
| 418 | currState->maxvars = 0; | ||
| 419 | } | ||
| 420 | else | ||
| 421 | currState->maxvars = -1; /* flag no debug information */ | ||
| 422 | } | ||
| 423 | |||
| 424 | |||
| 425 | static void init_func (Long v) | ||
| 426 | { | ||
| 427 | TFunc *f = new(TFunc); | ||
| 428 | int c = next_constant(); | ||
| 429 | ttype(&currState->f->consts[c]) = LUA_T_FUNCTION; | ||
| 430 | currState->f->consts[c].value.tf = f; | ||
| 431 | code_constant(c); | ||
| 432 | storesinglevar(v); | ||
| 433 | currState = &stateFunc; | ||
| 434 | luaI_initTFunc(f); | ||
| 435 | init_state(f); | ||
| 436 | luaI_codedebugline(lua_linenumber); | ||
| 437 | } | ||
| 438 | |||
| 439 | |||
| 440 | static void codereturn (void) | ||
| 441 | { | ||
| 442 | if (currState->nlocalvar == 0) | ||
| 443 | code_byte(RETCODE0); | ||
| 444 | else | ||
| 445 | { | ||
| 446 | code_byte(RETCODE); | ||
| 447 | code_byte(currState->nlocalvar); | ||
| 448 | } | ||
| 449 | } | ||
| 450 | |||
| 451 | |||
| 452 | static void close_func (void) | ||
| 453 | { | ||
| 454 | codereturn(); | ||
| 455 | code_byte(ENDCODE); | ||
| 456 | currState->f->code = shrinkvector(currState->f->code, currState->pc, Byte); | ||
| 457 | currState->f->consts = shrinkvector(currState->f->consts, | ||
| 458 | currState->f->nconsts, TObject); | ||
| 459 | if (currState->maxvars != -1) { /* debug information? */ | ||
| 460 | luaI_registerlocalvar(NULL, -1); /* flag end of vector */ | ||
| 461 | currState->f->locvars = shrinkvector(currState->f->locvars, | ||
| 462 | currState->nvars, LocVar); | ||
| 463 | } | ||
| 464 | } | ||
| 465 | |||
| 466 | |||
| 426 | /* | 467 | /* |
| 427 | ** Parse LUA code. | 468 | ** Parse LUA code. |
| 428 | */ | 469 | */ |
| @@ -444,9 +485,7 @@ void lua_parse (TFunc *tf) | |||
| 444 | int vInt; | 485 | int vInt; |
| 445 | float vFloat; | 486 | float vFloat; |
| 446 | char *pChar; | 487 | char *pChar; |
| 447 | Word vWord; | ||
| 448 | Long vLong; | 488 | Long vLong; |
| 449 | TFunc *pFunc; | ||
| 450 | TaggedString *pTStr; | 489 | TaggedString *pTStr; |
| 451 | } | 490 | } |
| 452 | 491 | ||
| @@ -460,8 +499,7 @@ void lua_parse (TFunc *tf) | |||
| 460 | %token FUNCTION | 499 | %token FUNCTION |
| 461 | %token DOTS | 500 | %token DOTS |
| 462 | %token <vFloat> NUMBER | 501 | %token <vFloat> NUMBER |
| 463 | %token <vWord> STRING | 502 | %token <pTStr> NAME STRING |
| 464 | %token <pTStr> NAME | ||
| 465 | 503 | ||
| 466 | %type <vLong> PrepJump | 504 | %type <vLong> PrepJump |
| 467 | %type <vLong> exprlist, exprlist1 /* if > 0, points to function return | 505 | %type <vLong> exprlist, exprlist1 /* if > 0, points to function return |
| @@ -473,8 +511,7 @@ void lua_parse (TFunc *tf) | |||
| 473 | %type <vInt> ffieldlist, ffieldlist1, semicolonpart | 511 | %type <vInt> ffieldlist, ffieldlist1, semicolonpart |
| 474 | %type <vInt> lfieldlist, lfieldlist1 | 512 | %type <vInt> lfieldlist, lfieldlist1 |
| 475 | %type <vInt> parlist, parlist1, par | 513 | %type <vInt> parlist, parlist1, par |
| 476 | %type <vLong> var, singlevar, funcname | 514 | %type <vLong> var, singlevar |
| 477 | %type <pFunc> body | ||
| 478 | 515 | ||
| 479 | %left AND OR | 516 | %left AND OR |
| 480 | %left EQ NE '>' '<' LE GE | 517 | %left EQ NE '>' '<' LE GE |
| @@ -495,28 +532,21 @@ chunklist : /* empty */ | |||
| 495 | ; | 532 | ; |
| 496 | 533 | ||
| 497 | function : FUNCTION funcname body | 534 | function : FUNCTION funcname body |
| 498 | { | ||
| 499 | code_byte(PUSHFUNCTION); | ||
| 500 | code_code($3); | ||
| 501 | storesinglevar($2); | ||
| 502 | } | ||
| 503 | ; | 535 | ; |
| 504 | 536 | ||
| 505 | funcname : var { $$ =$1; init_func(); } | 537 | funcname : var { init_func($1); } |
| 506 | | varexp ':' NAME | 538 | | varexp ':' NAME |
| 507 | { | 539 | { |
| 508 | code_constant($3); | 540 | code_string($3); |
| 509 | $$ = 0; /* indexed variable */ | 541 | init_func(0); /* indexed variable */ |
| 510 | init_func(); | 542 | add_localvar(luaI_createstring("self")); |
| 511 | add_localvar(luaI_createfixedstring("self")); | ||
| 512 | } | 543 | } |
| 513 | ; | 544 | ; |
| 514 | 545 | ||
| 515 | body : '(' parlist ')' block END | 546 | body : '(' parlist ')' block END |
| 516 | { | 547 | { |
| 517 | close_func(); | 548 | close_func(); |
| 518 | $$ = currState->f; | 549 | currState->f->lineDefined = $2; |
| 519 | $$->lineDefined = $2; | ||
| 520 | currState = &stateMain; /* change back to main code */ | 550 | currState = &stateMain; /* change back to main code */ |
| 521 | } | 551 | } |
| 522 | ; | 552 | ; |
| @@ -658,7 +688,7 @@ funcvalue : varexp { $$ = 0; } | |||
| 658 | | varexp ':' NAME | 688 | | varexp ':' NAME |
| 659 | { | 689 | { |
| 660 | code_byte(PUSHSELF); | 690 | code_byte(PUSHSELF); |
| 661 | code_word(luaI_findconstant($3)); | 691 | code_word(string_constant($3)); |
| 662 | $$ = 1; | 692 | $$ = 1; |
| 663 | } | 693 | } |
| 664 | ; | 694 | ; |
| @@ -735,7 +765,7 @@ ffield : ffieldkey '=' expr1 | |||
| 735 | ; | 765 | ; |
| 736 | 766 | ||
| 737 | ffieldkey : '[' expr1 ']' | 767 | ffieldkey : '[' expr1 ']' |
| 738 | | NAME { code_constant($1); } | 768 | | NAME { code_string($1); } |
| 739 | ; | 769 | ; |
| 740 | 770 | ||
| 741 | lfieldlist : /* empty */ { $$ = 0; } | 771 | lfieldlist : /* empty */ { $$ = 0; } |
| @@ -771,7 +801,7 @@ var : singlevar { $$ = $1; } | |||
| 771 | } | 801 | } |
| 772 | | varexp '.' NAME | 802 | | varexp '.' NAME |
| 773 | { | 803 | { |
| 774 | code_constant($3); | 804 | code_string($3); |
| 775 | $$ = 0; /* indexed variable */ | 805 | $$ = 0; /* indexed variable */ |
| 776 | } | 806 | } |
| 777 | ; | 807 | ; |
| @@ -783,6 +813,7 @@ singlevar : NAME | |||
| 783 | $$ = luaI_findsymbol($1)+1; /* return positive value */ | 813 | $$ = luaI_findsymbol($1)+1; /* return positive value */ |
| 784 | else | 814 | else |
| 785 | $$ = -(local+1); /* return negative value */ | 815 | $$ = -(local+1); /* return negative value */ |
| 816 | luaI_fixstring($1); /* cannot GC variable names */ | ||
| 786 | } | 817 | } |
| 787 | ; | 818 | ; |
| 788 | 819 | ||
| @@ -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 | } |
| @@ -1,6 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** TeCGraf - PUC-Rio | 2 | ** TeCGraf - PUC-Rio |
| 3 | ** $Id: opcode.h,v 3.35 1997/07/04 14:55:37 roberto Exp roberto $ | 3 | ** $Id: opcode.h,v 3.36 1997/07/29 20:38:06 roberto Exp roberto $ |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | #ifndef opcode_h | 6 | #ifndef opcode_h |
| @@ -47,9 +47,6 @@ PUSH1,/* - 1.0 */ | |||
| 47 | PUSH2,/* - 2.0 */ | 47 | PUSH2,/* - 2.0 */ |
| 48 | PUSHBYTE,/* b - (float)b */ | 48 | PUSHBYTE,/* b - (float)b */ |
| 49 | PUSHWORD,/* w - (float)w */ | 49 | PUSHWORD,/* w - (float)w */ |
| 50 | PUSHFLOAT,/* f - f */ | ||
| 51 | PUSHSTRING,/* w - STR[w] */ | ||
| 52 | PUSHFUNCTION,/* p - FUN(p) */ | ||
| 53 | PUSHLOCAL0,/* - LOC[0] */ | 50 | PUSHLOCAL0,/* - LOC[0] */ |
| 54 | PUSHLOCAL1,/* - LOC[1] */ | 51 | PUSHLOCAL1,/* - LOC[1] */ |
| 55 | PUSHLOCAL2,/* - LOC[2] */ | 52 | PUSHLOCAL2,/* - LOC[2] */ |
| @@ -111,6 +108,8 @@ RETCODE,/* b - - */ | |||
| 111 | SETLINE,/* w - - LINE=w */ | 108 | SETLINE,/* w - - LINE=w */ |
| 112 | VARARGS,/* b v_b...v_1 {v_1...v_b;n=b} */ | 109 | VARARGS,/* b v_b...v_1 {v_1...v_b;n=b} */ |
| 113 | STOREMAP,/* b v_b k_b ...v_1 k_1 t - t[k_i]=v_i */ | 110 | STOREMAP,/* b v_b k_b ...v_1 k_1 t - t[k_i]=v_i */ |
| 111 | PUSHCONSTANTB,/*b - const[b] */ | ||
| 112 | PUSHCONSTANT,/* w - const[w] */ | ||
| 114 | ENDCODE = 127 | 113 | ENDCODE = 127 |
| 115 | } OpCode; | 114 | } OpCode; |
| 116 | 115 | ||
| @@ -153,11 +152,6 @@ typedef struct TObject | |||
| 153 | #define s_fvalue(i) (fvalue(&s_object(i))) | 152 | #define s_fvalue(i) (fvalue(&s_object(i))) |
| 154 | #define s_uvalue(i) (uvalue(&s_object(i))) | 153 | #define s_uvalue(i) (uvalue(&s_object(i))) |
| 155 | 154 | ||
| 156 | #define get_word(code,pc) {memcpy(&code, pc, sizeof(Word)); pc+=sizeof(Word);} | ||
| 157 | #define get_float(code,pc){memcpy(&code, pc, sizeof(real)); pc+=sizeof(real);} | ||
| 158 | #define get_code(code,pc) {memcpy(&code, pc, sizeof(TFunc *)); \ | ||
| 159 | pc+=sizeof(TFunc *);} | ||
| 160 | |||
| 161 | 155 | ||
| 162 | /* Exported functions */ | 156 | /* Exported functions */ |
| 163 | void lua_parse (TFunc *tf); /* from "lua.stx" module */ | 157 | void lua_parse (TFunc *tf); /* from "lua.stx" module */ |
| @@ -3,7 +3,7 @@ | |||
| 3 | ** Module to control static tables | 3 | ** Module to control static tables |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_table="$Id: table.c,v 2.72 1997/06/17 18:09:31 roberto Exp roberto $"; | 6 | char *rcs_table="$Id: table.c,v 2.73 1997/07/07 16:44:26 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include "luamem.h" | 8 | #include "luamem.h" |
| 9 | #include "auxlib.h" | 9 | #include "auxlib.h" |
| @@ -24,14 +24,17 @@ Symbol *lua_table = NULL; | |||
| 24 | Word lua_ntable = 0; | 24 | Word lua_ntable = 0; |
| 25 | static Long lua_maxsymbol = 0; | 25 | static Long lua_maxsymbol = 0; |
| 26 | 26 | ||
| 27 | TaggedString **lua_constant = NULL; | ||
| 28 | Word lua_nconstant = 0; | ||
| 29 | static Long lua_maxconstant = 0; | ||
| 30 | |||
| 31 | 27 | ||
| 32 | #define GARBAGE_BLOCK 100 | 28 | #define GARBAGE_BLOCK 100 |
| 33 | 29 | ||
| 34 | 30 | ||
| 31 | static TaggedString *luaI_createfixedstring (char *name) | ||
| 32 | { | ||
| 33 | TaggedString *ts = luaI_createstring(name); | ||
| 34 | luaI_fixstring(ts); | ||
| 35 | return ts; | ||
| 36 | } | ||
| 37 | |||
| 35 | void luaI_initsymbol (void) | 38 | void luaI_initsymbol (void) |
| 36 | { | 39 | { |
| 37 | lua_maxsymbol = BUFFER_BLOCK; | 40 | lua_maxsymbol = BUFFER_BLOCK; |
| @@ -40,16 +43,11 @@ void luaI_initsymbol (void) | |||
| 40 | } | 43 | } |
| 41 | 44 | ||
| 42 | 45 | ||
| 43 | /* | ||
| 44 | ** Initialise constant table with pre-defined constants | ||
| 45 | */ | ||
| 46 | void luaI_initconstant (void) | 46 | void luaI_initconstant (void) |
| 47 | { | 47 | { |
| 48 | lua_maxconstant = BUFFER_BLOCK; | 48 | /* pre-register mem error messages, to avoid loop when error arises */ |
| 49 | lua_constant = newvector(lua_maxconstant, TaggedString *); | 49 | luaI_createfixedstring(tableEM); |
| 50 | /* pre-register mem error messages, to avoid loop when error arises */ | 50 | luaI_createfixedstring(memEM); |
| 51 | luaI_findconstantbyname(tableEM); | ||
| 52 | luaI_findconstantbyname(memEM); | ||
| 53 | } | 51 | } |
| 54 | 52 | ||
| 55 | 53 | ||
| @@ -79,35 +77,25 @@ Word luaI_findsymbolbyname (char *name) | |||
| 79 | } | 77 | } |
| 80 | 78 | ||
| 81 | 79 | ||
| 82 | /* | 80 | void luaI_releasestring (TaggedString *t) |
| 83 | ** Given a tree node, check it is has a correspondent constant index. If not, | ||
| 84 | ** allocate it. | ||
| 85 | */ | ||
| 86 | Word luaI_findconstant (TaggedString *t) | ||
| 87 | { | 81 | { |
| 88 | if (t->u.s.constindex == NOT_USED) | 82 | if (t->marked == 2) /* string has temporary mark? */ |
| 89 | { | 83 | t->marked = 0; |
| 90 | if (lua_nconstant == lua_maxconstant) | ||
| 91 | lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *, | ||
| 92 | constantEM, MAX_WORD); | ||
| 93 | t->u.s.constindex = lua_nconstant; | ||
| 94 | lua_constant[lua_nconstant] = t; | ||
| 95 | lua_nconstant++; | ||
| 96 | } | ||
| 97 | return t->u.s.constindex; | ||
| 98 | } | 84 | } |
| 99 | 85 | ||
| 100 | 86 | ||
| 101 | Word luaI_findconstantbyname (char *name) | 87 | void luaI_fixstring (TaggedString *t) |
| 102 | { | 88 | { |
| 103 | return luaI_findconstant(luaI_createfixedstring(name)); | 89 | if (t->marked < 3) |
| 90 | t->marked = 3; /* avoid GC */ | ||
| 104 | } | 91 | } |
| 105 | 92 | ||
| 106 | TaggedString *luaI_createfixedstring (char *name) | 93 | |
| 94 | TaggedString *luaI_createtempstring (char *name) | ||
| 107 | { | 95 | { |
| 108 | TaggedString *ts = lua_createstring(name); | 96 | TaggedString *ts = luaI_createstring(name); |
| 109 | if (!ts->marked) | 97 | if (!ts->marked) |
| 110 | ts->marked = 2; /* avoid GC */ | 98 | ts->marked = 2; /* avoid (temporarily) GC */ |
| 111 | return ts; | 99 | return ts; |
| 112 | } | 100 | } |
| 113 | 101 | ||
| @@ -131,9 +119,6 @@ static char *lua_travsymbol (int (*fn)(TObject *)) | |||
| 131 | } | 119 | } |
| 132 | 120 | ||
| 133 | 121 | ||
| 134 | /* | ||
| 135 | ** Mark an object if it is a string or a unmarked array. | ||
| 136 | */ | ||
| 137 | int lua_markobject (TObject *o) | 122 | int lua_markobject (TObject *o) |
| 138 | {/* if already marked, does not change mark value */ | 123 | {/* if already marked, does not change mark value */ |
| 139 | if (ttype(o) == LUA_T_USERDATA || | 124 | if (ttype(o) == LUA_T_USERDATA || |
| @@ -143,7 +128,7 @@ int lua_markobject (TObject *o) | |||
| 143 | lua_hashmark (avalue(o)); | 128 | lua_hashmark (avalue(o)); |
| 144 | else if ((o->ttype == LUA_T_FUNCTION || o->ttype == LUA_T_MARK) | 129 | else if ((o->ttype == LUA_T_FUNCTION || o->ttype == LUA_T_MARK) |
| 145 | && !o->value.tf->marked) | 130 | && !o->value.tf->marked) |
| 146 | o->value.tf->marked = 1; | 131 | luaI_funcmark(o->value.tf); |
| 147 | return 0; | 132 | return 0; |
| 148 | } | 133 | } |
| 149 | 134 | ||
| @@ -208,6 +193,7 @@ long lua_collectgarbage (long limit) | |||
| 208 | luaI_hashfree(freetable); | 193 | luaI_hashfree(freetable); |
| 209 | luaI_strfree(freestr); | 194 | luaI_strfree(freestr); |
| 210 | luaI_funcfree(freefunc); | 195 | luaI_funcfree(freefunc); |
| 196 | /*printf("total %d coletados %d\n", (int)gc_nentity, (int)recovered);*/ | ||
| 211 | return recovered; | 197 | return recovered; |
| 212 | } | 198 | } |
| 213 | 199 | ||
| @@ -1,7 +1,7 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** Module to control static tables | 2 | ** Module to control static tables |
| 3 | ** TeCGraf - PUC-Rio | 3 | ** TeCGraf - PUC-Rio |
| 4 | ** $Id: table.h,v 2.24 1997/04/07 14:48:53 roberto Exp roberto $ | 4 | ** $Id: table.h,v 2.25 1997/05/26 14:42:36 roberto Exp roberto $ |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| 7 | #ifndef table_h | 7 | #ifndef table_h |
| @@ -19,18 +19,16 @@ typedef struct | |||
| 19 | 19 | ||
| 20 | extern Symbol *lua_table; | 20 | extern Symbol *lua_table; |
| 21 | extern Word lua_ntable; | 21 | extern Word lua_ntable; |
| 22 | extern TaggedString **lua_constant; | ||
| 23 | extern Word lua_nconstant; | ||
| 24 | 22 | ||
| 25 | void luaI_initsymbol (void); | 23 | void luaI_initsymbol (void); |
| 26 | void luaI_initconstant (void); | 24 | void luaI_initconstant (void); |
| 27 | Word luaI_findsymbolbyname (char *name); | 25 | Word luaI_findsymbolbyname (char *name); |
| 28 | Word luaI_findsymbol (TaggedString *t); | 26 | Word luaI_findsymbol (TaggedString *t); |
| 29 | Word luaI_findconstant (TaggedString *t); | ||
| 30 | Word luaI_findconstantbyname (char *name); | ||
| 31 | int luaI_globaldefined (char *name); | 27 | int luaI_globaldefined (char *name); |
| 32 | void luaI_nextvar (void); | 28 | void luaI_nextvar (void); |
| 33 | TaggedString *luaI_createfixedstring (char *str); | 29 | TaggedString *luaI_createtempstring (char *name); |
| 30 | void luaI_releasestring (TaggedString *t); | ||
| 31 | void luaI_fixstring (TaggedString *t); | ||
| 34 | int lua_markobject (TObject *o); | 32 | int lua_markobject (TObject *o); |
| 35 | int luaI_ismarked (TObject *o); | 33 | int luaI_ismarked (TObject *o); |
| 36 | void lua_pack (void); | 34 | void lua_pack (void); |
| @@ -3,7 +3,7 @@ | |||
| 3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_tree="$Id: tree.c,v 1.27 1997/06/09 17:28:14 roberto Exp roberto $"; | 6 | char *rcs_tree="$Id: tree.c,v 1.28 1997/06/11 14:24:40 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | #include <string.h> | 9 | #include <string.h> |
| @@ -149,7 +149,7 @@ TaggedString *luaI_createudata (void *udata, int tag) | |||
| 149 | return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]); | 149 | return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]); |
| 150 | } | 150 | } |
| 151 | 151 | ||
| 152 | TaggedString *lua_createstring (char *str) | 152 | TaggedString *luaI_createstring (char *str) |
| 153 | { | 153 | { |
| 154 | return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]); | 154 | return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]); |
| 155 | } | 155 | } |
| @@ -1,7 +1,7 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** tree.h | 2 | ** tree.h |
| 3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
| 4 | ** $Id: tree.h,v 1.17 1997/05/14 18:38:29 roberto Exp roberto $ | 4 | ** $Id: tree.h,v 1.18 1997/06/09 17:28:14 roberto Exp roberto $ |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| 7 | #ifndef tree_h | 7 | #ifndef tree_h |
| @@ -19,7 +19,7 @@ typedef struct TaggedString | |||
| 19 | union { | 19 | union { |
| 20 | struct { | 20 | struct { |
| 21 | Word varindex; /* != NOT_USED if this is a symbol */ | 21 | Word varindex; /* != NOT_USED if this is a symbol */ |
| 22 | Word constindex; /* != NOT_USED if this is a constant */ | 22 | Word constindex; /* hint to reuse constant indexes */ |
| 23 | } s; | 23 | } s; |
| 24 | void *v; /* if this is a userdata, here is its value */ | 24 | void *v; /* if this is a userdata, here is its value */ |
| 25 | } u; | 25 | } u; |
| @@ -29,7 +29,7 @@ typedef struct TaggedString | |||
| 29 | } TaggedString; | 29 | } TaggedString; |
| 30 | 30 | ||
| 31 | 31 | ||
| 32 | TaggedString *lua_createstring (char *str); | 32 | TaggedString *luaI_createstring (char *str); |
| 33 | TaggedString *luaI_createudata (void *udata, int tag); | 33 | TaggedString *luaI_createudata (void *udata, int tag); |
| 34 | TaggedString *luaI_strcollector (long *cont); | 34 | TaggedString *luaI_strcollector (long *cont); |
| 35 | void luaI_strfree (TaggedString *l); | 35 | void luaI_strfree (TaggedString *l); |
