msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
mark.lam@apple.com | 96fa033 | 2017-05-28 08:12:09 +0000 | [diff] [blame] | 3 | # Copyright (C) 2014-2017 Apple Inc. All rights reserved. |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 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 | |
| 28 | import hashlib |
| 29 | import json |
| 30 | import optparse |
| 31 | import os |
| 32 | import re |
| 33 | import sys |
| 34 | |
| 35 | cCopyrightMsg = """/* |
| 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 | |
| 64 | asmCopyrightMsg = """# 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 | """ |
| 90 | def 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 | |
| 97 | def 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 | |
keith_miller@apple.com | 1364951 | 2017-08-15 20:13:54 +0000 | [diff] [blame] | 107 | |
| 108 | def toCpp(name): |
| 109 | camelCase = re.sub(r'([^a-z0-9].)', lambda c: c.group(0)[1].upper(), name) |
| 110 | CamelCase = camelCase[:1].upper() + camelCase[1:] |
| 111 | return CamelCase |
| 112 | |
| 113 | |
| 114 | def writeInstructionAccessor(bytecodeHFile, typeName, name): |
| 115 | bytecodeHFile.write(" {0}& {1}() {{ return *reinterpret_cast<{0}*>(&m_{1}); }}\n".format(typeName, name)) |
| 116 | bytecodeHFile.write(" const {0}& {1}() const {{ return *reinterpret_cast<const {0}*>(&m_{1}); }}\n".format(typeName, name)) |
| 117 | |
| 118 | |
| 119 | def writeInstructionMember(bytecodeHFile, typeName, name): |
| 120 | bytecodeHFile.write(" std::aligned_storage<sizeof({0}), sizeof(Instruction)>::type m_{1};\n".format(typeName, name)) |
| 121 | |
| 122 | |
| 123 | def writeStruct(bytecodeHFile, bytecode): |
| 124 | bytecodeHFile.write("struct {0} {{\n".format(toCpp(bytecode["name"]))) |
| 125 | bytecodeHFile.write("public:\n") |
| 126 | |
| 127 | writeInstructionAccessor(bytecodeHFile, "Opcode", "opcode") |
| 128 | for offset in bytecode["offsets"]: |
| 129 | for name, typeName in offset.iteritems(): |
| 130 | writeInstructionAccessor(bytecodeHFile, typeName, name) |
| 131 | |
| 132 | bytecodeHFile.write("\nprivate:\n") |
| 133 | bytecodeHFile.write(" friend class LLIntOffsetsExtractor;\n\n") |
| 134 | |
| 135 | writeInstructionMember(bytecodeHFile, "Opcode", "opcode") |
| 136 | for offset in bytecode["offsets"]: |
| 137 | for name, typeName in offset.iteritems(): |
| 138 | writeInstructionMember(bytecodeHFile, typeName, name) |
| 139 | bytecodeHFile.write("};\n\n") |
| 140 | |
| 141 | |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 142 | if __name__ == "__main__": |
| 143 | parser = optparse.OptionParser(usage = "usage: %prog [--bytecodes_h <FILE>] [--init_bytecodes_asm <FILE>] <bytecode-json-file>") |
| 144 | parser.add_option("-b", "--bytecodes_h", dest = "bytecodesHFileName", help = "generate bytecodes macro .h FILE", metavar = "FILE") |
keith_miller@apple.com | 1364951 | 2017-08-15 20:13:54 +0000 | [diff] [blame] | 145 | parser.add_option("-s", "--bytecode_structs_h", dest = "bytecodeStructsHFileName", help = "generate bytecodes macro .h FILE", metavar = "FILE") |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 146 | parser.add_option("-a", "--init_bytecodes_asm", dest = "initASMFileName", help="generate ASM bytecodes init FILE", metavar = "FILE") |
| 147 | (options, args) = parser.parse_args() |
| 148 | |
| 149 | if len(args) != 1: |
| 150 | parser.error("missing <bytecode-json-file>") |
| 151 | |
| 152 | bytecodeJSONFile = args[0] |
| 153 | bytecodeFile = openOrExit(bytecodeJSONFile, "rb") |
| 154 | sha1Hash = hashFile(bytecodeFile) |
| 155 | |
| 156 | hFileHashString = "// SHA1Hash: {0}\n".format(sha1Hash) |
| 157 | asmFileHashString = "# SHA1Hash: {0}\n".format(sha1Hash) |
| 158 | |
| 159 | bytecodeHFilename = options.bytecodesHFileName |
keith_miller@apple.com | 1364951 | 2017-08-15 20:13:54 +0000 | [diff] [blame] | 160 | bytecodeStructsHFilename = options.bytecodeStructsHFileName |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 161 | initASMFileName = options.initASMFileName |
| 162 | |
keith_miller@apple.com | 1364951 | 2017-08-15 20:13:54 +0000 | [diff] [blame] | 163 | if not bytecodeHFilename and not initASMFileName and not bytecodeStructsHFilename: |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 164 | parser.print_help() |
| 165 | exit(0) |
| 166 | |
| 167 | needToGenerate = False |
| 168 | |
| 169 | if bytecodeHFilename: |
| 170 | try: |
| 171 | bytecodeHReadFile = open(bytecodeHFilename, "rb") |
keith_miller@apple.com | 1364951 | 2017-08-15 20:13:54 +0000 | [diff] [blame] | 172 | |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 173 | hashLine = bytecodeHReadFile.readline() |
| 174 | if hashLine != hFileHashString: |
| 175 | needToGenerate = True |
| 176 | except: |
| 177 | needToGenerate = True |
| 178 | else: |
| 179 | bytecodeHReadFile.close() |
| 180 | |
keith_miller@apple.com | 1364951 | 2017-08-15 20:13:54 +0000 | [diff] [blame] | 181 | if bytecodeStructsHFilename: |
| 182 | try: |
| 183 | bytecodeStructsHReadFile = open(bytecodeStructsHFilename, "rb") |
| 184 | |
| 185 | hashLine = bytecodeStructsHReadFile.readline() |
| 186 | if hashLine != hFileHashString: |
| 187 | needToGenerate = True |
| 188 | except: |
| 189 | needToGenerate = True |
| 190 | else: |
| 191 | bytecodeStructsHReadFile.close() |
| 192 | |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 193 | if initASMFileName: |
| 194 | try: |
| 195 | initBytecodesReadFile = open(initASMFileName, "rb") |
| 196 | |
| 197 | hashLine = initBytecodesReadFile.readline() |
| 198 | if hashLine != asmFileHashString: |
| 199 | needToGenerate = True |
| 200 | except: |
| 201 | needToGenerate = True |
| 202 | else: |
| 203 | initBytecodesReadFile.close() |
| 204 | |
| 205 | if not needToGenerate: |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 206 | exit(0) |
| 207 | |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 208 | if bytecodeHFilename: |
| 209 | bytecodeHFile = openOrExit(bytecodeHFilename, "wb") |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 210 | |
keith_miller@apple.com | 1364951 | 2017-08-15 20:13:54 +0000 | [diff] [blame] | 211 | if bytecodeStructsHFilename: |
| 212 | bytecodeStructsHFile = openOrExit(bytecodeStructsHFilename, "wb") |
| 213 | |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 214 | if initASMFileName: |
| 215 | initBytecodesFile = openOrExit(initASMFileName, "wb") |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 216 | |
| 217 | try: |
| 218 | bytecodeSections = json.load(bytecodeFile, encoding = "utf-8") |
| 219 | except: |
| 220 | print "Unexpected error parsing {0}: {1}".format(bytecodeJSONFile, sys.exc_info()) |
| 221 | |
| 222 | if bytecodeHFilename: |
| 223 | bytecodeHFile.write(hFileHashString) |
| 224 | bytecodeHFile.write(cCopyrightMsg % bytecodeJSONFile) |
commit-queue@webkit.org | b06ac44 | 2016-09-28 19:34:27 +0000 | [diff] [blame] | 225 | bytecodeHFile.write("#pragma once\n\n") |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 226 | |
keith_miller@apple.com | 1364951 | 2017-08-15 20:13:54 +0000 | [diff] [blame] | 227 | if bytecodeStructsHFilename: |
| 228 | bytecodeStructsHFile.write(hFileHashString) |
| 229 | bytecodeStructsHFile.write(cCopyrightMsg % bytecodeJSONFile) |
| 230 | bytecodeStructsHFile.write("#pragma once\n\n") |
| 231 | bytecodeStructsHFile.write("#include \"Instruction.h\"\n") |
| 232 | bytecodeStructsHFile.write("\n") |
| 233 | |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 234 | if initASMFileName: |
| 235 | initBytecodesFile.write(asmFileHashString) |
| 236 | initBytecodesFile.write(asmCopyrightMsg % bytecodeJSONFile) |
| 237 | initASMBytecodeNum = 0 |
| 238 | |
| 239 | for section in bytecodeSections: |
| 240 | if bytecodeHFilename and section['emitInHFile']: |
| 241 | bytecodeHFile.write("#define FOR_EACH_{0}_ID(macro) \\\n".format(section["macroNameComponent"])) |
| 242 | firstMacro = True |
| 243 | defaultLength = 1 |
| 244 | if "defaultLength" in section: |
| 245 | defaultLength = section["defaultLength"] |
| 246 | |
| 247 | bytecodeNum = 0 |
| 248 | for bytecode in section["bytecodes"]: |
| 249 | if not firstMacro: |
| 250 | bytecodeHFile.write(" \\\n") |
| 251 | |
| 252 | length = defaultLength |
| 253 | if "length" in bytecode: |
| 254 | length = bytecode["length"] |
keith_miller@apple.com | 1364951 | 2017-08-15 20:13:54 +0000 | [diff] [blame] | 255 | elif "offsets" in bytecode: |
| 256 | # Add one for the opcode |
| 257 | length = len(bytecode["offsets"]) + 1 |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 258 | |
| 259 | bytecodeHFile.write(" macro({0}, {1})".format(bytecode["name"], length)) |
| 260 | firstMacro = False |
| 261 | bytecodeNum = bytecodeNum + 1 |
| 262 | |
| 263 | bytecodeHFile.write("\n\n") |
| 264 | bytecodeHFile.write("#define NUMBER_OF_{0}_IDS {1}\n\n".format(section["macroNameComponent"], bytecodeNum)) |
| 265 | |
keith_miller@apple.com | 1364951 | 2017-08-15 20:13:54 +0000 | [diff] [blame] | 266 | |
| 267 | if bytecodeStructsHFilename and section['emitInStructsFile']: |
| 268 | bytecodeStructsHFile.write("namespace JSC {\n\n") |
| 269 | |
| 270 | for bytecode in section["bytecodes"]: |
| 271 | if not "offsets" in bytecode: |
| 272 | continue |
| 273 | writeStruct(bytecodeStructsHFile, bytecode) |
| 274 | |
| 275 | bytecodeStructsHFile.write("} // namespace JSC \n") |
| 276 | |
mark.lam@apple.com | 96fa033 | 2017-05-28 08:12:09 +0000 | [diff] [blame] | 277 | if bytecodeHFilename and section['emitOpcodeIDStringValuesInHFile']: |
| 278 | bytecodeNum = 0 |
| 279 | for bytecode in section["bytecodes"]: |
| 280 | bytecodeHFile.write("#define {0}_value_string \"{1}\"\n".format(bytecode["name"], bytecodeNum)) |
| 281 | firstMacro = False |
| 282 | bytecodeNum = bytecodeNum + 1 |
| 283 | |
| 284 | bytecodeHFile.write("\n") |
| 285 | |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 286 | if initASMFileName and section['emitInASMFile']: |
msaboff@apple.com | 8f8907e | 2014-04-10 22:33:59 +0000 | [diff] [blame] | 287 | prefix = "" |
| 288 | if "asmPrefix" in section: |
| 289 | prefix = section["asmPrefix"] |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 290 | for bytecode in section["bytecodes"]: |
msaboff@apple.com | 8f8907e | 2014-04-10 22:33:59 +0000 | [diff] [blame] | 291 | initBytecodesFile.write("setEntryAddress({0}, _{1}{2})\n".format(initASMBytecodeNum, prefix, bytecode["name"])) |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 292 | initASMBytecodeNum = initASMBytecodeNum + 1 |
| 293 | |
| 294 | if bytecodeHFilename: |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 295 | bytecodeHFile.close() |
| 296 | |
| 297 | if initASMFileName: |
| 298 | initBytecodesFile.close() |
| 299 | |
| 300 | bytecodeFile.close() |
| 301 | |
| 302 | exit(0) |