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 pathJLangArrayInitExt.java
48 lines (37 loc) · 1.59 KB
/
JLangArrayInitExt.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
//Copyright (C) 2018 Cornell University
package jlang.extension;
import polyglot.ast.ArrayInit;
import polyglot.ast.Expr;
import polyglot.ast.Node;
import polyglot.util.InternalCompilerError;
import polyglot.util.SerialVersionUID;
import java.lang.Override;
import jlang.ast.JLangExt;
import jlang.visit.LLVMTranslator;
import static org.bytedeco.javacpp.LLVM.*;
public class JLangArrayInitExt extends JLangExt {
private static final long serialVersionUID = SerialVersionUID.generate();
@Override
public Node leaveTranslateLLVM(LLVMTranslator v) {
ArrayInit n = (ArrayInit) node();
// Normally an empty ArrayInit could have ts.Null() as its base type,
// but we fixed that in an earlier pass using the expected type of
// this node with respect to its parent.
if (!n.type().isArray())
throw new InternalCompilerError("ArrayInit node does not have an array type");
LLVMValueRef len = LLVMConstInt(
v.utils.toLL(v.ts.Int()), n.elements().size(), /*signExtend*/ 0);
LLVMValueRef array = JLangNewArrayExt.translateNew1DArray(v, len, n.type().toArray());
if (!n.elements().isEmpty()) {
LLVMValueRef base = v.obj.buildArrayBaseElementPtr(array, n.type().toArray());
int idx = 0;
for (Expr expr : n.elements()) {
LLVMValueRef gep = v.utils.buildGEP(base, idx);
LLVMBuildStore(v.builder, v.getTranslation(expr), gep);
++idx;
}
}
v.addTranslation(n, array);
return super.leaveTranslateLLVM(v);
}
}