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 | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 41 | enum KindTy { Token, Integer, Float, Symbol, BrList } 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 | |
Wouter van Oortmerssen | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 61 | struct BrLOp { |
| 62 | std::vector<unsigned> List; |
| 63 | }; |
| 64 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 65 | union { |
| 66 | struct TokOp Tok; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 67 | struct IntOp Int; |
| 68 | struct FltOp Flt; |
| 69 | struct SymOp Sym; |
Wouter van Oortmerssen | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 70 | struct BrLOp BrL; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, TokOp T) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 74 | : Kind(K), StartLoc(Start), EndLoc(End), Tok(T) {} |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 75 | WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, IntOp I) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 76 | : Kind(K), StartLoc(Start), EndLoc(End), Int(I) {} |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 77 | WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, FltOp F) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 78 | : Kind(K), StartLoc(Start), EndLoc(End), Flt(F) {} |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 79 | WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, SymOp S) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 80 | : Kind(K), StartLoc(Start), EndLoc(End), Sym(S) {} |
Wouter van Oortmerssen | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 81 | WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End) |
| 82 | : Kind(K), StartLoc(Start), EndLoc(End), BrL() {} |
| 83 | |
| 84 | ~WebAssemblyOperand() { |
| 85 | if (isBrList()) |
| 86 | BrL.~BrLOp(); |
| 87 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 88 | |
| 89 | bool isToken() const override { return Kind == Token; } |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 90 | bool isImm() const override { |
| 91 | return Kind == Integer || Kind == Float || Kind == Symbol; |
| 92 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 93 | bool isMem() const override { return false; } |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 94 | bool isReg() const override { return false; } |
Wouter van Oortmerssen | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 95 | bool isBrList() const { return Kind == BrList; } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 96 | |
| 97 | unsigned getReg() const override { |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 98 | llvm_unreachable("Assembly inspects a register operand"); |
| 99 | return 0; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | StringRef getToken() const { |
| 103 | assert(isToken()); |
| 104 | return Tok.Tok; |
| 105 | } |
| 106 | |
| 107 | SMLoc getStartLoc() const override { return StartLoc; } |
| 108 | SMLoc getEndLoc() const override { return EndLoc; } |
| 109 | |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 110 | void addRegOperands(MCInst &, unsigned) const { |
| 111 | // Required by the assembly matcher. |
| 112 | llvm_unreachable("Assembly matcher creates register operands"); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void addImmOperands(MCInst &Inst, unsigned N) const { |
| 116 | assert(N == 1 && "Invalid number of operands!"); |
| 117 | if (Kind == Integer) |
| 118 | Inst.addOperand(MCOperand::createImm(Int.Val)); |
| 119 | else if (Kind == Float) |
| 120 | Inst.addOperand(MCOperand::createFPImm(Flt.Val)); |
| 121 | else if (Kind == Symbol) |
| 122 | Inst.addOperand(MCOperand::createExpr(Sym.Exp)); |
| 123 | else |
| 124 | llvm_unreachable("Should be immediate or symbol!"); |
| 125 | } |
| 126 | |
Wouter van Oortmerssen | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 127 | void addBrListOperands(MCInst &Inst, unsigned N) const { |
| 128 | assert(N == 1 && isBrList() && "Invalid BrList!"); |
| 129 | for (auto Br : BrL.List) |
| 130 | Inst.addOperand(MCOperand::createImm(Br)); |
| 131 | } |
| 132 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 133 | void print(raw_ostream &OS) const override { |
| 134 | switch (Kind) { |
| 135 | case Token: |
| 136 | OS << "Tok:" << Tok.Tok; |
| 137 | break; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 138 | case Integer: |
| 139 | OS << "Int:" << Int.Val; |
| 140 | break; |
| 141 | case Float: |
| 142 | OS << "Flt:" << Flt.Val; |
| 143 | break; |
| 144 | case Symbol: |
| 145 | OS << "Sym:" << Sym.Exp; |
| 146 | break; |
Wouter van Oortmerssen | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 147 | case BrList: |
| 148 | OS << "BrList:" << BrL.List.size(); |
| 149 | break; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | class WebAssemblyAsmParser final : public MCTargetAsmParser { |
| 155 | MCAsmParser &Parser; |
| 156 | MCAsmLexer &Lexer; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 157 | |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 158 | // Much like WebAssemblyAsmPrinter in the backend, we have to own these. |
| 159 | std::vector<std::unique_ptr<wasm::WasmSignature>> Signatures; |
| 160 | |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 161 | // Order of labels, directives and instructions in a .s file have no |
| 162 | // syntactical enforcement. This class is a callback from the actual parser, |
| 163 | // and yet we have to be feeding data to the streamer in a very particular |
| 164 | // order to ensure a correct binary encoding that matches the regular backend |
| 165 | // (the streamer does not enforce this). This "state machine" enum helps |
| 166 | // guarantee that correct order. |
| 167 | enum ParserState { |
| 168 | FileStart, |
| 169 | Label, |
| 170 | FunctionStart, |
| 171 | FunctionLocals, |
| 172 | Instructions, |
| 173 | } CurrentState = FileStart; |
| 174 | |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 175 | // For ensuring blocks are properly nested. |
| 176 | enum NestingType { |
| 177 | Function, |
| 178 | Block, |
| 179 | Loop, |
| 180 | Try, |
| 181 | If, |
| 182 | Else, |
| 183 | Undefined, |
| 184 | }; |
| 185 | std::vector<NestingType> NestingStack; |
| 186 | |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 187 | // We track this to see if a .functype following a label is the same, |
| 188 | // as this is how we recognize the start of a function. |
| 189 | MCSymbol *LastLabel = nullptr; |
| 190 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 191 | public: |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 192 | WebAssemblyAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, |
| 193 | const MCInstrInfo &MII, const MCTargetOptions &Options) |
| 194 | : MCTargetAsmParser(Options, STI, MII), Parser(Parser), |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 195 | Lexer(Parser.getLexer()) { |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 196 | setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | #define GET_ASSEMBLER_HEADER |
| 200 | #include "WebAssemblyGenAsmMatcher.inc" |
| 201 | |
| 202 | // TODO: This is required to be implemented, but appears unused. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 203 | bool ParseRegister(unsigned & /*RegNo*/, SMLoc & /*StartLoc*/, |
| 204 | SMLoc & /*EndLoc*/) override { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 205 | llvm_unreachable("ParseRegister is not implemented."); |
| 206 | } |
| 207 | |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 208 | bool error(const Twine &Msg, const AsmToken &Tok) { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 209 | return Parser.Error(Tok.getLoc(), Msg + Tok.getString()); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 212 | bool error(const Twine &Msg) { |
| 213 | return Parser.Error(Lexer.getTok().getLoc(), Msg); |
| 214 | } |
| 215 | |
| 216 | void addSignature(std::unique_ptr<wasm::WasmSignature> &&Sig) { |
| 217 | Signatures.push_back(std::move(Sig)); |
| 218 | } |
| 219 | |
| 220 | std::pair<StringRef, StringRef> nestingString(NestingType NT) { |
| 221 | switch (NT) { |
| 222 | case Function: |
| 223 | return {"function", "end_function"}; |
| 224 | case Block: |
| 225 | return {"block", "end_block"}; |
| 226 | case Loop: |
| 227 | return {"loop", "end_loop"}; |
| 228 | case Try: |
| 229 | return {"try", "end_try"}; |
| 230 | case If: |
| 231 | return {"if", "end_if"}; |
| 232 | case Else: |
| 233 | return {"else", "end_if"}; |
| 234 | default: |
| 235 | llvm_unreachable("unknown NestingType"); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void push(NestingType NT) { NestingStack.push_back(NT); } |
| 240 | |
| 241 | bool pop(StringRef Ins, NestingType NT1, NestingType NT2 = Undefined) { |
| 242 | if (NestingStack.empty()) |
| 243 | return error(Twine("End of block construct with no start: ") + Ins); |
| 244 | auto Top = NestingStack.back(); |
| 245 | if (Top != NT1 && Top != NT2) |
| 246 | return error(Twine("Block construct type mismatch, expected: ") + |
| 247 | nestingString(Top).second + ", instead got: " + Ins); |
| 248 | NestingStack.pop_back(); |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | bool ensureEmptyNestingStack() { |
| 253 | auto err = !NestingStack.empty(); |
| 254 | while (!NestingStack.empty()) { |
| 255 | error(Twine("Unmatched block construct(s) at function end: ") + |
| 256 | nestingString(NestingStack.back()).first); |
| 257 | NestingStack.pop_back(); |
| 258 | } |
| 259 | return err; |
| 260 | } |
| 261 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 262 | bool isNext(AsmToken::TokenKind Kind) { |
| 263 | auto Ok = Lexer.is(Kind); |
| 264 | if (Ok) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 265 | Parser.Lex(); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 266 | return Ok; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 269 | bool expect(AsmToken::TokenKind Kind, const char *KindName) { |
| 270 | if (!isNext(Kind)) |
| 271 | return error(std::string("Expected ") + KindName + ", instead got: ", |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 272 | Lexer.getTok()); |
| 273 | return false; |
| 274 | } |
| 275 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 276 | StringRef expectIdent() { |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 277 | if (!Lexer.is(AsmToken::Identifier)) { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 278 | error("Expected identifier, got: ", Lexer.getTok()); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 279 | return StringRef(); |
| 280 | } |
| 281 | auto Name = Lexer.getTok().getString(); |
| 282 | Parser.Lex(); |
| 283 | return Name; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 286 | Optional<wasm::ValType> parseType(const StringRef &Type) { |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 287 | // FIXME: can't use StringSwitch because wasm::ValType doesn't have a |
| 288 | // "invalid" value. |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 289 | if (Type == "i32") |
| 290 | return wasm::ValType::I32; |
| 291 | if (Type == "i64") |
| 292 | return wasm::ValType::I64; |
| 293 | if (Type == "f32") |
| 294 | return wasm::ValType::F32; |
| 295 | if (Type == "f64") |
| 296 | return wasm::ValType::F64; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 297 | if (Type == "v128" || Type == "i8x16" || Type == "i16x8" || |
| 298 | Type == "i32x4" || Type == "i64x2" || Type == "f32x4" || |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 299 | Type == "f64x2") |
| 300 | return wasm::ValType::V128; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 301 | return Optional<wasm::ValType>(); |
| 302 | } |
| 303 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 304 | bool parseRegTypeList(SmallVectorImpl<wasm::ValType> &Types) { |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 305 | while (Lexer.is(AsmToken::Identifier)) { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 306 | auto Type = parseType(Lexer.getTok().getString()); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 307 | if (!Type) |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 308 | return true; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 309 | Types.push_back(Type.getValue()); |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 310 | Parser.Lex(); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 311 | if (!isNext(AsmToken::Comma)) |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 312 | break; |
| 313 | } |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 314 | return false; |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 317 | void parseSingleInteger(bool IsNegative, OperandVector &Operands) { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 318 | auto &Int = Lexer.getTok(); |
| 319 | int64_t Val = Int.getIntVal(); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 320 | if (IsNegative) |
| 321 | Val = -Val; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 322 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 323 | WebAssemblyOperand::Integer, Int.getLoc(), Int.getEndLoc(), |
| 324 | WebAssemblyOperand::IntOp{Val})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 325 | Parser.Lex(); |
| 326 | } |
| 327 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 328 | bool parseOperandStartingWithInteger(bool IsNegative, OperandVector &Operands, |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 329 | StringRef InstName) { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 330 | parseSingleInteger(IsNegative, Operands); |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 331 | // FIXME: there is probably a cleaner way to do this. |
| 332 | auto IsLoadStore = InstName.startswith("load") || |
| 333 | InstName.startswith("store") || |
| 334 | InstName.startswith("atomic_load") || |
| 335 | InstName.startswith("atomic_store"); |
| 336 | if (IsLoadStore) { |
| 337 | // Parse load/store operands of the form: offset align |
| 338 | auto &Offset = Lexer.getTok(); |
| 339 | if (Offset.is(AsmToken::Integer)) { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 340 | parseSingleInteger(false, Operands); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 341 | } else { |
| 342 | // Alignment not specified. |
| 343 | // FIXME: correctly derive a default from the instruction. |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 344 | // We can't just call WebAssembly::GetDefaultP2Align since we don't have |
| 345 | // an opcode until after the assembly matcher. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 346 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 347 | WebAssemblyOperand::Integer, Offset.getLoc(), Offset.getEndLoc(), |
| 348 | WebAssemblyOperand::IntOp{0})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | return false; |
| 352 | } |
| 353 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 354 | bool ParseInstruction(ParseInstructionInfo & /*Info*/, StringRef Name, |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 355 | SMLoc NameLoc, OperandVector &Operands) override { |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 356 | // Note: Name does NOT point into the sourcecode, but to a local, so |
| 357 | // use NameLoc instead. |
| 358 | Name = StringRef(NameLoc.getPointer(), Name.size()); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 359 | |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 360 | // WebAssembly has instructions with / in them, which AsmLexer parses |
| 361 | // as seperate tokens, so if we find such tokens immediately adjacent (no |
| 362 | // whitespace), expand the name to include them: |
| 363 | for (;;) { |
| 364 | auto &Sep = Lexer.getTok(); |
| 365 | if (Sep.getLoc().getPointer() != Name.end() || |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 366 | Sep.getKind() != AsmToken::Slash) |
| 367 | break; |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 368 | // Extend name with / |
| 369 | Name = StringRef(Name.begin(), Name.size() + Sep.getString().size()); |
| 370 | Parser.Lex(); |
| 371 | // We must now find another identifier, or error. |
| 372 | auto &Id = Lexer.getTok(); |
| 373 | if (Id.getKind() != AsmToken::Identifier || |
| 374 | Id.getLoc().getPointer() != Name.end()) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 375 | return error("Incomplete instruction name: ", Id); |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 376 | Name = StringRef(Name.begin(), Name.size() + Id.getString().size()); |
| 377 | Parser.Lex(); |
| 378 | } |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 379 | |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 380 | // Now construct the name as first operand. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 381 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 382 | WebAssemblyOperand::Token, NameLoc, SMLoc::getFromPointer(Name.end()), |
| 383 | WebAssemblyOperand::TokOp{Name})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 384 | auto NamePair = Name.split('.'); |
| 385 | // If no '.', there is no type prefix. |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 386 | auto BaseName = NamePair.second.empty() ? NamePair.first : NamePair.second; |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 387 | |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 388 | // If this instruction is part of a control flow structure, ensure |
| 389 | // proper nesting. |
| 390 | if (BaseName == "block") { |
| 391 | push(Block); |
| 392 | } else if (BaseName == "loop") { |
| 393 | push(Loop); |
| 394 | } else if (BaseName == "try") { |
| 395 | push(Try); |
| 396 | } else if (BaseName == "if") { |
| 397 | push(If); |
| 398 | } else if (BaseName == "else") { |
| 399 | if (pop(BaseName, If)) |
| 400 | return true; |
| 401 | push(Else); |
| 402 | } else if (BaseName == "catch") { |
| 403 | if (pop(BaseName, Try)) |
| 404 | return true; |
| 405 | push(Try); |
| 406 | } else if (BaseName == "catch_all") { |
| 407 | if (pop(BaseName, Try)) |
| 408 | return true; |
| 409 | push(Try); |
| 410 | } else if (BaseName == "end_if") { |
| 411 | if (pop(BaseName, If, Else)) |
| 412 | return true; |
| 413 | } else if (BaseName == "end_try") { |
| 414 | if (pop(BaseName, Try)) |
| 415 | return true; |
| 416 | } else if (BaseName == "end_loop") { |
| 417 | if (pop(BaseName, Loop)) |
| 418 | return true; |
| 419 | } else if (BaseName == "end_block") { |
| 420 | if (pop(BaseName, Block)) |
| 421 | return true; |
| 422 | } else if (BaseName == "end_function") { |
| 423 | if (pop(BaseName, Function) || ensureEmptyNestingStack()) |
| 424 | return true; |
| 425 | } |
| 426 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 427 | while (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 428 | auto &Tok = Lexer.getTok(); |
| 429 | switch (Tok.getKind()) { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 430 | case AsmToken::Identifier: { |
| 431 | auto &Id = Lexer.getTok(); |
| 432 | const MCExpr *Val; |
| 433 | SMLoc End; |
| 434 | if (Parser.parsePrimaryExpr(Val, End)) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 435 | return error("Cannot parse symbol: ", Lexer.getTok()); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 436 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 437 | WebAssemblyOperand::Symbol, Id.getLoc(), Id.getEndLoc(), |
| 438 | WebAssemblyOperand::SymOp{Val})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | case AsmToken::Minus: |
| 442 | Parser.Lex(); |
| 443 | if (Lexer.isNot(AsmToken::Integer)) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 444 | return error("Expected integer instead got: ", Lexer.getTok()); |
| 445 | if (parseOperandStartingWithInteger(true, Operands, BaseName)) |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 446 | return true; |
| 447 | break; |
| 448 | case AsmToken::Integer: |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 449 | if (parseOperandStartingWithInteger(false, Operands, BaseName)) |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 450 | return true; |
| 451 | break; |
| 452 | case AsmToken::Real: { |
| 453 | double Val; |
| 454 | if (Tok.getString().getAsDouble(Val, false)) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 455 | return error("Cannot parse real: ", Tok); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 456 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 457 | WebAssemblyOperand::Float, Tok.getLoc(), Tok.getEndLoc(), |
| 458 | WebAssemblyOperand::FltOp{Val})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 459 | Parser.Lex(); |
| 460 | break; |
| 461 | } |
Wouter van Oortmerssen | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 462 | case AsmToken::LCurly: { |
| 463 | Parser.Lex(); |
| 464 | auto Op = make_unique<WebAssemblyOperand>( |
| 465 | WebAssemblyOperand::BrList, Tok.getLoc(), Tok.getEndLoc()); |
| 466 | if (!Lexer.is(AsmToken::RCurly)) |
| 467 | for (;;) { |
| 468 | Op->BrL.List.push_back(Lexer.getTok().getIntVal()); |
| 469 | expect(AsmToken::Integer, "integer"); |
| 470 | if (!isNext(AsmToken::Comma)) |
| 471 | break; |
| 472 | } |
| 473 | expect(AsmToken::RCurly, "}"); |
| 474 | Operands.push_back(std::move(Op)); |
| 475 | break; |
| 476 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 477 | default: |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 478 | return error("Unexpected token in operand: ", Tok); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 479 | } |
| 480 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 481 | if (expect(AsmToken::Comma, ",")) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 482 | return true; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | Parser.Lex(); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 486 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 487 | // Block instructions require a signature index, but these are missing in |
| 488 | // assembly, so we add a dummy one explicitly (since we have no control |
| 489 | // over signature tables here, we assume these will be regenerated when |
| 490 | // the wasm module is generated). |
Wouter van Oortmerssen | f227621 | 2018-12-26 22:55:26 +0000 | [diff] [blame] | 491 | if (BaseName == "block" || BaseName == "loop" || BaseName == "try" || |
| 492 | BaseName == "if") { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 493 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 494 | WebAssemblyOperand::Integer, NameLoc, NameLoc, |
| 495 | WebAssemblyOperand::IntOp{-1})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 496 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 497 | return false; |
| 498 | } |
| 499 | |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 500 | void onLabelParsed(MCSymbol *Symbol) override { |
| 501 | LastLabel = Symbol; |
| 502 | CurrentState = Label; |
| 503 | } |
| 504 | |
Heejin Ahn | be5e587 | 2018-12-11 01:11:04 +0000 | [diff] [blame] | 505 | bool parseSignature(wasm::WasmSignature *Signature) { |
| 506 | if (expect(AsmToken::LParen, "(")) |
| 507 | return true; |
| 508 | if (parseRegTypeList(Signature->Params)) |
| 509 | return true; |
| 510 | if (expect(AsmToken::RParen, ")")) |
| 511 | return true; |
| 512 | if (expect(AsmToken::MinusGreater, "->")) |
| 513 | return true; |
| 514 | if (expect(AsmToken::LParen, "(")) |
| 515 | return true; |
| 516 | if (parseRegTypeList(Signature->Returns)) |
| 517 | return true; |
| 518 | if (expect(AsmToken::RParen, ")")) |
| 519 | return true; |
| 520 | return false; |
| 521 | } |
| 522 | |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 523 | // This function processes wasm-specific directives streamed to |
| 524 | // WebAssemblyTargetStreamer, all others go to the generic parser |
| 525 | // (see WasmAsmParser). |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 526 | bool ParseDirective(AsmToken DirectiveID) override { |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 527 | // This function has a really weird return value behavior that is different |
| 528 | // from all the other parsing functions: |
| 529 | // - return true && no tokens consumed -> don't know this directive / let |
| 530 | // the generic parser handle it. |
| 531 | // - return true && tokens consumed -> a parsing error occurred. |
| 532 | // - return false -> processed this directive successfully. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 533 | assert(DirectiveID.getKind() == AsmToken::Identifier); |
| 534 | auto &Out = getStreamer(); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 535 | auto &TOut = |
| 536 | reinterpret_cast<WebAssemblyTargetStreamer &>(*Out.getTargetStreamer()); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 537 | |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 538 | // TODO: any time we return an error, at least one token must have been |
| 539 | // consumed, otherwise this will not signal an error to the caller. |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 540 | if (DirectiveID.getString() == ".globaltype") { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 541 | auto SymName = expectIdent(); |
| 542 | if (SymName.empty()) |
| 543 | return true; |
| 544 | if (expect(AsmToken::Comma, ",")) |
| 545 | return true; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 546 | auto TypeTok = Lexer.getTok(); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 547 | auto TypeName = expectIdent(); |
| 548 | if (TypeName.empty()) |
| 549 | return true; |
| 550 | auto Type = parseType(TypeName); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 551 | if (!Type) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 552 | return error("Unknown type in .globaltype directive: ", TypeTok); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 553 | // Now set this symbol with the correct type. |
| 554 | auto WasmSym = cast<MCSymbolWasm>( |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 555 | TOut.getStreamer().getContext().getOrCreateSymbol(SymName)); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 556 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 557 | WasmSym->setGlobalType( |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 558 | wasm::WasmGlobalType{uint8_t(Type.getValue()), true}); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 559 | // And emit the directive again. |
| 560 | TOut.emitGlobalType(WasmSym); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 561 | return expect(AsmToken::EndOfStatement, "EOL"); |
| 562 | } |
| 563 | |
| 564 | if (DirectiveID.getString() == ".functype") { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 565 | // This code has to send things to the streamer similar to |
| 566 | // WebAssemblyAsmPrinter::EmitFunctionBodyStart. |
| 567 | // TODO: would be good to factor this into a common function, but the |
| 568 | // assembler and backend really don't share any common code, and this code |
| 569 | // parses the locals seperately. |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 570 | auto SymName = expectIdent(); |
| 571 | if (SymName.empty()) |
| 572 | return true; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 573 | auto WasmSym = cast<MCSymbolWasm>( |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 574 | TOut.getStreamer().getContext().getOrCreateSymbol(SymName)); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 575 | if (CurrentState == Label && WasmSym == LastLabel) { |
| 576 | // This .functype indicates a start of a function. |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 577 | if (ensureEmptyNestingStack()) |
| 578 | return true; |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 579 | CurrentState = FunctionStart; |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 580 | push(Function); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 581 | } |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 582 | auto Signature = make_unique<wasm::WasmSignature>(); |
Heejin Ahn | be5e587 | 2018-12-11 01:11:04 +0000 | [diff] [blame] | 583 | if (parseSignature(Signature.get())) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 584 | return true; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 585 | WasmSym->setSignature(Signature.get()); |
| 586 | addSignature(std::move(Signature)); |
| 587 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
| 588 | TOut.emitFunctionType(WasmSym); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 589 | // TODO: backend also calls TOut.emitIndIdx, but that is not implemented. |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 590 | return expect(AsmToken::EndOfStatement, "EOL"); |
| 591 | } |
| 592 | |
Heejin Ahn | be5e587 | 2018-12-11 01:11:04 +0000 | [diff] [blame] | 593 | if (DirectiveID.getString() == ".eventtype") { |
| 594 | auto SymName = expectIdent(); |
| 595 | if (SymName.empty()) |
| 596 | return true; |
| 597 | auto WasmSym = cast<MCSymbolWasm>( |
| 598 | TOut.getStreamer().getContext().getOrCreateSymbol(SymName)); |
| 599 | auto Signature = make_unique<wasm::WasmSignature>(); |
| 600 | if (parseRegTypeList(Signature->Params)) |
| 601 | return true; |
| 602 | WasmSym->setSignature(Signature.get()); |
| 603 | addSignature(std::move(Signature)); |
| 604 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT); |
| 605 | TOut.emitEventType(WasmSym); |
| 606 | // TODO: backend also calls TOut.emitIndIdx, but that is not implemented. |
| 607 | return expect(AsmToken::EndOfStatement, "EOL"); |
| 608 | } |
| 609 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 610 | if (DirectiveID.getString() == ".local") { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 611 | if (CurrentState != FunctionStart) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 612 | return error(".local directive should follow the start of a function", |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 613 | Lexer.getTok()); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 614 | SmallVector<wasm::ValType, 4> Locals; |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 615 | if (parseRegTypeList(Locals)) |
| 616 | return true; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 617 | TOut.emitLocal(Locals); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 618 | CurrentState = FunctionLocals; |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 619 | return expect(AsmToken::EndOfStatement, "EOL"); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 620 | } |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 621 | |
| 622 | return true; // We didn't process this directive. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 625 | bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned & /*Opcode*/, |
| 626 | OperandVector &Operands, MCStreamer &Out, |
| 627 | uint64_t &ErrorInfo, |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 628 | bool MatchingInlineAsm) override { |
| 629 | MCInst Inst; |
| 630 | unsigned MatchResult = |
| 631 | MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm); |
| 632 | switch (MatchResult) { |
| 633 | case Match_Success: { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 634 | if (CurrentState == FunctionStart) { |
| 635 | // This is the first instruction in a function, but we haven't seen |
| 636 | // a .local directive yet. The streamer requires locals to be encoded |
| 637 | // as a prelude to the instructions, so emit an empty list of locals |
| 638 | // here. |
| 639 | auto &TOut = reinterpret_cast<WebAssemblyTargetStreamer &>( |
| 640 | *Out.getTargetStreamer()); |
| 641 | TOut.emitLocal(SmallVector<wasm::ValType, 0>()); |
| 642 | } |
| 643 | CurrentState = Instructions; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 644 | Out.EmitInstruction(Inst, getSTI()); |
| 645 | return false; |
| 646 | } |
| 647 | case Match_MissingFeature: |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 648 | return Parser.Error( |
| 649 | IDLoc, "instruction requires a WASM feature not currently enabled"); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 650 | case Match_MnemonicFail: |
| 651 | return Parser.Error(IDLoc, "invalid instruction"); |
| 652 | case Match_NearMisses: |
| 653 | return Parser.Error(IDLoc, "ambiguous instruction"); |
| 654 | case Match_InvalidTiedOperand: |
| 655 | case Match_InvalidOperand: { |
| 656 | SMLoc ErrorLoc = IDLoc; |
| 657 | if (ErrorInfo != ~0ULL) { |
| 658 | if (ErrorInfo >= Operands.size()) |
| 659 | return Parser.Error(IDLoc, "too few operands for instruction"); |
| 660 | ErrorLoc = Operands[ErrorInfo]->getStartLoc(); |
| 661 | if (ErrorLoc == SMLoc()) |
| 662 | ErrorLoc = IDLoc; |
| 663 | } |
| 664 | return Parser.Error(ErrorLoc, "invalid operand for instruction"); |
| 665 | } |
| 666 | } |
| 667 | llvm_unreachable("Implement any new match types added!"); |
| 668 | } |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 669 | |
| 670 | void onEndOfFile() override { ensureEmptyNestingStack(); } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 671 | }; |
| 672 | } // end anonymous namespace |
| 673 | |
| 674 | // Force static initialization. |
| 675 | extern "C" void LLVMInitializeWebAssemblyAsmParser() { |
| 676 | RegisterMCAsmParser<WebAssemblyAsmParser> X(getTheWebAssemblyTarget32()); |
| 677 | RegisterMCAsmParser<WebAssemblyAsmParser> Y(getTheWebAssemblyTarget64()); |
| 678 | } |
| 679 | |
| 680 | #define GET_REGISTER_MATCHER |
| 681 | #define GET_MATCHER_IMPLEMENTATION |
| 682 | #include "WebAssemblyGenAsmMatcher.inc" |