我正在尝试将字符串转换为LLVM汇编代码中的整数。代码适用于atoi,但我想切换到strtol。
这是代码:
; initialise a number
@number0 = private unnamed_addr constant [2 x i8] c"5\00"
%str = getelementptr [2 x i8]* @number0, i64 0, i64 0
; the endpointer that indicates an error
%endptr = alloca i8*
; the actual call of strtol
%addr = getelementptr i8* %str, i64 0
%new_long = call i64 @strtol(i8* %addr, i8** %endptr)
; debug printing
%after_casting = getelementptr [18 x i8]* @after_casting, i64 0, i64 0
call i64(i8*, ...)* @printf(i8* %after_casting, i64 %new_long)
现在,debug printf消息打印0.我猜错了endptr传递。我做错了什么?
答案 0 :(得分:0)
当想知道这样的事情时,只需使用LLVM IR发射运行Clang。例如这个C代码:
int main ()
{
char szNumbers[] = "2001";
char * pEnd;
long int li1;
li1 = strtol (szNumbers,&pEnd,10);
printf ("%ld\n", li1);
return 0;
}
变成这个IR:
@main.szNumbers = private unnamed_addr constant [5 x i8] c"2001\00", align 1
@.str = private unnamed_addr constant [5 x i8] c"%ld\0A\00", align 1
; Function Attrs: nounwind uwtable
define i32 @main() #0 {
entry:
%szNumbers = alloca [5 x i8], align 1
%pEnd = alloca i8*, align 8
%0 = getelementptr inbounds [5 x i8]* %szNumbers, i64 0, i64 0
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* getelementptr inbounds ([5 x i8]* @main.szNumbers, i64 0, i64 0), i64 5, i32 1, i1 false)
%call = call i64 @strtol(i8* %0, i8** %pEnd, i32 10) #1
%call1 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([5 x i8]* @.str, i64 0, i64 0), i64 %call) #1
ret i32 0
}