From 59858cf7926702596cf708daf21aeaf5b6feedf8 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 27 Jul 2009 16:11:46 +0000 Subject: Change the assembly syntax for nsw, nuw, and exact, putting them after their associated opcodes rather than before. This makes them a little easier to read. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77194 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AsmParser/LLParser.cpp | 162 ++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 91 deletions(-) (limited to 'lib/AsmParser') diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index 481a6169ec..c6f1b257bd 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -1954,9 +1954,27 @@ bool LLParser::ParseValID(ValID &ID) { case lltok::kw_urem: case lltok::kw_srem: case lltok::kw_frem: { + bool NUW = false; + bool NSW = false; + bool Exact = false; unsigned Opc = Lex.getUIntVal(); Constant *Val0, *Val1; Lex.Lex(); + LocTy ModifierLoc = Lex.getLoc(); + if (Opc == Instruction::Add || + Opc == Instruction::Sub || + Opc == Instruction::Mul) { + if (EatIfPresent(lltok::kw_nuw)) + NUW = true; + if (EatIfPresent(lltok::kw_nsw)) { + NSW = true; + if (EatIfPresent(lltok::kw_nuw)) + NUW = true; + } + } else if (Opc == Instruction::SDiv) { + if (EatIfPresent(lltok::kw_exact)) + Exact = true; + } if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") || ParseGlobalTypeAndValue(Val0) || ParseToken(lltok::comma, "expected comma in binary constantexpr") || @@ -1965,10 +1983,25 @@ bool LLParser::ParseValID(ValID &ID) { return true; if (Val0->getType() != Val1->getType()) return Error(ID.Loc, "operands of constexpr must have same type"); + if (!Val0->getType()->isIntOrIntVector()) { + if (NUW) + return Error(ModifierLoc, "nuw only applies to integer operations"); + if (NSW) + return Error(ModifierLoc, "nsw only applies to integer operations"); + } + // API compatibility: Accept either integer or floating-point types with + // add, sub, and mul. if (!Val0->getType()->isIntOrIntVector() && !Val0->getType()->isFPOrFPVector()) return Error(ID.Loc,"constexpr requires integer, fp, or vector operands"); - ID.ConstantVal = Context.getConstantExpr(Opc, Val0, Val1); + Constant *C = Context.getConstantExpr(Opc, Val0, Val1); + if (NUW) + cast(C)->setHasNoUnsignedOverflow(true); + if (NSW) + cast(C)->setHasNoSignedOverflow(true); + if (Exact) + cast(C)->setIsExact(true); + ID.ConstantVal = C; ID.Kind = ValID::t_Constant; return false; } @@ -2055,49 +2088,6 @@ bool LLParser::ParseValID(ValID &ID) { ID.Kind = ValID::t_Constant; return false; } - case lltok::kw_nuw: { - Lex.Lex(); - bool AlsoSigned = EatIfPresent(lltok::kw_nsw); - if (Lex.getKind() != lltok::kw_add && - Lex.getKind() != lltok::kw_sub && - Lex.getKind() != lltok::kw_mul) - return TokError("expected 'add', 'sub', or 'mul'"); - bool Result = LLParser::ParseValID(ID); - if (!Result) { - cast(ID.ConstantVal) - ->setHasNoUnsignedOverflow(true); - if (AlsoSigned) - cast(ID.ConstantVal) - ->setHasNoSignedOverflow(true); - } - return Result; - } - case lltok::kw_nsw: { - Lex.Lex(); - bool AlsoUnsigned = EatIfPresent(lltok::kw_nuw); - if (Lex.getKind() != lltok::kw_add && - Lex.getKind() != lltok::kw_sub && - Lex.getKind() != lltok::kw_mul) - return TokError("expected 'add', 'sub', or 'mul'"); - bool Result = LLParser::ParseValID(ID); - if (!Result) { - cast(ID.ConstantVal) - ->setHasNoSignedOverflow(true); - if (AlsoUnsigned) - cast(ID.ConstantVal) - ->setHasNoUnsignedOverflow(true); - } - return Result; - } - case lltok::kw_exact: { - Lex.Lex(); - if (Lex.getKind() != lltok::kw_sdiv) - return TokError("expected 'sdiv'"); - bool Result = LLParser::ParseValID(ID); - if (!Result) - cast(ID.ConstantVal)->setIsExact(true); - return Result; - } } Lex.Lex(); @@ -2563,15 +2553,49 @@ bool LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB, // Binary Operators. case lltok::kw_add: case lltok::kw_sub: - case lltok::kw_mul: + case lltok::kw_mul: { + bool NUW = false; + bool NSW = false; + LocTy ModifierLoc = Lex.getLoc(); + if (EatIfPresent(lltok::kw_nuw)) + NUW = true; + if (EatIfPresent(lltok::kw_nsw)) { + NSW = true; + if (EatIfPresent(lltok::kw_nuw)) + NUW = true; + } // API compatibility: Accept either integer or floating-point types. - return ParseArithmetic(Inst, PFS, KeywordVal, 0); + bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 0); + if (!Result) { + if (!Inst->getType()->isIntOrIntVector()) { + if (NUW) + return Error(ModifierLoc, "nuw only applies to integer operations"); + if (NSW) + return Error(ModifierLoc, "nsw only applies to integer operations"); + } + if (NUW) + cast(Inst)->setHasNoUnsignedOverflow(true); + if (NSW) + cast(Inst)->setHasNoSignedOverflow(true); + } + return Result; + } case lltok::kw_fadd: case lltok::kw_fsub: case lltok::kw_fmul: return ParseArithmetic(Inst, PFS, KeywordVal, 2); + case lltok::kw_sdiv: { + bool Exact = false; + if (EatIfPresent(lltok::kw_exact)) + Exact = true; + bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 1); + if (!Result) + if (Exact) + cast(Inst)->setIsExact(true); + return Result; + } + case lltok::kw_udiv: - case lltok::kw_sdiv: case lltok::kw_urem: case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1); case lltok::kw_fdiv: @@ -2619,50 +2643,6 @@ bool LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB, return ParseStore(Inst, PFS, true); else return TokError("expected 'load' or 'store'"); - case lltok::kw_nuw: { - bool AlsoSigned = EatIfPresent(lltok::kw_nsw); - if (Lex.getKind() == lltok::kw_add || - Lex.getKind() == lltok::kw_sub || - Lex.getKind() == lltok::kw_mul) { - Lex.Lex(); - KeywordVal = Lex.getUIntVal(); - bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 0); - if (!Result) { - cast(Inst)->setHasNoUnsignedOverflow(true); - if (AlsoSigned) - cast(Inst)->setHasNoSignedOverflow(true); - } - return Result; - } - return TokError("expected 'add', 'sub', or 'mul'"); - } - case lltok::kw_nsw: { - bool AlsoUnsigned = EatIfPresent(lltok::kw_nuw); - if (Lex.getKind() == lltok::kw_add || - Lex.getKind() == lltok::kw_sub || - Lex.getKind() == lltok::kw_mul) { - Lex.Lex(); - KeywordVal = Lex.getUIntVal(); - bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 1); - if (!Result) { - cast(Inst)->setHasNoSignedOverflow(true); - if (AlsoUnsigned) - cast(Inst)->setHasNoUnsignedOverflow(true); - } - return Result; - } - return TokError("expected 'add', 'sub', or 'mul'"); - } - case lltok::kw_exact: - if (Lex.getKind() == lltok::kw_sdiv) { - Lex.Lex(); - KeywordVal = Lex.getUIntVal(); - bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 1); - if (!Result) - cast(Inst)->setIsExact(true); - return Result; - } - return TokError("expected 'udiv'"); case lltok::kw_getresult: return ParseGetResult(Inst, PFS); case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS); case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS); -- cgit v1.2.3