1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00

[AsmParser] Add a function to parse a standalone type.

This is useful for MIR serialization. Indeed generic machine instructions
must have a type and we don't want to duplicate the logic in the MIParser.

llvm-svn: 262868
This commit is contained in:
Quentin Colombet 2016-03-07 22:09:05 +00:00
parent a1fc09c260
commit 20cb4dd191
5 changed files with 155 additions and 0 deletions

View File

@ -23,6 +23,7 @@ class LLVMContext;
class Module;
struct SlotMapping;
class SMDiagnostic;
class Type;
/// This function is the main interface to the LLVM Assembly Parser. It parses
/// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
@ -91,6 +92,14 @@ bool parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err,
Constant *parseConstantValue(StringRef Asm, SMDiagnostic &Err, const Module &M,
const SlotMapping *Slots = nullptr);
/// Parse a type in the given string.
///
/// \param Slots The optional slot mapping that will restore the parsing state
/// of the module.
/// \return null on error.
Type *parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
const SlotMapping *Slots = nullptr);
} // End llvm namespace
#endif

View File

@ -63,6 +63,18 @@ bool LLParser::parseStandaloneConstantValue(Constant *&C,
return false;
}
bool LLParser::parseStandaloneType(Type *&Ty, const SlotMapping *Slots) {
restoreParsingState(Slots);
Lex.Lex();
Ty = nullptr;
if (ParseType(Ty))
return true;
if (Lex.getKind() != lltok::Eof)
return Error(Lex.getLoc(), "expected end of string");
return false;
}
void LLParser::restoreParsingState(const SlotMapping *Slots) {
if (!Slots)
return;

View File

@ -148,6 +148,8 @@ namespace llvm {
bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots);
bool parseStandaloneType(Type *&Ty, const SlotMapping *Slots);
LLVMContext &getContext() { return Context; }
private:

View File

@ -78,3 +78,15 @@ Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err,
return nullptr;
return C;
}
Type *llvm::parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
const SlotMapping *Slots) {
SourceMgr SM;
std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
Type *Ty;
if (LLParser(Asm, SM, Err, const_cast<Module *>(&M))
.parseStandaloneType(Ty, Slots))
return nullptr;
return Ty;
}

View File

@ -152,4 +152,124 @@ TEST(AsmParserTest, TypeAndConstantValueWithSlotMappingParsing) {
ASSERT_TRUE(isa<ConstantExpr>(V));
}
TEST(AsmParserTest, TypeWithSlotMappingParsing) {
LLVMContext &Ctx = getGlobalContext();
SMDiagnostic Error;
StringRef Source =
"%st = type { i32, i32 }\n"
"@v = common global [50 x %st] zeroinitializer, align 16\n"
"%0 = type { i32, i32, i32, i32 }\n"
"@g = common global [50 x %0] zeroinitializer, align 16\n"
"define void @marker4(i64 %d) {\n"
"entry:\n"
" %conv = trunc i64 %d to i32\n"
" store i32 %conv, i32* getelementptr inbounds "
" ([50 x %st], [50 x %st]* @v, i64 0, i64 0, i32 0), align 16\n"
" store i32 %conv, i32* getelementptr inbounds "
" ([50 x %0], [50 x %0]* @g, i64 0, i64 0, i32 0), align 16\n"
" ret void\n"
"}";
SlotMapping Mapping;
auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
ASSERT_TRUE(Mod != nullptr);
auto &M = *Mod;
// Check we properly parse integer types.
Type *Ty;
Ty = parseType("i32", Error, M, &Mapping);
ASSERT_TRUE(Ty);
ASSERT_TRUE(Ty->isIntegerTy());
ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
// Check we properly parse integer types with exotic size.
Ty = parseType("i13", Error, M, &Mapping);
ASSERT_TRUE(Ty);
ASSERT_TRUE(Ty->isIntegerTy());
ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 13);
// Check we properly parse floating point types.
Ty = parseType("float", Error, M, &Mapping);
ASSERT_TRUE(Ty);
ASSERT_TRUE(Ty->isFloatTy());
Ty = parseType("double", Error, M, &Mapping);
ASSERT_TRUE(Ty);
ASSERT_TRUE(Ty->isDoubleTy());
// Check we properly parse struct types.
// Named struct.
Ty = parseType("%st", Error, M, &Mapping);
ASSERT_TRUE(Ty);
ASSERT_TRUE(Ty->isStructTy());
// Check the details of the struct.
StructType *ST = cast<StructType>(Ty);
ASSERT_TRUE(ST->getNumElements() == 2);
for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
Ty = ST->getElementType(i);
ASSERT_TRUE(Ty->isIntegerTy());
ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
}
// Anonymous struct.
Ty = parseType("%0", Error, M, &Mapping);
ASSERT_TRUE(Ty);
ASSERT_TRUE(Ty->isStructTy());
// Check the details of the struct.
ST = cast<StructType>(Ty);
ASSERT_TRUE(ST->getNumElements() == 4);
for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
Ty = ST->getElementType(i);
ASSERT_TRUE(Ty->isIntegerTy());
ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
}
// Check we properly parse vector types.
Ty = parseType("<5 x i32>", Error, M, &Mapping);
ASSERT_TRUE(Ty);
ASSERT_TRUE(Ty->isVectorTy());
// Check the details of the vector.
VectorType *VT = cast<VectorType>(Ty);
ASSERT_TRUE(VT->getNumElements() == 5);
ASSERT_TRUE(VT->getBitWidth() == 160);
Ty = VT->getElementType();
ASSERT_TRUE(Ty->isIntegerTy());
ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
// Opaque struct.
Ty = parseType("%opaque", Error, M, &Mapping);
ASSERT_TRUE(Ty);
ASSERT_TRUE(Ty->isStructTy());
ST = cast<StructType>(Ty);
ASSERT_TRUE(ST->isOpaque());
// Check we properly parse pointer types.
// One indirection.
Ty = parseType("i32*", Error, M, &Mapping);
ASSERT_TRUE(Ty);
ASSERT_TRUE(Ty->isPointerTy());
PointerType *PT = cast<PointerType>(Ty);
Ty = PT->getElementType();
ASSERT_TRUE(Ty->isIntegerTy());
ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
// Two indirections.
Ty = parseType("i32**", Error, M, &Mapping);
ASSERT_TRUE(Ty);
ASSERT_TRUE(Ty->isPointerTy());
PT = cast<PointerType>(Ty);
Ty = PT->getElementType();
ASSERT_TRUE(Ty->isPointerTy());
PT = cast<PointerType>(Ty);
Ty = PT->getElementType();
ASSERT_TRUE(Ty->isIntegerTy());
ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
}
} // end anonymous namespace