Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 1 | //==- WebAssemblyAsmParser.cpp - Assembler for WebAssembly -*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// This file is part of the WebAssembly Assembler. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 12 | /// |
| 13 | /// It contains code to translate a parsed .s file into MCInsts. |
| 14 | /// |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 18 | #include "MCTargetDesc/WebAssemblyTargetStreamer.h" |
| 19 | #include "WebAssembly.h" |
| 20 | #include "llvm/MC/MCContext.h" |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCInst.h" |
| 22 | #include "llvm/MC/MCInstrInfo.h" |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
| 24 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
| 25 | #include "llvm/MC/MCStreamer.h" |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSubtargetInfo.h" |
| 27 | #include "llvm/MC/MCSymbol.h" |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSymbolWasm.h" |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Endian.h" |
| 30 | #include "llvm/Support/TargetRegistry.h" |
| 31 | |
| 32 | using namespace llvm; |
| 33 | |
| 34 | #define DEBUG_TYPE "wasm-asm-parser" |
| 35 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 38 | /// WebAssemblyOperand - Instances of this class represent the operands in a |
| 39 | /// parsed WASM machine instruction. |
| 40 | struct WebAssemblyOperand : public MCParsedAsmOperand { |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 41 | enum KindTy { Token, Integer, Float, Symbol } Kind; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 42 | |
| 43 | SMLoc StartLoc, EndLoc; |
| 44 | |
| 45 | struct TokOp { |
| 46 | StringRef Tok; |
| 47 | }; |
| 48 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 49 | struct IntOp { |
| 50 | int64_t Val; |
| 51 | }; |
| 52 | |
| 53 | struct FltOp { |
| 54 | double Val; |
| 55 | }; |
| 56 | |
| 57 | struct SymOp { |
| 58 | const MCExpr *Exp; |
| 59 | }; |
| 60 | |
| 61 | union { |
| 62 | struct TokOp Tok; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 63 | struct IntOp Int; |
| 64 | struct FltOp Flt; |
| 65 | struct SymOp Sym; |
| 66 | }; |
| 67 | |
| 68 | WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, TokOp T) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 69 | : Kind(K), StartLoc(Start), EndLoc(End), Tok(T) {} |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 70 | WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, IntOp I) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 71 | : Kind(K), StartLoc(Start), EndLoc(End), Int(I) {} |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 72 | WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, FltOp F) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 73 | : Kind(K), StartLoc(Start), EndLoc(End), Flt(F) {} |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 74 | WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, SymOp S) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 75 | : Kind(K), StartLoc(Start), EndLoc(End), Sym(S) {} |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 76 | |
| 77 | bool isToken() const override { return Kind == Token; } |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 78 | bool isImm() const override { |
| 79 | return Kind == Integer || Kind == Float || Kind == Symbol; |
| 80 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 81 | bool isMem() const override { return false; } |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 82 | bool isReg() const override { return false; } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 83 | |
| 84 | unsigned getReg() const override { |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 85 | llvm_unreachable("Assembly inspects a register operand"); |
| 86 | return 0; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | StringRef getToken() const { |
| 90 | assert(isToken()); |
| 91 | return Tok.Tok; |
| 92 | } |
| 93 | |
| 94 | SMLoc getStartLoc() const override { return StartLoc; } |
| 95 | SMLoc getEndLoc() const override { return EndLoc; } |
| 96 | |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 97 | void addRegOperands(MCInst &, unsigned) const { |
| 98 | // Required by the assembly matcher. |
| 99 | llvm_unreachable("Assembly matcher creates register operands"); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void addImmOperands(MCInst &Inst, unsigned N) const { |
| 103 | assert(N == 1 && "Invalid number of operands!"); |
| 104 | if (Kind == Integer) |
| 105 | Inst.addOperand(MCOperand::createImm(Int.Val)); |
| 106 | else if (Kind == Float) |
| 107 | Inst.addOperand(MCOperand::createFPImm(Flt.Val)); |
| 108 | else if (Kind == Symbol) |
| 109 | Inst.addOperand(MCOperand::createExpr(Sym.Exp)); |
| 110 | else |
| 111 | llvm_unreachable("Should be immediate or symbol!"); |
| 112 | } |
| 113 | |
| 114 | void print(raw_ostream &OS) const override { |
| 115 | switch (Kind) { |
| 116 | case Token: |
| 117 | OS << "Tok:" << Tok.Tok; |
| 118 | break; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 119 | case Integer: |
| 120 | OS << "Int:" << Int.Val; |
| 121 | break; |
| 122 | case Float: |
| 123 | OS << "Flt:" << Flt.Val; |
| 124 | break; |
| 125 | case Symbol: |
| 126 | OS << "Sym:" << Sym.Exp; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | class WebAssemblyAsmParser final : public MCTargetAsmParser { |
| 133 | MCAsmParser &Parser; |
| 134 | MCAsmLexer &Lexer; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 135 | |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 136 | // Much like WebAssemblyAsmPrinter in the backend, we have to own these. |
| 137 | std::vector<std::unique_ptr<wasm::WasmSignature>> Signatures; |
| 138 | |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame^] | 139 | // Order of labels, directives and instructions in a .s file have no |
| 140 | // syntactical enforcement. This class is a callback from the actual parser, |
| 141 | // and yet we have to be feeding data to the streamer in a very particular |
| 142 | // order to ensure a correct binary encoding that matches the regular backend |
| 143 | // (the streamer does not enforce this). This "state machine" enum helps |
| 144 | // guarantee that correct order. |
| 145 | enum ParserState { |
| 146 | FileStart, |
| 147 | Label, |
| 148 | FunctionStart, |
| 149 | FunctionLocals, |
| 150 | Instructions, |
| 151 | } CurrentState = FileStart; |
| 152 | |
| 153 | // We track this to see if a .functype following a label is the same, |
| 154 | // as this is how we recognize the start of a function. |
| 155 | MCSymbol *LastLabel = nullptr; |
| 156 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 157 | public: |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 158 | WebAssemblyAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, |
| 159 | const MCInstrInfo &MII, const MCTargetOptions &Options) |
| 160 | : MCTargetAsmParser(Options, STI, MII), Parser(Parser), |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 161 | Lexer(Parser.getLexer()) { |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 162 | setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 165 | void addSignature(std::unique_ptr<wasm::WasmSignature> &&Sig) { |
| 166 | Signatures.push_back(std::move(Sig)); |
| 167 | } |
| 168 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 169 | #define GET_ASSEMBLER_HEADER |
| 170 | #include "WebAssemblyGenAsmMatcher.inc" |
| 171 | |
| 172 | // TODO: This is required to be implemented, but appears unused. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 173 | bool ParseRegister(unsigned & /*RegNo*/, SMLoc & /*StartLoc*/, |
| 174 | SMLoc & /*EndLoc*/) override { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 175 | llvm_unreachable("ParseRegister is not implemented."); |
| 176 | } |
| 177 | |
| 178 | bool Error(const StringRef &msg, const AsmToken &tok) { |
| 179 | return Parser.Error(tok.getLoc(), msg + tok.getString()); |
| 180 | } |
| 181 | |
| 182 | bool IsNext(AsmToken::TokenKind Kind) { |
| 183 | auto ok = Lexer.is(Kind); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 184 | if (ok) |
| 185 | Parser.Lex(); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 186 | return ok; |
| 187 | } |
| 188 | |
| 189 | bool Expect(AsmToken::TokenKind Kind, const char *KindName) { |
| 190 | if (!IsNext(Kind)) |
| 191 | return Error(std::string("Expected ") + KindName + ", instead got: ", |
| 192 | Lexer.getTok()); |
| 193 | return false; |
| 194 | } |
| 195 | |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 196 | StringRef ExpectIdent() { |
| 197 | if (!Lexer.is(AsmToken::Identifier)) { |
| 198 | Error("Expected identifier, got: ", Lexer.getTok()); |
| 199 | return StringRef(); |
| 200 | } |
| 201 | auto Name = Lexer.getTok().getString(); |
| 202 | Parser.Lex(); |
| 203 | return Name; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 206 | Optional<wasm::ValType> ParseType(const StringRef &Type) { |
| 207 | // FIXME: can't use StringSwitch because wasm::ValType doesn't have a |
| 208 | // "invalid" value. |
| 209 | if (Type == "i32") return wasm::ValType::I32; |
| 210 | if (Type == "i64") return wasm::ValType::I64; |
| 211 | if (Type == "f32") return wasm::ValType::F32; |
| 212 | if (Type == "f64") return wasm::ValType::F64; |
| 213 | if (Type == "v128" || Type == "i8x16" || Type == "i16x8" || |
| 214 | Type == "i32x4" || Type == "i64x2" || Type == "f32x4" || |
| 215 | Type == "f64x2") return wasm::ValType::V128; |
| 216 | return Optional<wasm::ValType>(); |
| 217 | } |
| 218 | |
| 219 | bool ParseRegTypeList(SmallVectorImpl<wasm::ValType> &Types) { |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 220 | while (Lexer.is(AsmToken::Identifier)) { |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 221 | auto Type = ParseType(Lexer.getTok().getString()); |
| 222 | if (!Type) |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 223 | return true; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 224 | Types.push_back(Type.getValue()); |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 225 | Parser.Lex(); |
| 226 | if (!IsNext(AsmToken::Comma)) |
| 227 | break; |
| 228 | } |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 229 | return false; |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 232 | void ParseSingleInteger(bool IsNegative, OperandVector &Operands) { |
| 233 | auto &Int = Lexer.getTok(); |
| 234 | int64_t Val = Int.getIntVal(); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 235 | if (IsNegative) |
| 236 | Val = -Val; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 237 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 238 | WebAssemblyOperand::Integer, Int.getLoc(), Int.getEndLoc(), |
| 239 | WebAssemblyOperand::IntOp{Val})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 240 | Parser.Lex(); |
| 241 | } |
| 242 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 243 | bool ParseOperandStartingWithInteger(bool IsNegative, OperandVector &Operands, |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 244 | StringRef InstName) { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 245 | ParseSingleInteger(IsNegative, Operands); |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 246 | // FIXME: there is probably a cleaner way to do this. |
| 247 | auto IsLoadStore = InstName.startswith("load") || |
| 248 | InstName.startswith("store") || |
| 249 | InstName.startswith("atomic_load") || |
| 250 | InstName.startswith("atomic_store"); |
| 251 | if (IsLoadStore) { |
| 252 | // Parse load/store operands of the form: offset align |
| 253 | auto &Offset = Lexer.getTok(); |
| 254 | if (Offset.is(AsmToken::Integer)) { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 255 | ParseSingleInteger(false, Operands); |
| 256 | } else { |
| 257 | // Alignment not specified. |
| 258 | // FIXME: correctly derive a default from the instruction. |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 259 | // We can't just call WebAssembly::GetDefaultP2Align since we don't have |
| 260 | // an opcode until after the assembly matcher. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 261 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 262 | WebAssemblyOperand::Integer, Offset.getLoc(), Offset.getEndLoc(), |
| 263 | WebAssemblyOperand::IntOp{0})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | return false; |
| 267 | } |
| 268 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 269 | bool ParseInstruction(ParseInstructionInfo & /*Info*/, StringRef Name, |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 270 | SMLoc NameLoc, OperandVector &Operands) override { |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 271 | // Note: Name does NOT point into the sourcecode, but to a local, so |
| 272 | // use NameLoc instead. |
| 273 | Name = StringRef(NameLoc.getPointer(), Name.size()); |
| 274 | // WebAssembly has instructions with / in them, which AsmLexer parses |
| 275 | // as seperate tokens, so if we find such tokens immediately adjacent (no |
| 276 | // whitespace), expand the name to include them: |
| 277 | for (;;) { |
| 278 | auto &Sep = Lexer.getTok(); |
| 279 | if (Sep.getLoc().getPointer() != Name.end() || |
| 280 | Sep.getKind() != AsmToken::Slash) break; |
| 281 | // Extend name with / |
| 282 | Name = StringRef(Name.begin(), Name.size() + Sep.getString().size()); |
| 283 | Parser.Lex(); |
| 284 | // We must now find another identifier, or error. |
| 285 | auto &Id = Lexer.getTok(); |
| 286 | if (Id.getKind() != AsmToken::Identifier || |
| 287 | Id.getLoc().getPointer() != Name.end()) |
| 288 | return Error("Incomplete instruction name: ", Id); |
| 289 | Name = StringRef(Name.begin(), Name.size() + Id.getString().size()); |
| 290 | Parser.Lex(); |
| 291 | } |
| 292 | // Now construct the name as first operand. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 293 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 294 | WebAssemblyOperand::Token, NameLoc, SMLoc::getFromPointer(Name.end()), |
| 295 | WebAssemblyOperand::TokOp{Name})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 296 | auto NamePair = Name.split('.'); |
| 297 | // If no '.', there is no type prefix. |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 298 | auto BaseName = NamePair.second.empty() ? NamePair.first : NamePair.second; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 299 | while (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 300 | auto &Tok = Lexer.getTok(); |
| 301 | switch (Tok.getKind()) { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 302 | case AsmToken::Identifier: { |
| 303 | auto &Id = Lexer.getTok(); |
| 304 | const MCExpr *Val; |
| 305 | SMLoc End; |
| 306 | if (Parser.parsePrimaryExpr(Val, End)) |
| 307 | return Error("Cannot parse symbol: ", Lexer.getTok()); |
| 308 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 309 | WebAssemblyOperand::Symbol, Id.getLoc(), Id.getEndLoc(), |
| 310 | WebAssemblyOperand::SymOp{Val})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 311 | break; |
| 312 | } |
| 313 | case AsmToken::Minus: |
| 314 | Parser.Lex(); |
| 315 | if (Lexer.isNot(AsmToken::Integer)) |
| 316 | return Error("Expected integer instead got: ", Lexer.getTok()); |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 317 | if (ParseOperandStartingWithInteger(true, Operands, BaseName)) |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 318 | return true; |
| 319 | break; |
| 320 | case AsmToken::Integer: |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 321 | if (ParseOperandStartingWithInteger(false, Operands, BaseName)) |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 322 | return true; |
| 323 | break; |
| 324 | case AsmToken::Real: { |
| 325 | double Val; |
| 326 | if (Tok.getString().getAsDouble(Val, false)) |
| 327 | return Error("Cannot parse real: ", Tok); |
| 328 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 329 | WebAssemblyOperand::Float, Tok.getLoc(), Tok.getEndLoc(), |
| 330 | WebAssemblyOperand::FltOp{Val})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 331 | Parser.Lex(); |
| 332 | break; |
| 333 | } |
| 334 | default: |
| 335 | return Error("Unexpected token in operand: ", Tok); |
| 336 | } |
| 337 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 338 | if (Expect(AsmToken::Comma, ",")) |
| 339 | return true; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | Parser.Lex(); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 343 | // Block instructions require a signature index, but these are missing in |
| 344 | // assembly, so we add a dummy one explicitly (since we have no control |
| 345 | // over signature tables here, we assume these will be regenerated when |
| 346 | // the wasm module is generated). |
Heejin Ahn | 2e39897 | 2018-11-01 20:32:15 +0000 | [diff] [blame] | 347 | if (BaseName == "block" || BaseName == "loop" || BaseName == "try") { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 348 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 349 | WebAssemblyOperand::Integer, NameLoc, NameLoc, |
| 350 | WebAssemblyOperand::IntOp{-1})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 351 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 352 | return false; |
| 353 | } |
| 354 | |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame^] | 355 | void onLabelParsed(MCSymbol *Symbol) override { |
| 356 | LastLabel = Symbol; |
| 357 | CurrentState = Label; |
| 358 | } |
| 359 | |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 360 | // This function processes wasm-specific directives streamed to |
| 361 | // WebAssemblyTargetStreamer, all others go to the generic parser |
| 362 | // (see WasmAsmParser). |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 363 | bool ParseDirective(AsmToken DirectiveID) override { |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 364 | // This function has a really weird return value behavior that is different |
| 365 | // from all the other parsing functions: |
| 366 | // - return true && no tokens consumed -> don't know this directive / let |
| 367 | // the generic parser handle it. |
| 368 | // - return true && tokens consumed -> a parsing error occurred. |
| 369 | // - return false -> processed this directive successfully. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 370 | assert(DirectiveID.getKind() == AsmToken::Identifier); |
| 371 | auto &Out = getStreamer(); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 372 | auto &TOut = |
| 373 | reinterpret_cast<WebAssemblyTargetStreamer &>(*Out.getTargetStreamer()); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 374 | // TODO: any time we return an error, at least one token must have been |
| 375 | // consumed, otherwise this will not signal an error to the caller. |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 376 | if (DirectiveID.getString() == ".globaltype") { |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 377 | auto SymName = ExpectIdent(); |
| 378 | if (SymName.empty()) return true; |
| 379 | if (Expect(AsmToken::Comma, ",")) return true; |
| 380 | auto TypeTok = Lexer.getTok(); |
| 381 | auto TypeName = ExpectIdent(); |
| 382 | if (TypeName.empty()) return true; |
| 383 | auto Type = ParseType(TypeName); |
| 384 | if (!Type) |
| 385 | return Error("Unknown type in .globaltype directive: ", TypeTok); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 386 | // Now set this symbol with the correct type. |
| 387 | auto WasmSym = cast<MCSymbolWasm>( |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 388 | TOut.getStreamer().getContext().getOrCreateSymbol(SymName)); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 389 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 390 | WasmSym->setGlobalType( |
| 391 | wasm::WasmGlobalType{uint8_t(Type.getValue()), true}); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 392 | // And emit the directive again. |
| 393 | TOut.emitGlobalType(WasmSym); |
| 394 | return Expect(AsmToken::EndOfStatement, "EOL"); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 395 | } else if (DirectiveID.getString() == ".functype") { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame^] | 396 | // This code has to send things to the streamer similar to |
| 397 | // WebAssemblyAsmPrinter::EmitFunctionBodyStart. |
| 398 | // TODO: would be good to factor this into a common function, but the |
| 399 | // assembler and backend really don't share any common code, and this code |
| 400 | // parses the locals seperately. |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 401 | auto SymName = ExpectIdent(); |
| 402 | if (SymName.empty()) return true; |
| 403 | auto WasmSym = cast<MCSymbolWasm>( |
| 404 | TOut.getStreamer().getContext().getOrCreateSymbol(SymName)); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame^] | 405 | if (CurrentState == Label && WasmSym == LastLabel) { |
| 406 | // This .functype indicates a start of a function. |
| 407 | CurrentState = FunctionStart; |
| 408 | } |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 409 | auto Signature = make_unique<wasm::WasmSignature>(); |
| 410 | if (Expect(AsmToken::LParen, "(")) return true; |
| 411 | if (ParseRegTypeList(Signature->Params)) return true; |
| 412 | if (Expect(AsmToken::RParen, ")")) return true; |
| 413 | if (Expect(AsmToken::MinusGreater, "->")) return true; |
| 414 | if (Expect(AsmToken::LParen, "(")) return true; |
| 415 | if (ParseRegTypeList(Signature->Returns)) return true; |
| 416 | if (Expect(AsmToken::RParen, ")")) return true; |
| 417 | WasmSym->setSignature(Signature.get()); |
| 418 | addSignature(std::move(Signature)); |
| 419 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
| 420 | TOut.emitFunctionType(WasmSym); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame^] | 421 | // TODO: backend also calls TOut.emitIndIdx, but that is not implemented. |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 422 | return Expect(AsmToken::EndOfStatement, "EOL"); |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 423 | } else if (DirectiveID.getString() == ".local") { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame^] | 424 | if (CurrentState != FunctionStart) |
| 425 | return Error(".local directive should follow the start of a function", |
| 426 | Lexer.getTok()); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 427 | SmallVector<wasm::ValType, 4> Locals; |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 428 | if (ParseRegTypeList(Locals)) return true; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 429 | TOut.emitLocal(Locals); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame^] | 430 | CurrentState = FunctionLocals; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 431 | return Expect(AsmToken::EndOfStatement, "EOL"); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 432 | } |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 433 | return true; // We didn't process this directive. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 436 | bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned & /*Opcode*/, |
| 437 | OperandVector &Operands, MCStreamer &Out, |
| 438 | uint64_t &ErrorInfo, |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 439 | bool MatchingInlineAsm) override { |
| 440 | MCInst Inst; |
| 441 | unsigned MatchResult = |
| 442 | MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm); |
| 443 | switch (MatchResult) { |
| 444 | case Match_Success: { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame^] | 445 | if (CurrentState == FunctionStart) { |
| 446 | // This is the first instruction in a function, but we haven't seen |
| 447 | // a .local directive yet. The streamer requires locals to be encoded |
| 448 | // as a prelude to the instructions, so emit an empty list of locals |
| 449 | // here. |
| 450 | auto &TOut = reinterpret_cast<WebAssemblyTargetStreamer &>( |
| 451 | *Out.getTargetStreamer()); |
| 452 | TOut.emitLocal(SmallVector<wasm::ValType, 0>()); |
| 453 | } |
| 454 | CurrentState = Instructions; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 455 | Out.EmitInstruction(Inst, getSTI()); |
| 456 | return false; |
| 457 | } |
| 458 | case Match_MissingFeature: |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 459 | return Parser.Error( |
| 460 | IDLoc, "instruction requires a WASM feature not currently enabled"); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 461 | case Match_MnemonicFail: |
| 462 | return Parser.Error(IDLoc, "invalid instruction"); |
| 463 | case Match_NearMisses: |
| 464 | return Parser.Error(IDLoc, "ambiguous instruction"); |
| 465 | case Match_InvalidTiedOperand: |
| 466 | case Match_InvalidOperand: { |
| 467 | SMLoc ErrorLoc = IDLoc; |
| 468 | if (ErrorInfo != ~0ULL) { |
| 469 | if (ErrorInfo >= Operands.size()) |
| 470 | return Parser.Error(IDLoc, "too few operands for instruction"); |
| 471 | ErrorLoc = Operands[ErrorInfo]->getStartLoc(); |
| 472 | if (ErrorLoc == SMLoc()) |
| 473 | ErrorLoc = IDLoc; |
| 474 | } |
| 475 | return Parser.Error(ErrorLoc, "invalid operand for instruction"); |
| 476 | } |
| 477 | } |
| 478 | llvm_unreachable("Implement any new match types added!"); |
| 479 | } |
| 480 | }; |
| 481 | } // end anonymous namespace |
| 482 | |
| 483 | // Force static initialization. |
| 484 | extern "C" void LLVMInitializeWebAssemblyAsmParser() { |
| 485 | RegisterMCAsmParser<WebAssemblyAsmParser> X(getTheWebAssemblyTarget32()); |
| 486 | RegisterMCAsmParser<WebAssemblyAsmParser> Y(getTheWebAssemblyTarget64()); |
| 487 | } |
| 488 | |
| 489 | #define GET_REGISTER_MATCHER |
| 490 | #define GET_MATCHER_IMPLEMENTATION |
| 491 | #include "WebAssemblyGenAsmMatcher.inc" |