From cbd1a9443bda7791d606825766ec264550df5ccf Mon Sep 17 00:00:00 2001 From: inglorion Date: Tue, 13 Jul 2010 21:13:15 +0200 Subject: [PATCH] Made top-level blocks in i386 and AMD64 set EBP/RBP, so BP-relative addressing can be used inside the block. --- lib/voodoo/generators/amd64_nasm_generator.rb | 16 ++++------------ lib/voodoo/generators/i386_nasm_generator.rb | 16 ++++------------ lib/voodoo/generators/nasm_generator.rb | 23 ++++++++++++++++++++++- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/lib/voodoo/generators/amd64_nasm_generator.rb b/lib/voodoo/generators/amd64_nasm_generator.rb index 86d30ac..00044fd 100644 --- a/lib/voodoo/generators/amd64_nasm_generator.rb +++ b/lib/voodoo/generators/amd64_nasm_generator.rb @@ -76,6 +76,10 @@ module Voodoo @CX = 'rcx' # Data index @DX = 'rdx' + # Base pointer + @BP = 'rbp' + # Stack pointer + @SP = 'rsp' super params end @@ -206,18 +210,6 @@ module Voodoo end end - # Ends the current block. - def end_block - emit "; end block\n" - - # De-allocate block's variables - nvars = @environment.locals - @environment.parent.locals - emit "add rsp, #{nvars * @WORDSIZE}\n" unless nvars == 0 - - # Restore old value of @environment - @environment = @environment.parent - end - # # == Loading Values # diff --git a/lib/voodoo/generators/i386_nasm_generator.rb b/lib/voodoo/generators/i386_nasm_generator.rb index ee9ddd8..b0e7e92 100644 --- a/lib/voodoo/generators/i386_nasm_generator.rb +++ b/lib/voodoo/generators/i386_nasm_generator.rb @@ -56,6 +56,10 @@ module Voodoo @CX = 'ecx' # Data index @DX = 'edx' + # Base pointer + @BP = 'ebp' + # Stack pointer + @SP = 'esp' super params end @@ -70,18 +74,6 @@ module Voodoo end end - # Ends the current block. - def end_block - emit "; end block\n" - - # De-allocate block's variables - nvars = @environment.locals - @environment.parent.locals - emit "add esp, #{nvars * @WORDSIZE}\n" unless nvars == 0 - - # Restore old value of @environment - @environment = @environment.parent - end - # Emit function prologue. def emit_function_prologue formals = [] emit "push ebp\nmov ebp, esp\n" diff --git a/lib/voodoo/generators/nasm_generator.rb b/lib/voodoo/generators/nasm_generator.rb index e1ddbf6..f600fb2 100644 --- a/lib/voodoo/generators/nasm_generator.rb +++ b/lib/voodoo/generators/nasm_generator.rb @@ -21,7 +21,7 @@ module Voodoo # - @SCRATCH_REG # - @WORD_NAME # - @WORDSIZE - # - @AX, @BX, @CX, and @DX + # - @AX, @BX, @CX, @DX, @BP, and @SP class NasmGenerator < CommonCodeGenerator def initialize params = {} super params @@ -178,10 +178,31 @@ module Voodoo # Begins a new block. def begin_block emit "; begin block\n" + # If entering a block at top level, + # Save @BP, then set @BP to @SP + if @environment == @top_level + emit "push #{@BP}\n" + emit "mov #{@BP}, #{@SP}\n" + end environment = Environment.new @environment @environment = environment end + # Ends the current block. + def end_block + emit "; end block\n" + + # De-allocate block's variables + nvars = @environment.locals - @environment.parent.locals + emit "add #{@SP}, #{nvars * @WORDSIZE}\n" unless nvars == 0 + + # Restore old value of @environment + @environment = @environment.parent + + # If returning to top level, restore old @BP + emit "pop #{@BP}\n" if @environment == @top_level + end + # # == Conditionals # -- 2.11.4.GIT