1//===- TranslateFromWasm.cpp - Translating to WasmSSA dialect -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the WebAssembly importer.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Dialect/WasmSSA/IR/WasmSSA.h"
14#include "mlir/IR/Attributes.h"
15#include "mlir/IR/Builders.h"
16#include "mlir/IR/BuiltinAttributeInterfaces.h"
17#include "mlir/IR/BuiltinAttributes.h"
18#include "mlir/IR/BuiltinTypes.h"
19#include "mlir/IR/Diagnostics.h"
20#include "mlir/IR/Location.h"
21#include "mlir/Support/LLVM.h"
22#include "mlir/Target/Wasm/WasmBinaryEncoding.h"
23#include "mlir/Target/Wasm/WasmImporter.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/DebugLog.h"
26#include "llvm/Support/Endian.h"
27#include "llvm/Support/FormatVariadic.h"
28#include "llvm/Support/LEB128.h"
29#include "llvm/Support/LogicalResult.h"
30
31#include <cassert>
32#include <cstddef>
33#include <cstdint>
34#include <type_traits>
35#include <variant>
36
37#define DEBUG_TYPE "wasm-translate"
38
39static_assert(CHAR_BIT == 8,
40 "This code expects std::byte to be exactly 8 bits");
41
42using namespace mlir;
43using namespace mlir::wasm;
44using namespace mlir::wasmssa;
45
46namespace {
47using section_id_t = uint8_t;
48enum struct WasmSectionType : section_id_t {
49 CUSTOM = 0,
50 TYPE = 1,
51 IMPORT = 2,
52 FUNCTION = 3,
53 TABLE = 4,
54 MEMORY = 5,
55 GLOBAL = 6,
56 EXPORT = 7,
57 START = 8,
58 ELEMENT = 9,
59 CODE = 10,
60 DATA = 11,
61 DATACOUNT = 12
62};
63
64constexpr section_id_t highestWasmSectionID{
65 static_cast<section_id_t>(WasmSectionType::DATACOUNT)};
66
67#define APPLY_WASM_SEC_TRANSFORM \
68 WASM_SEC_TRANSFORM(CUSTOM) \
69 WASM_SEC_TRANSFORM(TYPE) \
70 WASM_SEC_TRANSFORM(IMPORT) \
71 WASM_SEC_TRANSFORM(FUNCTION) \
72 WASM_SEC_TRANSFORM(TABLE) \
73 WASM_SEC_TRANSFORM(MEMORY) \
74 WASM_SEC_TRANSFORM(GLOBAL) \
75 WASM_SEC_TRANSFORM(EXPORT) \
76 WASM_SEC_TRANSFORM(START) \
77 WASM_SEC_TRANSFORM(ELEMENT) \
78 WASM_SEC_TRANSFORM(CODE) \
79 WASM_SEC_TRANSFORM(DATA) \
80 WASM_SEC_TRANSFORM(DATACOUNT)
81
82template <WasmSectionType>
83constexpr const char *wasmSectionName = "";
84
85#define WASM_SEC_TRANSFORM(section) \
86 template <> \
87 [[maybe_unused]] constexpr const char \
88 *wasmSectionName<WasmSectionType::section> = #section;
89APPLY_WASM_SEC_TRANSFORM
90#undef WASM_SEC_TRANSFORM
91
92constexpr bool sectionShouldBeUnique(WasmSectionType secType) {
93 return secType != WasmSectionType::CUSTOM;
94}
95
96template <std::byte... Bytes>
97struct ByteSequence {};
98
99/// Template class for representing a byte sequence of only one byte
100template <std::byte Byte>
101struct UniqueByte : ByteSequence<Byte> {};
102
103[[maybe_unused]] constexpr ByteSequence<
104 WasmBinaryEncoding::Type::i32, WasmBinaryEncoding::Type::i64,
105 WasmBinaryEncoding::Type::f32, WasmBinaryEncoding::Type::f64,
106 WasmBinaryEncoding::Type::v128> valueTypesEncodings{};
107
108template <std::byte... allowedFlags>
109constexpr bool isValueOneOf(std::byte value,
110 ByteSequence<allowedFlags...> = {}) {
111 return ((value == allowedFlags) | ... | false);
112}
113
114template <std::byte... flags>
115constexpr bool isNotIn(std::byte value, ByteSequence<flags...> = {}) {
116 return !isValueOneOf<flags...>(value);
117}
118
119struct GlobalTypeRecord {
120 Type type;
121 bool isMutable;
122};
123
124struct TypeIdxRecord {
125 size_t id;
126};
127
128struct SymbolRefContainer {
129 FlatSymbolRefAttr symbol;
130};
131
132struct GlobalSymbolRefContainer : SymbolRefContainer {
133 Type globalType;
134};
135
136struct FunctionSymbolRefContainer : SymbolRefContainer {
137 FunctionType functionType;
138};
139
140using ImportDesc =
141 std::variant<TypeIdxRecord, TableType, LimitType, GlobalTypeRecord>;
142
143using parsed_inst_t = FailureOr<SmallVector<Value>>;
144
145struct EmptyBlockMarker {};
146using BlockTypeParseResult =
147 std::variant<EmptyBlockMarker, TypeIdxRecord, Type>;
148
149struct WasmModuleSymbolTables {
150 SmallVector<FunctionSymbolRefContainer> funcSymbols;
151 SmallVector<GlobalSymbolRefContainer> globalSymbols;
152 SmallVector<SymbolRefContainer> memSymbols;
153 SmallVector<SymbolRefContainer> tableSymbols;
154 SmallVector<FunctionType> moduleFuncTypes;
155
156 std::string getNewSymbolName(StringRef prefix, size_t id) const {
157 return (prefix + Twine{id}).str();
158 }
159
160 std::string getNewFuncSymbolName() const {
161 size_t id = funcSymbols.size();
162 return getNewSymbolName(prefix: "func_", id);
163 }
164
165 std::string getNewGlobalSymbolName() const {
166 size_t id = globalSymbols.size();
167 return getNewSymbolName(prefix: "global_", id);
168 }
169
170 std::string getNewMemorySymbolName() const {
171 size_t id = memSymbols.size();
172 return getNewSymbolName(prefix: "mem_", id);
173 }
174
175 std::string getNewTableSymbolName() const {
176 size_t id = tableSymbols.size();
177 return getNewSymbolName(prefix: "table_", id);
178 }
179};
180
181class ParserHead;
182
183/// Wrapper around SmallVector to only allow access as push and pop on the
184/// stack. Makes sure that there are no "free accesses" on the stack to preserve
185/// its state.
186/// This class also keep tracks of the Wasm labels defined by different ops,
187/// which can be targeted by control flow ops. This can be modeled as part of
188/// the Value Stack as Wasm control flow ops can only target enclosing labels.
189class ValueStack {
190private:
191 struct LabelLevel {
192 size_t stackIdx;
193 LabelLevelOpInterface levelOp;
194 };
195
196public:
197 bool empty() const { return values.empty(); }
198
199 size_t size() const { return values.size(); }
200
201 /// Pops values from the stack because they are being used in an operation.
202 /// @param operandTypes The list of expected types of the operation, used
203 /// to know how many values to pop and check if the types match the
204 /// expectation.
205 /// @param opLoc Location of the caller, used to report accurately the
206 /// location
207 /// if an error occurs.
208 /// @return Failure or the vector of popped values.
209 FailureOr<SmallVector<Value>> popOperands(TypeRange operandTypes,
210 Location *opLoc);
211
212 /// Push the results of an operation to the stack so they can be used in a
213 /// following operation.
214 /// @param results The list of results of the operation
215 /// @param opLoc Location of the caller, used to report accurately the
216 /// location
217 /// if an error occurs.
218 LogicalResult pushResults(ValueRange results, Location *opLoc);
219
220 void addLabelLevel(LabelLevelOpInterface levelOp) {
221 labelLevel.push_back(Elt: {.stackIdx: values.size(), .levelOp: levelOp});
222 LDBG() << "Adding a new frame context to ValueStack";
223 }
224
225 void dropLabelLevel() {
226 assert(!labelLevel.empty() && "Trying to drop a frame from empty context");
227 auto newSize = labelLevel.pop_back_val().stackIdx;
228 values.truncate(N: newSize);
229 }
230#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
231 /// A simple dump function for debugging.
232 /// Writes output to llvm::dbgs().
233 LLVM_DUMP_METHOD void dump() const;
234#endif
235
236private:
237 SmallVector<Value> values;
238 SmallVector<LabelLevel> labelLevel;
239};
240
241using local_val_t = TypedValue<wasmssa::LocalRefType>;
242
243// MSVC 19.29 cannot parse a pack expansion of a braced conversion
244// (std::byte{IS}...) as a non-type template argument.
245template <size_t I>
246constexpr std::byte byteAt = static_cast<std::byte>(I);
247
248template <size_t... IS>
249constexpr ByteSequence<byteAt<IS>...>
250castIndexSequenceToBytes(std::index_sequence<IS...>) {
251 return {};
252}
253
254constexpr auto all8bitsBytes =
255 castIndexSequenceToBytes(std::make_index_sequence<256>());
256
257template <std::byte>
258struct OpCode {};
259
260class ExpressionParser {
261public:
262 using locals_t = SmallVector<local_val_t>;
263 ExpressionParser(ParserHead &parser, WasmModuleSymbolTables const &symbols,
264 ArrayRef<local_val_t> initLocal)
265 : parser{parser}, symbols{symbols}, locals{initLocal} {}
266
267private:
268 inline parsed_inst_t dispatchToInstParser(std::byte opCode,
269 OpBuilder &builder);
270 ///
271 /// RAII guard class for creating a nesting level
272 ///
273 struct NestingContextGuard {
274 NestingContextGuard(ExpressionParser &parser, LabelLevelOpInterface levelOp)
275 : parser{parser} {
276 parser.addNestingContextLevel(levelOp);
277 }
278 NestingContextGuard(NestingContextGuard &&other) : parser{other.parser} {
279 other.shouldDropOnDestruct = false;
280 }
281 NestingContextGuard(NestingContextGuard const &) = delete;
282 ~NestingContextGuard() {
283 if (shouldDropOnDestruct)
284 parser.dropNestingContextLevel();
285 }
286 ExpressionParser &parser;
287 bool shouldDropOnDestruct = true;
288 };
289
290 void addNestingContextLevel(LabelLevelOpInterface levelOp) {
291 valueStack.addLabelLevel(levelOp);
292 }
293
294 void dropNestingContextLevel() {
295 // Should always succeed as we are droping the frame that was previously
296 // created.
297 valueStack.dropLabelLevel();
298 }
299
300 llvm::FailureOr<FunctionType> getFuncTypeFor(OpBuilder &builder,
301 EmptyBlockMarker) {
302 return builder.getFunctionType(inputs: {}, results: {});
303 }
304
305 llvm::FailureOr<FunctionType> getFuncTypeFor(OpBuilder &builder,
306 TypeIdxRecord type) {
307 if (type.id >= symbols.moduleFuncTypes.size())
308 return emitError(loc: *currentOpLoc,
309 message: "type index references nonexistent type (")
310 << type.id << "). Only " << symbols.moduleFuncTypes.size()
311 << " types are registered";
312 return symbols.moduleFuncTypes[type.id];
313 }
314
315 llvm::FailureOr<FunctionType> getFuncTypeFor(OpBuilder &builder,
316 Type valType) {
317 return builder.getFunctionType(inputs: {}, results: {valType});
318 }
319
320 llvm::FailureOr<FunctionType>
321 getFuncTypeFor(OpBuilder &builder, BlockTypeParseResult parseResult) {
322 return std::visit(
323 visitor: [this, &builder](auto value) { return getFuncTypeFor(builder, value); },
324 variants&: parseResult);
325 }
326
327 llvm::FailureOr<FunctionType>
328 getFuncTypeFor(OpBuilder &builder,
329 llvm::FailureOr<BlockTypeParseResult> parseResult) {
330 if (llvm::failed(Result: parseResult))
331 return failure();
332 return getFuncTypeFor(builder, parseResult: *parseResult);
333 }
334
335 struct ParseResultWithInfo {
336 SmallVector<Value> opResults;
337 std::byte endingByte;
338 };
339
340 llvm::FailureOr<FunctionType> parseBlockFuncType(OpBuilder &builder);
341
342public:
343 template <std::byte ParseEndByte = WasmBinaryEncoding::endByte>
344 parsed_inst_t parse(OpBuilder &builder, UniqueByte<ParseEndByte> = {});
345
346 template <std::byte... ExpressionParseEnd>
347 FailureOr<ParseResultWithInfo>
348 parse(OpBuilder &builder,
349 ByteSequence<ExpressionParseEnd...> parsingEndFilters);
350
351 NestingContextGuard addNesting(LabelLevelOpInterface levelOp) {
352 return NestingContextGuard{*this, levelOp};
353 }
354
355 FailureOr<llvm::SmallVector<Value>> popOperands(TypeRange operandTypes) {
356 return valueStack.popOperands(operandTypes, opLoc: &currentOpLoc.value());
357 }
358
359 LogicalResult pushResults(ValueRange results) {
360 return valueStack.pushResults(results, opLoc: &currentOpLoc.value());
361 }
362
363 /// The local.set and local.tee operations behave similarly and only differ
364 /// on their return value. This function factorizes the behavior of the two
365 /// operations in one place.
366 template <typename OpToCreate>
367 parsed_inst_t parseSetOrTee(OpBuilder &);
368
369 Location getCurrentOpLoc() {
370 assert(currentOpLoc.has_value() &&
371 "expects current opcode location to be set");
372 return *currentOpLoc;
373 }
374
375 class ExprParserProxy {
376 public:
377 friend ExpressionParser;
378 inline auto parseBlockFuncType(OpBuilder &builder) {
379 return exprParser.parseBlockFuncType(builder);
380 }
381
382 /// @param blockToFill: the block which content will be populated
383 /// @param resType: the type that this block is supposed to return
384 template <typename FilterT = ByteSequence<WasmBinaryEncoding::endByte>>
385 llvm::FailureOr<std::byte>
386 parseBlockContent(OpBuilder &builder, Block *blockToFill,
387 TypeRange resTypes, Location opLoc,
388 LabelLevelOpInterface levelOp,
389 FilterT parseEndBytes = {}) {
390 OpBuilder::InsertionGuard guard(builder);
391 assert(blockToFill && blockToFill->empty() && "expected an empty block");
392 builder.setInsertionPointToStart(blockToFill);
393 LDBG() << "parsing a block of type "
394 << builder.getFunctionType(inputs: blockToFill->getArgumentTypes(),
395 results: resTypes);
396 auto nestingGuard = exprParser.addNesting(levelOp);
397
398 if (failed(Result: exprParser.pushResults(results: blockToFill->getArguments())))
399 return failure();
400 auto bodyParsingRes = exprParser.parse(builder, parseEndBytes);
401 if (failed(bodyParsingRes))
402 return failure();
403 auto returnOperands = exprParser.popOperands(operandTypes: resTypes);
404 if (failed(Result: returnOperands))
405 return failure();
406 BlockReturnOp::create(builder, location: opLoc, inputs: *returnOperands);
407 LDBG() << "end of parsing of a block";
408 return bodyParsingRes->endingByte;
409 }
410
411 inline ParserHead &parser() { return exprParser.parser; }
412
413 /// Blocks and Loops have a similar format and differ only in how their exit
414 /// is handled which doesn´t matter at parsing time. Factorizes in one
415 /// function.
416 template <typename OpToCreate>
417 parsed_inst_t parseBlockLikeOp(OpBuilder &);
418
419 inline auto getCurrentOpLoc() { return exprParser.getCurrentOpLoc(); }
420
421 inline auto popOperands(TypeRange operandTypes) {
422 return exprParser.popOperands(operandTypes);
423 }
424
425 inline auto &symbols() { return exprParser.symbols; }
426
427 inline auto &locals() { return exprParser.locals; }
428
429 template <typename OpToCreate>
430 parsed_inst_t parseSetOrTee(OpBuilder &builder) {
431 return exprParser.parseSetOrTee<OpToCreate>(builder);
432 }
433
434 template <typename valueT>
435 parsed_inst_t
436 parseConstInst(OpBuilder &builder,
437 std::enable_if_t<std::is_arithmetic_v<valueT>> * = nullptr);
438
439 /// Construct an operation with \p numOperands operands and a single result.
440 /// Each operand must have the same type. Suitable for e.g. binops, unary
441 /// ops, etc.
442 ///
443 /// \p opcode - The WASM opcode to build.
444 /// \p valueType - The operand and result type for the built instruction.
445 /// \p numOperands - The number of operands for the built operation.
446 ///
447 /// \returns The parsed instruction result, or failure.
448 template <typename opcode, typename valueType, unsigned int numOperands>
449 inline parsed_inst_t buildNumericOp(
450 OpBuilder &builder,
451 std::enable_if_t<std::is_arithmetic_v<valueType>> * = nullptr);
452
453 /// Construct a conversion operation of type \p opType that takes a value
454 /// from
455 /// type \p inputType on the stack and will produce a value of type
456 /// \p outputType.
457 ///
458 /// \p opType - The WASM dialect operation to build.
459 /// \p inputType - The operand type for the built instruction.
460 /// \p outputType - The result type for the built instruction.
461 ///
462 /// \returns The parsed instruction result, or failure.
463 template <typename opType, typename inputType, typename outputType,
464 typename... extraArgsT>
465 inline parsed_inst_t buildConvertOp(OpBuilder &builder, extraArgsT...);
466
467 private:
468 explicit ExprParserProxy(ExpressionParser &exprParser)
469 : exprParser{exprParser} {};
470
471 private:
472 ExpressionParser &exprParser;
473 };
474
475private:
476 std::optional<Location> currentOpLoc;
477 ParserHead &parser;
478 WasmModuleSymbolTables const &symbols;
479 locals_t locals;
480 ValueStack valueStack;
481};
482
483class ParserHead {
484public:
485 ParserHead(StringRef src, StringAttr name) : head{src}, locName{name} {}
486 ParserHead(ParserHead &&) = default;
487
488private:
489 ParserHead(ParserHead const &other) = default;
490
491public:
492 auto getLocation() const {
493 return FileLineColLoc::get(filename: locName, line: 0, column: anchorOffset + offset);
494 }
495
496 FailureOr<StringRef> consumeNBytes(size_t nBytes) {
497 LDBG() << "Consume " << nBytes << " bytes";
498 LDBG() << " Bytes remaining: " << size();
499 LDBG() << " Current offset: " << offset;
500 if (nBytes > size())
501 return emitError(loc: getLocation(), message: "trying to extract ")
502 << nBytes << "bytes when only " << size() << "are available";
503
504 StringRef res = head.slice(Start: offset, End: offset + nBytes);
505 offset += nBytes;
506 LDBG() << " Updated offset (+" << nBytes << "): " << offset;
507 return res;
508 }
509
510 FailureOr<std::byte> consumeByte() {
511 FailureOr<StringRef> res = consumeNBytes(nBytes: 1);
512 if (failed(Result: res))
513 return failure();
514 return std::byte{*res->bytes_begin()};
515 }
516
517 template <typename T>
518 FailureOr<T> parseLiteral();
519
520 FailureOr<uint32_t> parseVectorSize();
521
522private:
523 // TODO: This is equivalent to parseLiteral<uint32_t> and could be removed
524 // if parseLiteral specialization were moved here, but default GCC on Ubuntu
525 // 22.04 has bug with template specialization in class declaration
526 inline FailureOr<uint32_t> parseUI32();
527 inline FailureOr<int64_t> parseI64();
528
529public:
530 FailureOr<StringRef> parseName() {
531 FailureOr<uint32_t> size = parseVectorSize();
532 if (failed(Result: size))
533 return failure();
534
535 return consumeNBytes(nBytes: *size);
536 }
537
538 FailureOr<WasmSectionType> parseWasmSectionType() {
539 FailureOr<std::byte> id = consumeByte();
540 if (failed(Result: id))
541 return failure();
542 if (std::to_integer<unsigned>(b: *id) > highestWasmSectionID)
543 return emitError(loc: getLocation(), message: "invalid section ID: ")
544 << static_cast<int>(*id);
545 return static_cast<WasmSectionType>(*id);
546 }
547
548 FailureOr<LimitType> parseLimit(MLIRContext *ctx) {
549 using WasmLimits = WasmBinaryEncoding::LimitHeader;
550 FileLineColLoc limitLocation = getLocation();
551 FailureOr<std::byte> limitHeader = consumeByte();
552 if (failed(Result: limitHeader))
553 return failure();
554
555 if (isNotIn<WasmLimits::bothLimits, WasmLimits::lowLimitOnly>(value: *limitHeader))
556 return emitError(loc: limitLocation, message: "invalid limit header: ")
557 << static_cast<int>(*limitHeader);
558 FailureOr<uint32_t> minParse = parseUI32();
559 if (failed(Result: minParse))
560 return failure();
561 std::optional<uint32_t> max{std::nullopt};
562 if (*limitHeader == WasmLimits::bothLimits) {
563 FailureOr<uint32_t> maxParse = parseUI32();
564 if (failed(Result: maxParse))
565 return failure();
566 max = *maxParse;
567 }
568 return LimitType::get(context: ctx, min: *minParse, max);
569 }
570
571 FailureOr<Type> parseValueType(MLIRContext *ctx) {
572 FileLineColLoc typeLoc = getLocation();
573 FailureOr<std::byte> typeEncoding = consumeByte();
574 if (failed(Result: typeEncoding))
575 return failure();
576 switch (*typeEncoding) {
577 case WasmBinaryEncoding::Type::i32:
578 return IntegerType::get(context: ctx, width: 32);
579 case WasmBinaryEncoding::Type::i64:
580 return IntegerType::get(context: ctx, width: 64);
581 case WasmBinaryEncoding::Type::f32:
582 return Float32Type::get(context: ctx);
583 case WasmBinaryEncoding::Type::f64:
584 return Float64Type::get(context: ctx);
585 case WasmBinaryEncoding::Type::v128:
586 return IntegerType::get(context: ctx, width: 128);
587 case WasmBinaryEncoding::Type::funcRef:
588 return wasmssa::FuncRefType::get(ctx);
589 case WasmBinaryEncoding::Type::externRef:
590 return wasmssa::ExternRefType::get(ctx);
591 default:
592 return emitError(loc: typeLoc, message: "invalid value type encoding: ")
593 << static_cast<int>(*typeEncoding);
594 }
595 }
596
597 FailureOr<GlobalTypeRecord> parseGlobalType(MLIRContext *ctx) {
598 using WasmGlobalMut = WasmBinaryEncoding::GlobalMutability;
599 FailureOr<Type> typeParsed = parseValueType(ctx);
600 if (failed(Result: typeParsed))
601 return failure();
602 FileLineColLoc mutLoc = getLocation();
603 FailureOr<std::byte> mutSpec = consumeByte();
604 if (failed(Result: mutSpec))
605 return failure();
606 if (isNotIn<WasmGlobalMut::isConst, WasmGlobalMut::isMutable>(value: *mutSpec))
607 return emitError(loc: mutLoc, message: "invalid global mutability specifier: ")
608 << static_cast<int>(*mutSpec);
609 return GlobalTypeRecord{.type: *typeParsed, .isMutable: *mutSpec == WasmGlobalMut::isMutable};
610 }
611
612 FailureOr<TupleType> parseResultType(MLIRContext *ctx) {
613 FailureOr<uint32_t> nParamsParsed = parseVectorSize();
614 if (failed(Result: nParamsParsed))
615 return failure();
616 uint32_t nParams = *nParamsParsed;
617 SmallVector<Type> res{};
618 res.reserve(N: nParams);
619 for (size_t i = 0; i < nParams; ++i) {
620 FailureOr<Type> parsedType = parseValueType(ctx);
621 if (failed(Result: parsedType))
622 return failure();
623 res.push_back(Elt: *parsedType);
624 }
625 return TupleType::get(context: ctx, elementTypes: res);
626 }
627
628 FailureOr<FunctionType> parseFunctionType(MLIRContext *ctx) {
629 FileLineColLoc typeLoc = getLocation();
630 FailureOr<std::byte> funcTypeHeader = consumeByte();
631 if (failed(Result: funcTypeHeader))
632 return failure();
633 if (*funcTypeHeader != WasmBinaryEncoding::Type::funcType)
634 return emitError(loc: typeLoc, message: "invalid function type header byte. Expecting ")
635 << std::to_integer<unsigned>(b: WasmBinaryEncoding::Type::funcType)
636 << " got " << std::to_integer<unsigned>(b: *funcTypeHeader);
637 FailureOr<TupleType> inputTypes = parseResultType(ctx);
638 if (failed(Result: inputTypes))
639 return failure();
640
641 FailureOr<TupleType> resTypes = parseResultType(ctx);
642 if (failed(Result: resTypes))
643 return failure();
644
645 return FunctionType::get(context: ctx, inputs: inputTypes->getTypes(), results: resTypes->getTypes());
646 }
647
648 FailureOr<TypeIdxRecord> parseTypeIndex() {
649 FailureOr<uint32_t> res = parseUI32();
650 if (failed(Result: res))
651 return failure();
652 return TypeIdxRecord{.id: *res};
653 }
654
655 FailureOr<TableType> parseTableType(MLIRContext *ctx) {
656 FailureOr<Type> elmTypeParse = parseValueType(ctx);
657 if (failed(Result: elmTypeParse))
658 return failure();
659 if (!isWasmRefType(type: *elmTypeParse))
660 return emitError(loc: getLocation(), message: "invalid element type for table");
661 FailureOr<LimitType> limitParse = parseLimit(ctx);
662 if (failed(Result: limitParse))
663 return failure();
664 return TableType::get(context: ctx, reference: *elmTypeParse, limit: *limitParse);
665 }
666
667 FailureOr<ImportDesc> parseImportDesc(MLIRContext *ctx) {
668 FileLineColLoc importLoc = getLocation();
669 FailureOr<std::byte> importType = consumeByte();
670 auto packager = [](auto parseResult) -> FailureOr<ImportDesc> {
671 if (failed(parseResult))
672 return failure();
673 return {*parseResult};
674 };
675 if (failed(Result: importType))
676 return failure();
677 switch (*importType) {
678 case WasmBinaryEncoding::Import::typeID:
679 return packager(parseTypeIndex());
680 case WasmBinaryEncoding::Import::tableType:
681 return packager(parseTableType(ctx));
682 case WasmBinaryEncoding::Import::memType:
683 return packager(parseLimit(ctx));
684 case WasmBinaryEncoding::Import::globalType:
685 return packager(parseGlobalType(ctx));
686 default:
687 return emitError(loc: importLoc, message: "invalid import type descriptor: ")
688 << static_cast<int>(*importType);
689 }
690 }
691
692 parsed_inst_t parseExpression(OpBuilder &builder,
693 WasmModuleSymbolTables const &symbols,
694 ArrayRef<local_val_t> locals = {}) {
695 auto eParser = ExpressionParser{*this, symbols, locals};
696 return eParser.parse(builder);
697 }
698
699 LogicalResult parseCodeFor(FuncOp func,
700 WasmModuleSymbolTables const &symbols) {
701 SmallVector<local_val_t> locals{};
702 // Populating locals with function argument
703 Block &block = func.getBody().front();
704 // Delete temporary return argument which was only created for IR validity
705 assert(func.getBody().getBlocks().size() == 1 &&
706 "Function should only have its default created block at this point");
707 assert(block.getOperations().size() == 1 &&
708 "Only the placeholder return op should be present at this point");
709 auto returnOp = cast<ReturnOp>(Val: &block.back());
710 assert(returnOp);
711
712 FailureOr<uint32_t> codeSizeInBytes = parseUI32();
713 if (failed(Result: codeSizeInBytes))
714 return failure();
715 FailureOr<StringRef> codeContent = consumeNBytes(nBytes: *codeSizeInBytes);
716 if (failed(Result: codeContent))
717 return failure();
718 auto name = StringAttr::get(context: func->getContext(),
719 bytes: locName.str() + "::" + func.getSymName());
720 auto cParser = ParserHead{*codeContent, name};
721 FailureOr<uint32_t> localVecSize = cParser.parseVectorSize();
722 if (failed(Result: localVecSize))
723 return failure();
724 OpBuilder builder{&func.getBody().front().back()};
725 for (auto arg : block.getArguments())
726 locals.push_back(Elt: cast<TypedValue<LocalRefType>>(Val&: arg));
727 // Declare the local ops
728 uint32_t nVarVec = *localVecSize;
729 for (size_t i = 0; i < nVarVec; ++i) {
730 FileLineColLoc varLoc = cParser.getLocation();
731 FailureOr<uint32_t> nSubVar = cParser.parseUI32();
732 if (failed(Result: nSubVar))
733 return failure();
734 FailureOr<Type> varT = cParser.parseValueType(ctx: func->getContext());
735 if (failed(Result: varT))
736 return failure();
737 for (size_t j = 0; j < *nSubVar; ++j) {
738 auto local = LocalOp::create(builder, location: varLoc, type: *varT);
739 locals.push_back(Elt: local.getResult());
740 }
741 }
742 parsed_inst_t res = cParser.parseExpression(builder, symbols, locals);
743 if (failed(Result: res))
744 return failure();
745 if (!cParser.end())
746 return emitError(loc: cParser.getLocation(),
747 message: "unparsed garbage remaining at end of code block");
748 ReturnOp::create(builder, location: func->getLoc(), operands: *res);
749 returnOp->erase();
750 return success();
751 }
752
753 llvm::FailureOr<BlockTypeParseResult> parseBlockType(MLIRContext *ctx) {
754 auto loc = getLocation();
755 auto blockIndicator = peek();
756 if (failed(Result: blockIndicator))
757 return failure();
758 if (*blockIndicator == WasmBinaryEncoding::Type::emptyBlockType) {
759 offset += 1;
760 return {EmptyBlockMarker{}};
761 }
762 if (isValueOneOf(value: *blockIndicator, valueTypesEncodings))
763 return parseValueType(ctx);
764 /// Block type idx is a 32 bit positive integer encoded as a 33 bit signed
765 /// value
766 auto typeIdx = parseI64();
767 if (failed(Result: typeIdx))
768 return failure();
769 if (*typeIdx < 0 || *typeIdx > std::numeric_limits<uint32_t>::max())
770 return emitError(loc, message: "type ID should be representable with an unsigned "
771 "32 bits integer. Got ")
772 << *typeIdx;
773 return {TypeIdxRecord{.id: static_cast<uint32_t>(*typeIdx)}};
774 }
775
776 bool end() const { return curHead().empty(); }
777
778 ParserHead copy() const { return *this; }
779
780private:
781 StringRef curHead() const { return head.drop_front(N: offset); }
782
783 FailureOr<std::byte> peek() const {
784 if (end())
785 return emitError(
786 loc: getLocation(),
787 message: "trying to peek at next byte, but input stream is empty");
788 return static_cast<std::byte>(curHead().front());
789 }
790
791 size_t size() const { return head.size() - offset; }
792
793 StringRef head;
794 StringAttr locName;
795 unsigned anchorOffset{0};
796 unsigned offset{0};
797};
798
799template <>
800FailureOr<float> ParserHead::parseLiteral<float>() {
801 FailureOr<StringRef> bytes = consumeNBytes(nBytes: 4);
802 if (failed(Result: bytes))
803 return failure();
804 return llvm::support::endian::read<float>(memory: bytes->bytes_begin(),
805 endian: llvm::endianness::little);
806}
807
808template <>
809FailureOr<double> ParserHead::parseLiteral<double>() {
810 FailureOr<StringRef> bytes = consumeNBytes(nBytes: 8);
811 if (failed(Result: bytes))
812 return failure();
813 return llvm::support::endian::read<double>(memory: bytes->bytes_begin(),
814 endian: llvm::endianness::little);
815}
816
817template <>
818FailureOr<uint32_t> ParserHead::parseLiteral<uint32_t>() {
819 char const *error = nullptr;
820 uint32_t res{0};
821 unsigned encodingSize{0};
822 StringRef src = curHead();
823 uint64_t decoded = llvm::decodeULEB128(p: src.bytes_begin(), n: &encodingSize,
824 end: src.bytes_end(), error: &error);
825 if (error)
826 return emitError(loc: getLocation(), message: error);
827
828 if (std::isgreater(x: decoded, y: std::numeric_limits<uint32_t>::max()))
829 return emitError(loc: getLocation()) << "literal does not fit on 32 bits";
830
831 res = static_cast<uint32_t>(decoded);
832 offset += encodingSize;
833 return res;
834}
835
836template <>
837FailureOr<int32_t> ParserHead::parseLiteral<int32_t>() {
838 char const *error = nullptr;
839 int32_t res{0};
840 unsigned encodingSize{0};
841 StringRef src = curHead();
842 int64_t decoded = llvm::decodeSLEB128(p: src.bytes_begin(), n: &encodingSize,
843 end: src.bytes_end(), error: &error);
844 if (error)
845 return emitError(loc: getLocation(), message: error);
846 if (std::isgreater(x: decoded, y: std::numeric_limits<int32_t>::max()) ||
847 std::isgreater(x: std::numeric_limits<int32_t>::min(), y: decoded))
848 return emitError(loc: getLocation()) << "literal does not fit on 32 bits";
849
850 res = static_cast<int32_t>(decoded);
851 offset += encodingSize;
852 return res;
853}
854
855template <>
856FailureOr<int64_t> ParserHead::parseLiteral<int64_t>() {
857 char const *error = nullptr;
858 unsigned encodingSize{0};
859 StringRef src = curHead();
860 int64_t res = llvm::decodeSLEB128(p: src.bytes_begin(), n: &encodingSize,
861 end: src.bytes_end(), error: &error);
862 if (error)
863 return emitError(loc: getLocation(), message: error);
864
865 offset += encodingSize;
866 return res;
867}
868
869FailureOr<uint32_t> ParserHead::parseVectorSize() {
870 return parseLiteral<uint32_t>();
871}
872
873inline FailureOr<uint32_t> ParserHead::parseUI32() {
874 return parseLiteral<uint32_t>();
875}
876
877inline FailureOr<int64_t> ParserHead::parseI64() {
878 return parseLiteral<int64_t>();
879}
880
881#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
882void ValueStack::dump() const {
883 llvm::dbgs() << "================= Wasm ValueStack =======================\n";
884 llvm::dbgs() << "size: " << size() << "\n";
885 llvm::dbgs() << "nbFrames: " << labelLevel.size() << '\n';
886 llvm::dbgs() << "<Top>"
887 << "\n";
888 // Stack is pushed to via push_back. Therefore the top of the stack is the
889 // end of the vector. Iterate in reverse so that the first thing we print
890 // is the top of the stack.
891 auto indexGetter = [this]() {
892 size_t idx = labelLevel.size();
893 return [this, idx]() mutable -> std::optional<std::pair<size_t, size_t>> {
894 llvm::dbgs() << "IDX: " << idx << '\n';
895 if (idx == 0)
896 return std::nullopt;
897 auto frameId = idx - 1;
898 auto frameLimit = labelLevel[frameId].stackIdx;
899 idx -= 1;
900 return {{frameId, frameLimit}};
901 };
902 };
903 auto getNextFrameIndex = indexGetter();
904 auto nextFrameIdx = getNextFrameIndex();
905 size_t stackSize = size();
906 for (size_t idx = 0; idx < stackSize; ++idx) {
907 size_t actualIdx = stackSize - 1 - idx;
908 while (nextFrameIdx && (nextFrameIdx->second > actualIdx)) {
909 llvm::dbgs() << " --------------- Frame (" << nextFrameIdx->first
910 << ")\n";
911 nextFrameIdx = getNextFrameIndex();
912 }
913 llvm::dbgs() << " ";
914 values[actualIdx].dump();
915 }
916 while (nextFrameIdx) {
917 llvm::dbgs() << " --------------- Frame (" << nextFrameIdx->first << ")\n";
918 nextFrameIdx = getNextFrameIndex();
919 }
920 llvm::dbgs() << "<Bottom>"
921 << "\n";
922 llvm::dbgs() << "=========================================================\n";
923}
924#endif
925
926parsed_inst_t ValueStack::popOperands(TypeRange operandTypes, Location *opLoc) {
927 LDBG() << "Popping from ValueStack\n"
928 << " Elements(s) to pop: " << operandTypes.size() << "\n"
929 << " Current stack size: " << values.size();
930 if (operandTypes.size() > values.size())
931 return emitError(loc: *opLoc,
932 message: "stack doesn't contain enough values. trying to get ")
933 << operandTypes.size() << " operands on a stack containing only "
934 << values.size() << " values";
935 size_t stackIdxOffset = values.size() - operandTypes.size();
936 SmallVector<Value> res{};
937 res.reserve(N: operandTypes.size());
938 for (size_t i{0}; i < operandTypes.size(); ++i) {
939 Value operand = values[i + stackIdxOffset];
940 Type stackType = operand.getType();
941 if (stackType != operandTypes[i])
942 return emitError(loc: *opLoc, message: "invalid operand type on stack. expecting ")
943 << operandTypes[i] << ", value on stack is of type " << stackType;
944 LDBG() << " POP: " << operand;
945 res.push_back(Elt: operand);
946 }
947 values.resize(N: values.size() - operandTypes.size());
948 LDBG() << " Updated stack size: " << values.size();
949 return res;
950}
951
952LogicalResult ValueStack::pushResults(ValueRange results, Location *opLoc) {
953 LDBG() << "Pushing to ValueStack\n"
954 << " Elements(s) to push: " << results.size() << "\n"
955 << " Current stack size: " << values.size();
956 for (Value val : results) {
957 if (!isWasmValueType(type: val.getType()))
958 return emitError(loc: *opLoc, message: "invalid value type on stack: ")
959 << val.getType();
960 LDBG() << " PUSH: " << val;
961 values.push_back(Elt: val);
962 }
963
964 LDBG() << " Updated stack size: " << values.size();
965 return success();
966}
967
968template <std::byte EndParseByte>
969parsed_inst_t ExpressionParser::parse(OpBuilder &builder,
970 UniqueByte<EndParseByte> endByte) {
971 auto res = parse(builder, ByteSequence<EndParseByte>{});
972 if (failed(res))
973 return failure();
974 return res->opResults;
975}
976
977template <std::byte... ExpressionParseEnd>
978FailureOr<ExpressionParser::ParseResultWithInfo>
979ExpressionParser::parse(OpBuilder &builder,
980 ByteSequence<ExpressionParseEnd...> parsingEndFilters) {
981 SmallVector<Value> res;
982 for (;;) {
983 currentOpLoc = parser.getLocation();
984 FailureOr<std::byte> opCode = parser.consumeByte();
985 if (failed(Result: opCode))
986 return failure();
987 if (isValueOneOf(*opCode, parsingEndFilters))
988 return {{.opResults: res, .endingByte: *opCode}};
989 parsed_inst_t resParsed;
990 resParsed = dispatchToInstParser(opCode: *opCode, builder);
991 if (failed(Result: resParsed))
992 return failure();
993 std::swap(LHS&: res, RHS&: *resParsed);
994 if (failed(Result: pushResults(results: res)))
995 return failure();
996 }
997}
998
999llvm::FailureOr<FunctionType>
1000ExpressionParser::parseBlockFuncType(OpBuilder &builder) {
1001 return getFuncTypeFor(builder, parseResult: parser.parseBlockType(ctx: builder.getContext()));
1002}
1003
1004template <typename OpToCreate>
1005parsed_inst_t
1006ExpressionParser::ExprParserProxy::parseBlockLikeOp(OpBuilder &builder) {
1007 auto opLoc = getCurrentOpLoc();
1008 auto funcType = parseBlockFuncType(builder);
1009 if (failed(Result: funcType))
1010 return failure();
1011
1012 auto inputTypes = funcType->getInputs();
1013 auto inputOps = popOperands(operandTypes: inputTypes);
1014 if (failed(Result: inputOps))
1015 return failure();
1016
1017 Block *curBlock = builder.getBlock();
1018 Region *curRegion = curBlock->getParent();
1019 auto resTypes = funcType->getResults();
1020 llvm::SmallVector<Location> locations{};
1021 locations.resize(N: resTypes.size(), NV: getCurrentOpLoc());
1022 auto *successor =
1023 builder.createBlock(parent: curRegion, insertPt: curRegion->end(), argTypes: resTypes, locs: locations);
1024 builder.setInsertionPointToEnd(curBlock);
1025 auto blockOp =
1026 OpToCreate::create(builder, getCurrentOpLoc(), *inputOps, successor);
1027 auto *blockBody = blockOp.createBlock();
1028 if (failed(parseBlockContent(builder, blockBody, resTypes, opLoc, blockOp)))
1029 return failure();
1030 builder.setInsertionPointToStart(successor);
1031 return {ValueRange{successor->getArguments()}};
1032}
1033
1034parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::block>,
1035 OpBuilder &builder,
1036 ExpressionParser::ExprParserProxy &exprParser) {
1037 return exprParser.parseBlockLikeOp<BlockOp>(builder);
1038}
1039
1040parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::loop>,
1041 OpBuilder &builder,
1042 ExpressionParser::ExprParserProxy &exprParser) {
1043 return exprParser.parseBlockLikeOp<LoopOp>(builder);
1044}
1045
1046parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::ifOpCode>,
1047 OpBuilder &builder,
1048 ExpressionParser::ExprParserProxy &exprParser) {
1049 auto opLoc = exprParser.getCurrentOpLoc();
1050 auto funcType = exprParser.parseBlockFuncType(builder);
1051 if (failed(Result: funcType))
1052 return failure();
1053
1054 LDBG() << "Parsing an if instruction of type " << *funcType;
1055 auto inputTypes = funcType->getInputs();
1056 auto conditionValue = exprParser.popOperands(operandTypes: builder.getI32Type());
1057 if (failed(Result: conditionValue))
1058 return failure();
1059 auto inputOps = exprParser.popOperands(operandTypes: inputTypes);
1060 if (failed(Result: inputOps))
1061 return failure();
1062
1063 Block *curBlock = builder.getBlock();
1064 Region *curRegion = curBlock->getParent();
1065 auto resTypes = funcType->getResults();
1066 llvm::SmallVector<Location> locations{};
1067 locations.resize(N: resTypes.size(), NV: exprParser.getCurrentOpLoc());
1068 auto *successor =
1069 builder.createBlock(parent: curRegion, insertPt: curRegion->end(), argTypes: resTypes, locs: locations);
1070 builder.setInsertionPointToEnd(curBlock);
1071 auto ifOp = IfOp::create(builder, location: exprParser.getCurrentOpLoc(),
1072 condition: conditionValue->front(), inputs: *inputOps, target: successor);
1073 auto *ifEntryBlock = ifOp.createIfBlock();
1074 constexpr auto ifElseFilter =
1075 ByteSequence<WasmBinaryEncoding::endByte,
1076 WasmBinaryEncoding::OpCode::elseOpCode>{};
1077 auto parseIfRes = exprParser.parseBlockContent(
1078 builder, blockToFill: ifEntryBlock, resTypes, opLoc, levelOp: ifOp, parseEndBytes: ifElseFilter);
1079 if (failed(Result: parseIfRes))
1080 return failure();
1081 if (*parseIfRes == WasmBinaryEncoding::OpCode::elseOpCode) {
1082 LDBG() << " else block is present.";
1083 Block *elseEntryBlock = ifOp.createElseBlock();
1084 auto parseElseRes = exprParser.parseBlockContent(builder, blockToFill: elseEntryBlock,
1085 resTypes, opLoc, levelOp: ifOp);
1086 if (failed(Result: parseElseRes))
1087 return failure();
1088 }
1089 builder.setInsertionPointToStart(successor);
1090 return {ValueRange{successor->getArguments()}};
1091}
1092
1093parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::branchIf>,
1094 OpBuilder &builder,
1095 ExpressionParser::ExprParserProxy &exprParser) {
1096 auto level = exprParser.parser().parseLiteral<uint32_t>();
1097 if (failed(Result: level))
1098 return failure();
1099 Block *curBlock = builder.getBlock();
1100 Region *curRegion = curBlock->getParent();
1101 auto sip = builder.saveInsertionPoint();
1102 Block *elseBlock = builder.createBlock(parent: curRegion, insertPt: curRegion->end());
1103 auto condition = exprParser.popOperands(operandTypes: builder.getI32Type());
1104 if (failed(Result: condition))
1105 return failure();
1106 builder.restoreInsertionPoint(ip: sip);
1107 auto targetOp =
1108 LabelBranchingOpInterface::getTargetOpFromBlock(block: curBlock, level: *level);
1109 if (failed(Result: targetOp))
1110 return failure();
1111 auto inputTypes = targetOp->getLabelTarget()->getArgumentTypes();
1112 auto branchArgs = exprParser.popOperands(operandTypes: inputTypes);
1113 if (failed(Result: branchArgs))
1114 return failure();
1115 BranchIfOp::create(builder, location: exprParser.getCurrentOpLoc(), condition: condition->front(),
1116 exitLevel: builder.getUI32IntegerAttr(value: *level), inputs: *branchArgs,
1117 elseSuccessor: elseBlock);
1118 builder.setInsertionPointToStart(elseBlock);
1119 return {*branchArgs};
1120}
1121
1122parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::call>,
1123 OpBuilder &builder,
1124 ExpressionParser::ExprParserProxy &exprParser) {
1125 auto loc = exprParser.getCurrentOpLoc();
1126 auto funcIdx = exprParser.parser().parseLiteral<uint32_t>();
1127 if (failed(Result: funcIdx))
1128 return failure();
1129 if (*funcIdx >= exprParser.symbols().funcSymbols.size())
1130 return emitError(loc, message: "Invalid function index: ") << *funcIdx;
1131 auto callee = exprParser.symbols().funcSymbols[*funcIdx];
1132 llvm::ArrayRef<Type> inTypes = callee.functionType.getInputs();
1133 llvm::ArrayRef<Type> resTypes = callee.functionType.getResults();
1134 parsed_inst_t inOperands = exprParser.popOperands(operandTypes: inTypes);
1135 if (failed(Result: inOperands))
1136 return failure();
1137 auto callOp =
1138 FuncCallOp::create(builder, location: loc, results: resTypes, callee: callee.symbol, operands: *inOperands);
1139 return {callOp.getResults()};
1140}
1141
1142parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::localGet>,
1143 OpBuilder &builder,
1144 ExpressionParser::ExprParserProxy &exprParser) {
1145 FailureOr<uint32_t> id = exprParser.parser().parseLiteral<uint32_t>();
1146 Location instLoc = exprParser.getCurrentOpLoc();
1147 if (failed(Result: id))
1148 return failure();
1149 if (*id >= exprParser.locals().size())
1150 return emitError(loc: instLoc, message: "invalid local index. function has ")
1151 << exprParser.locals().size()
1152 << " accessible locals, received index " << *id;
1153 return {{LocalGetOp::create(builder, location: instLoc, localVar: exprParser.locals()[*id])
1154 .getResult()}};
1155}
1156
1157parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::globalGet>,
1158 OpBuilder &builder,
1159 ExpressionParser::ExprParserProxy &exprParser) {
1160 FailureOr<uint32_t> id = exprParser.parser().parseLiteral<uint32_t>();
1161 Location instLoc = exprParser.getCurrentOpLoc();
1162 if (failed(Result: id))
1163 return failure();
1164 if (*id >= exprParser.symbols().globalSymbols.size())
1165 return emitError(loc: instLoc, message: "invalid global index. function has ")
1166 << exprParser.symbols().globalSymbols.size()
1167 << " accessible globals, received index " << *id;
1168 GlobalSymbolRefContainer globalVar = exprParser.symbols().globalSymbols[*id];
1169 auto globalOp = GlobalGetOp::create(builder, location: instLoc, global_val: globalVar.globalType,
1170 global: globalVar.symbol);
1171
1172 return {{globalOp.getResult()}};
1173}
1174
1175template <typename OpToCreate>
1176parsed_inst_t ExpressionParser::parseSetOrTee(OpBuilder &builder) {
1177 FailureOr<uint32_t> id = parser.parseLiteral<uint32_t>();
1178 if (failed(Result: id))
1179 return failure();
1180 if (*id >= locals.size())
1181 return emitError(loc: *currentOpLoc, message: "invalid local index. function has ")
1182 << locals.size() << " accessible locals, received index " << *id;
1183 if (valueStack.empty())
1184 return emitError(
1185 loc: *currentOpLoc,
1186 message: "invalid stack access, trying to access a value on an empty stack");
1187
1188 parsed_inst_t poppedOp = popOperands(operandTypes: locals[*id].getType().getElementType());
1189 if (failed(Result: poppedOp))
1190 return failure();
1191 return {
1192 OpToCreate::create(builder, *currentOpLoc, locals[*id], poppedOp->front())
1193 ->getResults()};
1194}
1195
1196parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::localSet>,
1197 OpBuilder &builder,
1198 ExpressionParser::ExprParserProxy &exprParser) {
1199 return exprParser.parseSetOrTee<LocalSetOp>(builder);
1200}
1201
1202parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::localTee>,
1203 OpBuilder &builder,
1204 ExpressionParser::ExprParserProxy &exprParser) {
1205 return exprParser.parseSetOrTee<LocalTeeOp>(builder);
1206}
1207
1208template <typename T>
1209inline Type buildLiteralType(OpBuilder &);
1210
1211template <>
1212inline Type buildLiteralType<int32_t>(OpBuilder &builder) {
1213 return builder.getI32Type();
1214}
1215
1216template <>
1217inline Type buildLiteralType<int64_t>(OpBuilder &builder) {
1218 return builder.getI64Type();
1219}
1220
1221template <>
1222[[maybe_unused]] inline Type buildLiteralType<uint32_t>(OpBuilder &builder) {
1223 return builder.getI32Type();
1224}
1225
1226template <>
1227[[maybe_unused]] inline Type buildLiteralType<uint64_t>(OpBuilder &builder) {
1228 return builder.getI64Type();
1229}
1230
1231template <>
1232inline Type buildLiteralType<float>(OpBuilder &builder) {
1233 return builder.getF32Type();
1234}
1235
1236template <>
1237inline Type buildLiteralType<double>(OpBuilder &builder) {
1238 return builder.getF64Type();
1239}
1240
1241template <typename ValT,
1242 typename E = std::enable_if_t<std::is_arithmetic_v<ValT>>>
1243struct AttrHolder;
1244
1245template <typename ValT>
1246struct AttrHolder<ValT, std::enable_if_t<std::is_integral_v<ValT>>> {
1247 using type = IntegerAttr;
1248};
1249
1250template <typename ValT>
1251struct AttrHolder<ValT, std::enable_if_t<std::is_floating_point_v<ValT>>> {
1252 using type = FloatAttr;
1253};
1254
1255template <typename ValT>
1256using attr_holder_t = typename AttrHolder<ValT>::type;
1257
1258template <typename ValT,
1259 typename EnableT = std::enable_if_t<std::is_arithmetic_v<ValT>>>
1260attr_holder_t<ValT> buildLiteralAttr(OpBuilder &builder, ValT val) {
1261 return attr_holder_t<ValT>::get(buildLiteralType<ValT>(builder), val);
1262}
1263
1264template <typename valueT>
1265parsed_inst_t ExpressionParser::ExprParserProxy::parseConstInst(
1266 OpBuilder &builder, std::enable_if_t<std::is_arithmetic_v<valueT>> *) {
1267 auto parsedConstant = parser().parseLiteral<valueT>();
1268 if (failed(parsedConstant))
1269 return failure();
1270 auto constOp =
1271 ConstOp::create(builder, getCurrentOpLoc(),
1272 buildLiteralAttr<valueT>(builder, *parsedConstant));
1273 return {{constOp.getResult()}};
1274}
1275
1276parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::constI32>,
1277 OpBuilder &builder,
1278 ExpressionParser::ExprParserProxy &exprParser) {
1279 return exprParser.parseConstInst<int32_t>(builder);
1280}
1281
1282parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::constI64>,
1283 OpBuilder &builder,
1284 ExpressionParser::ExprParserProxy &exprParser) {
1285 return exprParser.parseConstInst<int64_t>(builder);
1286}
1287
1288parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::constFP32>,
1289 OpBuilder &builder,
1290 ExpressionParser::ExprParserProxy &exprParser) {
1291 return exprParser.parseConstInst<float>(builder);
1292}
1293
1294parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::constFP64>,
1295 OpBuilder &builder,
1296 ExpressionParser::ExprParserProxy &exprParser) {
1297 return exprParser.parseConstInst<double>(builder);
1298}
1299
1300template <typename opcode, typename valueType, unsigned int numOperands>
1301inline parsed_inst_t ExpressionParser::ExprParserProxy::buildNumericOp(
1302 OpBuilder &builder, std::enable_if_t<std::is_arithmetic_v<valueType>> *) {
1303 auto ty = buildLiteralType<valueType>(builder);
1304 LDBG() << "*** buildNumericOp: numOperands = " << numOperands
1305 << ", type = " << ty << " ***";
1306 auto tysToPop = SmallVector<Type, numOperands>();
1307 tysToPop.resize(numOperands);
1308 llvm::fill(tysToPop, ty);
1309 auto operands = popOperands(operandTypes: tysToPop);
1310 if (failed(operands))
1311 return failure();
1312 auto op = opcode::create(builder, getCurrentOpLoc(), *operands).getResult();
1313 LDBG() << "Built operation: " << op;
1314 return {{op}};
1315}
1316
1317// Convenience macro for generating numerical operations.
1318#define BUILD_NUMERIC_OP(OP_NAME, N_ARGS, PREFIX, SUFFIX, TYPE) \
1319 inline parsed_inst_t parse( \
1320 OpCode<WasmBinaryEncoding::OpCode::PREFIX##SUFFIX>, OpBuilder &builder, \
1321 ExpressionParser::ExprParserProxy &exprParser) { \
1322 return exprParser.buildNumericOp<OP_NAME, TYPE, N_ARGS>(builder); \
1323 }
1324
1325// Macro to define binops that only support integer types.
1326#define BUILD_NUMERIC_BINOP_INT(OP_NAME, PREFIX) \
1327 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, I32, int32_t) \
1328 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, I64, int64_t)
1329
1330// Macro to define binops that only support floating point types.
1331#define BUILD_NUMERIC_BINOP_FP(OP_NAME, PREFIX) \
1332 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, F32, float) \
1333 BUILD_NUMERIC_OP(OP_NAME, 2, PREFIX, F64, double)
1334
1335// Macro to define binops that support both floating point and integer types.
1336#define BUILD_NUMERIC_BINOP_INTFP(OP_NAME, PREFIX) \
1337 BUILD_NUMERIC_BINOP_INT(OP_NAME, PREFIX) \
1338 BUILD_NUMERIC_BINOP_FP(OP_NAME, PREFIX)
1339
1340// Macro to implement unary ops that only support integers.
1341#define BUILD_NUMERIC_UNARY_OP_INT(OP_NAME, PREFIX) \
1342 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, I32, int32_t) \
1343 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, I64, int64_t)
1344
1345// Macro to implement unary ops that support integer and floating point types.
1346#define BUILD_NUMERIC_UNARY_OP_FP(OP_NAME, PREFIX) \
1347 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, F32, float) \
1348 BUILD_NUMERIC_OP(OP_NAME, 1, PREFIX, F64, double)
1349
1350BUILD_NUMERIC_BINOP_FP(CopySignOp, copysign)
1351BUILD_NUMERIC_BINOP_FP(DivOp, div)
1352BUILD_NUMERIC_BINOP_FP(GeOp, ge)
1353BUILD_NUMERIC_BINOP_FP(GtOp, gt)
1354BUILD_NUMERIC_BINOP_FP(LeOp, le)
1355BUILD_NUMERIC_BINOP_FP(LtOp, lt)
1356BUILD_NUMERIC_BINOP_FP(MaxOp, max)
1357BUILD_NUMERIC_BINOP_FP(MinOp, min)
1358BUILD_NUMERIC_BINOP_INT(AndOp, and)
1359BUILD_NUMERIC_BINOP_INT(DivSIOp, divS)
1360BUILD_NUMERIC_BINOP_INT(DivUIOp, divU)
1361BUILD_NUMERIC_BINOP_INT(GeSIOp, geS)
1362BUILD_NUMERIC_BINOP_INT(GeUIOp, geU)
1363BUILD_NUMERIC_BINOP_INT(GtSIOp, gtS)
1364BUILD_NUMERIC_BINOP_INT(GtUIOp, gtU)
1365BUILD_NUMERIC_BINOP_INT(LeSIOp, leS)
1366BUILD_NUMERIC_BINOP_INT(LeUIOp, leU)
1367BUILD_NUMERIC_BINOP_INT(LtSIOp, ltS)
1368BUILD_NUMERIC_BINOP_INT(LtUIOp, ltU)
1369BUILD_NUMERIC_BINOP_INT(OrOp, or)
1370BUILD_NUMERIC_BINOP_INT(RemSIOp, remS)
1371BUILD_NUMERIC_BINOP_INT(RemUIOp, remU)
1372BUILD_NUMERIC_BINOP_INT(RotlOp, rotl)
1373BUILD_NUMERIC_BINOP_INT(RotrOp, rotr)
1374BUILD_NUMERIC_BINOP_INT(ShLOp, shl)
1375BUILD_NUMERIC_BINOP_INT(ShRSOp, shrS)
1376BUILD_NUMERIC_BINOP_INT(ShRUOp, shrU)
1377BUILD_NUMERIC_BINOP_INT(XOrOp, xor)
1378BUILD_NUMERIC_BINOP_INTFP(AddOp, add)
1379BUILD_NUMERIC_BINOP_INTFP(EqOp, eq)
1380BUILD_NUMERIC_BINOP_INTFP(MulOp, mul)
1381BUILD_NUMERIC_BINOP_INTFP(NeOp, ne)
1382BUILD_NUMERIC_BINOP_INTFP(SubOp, sub)
1383BUILD_NUMERIC_UNARY_OP_FP(AbsOp, abs)
1384BUILD_NUMERIC_UNARY_OP_FP(CeilOp, ceil)
1385BUILD_NUMERIC_UNARY_OP_FP(FloorOp, floor)
1386BUILD_NUMERIC_UNARY_OP_FP(NearestOp, nearest)
1387BUILD_NUMERIC_UNARY_OP_FP(NegOp, neg)
1388BUILD_NUMERIC_UNARY_OP_FP(SqrtOp, sqrt)
1389BUILD_NUMERIC_UNARY_OP_FP(TruncOp, trunc)
1390BUILD_NUMERIC_UNARY_OP_INT(ClzOp, clz)
1391BUILD_NUMERIC_UNARY_OP_INT(CtzOp, ctz)
1392BUILD_NUMERIC_UNARY_OP_INT(EqzOp, eqz)
1393BUILD_NUMERIC_UNARY_OP_INT(PopCntOp, popcnt)
1394
1395// Don't need these anymore so let's undef them.
1396#undef BUILD_NUMERIC_BINOP_FP
1397#undef BUILD_NUMERIC_BINOP_INT
1398#undef BUILD_NUMERIC_BINOP_INTFP
1399#undef BUILD_NUMERIC_UNARY_OP_FP
1400#undef BUILD_NUMERIC_UNARY_OP_INT
1401#undef BUILD_NUMERIC_OP
1402#undef BUILD_NUMERIC_CAST_OP
1403
1404template <typename opType, typename inputType, typename outputType,
1405 typename... extraArgsT>
1406inline parsed_inst_t
1407ExpressionParser::ExprParserProxy::buildConvertOp(OpBuilder &builder,
1408 extraArgsT... extraArgs) {
1409 static_assert(std::is_arithmetic_v<inputType>,
1410 "InputType should be an arithmetic type");
1411 static_assert(std::is_arithmetic_v<outputType>,
1412 "OutputType should be an arithmetic type");
1413 auto intype = buildLiteralType<inputType>(builder);
1414 auto outType = buildLiteralType<outputType>(builder);
1415 auto operand = popOperands(operandTypes: intype);
1416 if (failed(operand))
1417 return failure();
1418 auto op = opType::create(builder, getCurrentOpLoc(), outType,
1419 operand->front(), extraArgs...);
1420 LDBG() << "Built operation: " << op;
1421 return {{op.getResult()}};
1422}
1423
1424parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::demoteF64ToF32>,
1425 OpBuilder &builder,
1426 ExpressionParser::ExprParserProxy &exprParser) {
1427 return exprParser.buildConvertOp<DemoteOp, double, float>(builder);
1428}
1429
1430parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::wrap>,
1431 OpBuilder &builder,
1432 ExpressionParser::ExprParserProxy &exprParser) {
1433 return exprParser.buildConvertOp<WrapOp, int64_t, int32_t>(builder);
1434}
1435
1436#define BUILD_CONVERSION_OP(IN_T, OUT_T, SOURCE_OP, TARGET_OP) \
1437 inline parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::SOURCE_OP>, \
1438 OpBuilder &builder, \
1439 ExpressionParser::ExprParserProxy &exprParser) { \
1440 return exprParser.buildConvertOp<TARGET_OP, IN_T, OUT_T>(builder); \
1441 }
1442
1443#define BUILD_CONVERT_OP_FOR(DEST_T, WIDTH) \
1444 BUILD_CONVERSION_OP(uint32_t, DEST_T, convertUI32F##WIDTH, ConvertUOp) \
1445 BUILD_CONVERSION_OP(int32_t, DEST_T, convertSI32F##WIDTH, ConvertSOp) \
1446 BUILD_CONVERSION_OP(uint64_t, DEST_T, convertUI64F##WIDTH, ConvertUOp) \
1447 BUILD_CONVERSION_OP(int64_t, DEST_T, convertSI64F##WIDTH, ConvertSOp)
1448
1449BUILD_CONVERT_OP_FOR(float, 32)
1450BUILD_CONVERT_OP_FOR(double, 64)
1451
1452#undef BUILD_CONVERT_OP_FOR
1453
1454#define BUILD_TRUNC_OP_FOR(SRC_T, WIDTH) \
1455 BUILD_CONVERSION_OP(SRC_T, int32_t, truncSI32F##WIDTH, TruncSIOp) \
1456 BUILD_CONVERSION_OP(SRC_T, uint32_t, truncUI32F##WIDTH, TruncUIOp) \
1457 BUILD_CONVERSION_OP(SRC_T, int64_t, truncSI64F##WIDTH, TruncSIOp) \
1458 BUILD_CONVERSION_OP(SRC_T, uint64_t, truncUI64F##WIDTH, TruncUIOp)
1459
1460BUILD_TRUNC_OP_FOR(float, 32)
1461BUILD_TRUNC_OP_FOR(double, 64)
1462
1463#undef BUILD_TRUNC_OP_FOR
1464
1465BUILD_CONVERSION_OP(int32_t, int64_t, extendS, ExtendSI32Op)
1466BUILD_CONVERSION_OP(int32_t, int64_t, extendU, ExtendUI32Op)
1467
1468#undef BUILD_CONVERSION_OP
1469
1470parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::saturatedTruncate>,
1471 OpBuilder &builder,
1472 ExpressionParser::ExprParserProxy &parser,
1473 std::uint32_t subOpCode) {
1474 if (subOpCode > 7)
1475 return emitError(loc: parser.getCurrentOpLoc())
1476 << "invalid sub-opcode for trunc_saturate: " << subOpCode;
1477 LDBG() << "Sub subOpcode for operation: " << subOpCode;
1478 bool isDestUnsigned = (subOpCode & 1) != 0;
1479 bool isSrcF64 = (subOpCode & 2) != 0;
1480 bool isDestI64 = (subOpCode & 4) != 0;
1481
1482 Type srcType = isSrcF64 ? builder.getF64Type() : builder.getF32Type();
1483 Type destType = isDestI64 ? builder.getI64Type() : builder.getI32Type();
1484 auto srcOp = parser.popOperands(operandTypes: srcType);
1485 if (failed(Result: srcOp))
1486 return failure();
1487 Operation *op = isDestUnsigned
1488 ? TruncSatUIOp::create(builder, location: parser.getCurrentOpLoc(),
1489 result: destType, input: srcOp->front())
1490 : TruncSatSIOp::create(builder, location: parser.getCurrentOpLoc(),
1491 result: destType, input: srcOp->front());
1492 LDBG() << "Built operation: " << op;
1493 return {{op->getResult(idx: 0)}};
1494}
1495
1496#define BUILD_SLICE_EXTEND_PARSER(IT_WIDTH, EXTRACT_WIDTH) \
1497 parsed_inst_t parse( \
1498 OpCode<WasmBinaryEncoding::OpCode::extendI##IT_WIDTH##EXTRACT_WIDTH##S>, \
1499 OpBuilder &builder, ExpressionParser::ExprParserProxy &exprParser) { \
1500 using inout_t = int##IT_WIDTH##_t; \
1501 auto attr = builder.getUI32IntegerAttr(EXTRACT_WIDTH); \
1502 return exprParser.buildConvertOp<ExtendLowBitsSOp, inout_t, inout_t>( \
1503 builder, attr); \
1504 }
1505
1506BUILD_SLICE_EXTEND_PARSER(32, 8)
1507BUILD_SLICE_EXTEND_PARSER(32, 16)
1508BUILD_SLICE_EXTEND_PARSER(64, 8)
1509BUILD_SLICE_EXTEND_PARSER(64, 16)
1510BUILD_SLICE_EXTEND_PARSER(64, 32)
1511
1512#undef BUILD_SLICE_EXTEND_PARSER
1513
1514parsed_inst_t parse(OpCode<WasmBinaryEncoding::OpCode::promoteF32ToF64>,
1515 OpBuilder &builder,
1516 ExpressionParser::ExprParserProxy &exprParser) {
1517 return exprParser.buildConvertOp<PromoteOp, float, double>(builder);
1518}
1519
1520#define BUILD_REINTERPRET_PARSER(WIDTH, FP_TYPE) \
1521 inline parsed_inst_t parse( \
1522 OpCode<WasmBinaryEncoding::OpCode::reinterpretF##WIDTH##AsI##WIDTH>, \
1523 OpBuilder &builder, ExpressionParser::ExprParserProxy &exprParser) { \
1524 return exprParser.buildConvertOp<ReinterpretOp, FP_TYPE, int##WIDTH##_t>( \
1525 builder); \
1526 } \
1527 \
1528 inline parsed_inst_t parse( \
1529 OpCode<WasmBinaryEncoding::OpCode::reinterpretI##WIDTH##AsF##WIDTH>, \
1530 OpBuilder &builder, ExpressionParser::ExprParserProxy &exprParser) { \
1531 return exprParser.buildConvertOp<ReinterpretOp, int##WIDTH##_t, FP_TYPE>( \
1532 builder); \
1533 }
1534
1535BUILD_REINTERPRET_PARSER(32, float)
1536BUILD_REINTERPRET_PARSER(64, double)
1537
1538#undef BUILD_REINTERPRET_PARSER
1539
1540class InstDispatcher {
1541private:
1542 template <std::byte OpCode, typename = void>
1543 struct HasParserRegistered : std::false_type {};
1544
1545 template <std::byte opCode>
1546 struct HasParserRegistered<
1547 opCode, std::void_t<decltype(parse(
1548 std::declval<OpCode<opCode>>(), std::declval<OpBuilder &>(),
1549 std::declval<ExpressionParser::ExprParserProxy &>()))>>
1550 : std::true_type {};
1551
1552 template <std::byte opCode>
1553 static constexpr bool hasParseredRegistered =
1554 HasParserRegistered<opCode>::value;
1555
1556 template <std::byte OpCode, typename = void>
1557 struct HasParserWthSubOpCodeRegistered : std::false_type {};
1558
1559 template <std::byte opCode>
1560 struct HasParserWthSubOpCodeRegistered<
1561 opCode, std::void_t<decltype(parse(
1562 std::declval<OpCode<opCode>>(), std::declval<OpBuilder &>(),
1563 std::declval<ExpressionParser::ExprParserProxy &>(),
1564 std::declval<std::uint32_t>()))>> : std::true_type {
1565 static_assert(!hasParseredRegistered<opCode>,
1566 "plain parser and parser with sub-opcode can't be registered "
1567 "for the same opcode");
1568 };
1569
1570 template <std::byte opCode>
1571 static constexpr bool hasParseredWithSubOpCodeRegistered =
1572 HasParserWthSubOpCodeRegistered<opCode>::value;
1573
1574 using dispatch_t = parsed_inst_t (*)(OpBuilder &,
1575 ExpressionParser::ExprParserProxy &);
1576
1577 static inline parsed_inst_t
1578 unreachableHandler(OpBuilder &,
1579 ExpressionParser::ExprParserProxy &expressionParser) {
1580 llvm_unreachable("Failure in opcode parser dispatch logic.");
1581 return mlir::failure();
1582 }
1583
1584 template <std::byte opCode>
1585 static parsed_inst_t
1586 forwardToParser(OpBuilder &builder,
1587 ExpressionParser::ExprParserProxy &exprParser) {
1588 return parse(OpCode<opCode>{}, builder, exprParser);
1589 }
1590
1591 template <std::byte opCode>
1592 static parsed_inst_t
1593 forwardToParserWithSubOpCode(OpBuilder &builder,
1594 ExpressionParser::ExprParserProxy &exprParser) {
1595 auto loc = exprParser.getCurrentOpLoc();
1596 auto subOpCode = exprParser.parser().parseLiteral<std::uint32_t>();
1597 if (failed(Result: subOpCode))
1598 return emitError(loc) << "expecting sub opcode for opcode "
1599 << static_cast<uint8_t>(opCode);
1600 return parse(OpCode<opCode>{}, builder, exprParser, subOpCode.value());
1601 }
1602
1603 template <std::byte opCode>
1604 static constexpr dispatch_t getHandlerForOpCode() {
1605 if constexpr (hasParseredRegistered<opCode>)
1606 return forwardToParser<opCode>;
1607 else if constexpr (hasParseredWithSubOpCodeRegistered<opCode>)
1608 return forwardToParserWithSubOpCode<opCode>;
1609 else
1610 return unreachableHandler;
1611 }
1612
1613private:
1614 static inline parsed_inst_t
1615 invalidOpcodeDiag(OpBuilder &,
1616 ExpressionParser::ExprParserProxy &expressionParser,
1617 std::byte opCode) {
1618 return emitError(loc: expressionParser.getCurrentOpLoc(),
1619 message: "unknown instruction opcode: ")
1620 << static_cast<int>(opCode);
1621 }
1622
1623 template <std::byte... opCodes>
1624 static inline parsed_inst_t
1625 dispatchImpl(std::byte opCode, OpBuilder &builder,
1626 ExpressionParser::ExprParserProxy &exprParser,
1627 ByteSequence<opCodes...>) {
1628 static constexpr std::array<bool, 256> opcodeValidityMap{
1629 (hasParseredRegistered<opCodes> ||
1630 hasParseredWithSubOpCodeRegistered<opCodes>)...};
1631 static constexpr std::array<dispatch_t, 256> dispatchTable{
1632 getHandlerForOpCode<opCodes>()...};
1633 if (opcodeValidityMap[static_cast<size_t>(opCode)]) {
1634 return dispatchTable[static_cast<size_t>(opCode)](builder, exprParser);
1635 }
1636 return invalidOpcodeDiag(builder, expressionParser&: exprParser, opCode);
1637 }
1638
1639public:
1640 ///
1641 /// @brief dispatch control flow to the sub parser registered for opCode in
1642 /// `ParserRegistry`
1643 ///
1644 /// @param opCode opCode of the instruction to be Parsed
1645 /// @param builder builder that will be passed to the parser
1646 /// @param exprParser the generic parser passed to the sub parser
1647 ///
1648 /// @return the result of the parser or an error if there is no parser
1649 /// registered for the opcode (emits a diagnostic)
1650 static parsed_inst_t dispatch(std::byte opCode, OpBuilder &builder,
1651 ExpressionParser::ExprParserProxy &exprParser) {
1652 return dispatchImpl(opCode, builder, exprParser, all8bitsBytes);
1653 }
1654};
1655
1656inline parsed_inst_t
1657ExpressionParser::dispatchToInstParser(std::byte opCode, OpBuilder &builder) {
1658 ExpressionParser::ExprParserProxy exprParser{*this};
1659 return InstDispatcher::dispatch(opCode, builder, exprParser);
1660}
1661class WasmBinaryParser {
1662private:
1663 struct SectionRegistry {
1664 using section_location_t = StringRef;
1665
1666 std::array<SmallVector<section_location_t>, highestWasmSectionID + 1>
1667 registry;
1668
1669 template <WasmSectionType SecType>
1670 std::conditional_t<sectionShouldBeUnique(secType: SecType),
1671 std::optional<section_location_t>,
1672 ArrayRef<section_location_t>>
1673 getContentForSection() const {
1674 constexpr auto idx = static_cast<size_t>(SecType);
1675 if constexpr (sectionShouldBeUnique(secType: SecType)) {
1676 return registry[idx].empty() ? std::nullopt
1677 : std::make_optional(t: registry[idx][0]);
1678 } else {
1679 return registry[idx];
1680 }
1681 }
1682
1683 bool hasSection(WasmSectionType secType) const {
1684 return !registry[static_cast<size_t>(secType)].empty();
1685 }
1686
1687 ///
1688 /// @returns success if registration valid, failure in case registration
1689 /// can't be done (if another section of same type already exist and this
1690 /// section type should only be present once)
1691 ///
1692 LogicalResult registerSection(WasmSectionType secType,
1693 section_location_t location, Location loc) {
1694 if (sectionShouldBeUnique(secType) && hasSection(secType))
1695 return emitError(loc,
1696 message: "trying to add a second instance of unique section");
1697
1698 registry[static_cast<size_t>(secType)].push_back(Elt: location);
1699 emitRemark(loc, message: "Adding section with section ID ")
1700 << static_cast<uint8_t>(secType);
1701 return success();
1702 }
1703
1704 LogicalResult populateFromBody(ParserHead ph) {
1705 while (!ph.end()) {
1706 FileLineColLoc sectionLoc = ph.getLocation();
1707 FailureOr<WasmSectionType> secType = ph.parseWasmSectionType();
1708 if (failed(Result: secType))
1709 return failure();
1710
1711 FailureOr<uint32_t> secSizeParsed = ph.parseLiteral<uint32_t>();
1712 if (failed(Result: secSizeParsed))
1713 return failure();
1714
1715 uint32_t secSize = *secSizeParsed;
1716 FailureOr<StringRef> sectionContent = ph.consumeNBytes(nBytes: secSize);
1717 if (failed(Result: sectionContent))
1718 return failure();
1719
1720 LogicalResult registration =
1721 registerSection(secType: *secType, location: *sectionContent, loc: sectionLoc);
1722
1723 if (failed(Result: registration))
1724 return failure();
1725 }
1726 return success();
1727 }
1728 };
1729
1730 auto getLocation(int offset = 0) const {
1731 return FileLineColLoc::get(filename: srcName, line: 0, column: offset);
1732 }
1733
1734 template <WasmSectionType>
1735 LogicalResult parseSectionItem(ParserHead &, size_t);
1736
1737 template <WasmSectionType section>
1738 LogicalResult parseSection() {
1739 auto secName = std::string{wasmSectionName<section>};
1740 auto sectionNameAttr =
1741 StringAttr::get(context: ctx, bytes: srcName.strref() + ":" + secName + "-SECTION");
1742 unsigned offset = 0;
1743 auto getLocation = [sectionNameAttr, &offset]() {
1744 return FileLineColLoc::get(filename: sectionNameAttr, line: 0, column: offset);
1745 };
1746 auto secContent = registry.getContentForSection<section>();
1747 if (!secContent) {
1748 LDBG() << secName << " section is not present in file.";
1749 return success();
1750 }
1751
1752 auto secSrc = secContent.value();
1753 ParserHead ph{secSrc, sectionNameAttr};
1754 FailureOr<uint32_t> nElemsParsed = ph.parseVectorSize();
1755 if (failed(Result: nElemsParsed))
1756 return failure();
1757 uint32_t nElems = *nElemsParsed;
1758 LDBG() << "starting to parse " << nElems << " items for section "
1759 << secName;
1760 for (size_t i = 0; i < nElems; ++i) {
1761 if (failed(parseSectionItem<section>(ph, i)))
1762 return failure();
1763 }
1764
1765 if (!ph.end())
1766 return emitError(getLocation(), "unparsed garbage at end of section ")
1767 << secName;
1768 return success();
1769 }
1770
1771 /// Handles the registration of a function import
1772 LogicalResult visitImport(Location loc, StringRef moduleName,
1773 StringRef importName, TypeIdxRecord tid) {
1774 using llvm::Twine;
1775 if (tid.id >= symbols.moduleFuncTypes.size())
1776 return emitError(loc, message: "invalid type id: ")
1777 << tid.id << ". Only " << symbols.moduleFuncTypes.size()
1778 << " type registrations";
1779 FunctionType type = symbols.moduleFuncTypes[tid.id];
1780 std::string symbol = symbols.getNewFuncSymbolName();
1781 auto funcOp = FuncImportOp::create(builder, location: loc, symbol, moduleName,
1782 importName, type);
1783 symbols.funcSymbols.push_back(Elt: {{.symbol: FlatSymbolRefAttr::get(symbol: funcOp)}, .functionType: type});
1784 return funcOp.verify();
1785 }
1786
1787 /// Handles the registration of a memory import
1788 LogicalResult visitImport(Location loc, StringRef moduleName,
1789 StringRef importName, LimitType limitType) {
1790 std::string symbol = symbols.getNewMemorySymbolName();
1791 auto memOp = MemImportOp::create(builder, location: loc, sym_name: symbol, moduleName,
1792 importName, limits: limitType);
1793 symbols.memSymbols.push_back(Elt: {.symbol: FlatSymbolRefAttr::get(symbol: memOp)});
1794 return memOp.verify();
1795 }
1796
1797 /// Handles the registration of a table import
1798 LogicalResult visitImport(Location loc, StringRef moduleName,
1799 StringRef importName, TableType tableType) {
1800 std::string symbol = symbols.getNewTableSymbolName();
1801 auto tableOp = TableImportOp::create(builder, location: loc, sym_name: symbol, moduleName,
1802 importName, type: tableType);
1803 symbols.tableSymbols.push_back(Elt: {.symbol: FlatSymbolRefAttr::get(symbol: tableOp)});
1804 return tableOp.verify();
1805 }
1806
1807 /// Handles the registration of a global variable import
1808 LogicalResult visitImport(Location loc, StringRef moduleName,
1809 StringRef importName, GlobalTypeRecord globalType) {
1810 std::string symbol = symbols.getNewGlobalSymbolName();
1811 auto giOp =
1812 GlobalImportOp::create(builder, location: loc, sym_name: symbol, moduleName, importName,
1813 type: globalType.type, isMutable: globalType.isMutable);
1814 symbols.globalSymbols.push_back(
1815 Elt: {{.symbol: FlatSymbolRefAttr::get(symbol: giOp)}, .globalType: giOp.getType()});
1816 return giOp.verify();
1817 }
1818
1819 // Detect occurence of errors
1820 LogicalResult peekDiag(Diagnostic &diag) {
1821 if (diag.getSeverity() == DiagnosticSeverity::Error)
1822 isValid = false;
1823 return failure();
1824 }
1825
1826public:
1827 WasmBinaryParser(llvm::SourceMgr &sourceMgr, MLIRContext *ctx)
1828 : builder{ctx}, ctx{ctx} {
1829 ctx->getDiagEngine().registerHandler(
1830 handler: [this](Diagnostic &diag) { return peekDiag(diag); });
1831 ctx->loadAllAvailableDialects();
1832 if (sourceMgr.getNumBuffers() != 1) {
1833 emitError(loc: UnknownLoc::get(context: ctx), message: "one source file should be provided");
1834 return;
1835 }
1836 uint32_t sourceBufId = sourceMgr.getMainFileID();
1837 StringRef source = sourceMgr.getMemoryBuffer(i: sourceBufId)->getBuffer();
1838 srcName = StringAttr::get(
1839 context: ctx, bytes: sourceMgr.getMemoryBuffer(i: sourceBufId)->getBufferIdentifier());
1840
1841 auto parser = ParserHead{source, srcName};
1842 auto const wasmHeader = StringRef{"\0asm", 4};
1843 FileLineColLoc magicLoc = parser.getLocation();
1844 FailureOr<StringRef> magic = parser.consumeNBytes(nBytes: wasmHeader.size());
1845 if (failed(Result: magic) || magic->compare(RHS: wasmHeader)) {
1846 emitError(loc: magicLoc, message: "source file does not contain valid Wasm header");
1847 return;
1848 }
1849 auto const expectedVersionString = StringRef{"\1\0\0\0", 4};
1850 FileLineColLoc versionLoc = parser.getLocation();
1851 FailureOr<StringRef> version =
1852 parser.consumeNBytes(nBytes: expectedVersionString.size());
1853 if (failed(Result: version))
1854 return;
1855 if (version->compare(RHS: expectedVersionString)) {
1856 emitError(loc: versionLoc,
1857 message: "unsupported Wasm version. only version 1 is supported");
1858 return;
1859 }
1860 LogicalResult fillRegistry = registry.populateFromBody(ph: parser.copy());
1861 if (failed(Result: fillRegistry))
1862 return;
1863
1864 mOp = ModuleOp::create(builder, location: getLocation());
1865 builder.setInsertionPointToStart(&mOp.getBodyRegion().front());
1866 LogicalResult parsingTypes = parseSection<WasmSectionType::TYPE>();
1867 if (failed(Result: parsingTypes))
1868 return;
1869
1870 LogicalResult parsingImports = parseSection<WasmSectionType::IMPORT>();
1871 if (failed(Result: parsingImports))
1872 return;
1873
1874 firstInternalFuncID = symbols.funcSymbols.size();
1875
1876 LogicalResult parsingFunctions = parseSection<WasmSectionType::FUNCTION>();
1877 if (failed(Result: parsingFunctions))
1878 return;
1879
1880 LogicalResult parsingTables = parseSection<WasmSectionType::TABLE>();
1881 if (failed(Result: parsingTables))
1882 return;
1883
1884 LogicalResult parsingMems = parseSection<WasmSectionType::MEMORY>();
1885 if (failed(Result: parsingMems))
1886 return;
1887
1888 LogicalResult parsingGlobals = parseSection<WasmSectionType::GLOBAL>();
1889 if (failed(Result: parsingGlobals))
1890 return;
1891
1892 LogicalResult parsingCode = parseSection<WasmSectionType::CODE>();
1893 if (failed(Result: parsingCode))
1894 return;
1895
1896 LogicalResult parsingExports = parseSection<WasmSectionType::EXPORT>();
1897 if (failed(Result: parsingExports))
1898 return;
1899
1900 // Copy over sizes of containers into statistics.
1901 LDBG() << "WASM Imports:"
1902 << "\n"
1903 << " - Num functions: " << symbols.funcSymbols.size() << "\n"
1904 << " - Num globals: " << symbols.globalSymbols.size() << "\n"
1905 << " - Num memories: " << symbols.memSymbols.size() << "\n"
1906 << " - Num tables: " << symbols.tableSymbols.size();
1907 }
1908
1909 ModuleOp getModule() {
1910 if (isValid)
1911 return mOp;
1912 if (mOp)
1913 mOp.erase();
1914 return ModuleOp{};
1915 }
1916
1917private:
1918 mlir::StringAttr srcName;
1919 OpBuilder builder;
1920 WasmModuleSymbolTables symbols;
1921 MLIRContext *ctx;
1922 ModuleOp mOp;
1923 SectionRegistry registry;
1924 size_t firstInternalFuncID{0};
1925 bool isValid{true};
1926};
1927
1928template <>
1929LogicalResult
1930WasmBinaryParser::parseSectionItem<WasmSectionType::IMPORT>(ParserHead &ph,
1931 size_t) {
1932 FileLineColLoc importLoc = ph.getLocation();
1933 auto moduleName = ph.parseName();
1934 if (failed(Result: moduleName))
1935 return failure();
1936
1937 auto importName = ph.parseName();
1938 if (failed(Result: importName))
1939 return failure();
1940
1941 FailureOr<ImportDesc> import = ph.parseImportDesc(ctx);
1942 if (failed(Result: import))
1943 return failure();
1944
1945 return std::visit(
1946 visitor: [this, importLoc, &moduleName, &importName](auto import) {
1947 return visitImport(importLoc, *moduleName, *importName, import);
1948 },
1949 variants&: *import);
1950}
1951
1952template <>
1953LogicalResult
1954WasmBinaryParser::parseSectionItem<WasmSectionType::EXPORT>(ParserHead &ph,
1955 size_t) {
1956 FileLineColLoc exportLoc = ph.getLocation();
1957
1958 auto exportName = ph.parseName();
1959 if (failed(Result: exportName))
1960 return failure();
1961
1962 FailureOr<std::byte> opcode = ph.consumeByte();
1963 if (failed(Result: opcode))
1964 return failure();
1965
1966 FailureOr<uint32_t> idx = ph.parseLiteral<uint32_t>();
1967 if (failed(Result: idx))
1968 return failure();
1969
1970 using SymbolRefDesc = std::variant<SmallVector<SymbolRefContainer>,
1971 SmallVector<GlobalSymbolRefContainer>,
1972 SmallVector<FunctionSymbolRefContainer>>;
1973
1974 SymbolRefDesc currentSymbolList;
1975 std::string symbolType = "";
1976 switch (*opcode) {
1977 case WasmBinaryEncoding::Export::function:
1978 symbolType = "function";
1979 currentSymbolList = symbols.funcSymbols;
1980 break;
1981 case WasmBinaryEncoding::Export::table:
1982 symbolType = "table";
1983 currentSymbolList = symbols.tableSymbols;
1984 break;
1985 case WasmBinaryEncoding::Export::memory:
1986 symbolType = "memory";
1987 currentSymbolList = symbols.memSymbols;
1988 break;
1989 case WasmBinaryEncoding::Export::global:
1990 symbolType = "global";
1991 currentSymbolList = symbols.globalSymbols;
1992 break;
1993 default:
1994 return emitError(loc: exportLoc, message: "invalid value for export type: ")
1995 << std::to_integer<unsigned>(b: *opcode);
1996 }
1997
1998 auto currentSymbol = std::visit(
1999 visitor: [&](const auto &list) -> FailureOr<FlatSymbolRefAttr> {
2000 if (*idx > list.size()) {
2001 emitError(
2002 loc: exportLoc,
2003 message: llvm::formatv(
2004 Fmt: "trying to export {0} {1} which is undefined in this scope",
2005 Vals&: symbolType, Vals&: *idx));
2006 return failure();
2007 }
2008 return list[*idx].symbol;
2009 },
2010 variants&: currentSymbolList);
2011
2012 if (failed(Result: currentSymbol))
2013 return failure();
2014
2015 Operation *op = SymbolTable::lookupSymbolIn(op: mOp, symbol: *currentSymbol);
2016 op->setAttr(name: "exported", value: UnitAttr::get(context: op->getContext()));
2017 StringAttr symName = SymbolTable::getSymbolName(symbol: op);
2018 return SymbolTable{mOp}.rename(from: symName, to: *exportName);
2019}
2020
2021template <>
2022LogicalResult
2023WasmBinaryParser::parseSectionItem<WasmSectionType::TABLE>(ParserHead &ph,
2024 size_t) {
2025 FileLineColLoc opLocation = ph.getLocation();
2026 FailureOr<TableType> tableType = ph.parseTableType(ctx);
2027 if (failed(Result: tableType))
2028 return failure();
2029 LDBG() << " Parsed table description: " << *tableType;
2030 StringAttr symbol = builder.getStringAttr(bytes: symbols.getNewTableSymbolName());
2031 auto tableOp =
2032 TableOp::create(builder, location: opLocation, sym_name: symbol.strref(), type: *tableType);
2033 symbols.tableSymbols.push_back(Elt: {.symbol: SymbolRefAttr::get(symbol: tableOp)});
2034 return success();
2035}
2036
2037template <>
2038LogicalResult
2039WasmBinaryParser::parseSectionItem<WasmSectionType::FUNCTION>(ParserHead &ph,
2040 size_t) {
2041 FileLineColLoc opLoc = ph.getLocation();
2042 auto typeIdxParsed = ph.parseLiteral<uint32_t>();
2043 if (failed(Result: typeIdxParsed))
2044 return failure();
2045 uint32_t typeIdx = *typeIdxParsed;
2046 if (typeIdx >= symbols.moduleFuncTypes.size())
2047 return emitError(loc: getLocation(), message: "invalid type index: ") << typeIdx;
2048 std::string symbol = symbols.getNewFuncSymbolName();
2049 auto funcOp =
2050 FuncOp::create(builder, location: opLoc, symbol, funcType: symbols.moduleFuncTypes[typeIdx]);
2051 Block *block = funcOp.addEntryBlock();
2052 OpBuilder::InsertionGuard guard{builder};
2053 builder.setInsertionPointToEnd(block);
2054 ReturnOp::create(builder, location: opLoc);
2055 symbols.funcSymbols.push_back(
2056 Elt: {{.symbol: FlatSymbolRefAttr::get(value: funcOp.getSymNameAttr())},
2057 .functionType: symbols.moduleFuncTypes[typeIdx]});
2058 return funcOp.verify();
2059}
2060
2061template <>
2062LogicalResult
2063WasmBinaryParser::parseSectionItem<WasmSectionType::TYPE>(ParserHead &ph,
2064 size_t) {
2065 FailureOr<FunctionType> funcType = ph.parseFunctionType(ctx);
2066 if (failed(Result: funcType))
2067 return failure();
2068 LDBG() << "Parsed function type " << *funcType;
2069 symbols.moduleFuncTypes.push_back(Elt: *funcType);
2070 return success();
2071}
2072
2073template <>
2074LogicalResult
2075WasmBinaryParser::parseSectionItem<WasmSectionType::MEMORY>(ParserHead &ph,
2076 size_t) {
2077 FileLineColLoc opLocation = ph.getLocation();
2078 FailureOr<LimitType> memory = ph.parseLimit(ctx);
2079 if (failed(Result: memory))
2080 return failure();
2081
2082 LDBG() << " Registering memory " << *memory;
2083 std::string symbol = symbols.getNewMemorySymbolName();
2084 auto memOp = MemOp::create(builder, location: opLocation, sym_name: symbol, limits: *memory);
2085 symbols.memSymbols.push_back(Elt: {.symbol: SymbolRefAttr::get(symbol: memOp)});
2086 return success();
2087}
2088
2089template <>
2090LogicalResult
2091WasmBinaryParser::parseSectionItem<WasmSectionType::GLOBAL>(ParserHead &ph,
2092 size_t) {
2093 FileLineColLoc globalLocation = ph.getLocation();
2094 auto globalTypeParsed = ph.parseGlobalType(ctx);
2095 if (failed(Result: globalTypeParsed))
2096 return failure();
2097
2098 GlobalTypeRecord globalType = *globalTypeParsed;
2099 auto symbol = builder.getStringAttr(bytes: symbols.getNewGlobalSymbolName());
2100 auto globalOp = wasmssa::GlobalOp::create(
2101 builder, location: globalLocation, sym_name: symbol, type: globalType.type, isMutable: globalType.isMutable);
2102 symbols.globalSymbols.push_back(
2103 Elt: {{.symbol: FlatSymbolRefAttr::get(symbol: globalOp)}, .globalType: globalOp.getType()});
2104 OpBuilder::InsertionGuard guard{builder};
2105 Block *block = builder.createBlock(parent: &globalOp.getInitializer());
2106 builder.setInsertionPointToStart(block);
2107 parsed_inst_t expr = ph.parseExpression(builder, symbols);
2108 if (failed(Result: expr))
2109 return failure();
2110 if (block->empty())
2111 return emitError(loc: globalLocation, message: "global with empty initializer");
2112 if (expr->size() != 1 && (*expr)[0].getType() != globalType.type)
2113 return emitError(
2114 loc: globalLocation,
2115 message: "initializer result type does not match global declaration type");
2116 ReturnOp::create(builder, location: globalLocation, operands: *expr);
2117 return success();
2118}
2119
2120template <>
2121LogicalResult WasmBinaryParser::parseSectionItem<WasmSectionType::CODE>(
2122 ParserHead &ph, size_t innerFunctionId) {
2123 unsigned long funcId = innerFunctionId + firstInternalFuncID;
2124 FunctionSymbolRefContainer symRef = symbols.funcSymbols[funcId];
2125 auto funcOp =
2126 dyn_cast<FuncOp>(Val: SymbolTable::lookupSymbolIn(op: mOp, symbol: symRef.symbol));
2127 assert(funcOp);
2128 if (failed(Result: ph.parseCodeFor(func: funcOp, symbols)))
2129 return failure();
2130 return success();
2131}
2132} // namespace
2133
2134namespace mlir::wasm {
2135OwningOpRef<ModuleOp> importWebAssemblyToModule(llvm::SourceMgr &source,
2136 MLIRContext *context) {
2137 WasmBinaryParser wBN{source, context};
2138 ModuleOp mOp = wBN.getModule();
2139 if (mOp)
2140 return {mOp};
2141
2142 return {nullptr};
2143}
2144} // namespace mlir::wasm
2145