dschmenk / PLASMA

Proto Language AsSeMbler for All (formerly Apple)
MIT License
189 stars 26 forks source link

Bug in test.pla? #23

Closed ZornsLemma closed 6 years ago

ZornsLemma commented 6 years ago

Hi Dave,

I see you've been eating your vegetables/adding some # return value counts to the standard library. :-) Thanks, this is good stuff and will have a nice effect on code size in general I'm sure.

I've been porting these changes to my repository and I might have found a problem, but I'm not sure. I noticed this because I have some experimental ESTK under/overflow checking in my version of the VM, but I think I can illustrate the problem at the bytecode level.

Looking at the compiled version samplesrc/test.a, the line 'print:newln()' appear to be compiled as:

; <stdin>: 0047:     print:newln()
[...]
        !BYTE   $6A                     ; LAW   _X024+4
_F003   !WORD   0+4             
        !BYTE   $56                     ; ICAL
        !BYTE   $30                     ; DROP

You can see there's a DROP after the ICAL; I think this is because there's no #0 on that 'print:newln()' line. inc/testlib.plh declares print to have 0 return values ('word print(s)#0') but I am not sure that actually has any effect. However, none of the puti/puth/putln/puts/putc functions in the print[] array actually return a value any more, so the DROP will remove a value when none was left on the stack by the called function.

Does this make sense or have I lost the plot?

I think the same problem affects three lines, as shown in this diff:

diff --git a/src/samplesrc/test.pla b/src/samplesrc/test.pla
index f07cab7..be96122 100755
--- a/src/samplesrc/test.pla
+++ b/src/samplesrc/test.pla
@@ -42,9 +42,9 @@ def tens(start)#0
   pptr = @print
   repeat
     print:hex(i)#0
-    print:str("   ")
+    print:str("   ")#0
     pptr=>dec(i)#0
-    print:newln()
+    print:newln()#0
     i = i / 10
   until i == 0
 end
@@ -138,7 +138,7 @@ puti((@array)=>4);putc(' ')
 puti((@array)=>6);putc(' ')
 puti((@array)=>8);putln
 ptr = @main
-ptr(@array:6)
+ptr(@array:6)#0
 ptr = @array
 puti((ptr):6)
 putln

Cheers.

Steve

dschmenk commented 6 years ago

Many thanks, Steve. You are absolutely correct! This is what I get for editing code while watching Game of Thrones. It actually ran on the portable VM, but obviously the stack pointer is off. Thank heavens there are people like you to catch my blunders. I'm going to add some stack pointer tests in the portable VM because of this.

Dave...

dschmenk commented 6 years ago

Test cases fixed. Now for all the other modules...