Best Python code snippet using localstack_python
generateWasmOpsHeader.py
Source:generateWasmOpsHeader.py
1#! /usr/bin/python2# Copyright (C) 2016-2017 Apple Inc. All rights reserved.3#4# Redistribution and use in source and binary forms, with or without5# modification, are permitted provided that the following conditions6# are met:7#8# 1. Redistributions of source code must retain the above copyright9# notice, this list of conditions and the following disclaimer.10# 2. Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13#14# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY15# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED16# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE17# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY18# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES19# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;20# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND21# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24# This tool has a couple of helpful macros to process Wasm files from the wasm.json.25from generateWasm import *26import optparse27import sys28parser = optparse.OptionParser(usage="usage: %prog <wasm.json> <WasmOps.h>")29(options, args) = parser.parse_args(sys.argv[0:])30if len(args) != 3:31 parser.error(parser.usage)32wasm = Wasm(args[0], args[1])33types = wasm.types34opcodes = wasm.opcodes35wasmOpsHFile = open(args[2], "w")36def cppMacro(wasmOpcode, value, b3, inc):37 return " \\\n macro(" + wasm.toCpp(wasmOpcode) + ", " + hex(int(value)) + ", " + b3 + ", " + str(inc) + ")"38def typeMacroizer():39 inc = 040 for ty in wasm.types:41 yield cppMacro(ty, wasm.types[ty]["value"], wasm.types[ty]["b3type"], inc)42 inc += 143type_definitions = ["#define FOR_EACH_WASM_TYPE(macro)"]44type_definitions.extend([t for t in typeMacroizer()])45type_definitions = "".join(type_definitions)46def opcodeMacroizer(filter):47 inc = 048 for op in wasm.opcodeIterator(filter):49 b3op = "Oops"50 if isSimple(op["opcode"]):51 b3op = op["opcode"]["b3op"]52 yield cppMacro(op["name"], op["opcode"]["value"], b3op, inc)53 inc += 154defines = ["#define FOR_EACH_WASM_SPECIAL_OP(macro)"]55defines.extend([op for op in opcodeMacroizer(lambda op: not (isUnary(op) or isBinary(op) or op["category"] == "control" or op["category"] == "memory"))])56defines.append("\n\n#define FOR_EACH_WASM_CONTROL_FLOW_OP(macro)")57defines.extend([op for op in opcodeMacroizer(lambda op: op["category"] == "control")])58defines.append("\n\n#define FOR_EACH_WASM_SIMPLE_UNARY_OP(macro)")59defines.extend([op for op in opcodeMacroizer(lambda op: isUnary(op) and isSimple(op))])60defines.append("\n\n#define FOR_EACH_WASM_UNARY_OP(macro) \\\n FOR_EACH_WASM_SIMPLE_UNARY_OP(macro)")61defines.extend([op for op in opcodeMacroizer(lambda op: isUnary(op) and not (isSimple(op)))])62defines.append("\n\n#define FOR_EACH_WASM_SIMPLE_BINARY_OP(macro)")63defines.extend([op for op in opcodeMacroizer(lambda op: isBinary(op) and isSimple(op))])64defines.append("\n\n#define FOR_EACH_WASM_BINARY_OP(macro) \\\n FOR_EACH_WASM_SIMPLE_BINARY_OP(macro)")65defines.extend([op for op in opcodeMacroizer(lambda op: isBinary(op) and not (isSimple(op)))])66defines.append("\n\n#define FOR_EACH_WASM_MEMORY_LOAD_OP(macro)")67defines.extend([op for op in opcodeMacroizer(lambda op: (op["category"] == "memory" and len(op["return"]) == 1))])68defines.append("\n\n#define FOR_EACH_WASM_MEMORY_STORE_OP(macro)")69defines.extend([op for op in opcodeMacroizer(lambda op: (op["category"] == "memory" and len(op["return"]) == 0))])70defines.append("\n\n")71defines = "".join(defines)72opValueSet = set([op for op in wasm.opcodeIterator(lambda op: True, lambda op: opcodes[op]["value"])])73maxOpValue = max(opValueSet)74# Luckily, python does floor division rather than trunc division so this works.75def ceilDiv(a, b):76 return -(-a // b)77def bitSet():78 v = ""79 for i in range(ceilDiv(maxOpValue, 8)):80 entry = 081 for j in range(8):82 if i * 8 + j in opValueSet:83 entry |= 1 << j84 v += (", " if i else "") + hex(entry)85 return v86validOps = bitSet()87def memoryLog2AlignmentGenerator(filter):88 result = []89 for op in wasm.opcodeIterator(filter):90 result.append(" case " + wasm.toCpp(op["name"]) + ": return " + memoryLog2Alignment(op) + ";")91 return "\n".join(result)92memoryLog2AlignmentLoads = memoryLog2AlignmentGenerator(lambda op: (op["category"] == "memory" and len(op["return"]) == 1))93memoryLog2AlignmentStores = memoryLog2AlignmentGenerator(lambda op: (op["category"] == "memory" and len(op["return"]) == 0))94contents = wasm.header + """95#pragma once96#if ENABLE(WEBASSEMBLY)97#include <cstdint>98namespace JSC { namespace Wasm {99static constexpr unsigned expectedVersionNumber = """ + wasm.expectedVersionNumber + """;100static constexpr unsigned numTypes = """ + str(len(types)) + """;101""" + type_definitions + """102#define CREATE_ENUM_VALUE(name, id, b3type, inc) name = id,103enum Type : int8_t {104 FOR_EACH_WASM_TYPE(CREATE_ENUM_VALUE)105};106#undef CREATE_ENUM_VALUE107#define CREATE_CASE(name, id, b3type, inc) case id: return true;108template <typename Int>109inline bool isValidType(Int i)110{111 switch (i) {112 default: return false;113 FOR_EACH_WASM_TYPE(CREATE_CASE)114 }115 RELEASE_ASSERT_NOT_REACHED();116 return false;117}118#undef CREATE_CASE119#define CREATE_CASE(name, id, b3type, inc) case name: return b3type;120inline B3::Type toB3Type(Type type)121{122 switch (type) {123 FOR_EACH_WASM_TYPE(CREATE_CASE)124 }125 RELEASE_ASSERT_NOT_REACHED();126 return B3::Void;127}128#undef CREATE_CASE129#define CREATE_CASE(name, id, b3type, inc) case name: return #name;130inline const char* makeString(Type type)131{132 switch (type) {133 FOR_EACH_WASM_TYPE(CREATE_CASE)134 }135 RELEASE_ASSERT_NOT_REACHED();136 return nullptr;137}138#undef CREATE_CASE139#define CREATE_CASE(name, id, b3type, inc) case id: return inc;140inline int linearizeType(Type type)141{142 switch (type) {143 FOR_EACH_WASM_TYPE(CREATE_CASE)144 }145 RELEASE_ASSERT_NOT_REACHED();146 return 0;147}148#undef CREATE_CASE149#define CREATE_CASE(name, id, b3type, inc) case inc: return name;150inline Type linearizedToType(int i)151{152 switch (i) {153 FOR_EACH_WASM_TYPE(CREATE_CASE)154 }155 RELEASE_ASSERT_NOT_REACHED();156 return Void;157}158#undef CREATE_CASE159""" + defines + """160#define FOR_EACH_WASM_OP(macro) \\161 FOR_EACH_WASM_SPECIAL_OP(macro) \\162 FOR_EACH_WASM_CONTROL_FLOW_OP(macro) \\163 FOR_EACH_WASM_UNARY_OP(macro) \\164 FOR_EACH_WASM_BINARY_OP(macro) \\165 FOR_EACH_WASM_MEMORY_LOAD_OP(macro) \\166 FOR_EACH_WASM_MEMORY_STORE_OP(macro)167#define CREATE_ENUM_VALUE(name, id, b3op, inc) name = id,168enum OpType : uint8_t {169 FOR_EACH_WASM_OP(CREATE_ENUM_VALUE)170};171template<typename Int>172inline bool isValidOpType(Int i)173{174 // Bitset of valid ops.175 static const uint8_t valid[] = { """ + validOps + """ };176 return 0 <= i && i <= """ + str(maxOpValue) + """ && (valid[i / 8] & (1 << (i % 8)));177}178enum class BinaryOpType : uint8_t {179 FOR_EACH_WASM_BINARY_OP(CREATE_ENUM_VALUE)180};181enum class UnaryOpType : uint8_t {182 FOR_EACH_WASM_UNARY_OP(CREATE_ENUM_VALUE)183};184enum class LoadOpType : uint8_t {185 FOR_EACH_WASM_MEMORY_LOAD_OP(CREATE_ENUM_VALUE)186};187enum class StoreOpType : uint8_t {188 FOR_EACH_WASM_MEMORY_STORE_OP(CREATE_ENUM_VALUE)189};190#undef CREATE_ENUM_VALUE191inline bool isControlOp(OpType op)192{193 switch (op) {194#define CREATE_CASE(name, id, b3op, inc) case OpType::name:195 FOR_EACH_WASM_CONTROL_FLOW_OP(CREATE_CASE)196 return true;197#undef CREATE_CASE198 default:199 break;200 }201 return false;202}203inline bool isSimple(UnaryOpType op)204{205 switch (op) {206#define CREATE_CASE(name, id, b3op, inc) case UnaryOpType::name:207 FOR_EACH_WASM_SIMPLE_UNARY_OP(CREATE_CASE)208 return true;209#undef CREATE_CASE210 default:211 break;212 }213 return false;214}215inline bool isSimple(BinaryOpType op)216{217 switch (op) {218#define CREATE_CASE(name, id, b3op, inc) case BinaryOpType::name:219 FOR_EACH_WASM_SIMPLE_BINARY_OP(CREATE_CASE)220 return true;221#undef CREATE_CASE222 default:223 break;224 }225 return false;226}227inline uint32_t memoryLog2Alignment(OpType op)228{229 switch (op) {230""" + memoryLog2AlignmentLoads + """231""" + memoryLog2AlignmentStores + """232 default:233 break;234 }235 RELEASE_ASSERT_NOT_REACHED();236 return 0;237}238#define CREATE_CASE(name, id, b3type, inc) case name: return #name;239inline const char* makeString(OpType op)240{241 switch (op) {242 FOR_EACH_WASM_OP(CREATE_CASE)243 }244 RELEASE_ASSERT_NOT_REACHED();245 return nullptr;246}247#undef CREATE_CASE248} } // namespace JSC::Wasm249#endif // ENABLE(WEBASSEMBLY)250"""251wasmOpsHFile.write(contents)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!