question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't yield item from ValueRef iterator.

See original GitHub issue

Store instruction in llvm ir. i would like to get second operand. “<pointer>” by using next function. but how many use next. it always return first operand. python for clause works. but next function doesn’t work.

I did something wrong? my test code like this.

def applyConstraint(instruction: llvm.ValueRef):
	if instruction.opcode == 'store':
		print(instruction.operands.next())
		print(instruction.operands.next())
		for i in instruction.operands:
			print(i)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dbwodlf3commented, Nov 18, 2020

I appreciate your answer!

0reactions
gmarkallcommented, Nov 18, 2020

Thanks for the details - I can now see the issue. When you call instruction.operands.next(), instruction.operands gives you a new _OperandsIterator - so when you call next() on it, you get the first operand. When you then do instruction.operands.next() on the following line, instruction.operands gives a new _OperandsIterator again, so calling next() on it gives you the first operand again.

Your code:

for function in module.functions:
    for block in function.blocks
        for instruction in block.instructions:
            # HERE operands IS NOT LIST.
            # HERE operands is ITERATOR TYPE.
            # _OperandsIterator TYPE.       
            for operand in instruction.operands:
                print(operand)

written using next() instead could look like this to accomplish what you were originally trying to do:

for function in module.functions:
    for block in function.blocks:
        for instruction in block.instructions:
            if instruction.opcode == 'store':
                ops = instruction.operands # ops holds the _OperandsIterator
                print(ops.next()) 
                print(ops.next())

With your simple2.ll file, this gives me the output:

i32 %foo
  %alloca_ins1 = alloca i32
i8 1
  %alloca_ins1 = alloca i8
i16 2
  %alloca_ins2 = alloca i16
i32 3
  %alloca_ins3 = alloca i32
i64 4
  %alloca_ins4 = alloca i64
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't iterator methods take either 'ref' or 'out' parameters?
C# iterators are state machines internally. Every time you yield return something, the place where you left off should be saved along with ......
Read more >
yield statement - provide the next element in an iterator
Use the yield statement in iterators to provide the next value or signal the end of an iteration.
Read more >
C#: IEnumerable, yield return, and lazy evaluation
An iterator is a method that uses the yield return keywords. yield return is ... First() to try to find a specific item...
Read more >
Python Yield - What does the yield keyword do? | ML+
Adding yield keyword to a function will make the function return a ... You can then iterate through the generator to extract items....
Read more >
Python Yield vs Return - Initial Commit
An iterator in Python is an object to which you can apply the __next__() method to and iterate through it. We can turn...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found