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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
package org.shedlang.compiler.backends.llvm;
import org.pcollections.PVector;
import org.shedlang.compiler.backends.Backend;
import org.shedlang.compiler.backends.PatioBackends;
import org.shedlang.compiler.backends.llvm.codegen.PatioToLlvm;
import org.shedlang.compiler.backends.llvm.codegen.PatioToLlvmGlobalContext;
import org.shedlang.compiler.backends.llvm.ir.ast.LlvmBasicBlock;
import org.shedlang.compiler.backends.llvm.ir.ast.LlvmFunctionDefinition;
import org.shedlang.compiler.backends.llvm.ir.ast.LlvmModule;
import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmCall;
import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmReturn;
import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmTrunc;
import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmVariableGlobal;
import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmVariableLocal;
import org.shedlang.compiler.backends.llvm.ir.ast.types.LlvmTypes;
import org.shedlang.compiler.backends.paths.TargetDir;
import org.shedlang.compiler.backends.targets.NativeTarget;
import org.shedlang.compiler.config.ProfileConfig;
import org.shedlang.compiler.config.ProjectConfig;
import org.shedlang.compiler.patio.PatioCompilationUnit;
import org.shedlang.compiler.runtime.ShedRuntime;
import org.shedlang.compiler.shid.ShidNamespace;
import org.shedlang.compiler.typechecker.TypeInfos;
import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.util.P;
import org.shedlang.compiler.util.processes.ProcessError;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.shedlang.compiler.util.processes.ProcessRunner.runInheritIo;
public class LlvmBackend implements Backend {
public static final LlvmBackend AMD64 = new LlvmBackend(NativeTarget.AMD64);
public static final LlvmBackend I686 = new LlvmBackend(NativeTarget.I686);
public static LlvmBackend forTarget(NativeTarget target) {
return new LlvmBackend(target);
}
private final NativeTarget target;
private LlvmBackend(NativeTarget target) {
this.target = target;
}
@Override
public String targetName() {
return this.target.targetName();
}
@Override
public void compile(
ProjectConfig config,
ProfileConfig profileConfig,
List<ShidNamespace> namespaces,
TypeInfos typeInfos,
TargetDir targetDir
) throws IOException, InterruptedException {
PatioBackends.compile(
config,
profileConfig,
namespaces,
typeInfos,
new PatioToBinary(
targetDir,
P.vector(),
PatioToLlvmGlobalContext.empty(this.target)
)
);
}
private class PatioToBinary implements PatioBackends.PatioToTarget {
private final TargetDir targetDir;
private final PVector<Path> objectFilePaths;
private final PatioToLlvmGlobalContext patioToLlvmGlobalContext;
public PatioToBinary(
TargetDir targetDir,
PVector<Path> objectFilePaths,
PatioToLlvmGlobalContext patioToLlvmGlobalContext
) {
this.targetDir = targetDir;
this.objectFilePaths = objectFilePaths;
this.patioToLlvmGlobalContext = patioToLlvmGlobalContext;
}
@Override
public PatioBackends.PatioToTarget namespace(
NamespaceName namespaceName,
PatioCompilationUnit patioCompilationUnit
) throws IOException, InterruptedException {
var llvmModuleResult = PatioToLlvm.generateCompilationUnit(
patioCompilationUnit,
this.patioToLlvmGlobalContext
);
var llvmModule = llvmModuleResult.value();
var patioToLlvmGlobalContext = llvmModuleResult.context();
var objectPath = LlvmCompiler.compileLlvmModule(
llvmModule,
targetDir.namespace(namespaceName)
);
return new PatioToBinary(
this.targetDir,
this.objectFilePaths.plus(objectPath),
patioToLlvmGlobalContext
);
}
@Override
public void bin(
String binName,
PatioCompilationUnit patioCompilationUnit,
String mainFunctionName,
ProfileConfig profileConfig
) throws IOException, InterruptedException {
var linkInputs = new ArrayList<>(objectFilePaths);
var mainModule = generateMain(
patioCompilationUnit,
mainFunctionName,
patioToLlvmGlobalContext
);
var mainObjectPath = LlvmCompiler.compileLlvmModule(mainModule, targetDir.bin(binName));
linkInputs.add(mainObjectPath);
var shedRuntime = ShedRuntime.runtime(
target.targetName(),
profileConfig.gc(),
targetDir
);
link(
linkInputs,
targetDir.topLevel(binName),
shedRuntime
);
}
}
public void link(
List<Path> inputs,
Path outputPath,
ShedRuntime shedRuntime
) throws ProcessError, InterruptedException {
var clangArgs = new ArrayList<String>();
clangArgs.add("clang");
clangArgs.add("-o");
clangArgs.add(outputPath.toString());
for (var input : inputs) {
clangArgs.add(input.toString());
}
clangArgs.add(shedRuntime.dir().resolve("libpatio.a").toString());
clangArgs.add(shedRuntime.dir().resolve("libshed.a").toString());
clangArgs.addAll(this.target.clangFlags());
runInheritIo(clangArgs);
}
private static LlvmModule generateMain(
PatioCompilationUnit patioCompilationUnit,
String mainFunctionName,
PatioToLlvmGlobalContext patioToLlvmGlobalContext
) {
var llvmModuleResult = PatioToLlvm.generateCompilationUnit(
patioCompilationUnit,
patioToLlvmGlobalContext
);
var llvmModule = llvmModuleResult.value();
llvmModule = llvmModule.addTopLevelEntity(new LlvmFunctionDefinition(
"main",
LlvmTypes.INT_32,
List.of(),
List.of(
new LlvmBasicBlock(
Optional.empty(),
List.of(
new LlvmCall(
new LlvmVariableLocal("exitCode"),
LlvmTypes.INT_64,
new LlvmVariableGlobal(mainFunctionName),
List.of()
),
new LlvmTrunc(
new LlvmVariableLocal("exitCode32"),
LlvmTypes.INT_64,
new LlvmVariableLocal("exitCode"),
LlvmTypes.INT_32
)
),
new LlvmReturn(LlvmTypes.INT_32, new LlvmVariableLocal("exitCode32"))
)
)
));
return llvmModule;
}
}
|