ExecutionEngineTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //===- ExecutionEngineTest.cpp - Unit tests for ExecutionEngine -----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/DerivedTypes.h"
  10. #include "llvm/GlobalVariable.h"
  11. #include "llvm/LLVMContext.h"
  12. #include "llvm/Module.h"
  13. #include "llvm/ADT/OwningPtr.h"
  14. #include "llvm/ExecutionEngine/Interpreter.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. namespace {
  18. class ExecutionEngineTest : public testing::Test {
  19. protected:
  20. ExecutionEngineTest()
  21. : M(new Module("<main>", getGlobalContext())),
  22. Engine(EngineBuilder(M).create()) {
  23. }
  24. virtual void SetUp() {
  25. ASSERT_TRUE(Engine.get() != NULL);
  26. }
  27. GlobalVariable *NewExtGlobal(const Type *T, const Twine &Name) {
  28. return new GlobalVariable(*M, T, false, // Not constant.
  29. GlobalValue::ExternalLinkage, NULL, Name);
  30. }
  31. Module *const M;
  32. const OwningPtr<ExecutionEngine> Engine;
  33. };
  34. TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
  35. GlobalVariable *G1 =
  36. NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
  37. int32_t Mem1 = 3;
  38. Engine->addGlobalMapping(G1, &Mem1);
  39. EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
  40. int32_t Mem2 = 4;
  41. Engine->updateGlobalMapping(G1, &Mem2);
  42. EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
  43. Engine->updateGlobalMapping(G1, NULL);
  44. EXPECT_EQ(NULL, Engine->getPointerToGlobalIfAvailable(G1));
  45. Engine->updateGlobalMapping(G1, &Mem2);
  46. EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
  47. GlobalVariable *G2 =
  48. NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
  49. EXPECT_EQ(NULL, Engine->getPointerToGlobalIfAvailable(G2))
  50. << "The NULL return shouldn't depend on having called"
  51. << " updateGlobalMapping(..., NULL)";
  52. // Check that update...() can be called before add...().
  53. Engine->updateGlobalMapping(G2, &Mem1);
  54. EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G2));
  55. EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1))
  56. << "A second mapping shouldn't affect the first.";
  57. }
  58. TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
  59. GlobalVariable *G1 =
  60. NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
  61. int32_t Mem1 = 3;
  62. Engine->addGlobalMapping(G1, &Mem1);
  63. EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
  64. int32_t Mem2 = 4;
  65. Engine->updateGlobalMapping(G1, &Mem2);
  66. EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1));
  67. EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
  68. GlobalVariable *G2 =
  69. NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
  70. Engine->updateGlobalMapping(G2, &Mem1);
  71. EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
  72. EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
  73. Engine->updateGlobalMapping(G1, NULL);
  74. EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1))
  75. << "Removing one mapping doesn't affect a different one.";
  76. EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem2));
  77. Engine->updateGlobalMapping(G2, &Mem2);
  78. EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1));
  79. EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2))
  80. << "Once a mapping is removed, we can point another GV at the"
  81. << " now-free address.";
  82. }
  83. TEST_F(ExecutionEngineTest, ClearModuleMappings) {
  84. GlobalVariable *G1 =
  85. NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
  86. int32_t Mem1 = 3;
  87. Engine->addGlobalMapping(G1, &Mem1);
  88. EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
  89. Engine->clearGlobalMappingsFromModule(M);
  90. EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1));
  91. GlobalVariable *G2 =
  92. NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
  93. // After clearing the module mappings, we can assign a new GV to the
  94. // same address.
  95. Engine->addGlobalMapping(G2, &Mem1);
  96. EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
  97. }
  98. }