Skip to content

Commit

Permalink
Remove goto after return (this makes the JVM mad)
Browse files Browse the repository at this point in the history
`function2.decaf` now compiles.
  • Loading branch information
hawkw committed Dec 28, 2014
1 parent 4910560 commit 6daab01
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/scala/decaf/backend/JasminBackend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,14 @@ object JasminBackend extends Backend{
("\t" * tabLevel) + s"If$label:\n" +
("\t" * tabLevel) + s".line ${testBody.pos.line}\n" +
emit(className,testBody,localVars,tabLevel + 1, breakable) +
("\t" * (tabLevel + 1)) + s"goto\tIfDone$label\n" +
(testBody match{
case StmtBlock(_,stmts,_) =>
if (!stmts.last.isInstanceOf[ReturnStmt])
("\t" * (tabLevel + 1)) + s"goto\tIfDone$label\n"
else ""
case ReturnStmt(_,_) => ""
case _ => ("\t" * (tabLevel + 1)) + s"goto\tIfDone$label\n"
}) +
("\t" * tabLevel) + s"IfElse$label:\n" +
("\t" * tabLevel) + s".line ${elseBody.pos.line}\n" +
emit(className,elseBody,localVars,tabLevel + 1, breakable) +
Expand Down
131 changes: 131 additions & 0 deletions src/test/resources/lab4-samples/passed/function2.j
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
.source function2.decaf
.class public function2
.super java/lang/Object
;
; standard initializer (calls java.lang.Object's initializer)
;
.method public <init>()V
aload_0
invokenonvirtual java/lang/Object/<init>()V
return
.end method
.method public static readInt()I
.limit stack 5
.limit locals 1
.line 4
ReadIntBegin:
new java/io/BufferedReader
dup
new java/io/InputStreamReader
dup
getstatic java/lang/System/in Ljava/io/InputStream;
invokespecial java/io/InputStreamReader/<init>(Ljava/io/InputStream;)V
invokespecial java/io/BufferedReader/<init>(Ljava/io/Reader;)V
invokevirtual java/io/BufferedReader/readLine()Ljava/lang/String;
invokestatic java/lang/Integer/parseInt(Ljava/lang/String;)I
ReadIntReturn:
ireturn
ReadIntCatch:
astore_0
iconst_0
ireturn
.catch java/lang/Exception from ReadIntBegin to ReadIntReturn using ReadIntCatch
.end method

.method public static readLine()Ljava/lang/String;
.limit stack 5
.limit locals 1
ReadLineBegin:
new java/io/BufferedReader
dup
new java/io/InputStreamReader
dup
getstatic java/lang/System/in Ljava/io/InputStream;
invokespecial java/io/InputStreamReader/<init>(Ljava/io/InputStream;)V
invokespecial java/io/BufferedReader/<init>(Ljava/io/Reader;)V
invokevirtual java/io/BufferedReader/readLine()Ljava/lang/String;
ReadLineReturn:
areturn
ReadLineCatch:
astore_0
ldc "Fail"
areturn
.catch java/lang/Exception from ReadLineBegin to ReadLineReturn using ReadLineCatch
.end method

.method public static f()I

.limit locals 0
.limit stack 5
BeginGlobal:
ldc 0x5
ldc 0xA
imul
ldc 0x4
iadd
ireturn

EndGlobal:
.end method
.method public static g(I)Ljava/lang/String;

.limit locals 1
.limit stack 5
.var 0 is a I from BeginGlobal to EndGlobal
BeginGlobal:
.line 6
iload 0
ldc 0x5
if_icmpeq CmpEQ80792139
ldc 0x0
goto CmpEQDone80792139
CmpEQ80792139:
ldc 0x1
CmpEQDone80792139:
ldc 0x1
if_icmpeq If111812458
goto IfElse111812458
If111812458:
.line 7
ldc "hello"
areturn
IfElse111812458:
.line 9
ldc "world"
areturn
IfDone111812458:

EndGlobal:
.end method
.method public static main([Ljava/lang/String;)V

.limit locals 2
.limit stack 5
BeginGlobal:
.var 0 is s Ljava/lang/String; from BeginGlobal to EndGlobal
.line 14
getstatic java/lang/System/out Ljava/io/PrintStream;
.line 14
invokestatic function2/f()I
invokevirtual java/io/PrintStream/print(I)V


.line 15
ldc 0x4
ldc 0x5
imul
ldc 0x2
idiv
invokestatic function2/g(I)Ljava/lang/String;
astore 0

.line 16
getstatic java/lang/System/out Ljava/io/PrintStream;
aload 0
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V


EndGlobal:
return
.end method

0 comments on commit 6daab01

Please sign in to comment.