]>
git.saurik.com Git - apple/javascriptcore.git/blob - generate-bytecode-files
3 # Copyright (C) 2014 Apple Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 # This tool processes the bytecode list to create Bytecodes.h and InitBytecodes.asm
36 * Copyright (C) 2014 Apple Inc. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
48 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
49 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
52 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
53 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
54 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 * Autogenerated from %s, do not modify.
64 asmCopyrightMsg
= """# Copyright (C) 2014 Apple Inc. All rights reserved.
66 # Redistribution and use in source and binary forms, with or without
67 # modification, are permitted provided that the following conditions
70 # 1. Redistributions of source code must retain the above copyright
71 # notice, this list of conditions and the following disclaimer.
72 # 2. Redistributions in binary form must reproduce the above copyright
73 # notice, this list of conditions and the following disclaimer in the
74 # documentation and/or other materials provided with the distribution.
76 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
77 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
78 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
79 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
80 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
81 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
82 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
83 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
84 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
85 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
87 # Autogenerated from %s, do not modify.
90 def openOrExit(path
, mode
):
92 return open(path
, mode
)
94 print "I/O error opening {0}, ({1}): {2}".format(path
, e
.errno
, e
.strerror
)
105 return sha1
.hexdigest()
107 if __name__
== "__main__":
108 parser
= optparse
.OptionParser(usage
= "usage: %prog [--bytecodes_h <FILE>] [--init_bytecodes_asm <FILE>] <bytecode-json-file>")
109 parser
.add_option("-b", "--bytecodes_h", dest
= "bytecodesHFileName", help = "generate bytecodes macro .h FILE", metavar
= "FILE")
110 parser
.add_option("-a", "--init_bytecodes_asm", dest
= "initASMFileName", help="generate ASM bytecodes init FILE", metavar
= "FILE")
111 (options
, args
) = parser
.parse_args()
114 parser
.error("missing <bytecode-json-file>")
116 bytecodeJSONFile
= args
[0]
117 bytecodeFile
= openOrExit(bytecodeJSONFile
, "rb")
118 sha1Hash
= hashFile(bytecodeFile
)
120 hFileHashString
= "// SHA1Hash: {0}\n".format(sha1Hash
)
121 asmFileHashString
= "# SHA1Hash: {0}\n".format(sha1Hash
)
123 bytecodeHFilename
= options
.bytecodesHFileName
124 initASMFileName
= options
.initASMFileName
126 if not bytecodeHFilename
and not initASMFileName
:
130 needToGenerate
= False
132 if bytecodeHFilename
:
134 bytecodeHReadFile
= open(bytecodeHFilename
, "rb")
136 hashLine
= bytecodeHReadFile
.readline()
137 if hashLine
!= hFileHashString
:
138 needToGenerate
= True
140 needToGenerate
= True
142 bytecodeHReadFile
.close()
146 initBytecodesReadFile
= open(initASMFileName
, "rb")
148 hashLine
= initBytecodesReadFile
.readline()
149 if hashLine
!= asmFileHashString
:
150 needToGenerate
= True
152 needToGenerate
= True
154 initBytecodesReadFile
.close()
156 if not needToGenerate
:
157 print "Nothing changed.\n"
160 genString
= "Generating "
162 if bytecodeHFilename
:
163 bytecodeHFile
= openOrExit(bytecodeHFilename
, "wb")
164 genString
= genString
+ "{0} ".format(bytecodeHFilename
)
167 initBytecodesFile
= openOrExit(initASMFileName
, "wb")
168 if bytecodeHFilename
:
169 genString
= genString
+ "and "
170 genString
= genString
+ "{0} ".format(initASMFileName
)
172 print "{0}from {1}\n".format(genString
, bytecodeJSONFile
)
176 bytecodeSections
= json
.load(bytecodeFile
, encoding
= "utf-8")
178 print "Unexpected error parsing {0}: {1}".format(bytecodeJSONFile
, sys
.exc_info())
180 if bytecodeHFilename
:
181 bytecodeHFile
.write(hFileHashString
)
182 bytecodeHFile
.write(cCopyrightMsg
% bytecodeJSONFile
)
183 bytecodeHFile
.write("#ifndef Bytecodes_h\n")
184 bytecodeHFile
.write("#define Bytecodes_h\n\n")
187 initBytecodesFile
.write(asmFileHashString
)
188 initBytecodesFile
.write(asmCopyrightMsg
% bytecodeJSONFile
)
189 initASMBytecodeNum
= 0
191 for section
in bytecodeSections
:
192 if bytecodeHFilename
and section
['emitInHFile']:
193 bytecodeHFile
.write("#define FOR_EACH_{0}_ID(macro) \\\n".format(section
["macroNameComponent"]))
196 if "defaultLength" in section
:
197 defaultLength
= section
["defaultLength"]
200 for bytecode
in section
["bytecodes"]:
202 bytecodeHFile
.write(" \\\n")
204 length
= defaultLength
205 if "length" in bytecode
:
206 length
= bytecode
["length"]
208 bytecodeHFile
.write(" macro({0}, {1})".format(bytecode
["name"], length
))
210 bytecodeNum
= bytecodeNum
+ 1
212 bytecodeHFile
.write("\n\n")
213 bytecodeHFile
.write("#define NUMBER_OF_{0}_IDS {1}\n\n".format(section
["macroNameComponent"], bytecodeNum
))
215 if initASMFileName
and section
['emitInASMFile']:
217 if "asmPrefix" in section
:
218 prefix
= section
["asmPrefix"]
219 for bytecode
in section
["bytecodes"]:
220 initBytecodesFile
.write("setEntryAddress({0}, _{1}{2})\n".format(initASMBytecodeNum
, prefix
, bytecode
["name"]))
221 initASMBytecodeNum
= initASMBytecodeNum
+ 1
223 if bytecodeHFilename
:
224 bytecodeHFile
.write("#endif // Bytecodes_h\n")
225 bytecodeHFile
.close()
228 initBytecodesFile
.close()