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 pathJLangLabeledExt.java
43 lines (33 loc) · 1.55 KB
/
JLangLabeledExt.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
//Copyright (C) 2018 Cornell University
package jlang.extension;
import org.bytedeco.javacpp.LLVM.LLVMBasicBlockRef;
import jlang.ast.JLangExt;
import jlang.visit.LLVMTranslator;
import polyglot.ast.Labeled;
import polyglot.ast.Node;
import polyglot.util.SerialVersionUID;
import static org.bytedeco.javacpp.LLVM.LLVMBuildBr;
import static org.bytedeco.javacpp.LLVM.LLVMPositionBuilderAtEnd;
public class JLangLabeledExt extends JLangExt {
private static final long serialVersionUID = SerialVersionUID.generate();
@Override
public Node overrideTranslateLLVM(Node parent, LLVMTranslator v) {
Labeled n = (Labeled) node();
// Any statement can be labeled. We need to make sure that the translation of a labeled
// statement starts and ends at basic block boundaries so that break and continue statements
// work as expected. (This may only be relevant for Java blocks.)
//
// Note that some loop translations override the head block so that continue
// statements jump to the correct part of the loop, skipping initialization statements.
LLVMBasicBlockRef head = v.utils.buildBlock(n.label() + ".head");
LLVMBasicBlockRef end = v.utils.buildBlock(n.label() + ".end");
LLVMBuildBr(v.builder, head);
LLVMPositionBuilderAtEnd(v.builder, head);
v.pushLabel(n.label(), head, end);
n.visitChild(n.statement(), v);
v.popLabel(n.label());
v.utils.branchUnlessTerminated(end);
LLVMPositionBuilderAtEnd(v.builder, end);
return n;
}
}