This repository was archived by the owner on Jul 12, 2023. It is now read-only.
forked from polyglot-compiler/JLang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJLangLocalDeclExt.java
51 lines (41 loc) · 1.63 KB
/
JLangLocalDeclExt.java
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
//Copyright (C) 2018 Cornell University
package jlang.extension;
import polyglot.ast.LocalDecl;
import polyglot.ast.Node;
import polyglot.util.InternalCompilerError;
import polyglot.util.SerialVersionUID;
import java.lang.Override;
import jlang.ast.JLangExt;
import jlang.types.JLangLocalInstance;
import jlang.visit.LLVMTranslator;
import static org.bytedeco.javacpp.LLVM.*;
public class JLangLocalDeclExt extends JLangExt {
private static final long serialVersionUID = SerialVersionUID.generate();
@Override
public Node leaveTranslateLLVM(LLVMTranslator v) {
LocalDecl n = (LocalDecl) node();
JLangLocalInstance li = (JLangLocalInstance) n.localInstance().orig();
LLVMTypeRef typeRef = v.utils.toLL(n.declType());
LLVMValueRef translation;
if (li.isSSA()) {
// If SSA, just forward the initializer value.
if (n.init() == null)
throw new InternalCompilerError(
"Variable marked SSA, but the declaration has no initial value");
translation = v.getTranslation(n.init());
}
else {
// Otherwise, allocate on the stack.
translation = v.utils.buildAlloca(n.name(), typeRef);
if (n.init() != null) {
LLVMBuildStore(v.builder, v.getTranslation(n.init()), translation);
}
}
// Declare debug information if this variable is visible to the user.
if (!li.isTemp()) {
v.debugInfo.createLocalVariable(v, n, translation);
}
v.addTranslation(li, translation);
return super.leaveTranslateLLVM(v);
}
}