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