Skip to content

Commit

Permalink
[WebAssembly] tablegen: distinguish float/int immediate operands.
Browse files Browse the repository at this point in the history
Summary:
Before, they were one category of operands which could cause
crashes in non-sensical combinations, e.g. "f32.const symbol".
Now these are forced to be an error.

Reviewers: dschuff

Subscribers: sbc100, jgravelle-google, aheejin, sunfish, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D64039

llvm-svn: 365351
  • Loading branch information
aardappel committed Jul 8, 2019
1 parent 224d8cd commit 81db9f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
17 changes: 11 additions & 6 deletions llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ struct WebAssemblyOperand : public MCParsedAsmOperand {
}

bool isToken() const override { return Kind == Token; }
bool isImm() const override {
return Kind == Integer || Kind == Float || Kind == Symbol;
}
bool isImm() const override { return Kind == Integer || Kind == Symbol; }
bool isFPImm() const { return Kind == Float; }
bool isMem() const override { return false; }
bool isReg() const override { return false; }
bool isBrList() const { return Kind == BrList; }
Expand All @@ -118,12 +117,18 @@ struct WebAssemblyOperand : public MCParsedAsmOperand {
assert(N == 1 && "Invalid number of operands!");
if (Kind == Integer)
Inst.addOperand(MCOperand::createImm(Int.Val));
else if (Kind == Float)
Inst.addOperand(MCOperand::createFPImm(Flt.Val));
else if (Kind == Symbol)
Inst.addOperand(MCOperand::createExpr(Sym.Exp));
else
llvm_unreachable("Should be immediate or symbol!");
llvm_unreachable("Should be integer immediate or symbol!");
}

void addFPImmOperands(MCInst &Inst, unsigned N) const {
assert(N == 1 && "Invalid number of operands!");
if (Kind == Float)
Inst.addOperand(MCOperand::createFPImm(Flt.Val));
else
llvm_unreachable("Should be float immediate!");
}

void addBrListOperands(MCInst &Inst, unsigned N) const {
Expand Down
15 changes: 13 additions & 2 deletions llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ def WebAssemblythrow : SDNode<"WebAssemblyISD::THROW", SDT_WebAssemblyThrow,
// WebAssembly-specific Operands.
//===----------------------------------------------------------------------===//

// Default Operand has AsmOperandClass "Imm" which is for integers (and
// symbols), so specialize one for floats:
def FPImmAsmOperand : AsmOperandClass {
let Name = "FPImm";
let PredicateMethod = "isFPImm";
}

class FPOperand<ValueType ty> : Operand<ty> {
AsmOperandClass ParserMatchClass = FPImmAsmOperand;
}

let OperandNamespace = "WebAssembly" in {

let OperandType = "OPERAND_BASIC_BLOCK" in
Expand All @@ -136,10 +147,10 @@ let OperandType = "OPERAND_I64IMM" in
def i64imm_op : Operand<i64>;

let OperandType = "OPERAND_F32IMM" in
def f32imm_op : Operand<f32>;
def f32imm_op : FPOperand<f32>;

let OperandType = "OPERAND_F64IMM" in
def f64imm_op : Operand<f64>;
def f64imm_op : FPOperand<f64>;

let OperandType = "OPERAND_VEC_I8IMM" in
def vec_i8imm_op : Operand<i32>;
Expand Down
4 changes: 4 additions & 0 deletions llvm/test/MC/WebAssembly/basic-assembly-errors.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# RUN: not llvm-mc -triple=wasm32-unknown-unknown -mattr=+simd128,+nontrapping-fptoint,+exception-handling < %s 2>&1 | FileCheck %s

# CHECK: invalid operand for instruction
# (must be 0.0 or similar)
f32.const 0

# CHECK: End of block construct with no start: end_try
end_try
test0:
Expand Down

0 comments on commit 81db9f5

Please sign in to comment.