Skip to content

Commit

Permalink
added exception handling, stupid control structures in jimple are stupid
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Sep 20, 2012
1 parent f09cf21 commit 8d7388a
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 8 deletions.
7 changes: 7 additions & 0 deletions jack/native/jack.cpp
Expand Up @@ -84,6 +84,12 @@ void testInstanceOf() {
obj->m_test();
}

void testExceptions() {
jack_tests_Exceptions* obj = new jack_tests_Exceptions();
obj->m_init();
obj->m_simpleCatch();
}

int main() {
jack_gc_init();
jack_init();
Expand All @@ -92,4 +98,5 @@ int main() {
testReflection();
testPrimes();
testInstanceOf();
testExceptions();
}
4 changes: 3 additions & 1 deletion jack/src/com/badlogic/jack/generators/HeaderGenerator.java
Expand Up @@ -129,7 +129,9 @@ public void generate() {
}

private boolean needsUpdate(String fileName, String newContent) {
return !new FileDescriptor(fileName).readString().equals(newContent);
FileDescriptor file = new FileDescriptor(fileName);
if(!file.exists()) return true;
return !file.readString().equals(newContent);
}

private void generateMethod(SootMethod method, MethodInfo info) {
Expand Down
Expand Up @@ -72,6 +72,8 @@ public void generate() {
}

private boolean needsUpdate(String fileName, String newContent) {
return !new FileDescriptor(fileName).readString().equals(newContent);
FileDescriptor file = new FileDescriptor(fileName);
if(!file.exists()) return true;
return !file.readString().equals(newContent);
}
}
Expand Up @@ -38,7 +38,8 @@ public void generate() {
}

/**
* Declares all local variables.
* Declares all local variables plus a variable called _exception that
* holds a pointer to any exception thrown.
*/
private void declareLocals() {
// declare locals
Expand All @@ -54,5 +55,6 @@ private void declareLocals() {
}
writer.wl(cType + " " + local.getName() + " = 0;");
}
writer.wl("java_lang_Object* _exception = 0;");
}
}
72 changes: 67 additions & 5 deletions jack/src/com/badlogic/jack/generators/StatementGenerator.java
Expand Up @@ -15,6 +15,7 @@
import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.Trap;
import soot.Type;
import soot.Unit;
import soot.Value;
Expand Down Expand Up @@ -82,6 +83,7 @@
import soot.jimple.UshrExpr;
import soot.jimple.VirtualInvokeExpr;
import soot.jimple.XorExpr;
import soot.jimple.internal.JCaughtExceptionRef;
import soot.jimple.internal.JInstanceFieldRef;
import soot.shimple.toolkits.scalar.SEvaluator.MetaConstant;
import soot.tagkit.LineNumberTag;
Expand Down Expand Up @@ -110,16 +112,19 @@ public class StatementGenerator {
private final Map<Stmt, String> labels = new HashMap<Stmt, String>();
private int nextLabelId = 0;
private int lastEmittedSourceLine = 0;
private Map<Stmt, List<Trap>> tries = new HashMap<Stmt, List<Trap>>();
private Map<Stmt, List<Trap>> catches = new HashMap<Stmt, List<Trap>>();

public StatementGenerator(SourceWriter writer, JavaSourceProvider sourceProvider, ClassInfo info, SootMethod method) {
this.writer = writer;
this.sourceProvider = sourceProvider;
this.info = info;
this.body = (JimpleBody)method.retrieveActiveBody();
this.body = (JimpleBody)method.retrieveActiveBody();
}

public void generate() {
generateLabels();
generateTryCatches();
generateStatements();
}

Expand Down Expand Up @@ -148,8 +153,45 @@ private void generateLabels() {
newLabel((Stmt)target);
}
}
// FIXME exceptions labels for handlers
}
}

// generate label for begin/end statements of try/catch/finally
for(Trap trap: body.getTraps()) {
Stmt begin = (Stmt)trap.getBeginUnit();
Stmt end = (Stmt)trap.getEndUnit();
Stmt handler = (Stmt)trap.getHandlerUnit();
newLabel(begin);
newLabel(end);
newLabel(handler);
}
}

private void generateTryCatches() {
// generate label for begin/end statements of try/catch/finally
for(Trap trap: body.getTraps()) {
Stmt begin = (Stmt)trap.getBeginUnit();
Stmt end = (Stmt)trap.getEndUnit();
newTry(begin, trap);
newCatch(end, trap);
}
}

private void newTry(Stmt stmt, Trap trap) {
List<Trap> traps = tries.get(stmt);
if(traps == null) {
traps = new ArrayList<Trap>();
tries.put(stmt, traps);
}
traps.add(trap);
}

private void newCatch(Stmt stmt, Trap trap) {
List<Trap> traps = catches.get(stmt);
if(traps == null) {
traps = new ArrayList<Trap>();
catches.put(stmt, traps);
}
traps.add(trap);
}

/**
Expand Down Expand Up @@ -195,6 +237,14 @@ private void generateStatement(Stmt stmt) {
writer.push();
}

// emit tries if any
if(tries.containsKey(stmt)) {
List<Trap> traps = tries.get(stmt);
for(Trap trap: traps) {
writer.wl("try {");
}
}

// translate statements and emit as C++
if(stmt instanceof BreakpointStmt) {
// BreakpointStmt s = (BreakpointStmt)stmt;
Expand Down Expand Up @@ -248,7 +298,11 @@ else if(rightOp.getType() instanceof NullType) {
Value rightOp = s.getRightOp();
String l = translateValue(leftOp);
String r = translateValue(rightOp);
writer.wl(l + " = " + r + ";");
if(rightOp instanceof JCaughtExceptionRef) {
writer.wl(l + " = dynamic_cast<" + CTypes.toCType(leftOp.getType()) + ">(" + r + ");");
} else {
writer.wl(l + " = " + r + ";");
}
} else if(stmt instanceof GotoStmt) {
GotoStmt s = (GotoStmt)stmt;
String label = labels.get((Stmt)s.getTarget());
Expand Down Expand Up @@ -308,6 +362,14 @@ else if(rightOp.getType() instanceof NullType) {
} else {
throw new RuntimeException("Unkown statement " + stmt);
}

// emit catches if any
if(catches.containsKey(stmt)) {
List<Trap> traps = catches.get(stmt);
for(Trap trap: traps) {
writer.wl("} catch(" + Mangling.mangle(trap.getException()) + "* e) { _exception = e; goto " + labels.get(trap.getHandlerUnit()) + "; }");
}
}
}

private void generateSourceComment(Stmt stmt) {
Expand Down Expand Up @@ -411,7 +473,7 @@ private String translateRef(Ref val) {
} else if(val instanceof IdentityRef) {
IdentityRef v = (IdentityRef)val;
if(v instanceof ThisRef) return "this";
if(v instanceof CaughtExceptionRef) return "(0)"; // FIXME exceptions
if(v instanceof CaughtExceptionRef) return "_exception";
else return "param" + ((ParameterRef)v).getIndex();
} else throw new RuntimeException("Unknown Ref Value " + val);
}
Expand Down
30 changes: 30 additions & 0 deletions runtime/jack-tests/src/jack/tests/Exceptions.java
Expand Up @@ -13,6 +13,36 @@ public void simpleCatch() {
try {
checked();
} catch(Exception e) {
return;
}
}

public void controlCatch() {
try {
for(int i = 0; i < 10; i++) {
if(i == 3) {
return;
}
}
} catch(Throwable t) {

}
}

public void complexCatch() {
try {
try {
try {
checked();
} catch(ClassNotFoundException e) {

} catch(Throwable e) {

}
} catch(NullPointerException e) {

}
} catch(Exception e) {

}
}
Expand Down

0 comments on commit 8d7388a

Please sign in to comment.