Can't find what is wrong

5 views
Skip to first unread message

Maria Lemon Sjölander

unread,
Apr 15, 2011, 4:21:49 AM4/15/11
to Compiler construction 2011
I get an error:
"Unable to pop operand of an empty stack"

This is my generated code:

.method public static main()I
.limit locals 2
.limit stack 4

iconst_2
istore 0
iconst_5
istore 1
lab0:
iconst_1 //Push on constant on the stack
iload 0 //loads an item, stack = 2 now
iconst_0 //push constant on stack, stack = 3
if_icmpgt lab2 //Compares, pops the 2 topmost items so one
should be left on the stack
pop //At this point there should be one item
left on the stack bur apparently there isn't
iconst_0
lab2:
if_icmplt lab1
iload 1
iload 0
imul
istore 1
iinc 0 -1
goto lab0
lab1:
iconst_0
ireturn
.end method

Rasmus Knutsson

unread,
Apr 15, 2011, 4:54:49 AM4/15/11
to Compiler construction 2011
As far as I understand it, if_icmpgt will pop the stack whether it
jumps or not. You seem to assume it doesn't pop if it doesn't jump.

On Apr 15, 10:21 am, Maria Lemon Sjölander <maria.lemo...@gmail.com>
wrote:

Maria Lemon Sjölander

unread,
Apr 15, 2011, 4:57:59 AM4/15/11
to Compiler construction 2011
No thats not it, I push 3 items onto the stack if_icmpgt pops two of
them so there should be one item left on the stack when pop is called
or am I wrong?

Maria Lemon Sjölander

unread,
Apr 15, 2011, 5:06:12 AM4/15/11
to Compiler construction 2011
I spotted the error, when jumping to lab2 I only have one item on the
stack when if_icmplt lab1 expects two.

On Apr 15, 10:57 am, Maria Lemon Sjölander <maria.lemo...@gmail.com>
wrote:

Rasmus Knutsson

unread,
Apr 15, 2011, 5:08:04 AM4/15/11
to Compiler construction 2011
No, you are right, I see now. But in that case you will get into
trouble when you jump to lab2, because there will only be one value on
the stack.

On Apr 15, 10:57 am, Maria Lemon Sjölander <maria.lemo...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages