source: webkit/trunk/Source/JavaScriptCore/generate-bytecode-files@ 194531

Last change on this file since 194531 was 194531, checked in by Csaba Osztrogonác, 9 years ago

JSC generator scripts shouldn't have verbose output
https://bugs.webkit.org/show_bug.cgi?id=152382

Reviewed by Michael Catanzaro.

  • b3/air/opcode_generator.rb:
  • generate-bytecode-files:
  • offlineasm/asm.rb:
  • offlineasm/generate_offset_extractor.rb:
  • offlineasm/parser.rb:
File size: 8.6 KB
Line 
1#! /usr/bin/python
2
3# Copyright (C) 2014 Apple Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8#
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.
14#
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.
25
26# This tool processes the bytecode list to create Bytecodes.h and InitBytecodes.asm
27
28import hashlib
29import json
30import optparse
31import os
32import re
33import sys
34
35cCopyrightMsg = """/*
36* Copyright (C) 2014 Apple Inc. All rights reserved.
37*
38* Redistribution and use in source and binary forms, with or without
39* modification, are permitted provided that the following conditions
40* are met:
41*
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.
47*
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.
58
59* Autogenerated from %s, do not modify.
60*/
61
62"""
63
64asmCopyrightMsg = """# Copyright (C) 2014 Apple Inc. All rights reserved.
65#
66# Redistribution and use in source and binary forms, with or without
67# modification, are permitted provided that the following conditions
68# are met:
69#
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.
75#
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.
86
87# Autogenerated from %s, do not modify.
88
89"""
90def openOrExit(path, mode):
91 try:
92 return open(path, mode)
93 except IOError as e:
94 print "I/O error opening {0}, ({1}): {2}".format(path, e.errno, e.strerror)
95 exit(1)
96
97def hashFile(file):
98 sha1 = hashlib.sha1()
99 file.seek(0)
100 for line in file:
101 sha1.update(line)
102
103 file.seek(0)
104
105 return sha1.hexdigest()
106
107if __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()
112
113 if len(args) != 1:
114 parser.error("missing <bytecode-json-file>")
115
116 bytecodeJSONFile = args[0]
117 bytecodeFile = openOrExit(bytecodeJSONFile, "rb")
118 sha1Hash = hashFile(bytecodeFile)
119
120 hFileHashString = "// SHA1Hash: {0}\n".format(sha1Hash)
121 asmFileHashString = "# SHA1Hash: {0}\n".format(sha1Hash)
122
123 bytecodeHFilename = options.bytecodesHFileName
124 initASMFileName = options.initASMFileName
125
126 if not bytecodeHFilename and not initASMFileName:
127 parser.print_help()
128 exit(0)
129
130 needToGenerate = False
131
132 if bytecodeHFilename:
133 try:
134 bytecodeHReadFile = open(bytecodeHFilename, "rb")
135
136 hashLine = bytecodeHReadFile.readline()
137 if hashLine != hFileHashString:
138 needToGenerate = True
139 except:
140 needToGenerate = True
141 else:
142 bytecodeHReadFile.close()
143
144 if initASMFileName:
145 try:
146 initBytecodesReadFile = open(initASMFileName, "rb")
147
148 hashLine = initBytecodesReadFile.readline()
149 if hashLine != asmFileHashString:
150 needToGenerate = True
151 except:
152 needToGenerate = True
153 else:
154 initBytecodesReadFile.close()
155
156 if not needToGenerate:
157 exit(0)
158
159 if bytecodeHFilename:
160 bytecodeHFile = openOrExit(bytecodeHFilename, "wb")
161
162 if initASMFileName:
163 initBytecodesFile = openOrExit(initASMFileName, "wb")
164
165 try:
166 bytecodeSections = json.load(bytecodeFile, encoding = "utf-8")
167 except:
168 print "Unexpected error parsing {0}: {1}".format(bytecodeJSONFile, sys.exc_info())
169
170 if bytecodeHFilename:
171 bytecodeHFile.write(hFileHashString)
172 bytecodeHFile.write(cCopyrightMsg % bytecodeJSONFile)
173 bytecodeHFile.write("#ifndef Bytecodes_h\n")
174 bytecodeHFile.write("#define Bytecodes_h\n\n")
175
176 if initASMFileName:
177 initBytecodesFile.write(asmFileHashString)
178 initBytecodesFile.write(asmCopyrightMsg % bytecodeJSONFile)
179 initASMBytecodeNum = 0
180
181 for section in bytecodeSections:
182 if bytecodeHFilename and section['emitInHFile']:
183 bytecodeHFile.write("#define FOR_EACH_{0}_ID(macro) \\\n".format(section["macroNameComponent"]))
184 firstMacro = True
185 defaultLength = 1
186 if "defaultLength" in section:
187 defaultLength = section["defaultLength"]
188
189 bytecodeNum = 0
190 for bytecode in section["bytecodes"]:
191 if not firstMacro:
192 bytecodeHFile.write(" \\\n")
193
194 length = defaultLength
195 if "length" in bytecode:
196 length = bytecode["length"]
197
198 bytecodeHFile.write(" macro({0}, {1})".format(bytecode["name"], length))
199 firstMacro = False
200 bytecodeNum = bytecodeNum + 1
201
202 bytecodeHFile.write("\n\n")
203 bytecodeHFile.write("#define NUMBER_OF_{0}_IDS {1}\n\n".format(section["macroNameComponent"], bytecodeNum))
204
205 if initASMFileName and section['emitInASMFile']:
206 prefix = ""
207 if "asmPrefix" in section:
208 prefix = section["asmPrefix"]
209 for bytecode in section["bytecodes"]:
210 initBytecodesFile.write("setEntryAddress({0}, _{1}{2})\n".format(initASMBytecodeNum, prefix, bytecode["name"]))
211 initASMBytecodeNum = initASMBytecodeNum + 1
212
213 if bytecodeHFilename:
214 bytecodeHFile.write("#endif // Bytecodes_h\n")
215 bytecodeHFile.close()
216
217 if initASMFileName:
218 initBytecodesFile.close()
219
220 bytecodeFile.close()
221
222 exit(0)
Note: See TracBrowser for help on using the repository browser.