Skip to content

Commit

Permalink
Adding %s(saveregs)
Browse files Browse the repository at this point in the history
  • Loading branch information
vidarh committed Aug 8, 2013
1 parent e2a375d commit b86ee7c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require 'compile_comparisons'
require 'trace'
require 'stackfence'
require 'saveregs'

class Compiler
attr_reader :global_functions
Expand All @@ -28,7 +29,7 @@ class Compiler
:assign, :while, :index, :let, :case, :ternif,
:hash, :return,:sexp, :module, :rescue, :incr, :block,
:required, :add, :sub, :mul, :div, :eq, :ne,
:lt, :le, :gt, :ge
:lt, :le, :gt, :ge,:saveregs
]

Keywords = @@keywords
Expand Down Expand Up @@ -167,7 +168,6 @@ def clean_method_name(name)
return cleaned
end


# Compiles a function definition.
# Takes the current scope, in which the function is defined,
# the name of the function, its arguments as well as the body-expression that holds
Expand Down
44 changes: 44 additions & 0 deletions saveregs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

class Compiler

# Debug instruction, to save registers
#
def compile_saveregs(scope)
# First we push the registers on the stack, to ensure they won't get messed up
# when we allocate memory.

@e.pushl(:esp)
@e.pushl(:ebp)
@e.pushl(:edi)
@e.pushl(:esi)
@e.pushl(:edx)
@e.pushl(:ecx)
@e.pushl(:ebx)
@e.pushl(:eax)

# Allocate memory
@e.pushl(8)
@e.call("malloc")
@e.addl(4,:esp)

# We're naughty and assume we get memory:
@e.popl(:ebx)
@e.movl(:ebx,"(%eax)")
@e.popl(:ebx)
@e.movl(:ebx,"4(%eax)")
@e.popl(:ebx)
@e.movl(:ebx,"8(%eax)")
@e.popl(:ebx)
@e.movl(:ebx,"12(%eax)")
@e.popl(:ebx)
@e.movl(:ebx,"16(%eax)")
@e.popl(:ebx)
@e.movl(:ebx,"20(%eax)")
@e.popl(:ebx)
@e.movl(:ebx,"24(%eax)")
@e.popl(:ebx)
@e.movl(:ebx,"28(%eax)")

[:subexpr]
end
end

0 comments on commit b86ee7c

Please sign in to comment.