使用OCaml绑定在LLVM IR中定义新类型
我试图使用LLVM和OCaml实现具有用户定义类型的静态类型语言,但我不知道如何将新类型定义添加到LLVM模块中 我已经查阅了OCaml文档,还没有找到任何相关的函数。我已经试过了他的答案,但不起作用 我尝试过的示例代码:使用OCaml绑定在LLVM IR中定义新类型,ocaml,llvm,llvm-ir,Ocaml,Llvm,Llvm Ir,我试图使用LLVM和OCaml实现具有用户定义类型的静态类型语言,但我不知道如何将新类型定义添加到LLVM模块中 我已经查阅了OCaml文档,还没有找到任何相关的函数。我已经试过了他的答案,但不起作用 我尝试过的示例代码: let c = Llvm.create_context () in let m = Llvm.create_module c "MainModule" in let llar = [| Llvm.i16_type c; Llvm.float_
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"
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"
%B = type { %B*, i32 }
declare void @main(%B, { i32, i8* })
; ModuleID = 'MainModule'
source_filename = "MainModule"
这是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"
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"
%B = type { %B*, i32 }
declare void @main(%B, { i32, i8* })
; ModuleID = 'MainModule'
source_filename = "MainModule"
所以据我所知,新类型声明会自动插入到使用结构的模块中。它不仅在OCAM绑定中,而且在C++中有<代码> StuttType::创建(LLVMWield&Curror,StringRef Name),它的目的相同。我认为<代码> NAMDED StultType type <代码>是为了获得命名类型,而不是创建一个命名类型。若是最新的,那个么我认为您是对的,并没有办法用OCaml绑定创建命名类型。不过,也许自己添加绑定并不难?LLVM C API似乎有必要的函数,
LLVMStructCreateNamed
。