added gpl header
[swftools.git] / lib / as3 / mkabc.py
1 #!/usr/bin/python
2 #
3 # mkops.py
4 #
5 # Generate opcodes.h, opcodes.h
6 #
7 # Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
22
23 import re
24
25 fi = open("code.c", "rb")
26 foc = open("opcodes.c", "wb")
27 foh = open("opcodes.h", "wb")
28
29 foh.write("#ifndef __opcodes_h__\n")
30 foh.write("#define __opcodes_h__\n")
31 foh.write("#include \"abc.h\"\n")
32 foh.write("#include \"pool.h\"\n")
33 foh.write("#include \"code.h\"\n")
34
35 foc.write("#include \"opcodes.h\"\n")
36
37 R = re.compile('{(0x..),\s*"([^"]*)"\s*,\s*"([^"]*)"[^}]*}\s*')
38
39 for line in fi.readlines():
40     line = line.strip()
41     m = R.match(line)
42     if m:
43         op,name,params = m.group(1),m.group(2),m.group(3)
44
45         iterations=1
46         if "2" in params:
47             iterations=2
48
49         for iteration in range(iterations):
50             if iteration==1:
51                 name=name+"2"
52             params = params.strip()
53             paramstr = ""
54             seen = {}
55             names = []
56
57             for c in params:
58                 paramstr += ", "
59                 if c == "2":
60                     if iteration==0:
61                         type,pname="char*","name"
62                     else:
63                         type,pname="multiname_t*","name"
64                 elif c == "s":
65                     type,pname="char*","s"
66                 elif c in "nubs":
67                     type,pname="int","v"
68                 elif c == "m":
69                     type,pname="abc_method_body_t*","m"
70                 elif c == "i":
71                     type,pname="abc_method_t*","m"
72                 elif c == "c":
73                     type,pname="abc_class_t*","m"
74                 elif c == "j":
75                     type,pname="code_t*","label"
76                 elif c == "S":
77                     type,pname="lookupswitch_t*","l"
78                 elif c == "D":
79                     type,pname="void*","debuginfo"
80                 elif c == "r":
81                     type,pname="int","reg"
82                 elif c == "f":
83                     type,pname="double","f"
84                 elif c == "I":
85                     type,pname="int","i"
86                 elif c == "U":
87                     type,pname="unsigned int","u"
88                 else:
89                     raise "Unknown type "+c
90                 paramstr += type
91                 paramstr += " "
92                 if pname in seen:
93                     seen[pname]+=1
94                     pname += str(seen[pname])
95                 else:
96                     seen[pname]=1
97                 paramstr += pname
98                 names += [pname]
99
100             foh.write("code_t* abc_%s(code_t*prev%s);\n" % (name, paramstr))
101
102             foc.write("code_t* abc_%s(code_t*prev%s)\n" % (name, paramstr))
103             foc.write("{\n")
104             foc.write("    code_t*self = add_opcode(prev, %s);\n" % op)
105             i = 0
106             for pname,c in zip(names,params):
107                 if(c == "2"):
108                     if iteration==0:
109                      foc.write("    self->data[%d] = multiname_fromstring(%s);\n" % (i,pname));
110                     else:
111                      foc.write("    self->data[%d] = multiname_clone(%s);\n" % (i,pname));
112                 elif(c in "nur"):
113                     foc.write("    self->data[%d] = (void*)(ptroff_t)%s;\n" % (i,pname))
114                 elif(c in "IU"):
115                     foc.write("    self->data[%d] = (void*)(ptroff_t)%s;\n" % (i,pname))
116                 elif(c in "f"):
117                     foc.write("    double*fp = malloc(sizeof(double));\n")
118                     foc.write("    *fp = %s;\n" % (pname))
119                     foc.write("    self->data[%d] = fp;\n" % (i))
120                 elif(c == "b"):
121                     foc.write("    self->data[%d] = (void*)(ptroff_t)%s;\n" % (i,pname))
122                 elif(c == "s"):
123                     foc.write("    self->data[%d] = strdup(%s);\n" % (i,pname))
124                 elif(c == "m"):
125                     foc.write("    self->data[%d] = %s;\n" % (i,pname))
126                 elif(c == "c"):
127                     foc.write("    self->data[%d] = %s;\n" % (i,pname))
128                 elif(c == "i"):
129                     foc.write("    self->data[%d] = %s;\n" % (i,pname))
130                 elif(c == "j"):
131                     foc.write("    self->data[%d] = 0; //placeholder\n" % i)
132                     foc.write("    self->branch = %s;\n" % pname)
133                 elif(c == "S"):
134                     foc.write("    self->data[%d] = %s;\n" % (i,pname))
135                 elif(c == "D"):
136                     foc.write("    /* FIXME: write debuginfo %s */\n" % pname)
137                 else:
138                     raise "Unknown type "+c
139                 i = i+1
140             foc.write("    return self;\n")
141             foc.write("}\n")
142
143             foh.write("#define "+name+"(")
144             foh.write(",".join(["method"]+names))
145             foh.write(") (method->code = abc_"+name+"(")
146             foh.write(",".join(["method->code"]+names))
147             foh.write("))\n")
148
149             foh.write("#define OPCODE_"+name.upper()+" "+op+"\n")
150
151 foh.write("#endif\n")
152
153 foh.close()
154 foc.close()
155 fi.close()
156 #{0x75, "convert_d", ""},
157