started integrating expression ast
[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 or "s" 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                     if iteration==0:
66                         type,pname="char*","name"
67                     else:
68                         type,pname="string_t*","s"
69                 elif c == "N":
70                     type,pname="namespace_t*","ns"
71                 elif c in "nubs":
72                     type,pname="int","v"
73                 elif c == "m":
74                     type,pname="abc_method_t*","m"
75                 elif c == "i":
76                     type,pname="abc_method_body_t*","m"
77                 elif c == "c":
78                     type,pname="abc_class_t*","m"
79                 elif c == "j":
80                     type,pname="code_t*","label"
81                 elif c == "S":
82                     type,pname="lookupswitch_t*","l"
83                 elif c == "D":
84                     type,pname="void*","debuginfo"
85                 elif c == "r":
86                     type,pname="int","reg"
87                 elif c == "f":
88                     type,pname="double","f"
89                 elif c == "I":
90                     type,pname="int","i"
91                 elif c == "U":
92                     type,pname="unsigned int","u"
93                 else:
94                     raise "Unknown type "+c
95                 paramstr += type
96                 paramstr += " "
97                 if pname in seen:
98                     seen[pname]+=1
99                     pname += str(seen[pname])
100                 else:
101                     seen[pname]=1
102                 paramstr += pname
103                 names += [pname]
104
105             foh.write("code_t* abc_%s(code_t*prev%s);\n" % (name, paramstr))
106
107             foc.write("code_t* abc_%s(code_t*prev%s)\n" % (name, paramstr))
108             foc.write("{\n")
109             foc.write("    code_t*self = add_opcode(prev, %s);\n" % op)
110             i = 0
111             for pname,c in zip(names,params):
112                 if(c == "2"):
113                     if iteration==0:
114                      foc.write("    self->data[%d] = multiname_fromstring(%s);\n" % (i,pname));
115                     else:
116                      foc.write("    self->data[%d] = multiname_clone(%s);\n" % (i,pname));
117                 elif(c in "nur"):
118                     foc.write("    self->data[%d] = (void*)(ptroff_t)%s;\n" % (i,pname))
119                 elif(c in "IU"):
120                     foc.write("    self->data[%d] = (void*)(ptroff_t)%s;\n" % (i,pname))
121                 elif(c in "N"):
122                     foc.write("    self->data[%d] = namespace_clone(%s);\n" % (i,pname))
123                 elif(c in "f"):
124                     foc.write("    double*fp = malloc(sizeof(double));\n")
125                     foc.write("    *fp = %s;\n" % (pname))
126                     foc.write("    self->data[%d] = fp;\n" % (i))
127                 elif(c == "b"):
128                     foc.write("    self->data[%d] = (void*)(ptroff_t)%s;\n" % (i,pname))
129                 elif(c == "s"):
130                     if iteration==0:
131                         foc.write("    self->data[%d] = string_new4(%s);\n" % (i,pname))
132                     else:
133                         foc.write("    self->data[%d] = string_dup3(%s);\n" % (i,pname))
134                 elif(c == "m"):
135                     foc.write("    self->data[%d] = %s;\n" % (i,pname))
136                 elif(c == "c"):
137                     foc.write("    self->data[%d] = %s;\n" % (i,pname))
138                 elif(c == "i"):
139                     foc.write("    self->data[%d] = %s;\n" % (i,pname))
140                 elif(c == "j"):
141                     foc.write("    self->data[%d] = 0; //placeholder\n" % i)
142                     foc.write("    self->branch = %s;\n" % pname)
143                 elif(c == "S"):
144                     foc.write("    self->data[%d] = %s;\n" % (i,pname))
145                 elif(c == "D"):
146                     foc.write("    /* FIXME: write debuginfo %s */\n" % pname)
147                 else:
148                     raise "Unknown type "+c
149                 i = i+1
150             foc.write("    return self;\n")
151             foc.write("}\n")
152
153             foh.write("#define "+name+"(")
154             foh.write(",".join(["method"]+names))
155             foh.write(") (method->code = abc_"+name+"(")
156             foh.write(",".join(["method->code"]+names))
157             foh.write("))\n")
158
159             foh.write("#define OPCODE_"+name.upper()+" "+op+"\n")
160
161 foh.write("#endif\n")
162
163 foh.close()
164 foc.close()
165 fi.close()
166 #{0x75, "convert_d", ""},
167