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
|
#!/usr/bin/env python3
# Copyright (C) 2016 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:n
#
# 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
import re
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])
opcodes = wasm.opcodes
wasmOMGIRGeneratorHFile = open(args[2], "w")
opcodeRegex = re.compile('([a-zA-Z0-9]+)')
argumentRegex = re.compile(r'(\@[0-9]+)')
decimalRegex = re.compile('([-]?[0-9]+)')
whitespaceRegex = re.compile(r'\s+')
commaRegex = re.compile('(,)')
oparenRegex = re.compile(r'(\()')
cparenRegex = re.compile(r'(\))')
class Source:
def __init__(self, contents, offset=0):
self.contents = contents
self.offset = offset
def read(regex, source):
match = regex.match(source.contents, source.offset)
if not match:
return None
source.offset = match.end()
return match.group()
def lex(source):
result = []
while source.offset != len(source.contents):
read(whitespaceRegex, source)
opcode = read(opcodeRegex, source)
if opcode:
result.append(opcode)
continue
argument = read(argumentRegex, source)
if argument:
result.append(argument)
continue
number = read(decimalRegex, source)
if number:
result.append(int(number))
continue
oparen = read(oparenRegex, source)
if oparen:
result.append(oparen)
continue
cparen = read(cparenRegex, source)
if cparen:
result.append(cparen)
continue
comma = read(commaRegex, source)
if comma:
# Skip commas
continue
raise Exception("Lexing Error: could not lex token from: " + source.contents + " at offset: " + str(source.offset) + " (" + source.contents[source.offset:] + "). With tokens: [" + ", ".join(result) + "]")
return result
class CodeGenerator:
def __init__(self, tokens):
self.tokens = tokens
self.index = 0
self.code = []
def advance(self):
self.index += 1
def token(self):
return self.tokens[self.index]
def parseError(self, string):
raise Exception("Parse error " + string)
def consume(self, string):
if self.token() != string:
self.parseError("Expected " + string + " but got " + self.token())
self.advance()
def generateParameters(self):
self.advance()
params = []
tokens = self.tokens
while self.index < len(tokens):
if self.token() == ")":
self.advance()
return params
params.append(self.generateOpcode())
self.parseError("Parsing arguments fell off end")
def generateB3OpCode(self, index, op, params):
self.code.append("Value* " + temp(index) + " = m_currentBlock->appendNew<Value>(m_proc, B3::" + op + ", origin(), " + ", ".join(params) + ");")
def generateConstCode(self, index, value, type):
self.code.append("Value* " + temp(index) + " = constant(" + type + ", " + value + ");")
def generateOpcode(self):
result = None
if self.token() == "i32" or self.token() == "i64":
type = "Int32"
if self.token() == "i64":
type = "Int64"
self.advance()
self.consume("(")
self.generateConstCode(self.index, self.token(), type)
result = temp(self.index)
self.advance()
self.consume(")")
elif argumentRegex.match(self.token()):
result = "get(arg" + self.token()[1:] + ")"
self.advance()
else:
op = self.token()
index = self.index
self.advance()
params = self.generateParameters()
self.generateB3OpCode(index, op, params)
result = temp(index)
return result
def makeResult(self, resultValue):
return resultValue + ";\n" + " result = push(resultValue);"
def generate(self, wasmOp):
if len(self.tokens) == 1:
op = self.token()
index = self.index
params = ["get(arg" + str(param) + ")" for param in range(len(wasmOp["parameter"]))]
self.generateB3OpCode(index, op, params)
result = temp(index)
self.code.append("Value* resultValue = " + result)
return self.makeResult(" " + "\n ".join(self.code))
result = self.generateOpcode()
self.code.append("Value* resultValue = " + result)
return self.makeResult(" " + "\n ".join(self.code))
def temp(index):
return "temp" + str(index)
def generateB3Code(wasmOp, source):
tokens = lex(Source(source))
parser = CodeGenerator(tokens)
return parser.generate(wasmOp)
def generateSimpleCode(op):
opcode = op["opcode"]
b3op = opcode["b3op"]
args = ["ExpressionType arg" + str(param) for param in range(len(opcode["parameter"]))]
args.append("ExpressionType& result")
return """
auto OMGIRGenerator::add""" + wasm.toCpp(op["name"]) + "(" + ", ".join(args) + """) -> PartialResult
{
""" + generateB3Code(opcode, b3op) + """
return { };
}
"""
definitions = [generateSimpleCode(op) for op in wasm.opcodeIterator(lambda op: isSimple(op) and (isBinary(op) or isUnary(op)))]
contents = wasm.header + """
#pragma once
#if ENABLE(WEBASSEMBLY)
namespace JSC { namespace Wasm {
""" + "".join(definitions) + """
} } // namespace JSC::Wasm
#endif // ENABLE(WEBASSEMBLY)
"""
wasmOMGIRGeneratorHFile.write(contents)
wasmOMGIRGeneratorHFile.close()
|