diff options
author | Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> | 1996-03-01 00:43:50 -0300 |
---|---|---|
committer | Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> | 1996-03-01 00:43:50 -0300 |
commit | cec1ffb80b6d482c4d66ce2b56fe5d8c862253b0 (patch) | |
tree | 833d921f9a37400d4a99a8c459e47fc7a4d27de6 | |
parent | 870967ca773c95909f8bccc3307bdac9cf389fc5 (diff) | |
download | lua-cec1ffb80b6d482c4d66ce2b56fe5d8c862253b0.tar.gz lua-cec1ffb80b6d482c4d66ce2b56fe5d8c862253b0.tar.bz2 lua-cec1ffb80b6d482c4d66ce2b56fe5d8c862253b0.zip |
uses ID_* constants
simplified signature checking
loads local vars
-rw-r--r-- | undump.c | 55 |
1 files changed, 40 insertions, 15 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** load bytecodes from files | 3 | ** load bytecodes from files |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_undump="$Id: undump.c,v 1.5 1996/02/26 19:44:17 lhf Exp lhf $"; | 6 | char *rcs_undump="$Id: undump.c,v 1.6 1996/02/28 23:10:46 lhf Exp lhf $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <string.h> | 9 | #include <string.h> |
@@ -11,7 +11,7 @@ char *rcs_undump="$Id: undump.c,v 1.5 1996/02/26 19:44:17 lhf Exp lhf $"; | |||
11 | 11 | ||
12 | static void warn(char *s) /* TODO: remove */ | 12 | static void warn(char *s) /* TODO: remove */ |
13 | { | 13 | { |
14 | fprintf(stderr,"luac: %s\n",s); | 14 | fprintf(stderr,"undump: %s\n",s); |
15 | } | 15 | } |
16 | 16 | ||
17 | static void panic(char *s) /* TODO: remove */ | 17 | static void panic(char *s) /* TODO: remove */ |
@@ -73,45 +73,67 @@ static void LoadFunction(FILE *D) | |||
73 | Byte *p; | 73 | Byte *p; |
74 | tf->marked=LoadWord(D); | 74 | tf->marked=LoadWord(D); |
75 | tf->fileName=Main->fileName; | 75 | tf->fileName=Main->fileName; |
76 | p=Main->code+tf->marked; /* TODO: tf->marked=? */ | 76 | p=Main->code+tf->marked; |
77 | c.tf=tf; | 77 | c.tf=tf; |
78 | *p++=c.m.c1; *p++=c.m.c2; *p++=c.m.c3; *p++=c.m.c4; | 78 | *p++=c.m.c1; *p++=c.m.c2; *p++=c.m.c3; *p++=c.m.c4; |
79 | lastF->next=tf; lastF=tf; | 79 | lastF=lastF->next=tf; |
80 | } | 80 | } |
81 | #if 0 | ||
82 | tf->marked=0; /* TODO: is this ok? */ | ||
83 | #endif | ||
81 | tf->code=LoadBlock(tf->size,D); | 84 | tf->code=LoadBlock(tf->size,D); |
82 | while (1) /* unthread */ | 85 | while (1) /* unthread */ |
83 | { | 86 | { |
84 | int c=getc(D); | 87 | int c=getc(D); |
85 | if (c=='V') | 88 | if (c==ID_VAR) /* global var */ |
86 | { | 89 | { |
87 | int i=LoadWord(D); | 90 | int i=LoadWord(D); |
88 | char *s=LoadString(D); | 91 | char *s=LoadString(D); |
89 | int v=luaI_findsymbolbyname(s); /* TODO: free s? */ | 92 | int v=luaI_findsymbolbyname(s); /* TODO: free s? */ |
90 | Unthread(tf->code,i,v); | 93 | Unthread(tf->code,i,v); |
91 | } | 94 | } |
92 | else if (c=='S') | 95 | else if (c==ID_STR) /* constant string */ |
93 | { | 96 | { |
94 | int i=LoadWord(D); | 97 | int i=LoadWord(D); |
95 | char *s=LoadString(D); | 98 | char *s=LoadString(D); |
96 | int v=luaI_findconstantbyname(s); /* TODO: free s? */ | 99 | int v=luaI_findconstantbyname(s); /* TODO: free s? */ |
97 | Unthread(tf->code,i,v); | 100 | Unthread(tf->code,i,v); |
98 | } | 101 | } |
102 | else if (c==ID_LOC) /* local vars */ | ||
103 | { | ||
104 | int n=LoadWord(D); | ||
105 | LocVar *v; | ||
106 | if (n==0) continue; | ||
107 | tf->locvars=v=newvector(n,LocVar); | ||
108 | while (n--) | ||
109 | { | ||
110 | LocLoc(v)=LoadWord(D); | ||
111 | LoadString(D); /* TODO: how to save it? */ | ||
112 | ++v; | ||
113 | } | ||
114 | } | ||
99 | else | 115 | else |
100 | { | 116 | { |
101 | ungetc(c,D); | 117 | ungetc(c,D); |
102 | return; | 118 | break; |
103 | } | 119 | } |
104 | } | 120 | } |
105 | } | 121 | } |
106 | 122 | ||
123 | static void LoadSignature(FILE *D) | ||
124 | { | ||
125 | char *s=SIGNATURE; | ||
126 | while (*s!=0 && getc(D)==*s) | ||
127 | ++s; | ||
128 | if (*s!=0) panic("bad signature"); | ||
129 | } | ||
130 | |||
107 | static void LoadHeader(FILE *D) /* TODO: error handling */ | 131 | static void LoadHeader(FILE *D) /* TODO: error handling */ |
108 | { | 132 | { |
109 | char *s=LoadString(D); | ||
110 | Word w,tw=TEST_WORD; | 133 | Word w,tw=TEST_WORD; |
111 | float f,tf=TEST_FLOAT; | 134 | float f,tf=TEST_FLOAT; |
112 | if (strcmp(s,SIGNATURE)!=0) panic("bad signature"); | 135 | LoadSignature(D); |
113 | luaI_free(s); | 136 | getc(D); /* skip version */ |
114 | getc(D); /* skip version */ | ||
115 | fread(&w,sizeof(w),1,D); /* a word for testing byte ordering */ | 137 | fread(&w,sizeof(w),1,D); /* a word for testing byte ordering */ |
116 | if (w!=tw) warn("different byte order"); | 138 | if (w!=tw) warn("different byte order"); |
117 | fread(&f,sizeof(f),1,D); /* a float for testing byte ordering */ | 139 | fread(&f,sizeof(f),1,D); /* a float for testing byte ordering */ |
@@ -124,7 +146,7 @@ static void LoadChunk(FILE *D) | |||
124 | while (1) | 146 | while (1) |
125 | { | 147 | { |
126 | int c=getc(D); | 148 | int c=getc(D); |
127 | if (c=='F') LoadFunction(D); else { ungetc(c,D); break; } | 149 | if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; } |
128 | } | 150 | } |
129 | #if 1 | 151 | #if 1 |
130 | { /* TODO: run Main? */ | 152 | { /* TODO: run Main? */ |
@@ -140,9 +162,12 @@ void luaI_undump(FILE *D) | |||
140 | while (1) | 162 | while (1) |
141 | { | 163 | { |
142 | int c=getc(D); | 164 | int c=getc(D); |
143 | if (c==ESC) LoadChunk(D); else | 165 | if (c==ID_CHUNK) |
144 | if (c==EOF) return; else | 166 | LoadChunk(D); |
145 | panic("not a lua binary file"); | 167 | else if (c==EOF) |
168 | break; | ||
169 | else | ||
170 | panic("not a lua binary file"); | ||
146 | } | 171 | } |
147 | } | 172 | } |
148 | 173 | ||