Skip to content
← Back

src/ir/llvm/llvm_linker.ghul

1
namespace IR.LLVMTarget is
2
/// Links an LLVM module's text into an executable with `ghul-llvm`, which
3
/// verifies, optimises and links it against the runtime.
4
///
5
/// `ghul-llvm` is found on the path unless `GHUL_LLVM` names it, and the
6
/// runtime it links against is the directory `GHUL_LLVM_RUNTIME` names,
7
/// where that is set.
8
class LLVM_LINKER is
9
/// Links `ll_path` into the executable `output`, throwing
10
/// `LinkFailedException` with what `ghul-llvm` said where it fails.
11
///
12
/// - ll_path: the module's text
13
/// - output: the executable to write
14
/// - optimisation_level: the level to optimise at, 0 to 3
15
@IF.dotnet()
16
link(ll_path: string, output: string, optimisation_level: int) static is
17
let start = System.Diagnostics.ProcessStartInfo(_linker)
18
19
for argument in _arguments(ll_path, output, optimisation_level) do
20
start.argument_list.add(argument)
21
od
22
23
start.redirect_standard_error = true
24
start.redirect_standard_output = true
25
start.use_shell_execute = false
26
27
let message mut = ""
28
29
try
30
let process = System.Diagnostics.Process.start(start)
31
32
if !process? then
33
throw LinkFailedException("ghul-llvm could not be started")
34
fi
35
36
let errors = process.standard_error.read_to_end_async()
37
let out = process.standard_output.read_to_end()
38
39
process.wait_for_exit()
40
41
if process.exit_code == 0 then
42
return
43
fi
44
45
message = "{errors.result}{out}".trim()
46
catch e: System.ComponentModel.Win32Exception
47
message = e.message
48
yrt
49
50
throw LinkFailedException("ghul-llvm failed: {message}")
51
si
52
53
/// Links `ll_path` into the executable `output`, throwing
54
/// `LinkFailedException` with what `ghul-llvm` said where it fails.
55
///
56
/// - ll_path: the module's text
57
/// - output: the executable to write
58
/// - optimisation_level: the level to optimise at, 0 to 3
59
@IF.not.dotnet()
60
link(ll_path: string, output: string, optimisation_level: int) static is
61
let message mut = ""
62
63
try
64
let result = IO.PROCESS.run(_linker, _arguments(ll_path, output, optimisation_level))
65
66
if result.exit_code == 0 then
67
return
68
fi
69
70
message = "{result.error}{result.output}".trim()
71
catch e: IO.FileNotFoundException
72
message = e.message
73
yrt
74
75
throw LinkFailedException("ghul-llvm failed: {message}")
76
si
77
78
_linker: string static => Driver.COMPILER_ENVIRONMENT.llvm_linker ?? "ghul-llvm"
79
80
_arguments(ll_path: string, output: string, optimisation_level: int) -> Collections.LIST[string] static is
81
let result = Collections.LIST[string]()
82
83
result.add(ll_path)
84
85
// ghul-llvm optimises at 2 unless told otherwise.
86
if optimisation_level != 2 then
87
result.add("-O{optimisation_level}")
88
fi
89
90
if let runtime = Driver.COMPILER_ENVIRONMENT.llvm_runtime then
91
result.add("-L")
92
result.add(runtime)
93
fi
94
95
result.add("-o")
96
result.add(output)
97
98
return result
99
si
100
si
101
102
/// Thrown where `ghul-llvm` cannot link a module into an executable.
103
class LinkFailedException: Ghul.Exception is
104
init(message: string) is
105
super.init(message)
106
si
107
si
108
si