[LLVMbugs] [Bug 13975] New: LLVMConstIntOfString: Possible bug or misunderstanding of feature
bugzilla-daemon at llvm.org
bugzilla-daemon at llvm.org
Fri Sep 28 20:31:09 PDT 2012
http://llvm.org/bugs/show_bug.cgi?id=13975
Bug #: 13975
Summary: LLVMConstIntOfString: Possible bug or misunderstanding
of feature
Product: new-bugs
Version: 3.1
Platform: PC
OS/Version: Linux
Status: NEW
Severity: normal
Priority: P
Component: new bugs
AssignedTo: unassignedbugs at nondot.org
ReportedBy: mr.sunny.narula at gmail.com
CC: llvmbugs at cs.uiuc.edu
Classification: Unclassified
Example code follows:
LLVMContextRef context = LLVMContextCreate();
LLVMValueRef value = LLVMConstIntOfString(LLVMInt64TypeInContext(context),
"0x0000000f0000ffff", 16);
This is working properly as expected.
However I have noticed that if the bits are not prefixed with enough zeros to
correspond to 32 or 64 bits this gives me:
For 0x0fff := 0xffffffff0fff (incorrect)
For 0xfff := 0xfffffffffff (incorrect)
For 0x00000fff := 0xffffffff00000fff (incorrect)
For 0x0000fff := 0xffffffff0000fff (incorrect)
For 0xffffffff := 0xffffffffffffffff (incorrect)
For 0x00000000ffffffff := 0xffffffff (correct)
For 0xffffffffff := 0xffffffffffffffff (incorrect)
I explored the code a bit and found that APInt.cpp file's fromString method
deals with this.
http://llvm.org/docs/doxygen/html/APInt_8cpp_source.html#l02024
Test case in C to reproduce the bug:
int main() {
LLVMContextRef context = LLVMContextCreate();
const char *moduleName = "LLVMConstHexIntOfString";
const char *template = "Integer converted from hexadecimal string# %lx";
LLVMValueRef testValue =
LLVMConstIntOfString(LLVMInt64TypeInContext(context), "0x00fffff", 16);
<---------------------------- Change this hex representation to test multiple
scenarios.
printf("[llvmPrintf# %s] Generating code from C program: \n",
moduleName);
LLVMModuleRef modCEx = LLVMModuleCreateWithNameInContext(moduleName,
context);
// "main" function type definition:
LLVMTypeRef ppInt8 = LLVMPointerType(LLVMPointerType(LLVMInt8Type(), 0),
0);
LLVMTypeRef vpMainFuncTypes[] = { LLVMInt32Type(), ppInt8 };
LLVMTypeRef rMainFuncType = LLVMFunctionType(LLVMInt32Type(),
vpMainFuncTypes, 2, 0);
LLVMValueRef vFnMain = LLVMAddFunction(modCEx, "main", rMainFuncType);
LLVMSetFunctionCallConv(vFnMain, LLVMCCallConv);
LLVMBasicBlockRef bEntry = LLVMAppendBasicBlock(vFnMain, "entry");
LLVMBuilderRef bldr = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(bldr, bEntry);
// "C" printf function;
LLVMTypeRef vpFnPrintfArgsType[] = { LLVMPointerType(LLVMInt8Type(), 0) };
LLVMTypeRef vFnPrintfType = LLVMFunctionType(LLVMInt32Type(),
vpFnPrintfArgsType, 1, 1);
LLVMValueRef vFnPrintf = LLVMAddFunction(modCEx, "printf", vFnPrintfType);
LLVMSetLinkage(vFnPrintf, LLVMExternalLinkage);
// printf with arguments
LLVMValueRef printfArgTemplate = LLVMBuildGlobalStringPtr(bldr, template,
"privateString");
LLVMValueRef vpArgsPrintf[] = { printfArgTemplate, testValue };
LLVMBuildCall(bldr, vFnPrintf, vpArgsPrintf, 2, "printf");
LLVMBuildRet(bldr, LLVMConstInt(LLVMInt32Type(), 0, 0));
// Dump Code:
printf("\n[llvmPrintf# %s] Verifying Function:\n", moduleName);
LLVMVerifyFunction(vFnMain, LLVMPrintMessageAction);
printf("\n[llvmPrintf# %s] Dumping LLVM generated Code:\n", moduleName);
LLVMDumpModule(modCEx);
// Compile and JIT above generated function:
LLVMInitializeNativeTarget();
LLVMExecutionEngineRef engExe;
char * pErr = NULL;
LLVMCreateJITCompilerForModule(&engExe, modCEx, 4, &pErr);
if (pErr) {
printf("[Creating JIT Compiler] Error: %s\n", pErr);
LLVMDisposeMessage(pErr);
exit(0);
}
LLVMPassManagerRef rPassMgr = LLVMCreatePassManager();
LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engExe), rPassMgr);
LLVMAddConstantPropagationPass(rPassMgr);
LLVMAddInstructionCombiningPass(rPassMgr);
LLVMAddPromoteMemoryToRegisterPass(rPassMgr);
LLVMAddGVNPass(rPassMgr);
LLVMAddCFGSimplificationPass(rPassMgr);
LLVMRunPassManager(rPassMgr, modCEx);
printf("\n[llvmPrintf# %s] Dumping OPTIMIZED LLVM generated Code\n",
moduleName);
LLVMDumpModule(modCEx);
// printf Executing:
printf("\n[llvmPrintf# %s] Executing generated Code\n", moduleName);
LLVMRunFunctionAsMain(engExe, vFnMain, 0, NULL, NULL);
printf("\n[llvmPrintf# %s] Finished executing generated Code\n",
moduleName);
LLVMDisposeBuilder(bldr);
LLVMDisposeExecutionEngine(engExe);
}
--
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.
More information about the llvm-bugs
mailing list