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 | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame] | 301 | if (Type == "except_ref") |
| 302 | return wasm::ValType::EXCEPT_REF; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 303 | return Optional<wasm::ValType>(); |
| 304 | } |
| 305 | |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 306 | WebAssembly::ExprType parseBlockType(StringRef ID) { |
| 307 | return StringSwitch<WebAssembly::ExprType>(ID) |
| 308 | .Case("i32", WebAssembly::ExprType::I32) |
| 309 | .Case("i64", WebAssembly::ExprType::I64) |
| 310 | .Case("f32", WebAssembly::ExprType::F32) |
| 311 | .Case("f64", WebAssembly::ExprType::F64) |
| 312 | .Case("v128", WebAssembly::ExprType::V128) |
| 313 | .Case("except_ref", WebAssembly::ExprType::ExceptRef) |
| 314 | .Case("void", WebAssembly::ExprType::Void) |
| 315 | .Default(WebAssembly::ExprType::Invalid); |
| 316 | } |
| 317 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 318 | bool parseRegTypeList(SmallVectorImpl<wasm::ValType> &Types) { |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 319 | while (Lexer.is(AsmToken::Identifier)) { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 320 | auto Type = parseType(Lexer.getTok().getString()); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 321 | if (!Type) |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame] | 322 | return error("unknown type: ", Lexer.getTok()); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 323 | Types.push_back(Type.getValue()); |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 324 | Parser.Lex(); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 325 | if (!isNext(AsmToken::Comma)) |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 326 | break; |
| 327 | } |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 328 | return false; |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 331 | void parseSingleInteger(bool IsNegative, OperandVector &Operands) { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 332 | auto &Int = Lexer.getTok(); |
| 333 | int64_t Val = Int.getIntVal(); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 334 | if (IsNegative) |
| 335 | Val = -Val; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 336 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 337 | WebAssemblyOperand::Integer, Int.getLoc(), Int.getEndLoc(), |
| 338 | WebAssemblyOperand::IntOp{Val})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 339 | Parser.Lex(); |
| 340 | } |
| 341 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 342 | bool parseOperandStartingWithInteger(bool IsNegative, OperandVector &Operands, |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 343 | StringRef InstName) { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 344 | parseSingleInteger(IsNegative, Operands); |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 345 | // FIXME: there is probably a cleaner way to do this. |
| 346 | auto IsLoadStore = InstName.startswith("load") || |
| 347 | InstName.startswith("store") || |
| 348 | InstName.startswith("atomic_load") || |
| 349 | InstName.startswith("atomic_store"); |
| 350 | if (IsLoadStore) { |
| 351 | // Parse load/store operands of the form: offset align |
| 352 | auto &Offset = Lexer.getTok(); |
| 353 | if (Offset.is(AsmToken::Integer)) { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 354 | parseSingleInteger(false, Operands); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 355 | } else { |
| 356 | // Alignment not specified. |
| 357 | // FIXME: correctly derive a default from the instruction. |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 358 | // We can't just call WebAssembly::GetDefaultP2Align since we don't have |
| 359 | // an opcode until after the assembly matcher. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 360 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 361 | WebAssemblyOperand::Integer, Offset.getLoc(), Offset.getEndLoc(), |
| 362 | WebAssemblyOperand::IntOp{0})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | return false; |
| 366 | } |
| 367 | |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 368 | void addBlockTypeOperand(OperandVector &Operands, SMLoc NameLoc, |
| 369 | WebAssembly::ExprType BT) { |
| 370 | Operands.push_back(make_unique<WebAssemblyOperand>( |
| 371 | WebAssemblyOperand::Integer, NameLoc, NameLoc, |
| 372 | WebAssemblyOperand::IntOp{static_cast<int64_t>(BT)})); |
| 373 | } |
| 374 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 375 | bool ParseInstruction(ParseInstructionInfo & /*Info*/, StringRef Name, |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 376 | SMLoc NameLoc, OperandVector &Operands) override { |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 377 | // Note: Name does NOT point into the sourcecode, but to a local, so |
| 378 | // use NameLoc instead. |
| 379 | Name = StringRef(NameLoc.getPointer(), Name.size()); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 380 | |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 381 | // WebAssembly has instructions with / in them, which AsmLexer parses |
| 382 | // as seperate tokens, so if we find such tokens immediately adjacent (no |
| 383 | // whitespace), expand the name to include them: |
| 384 | for (;;) { |
| 385 | auto &Sep = Lexer.getTok(); |
| 386 | if (Sep.getLoc().getPointer() != Name.end() || |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 387 | Sep.getKind() != AsmToken::Slash) |
| 388 | break; |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 389 | // Extend name with / |
| 390 | Name = StringRef(Name.begin(), Name.size() + Sep.getString().size()); |
| 391 | Parser.Lex(); |
| 392 | // We must now find another identifier, or error. |
| 393 | auto &Id = Lexer.getTok(); |
| 394 | if (Id.getKind() != AsmToken::Identifier || |
| 395 | Id.getLoc().getPointer() != Name.end()) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 396 | return error("Incomplete instruction name: ", Id); |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 397 | Name = StringRef(Name.begin(), Name.size() + Id.getString().size()); |
| 398 | Parser.Lex(); |
| 399 | } |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 400 | |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 401 | // Now construct the name as first operand. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 402 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 403 | WebAssemblyOperand::Token, NameLoc, SMLoc::getFromPointer(Name.end()), |
| 404 | WebAssemblyOperand::TokOp{Name})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 405 | auto NamePair = Name.split('.'); |
| 406 | // If no '.', there is no type prefix. |
Wouter van Oortmerssen | 0c83c3f | 2018-10-01 17:20:31 +0000 | [diff] [blame] | 407 | auto BaseName = NamePair.second.empty() ? NamePair.first : NamePair.second; |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 408 | |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 409 | // If this instruction is part of a control flow structure, ensure |
| 410 | // proper nesting. |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 411 | bool ExpectBlockType = false; |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 412 | if (BaseName == "block") { |
| 413 | push(Block); |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 414 | ExpectBlockType = true; |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 415 | } else if (BaseName == "loop") { |
| 416 | push(Loop); |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 417 | ExpectBlockType = true; |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 418 | } else if (BaseName == "try") { |
| 419 | push(Try); |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 420 | ExpectBlockType = true; |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 421 | } else if (BaseName == "if") { |
| 422 | push(If); |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 423 | ExpectBlockType = true; |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 424 | } else if (BaseName == "else") { |
| 425 | if (pop(BaseName, If)) |
| 426 | return true; |
| 427 | push(Else); |
| 428 | } else if (BaseName == "catch") { |
| 429 | if (pop(BaseName, Try)) |
| 430 | return true; |
| 431 | push(Try); |
| 432 | } else if (BaseName == "catch_all") { |
| 433 | if (pop(BaseName, Try)) |
| 434 | return true; |
| 435 | push(Try); |
| 436 | } else if (BaseName == "end_if") { |
| 437 | if (pop(BaseName, If, Else)) |
| 438 | return true; |
| 439 | } else if (BaseName == "end_try") { |
| 440 | if (pop(BaseName, Try)) |
| 441 | return true; |
| 442 | } else if (BaseName == "end_loop") { |
| 443 | if (pop(BaseName, Loop)) |
| 444 | return true; |
| 445 | } else if (BaseName == "end_block") { |
| 446 | if (pop(BaseName, Block)) |
| 447 | return true; |
| 448 | } else if (BaseName == "end_function") { |
| 449 | if (pop(BaseName, Function) || ensureEmptyNestingStack()) |
| 450 | return true; |
| 451 | } |
| 452 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 453 | while (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 454 | auto &Tok = Lexer.getTok(); |
| 455 | switch (Tok.getKind()) { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 456 | case AsmToken::Identifier: { |
| 457 | auto &Id = Lexer.getTok(); |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 458 | if (ExpectBlockType) { |
| 459 | // Assume this identifier is a block_type. |
| 460 | auto BT = parseBlockType(Id.getString()); |
| 461 | if (BT == WebAssembly::ExprType::Invalid) |
| 462 | return error("Unknown block type: ", Id); |
| 463 | addBlockTypeOperand(Operands, NameLoc, BT); |
| 464 | Parser.Lex(); |
| 465 | } else { |
| 466 | // Assume this identifier is a label. |
| 467 | const MCExpr *Val; |
| 468 | SMLoc End; |
| 469 | if (Parser.parsePrimaryExpr(Val, End)) |
| 470 | return error("Cannot parse symbol: ", Lexer.getTok()); |
| 471 | Operands.push_back(make_unique<WebAssemblyOperand>( |
| 472 | WebAssemblyOperand::Symbol, Id.getLoc(), Id.getEndLoc(), |
| 473 | WebAssemblyOperand::SymOp{Val})); |
| 474 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 475 | break; |
| 476 | } |
| 477 | case AsmToken::Minus: |
| 478 | Parser.Lex(); |
| 479 | if (Lexer.isNot(AsmToken::Integer)) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 480 | return error("Expected integer instead got: ", Lexer.getTok()); |
| 481 | if (parseOperandStartingWithInteger(true, Operands, BaseName)) |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 482 | return true; |
| 483 | break; |
| 484 | case AsmToken::Integer: |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 485 | if (parseOperandStartingWithInteger(false, Operands, BaseName)) |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 486 | return true; |
| 487 | break; |
| 488 | case AsmToken::Real: { |
| 489 | double Val; |
| 490 | if (Tok.getString().getAsDouble(Val, false)) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 491 | return error("Cannot parse real: ", Tok); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 492 | Operands.push_back(make_unique<WebAssemblyOperand>( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 493 | WebAssemblyOperand::Float, Tok.getLoc(), Tok.getEndLoc(), |
| 494 | WebAssemblyOperand::FltOp{Val})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 495 | Parser.Lex(); |
| 496 | break; |
| 497 | } |
Wouter van Oortmerssen | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 498 | case AsmToken::LCurly: { |
| 499 | Parser.Lex(); |
| 500 | auto Op = make_unique<WebAssemblyOperand>( |
| 501 | WebAssemblyOperand::BrList, Tok.getLoc(), Tok.getEndLoc()); |
| 502 | if (!Lexer.is(AsmToken::RCurly)) |
| 503 | for (;;) { |
| 504 | Op->BrL.List.push_back(Lexer.getTok().getIntVal()); |
| 505 | expect(AsmToken::Integer, "integer"); |
| 506 | if (!isNext(AsmToken::Comma)) |
| 507 | break; |
| 508 | } |
| 509 | expect(AsmToken::RCurly, "}"); |
| 510 | Operands.push_back(std::move(Op)); |
| 511 | break; |
| 512 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 513 | default: |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 514 | return error("Unexpected token in operand: ", Tok); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 515 | } |
| 516 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 517 | if (expect(AsmToken::Comma, ",")) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 518 | return true; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 521 | if (ExpectBlockType && Operands.size() == 1) { |
| 522 | // Support blocks with no operands as default to void. |
| 523 | addBlockTypeOperand(Operands, NameLoc, WebAssembly::ExprType::Void); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 524 | } |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 525 | Parser.Lex(); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 526 | return false; |
| 527 | } |
| 528 | |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 529 | void onLabelParsed(MCSymbol *Symbol) override { |
| 530 | LastLabel = Symbol; |
| 531 | CurrentState = Label; |
| 532 | } |
| 533 | |
Heejin Ahn | be5e587 | 2018-12-11 01:11:04 +0000 | [diff] [blame] | 534 | bool parseSignature(wasm::WasmSignature *Signature) { |
| 535 | if (expect(AsmToken::LParen, "(")) |
| 536 | return true; |
| 537 | if (parseRegTypeList(Signature->Params)) |
| 538 | return true; |
| 539 | if (expect(AsmToken::RParen, ")")) |
| 540 | return true; |
| 541 | if (expect(AsmToken::MinusGreater, "->")) |
| 542 | return true; |
| 543 | if (expect(AsmToken::LParen, "(")) |
| 544 | return true; |
| 545 | if (parseRegTypeList(Signature->Returns)) |
| 546 | return true; |
| 547 | if (expect(AsmToken::RParen, ")")) |
| 548 | return true; |
| 549 | return false; |
| 550 | } |
| 551 | |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 552 | // This function processes wasm-specific directives streamed to |
| 553 | // WebAssemblyTargetStreamer, all others go to the generic parser |
| 554 | // (see WasmAsmParser). |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 555 | bool ParseDirective(AsmToken DirectiveID) override { |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 556 | // This function has a really weird return value behavior that is different |
| 557 | // from all the other parsing functions: |
| 558 | // - return true && no tokens consumed -> don't know this directive / let |
| 559 | // the generic parser handle it. |
| 560 | // - return true && tokens consumed -> a parsing error occurred. |
| 561 | // - return false -> processed this directive successfully. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 562 | assert(DirectiveID.getKind() == AsmToken::Identifier); |
| 563 | auto &Out = getStreamer(); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 564 | auto &TOut = |
| 565 | reinterpret_cast<WebAssemblyTargetStreamer &>(*Out.getTargetStreamer()); |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame] | 566 | auto &Ctx = Out.getContext(); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 567 | |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 568 | // TODO: any time we return an error, at least one token must have been |
| 569 | // consumed, otherwise this will not signal an error to the caller. |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 570 | if (DirectiveID.getString() == ".globaltype") { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 571 | auto SymName = expectIdent(); |
| 572 | if (SymName.empty()) |
| 573 | return true; |
| 574 | if (expect(AsmToken::Comma, ",")) |
| 575 | return true; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 576 | auto TypeTok = Lexer.getTok(); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 577 | auto TypeName = expectIdent(); |
| 578 | if (TypeName.empty()) |
| 579 | return true; |
| 580 | auto Type = parseType(TypeName); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 581 | if (!Type) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 582 | return error("Unknown type in .globaltype directive: ", TypeTok); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 583 | // Now set this symbol with the correct type. |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame] | 584 | auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName)); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 585 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 586 | WasmSym->setGlobalType( |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 587 | wasm::WasmGlobalType{uint8_t(Type.getValue()), true}); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 588 | // And emit the directive again. |
| 589 | TOut.emitGlobalType(WasmSym); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 590 | return expect(AsmToken::EndOfStatement, "EOL"); |
| 591 | } |
| 592 | |
| 593 | if (DirectiveID.getString() == ".functype") { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 594 | // This code has to send things to the streamer similar to |
| 595 | // WebAssemblyAsmPrinter::EmitFunctionBodyStart. |
| 596 | // TODO: would be good to factor this into a common function, but the |
| 597 | // assembler and backend really don't share any common code, and this code |
| 598 | // parses the locals seperately. |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 599 | auto SymName = expectIdent(); |
| 600 | if (SymName.empty()) |
| 601 | return true; |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame] | 602 | auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName)); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 603 | if (CurrentState == Label && WasmSym == LastLabel) { |
| 604 | // This .functype indicates a start of a function. |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 605 | if (ensureEmptyNestingStack()) |
| 606 | return true; |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 607 | CurrentState = FunctionStart; |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 608 | push(Function); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 609 | } |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 610 | auto Signature = make_unique<wasm::WasmSignature>(); |
Heejin Ahn | be5e587 | 2018-12-11 01:11:04 +0000 | [diff] [blame] | 611 | if (parseSignature(Signature.get())) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 612 | return true; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 613 | WasmSym->setSignature(Signature.get()); |
| 614 | addSignature(std::move(Signature)); |
| 615 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
| 616 | TOut.emitFunctionType(WasmSym); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 617 | // TODO: backend also calls TOut.emitIndIdx, but that is not implemented. |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 618 | return expect(AsmToken::EndOfStatement, "EOL"); |
| 619 | } |
| 620 | |
Heejin Ahn | be5e587 | 2018-12-11 01:11:04 +0000 | [diff] [blame] | 621 | if (DirectiveID.getString() == ".eventtype") { |
| 622 | auto SymName = expectIdent(); |
| 623 | if (SymName.empty()) |
| 624 | return true; |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame] | 625 | auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName)); |
Heejin Ahn | be5e587 | 2018-12-11 01:11:04 +0000 | [diff] [blame] | 626 | auto Signature = make_unique<wasm::WasmSignature>(); |
| 627 | if (parseRegTypeList(Signature->Params)) |
| 628 | return true; |
| 629 | WasmSym->setSignature(Signature.get()); |
| 630 | addSignature(std::move(Signature)); |
| 631 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT); |
| 632 | TOut.emitEventType(WasmSym); |
| 633 | // TODO: backend also calls TOut.emitIndIdx, but that is not implemented. |
| 634 | return expect(AsmToken::EndOfStatement, "EOL"); |
| 635 | } |
| 636 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 637 | if (DirectiveID.getString() == ".local") { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 638 | if (CurrentState != FunctionStart) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 639 | return error(".local directive should follow the start of a function", |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 640 | Lexer.getTok()); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 641 | SmallVector<wasm::ValType, 4> Locals; |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 642 | if (parseRegTypeList(Locals)) |
| 643 | return true; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 644 | TOut.emitLocal(Locals); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 645 | CurrentState = FunctionLocals; |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 646 | return expect(AsmToken::EndOfStatement, "EOL"); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 647 | } |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 648 | |
| 649 | return true; // We didn't process this directive. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 652 | bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned & /*Opcode*/, |
| 653 | OperandVector &Operands, MCStreamer &Out, |
| 654 | uint64_t &ErrorInfo, |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 655 | bool MatchingInlineAsm) override { |
| 656 | MCInst Inst; |
| 657 | unsigned MatchResult = |
| 658 | MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm); |
| 659 | switch (MatchResult) { |
| 660 | case Match_Success: { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 661 | if (CurrentState == FunctionStart) { |
| 662 | // This is the first instruction in a function, but we haven't seen |
| 663 | // a .local directive yet. The streamer requires locals to be encoded |
| 664 | // as a prelude to the instructions, so emit an empty list of locals |
| 665 | // here. |
| 666 | auto &TOut = reinterpret_cast<WebAssemblyTargetStreamer &>( |
| 667 | *Out.getTargetStreamer()); |
| 668 | TOut.emitLocal(SmallVector<wasm::ValType, 0>()); |
| 669 | } |
| 670 | CurrentState = Instructions; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 671 | Out.EmitInstruction(Inst, getSTI()); |
| 672 | return false; |
| 673 | } |
| 674 | case Match_MissingFeature: |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 675 | return Parser.Error( |
| 676 | IDLoc, "instruction requires a WASM feature not currently enabled"); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 677 | case Match_MnemonicFail: |
| 678 | return Parser.Error(IDLoc, "invalid instruction"); |
| 679 | case Match_NearMisses: |
| 680 | return Parser.Error(IDLoc, "ambiguous instruction"); |
| 681 | case Match_InvalidTiedOperand: |
| 682 | case Match_InvalidOperand: { |
| 683 | SMLoc ErrorLoc = IDLoc; |
| 684 | if (ErrorInfo != ~0ULL) { |
| 685 | if (ErrorInfo >= Operands.size()) |
| 686 | return Parser.Error(IDLoc, "too few operands for instruction"); |
| 687 | ErrorLoc = Operands[ErrorInfo]->getStartLoc(); |
| 688 | if (ErrorLoc == SMLoc()) |
| 689 | ErrorLoc = IDLoc; |
| 690 | } |
| 691 | return Parser.Error(ErrorLoc, "invalid operand for instruction"); |
| 692 | } |
| 693 | } |
| 694 | llvm_unreachable("Implement any new match types added!"); |
| 695 | } |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 696 | |
| 697 | void onEndOfFile() override { ensureEmptyNestingStack(); } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 698 | }; |
| 699 | } // end anonymous namespace |
| 700 | |
| 701 | // Force static initialization. |
| 702 | extern "C" void LLVMInitializeWebAssemblyAsmParser() { |
| 703 | RegisterMCAsmParser<WebAssemblyAsmParser> X(getTheWebAssemblyTarget32()); |
| 704 | RegisterMCAsmParser<WebAssemblyAsmParser> Y(getTheWebAssemblyTarget64()); |
| 705 | } |
| 706 | |
| 707 | #define GET_REGISTER_MATCHER |
| 708 | #define GET_MATCHER_IMPLEMENTATION |
| 709 | #include "WebAssemblyGenAsmMatcher.inc" |