簡體   English   中英

LLVM IR打印號碼

[英]LLVM IR printing a number

我正在嘗試打印數字,但是卻出現錯誤,提示我的打印功能不正確:

define i32 @main() {
entry:
  %d = shl i32 2, 3
  %call = call i32 (i8*, ...)* @printf(i8* %d)
  ret i32 1
}

declare i32 @printf(i8*, ...)

這是錯誤:

Error in compilation: /bin/this.program: llvm.ll:4:44: error: '%d' defined with type 'i8'
  %call = call i32 (i8*, ...)* @printf(i8* %d)
                                       ^

還有其他一些打印功能可以解決此問題嗎?

LLVM IR沒有隱式強制轉換(顯式強制轉換是單獨的指令)。 從第一條指令i32 ,您的%d變量的類型為i32 (奇怪的是,錯誤消息是'%d' defined with type 'i8' ,可能您的示例不是您的真實代碼?)。

至於printf函數,恰好是C printf 並且您應該傳遞完全相同的參數-格式字符串( i8*指向以null結尾的"%d" )和一個數字。

對於字符串,應定義全局

@formatString = private constant [2 x i8] c"%d" 

並將其作為第一個參數傳遞給printf

%call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @formatString , i32 0, i32 0), i32 %d)

完整代碼:

@formatString = private constant [2 x i8] c"%d" 

define i32 @main() {
entry:
  %d = shl i32 2, 3
  %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @formatString , i32 0, i32 0), i32 %d)
  ret i32 1
}

declare i32 @printf(i8*, ...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM