2012-02-08 9 views
6

के लिए सेट मान मैं एलएलवीएम के साथ खेल रहा हूं। मैंने इंटरमीडिएट कोड में स्थिर के मूल्य को बदलने के बारे में सोचा। हालांकि, कक्षा llvm::ConstantInt के लिए, मुझे एक सेटवल्यू फ़ंक्शन दिखाई नहीं देता है। कोई विचार मैं आईआर कोड में निरंतर के मूल्य को कैसे संशोधित कर सकता हूं?llvm :: ConstantInt

उत्तर

12

ConstantInt एक कारखाना है, है ना? कक्षा get method नई निरंतर निर्माण करने के लिए है:

 /* ... return a ConstantInt for the given value. */ 
00069 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false); 

तो, मुझे लगता है कि, आप मौजूदा ConstantInt संशोधित नहीं कर सकते। यदि आप आईआर को संशोधित करना चाहते हैं, तो आपको पॉइंटर को तर्क में बदलने की कोशिश करनी चाहिए (आईआर स्वयं को बदलें, लेकिन निरंतर ऑब्जेक्ट नहीं)।

क्या आप ऐसा कुछ चाहते हैं (कृपया याद रखें, मेरे पास एलएलवीएम के साथ शून्य अनुभव है; और मुझे पूरा यकीन है कि उदाहरण गलत है)।

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); 
} 

"संशोधित" करने के लिए एक निरंतर अकेले वहाँ एक समाधान है (वेतन वृद्धि और कमी are illustrated):

/// 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); 
} 

पुनश्च, Constant.h बनाने और की गैर हटाने के बारे में भीख माँग में महत्वपूर्ण टिप्पणी स्थिरांक 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 
+0

आपका समाधान अच्छा लग रहा है, यह कोशिश करेंगे :)। – MetallicPriest

+0

मुझे उम्मीद है, @ एंटोन Korobeynikov, मेरे कोड का जवाब या टिप्पणी करेंगे। आपको यह भी पता होना चाहिए कि सेटऑपरेंड कुछ ऐसा नहीं बदल सकता जो स्थिर है। – osgx

+0

यह काम किया! एक व्यक्ति (आप) के लिए शानदार जो कभी इसका इस्तेमाल नहीं करता! यह भी दिखाता है कि एलएलवीएम कितनी अच्छी तरह लिखा गया है, क्योंकि यह सीखना और उपयोग करना इतना आसान है! – MetallicPriest

संबंधित मुद्दे