Hi Gordon, Thanks for your comments.> > Constant.string(value, dont_null_terminate) -- value is a string > > Constant.struct(consts, packed) -- a struct, consts is a list of > > other constants, packed is boolean > > I did this in Ocaml initially, but found the boolean constants pretty > confusing to read in code. I kept asking "What's that random true > doing there?" Therefore, the bindings expose these as const_string/ > const_stringz and const_struct/const_packed_struct respectively. IOK, will do.> :) Type handles in particular are very important. You can't form a > recursive type without using them, so you can't build any sort of data > structure.On it already. BTW, where can I find a good example of how to use it?> Uninitialized builders are very dangerous (they leak instructions if > you use them), so you might want to add overloads for new in order to > avoid boilerplate code.By 'uninitialized', I guess you're referring to builders that are yet positioned on a block/instruction? Maybe it makes more sense to create it 'from' a block, something like: builder = basic_block_obj.builder() with it being positioned at the end of the block by default. But then, your ocaml syntax is much cleaner:> // At the start or end of a BB: > Builder.new(At_end bb) > Builder.new(bb.begin) > > // Before or after a given instruction: > Builder.new(Before instr) > Builder.new(instr.succ)so I'll see how this can be done a bit, ah, Pythonically.> Finally, just as the C++ STL has reverse_iterator, it did prove > necessary to have a separate (At_begin parent | After element) type in > order to walk the IR backwards.Well, it's possible to do: for inst in reversed(block.instructions): # do stuff with inst which will iterate backwards over the instructions of a block. Thanks & Regards, -Mahadevan.
On May 11, 2008, at 07:36, Mahadevan R wrote:> Hi Gordon, > > Thanks for your comments. >>No problem.>> :) Type handles in particular are very important. You can't form a >> recursive type without using them, so you can't build any sort of >> data structure. > > On it already. BTW, where can I find a good example of how to use it?To close the loop with C++ syntax, LLVMTypeHandleRef is really a pointer to a heap-allocated PATypeHolder, which is discussed here: http://llvm.org/docs/ProgrammersManual.html#BuildRecType The file test/Bindings/Ocaml/vmcore.ml contains this fragment: (* RUN: grep -v {RecursiveTy.*RecursiveTy} < %t.ll *) let ty = opaque_type () in let th = handle_to_type ty in refine_type ty (pointer_type ty); let ty = type_of_handle th in insist (define_type_name "RecursiveTy" ty m); insist (ty == element_type ty) Which constructs %RecursiveType = type %RecursiveType*.>> Finally, just as the C++ STL has reverse_iterator, it did prove >> necessary to have a separate (At_begin parent | After element) type >> in order to walk the IR backwards. > > Well, it's possible to do: > > for inst in reversed(block.instructions): > # do stuff with inst > > which will iterate backwards over the instructions of a block.Certainly. There are advantages to using the "up, prev, next" pointers embedded within the IR in some circumstances, though. Consider what if 'do stuff with inst' might entail deleting another related instruction, where that instruction might appear later in reversed(block.instructions)--dangling pointer, boom! :) On the other hand, creating a copy as your code presumably does is also sometimes exactly what's wanted... TMTOWTDI, as they say. — Gordon
> Certainly. There are advantages to using the "up, prev, next" pointers > embedded within the IR in some circumstances, though. Consider what if > 'do stuff with inst' might entail deleting another related > instruction, where that instruction might appear later in > reversed(block.instructions)--dangling pointer, boom! :) On the otherOK, that makes sense. I'll use first-class iterators. Python iterators don't support reverse iteration though. Even if the container supports it (ala rbegin/rend in C++). The "iterator protocol" is defined only for forward iteration. That's why the "reversed()" idiom. BTW, I didn't find any APIs for deleting an instruction?> hand, creating a copy as your code presumably does is also sometimes > exactly what's wanted... TMTOWTDI, as they say.Pythoners will disagree -- see 'python -c "import this"' ;-) Regards, -Mahadevan.