-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtarget.jou
79 lines (67 loc) · 2.47 KB
/
target.jou
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
# LLVM makes a mess of how to define what kind of computer will run the
# compiled programs. Sometimes it wants a target triple, sometimes a
# data layout. Sometimes it wants a string, sometimes an object
# representing the thing.
#
# This file aims to provide everything you may ever need. Hopefully it
# will make the mess slightly less miserable to you. Just use the global
# "target" variable, it contains everything you will ever need.
import "./llvm.jou"
import "stdlib/str.jou"
import "stdlib/io.jou"
import "stdlib/process.jou"
class Target:
triple: byte[100]
data_layout: byte[500]
target: LLVMTarget*
target_machine: LLVMTargetMachine*
target_data: LLVMTargetData*
global target: Target
def init_target() -> None:
LLVMInitializeX86TargetInfo()
LLVMInitializeX86Target()
LLVMInitializeX86TargetMC()
LLVMInitializeX86AsmParser()
LLVMInitializeX86AsmPrinter()
if MACOS:
# Support the new M1 macs. This will enable the target also on x86_64
# macs, but it doesn't matter.
LLVMInitializeAArch64TargetInfo()
LLVMInitializeAArch64Target()
LLVMInitializeAArch64TargetMC()
LLVMInitializeAArch64AsmParser()
LLVMInitializeAArch64AsmPrinter()
if WINDOWS:
# LLVM's default is x86_64-pc-windows-msvc
target.triple = "x86_64-pc-windows-gnu"
else:
triple = LLVMGetDefaultTargetTriple()
assert strlen(triple) < sizeof target.triple
strcpy(target.triple, triple)
LLVMDisposeMessage(triple)
error: byte* = NULL
if LLVMGetTargetFromTriple(target.triple, &target.target, &error) != 0:
assert error != NULL
fprintf(stderr, "LLVMGetTargetFromTriple(\"%s\") failed: %s\n", target.triple, error)
exit(1)
assert error == NULL
assert target.target != NULL
target.target_machine = LLVMCreateTargetMachine(
target.target,
target.triple,
"",
"",
LLVMCodeGenOptLevel.Default,
LLVMRelocMode.PIC,
LLVMCodeModel.Default,
)
assert target.target_machine != NULL
target.target_data = LLVMCreateTargetDataLayout(target.target_machine)
assert target.target_data != NULL
tmp = LLVMCopyStringRepOfTargetData(target.target_data)
assert strlen(tmp) < sizeof target.data_layout
strcpy(target.data_layout, tmp)
LLVMDisposeMessage(tmp)
def cleanup_target() -> None:
LLVMDisposeTargetMachine(target.target_machine)
LLVMDisposeTargetData(target.target_data)