1

I was trying to print out a 128 bit result at runtime using a call to printf in LLVM.

I am able to print 32 uptil 64 bit integers using the format string "%d" in printf .

Please see the code below , as I am having a bit difficulty in trying to print the 128 bit value computed at runtime. The code section containing a dynamic cast to ConstantInt is never executed since the value is never be casted to a ConstantInt.

( I saw this approach on SO - LLVM get constant integer back from Value* )

Only the relevant code section is shown below

Value* val = e->codegen() ;

        if(!val)
        {
            return logError("Error evaluating argument to function call");
        }


        if(val->getType()->isIntegerTy() )
        {
            if(val->getType()->getIntegerBitWidth() <= 64)
            {
                tempString = tempString + "%+d,";
            }

             // Is this correct ? Doesnt seem to be executing ...
            else 
            {
                if(ConstantInt* CI = dyn_cast<ConstantInt>(val))
                {

                    // base 10 and signed
                    std::string res = CI->getValue().toString(10,true);
                    val=Builder.CreateGlobalStringPtr(res,"str");

                    tempString = tempString + "%+s,";

                }
            }

        }



        argsValueVector.push_back(val);
        ++i;

    }

    formatString = formatString + tempString + "\n" ;



        // every string is declared as a "global constant" at the top of the module.
        Value* val=Builder.CreateGlobalStringPtr(formatString,"str");

        std::vector<Value*>::iterator it = argsValueVector.begin();

        argsValueVector.insert(it,val);
    }

    return Builder.CreateCall(F,argsValueVector,"calltmp") ;

}
mal
  • 143
  • 2
  • 12

0 Answers0