[llvm] r242579 - AsmParser: Add a function to parse a standalone constant value.
Alex Lorenz
arphaman at gmail.com
Fri Jul 17 15:07:03 PDT 2015
Author: arphaman
Date: Fri Jul 17 17:07:03 2015
New Revision: 242579
URL: http://llvm.org/viewvc/llvm-project?rev=242579&view=rev
Log:
AsmParser: Add a function to parse a standalone constant value.
This commit extends the interface provided by the AsmParser library by adding a
function that allows the user to parse a standalone contant value.
This change is useful for MIR serialization, as it will allow the MIR Parser to
parse the constant values in a machine constant pool.
Reviewers: Duncan P. N. Exon Smith
Differential Revision: http://reviews.llvm.org/D10280
Modified:
llvm/trunk/include/llvm/AsmParser/Parser.h
llvm/trunk/lib/AsmParser/LLParser.cpp
llvm/trunk/lib/AsmParser/LLParser.h
llvm/trunk/lib/AsmParser/Parser.cpp
llvm/trunk/unittests/AsmParser/AsmParserTest.cpp
Modified: llvm/trunk/include/llvm/AsmParser/Parser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/AsmParser/Parser.h?rev=242579&r1=242578&r2=242579&view=diff
==============================================================================
--- llvm/trunk/include/llvm/AsmParser/Parser.h (original)
+++ llvm/trunk/include/llvm/AsmParser/Parser.h Fri Jul 17 17:07:03 2015
@@ -18,6 +18,7 @@
namespace llvm {
+class Constant;
class LLVMContext;
class Module;
struct SlotMapping;
@@ -79,6 +80,14 @@ std::unique_ptr<Module> parseAssembly(Me
bool parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err,
SlotMapping *Slots = nullptr);
+/// Parse a type and a constant value in the given string.
+///
+/// The constant value can be any LLVM constant, including a constant
+/// expression.
+///
+/// \return null on error.
+Constant *parseConstantValue(StringRef Asm, SMDiagnostic &Err, const Module &M);
+
} // End llvm namespace
#endif
Modified: llvm/trunk/lib/AsmParser/LLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.cpp?rev=242579&r1=242578&r2=242579&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLParser.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLParser.cpp Fri Jul 17 17:07:03 2015
@@ -48,6 +48,17 @@ bool LLParser::Run() {
ValidateEndOfModule();
}
+bool LLParser::parseStandaloneConstantValue(Constant *&C) {
+ Lex.Lex();
+
+ Type *Ty = nullptr;
+ if (ParseType(Ty) || parseConstantValue(Ty, C))
+ return true;
+ if (Lex.getKind() != lltok::Eof)
+ return Error(Lex.getLoc(), "expected end of string");
+ return false;
+}
+
/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
/// module.
bool LLParser::ValidateEndOfModule() {
@@ -4065,6 +4076,30 @@ bool LLParser::ConvertValIDToValue(Type
llvm_unreachable("Invalid ValID");
}
+bool LLParser::parseConstantValue(Type *Ty, Constant *&C) {
+ C = nullptr;
+ ValID ID;
+ auto Loc = Lex.getLoc();
+ if (ParseValID(ID, /*PFS=*/nullptr))
+ return true;
+ switch (ID.Kind) {
+ case ValID::t_APSInt:
+ case ValID::t_APFloat:
+ case ValID::t_Constant:
+ case ValID::t_ConstantStruct:
+ case ValID::t_PackedConstantStruct: {
+ Value *V;
+ if (ConvertValIDToValue(Ty, ID, V, /*PFS=*/nullptr))
+ return true;
+ assert(isa<Constant>(V) && "Expected a constant value");
+ C = cast<Constant>(V);
+ return false;
+ }
+ default:
+ return Error(Loc, "expected a constant value");
+ }
+}
+
bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
V = nullptr;
ValID ID;
Modified: llvm/trunk/lib/AsmParser/LLParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.h?rev=242579&r1=242578&r2=242579&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLParser.h (original)
+++ llvm/trunk/lib/AsmParser/LLParser.h Fri Jul 17 17:07:03 2015
@@ -143,6 +143,8 @@ namespace llvm {
Slots(Slots), BlockAddressPFS(nullptr) {}
bool Run();
+ bool parseStandaloneConstantValue(Constant *&C);
+
LLVMContext &getContext() { return Context; }
private:
@@ -343,6 +345,7 @@ namespace llvm {
bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
PerFunctionState *PFS);
+ bool parseConstantValue(Type *Ty, Constant *&C);
bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
return ParseValue(Ty, V, &PFS);
Modified: llvm/trunk/lib/AsmParser/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/Parser.cpp?rev=242579&r1=242578&r2=242579&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/Parser.cpp (original)
+++ llvm/trunk/lib/AsmParser/Parser.cpp Fri Jul 17 17:07:03 2015
@@ -66,3 +66,15 @@ std::unique_ptr<Module> llvm::parseAssem
MemoryBufferRef F(AsmString, "<string>");
return parseAssembly(F, Err, Context, Slots);
}
+
+Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err,
+ const Module &M) {
+ SourceMgr SM;
+ std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
+ SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
+ Constant *C;
+ if (LLParser(Asm, SM, Err, const_cast<Module *>(&M))
+ .parseStandaloneConstantValue(C))
+ return nullptr;
+ return C;
+}
Modified: llvm/trunk/unittests/AsmParser/AsmParserTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/AsmParser/AsmParserTest.cpp?rev=242579&r1=242578&r2=242579&view=diff
==============================================================================
--- llvm/trunk/unittests/AsmParser/AsmParserTest.cpp (original)
+++ llvm/trunk/unittests/AsmParser/AsmParserTest.cpp Fri Jul 17 17:07:03 2015
@@ -10,6 +10,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/AsmParser/SlotMapping.h"
+#include "llvm/IR/Constants.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
@@ -64,4 +65,51 @@ TEST(AsmParserTest, SlotMappingTest) {
EXPECT_EQ(Mapping.MetadataNodes.count(1), 0u);
}
+TEST(AsmParserTest, TypeAndConstantValueParsing) {
+ LLVMContext &Ctx = getGlobalContext();
+ SMDiagnostic Error;
+ StringRef Source = "define void @test() {\n entry:\n ret void\n}";
+ auto Mod = parseAssemblyString(Source, Error, Ctx);
+ ASSERT_TRUE(Mod != nullptr);
+ auto &M = *Mod;
+
+ const Value *V;
+ V = parseConstantValue("double 3.5", Error, M);
+ ASSERT_TRUE(V);
+ EXPECT_TRUE(V->getType()->isDoubleTy());
+ ASSERT_TRUE(isa<ConstantFP>(V));
+ EXPECT_TRUE(cast<ConstantFP>(V)->isExactlyValue(3.5));
+
+ V = parseConstantValue("i32 42", Error, M);
+ ASSERT_TRUE(V);
+ EXPECT_TRUE(V->getType()->isIntegerTy());
+ ASSERT_TRUE(isa<ConstantInt>(V));
+ EXPECT_TRUE(cast<ConstantInt>(V)->equalsInt(42));
+
+ V = parseConstantValue("<4 x i32> <i32 0, i32 1, i32 2, i32 3>", Error, M);
+ ASSERT_TRUE(V);
+ EXPECT_TRUE(V->getType()->isVectorTy());
+ ASSERT_TRUE(isa<ConstantDataVector>(V));
+
+ V = parseConstantValue("i32 add (i32 1, i32 2)", Error, M);
+ ASSERT_TRUE(V);
+ ASSERT_TRUE(isa<ConstantInt>(V));
+
+ V = parseConstantValue("i8* blockaddress(@test, %entry)", Error, M);
+ ASSERT_TRUE(V);
+ ASSERT_TRUE(isa<BlockAddress>(V));
+
+ EXPECT_FALSE(parseConstantValue("duble 3.25", Error, M));
+ EXPECT_EQ(Error.getMessage(), "expected type");
+
+ EXPECT_FALSE(parseConstantValue("i32 3.25", Error, M));
+ EXPECT_EQ(Error.getMessage(), "floating point constant invalid for type");
+
+ EXPECT_FALSE(parseConstantValue("i32* @foo", Error, M));
+ EXPECT_EQ(Error.getMessage(), "expected a constant value");
+
+ EXPECT_FALSE(parseConstantValue("i32 3, ", Error, M));
+ EXPECT_EQ(Error.getMessage(), "expected end of string");
+}
+
} // end anonymous namespace
More information about the llvm-commits
mailing list