From 9da269cd79a3859251e852f64e4a56bf179c9823 Mon Sep 17 00:00:00 2001 From: inglorion Date: Sun, 17 Mar 2013 18:15:39 -0700 Subject: [PATCH] allocate fewer local variable slots --- lib/voodoo/generators/common_code_generator.rb | 27 +++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/voodoo/generators/common_code_generator.rb b/lib/voodoo/generators/common_code_generator.rb index 17e0997..da5c292 100644 --- a/lib/voodoo/generators/common_code_generator.rb +++ b/lib/voodoo/generators/common_code_generator.rb @@ -241,17 +241,10 @@ module Voodoo end_block end - # Counts the number of local variables created in + # Returns the number of local variable slots required by # a sequence of statements. def count_locals statements - count = 0 - each_statement(statements) do |statement| - if statement[0] == :let - # let introduces a single local - count = count + 1 - end - end - count + count_locals_helper statements, 0, 0 end # Returns the default alignment for the given section. @@ -680,5 +673,21 @@ module Voodoo end end + private + + # Returns the number of local variable slots required for the given + # statements, given the current count and required number of slots. + def count_locals_helper statements, count, max + statements.each do |statement| + case statement[0] + when :block + max = count_locals_helper statement[1..-1], count, max + when :let + count = count + 1 + max = count if count > max + end + end + max + end end end -- 2.11.4.GIT