Skip to content

Instantly share code, notes, and snippets.

@yutopp
Created June 19, 2020 20:34
Show Gist options
  • Save yutopp/90e5d50c01fcc1694b114cc6c9a5e9e7 to your computer and use it in GitHub Desktop.
Save yutopp/90e5d50c01fcc1694b114cc6c9a5e9e7 to your computer and use it in GitHub Desktop.
open! Base
module L = Llvm
module L_all_backends = Llvm_all_backends
module L_targets = Llvm_target
let initialize_if_not_initialized =
let initialized = ref false in
let f () =
if not !initialized then (
L_all_backends.initialize ();
initialized := true )
in
f
type t = { target : L_targets.Target.t }
let create_target triple m =
initialize_if_not_initialized ();
let target = L_targets.Target.by_triple triple in
let mc =
let cpu = "" in
let features = "" in
let level = L_targets.CodeGenOptLevel.Default in
let reloc_mode = L_targets.RelocMode.PIC in
let code_model = L_targets.CodeModel.Default in
L_targets.TargetMachine.create ~triple ~cpu ~features ~level ~reloc_mode
~code_model target
in
let data_layout = L_targets.TargetMachine.data_layout mc in
L.set_data_layout (L_targets.DataLayout.as_string data_layout) m;
let ft = L_targets.CodeGenFileType.ObjectFile in
let llbuf = L_targets.TargetMachine.emit_to_memory_buffer m ft mc in
Ok { target }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment