3 ** load bytecodes from files
6 char* rcs_undump
="$Id: undump.c,v 1.24 1997/06/17 18:19:17 roberto Exp $";
17 static int swapword
=0;
18 static int swapfloat
=0;
19 static TFunc
* Main
=NULL
; /* functions in a chunk */
20 static TFunc
* lastF
=NULL
;
22 static void FixCode(Byte
* code
, Byte
* end
) /* swap words */
90 p
+=5; /* TODO: use sizeof(TFunc*) or old? */
107 t
=p
[1]; p
[1]=p
[2]; p
[2]=t
;
111 case PUSHFLOAT
: /* assumes sizeof(float)==4 */
114 t
=p
[1]; p
[1]=p
[4]; p
[4]=t
;
115 t
=p
[2]; p
[2]=p
[3]; p
[3]=t
;
126 t
=p
[0]; p
[0]=p
[1]; p
[1]=t
;
132 luaL_verror("corrupt binary file: bad opcode %d at %d\n",
139 static void Unthread(Byte
* code
, int i
, int v
)
145 memcpy(&w
,p
,sizeof(w
));
147 memcpy(p
,&w
,sizeof(w
));
151 static int LoadWord(ZIO
* Z
)
154 zread(Z
,&w
,sizeof(w
));
159 t
=p
[0]; p
[0]=p
[1]; p
[1]=t
;
164 static int LoadSize(ZIO
* Z
)
169 if ((Word
)s
!= s
) lua_error("code too long");
173 static void* LoadBlock(int size
, ZIO
* Z
)
175 void* b
=luaI_malloc(size
);
180 static char* LoadString(ZIO
* Z
)
182 int size
=LoadWord(Z
);
183 char *b
=luaI_buffer(size
);
188 static char* LoadNewString(ZIO
* Z
)
190 return LoadBlock(LoadWord(Z
),Z
);
193 static void LoadFunction(ZIO
* Z
)
195 TFunc
* tf
=new(TFunc
);
198 tf
->size
=LoadSize(Z
);
199 tf
->lineDefined
=LoadWord(Z
);
200 if (IsMain(tf
)) /* new main */
202 tf
->fileName
=LoadNewString(Z
);
205 else /* fix PUSHFUNCTION */
207 tf
->marked
=LoadWord(Z
);
208 tf
->fileName
=Main
->fileName
;
209 memcpy(Main
->code
+tf
->marked
,&tf
,sizeof(tf
));
210 lastF
=lastF
->next
=tf
;
212 tf
->code
=LoadBlock(tf
->size
,Z
);
213 if (swapword
|| swapfloat
) FixCode(tf
->code
,tf
->code
+tf
->size
);
214 while (1) /* unthread */
217 if (c
==ID_VAR
) /* global var */
220 char* s
=LoadString(Z
);
221 int v
=luaI_findsymbolbyname(s
);
222 Unthread(tf
->code
,i
,v
);
224 else if (c
==ID_STR
) /* constant string */
227 char* s
=LoadString(Z
);
228 int v
=luaI_findconstantbyname(s
);
229 Unthread(tf
->code
,i
,v
);
239 static void LoadSignature(ZIO
* Z
)
242 while (*s
!=0 && zgetc(Z
)==*s
)
244 if (*s
!=0) lua_error("cannot load binary file: bad signature");
247 static void LoadHeader(ZIO
* Z
)
250 float f
,tf
=TEST_FLOAT
;
254 if (version
>0x23) /* after 2.5 */
256 int oldsizeofW
=zgetc(Z
);
257 int oldsizeofF
=zgetc(Z
);
258 int oldsizeofP
=zgetc(Z
);
261 "cannot load binary file created on machine with sizeof(Word)=%d; "
262 "expected 2",oldsizeofW
);
265 "cannot load binary file created on machine with sizeof(float)=%d; "
266 "expected 4\nnot an IEEE machine?",oldsizeofF
);
267 if (oldsizeofP
!=sizeof(TFunc
*)) /* TODO: pack? */
269 "cannot load binary file created on machine with sizeof(TFunc*)=%d; "
270 "expected %d",oldsizeofP
,(int)sizeof(TFunc
*));
272 zread(Z
,&w
,sizeof(w
)); /* test word */
277 zread(Z
,&f
,sizeof(f
)); /* test float */
283 t
=p
[0]; p
[0]=p
[3]; p
[3]=t
;
284 t
=p
[1]; p
[1]=p
[2]; p
[2]=t
;
285 if (f
!=tf
) /* TODO: try another perm? */
286 lua_error("cannot load binary file: unknown float representation");
290 static void LoadChunk(ZIO
* Z
)
296 if (c
==ID_FUN
) LoadFunction(Z
); else { zungetc(Z
); break; }
301 ** load one chunk from a file.
302 ** return list of functions found, headed by main, or NULL at EOF.
304 TFunc
* luaI_undump1(ZIO
* Z
)
313 lua_error("not a lua binary file");
318 ** load and run all chunks in a file
320 int luaI_undump(ZIO
* Z
)
323 while ((m
=luaI_undump1(Z
)))
325 int status
=luaI_dorun(m
);
327 if (status
!=0) return status
;