From c44cb02b96f25420caff09ba8825f7c5c9f9c6b5 Mon Sep 17 00:00:00 2001 From: Jesper Gravgaard Date: Thu, 27 Feb 2020 13:26:30 +0100 Subject: [PATCH] Fixed parameter offsets on stack by reversing order. --- .../kickc/model/CallingConventionStack.java | 8 +++++++- .../passes/PassNCallingConventionStack.java | 2 +- .../ref/procedure-callingconvention-stack-0.asm | 4 ++-- .../ref/procedure-callingconvention-stack-0.log | 16 ++++++++-------- .../ref/procedure-callingconvention-stack-0.sym | 4 ++-- .../ref/procedure-callingconvention-stack-1.asm | 4 ++-- .../ref/procedure-callingconvention-stack-1.log | 16 ++++++++-------- .../ref/procedure-callingconvention-stack-1.sym | 4 ++-- .../ref/procedure-callingconvention-stack-2.asm | 4 ++-- .../ref/procedure-callingconvention-stack-2.log | 16 ++++++++-------- .../ref/procedure-callingconvention-stack-2.sym | 4 ++-- .../ref/procedure-callingconvention-stack-3.asm | 4 ++-- .../ref/procedure-callingconvention-stack-3.log | 16 ++++++++-------- .../ref/procedure-callingconvention-stack-3.sym | 4 ++-- .../ref/procedure-callingconvention-stack-4.asm | 4 ++-- .../ref/procedure-callingconvention-stack-4.log | 16 ++++++++-------- .../ref/procedure-callingconvention-stack-4.sym | 4 ++-- 17 files changed, 68 insertions(+), 62 deletions(-) diff --git a/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java b/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java index d08260f44..2f7aefa69 100644 --- a/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java +++ b/src/main/java/dk/camelot64/kickc/model/CallingConventionStack.java @@ -5,6 +5,9 @@ import dk.camelot64.kickc.model.types.SymbolType; import dk.camelot64.kickc.model.values.ConstantInteger; import dk.camelot64.kickc.model.values.ConstantRef; +import java.util.ArrayList; +import java.util.Collections; + /** * Utility methods for {@link Procedure.CallingConvention#STACK_CALL} */ @@ -94,7 +97,10 @@ public class CallingConventionStack { */ public static long getParameterByteOffset(Procedure procedure, Variable parameter) { long byteOffset = 0; - for(Variable procedureParameter : procedure.getParameters()) { + ArrayList parameters = new ArrayList<>(procedure.getParameters()); + // Reverse because pushing is done in parameter order + Collections.reverse(parameters); + for(Variable procedureParameter : parameters) { if(parameter.equals(procedureParameter)) { break; } else { diff --git a/src/main/java/dk/camelot64/kickc/passes/PassNCallingConventionStack.java b/src/main/java/dk/camelot64/kickc/passes/PassNCallingConventionStack.java index 1ed8b4796..401f465dc 100644 --- a/src/main/java/dk/camelot64/kickc/passes/PassNCallingConventionStack.java +++ b/src/main/java/dk/camelot64/kickc/passes/PassNCallingConventionStack.java @@ -37,7 +37,7 @@ public class PassNCallingConventionStack extends Pass2SsaOptimization { createStackBase = true; } // Introduce the return value offset - if(procedure.getReturnType() != null) { + if(procedure.getReturnType() != null && !SymbolType.VOID.equals(procedure.getReturnType())) { CallingConventionStack.getReturnOffsetConstant(procedure); createStackBase = true; } diff --git a/src/test/ref/procedure-callingconvention-stack-0.asm b/src/test/ref/procedure-callingconvention-stack-0.asm index 448a4d451..2dea9d040 100644 --- a/src/test/ref/procedure-callingconvention-stack-0.asm +++ b/src/test/ref/procedure-callingconvention-stack-0.asm @@ -20,8 +20,8 @@ main: { } // plus(byte zp(2) a, byte register(A) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 2 tsx diff --git a/src/test/ref/procedure-callingconvention-stack-0.log b/src/test/ref/procedure-callingconvention-stack-0.log index 6ed7122c2..e9ce5d9db 100644 --- a/src/test/ref/procedure-callingconvention-stack-0.log +++ b/src/test/ref/procedure-callingconvention-stack-0.log @@ -205,8 +205,8 @@ main: { // plus // plus(byte zp(3) a, byte zp(4) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 3 .label b = 4 @@ -322,8 +322,8 @@ main: { // plus // plus(byte zp(2) a, byte register(A) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 2 // [10] (byte) plus::a#0 ← stackidx(byte,(const byte) plus::OFFSET_STACK_A) -- vbuz1=_stackidxbyte_vbuc1 @@ -380,8 +380,8 @@ FINAL SYMBOL TABLE (label) main::@return __stackcall (byte()) plus((byte) plus::a , (byte) plus::b) (label) plus::@return -(const byte) plus::OFFSET_STACK_A = (byte) 0 -(const byte) plus::OFFSET_STACK_B = (byte) 1 +(const byte) plus::OFFSET_STACK_A = (byte) 1 +(const byte) plus::OFFSET_STACK_B = (byte) 0 (const byte) plus::OFFSET_STACK_RETURN = (byte) 1 (byte) plus::a (byte) plus::a#0 a zp[1]:2 2.0 @@ -444,8 +444,8 @@ main: { // plus // plus(byte zp(2) a, byte register(A) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 2 // [10] (byte) plus::a#0 ← stackidx(byte,(const byte) plus::OFFSET_STACK_A) -- vbuz1=_stackidxbyte_vbuc1 diff --git a/src/test/ref/procedure-callingconvention-stack-0.sym b/src/test/ref/procedure-callingconvention-stack-0.sym index 5a8eed008..176d2a699 100644 --- a/src/test/ref/procedure-callingconvention-stack-0.sym +++ b/src/test/ref/procedure-callingconvention-stack-0.sym @@ -8,8 +8,8 @@ (label) main::@return __stackcall (byte()) plus((byte) plus::a , (byte) plus::b) (label) plus::@return -(const byte) plus::OFFSET_STACK_A = (byte) 0 -(const byte) plus::OFFSET_STACK_B = (byte) 1 +(const byte) plus::OFFSET_STACK_A = (byte) 1 +(const byte) plus::OFFSET_STACK_B = (byte) 0 (const byte) plus::OFFSET_STACK_RETURN = (byte) 1 (byte) plus::a (byte) plus::a#0 a zp[1]:2 2.0 diff --git a/src/test/ref/procedure-callingconvention-stack-1.asm b/src/test/ref/procedure-callingconvention-stack-1.asm index 448a4d451..2dea9d040 100644 --- a/src/test/ref/procedure-callingconvention-stack-1.asm +++ b/src/test/ref/procedure-callingconvention-stack-1.asm @@ -20,8 +20,8 @@ main: { } // plus(byte zp(2) a, byte register(A) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 2 tsx diff --git a/src/test/ref/procedure-callingconvention-stack-1.log b/src/test/ref/procedure-callingconvention-stack-1.log index 6ed7122c2..e9ce5d9db 100644 --- a/src/test/ref/procedure-callingconvention-stack-1.log +++ b/src/test/ref/procedure-callingconvention-stack-1.log @@ -205,8 +205,8 @@ main: { // plus // plus(byte zp(3) a, byte zp(4) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 3 .label b = 4 @@ -322,8 +322,8 @@ main: { // plus // plus(byte zp(2) a, byte register(A) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 2 // [10] (byte) plus::a#0 ← stackidx(byte,(const byte) plus::OFFSET_STACK_A) -- vbuz1=_stackidxbyte_vbuc1 @@ -380,8 +380,8 @@ FINAL SYMBOL TABLE (label) main::@return __stackcall (byte()) plus((byte) plus::a , (byte) plus::b) (label) plus::@return -(const byte) plus::OFFSET_STACK_A = (byte) 0 -(const byte) plus::OFFSET_STACK_B = (byte) 1 +(const byte) plus::OFFSET_STACK_A = (byte) 1 +(const byte) plus::OFFSET_STACK_B = (byte) 0 (const byte) plus::OFFSET_STACK_RETURN = (byte) 1 (byte) plus::a (byte) plus::a#0 a zp[1]:2 2.0 @@ -444,8 +444,8 @@ main: { // plus // plus(byte zp(2) a, byte register(A) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 2 // [10] (byte) plus::a#0 ← stackidx(byte,(const byte) plus::OFFSET_STACK_A) -- vbuz1=_stackidxbyte_vbuc1 diff --git a/src/test/ref/procedure-callingconvention-stack-1.sym b/src/test/ref/procedure-callingconvention-stack-1.sym index 5a8eed008..176d2a699 100644 --- a/src/test/ref/procedure-callingconvention-stack-1.sym +++ b/src/test/ref/procedure-callingconvention-stack-1.sym @@ -8,8 +8,8 @@ (label) main::@return __stackcall (byte()) plus((byte) plus::a , (byte) plus::b) (label) plus::@return -(const byte) plus::OFFSET_STACK_A = (byte) 0 -(const byte) plus::OFFSET_STACK_B = (byte) 1 +(const byte) plus::OFFSET_STACK_A = (byte) 1 +(const byte) plus::OFFSET_STACK_B = (byte) 0 (const byte) plus::OFFSET_STACK_RETURN = (byte) 1 (byte) plus::a (byte) plus::a#0 a zp[1]:2 2.0 diff --git a/src/test/ref/procedure-callingconvention-stack-2.asm b/src/test/ref/procedure-callingconvention-stack-2.asm index 4e6170361..2a23974dd 100644 --- a/src/test/ref/procedure-callingconvention-stack-2.asm +++ b/src/test/ref/procedure-callingconvention-stack-2.asm @@ -32,8 +32,8 @@ main: { } // plus(word zp(2) a, word zp(4) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 2 + .const OFFSET_STACK_A = 2 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 2 .label a = 2 .label b = 4 diff --git a/src/test/ref/procedure-callingconvention-stack-2.log b/src/test/ref/procedure-callingconvention-stack-2.log index e88b66402..ae87a0b51 100644 --- a/src/test/ref/procedure-callingconvention-stack-2.log +++ b/src/test/ref/procedure-callingconvention-stack-2.log @@ -232,8 +232,8 @@ main: { // plus // plus(word zp(4) a, word zp(6) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 2 + .const OFFSET_STACK_A = 2 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 2 .label a = 4 .label b = 6 @@ -364,8 +364,8 @@ main: { // plus // plus(word zp(2) a, word zp(4) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 2 + .const OFFSET_STACK_A = 2 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 2 .label a = 2 .label b = 4 @@ -437,8 +437,8 @@ FINAL SYMBOL TABLE (label) main::@return __stackcall (word()) plus((word) plus::a , (word) plus::b) (label) plus::@return -(const byte) plus::OFFSET_STACK_A = (byte) 0 -(const byte) plus::OFFSET_STACK_B = (byte) 2 +(const byte) plus::OFFSET_STACK_A = (byte) 2 +(const byte) plus::OFFSET_STACK_B = (byte) 0 (const byte) plus::OFFSET_STACK_RETURN = (byte) 2 (word) plus::a (word) plus::a#0 a zp[2]:2 2.0 @@ -511,8 +511,8 @@ main: { // plus // plus(word zp(2) a, word zp(4) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 2 + .const OFFSET_STACK_A = 2 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 2 .label a = 2 .label b = 4 diff --git a/src/test/ref/procedure-callingconvention-stack-2.sym b/src/test/ref/procedure-callingconvention-stack-2.sym index f2f026c3b..8e1b5048c 100644 --- a/src/test/ref/procedure-callingconvention-stack-2.sym +++ b/src/test/ref/procedure-callingconvention-stack-2.sym @@ -8,8 +8,8 @@ (label) main::@return __stackcall (word()) plus((word) plus::a , (word) plus::b) (label) plus::@return -(const byte) plus::OFFSET_STACK_A = (byte) 0 -(const byte) plus::OFFSET_STACK_B = (byte) 2 +(const byte) plus::OFFSET_STACK_A = (byte) 2 +(const byte) plus::OFFSET_STACK_B = (byte) 0 (const byte) plus::OFFSET_STACK_RETURN = (byte) 2 (word) plus::a (word) plus::a#0 a zp[2]:2 2.0 diff --git a/src/test/ref/procedure-callingconvention-stack-3.asm b/src/test/ref/procedure-callingconvention-stack-3.asm index 8d3074646..70374df50 100644 --- a/src/test/ref/procedure-callingconvention-stack-3.asm +++ b/src/test/ref/procedure-callingconvention-stack-3.asm @@ -35,8 +35,8 @@ main: { } // plus(word zp(2) a, word zp(4) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 2 + .const OFFSET_STACK_A = 2 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 2 .label a = 2 .label b = 4 diff --git a/src/test/ref/procedure-callingconvention-stack-3.log b/src/test/ref/procedure-callingconvention-stack-3.log index 559e025eb..fa3ee5160 100644 --- a/src/test/ref/procedure-callingconvention-stack-3.log +++ b/src/test/ref/procedure-callingconvention-stack-3.log @@ -232,8 +232,8 @@ main: { // plus // plus(word zp(4) a, word zp(6) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 2 + .const OFFSET_STACK_A = 2 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 2 .label a = 4 .label b = 6 @@ -367,8 +367,8 @@ main: { // plus // plus(word zp(2) a, word zp(4) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 2 + .const OFFSET_STACK_A = 2 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 2 .label a = 2 .label b = 4 @@ -440,8 +440,8 @@ FINAL SYMBOL TABLE (label) main::@return __stackcall (word()) plus((word) plus::a , (word) plus::b) (label) plus::@return -(const byte) plus::OFFSET_STACK_A = (byte) 0 -(const byte) plus::OFFSET_STACK_B = (byte) 2 +(const byte) plus::OFFSET_STACK_A = (byte) 2 +(const byte) plus::OFFSET_STACK_B = (byte) 0 (const byte) plus::OFFSET_STACK_RETURN = (byte) 2 (word) plus::a (word) plus::a#0 a zp[2]:2 2.0 @@ -517,8 +517,8 @@ main: { // plus // plus(word zp(2) a, word zp(4) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 2 + .const OFFSET_STACK_A = 2 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 2 .label a = 2 .label b = 4 diff --git a/src/test/ref/procedure-callingconvention-stack-3.sym b/src/test/ref/procedure-callingconvention-stack-3.sym index f2f026c3b..8e1b5048c 100644 --- a/src/test/ref/procedure-callingconvention-stack-3.sym +++ b/src/test/ref/procedure-callingconvention-stack-3.sym @@ -8,8 +8,8 @@ (label) main::@return __stackcall (word()) plus((word) plus::a , (word) plus::b) (label) plus::@return -(const byte) plus::OFFSET_STACK_A = (byte) 0 -(const byte) plus::OFFSET_STACK_B = (byte) 2 +(const byte) plus::OFFSET_STACK_A = (byte) 2 +(const byte) plus::OFFSET_STACK_B = (byte) 0 (const byte) plus::OFFSET_STACK_RETURN = (byte) 2 (word) plus::a (word) plus::a#0 a zp[2]:2 2.0 diff --git a/src/test/ref/procedure-callingconvention-stack-4.asm b/src/test/ref/procedure-callingconvention-stack-4.asm index 2cc0cf5ac..ff838814f 100644 --- a/src/test/ref/procedure-callingconvention-stack-4.asm +++ b/src/test/ref/procedure-callingconvention-stack-4.asm @@ -35,8 +35,8 @@ main: { } // plus(byte zp(2) a, byte register(A) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 2 tsx diff --git a/src/test/ref/procedure-callingconvention-stack-4.log b/src/test/ref/procedure-callingconvention-stack-4.log index 926eaa5e4..e78c1f3fc 100644 --- a/src/test/ref/procedure-callingconvention-stack-4.log +++ b/src/test/ref/procedure-callingconvention-stack-4.log @@ -337,8 +337,8 @@ main: { // plus // plus(byte zp(6) a, byte zp(7) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 6 .label b = 7 @@ -487,8 +487,8 @@ main: { // plus // plus(byte zp(2) a, byte register(A) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 2 // [15] (byte) plus::a#0 ← stackidx(byte,(const byte) plus::OFFSET_STACK_A) -- vbuz1=_stackidxbyte_vbuc1 @@ -560,8 +560,8 @@ FINAL SYMBOL TABLE (byte) main::w#0 reg byte a 11.0 __stackcall (byte()) plus((byte) plus::a , (byte) plus::b) (label) plus::@return -(const byte) plus::OFFSET_STACK_A = (byte) 0 -(const byte) plus::OFFSET_STACK_B = (byte) 1 +(const byte) plus::OFFSET_STACK_A = (byte) 1 +(const byte) plus::OFFSET_STACK_B = (byte) 0 (const byte) plus::OFFSET_STACK_RETURN = (byte) 1 (byte) plus::a (byte) plus::a#0 a zp[1]:2 2.0 @@ -651,8 +651,8 @@ main: { // plus // plus(byte zp(2) a, byte register(A) b) plus: { - .const OFFSET_STACK_A = 0 - .const OFFSET_STACK_B = 1 + .const OFFSET_STACK_A = 1 + .const OFFSET_STACK_B = 0 .const OFFSET_STACK_RETURN = 1 .label a = 2 // [15] (byte) plus::a#0 ← stackidx(byte,(const byte) plus::OFFSET_STACK_A) -- vbuz1=_stackidxbyte_vbuc1 diff --git a/src/test/ref/procedure-callingconvention-stack-4.sym b/src/test/ref/procedure-callingconvention-stack-4.sym index 7e8de4e2b..af3516db8 100644 --- a/src/test/ref/procedure-callingconvention-stack-4.sym +++ b/src/test/ref/procedure-callingconvention-stack-4.sym @@ -17,8 +17,8 @@ (byte) main::w#0 reg byte a 11.0 __stackcall (byte()) plus((byte) plus::a , (byte) plus::b) (label) plus::@return -(const byte) plus::OFFSET_STACK_A = (byte) 0 -(const byte) plus::OFFSET_STACK_B = (byte) 1 +(const byte) plus::OFFSET_STACK_A = (byte) 1 +(const byte) plus::OFFSET_STACK_B = (byte) 0 (const byte) plus::OFFSET_STACK_RETURN = (byte) 1 (byte) plus::a (byte) plus::a#0 a zp[1]:2 2.0