diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-11-16 09:55:07 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-11-16 09:55:07 -0200 |
| commit | 2753134a3800eb28185502d68c28d9225420bd04 (patch) | |
| tree | baadf08136338a8a5b1d4a7545d786ccfe5fb43a /ldump.c | |
| parent | cbbde11a8ab1cda25fa36533739e4ca1efd1916c (diff) | |
| download | lua-2753134a3800eb28185502d68c28d9225420bd04.tar.gz lua-2753134a3800eb28185502d68c28d9225420bd04.tar.bz2 lua-2753134a3800eb28185502d68c28d9225420bd04.zip | |
new versions by lhf
Diffstat (limited to 'ldump.c')
| -rw-r--r-- | ldump.c | 129 |
1 files changed, 57 insertions, 72 deletions
| @@ -1,6 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldump.c,v 2.5 2005/05/05 20:47:02 roberto Exp roberto $ | 2 | ** $Id: ldump.c,v 1.13 2005/11/01 17:04:55 lhf Exp lhf $ |
| 3 | ** save pre-compiled Lua chunks | 3 | ** save precompiled Lua chunks |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| @@ -12,13 +12,9 @@ | |||
| 12 | #include "lua.h" | 12 | #include "lua.h" |
| 13 | 13 | ||
| 14 | #include "lobject.h" | 14 | #include "lobject.h" |
| 15 | #include "lopcodes.h" | ||
| 16 | #include "lstate.h" | 15 | #include "lstate.h" |
| 17 | #include "lundump.h" | 16 | #include "lundump.h" |
| 18 | 17 | ||
| 19 | #define DumpVector(b,n,size,D) DumpBlock(b,(n)*(size),D) | ||
| 20 | #define DumpLiteral(s,D) DumpBlock("" s,(sizeof(s))-1,D) | ||
| 21 | |||
| 22 | typedef struct { | 18 | typedef struct { |
| 23 | lua_State* L; | 19 | lua_State* L; |
| 24 | lua_Writer writer; | 20 | lua_Writer writer; |
| @@ -27,6 +23,9 @@ typedef struct { | |||
| 27 | int status; | 23 | int status; |
| 28 | } DumpState; | 24 | } DumpState; |
| 29 | 25 | ||
| 26 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) | ||
| 27 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) | ||
| 28 | |||
| 30 | static void DumpBlock(const void* b, size_t size, DumpState* D) | 29 | static void DumpBlock(const void* b, size_t size, DumpState* D) |
| 31 | { | 30 | { |
| 32 | if (D->status==0) | 31 | if (D->status==0) |
| @@ -37,135 +36,121 @@ static void DumpBlock(const void* b, size_t size, DumpState* D) | |||
| 37 | } | 36 | } |
| 38 | } | 37 | } |
| 39 | 38 | ||
| 40 | static void DumpByte(int y, DumpState* D) | 39 | static void DumpChar(int y, DumpState* D) |
| 41 | { | 40 | { |
| 42 | char x=(char)y; | 41 | char x=(char)y; |
| 43 | DumpBlock(&x,sizeof(x),D); | 42 | DumpVar(x,D); |
| 44 | } | 43 | } |
| 45 | 44 | ||
| 46 | static void DumpInt(int x, DumpState* D) | 45 | static void DumpInt(int x, DumpState* D) |
| 47 | { | 46 | { |
| 48 | DumpBlock(&x,sizeof(x),D); | 47 | DumpVar(x,D); |
| 49 | } | 48 | } |
| 50 | 49 | ||
| 51 | static void DumpSize(size_t x, DumpState* D) | 50 | static void DumpNumber(lua_Number x, DumpState* D) |
| 52 | { | 51 | { |
| 53 | DumpBlock(&x,sizeof(x),D); | 52 | DumpVar(x,D); |
| 54 | } | 53 | } |
| 55 | 54 | ||
| 56 | static void DumpNumber(lua_Number x, DumpState* D) | 55 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) |
| 57 | { | 56 | { |
| 58 | DumpBlock(&x,sizeof(x),D); | 57 | DumpInt(n,D); |
| 58 | DumpMem(b,n,size,D); | ||
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | static void DumpString(const TString* s, DumpState* D) | 61 | static void DumpString(const TString* s, DumpState* D) |
| 62 | { | 62 | { |
| 63 | if (s==NULL || getstr(s)==NULL) | 63 | if (s==NULL || getstr(s)==NULL) |
| 64 | DumpSize(0,D); | 64 | { |
| 65 | size_t size=0; | ||
| 66 | DumpVar(size,D); | ||
| 67 | } | ||
| 65 | else | 68 | else |
| 66 | { | 69 | { |
| 67 | size_t size=s->tsv.len+1; /* include trailing '\0' */ | 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ |
| 68 | DumpSize(size,D); | 71 | DumpVar(size,D); |
| 69 | DumpBlock(getstr(s),size,D); | 72 | DumpBlock(getstr(s),size,D); |
| 70 | } | 73 | } |
| 71 | } | 74 | } |
| 72 | 75 | ||
| 73 | static void DumpCode(const Proto* f, DumpState* D) | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) |
| 74 | { | ||
| 75 | DumpInt(f->sizecode,D); | ||
| 76 | DumpVector(f->code,f->sizecode,sizeof(*f->code),D); | ||
| 77 | } | ||
| 78 | |||
| 79 | static void DumpLocals(const Proto* f, DumpState* D) | ||
| 80 | { | ||
| 81 | int i,n=f->sizelocvars; | ||
| 82 | DumpInt(n,D); | ||
| 83 | for (i=0; i<n; i++) | ||
| 84 | { | ||
| 85 | DumpString(f->locvars[i].varname,D); | ||
| 86 | DumpInt(f->locvars[i].startpc,D); | ||
| 87 | DumpInt(f->locvars[i].endpc,D); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | static void DumpLines(const Proto* f, DumpState* D) | ||
| 92 | { | ||
| 93 | DumpInt(f->sizelineinfo,D); | ||
| 94 | DumpVector(f->lineinfo,f->sizelineinfo,sizeof(*f->lineinfo),D); | ||
| 95 | } | ||
| 96 | |||
| 97 | static void DumpUpvalues(const Proto* f, DumpState* D) | ||
| 98 | { | ||
| 99 | int i,n=f->sizeupvalues; | ||
| 100 | DumpInt(n,D); | ||
| 101 | for (i=0; i<n; i++) DumpString(f->upvalues[i],D); | ||
| 102 | } | ||
| 103 | 77 | ||
| 104 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D); | 78 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D); |
| 105 | 79 | ||
| 106 | static void DumpConstants(const Proto* f, DumpState* D) | 80 | static void DumpConstants(const Proto* f, DumpState* D) |
| 107 | { | 81 | { |
| 108 | int i,n; | 82 | int i,n=f->sizek; |
| 109 | DumpInt(n=f->sizek,D); | 83 | DumpInt(n,D); |
| 110 | for (i=0; i<n; i++) | 84 | for (i=0; i<n; i++) |
| 111 | { | 85 | { |
| 112 | const TValue* o=&f->k[i]; | 86 | const TValue* o=&f->k[i]; |
| 113 | DumpByte(ttype(o),D); | 87 | DumpChar(ttype(o),D); |
| 114 | switch (ttype(o)) | 88 | switch (ttype(o)) |
| 115 | { | 89 | { |
| 90 | case LUA_TNIL: | ||
| 91 | break; | ||
| 92 | case LUA_TBOOLEAN: | ||
| 93 | DumpChar(bvalue(o),D); | ||
| 94 | break; | ||
| 116 | case LUA_TNUMBER: | 95 | case LUA_TNUMBER: |
| 117 | DumpNumber(nvalue(o),D); | 96 | DumpNumber(nvalue(o),D); |
| 118 | break; | 97 | break; |
| 119 | case LUA_TSTRING: | 98 | case LUA_TSTRING: |
| 120 | DumpString(rawtsvalue(o),D); | 99 | DumpString(rawtsvalue(o),D); |
| 121 | break; | 100 | break; |
| 122 | case LUA_TNIL: | ||
| 123 | break; | ||
| 124 | case LUA_TBOOLEAN: | ||
| 125 | DumpByte(bvalue(o),D); | ||
| 126 | break; | ||
| 127 | default: | 101 | default: |
| 128 | lua_assert(0); /* cannot happen */ | 102 | lua_assert(0); /* cannot happen */ |
| 129 | break; | 103 | break; |
| 130 | } | 104 | } |
| 131 | } | 105 | } |
| 132 | DumpInt(n=f->sizep,D); | 106 | n=f->sizep; |
| 107 | DumpInt(n,D); | ||
| 133 | for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D); | 108 | for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D); |
| 134 | } | 109 | } |
| 135 | 110 | ||
| 111 | static void DumpDebug(const Proto* f, DumpState* D) | ||
| 112 | { | ||
| 113 | int i,n; | ||
| 114 | n= (D->strip) ? 0 : f->sizelineinfo; | ||
| 115 | DumpVector(f->lineinfo,n,sizeof(int),D); | ||
| 116 | n= (D->strip) ? 0 : f->sizelocvars; | ||
| 117 | DumpInt(n,D); | ||
| 118 | for (i=0; i<n; i++) | ||
| 119 | { | ||
| 120 | DumpString(f->locvars[i].varname,D); | ||
| 121 | DumpInt(f->locvars[i].startpc,D); | ||
| 122 | DumpInt(f->locvars[i].endpc,D); | ||
| 123 | } | ||
| 124 | n= (D->strip) ? 0 : f->sizeupvalues; | ||
| 125 | DumpInt(n,D); | ||
| 126 | for (i=0; i<n; i++) DumpString(f->upvalues[i],D); | ||
| 127 | } | ||
| 128 | |||
| 136 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D) | 129 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D) |
| 137 | { | 130 | { |
| 138 | DumpString((f->source==p) ? NULL : f->source,D); | 131 | DumpString((f->source==p) ? NULL : f->source,D); |
| 139 | DumpInt(f->linedefined,D); | 132 | DumpInt(f->linedefined,D); |
| 140 | DumpInt(f->lastlinedefined,D); | 133 | DumpInt(f->lastlinedefined,D); |
| 141 | DumpByte(f->nups,D); | 134 | DumpChar(f->nups,D); |
| 142 | DumpByte(f->numparams,D); | 135 | DumpChar(f->numparams,D); |
| 143 | DumpByte(f->is_vararg,D); | 136 | DumpChar(f->is_vararg,D); |
| 144 | DumpByte(f->maxstacksize,D); | 137 | DumpChar(f->maxstacksize,D); |
| 145 | if (D->strip) DumpInt(0,D); else DumpLines(f,D); | ||
| 146 | if (D->strip) DumpInt(0,D); else DumpLocals(f,D); | ||
| 147 | if (D->strip) DumpInt(0,D); else DumpUpvalues(f,D); | ||
| 148 | DumpConstants(f,D); | ||
| 149 | DumpCode(f,D); | 138 | DumpCode(f,D); |
| 139 | DumpConstants(f,D); | ||
| 140 | DumpDebug(f,D); | ||
| 150 | } | 141 | } |
| 151 | 142 | ||
| 152 | static void DumpHeader(DumpState* D) | 143 | static void DumpHeader(DumpState* D) |
| 153 | { | 144 | { |
| 154 | DumpLiteral(LUA_SIGNATURE,D); | 145 | char h[LUAC_HEADERSIZE]; |
| 155 | DumpByte(VERSION,D); | 146 | luaU_header(h); |
| 156 | DumpByte(luaU_endianness(),D); | 147 | DumpBlock(h,LUAC_HEADERSIZE,D); |
| 157 | DumpByte(sizeof(int),D); | ||
| 158 | DumpByte(sizeof(size_t),D); | ||
| 159 | DumpByte(sizeof(Instruction),D); | ||
| 160 | DumpByte(sizeof(lua_Number),D); | ||
| 161 | DumpNumber(TEST_NUMBER,D); | ||
| 162 | } | 148 | } |
| 163 | 149 | ||
| 164 | /* | 150 | /* |
| 165 | ** dump Lua function as precompiled chunk | 151 | ** dump Lua function as precompiled chunk |
| 166 | */ | 152 | */ |
| 167 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, | 153 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) |
| 168 | int strip) | ||
| 169 | { | 154 | { |
| 170 | DumpState D; | 155 | DumpState D; |
| 171 | D.L=L; | 156 | D.L=L; |
