[llvm-dev] Printing out a 128 bit decimal

Tim Northover via llvm-dev
Fri, 26 May 2017 14:57:03 -0000

Adding the list back, in case this ever crops up for someone else.

On 26 May 2017 at 07:39, Jajoo, Malhar <malhar.jajoo14 at imperial.ac.uk>
wrote:
> On seei ng a "Value" , try and dynamic_cast it to a ConstantInt.
> If it does cast , then get the APInt for this ConstantInt.
> From the APInt get a std::string
> Convert std::string to StringRef
> Convert StringRef to Value*
> Place this Value* in a vector<Value*> as argument to printfCall.
> Use "%s" in format string of printf.
>
> My issue:  step 5. How can I get that done ?

Ah, so you actually want to print it at runtime rather than during
compilation? You've got 2 options there.

Your approach will work. You need to call
IRBuilder::CreateGlobalStringPtr, which will insert a global variable
into your Module which is initialized by your StringRef. Sort of like
'const char tmp[] = "whatever";' in C++. Then you can add the
return
result from that to a printf call.

Alternatively you could write your own i128 printer, link against it
and call that instead of printf from your code. Then the Value would
just be the original ConstantInt you started with. This has the
advantage that it'd work even with non-constant integers, and doesn't
put a bunch of extra strings into your module. The disadvantage is the
extra calls you'll have to make: printf("First part of line");
print128(value); printf("second half of line\n");.

So the choice is yours.

Tim.