-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
module.cr
122 lines (100 loc) · 2.85 KB
/
module.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
class LLVM::Module
# We let a module store a reference to the context so that if
# someone is still holding a reference to the module but not to
# the context, the context won't be disposed (if the context is disposed,
# the module will no longer be valid and segfaults will happen)
getter context : Context
{% if LibLLVM::IS_38 %}
def initialize(@unwrap : LibLLVM::ModuleRef, @name : String, @context : Context)
@owned = false
end
def name : String
@name
end
{% else %}
# LLVM >= 3.9
def initialize(@unwrap : LibLLVM::ModuleRef, @context : Context)
@owned = false
end
def name : String
bytes = LibLLVM.get_module_identifier(self, out bytesize)
String.new(Slice.new(bytes, bytesize))
end
def name=(name : String)
LibLLVM.set_module_identifier(self, name, name.bytesize)
end
{% end %}
def target=(target)
LibLLVM.set_target(self, target)
end
def data_layout=(data : TargetData)
{% if LibLLVM::IS_38 %}
LibLLVM.set_data_layout(self, data.to_data_layout_string)
{% else %}
# LLVM >= 3.9
LibLLVM.set_module_data_layout(self, data)
{% end %}
end
def dump
LibLLVM.dump_module(self)
end
def functions
FunctionCollection.new(self)
end
def globals
GlobalCollection.new(self)
end
def write_bitcode_to_file(filename : String)
LibLLVM.write_bitcode_to_file self, filename
end
{% unless LibLLVM::IS_38 || LibLLVM::IS_39 %}
def write_bitcode_with_summary_to_file(filename : String)
LibLLVMExt.write_bitcode_with_summary_to_file self, filename
end
{% end %}
def write_bitcode_to_memory_buffer
MemoryBuffer.new(LibLLVM.write_bitcode_to_memory_buffer self)
end
def write_bitcode_to_fd(fd : Int, should_close = false, buffered = false)
LibLLVM.write_bitcode_to_fd(self, fd, should_close ? 1 : 0, buffered ? 1 : 0)
end
def verify
error = LibLLVM.verify_module(self, LLVM::VerifierFailureAction::ReturnStatusAction, out message)
begin
if error == 1
raise "Module validation failed: #{String.new(message)}"
end
ensure
LibLLVM.dispose_message(message)
end
end
def print_to_file(filename)
if LibLLVM.print_module_to_file(self, filename, out error_msg) != 0
raise LLVM.string_and_dispose(error_msg)
end
self
end
def new_function_pass_manager
FunctionPassManager.new LibLLVM.create_function_pass_manager_for_module(self)
end
def add_named_metadata_operand(name : String, value : Value) : Nil
LibLLVM.add_named_metadata_operand(self, name, value)
end
def ==(other : self)
@unwrap == other.@unwrap
end
def to_s(io : IO) : Nil
LLVM.to_io(LibLLVM.print_module_to_string(self), io)
self
end
def to_unsafe
@unwrap
end
def take_ownership
if @owned
yield
else
@owned = true
end
end
end