/
llvm.cr
140 lines (119 loc) · 3.73 KB
/
llvm.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require "./llvm/**"
require "c/string"
module LLVM
@@initialized = false
def self.init_x86 : Nil
return if @@initialized_x86
@@initialized_x86 = true
{% if LibLLVM::BUILT_TARGETS.includes?(:x86) %}
LibLLVM.initialize_x86_target_info
LibLLVM.initialize_x86_target
LibLLVM.initialize_x86_target_mc
LibLLVM.initialize_x86_asm_printer
LibLLVM.initialize_x86_asm_parser
# LibLLVM.link_in_jit
LibLLVM.link_in_mc_jit
{% else %}
raise "ERROR: LLVM was built without X86 target"
{% end %}
end
def self.init_aarch64 : Nil
return if @@initialized_aarch64
@@initialized_aarch64 = true
{% if LibLLVM::BUILT_TARGETS.includes?(:aarch64) %}
LibLLVM.initialize_aarch64_target_info
LibLLVM.initialize_aarch64_target
LibLLVM.initialize_aarch64_target_mc
LibLLVM.initialize_aarch64_asm_printer
LibLLVM.initialize_aarch64_asm_parser
# LibLLVM.link_in_jit
LibLLVM.link_in_mc_jit
{% else %}
raise "ERROR: LLVM was built without AArch64 target"
{% end %}
end
def self.init_arm : Nil
return if @@initialized_arm
@@initialized_arm = true
{% if LibLLVM::BUILT_TARGETS.includes?(:arm) %}
LibLLVM.initialize_arm_target_info
LibLLVM.initialize_arm_target
LibLLVM.initialize_arm_target_mc
LibLLVM.initialize_arm_asm_printer
LibLLVM.initialize_arm_asm_parser
# LibLLVM.link_in_jit
LibLLVM.link_in_mc_jit
{% else %}
raise "ERROR: LLVM was built without ARM target"
{% end %}
end
def self.init_webassembly : Nil
return if @@initialized_webassembly
@@initialized_webassembly = true
{% if LibLLVM::BUILT_TARGETS.includes?(:webassembly) %}
LibLLVM.initialize_webassembly_target_info
LibLLVM.initialize_webassembly_target
LibLLVM.initialize_webassembly_target_mc
LibLLVM.initialize_webassembly_asm_printer
LibLLVM.initialize_webassembly_asm_parser
# LibLLVM.link_in_jit
LibLLVM.link_in_mc_jit
{% else %}
raise "ERROR: LLVM was built without WebAssembly target"
{% end %}
end
def self.start_multithreaded : Bool
if multithreaded?
true
else
LibLLVM.start_multithreaded != 0
end
end
def self.stop_multithreaded
if multithreaded?
LibLLVM.stop_multithreaded
end
end
def self.multithreaded? : Bool
LibLLVM.is_multithreaded != 0
end
def self.default_target_triple : String
chars = LibLLVM.get_default_target_triple
triple = string_and_dispose(chars)
if triple =~ /x86_64-apple-macosx|x86_64-apple-darwin/
"x86_64-apple-macosx"
else
triple
end
end
def self.host_cpu_name : String
{% unless LibLLVM::IS_LT_70 %}
String.new LibLLVM.get_host_cpu_name
{% else %}
raise "LibLLVM.host_cpu_name requires LLVM 7.0 or newer"
{% end %}
end
def self.normalize_triple(triple : String) : String
normalized = LibLLVMExt.normalize_target_triple(triple)
normalized = LLVM.string_and_dispose(normalized)
# Fix LLVM not replacing empty triple parts with "unknown"
# This was fixed in LLVM 8
normalized = normalized.split('-').map { |c| c.presence || "unknown" }.join('-')
normalized
end
def self.to_io(chars, io) : Nil
io.write_string Slice.new(chars, LibC.strlen(chars))
LibLLVM.dispose_message(chars)
end
def self.string_and_dispose(chars) : String
string = String.new(chars)
LibLLVM.dispose_message(chars)
string
end
{% unless LibLLVM::IS_LT_130 %}
def self.run_passes(module mod : Module, passes : String, target_machine : TargetMachine, options : PassBuilderOptions)
LibLLVM.run_passes(mod, passes, target_machine, options)
end
{% end %}
DEBUG_METADATA_VERSION = 3
end