AsmParserTest.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. //===- llvm/unittest/AsmParser/AsmParserTest.cpp - asm parser unittests ---===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/AsmParser/Parser.h"
  10. #include "llvm/AsmParser/SlotMapping.h"
  11. #include "llvm/IR/Constants.h"
  12. #include "llvm/IR/LLVMContext.h"
  13. #include "llvm/IR/Module.h"
  14. #include "llvm/Support/SourceMgr.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. namespace {
  18. TEST(AsmParserTest, NullTerminatedInput) {
  19. LLVMContext Ctx;
  20. StringRef Source = "; Empty module \n";
  21. SMDiagnostic Error;
  22. auto Mod = parseAssemblyString(Source, Error, Ctx);
  23. EXPECT_TRUE(Mod != nullptr);
  24. EXPECT_TRUE(Error.getMessage().empty());
  25. }
  26. #ifdef GTEST_HAS_DEATH_TEST
  27. #ifndef NDEBUG
  28. TEST(AsmParserTest, NonNullTerminatedInput) {
  29. LLVMContext Ctx;
  30. StringRef Source = "; Empty module \n\1\2";
  31. SMDiagnostic Error;
  32. std::unique_ptr<Module> Mod;
  33. EXPECT_DEATH(Mod = parseAssemblyString(Source.substr(0, Source.size() - 2),
  34. Error, Ctx),
  35. "Buffer is not null terminated!");
  36. }
  37. #endif
  38. #endif
  39. TEST(AsmParserTest, SlotMappingTest) {
  40. LLVMContext Ctx;
  41. StringRef Source = "@0 = global i32 0\n !0 = !{}\n !42 = !{i32 42}";
  42. SMDiagnostic Error;
  43. SlotMapping Mapping;
  44. auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
  45. EXPECT_TRUE(Mod != nullptr);
  46. EXPECT_TRUE(Error.getMessage().empty());
  47. ASSERT_EQ(Mapping.GlobalValues.size(), 1u);
  48. EXPECT_TRUE(isa<GlobalVariable>(Mapping.GlobalValues[0]));
  49. EXPECT_EQ(Mapping.MetadataNodes.size(), 2u);
  50. EXPECT_EQ(Mapping.MetadataNodes.count(0), 1u);
  51. EXPECT_EQ(Mapping.MetadataNodes.count(42), 1u);
  52. EXPECT_EQ(Mapping.MetadataNodes.count(1), 0u);
  53. }
  54. TEST(AsmParserTest, TypeAndConstantValueParsing) {
  55. LLVMContext Ctx;
  56. SMDiagnostic Error;
  57. StringRef Source = "define void @test() {\n entry:\n ret void\n}";
  58. auto Mod = parseAssemblyString(Source, Error, Ctx);
  59. ASSERT_TRUE(Mod != nullptr);
  60. auto &M = *Mod;
  61. const Value *V;
  62. V = parseConstantValue("double 3.5", Error, M);
  63. ASSERT_TRUE(V);
  64. EXPECT_TRUE(V->getType()->isDoubleTy());
  65. ASSERT_TRUE(isa<ConstantFP>(V));
  66. EXPECT_TRUE(cast<ConstantFP>(V)->isExactlyValue(3.5));
  67. V = parseConstantValue("i32 42", Error, M);
  68. ASSERT_TRUE(V);
  69. EXPECT_TRUE(V->getType()->isIntegerTy());
  70. ASSERT_TRUE(isa<ConstantInt>(V));
  71. EXPECT_TRUE(cast<ConstantInt>(V)->equalsInt(42));
  72. V = parseConstantValue("<4 x i32> <i32 0, i32 1, i32 2, i32 3>", Error, M);
  73. ASSERT_TRUE(V);
  74. EXPECT_TRUE(V->getType()->isVectorTy());
  75. ASSERT_TRUE(isa<ConstantDataVector>(V));
  76. V = parseConstantValue("i32 add (i32 1, i32 2)", Error, M);
  77. ASSERT_TRUE(V);
  78. ASSERT_TRUE(isa<ConstantInt>(V));
  79. V = parseConstantValue("i8* blockaddress(@test, %entry)", Error, M);
  80. ASSERT_TRUE(V);
  81. ASSERT_TRUE(isa<BlockAddress>(V));
  82. V = parseConstantValue("i8** undef", Error, M);
  83. ASSERT_TRUE(V);
  84. ASSERT_TRUE(isa<UndefValue>(V));
  85. EXPECT_FALSE(parseConstantValue("duble 3.25", Error, M));
  86. EXPECT_EQ(Error.getMessage(), "expected type");
  87. EXPECT_FALSE(parseConstantValue("i32 3.25", Error, M));
  88. EXPECT_EQ(Error.getMessage(), "floating point constant invalid for type");
  89. EXPECT_FALSE(parseConstantValue("i32* @foo", Error, M));
  90. EXPECT_EQ(Error.getMessage(), "expected a constant value");
  91. EXPECT_FALSE(parseConstantValue("i32 3, ", Error, M));
  92. EXPECT_EQ(Error.getMessage(), "expected end of string");
  93. }
  94. TEST(AsmParserTest, TypeAndConstantValueWithSlotMappingParsing) {
  95. LLVMContext Ctx;
  96. SMDiagnostic Error;
  97. StringRef Source =
  98. "%st = type { i32, i32 }\n"
  99. "@v = common global [50 x %st] zeroinitializer, align 16\n"
  100. "%0 = type { i32, i32, i32, i32 }\n"
  101. "@g = common global [50 x %0] zeroinitializer, align 16\n"
  102. "define void @marker4(i64 %d) {\n"
  103. "entry:\n"
  104. " %conv = trunc i64 %d to i32\n"
  105. " store i32 %conv, i32* getelementptr inbounds "
  106. " ([50 x %st], [50 x %st]* @v, i64 0, i64 0, i32 0), align 16\n"
  107. " store i32 %conv, i32* getelementptr inbounds "
  108. " ([50 x %0], [50 x %0]* @g, i64 0, i64 0, i32 0), align 16\n"
  109. " ret void\n"
  110. "}";
  111. SlotMapping Mapping;
  112. auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
  113. ASSERT_TRUE(Mod != nullptr);
  114. auto &M = *Mod;
  115. const Value *V;
  116. V = parseConstantValue("i32* getelementptr inbounds ([50 x %st], [50 x %st]* "
  117. "@v, i64 0, i64 0, i32 0)",
  118. Error, M, &Mapping);
  119. ASSERT_TRUE(V);
  120. ASSERT_TRUE(isa<ConstantExpr>(V));
  121. V = parseConstantValue("i32* getelementptr inbounds ([50 x %0], [50 x %0]* "
  122. "@g, i64 0, i64 0, i32 0)",
  123. Error, M, &Mapping);
  124. ASSERT_TRUE(V);
  125. ASSERT_TRUE(isa<ConstantExpr>(V));
  126. }
  127. TEST(AsmParserTest, TypeWithSlotMappingParsing) {
  128. LLVMContext Ctx;
  129. SMDiagnostic Error;
  130. StringRef Source =
  131. "%st = type { i32, i32 }\n"
  132. "@v = common global [50 x %st] zeroinitializer, align 16\n"
  133. "%0 = type { i32, i32, i32, i32 }\n"
  134. "@g = common global [50 x %0] zeroinitializer, align 16\n"
  135. "define void @marker4(i64 %d) {\n"
  136. "entry:\n"
  137. " %conv = trunc i64 %d to i32\n"
  138. " store i32 %conv, i32* getelementptr inbounds "
  139. " ([50 x %st], [50 x %st]* @v, i64 0, i64 0, i32 0), align 16\n"
  140. " store i32 %conv, i32* getelementptr inbounds "
  141. " ([50 x %0], [50 x %0]* @g, i64 0, i64 0, i32 0), align 16\n"
  142. " ret void\n"
  143. "}";
  144. SlotMapping Mapping;
  145. auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
  146. ASSERT_TRUE(Mod != nullptr);
  147. auto &M = *Mod;
  148. // Check we properly parse integer types.
  149. Type *Ty;
  150. Ty = parseType("i32", Error, M, &Mapping);
  151. ASSERT_TRUE(Ty);
  152. ASSERT_TRUE(Ty->isIntegerTy());
  153. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  154. // Check we properly parse integer types with exotic size.
  155. Ty = parseType("i13", Error, M, &Mapping);
  156. ASSERT_TRUE(Ty);
  157. ASSERT_TRUE(Ty->isIntegerTy());
  158. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 13);
  159. // Check we properly parse floating point types.
  160. Ty = parseType("float", Error, M, &Mapping);
  161. ASSERT_TRUE(Ty);
  162. ASSERT_TRUE(Ty->isFloatTy());
  163. Ty = parseType("double", Error, M, &Mapping);
  164. ASSERT_TRUE(Ty);
  165. ASSERT_TRUE(Ty->isDoubleTy());
  166. // Check we properly parse struct types.
  167. // Named struct.
  168. Ty = parseType("%st", Error, M, &Mapping);
  169. ASSERT_TRUE(Ty);
  170. ASSERT_TRUE(Ty->isStructTy());
  171. // Check the details of the struct.
  172. StructType *ST = cast<StructType>(Ty);
  173. ASSERT_TRUE(ST->getNumElements() == 2);
  174. for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
  175. Ty = ST->getElementType(i);
  176. ASSERT_TRUE(Ty->isIntegerTy());
  177. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  178. }
  179. // Anonymous struct.
  180. Ty = parseType("%0", Error, M, &Mapping);
  181. ASSERT_TRUE(Ty);
  182. ASSERT_TRUE(Ty->isStructTy());
  183. // Check the details of the struct.
  184. ST = cast<StructType>(Ty);
  185. ASSERT_TRUE(ST->getNumElements() == 4);
  186. for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
  187. Ty = ST->getElementType(i);
  188. ASSERT_TRUE(Ty->isIntegerTy());
  189. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  190. }
  191. // Check we properly parse vector types.
  192. Ty = parseType("<5 x i32>", Error, M, &Mapping);
  193. ASSERT_TRUE(Ty);
  194. ASSERT_TRUE(Ty->isVectorTy());
  195. // Check the details of the vector.
  196. VectorType *VT = cast<VectorType>(Ty);
  197. ASSERT_TRUE(VT->getNumElements() == 5);
  198. ASSERT_TRUE(VT->getBitWidth() == 160);
  199. Ty = VT->getElementType();
  200. ASSERT_TRUE(Ty->isIntegerTy());
  201. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  202. // Opaque struct.
  203. Ty = parseType("%opaque", Error, M, &Mapping);
  204. ASSERT_TRUE(Ty);
  205. ASSERT_TRUE(Ty->isStructTy());
  206. ST = cast<StructType>(Ty);
  207. ASSERT_TRUE(ST->isOpaque());
  208. // Check we properly parse pointer types.
  209. // One indirection.
  210. Ty = parseType("i32*", Error, M, &Mapping);
  211. ASSERT_TRUE(Ty);
  212. ASSERT_TRUE(Ty->isPointerTy());
  213. PointerType *PT = cast<PointerType>(Ty);
  214. Ty = PT->getElementType();
  215. ASSERT_TRUE(Ty->isIntegerTy());
  216. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  217. // Two indirections.
  218. Ty = parseType("i32**", Error, M, &Mapping);
  219. ASSERT_TRUE(Ty);
  220. ASSERT_TRUE(Ty->isPointerTy());
  221. PT = cast<PointerType>(Ty);
  222. Ty = PT->getElementType();
  223. ASSERT_TRUE(Ty->isPointerTy());
  224. PT = cast<PointerType>(Ty);
  225. Ty = PT->getElementType();
  226. ASSERT_TRUE(Ty->isIntegerTy());
  227. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  228. // Check that we reject types with garbage.
  229. Ty = parseType("i32 garbage", Error, M, &Mapping);
  230. ASSERT_TRUE(!Ty);
  231. }
  232. TEST(AsmParserTest, TypeAtBeginningWithSlotMappingParsing) {
  233. LLVMContext Ctx;
  234. SMDiagnostic Error;
  235. StringRef Source =
  236. "%st = type { i32, i32 }\n"
  237. "@v = common global [50 x %st] zeroinitializer, align 16\n"
  238. "%0 = type { i32, i32, i32, i32 }\n"
  239. "@g = common global [50 x %0] zeroinitializer, align 16\n"
  240. "define void @marker4(i64 %d) {\n"
  241. "entry:\n"
  242. " %conv = trunc i64 %d to i32\n"
  243. " store i32 %conv, i32* getelementptr inbounds "
  244. " ([50 x %st], [50 x %st]* @v, i64 0, i64 0, i32 0), align 16\n"
  245. " store i32 %conv, i32* getelementptr inbounds "
  246. " ([50 x %0], [50 x %0]* @g, i64 0, i64 0, i32 0), align 16\n"
  247. " ret void\n"
  248. "}";
  249. SlotMapping Mapping;
  250. auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
  251. ASSERT_TRUE(Mod != nullptr);
  252. auto &M = *Mod;
  253. unsigned Read;
  254. // Check we properly parse integer types.
  255. Type *Ty;
  256. Ty = parseTypeAtBeginning("i32", Read, Error, M, &Mapping);
  257. ASSERT_TRUE(Ty);
  258. ASSERT_TRUE(Ty->isIntegerTy());
  259. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  260. ASSERT_TRUE(Read == 3);
  261. // Check we properly parse integer types with exotic size.
  262. Ty = parseTypeAtBeginning("i13", Read, Error, M, &Mapping);
  263. ASSERT_TRUE(Ty);
  264. ASSERT_TRUE(Ty->isIntegerTy());
  265. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 13);
  266. ASSERT_TRUE(Read == 3);
  267. // Check we properly parse floating point types.
  268. Ty = parseTypeAtBeginning("float", Read, Error, M, &Mapping);
  269. ASSERT_TRUE(Ty);
  270. ASSERT_TRUE(Ty->isFloatTy());
  271. ASSERT_TRUE(Read == 5);
  272. Ty = parseTypeAtBeginning("double", Read, Error, M, &Mapping);
  273. ASSERT_TRUE(Ty);
  274. ASSERT_TRUE(Ty->isDoubleTy());
  275. ASSERT_TRUE(Read == 6);
  276. // Check we properly parse struct types.
  277. // Named struct.
  278. Ty = parseTypeAtBeginning("%st", Read, Error, M, &Mapping);
  279. ASSERT_TRUE(Ty);
  280. ASSERT_TRUE(Ty->isStructTy());
  281. ASSERT_TRUE(Read == 3);
  282. // Check the details of the struct.
  283. StructType *ST = cast<StructType>(Ty);
  284. ASSERT_TRUE(ST->getNumElements() == 2);
  285. for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
  286. Ty = ST->getElementType(i);
  287. ASSERT_TRUE(Ty->isIntegerTy());
  288. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  289. }
  290. // Anonymous struct.
  291. Ty = parseTypeAtBeginning("%0", Read, Error, M, &Mapping);
  292. ASSERT_TRUE(Ty);
  293. ASSERT_TRUE(Ty->isStructTy());
  294. ASSERT_TRUE(Read == 2);
  295. // Check the details of the struct.
  296. ST = cast<StructType>(Ty);
  297. ASSERT_TRUE(ST->getNumElements() == 4);
  298. for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
  299. Ty = ST->getElementType(i);
  300. ASSERT_TRUE(Ty->isIntegerTy());
  301. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  302. }
  303. // Check we properly parse vector types.
  304. Ty = parseTypeAtBeginning("<5 x i32>", Read, Error, M, &Mapping);
  305. ASSERT_TRUE(Ty);
  306. ASSERT_TRUE(Ty->isVectorTy());
  307. ASSERT_TRUE(Read == 9);
  308. // Check the details of the vector.
  309. VectorType *VT = cast<VectorType>(Ty);
  310. ASSERT_TRUE(VT->getNumElements() == 5);
  311. ASSERT_TRUE(VT->getBitWidth() == 160);
  312. Ty = VT->getElementType();
  313. ASSERT_TRUE(Ty->isIntegerTy());
  314. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  315. // Opaque struct.
  316. Ty = parseTypeAtBeginning("%opaque", Read, Error, M, &Mapping);
  317. ASSERT_TRUE(Ty);
  318. ASSERT_TRUE(Ty->isStructTy());
  319. ASSERT_TRUE(Read == 7);
  320. ST = cast<StructType>(Ty);
  321. ASSERT_TRUE(ST->isOpaque());
  322. // Check we properly parse pointer types.
  323. // One indirection.
  324. Ty = parseTypeAtBeginning("i32*", Read, Error, M, &Mapping);
  325. ASSERT_TRUE(Ty);
  326. ASSERT_TRUE(Ty->isPointerTy());
  327. ASSERT_TRUE(Read == 4);
  328. PointerType *PT = cast<PointerType>(Ty);
  329. Ty = PT->getElementType();
  330. ASSERT_TRUE(Ty->isIntegerTy());
  331. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  332. // Two indirections.
  333. Ty = parseTypeAtBeginning("i32**", Read, Error, M, &Mapping);
  334. ASSERT_TRUE(Ty);
  335. ASSERT_TRUE(Ty->isPointerTy());
  336. ASSERT_TRUE(Read == 5);
  337. PT = cast<PointerType>(Ty);
  338. Ty = PT->getElementType();
  339. ASSERT_TRUE(Ty->isPointerTy());
  340. PT = cast<PointerType>(Ty);
  341. Ty = PT->getElementType();
  342. ASSERT_TRUE(Ty->isIntegerTy());
  343. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  344. // Check that we reject types with garbage.
  345. Ty = parseTypeAtBeginning("i32 garbage", Read, Error, M, &Mapping);
  346. ASSERT_TRUE(Ty);
  347. ASSERT_TRUE(Ty->isIntegerTy());
  348. ASSERT_TRUE(Ty->getPrimitiveSizeInBits() == 32);
  349. // We go to the next token, i.e., we read "i32" + ' '.
  350. ASSERT_TRUE(Read == 4);
  351. }
  352. } // end anonymous namespace