1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
#! /usr/bin/python
# Copyright (C) 2016-2017 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# This tool has a couple of helpful macros to process Wasm files from the wasm.json.
from generateWasm import *
import optparse
import sys
parser = optparse.OptionParser(usage="usage: %prog <wasm.json> <WasmOps.h>")
(options, args) = parser.parse_args(sys.argv[0:])
if len(args) != 3:
parser.error(parser.usage)
wasm = Wasm(args[0], args[1])
types = wasm.types
opcodes = wasm.opcodes
wasmOpsHFile = open(args[2], "w")
def cppMacro(wasmOpcode, value, b3, inc):
return " \\\n macro(" + wasm.toCpp(wasmOpcode) + ", " + hex(int(value)) + ", " + b3 + ", " + str(inc) + ")"
def typeMacroizer():
inc = 0
for ty in wasm.types:
yield cppMacro(ty, wasm.types[ty]["value"], wasm.types[ty]["b3type"], inc)
inc += 1
type_definitions = ["#define FOR_EACH_WASM_TYPE(macro)"]
type_definitions.extend([t for t in typeMacroizer()])
type_definitions = "".join(type_definitions)
def opcodeMacroizer(filter):
inc = 0
for op in wasm.opcodeIterator(filter):
b3op = "Oops"
if isSimple(op["opcode"]):
b3op = op["opcode"]["b3op"]
yield cppMacro(op["name"], op["opcode"]["value"], b3op, inc)
inc += 1
defines = ["#define FOR_EACH_WASM_SPECIAL_OP(macro)"]
defines.extend([op for op in opcodeMacroizer(lambda op: not (isUnary(op) or isBinary(op) or op["category"] == "control" or op["category"] == "memory"))])
defines.append("\n\n#define FOR_EACH_WASM_CONTROL_FLOW_OP(macro)")
defines.extend([op for op in opcodeMacroizer(lambda op: op["category"] == "control")])
defines.append("\n\n#define FOR_EACH_WASM_SIMPLE_UNARY_OP(macro)")
defines.extend([op for op in opcodeMacroizer(lambda op: isUnary(op) and isSimple(op))])
defines.append("\n\n#define FOR_EACH_WASM_UNARY_OP(macro) \\\n FOR_EACH_WASM_SIMPLE_UNARY_OP(macro)")
defines.extend([op for op in opcodeMacroizer(lambda op: isUnary(op) and not (isSimple(op)))])
defines.append("\n\n#define FOR_EACH_WASM_SIMPLE_BINARY_OP(macro)")
defines.extend([op for op in opcodeMacroizer(lambda op: isBinary(op) and isSimple(op))])
defines.append("\n\n#define FOR_EACH_WASM_BINARY_OP(macro) \\\n FOR_EACH_WASM_SIMPLE_BINARY_OP(macro)")
defines.extend([op for op in opcodeMacroizer(lambda op: isBinary(op) and not (isSimple(op)))])
defines.append("\n\n#define FOR_EACH_WASM_MEMORY_LOAD_OP(macro)")
defines.extend([op for op in opcodeMacroizer(lambda op: (op["category"] == "memory" and len(op["return"]) == 1))])
defines.append("\n\n#define FOR_EACH_WASM_MEMORY_STORE_OP(macro)")
defines.extend([op for op in opcodeMacroizer(lambda op: (op["category"] == "memory" and len(op["return"]) == 0))])
defines.append("\n\n")
defines = "".join(defines)
opValueSet = set([op for op in wasm.opcodeIterator(lambda op: True, lambda op: opcodes[op]["value"])])
maxOpValue = max(opValueSet)
# Luckily, python does floor division rather than trunc division so this works.
def ceilDiv(a, b):
return -(-a // b)
def bitSet():
v = ""
for i in range(ceilDiv(maxOpValue, 8)):
entry = 0
for j in range(8):
if i * 8 + j in opValueSet:
entry |= 1 << j
v += (", " if i else "") + hex(entry)
return v
validOps = bitSet()
def memoryLog2AlignmentGenerator(filter):
result = []
for op in wasm.opcodeIterator(filter):
result.append(" case " + wasm.toCpp(op["name"]) + ": return " + memoryLog2Alignment(op) + ";")
return "\n".join(result)
memoryLog2AlignmentLoads = memoryLog2AlignmentGenerator(lambda op: (op["category"] == "memory" and len(op["return"]) == 1))
memoryLog2AlignmentStores = memoryLog2AlignmentGenerator(lambda op: (op["category"] == "memory" and len(op["return"]) == 0))
contents = wasm.header + """
#pragma once
#if ENABLE(WEBASSEMBLY)
#include <cstdint>
namespace JSC { namespace Wasm {
static constexpr unsigned expectedVersionNumber = """ + wasm.expectedVersionNumber + """;
static constexpr unsigned numTypes = """ + str(len(types)) + """;
""" + type_definitions + """
#define CREATE_ENUM_VALUE(name, id, b3type, inc) name = id,
enum Type : int8_t {
FOR_EACH_WASM_TYPE(CREATE_ENUM_VALUE)
};
#undef CREATE_ENUM_VALUE
#define CREATE_CASE(name, id, b3type, inc) case id: return true;
template <typename Int>
inline bool isValidType(Int i)
{
switch (i) {
default: return false;
FOR_EACH_WASM_TYPE(CREATE_CASE)
}
RELEASE_ASSERT_NOT_REACHED();
return false;
}
#undef CREATE_CASE
#define CREATE_CASE(name, id, b3type, inc) case name: return b3type;
inline B3::Type toB3Type(Type type)
{
switch (type) {
FOR_EACH_WASM_TYPE(CREATE_CASE)
}
RELEASE_ASSERT_NOT_REACHED();
return B3::Void;
}
#undef CREATE_CASE
#define CREATE_CASE(name, id, b3type, inc) case name: return #name;
inline const char* makeString(Type type)
{
switch (type) {
FOR_EACH_WASM_TYPE(CREATE_CASE)
}
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
#undef CREATE_CASE
#define CREATE_CASE(name, id, b3type, inc) case id: return inc;
inline int linearizeType(Type type)
{
switch (type) {
FOR_EACH_WASM_TYPE(CREATE_CASE)
}
RELEASE_ASSERT_NOT_REACHED();
return 0;
}
#undef CREATE_CASE
#define CREATE_CASE(name, id, b3type, inc) case inc: return name;
inline Type linearizedToType(int i)
{
switch (i) {
FOR_EACH_WASM_TYPE(CREATE_CASE)
}
RELEASE_ASSERT_NOT_REACHED();
return Void;
}
#undef CREATE_CASE
""" + defines + """
#define FOR_EACH_WASM_OP(macro) \\
FOR_EACH_WASM_SPECIAL_OP(macro) \\
FOR_EACH_WASM_CONTROL_FLOW_OP(macro) \\
FOR_EACH_WASM_UNARY_OP(macro) \\
FOR_EACH_WASM_BINARY_OP(macro) \\
FOR_EACH_WASM_MEMORY_LOAD_OP(macro) \\
FOR_EACH_WASM_MEMORY_STORE_OP(macro)
#define CREATE_ENUM_VALUE(name, id, b3op, inc) name = id,
enum OpType : uint8_t {
FOR_EACH_WASM_OP(CREATE_ENUM_VALUE)
};
template<typename Int>
inline bool isValidOpType(Int i)
{
// Bitset of valid ops.
static const uint8_t valid[] = { """ + validOps + """ };
return 0 <= i && i <= """ + str(maxOpValue) + """ && (valid[i / 8] & (1 << (i % 8)));
}
enum class BinaryOpType : uint8_t {
FOR_EACH_WASM_BINARY_OP(CREATE_ENUM_VALUE)
};
enum class UnaryOpType : uint8_t {
FOR_EACH_WASM_UNARY_OP(CREATE_ENUM_VALUE)
};
enum class LoadOpType : uint8_t {
FOR_EACH_WASM_MEMORY_LOAD_OP(CREATE_ENUM_VALUE)
};
enum class StoreOpType : uint8_t {
FOR_EACH_WASM_MEMORY_STORE_OP(CREATE_ENUM_VALUE)
};
#undef CREATE_ENUM_VALUE
inline bool isControlOp(OpType op)
{
switch (op) {
#define CREATE_CASE(name, id, b3op, inc) case OpType::name:
FOR_EACH_WASM_CONTROL_FLOW_OP(CREATE_CASE)
return true;
#undef CREATE_CASE
default:
break;
}
return false;
}
inline bool isSimple(UnaryOpType op)
{
switch (op) {
#define CREATE_CASE(name, id, b3op, inc) case UnaryOpType::name:
FOR_EACH_WASM_SIMPLE_UNARY_OP(CREATE_CASE)
return true;
#undef CREATE_CASE
default:
break;
}
return false;
}
inline bool isSimple(BinaryOpType op)
{
switch (op) {
#define CREATE_CASE(name, id, b3op, inc) case BinaryOpType::name:
FOR_EACH_WASM_SIMPLE_BINARY_OP(CREATE_CASE)
return true;
#undef CREATE_CASE
default:
break;
}
return false;
}
inline uint32_t memoryLog2Alignment(OpType op)
{
switch (op) {
""" + memoryLog2AlignmentLoads + """
""" + memoryLog2AlignmentStores + """
default:
break;
}
RELEASE_ASSERT_NOT_REACHED();
return 0;
}
#define CREATE_CASE(name, id, b3type, inc) case name: return #name;
inline const char* makeString(OpType op)
{
switch (op) {
FOR_EACH_WASM_OP(CREATE_CASE)
}
RELEASE_ASSERT_NOT_REACHED();
return nullptr;
}
#undef CREATE_CASE
} } // namespace JSC::Wasm
#endif // ENABLE(WEBASSEMBLY)
"""
wasmOpsHFile.write(contents)
wasmOpsHFile.close()
|