f32c1f791d4f86ede051be9dfba589342f15cf81
[swftools.git] / lib / as3 / mklib.c
1 /* mklib.c
2
3    File to generate builtin.c
4
5    Copyright (c) 2008,2009 Matthias Kramm <kramm@quiss.org>
6  
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <memory.h>
25 #include "../rfxswf.h"
26 #include "../os.h"
27 #include "../q.h"
28 #include "pool.h"
29 #include "files.h"
30 #include "tokenizer.h"
31 #include "parser.tab.h"
32 #include "parser.h"
33 #include "import.h"
34
35 void fixstring(char*s)
36 {
37     char first=1;
38     for(;*s;s++) {
39         if(!((*s>='a' && *s<='z') || (*s>='A' && *s<='Z') || 
40                     (*s>='0' && *s<='9' && !first))) {
41             *s = '_';
42         }
43         first = 0;
44     }
45 }
46
47 static char* mkpid(const char*package)
48 {
49     char*id = malloc(strlen(package)+20);
50     strcpy(id, "package_");
51     if(!*package) 
52         strcat(id, "global");
53     else
54         strcat(id, package);
55     fixstring(id);
56     return id;
57 }
58 static char* mkcid(const char*package, const char*name)
59 {
60     char*id = malloc(strlen(package)+strlen(name)+10);
61     strcpy(id, package);
62     strcat(id, "_");
63     strcat(id, name);
64     fixstring(id);
65     return id;
66 }
67 static char* mkptr2(const char*package, const char*name)
68 {
69     char*id = malloc(strlen(package)+strlen(name)+10);
70     strcpy(id, "&");
71     strcat(id, package);
72     strcat(id, "_");
73     strcat(id, name);
74     fixstring(id+1);
75     return id;
76 }
77 static char* mkid2(const char*cls, const char*member)
78 {
79     char*id = malloc(strlen(cls)+strlen(member)+10);
80     strcpy(id, cls);
81     strcat(id, "_");
82     strcat(id, member);
83     fixstring(id);
84     return id;
85 }
86 #define mkid(s) ((s)?mkcid((s)->package, (s)->name):"0")
87 #define mkptr(s) ((s)?mkptr2((s)->package, (s)->name):"0")
88
89 static array_t*tosort=0;
90 static int compare_classes(const void*v1, const void*v2)
91 {
92     int x1 = *(int*)v1;
93     int x2 = *(int*)v2;
94     abc_class_t*c1 = array_getvalue(tosort, x1);
95     abc_class_t*c2 = array_getvalue(tosort, x2);
96     int i = strcmp(c1->classname->ns->name, c2->classname->ns->name);
97     if(i)
98         return i;
99     return strcmp(c1->classname->name, c2->classname->name);
100 }
101
102 static int compare_traits(const void*v1, const void*v2)
103 {
104     trait_t* x1 = *(trait_t**)v1;
105     trait_t* x2 = *(trait_t**)v2;
106     int i = strcmp(x1->name->ns->name, x2->name->ns->name);
107     if(i)
108         return i;
109     return strcmp(x1->name->name, x2->name->name);
110 }
111
112 dict_t* builtin_getclasses()
113 {
114     return dict_new2(&slotinfo_type);
115 }
116
117 extern dict_t*registry_classes;
118
119 char*mktype(slotinfo_t*s) 
120 {
121     if(s->kind == INFOTYPE_CLASS) {
122         return "classinfo_t";
123     } else if(s->kind == INFOTYPE_METHOD) {
124         return "methodinfo_t";
125     } else if(s->kind == INFOTYPE_VAR) {
126         return "varinfo_t";
127     }
128     return "**ERROR**";
129 }
130
131 void write_slotinfo(FILE*fi, slotinfo_t*s, char*id, char*prefix);
132
133 void write_slotinfo_decl(FILE*fi, slotinfo_t*s, char*prefix)
134 {
135     fprintf(fi, "%s", prefix);
136     char*id = mkid(s);
137     fprintf(fi, "static %s %s;\n", mktype(s), id);
138
139     if(s->kind == INFOTYPE_CLASS) {
140         classinfo_t*c = (classinfo_t*)s;
141         dict_t*d = &c->members;
142         int t;
143         for(t=0;t<d->hashsize;t++) {
144             dictentry_t*l = d->slots[t];
145             while(l) {
146                 slotinfo_t*s2 = (slotinfo_t*)l->data;
147                 fprintf(fi, "static %s %s;\n", mktype(s2), mkid2(id, s2->name));
148                 l = l->next;
149             }
150         }
151     }
152 }
153 void write_initinfo(FILE*fi, slotinfo_t*s, char*prefix)
154 {
155     if(s->kind == INFOTYPE_CLASS) {
156         classinfo_t*c = (classinfo_t*)s;
157         fprintf(fi, "%s", prefix);
158         char*id = mkid(c);
159         dict_t*d = &c->members;
160         fprintf(fi, "dict_init2(&%s.members, &memberinfo_type, %d);\n", id, d->hashsize);
161         int t;
162         for(t=0;t<d->hashsize;t++) {
163             dictentry_t*l = d->slots[t];
164             while(l) {
165                 slotinfo_t*s2 = (slotinfo_t*)l->data;
166                 fprintf(fi, "%s", prefix);
167                 char*id2 = mkid2(id, s2->name);
168                 fprintf(fi, "dict_put(&%s.members, &%s, &%s);\n", id, id2,id2);
169                 l = l->next;
170             }
171         }
172     }
173 }
174
175 void write_constant(FILE*fi, constant_t*value, char*id, char*prefix)
176 {
177     if(NS_TYPE(value->type)) {
178         fprintf(fi, "%s", prefix);
179         fprintf(fi, "static namespace_t %s_constant_ns = {0x%02x, \"%s\"};\n", id, value->ns->access, value->ns->name);
180     } else if(value->type == CONSTANT_STRING) {
181         fprintf(fi, "%s", prefix);
182         fprintf(fi, "static string_t %s_constant_s = {\"%s\", %d};\n", id, value->s->str, value->s->len);
183     }
184     fprintf(fi, "%s", prefix);
185     fprintf(fi, "static constant_t %s_constant = ", id);
186     fprintf(fi, "{type: %d", value->type);
187     if(NS_TYPE(value->type)) {
188         fprintf(fi, ", &%s_constant_ns", id);
189     } else if(value->type == CONSTANT_INT) {
190         fprintf(fi, ",i: %d,", value->type);
191     } else if(value->type == CONSTANT_UINT) {
192         fprintf(fi, ",u: %u", value->u);
193     } else if(value->type == CONSTANT_FLOAT) {
194         if(!isnan(value->f) && !isinf(value->f))
195             fprintf(fi, ", %f", value->f);
196     } else if(value->type == CONSTANT_STRING) {
197         fprintf(fi, ", &%s_constant_s", id);
198     }
199     fprintf(fi, "};\n");
200 }
201
202 void write_slotinfo(FILE*fi, slotinfo_t*s, char*id, char*prefix)
203 {
204     if(s->kind == INFOTYPE_VAR) {
205         varinfo_t*v = (varinfo_t*)s;
206         if(v->value) {
207             write_constant(fi, v->value, id, prefix);
208         }
209     }
210     fprintf(fi, "%s", prefix);
211     fprintf(fi, "static %s %s = {", mktype(s), id);
212     fprintf(fi, "0x%02x, 0x%02x, 0x%02x, 0x%02x, ", s->kind, s->subtype, s->flags, s->access);
213     if(s->package)
214         fprintf(fi, "\"%s\", ", s->package);
215     else
216         fprintf(fi, "0, ");
217     
218     if(s->name)
219         fprintf(fi, "\"%s\", ", s->name);
220     else
221         fprintf(fi, "0, ");
222
223     fprintf(fi, "%d, ", s->slot);
224
225     if(s->kind == INFOTYPE_CLASS) {
226         classinfo_t*c = (classinfo_t*)s;
227         fprintf(fi, "%s, ", mkptr(c->superclass));
228         fprintf(fi, "interfaces: {");
229         int t;
230         for(t=0;c->interfaces[t];t++) {
231             fprintf(fi, "%s", mkptr(c->interfaces[t]));
232             fprintf(fi, ", ");
233         }
234         fprintf(fi, "0}};\n");
235     }
236     if(s->kind == INFOTYPE_METHOD) {
237         methodinfo_t*m = (methodinfo_t*)s;
238         fprintf(fi, "%s, ", mkptr(m->return_type));
239         fprintf(fi, "%s, ", mkptr(m->parent));
240         fprintf(fi, "0"); // params TODO
241         fprintf(fi, "};\n");
242     }
243     if(s->kind == INFOTYPE_VAR) {
244         varinfo_t*m = (varinfo_t*)s;
245         fprintf(fi, "%s, ", mkptr(m->type));
246         fprintf(fi, "%s, ", mkptr(m->parent));
247         if(!m->value) fprintf(fi, "0");
248         else          fprintf(fi, "&%s_constant", id);
249         fprintf(fi, "};\n");
250     }
251     
252     if(s->kind == INFOTYPE_CLASS) {
253         classinfo_t*c = (classinfo_t*)s;
254         dict_t*d = &c->members;
255         int t;
256         for(t=0;t<d->hashsize;t++) {
257             dictentry_t*l = d->slots[t];
258             while(l) {
259                 slotinfo_t*s2 = (slotinfo_t*)l->data;
260                 write_slotinfo(fi, s2, mkid2(id,s2->name), prefix);
261                 l = l->next;
262             }
263         }
264     }
265 }
266
267 int main()
268 {
269     registry_classes = builtin_getclasses();
270
271     as3_import_abc("/home/kramm/c/swftools/lib/as3/builtin.abc");
272     as3_import_abc("/home/kramm/c/swftools/lib/as3/playerglobal.abc");
273
274     FILE*fi = fopen("builtin.c", "wb");
275
276     int t;
277     int pass;
278
279
280     for(pass=1;pass<=3;pass++) {
281         if(pass==1) {
282             fprintf(fi, "#include \"builtin.h\"\n");
283             fprintf(fi, "\n");
284         }
285         if(pass==3) {
286             fprintf(fi, "dict_t* builtin_getclasses()\n");
287             fprintf(fi, "{\n");
288             fprintf(fi, "    dict_t*d = dict_new2(&slotinfo_type);\n");
289         }
290         for(t=0;t<registry_classes->hashsize;t++) {
291             dictentry_t*l = registry_classes->slots[t];
292             while(l) {
293                 slotinfo_t*s = (slotinfo_t*)l->key;
294                 //printf("%08x %s %s\n", s, s->package, s->name);
295                 char*id = mkid(s);
296                 if(pass==1) {
297                     write_slotinfo_decl(fi, s, "");
298                 }
299                 if(pass==2) {
300                     write_slotinfo(fi, s, mkid(s), "");
301                 }
302                 if(pass==3) {
303                     fprintf(fi, "    dict_put(d, &%s, &%s);\n", id, id);
304                     write_initinfo(fi, s, "    ");
305                 }
306                 l = l->next;
307             }
308         }
309     }
310     fprintf(fi, "    _NaN_constant.f =  __builtin_nan(\"\");\n");
311     fprintf(fi, "    _Infinity_constant.f = __builtin_inf();\n");
312     fprintf(fi, "    return d;\n");
313     fprintf(fi, "}\n");
314     return 0;
315 }