mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-22 10:42:39 +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:
parent
a1fc09c260
commit
20cb4dd191
@ -23,6 +23,7 @@ class LLVMContext;
|
|||||||
class Module;
|
class Module;
|
||||||
struct SlotMapping;
|
struct SlotMapping;
|
||||||
class SMDiagnostic;
|
class SMDiagnostic;
|
||||||
|
class Type;
|
||||||
|
|
||||||
/// This function is the main interface to the LLVM Assembly Parser. It parses
|
/// 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
|
/// 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,
|
Constant *parseConstantValue(StringRef Asm, SMDiagnostic &Err, const Module &M,
|
||||||
const SlotMapping *Slots = nullptr);
|
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
|
} // End llvm namespace
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -63,6 +63,18 @@ bool LLParser::parseStandaloneConstantValue(Constant *&C,
|
|||||||
return false;
|
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) {
|
void LLParser::restoreParsingState(const SlotMapping *Slots) {
|
||||||
if (!Slots)
|
if (!Slots)
|
||||||
return;
|
return;
|
||||||
|
@ -148,6 +148,8 @@ namespace llvm {
|
|||||||
|
|
||||||
bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots);
|
bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots);
|
||||||
|
|
||||||
|
bool parseStandaloneType(Type *&Ty, const SlotMapping *Slots);
|
||||||
|
|
||||||
LLVMContext &getContext() { return Context; }
|
LLVMContext &getContext() { return Context; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -78,3 +78,15 @@ Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err,
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
return C;
|
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;
|
||||||
|
}
|
||||||
|
@ -152,4 +152,124 @@ TEST(AsmParserTest, TypeAndConstantValueWithSlotMappingParsing) {
|
|||||||
ASSERT_TRUE(isa<ConstantExpr>(V));
|
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
|
} // end anonymous namespace
|
||||||
|
Loading…
Reference in New Issue
Block a user