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); |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 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") { |
Wouter van Oortmerssen | 0b3cf24 | 2019-02-04 18:03:11 +0000 | [diff] [blame] | 449 | CurrentState = EndFunction; |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 450 | if (pop(BaseName, Function) || ensureEmptyNestingStack()) |
| 451 | return true; |
| 452 | } |
| 453 | |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 454 | while (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 455 | auto &Tok = Lexer.getTok(); |
| 456 | switch (Tok.getKind()) { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 457 | case AsmToken::Identifier: { |
| 458 | auto &Id = Lexer.getTok(); |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 459 | if (ExpectBlockType) { |
| 460 | // Assume this identifier is a block_type. |
| 461 | auto BT = parseBlockType(Id.getString()); |
| 462 | if (BT == WebAssembly::ExprType::Invalid) |
| 463 | return error("Unknown block type: ", Id); |
| 464 | addBlockTypeOperand(Operands, NameLoc, BT); |
| 465 | Parser.Lex(); |
| 466 | } else { |
| 467 | // Assume this identifier is a label. |
| 468 | const MCExpr *Val; |
| 469 | SMLoc End; |
| 470 | if (Parser.parsePrimaryExpr(Val, End)) |
| 471 | return error("Cannot parse symbol: ", Lexer.getTok()); |
| 472 | Operands.push_back(make_unique<WebAssemblyOperand>( |
| 473 | WebAssemblyOperand::Symbol, Id.getLoc(), Id.getEndLoc(), |
| 474 | WebAssemblyOperand::SymOp{Val})); |
| 475 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | case AsmToken::Minus: |
| 479 | Parser.Lex(); |
| 480 | if (Lexer.isNot(AsmToken::Integer)) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 481 | return error("Expected integer instead got: ", Lexer.getTok()); |
| 482 | if (parseOperandStartingWithInteger(true, Operands, BaseName)) |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 483 | return true; |
| 484 | break; |
| 485 | case AsmToken::Integer: |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 486 | if (parseOperandStartingWithInteger(false, Operands, BaseName)) |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 487 | return true; |
| 488 | break; |
| 489 | case AsmToken::Real: { |
| 490 | double Val; |
| 491 | if (Tok.getString().getAsDouble(Val, false)) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 492 | return error("Cannot parse real: ", Tok); |
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::Float, Tok.getLoc(), Tok.getEndLoc(), |
| 495 | WebAssemblyOperand::FltOp{Val})); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 496 | Parser.Lex(); |
| 497 | break; |
| 498 | } |
Wouter van Oortmerssen | d3c544a | 2018-12-17 22:04:44 +0000 | [diff] [blame] | 499 | case AsmToken::LCurly: { |
| 500 | Parser.Lex(); |
| 501 | auto Op = make_unique<WebAssemblyOperand>( |
| 502 | WebAssemblyOperand::BrList, Tok.getLoc(), Tok.getEndLoc()); |
| 503 | if (!Lexer.is(AsmToken::RCurly)) |
| 504 | for (;;) { |
| 505 | Op->BrL.List.push_back(Lexer.getTok().getIntVal()); |
| 506 | expect(AsmToken::Integer, "integer"); |
| 507 | if (!isNext(AsmToken::Comma)) |
| 508 | break; |
| 509 | } |
| 510 | expect(AsmToken::RCurly, "}"); |
| 511 | Operands.push_back(std::move(Op)); |
| 512 | break; |
| 513 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 514 | default: |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 515 | return error("Unexpected token in operand: ", Tok); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 516 | } |
| 517 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 518 | if (expect(AsmToken::Comma, ",")) |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 519 | return true; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 522 | if (ExpectBlockType && Operands.size() == 1) { |
| 523 | // Support blocks with no operands as default to void. |
| 524 | addBlockTypeOperand(Operands, NameLoc, WebAssembly::ExprType::Void); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 525 | } |
Wouter van Oortmerssen | ad72f68 | 2019-01-02 23:23:51 +0000 | [diff] [blame] | 526 | Parser.Lex(); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 527 | return false; |
| 528 | } |
| 529 | |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 530 | void onLabelParsed(MCSymbol *Symbol) override { |
| 531 | LastLabel = Symbol; |
| 532 | CurrentState = Label; |
| 533 | } |
| 534 | |
Heejin Ahn | be5e587 | 2018-12-11 01:11:04 +0000 | [diff] [blame] | 535 | bool parseSignature(wasm::WasmSignature *Signature) { |
| 536 | if (expect(AsmToken::LParen, "(")) |
| 537 | return true; |
| 538 | if (parseRegTypeList(Signature->Params)) |
| 539 | return true; |
| 540 | if (expect(AsmToken::RParen, ")")) |
| 541 | return true; |
| 542 | if (expect(AsmToken::MinusGreater, "->")) |
| 543 | return true; |
| 544 | if (expect(AsmToken::LParen, "(")) |
| 545 | return true; |
| 546 | if (parseRegTypeList(Signature->Returns)) |
| 547 | return true; |
| 548 | if (expect(AsmToken::RParen, ")")) |
| 549 | return true; |
| 550 | return false; |
| 551 | } |
| 552 | |
Wouter van Oortmerssen | f3feb6a | 2019-03-04 17:18:04 +0000 | [diff] [blame] | 553 | bool CheckDataSection() { |
| 554 | if (CurrentState != DataSection) { |
| 555 | auto WS = cast<MCSectionWasm>(getStreamer().getCurrentSection().first); |
| 556 | if (WS && WS->getKind().isText()) |
| 557 | return error("data directive must occur in a data segment: ", |
| 558 | Lexer.getTok()); |
| 559 | } |
| 560 | CurrentState = DataSection; |
| 561 | return false; |
| 562 | } |
| 563 | |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 564 | // This function processes wasm-specific directives streamed to |
| 565 | // WebAssemblyTargetStreamer, all others go to the generic parser |
| 566 | // (see WasmAsmParser). |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 567 | bool ParseDirective(AsmToken DirectiveID) override { |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 568 | // This function has a really weird return value behavior that is different |
| 569 | // from all the other parsing functions: |
| 570 | // - return true && no tokens consumed -> don't know this directive / let |
| 571 | // the generic parser handle it. |
| 572 | // - return true && tokens consumed -> a parsing error occurred. |
| 573 | // - return false -> processed this directive successfully. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 574 | assert(DirectiveID.getKind() == AsmToken::Identifier); |
| 575 | auto &Out = getStreamer(); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 576 | auto &TOut = |
| 577 | reinterpret_cast<WebAssemblyTargetStreamer &>(*Out.getTargetStreamer()); |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame] | 578 | auto &Ctx = Out.getContext(); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 579 | |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 580 | // TODO: any time we return an error, at least one token must have been |
| 581 | // consumed, otherwise this will not signal an error to the caller. |
Wouter van Oortmerssen | cc75e77 | 2018-11-12 20:15:01 +0000 | [diff] [blame] | 582 | if (DirectiveID.getString() == ".globaltype") { |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 583 | auto SymName = expectIdent(); |
| 584 | if (SymName.empty()) |
| 585 | return true; |
| 586 | if (expect(AsmToken::Comma, ",")) |
| 587 | return true; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 588 | auto TypeTok = Lexer.getTok(); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 589 | auto TypeName = expectIdent(); |
| 590 | if (TypeName.empty()) |
| 591 | return true; |
| 592 | auto Type = parseType(TypeName); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 593 | if (!Type) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 594 | return error("Unknown type in .globaltype directive: ", TypeTok); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 595 | // Now set this symbol with the correct type. |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame] | 596 | auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName)); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 597 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 598 | WasmSym->setGlobalType( |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 599 | wasm::WasmGlobalType{uint8_t(Type.getValue()), true}); |
Wouter van Oortmerssen | de28b5d | 2018-11-02 22:04:33 +0000 | [diff] [blame] | 600 | // And emit the directive again. |
| 601 | TOut.emitGlobalType(WasmSym); |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 602 | return expect(AsmToken::EndOfStatement, "EOL"); |
| 603 | } |
| 604 | |
| 605 | if (DirectiveID.getString() == ".functype") { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 606 | // This code has to send things to the streamer similar to |
| 607 | // WebAssemblyAsmPrinter::EmitFunctionBodyStart. |
| 608 | // TODO: would be good to factor this into a common function, but the |
| 609 | // assembler and backend really don't share any common code, and this code |
| 610 | // parses the locals seperately. |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 611 | auto SymName = expectIdent(); |
| 612 | if (SymName.empty()) |
| 613 | return true; |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame] | 614 | auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName)); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 615 | if (CurrentState == Label && WasmSym == LastLabel) { |
| 616 | // This .functype indicates a start of a function. |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 617 | if (ensureEmptyNestingStack()) |
| 618 | return true; |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 619 | CurrentState = FunctionStart; |
Wouter van Oortmerssen | 0b3cf24 | 2019-02-04 18:03:11 +0000 | [diff] [blame] | 620 | LastFunctionLabel = LastLabel; |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 621 | push(Function); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 622 | } |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 623 | auto Signature = make_unique<wasm::WasmSignature>(); |
Heejin Ahn | be5e587 | 2018-12-11 01:11:04 +0000 | [diff] [blame] | 624 | if (parseSignature(Signature.get())) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 625 | return true; |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 626 | WasmSym->setSignature(Signature.get()); |
| 627 | addSignature(std::move(Signature)); |
| 628 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
| 629 | TOut.emitFunctionType(WasmSym); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 630 | // TODO: backend also calls TOut.emitIndIdx, but that is not implemented. |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 631 | return expect(AsmToken::EndOfStatement, "EOL"); |
| 632 | } |
| 633 | |
Heejin Ahn | be5e587 | 2018-12-11 01:11:04 +0000 | [diff] [blame] | 634 | if (DirectiveID.getString() == ".eventtype") { |
| 635 | auto SymName = expectIdent(); |
| 636 | if (SymName.empty()) |
| 637 | return true; |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame] | 638 | auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName)); |
Heejin Ahn | be5e587 | 2018-12-11 01:11:04 +0000 | [diff] [blame] | 639 | auto Signature = make_unique<wasm::WasmSignature>(); |
| 640 | if (parseRegTypeList(Signature->Params)) |
| 641 | return true; |
| 642 | WasmSym->setSignature(Signature.get()); |
| 643 | addSignature(std::move(Signature)); |
| 644 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT); |
| 645 | TOut.emitEventType(WasmSym); |
| 646 | // TODO: backend also calls TOut.emitIndIdx, but that is not implemented. |
| 647 | return expect(AsmToken::EndOfStatement, "EOL"); |
| 648 | } |
| 649 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 650 | if (DirectiveID.getString() == ".local") { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 651 | if (CurrentState != FunctionStart) |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 652 | return error(".local directive should follow the start of a function", |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 653 | Lexer.getTok()); |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 654 | SmallVector<wasm::ValType, 4> Locals; |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 655 | if (parseRegTypeList(Locals)) |
| 656 | return true; |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 657 | TOut.emitLocal(Locals); |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 658 | CurrentState = FunctionLocals; |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 659 | return expect(AsmToken::EndOfStatement, "EOL"); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 660 | } |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 661 | |
Wouter van Oortmerssen | f3feb6a | 2019-03-04 17:18:04 +0000 | [diff] [blame] | 662 | if (DirectiveID.getString() == ".int8") { |
| 663 | if (CheckDataSection()) return true; |
| 664 | int64_t V; |
| 665 | if (Parser.parseAbsoluteExpression(V)) |
| 666 | return error("Cannot parse int8 constant: ", Lexer.getTok()); |
| 667 | // TODO: error if value doesn't fit? |
| 668 | Out.EmitIntValue(static_cast<uint64_t>(V), 1); |
| 669 | return expect(AsmToken::EndOfStatement, "EOL"); |
| 670 | } |
| 671 | |
| 672 | if (DirectiveID.getString() == ".asciz") { |
| 673 | if (CheckDataSection()) return true; |
| 674 | std::string S; |
| 675 | if (Parser.parseEscapedString(S)) |
| 676 | return error("Cannot parse string constant: ", Lexer.getTok()); |
| 677 | Out.EmitBytes(StringRef(S.c_str(), S.length() + 1)); |
| 678 | return expect(AsmToken::EndOfStatement, "EOL"); |
| 679 | } |
| 680 | |
Heejin Ahn | 7ce5edf | 2018-12-07 21:35:37 +0000 | [diff] [blame] | 681 | return true; // We didn't process this directive. |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 684 | bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned & /*Opcode*/, |
| 685 | OperandVector &Operands, MCStreamer &Out, |
| 686 | uint64_t &ErrorInfo, |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 687 | bool MatchingInlineAsm) override { |
| 688 | MCInst Inst; |
| 689 | unsigned MatchResult = |
| 690 | MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm); |
| 691 | switch (MatchResult) { |
| 692 | case Match_Success: { |
Wouter van Oortmerssen | c7b89f0 | 2018-12-03 20:30:28 +0000 | [diff] [blame] | 693 | if (CurrentState == FunctionStart) { |
| 694 | // This is the first instruction in a function, but we haven't seen |
| 695 | // a .local directive yet. The streamer requires locals to be encoded |
| 696 | // as a prelude to the instructions, so emit an empty list of locals |
| 697 | // here. |
| 698 | auto &TOut = reinterpret_cast<WebAssemblyTargetStreamer &>( |
| 699 | *Out.getTargetStreamer()); |
| 700 | TOut.emitLocal(SmallVector<wasm::ValType, 0>()); |
| 701 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 702 | Out.EmitInstruction(Inst, getSTI()); |
Wouter van Oortmerssen | 0b3cf24 | 2019-02-04 18:03:11 +0000 | [diff] [blame] | 703 | if (CurrentState == EndFunction) { |
| 704 | onEndOfFunction(); |
| 705 | } else { |
| 706 | CurrentState = Instructions; |
| 707 | } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 708 | return false; |
| 709 | } |
| 710 | case Match_MissingFeature: |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 711 | return Parser.Error( |
| 712 | IDLoc, "instruction requires a WASM feature not currently enabled"); |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 713 | case Match_MnemonicFail: |
| 714 | return Parser.Error(IDLoc, "invalid instruction"); |
| 715 | case Match_NearMisses: |
| 716 | return Parser.Error(IDLoc, "ambiguous instruction"); |
| 717 | case Match_InvalidTiedOperand: |
| 718 | case Match_InvalidOperand: { |
| 719 | SMLoc ErrorLoc = IDLoc; |
| 720 | if (ErrorInfo != ~0ULL) { |
| 721 | if (ErrorInfo >= Operands.size()) |
| 722 | return Parser.Error(IDLoc, "too few operands for instruction"); |
| 723 | ErrorLoc = Operands[ErrorInfo]->getStartLoc(); |
| 724 | if (ErrorLoc == SMLoc()) |
| 725 | ErrorLoc = IDLoc; |
| 726 | } |
| 727 | return Parser.Error(ErrorLoc, "invalid operand for instruction"); |
| 728 | } |
| 729 | } |
| 730 | llvm_unreachable("Implement any new match types added!"); |
| 731 | } |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 732 | |
Wouter van Oortmerssen | 0b3cf24 | 2019-02-04 18:03:11 +0000 | [diff] [blame] | 733 | void doBeforeLabelEmit(MCSymbol *Symbol) override { |
| 734 | // Start a new section for the next function automatically, since our |
| 735 | // object writer expects each function to have its own section. This way |
| 736 | // The user can't forget this "convention". |
| 737 | auto SymName = Symbol->getName(); |
| 738 | if (SymName.startswith(".L")) |
| 739 | return; // Local Symbol. |
| 740 | auto SecName = ".text." + SymName; |
| 741 | auto WS = getContext().getWasmSection(SecName, SectionKind::getText()); |
| 742 | getStreamer().SwitchSection(WS); |
| 743 | } |
| 744 | |
| 745 | void onEndOfFunction() { |
| 746 | // Automatically output a .size directive, so it becomes optional for the |
| 747 | // user. |
Wouter van Oortmerssen | f3feb6a | 2019-03-04 17:18:04 +0000 | [diff] [blame] | 748 | if (!LastFunctionLabel) return; |
Wouter van Oortmerssen | 0b3cf24 | 2019-02-04 18:03:11 +0000 | [diff] [blame] | 749 | auto TempSym = getContext().createLinkerPrivateTempSymbol(); |
| 750 | getStreamer().EmitLabel(TempSym); |
Wouter van Oortmerssen | f3feb6a | 2019-03-04 17:18:04 +0000 | [diff] [blame] | 751 | auto Start = MCSymbolRefExpr::create(LastFunctionLabel, getContext()); |
Wouter van Oortmerssen | 0b3cf24 | 2019-02-04 18:03:11 +0000 | [diff] [blame] | 752 | auto End = MCSymbolRefExpr::create(TempSym, getContext()); |
| 753 | auto Expr = |
| 754 | MCBinaryExpr::create(MCBinaryExpr::Sub, End, Start, getContext()); |
| 755 | getStreamer().emitELFSize(LastFunctionLabel, Expr); |
| 756 | } |
| 757 | |
Wouter van Oortmerssen | 29c6ce5 | 2018-12-26 22:46:18 +0000 | [diff] [blame] | 758 | void onEndOfFile() override { ensureEmptyNestingStack(); } |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 759 | }; |
| 760 | } // end anonymous namespace |
| 761 | |
| 762 | // Force static initialization. |
Tom Stellard | 4b0b261 | 2019-06-11 03:21:13 +0000 | [diff] [blame] | 763 | extern "C" void LLVMInitializeWebAssemblyAsmParser() { |
Derek Schuff | e482597 | 2018-03-20 20:06:35 +0000 | [diff] [blame] | 764 | RegisterMCAsmParser<WebAssemblyAsmParser> X(getTheWebAssemblyTarget32()); |
| 765 | RegisterMCAsmParser<WebAssemblyAsmParser> Y(getTheWebAssemblyTarget64()); |
| 766 | } |
| 767 | |
| 768 | #define GET_REGISTER_MATCHER |
| 769 | #define GET_MATCHER_IMPLEMENTATION |
| 770 | #include "WebAssemblyGenAsmMatcher.inc" |