aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-09-20 15:43:54 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-09-20 15:43:54 -0300
commitd742b88fa1afdeb47c6aff4a623a98425088dbda (patch)
treeda5cdea5484b0ffb41fc630739ca648e95e43e17
parenteb822c314a4e369006b4535f12ca63ea717b5e67 (diff)
downloadlua-d742b88fa1afdeb47c6aff4a623a98425088dbda.tar.gz
lua-d742b88fa1afdeb47c6aff4a623a98425088dbda.tar.bz2
lua-d742b88fa1afdeb47c6aff4a623a98425088dbda.zip
final version (by lhf)
-rw-r--r--lundump.c68
1 files changed, 36 insertions, 32 deletions
diff --git a/lundump.c b/lundump.c
index 642971d9..376a3685 100644
--- a/lundump.c
+++ b/lundump.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lundump.c,v 1.29 2000/06/28 14:12:55 lhf Exp lhf $ 2** $Id: lundump.c,v 1.31 2000/09/19 18:18:38 lhf Exp $
3** load bytecodes from files 3** load bytecodes from files
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -13,11 +13,9 @@
13#include "lstring.h" 13#include "lstring.h"
14#include "lundump.h" 14#include "lundump.h"
15 15
16#define LoadVector(L,b,n,s,Z) LoadBlock(L,b,(n)*(s),Z)
17#define LoadBlock(L,b,size,Z) ezread(L,Z,b,size)
18#define LoadByte ezgetc 16#define LoadByte ezgetc
19 17
20static const char* ZNAME(ZIO* Z) 18static const char* ZNAME (ZIO* Z)
21{ 19{
22 const char* s=zname(Z); 20 const char* s=zname(Z);
23 return (*s=='@') ? s+1 : s; 21 return (*s=='@') ? s+1 : s;
@@ -41,40 +39,53 @@ static void ezread (lua_State* L, ZIO* Z, void* b, int n)
41 if (r!=0) unexpectedEOZ(L,Z); 39 if (r!=0) unexpectedEOZ(L,Z);
42} 40}
43 41
44static void LoadReverse (lua_State* L, void* b, size_t size, ZIO* Z) 42static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
45{ 43{
46 char *p=(char *) b+size; 44 if (swap)
47 int n=size; 45 {
48 while (n--) *p--=ezgetc(L,Z); 46 char *p=(char *) b+size-1;
47 int n=size;
48 while (n--) *p--=(char)ezgetc(L,Z);
49 }
50 else
51 ezread(L,Z,b,size);
49} 52}
50 53
51static int LoadInt (lua_State* L, ZIO* Z, int swap) 54static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
52{ 55{
53 int x;
54 if (swap) 56 if (swap)
55 LoadReverse(L,&x,sizeof(x),Z); 57 {
58 char *q=(char *) b;
59 while (m--)
60 {
61 char *p=q+size-1;
62 int n=size;
63 while (n--) *p--=(char)ezgetc(L,Z);
64 q+=size;
65 }
66 }
56 else 67 else
57 LoadBlock(L,&x,sizeof(x),Z); 68 ezread(L,Z,b,m*size);
69}
70
71static int LoadInt (lua_State* L, ZIO* Z, int swap)
72{
73 int x;
74 LoadBlock(L,&x,sizeof(x),Z,swap);
58 return x; 75 return x;
59} 76}
60 77
61static size_t LoadSize (lua_State* L, ZIO* Z, int swap) 78static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
62{ 79{
63 size_t x; 80 size_t x;
64 if (swap) 81 LoadBlock(L,&x,sizeof(x),Z,swap);
65 LoadReverse(L,&x,sizeof(x),Z);
66 else
67 LoadBlock(L,&x,sizeof(x),Z);
68 return x; 82 return x;
69} 83}
70 84
71static Number LoadNumber (lua_State* L, ZIO* Z, int swap) 85static Number LoadNumber (lua_State* L, ZIO* Z, int swap)
72{ 86{
73 Number x; 87 Number x;
74 if (swap) 88 LoadBlock(L,&x,sizeof(x),Z,swap);
75 LoadReverse(L,&x,sizeof(x),Z);
76 else
77 LoadBlock(L,&x,sizeof(x),Z);
78 return x; 89 return x;
79} 90}
80 91
@@ -86,7 +97,7 @@ static TString* LoadString (lua_State* L, ZIO* Z, int swap)
86 else 97 else
87 { 98 {
88 char* s=luaO_openspace(L,size); 99 char* s=luaO_openspace(L,size);
89 LoadBlock(L,s,size,Z); 100 LoadBlock(L,s,size,Z,0);
90 return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */ 101 return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
91 } 102 }
92} 103}
@@ -95,10 +106,7 @@ static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
95{ 106{
96 int size=LoadInt(L,Z,swap); 107 int size=LoadInt(L,Z,swap);
97 tf->code=luaM_newvector(L,size,Instruction); 108 tf->code=luaM_newvector(L,size,Instruction);
98 LoadVector(L,tf->code,size,sizeof(*tf->code),Z); 109 LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
99#if 0
100 if (swap) SwapBytes(tf->code,sizeof(*tf->code),size);
101#endif
102 if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.255s'",ZNAME(Z)); 110 if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.255s'",ZNAME(Z));
103} 111}
104 112
@@ -120,10 +128,7 @@ static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
120 int n=LoadInt(L,Z,swap); 128 int n=LoadInt(L,Z,swap);
121 if (n==0) return; 129 if (n==0) return;
122 tf->lineinfo=luaM_newvector(L,n,int); 130 tf->lineinfo=luaM_newvector(L,n,int);
123 LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z); 131 LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
124#if 0
125 if (swap) SwapBytes(tf->lineinfo,sizeof(*tf->lineinfo),n);
126#endif
127} 132}
128 133
129static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap); 134static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
@@ -137,9 +142,7 @@ static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
137 tf->kstr[i]=LoadString(L,Z,swap); 142 tf->kstr[i]=LoadString(L,Z,swap);
138 tf->nknum=n=LoadInt(L,Z,swap); 143 tf->nknum=n=LoadInt(L,Z,swap);
139 tf->knum=luaM_newvector(L,n,Number); 144 tf->knum=luaM_newvector(L,n,Number);
140 LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z); 145 LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
141 if (swap)
142 for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z,swap); /* TODO */
143 tf->nkproto=n=LoadInt(L,Z,swap); 146 tf->nkproto=n=LoadInt(L,Z,swap);
144 tf->kproto=luaM_newvector(L,n,Proto*); 147 tf->kproto=luaM_newvector(L,n,Proto*);
145 for (i=0; i<n; i++) 148 for (i=0; i<n; i++)
@@ -195,6 +198,7 @@ static int LoadHeader (lua_State* L, ZIO* Z)
195 " read version %d.%d; expected at least %d.%d", 198 " read version %d.%d; expected at least %d.%d",
196 ZNAME(Z),V(version),V(VERSION)); 199 ZNAME(Z),V(version),V(VERSION));
197 swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */ 200 swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
201if (swap) puts("swap!");
198 TESTSIZE(sizeof(int)); 202 TESTSIZE(sizeof(int));
199 TESTSIZE(sizeof(size_t)); 203 TESTSIZE(sizeof(size_t));
200 TESTSIZE(sizeof(Instruction)); 204 TESTSIZE(sizeof(Instruction));