LLVM指针作为函数参数

时间:2019-06-07 17:00:24

标签: llvm llvm-ir

我正在尝试将全局变量作为参数传递给函数,并在函数内部修改此变量

我的IR代码如下

interface Control { 
    var id: String?
    var width: Number?
    var height: Number?

    fun toMap() = mutableMapOf("id" to id, "width" to width, "height" to height) as MutableMap<String, Any>
}
class ControlImpl() : Control {
    override var id: String? = "Hello World!"
    override var width: Number? = 314159
    override var height: Number? = 271
}

class TextBoxControl {
    var id: String? = null
    var width: Number? = null
    var height: Number? = null
    var text: String? = null

    // this could also be a static function called `createTextBoxControlFromMap`, non-static function, or thrown into an apply block or generic extension function
    operator fun invoke(map: Map<String, Any>) : TextBoxControl {
        for((k, v) in map) {
            val f = this::class.java.getDeclaredField(k)
            f.setAccessible(true)
            f.set(this, v)
        }
        return this;
    }
}

fun buildTextBoxControl(dataFromDb: String, options: Control) : TextBoxControl {
    return TextBoxControl()(options.toMap().apply { 
        put("text", dataFromDb) 
    })
}

fun main() {
    var control = buildTextBoxControl("String", ControlImpl().apply { 
        id = "id"
        width = 42
        height = 42 
    })
    println(control)
}

readln在单独的模块中定义为C函数

@n = common global i32 0

declare i32 @readln(i32)

declare i32 @writeln(i32)

define i32 @main() {
entry:
  %n = load i32, i32* @n, align 4
  %calltmp = call i32 @readln(i32 %n)
  %n1 = load i32, i32* @n, align 4
  %calltmp2 = call i32 @writeln(i32 %n1)
  ret i32 0
}

运行时,我得到int readln(int * x) { return scanf("%d", x); } 我在做什么错了?

1 个答案:

答案 0 :(得分:0)

评论正确。以此为灵感

Builder.CreateLoad(Builder.CreateIntToPtr(Value,Type::getInt32PtrTy(TheContext)), "ptr");
Builder.CreateIntToPtr(Value,Type::getInt32PtrTy(TheContext));
相关问题