LLVM ile oynuyorum. Ara koddaki bir sabitin değerini değiştirmeyi düşündüm. Ancak, llvm::ConstantInt sınıfı için bir setvalue işlevi görmüyorum. Herhangi bir fikir IR kodundaki bir sabitin değerini nasıl değiştirebilirim?Set değeri için llvm :: ConstantInt
6
A
cevap
12
ConstantInt bir fabrika değil mi? Sınıf yeni sabit inşa etmek get method sahiptir:
/* ... return a ConstantInt for the given value. */
00069 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
Yani, bence, varolan ConstantInt değiştiremezsiniz. IR'yi değiştirmek isterseniz, göstergeyi argüman olarak değiştirmeyi denemelisiniz (IR nesnesini değiştirin, ancak sabit nesneyi değil).
Bunun gibi bir şey ister misiniz (lütfen unutmayın, LLVM ile sıfır deneyimim var ve neredeyse eminim örnek yanlıştır).
Instruction *I = /* your argument */;
/* check that instruction is of needed format, e.g: */
if (I->getOpcode() == Instruction::Add) {
/* read the first operand of instruction */
Value *oldvalue = I->getOperand(0);
/* construct new constant; here 0x1234 is used as value */
Value *newvalue = ConstantInt::get(oldValue->getType(), 0x1234);
/* replace operand with new value */
I->setOperand(0, newvalue);
}
sabit yalnız başına bir çözelti olan (artım ve are illustrated azaltma) "değiştirmek" için :
/// AddOne - Add one to a ConstantInt.
static Constant *AddOne(Constant *C) {
return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
}
/// SubOne - Subtract one from a ConstantInt.
static Constant *SubOne(ConstantInt *C) {
return ConstantInt::get(C->getContext(), C->getValue()-1);
}
PS Constant.h oluşturma ve olmayan silme yaklaşık dilenen olarak önemli yorumu vardır Sabitler http://llvm.org/docs/doxygen/html/Constant_8h_source.html
00035 /// Note that Constants are immutable (once created they never change)
00036 /// and are fully shared by structural equivalence. This means that two
00037 /// structurally equivalent constants will always have the same address.
00038 /// Constants are created on demand as needed and never deleted: thus clients
00039 /// don't have to worry about the lifetime of the objects.
00040 /// @brief LLVM Constant Representation
Çözümünüz iyi görünüyor, deneyeceğim :). – MetallicPriest
Umarım, @Anton Korobeynikov, kodumu cevaplayacak veya yorum yazacaktır. Ayrıca bilmelisiniz ki, setOperand sabit olan bir şeyi değiştiremez. – osgx
Çalıştı! Hiç kullanmayan bir kişi için (parlak)! Ayrıca öğrenmesi ve kullanması çok kolay olduğu için LLVM'nin ne kadar iyi yazıldığını göstermeye de devam ediyor! – MetallicPriest