From d13573f67bb643132482acee98242b4043f12ac1 Mon Sep 17 00:00:00 2001 From: Shagun Agrawal Date: Wed, 30 Oct 2024 23:30:18 +0530 Subject: Fix stack allocation bug Error in rounding stack allocator to nearest 16 divisible value Pass all tests in chapter 9 Used as reference https://github.com/nlsandler/nqcc2 --- src/cljcc/compiler.clj | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/cljcc/compiler.clj b/src/cljcc/compiler.clj index c1af6de..17da6cf 100644 --- a/src/cljcc/compiler.clj +++ b/src/cljcc/compiler.clj @@ -372,9 +372,12 @@ Stack space allocated needs to be a multiple of 16. Rouding up the size of stack frame makes it easier to maintain stack alignment during function calls." [{instructions :instructions max-stack-val :max-stack-val}] - (let [stack-abs (abs max-stack-val) - stack-value (- (+ stack-abs (mod stack-abs 16)))] - (cons (allocate-stack-instruction stack-value) instructions))) + (let [v (abs max-stack-val) + v (cond + (= (mod v 16) 0) v + (< v 0) (- v (- 16 (mod v 16))) + :else (+ v (- 16 (mod v 16))))] + (cons (allocate-stack-instruction v) instructions))) (defn- parameters->assembly-instructions "Moves parameters from registers and stacks to pseudoregisters. -- cgit v1.2.3