如何解决使用 OCaml 绑定在 LLVM IR 中定义新类型
我正在尝试使用 LLVM 和 OCaml 实现具有用户定义类型的静态类型语言,但我不知道如何将 newtype 定义实际添加到 LLVM 模块中。
我查看了 OCaml 文档,但没有找到任何相关功能。我已经尝试过 LLVM: Defining a new type via OCaml bindings 的回答,但没有用。
我尝试过的示例代码:
let c = Llvm.create_context () in
let m = Llvm.create_module c "MainModule" in
let llar = [| Llvm.i16_type c; Llvm.float_type c; |] in
let value_t = Llvm.struct_type c llar in
ignore (Llvm.declare_global value_t "value_t" m);
let llvalue_t = Llvm.named_struct_type c "value_t" in
Llvm.struct_set_body llvalue_t llar true;
ignore (Llvm.struct_type c llar);
编译输出:
; ModuleID = 'MainModule'
source_filename = "MainModule"
@value_t = external global { i16,float }
解决方法
OCaml 代码:
let () =
let c = Llvm.create_context () in
let m = Llvm.create_module c "MainModule" in
let struct1 = Llvm.named_struct_type c "B" in
Llvm.struct_set_body struct1 [| Llvm.pointer_type struct1; Llvm.i32_type c|] false;
let struct2 = Llvm.struct_type c [| Llvm.i32_type c; Llvm.pointer_type (Llvm.i8_type c) |] in
let main_t = Llvm.function_type (Llvm.void_type c) [| struct1; struct2 |] in
let _main = Llvm.declare_function "main" main_t m in
Llvm.print_module "./main.ll"
生成的LLVM IR:
; ModuleID = 'MainModule'
source_filename = "MainModule"
%B = type { %B*,i32 }
declare void @main(%B,{ i32,i8* })
还有这个 OCaml 代码:
let () =
let c = Llvm.create_context () in
let m = Llvm.create_module c "MainModule" in
let struct1 = Llvm.named_struct_type c "B" in
Llvm.struct_set_body struct1 [| Llvm.pointer_type struct1; Llvm.i32_type c|] false;
let _struct2 = Llvm.struct_type c [| Llvm.i32_type c; Llvm.pointer_type (Llvm.i8_type c) |] in
Llvm.print_module "./main.ll" m
生成的LLVM IR:
; ModuleID = 'MainModule'
source_filename = "MainModule"
因此,据我所知,新类型声明会自动插入到使用该结构的模块中。不仅在 OCaml 绑定中,而且在 C++ 中,您还有 StructType::create(LLVMContext &Context,StringRef Name)
,其用途相同。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。