work on function entry and exit. fix stack instructions

This commit is contained in:
Torsten Ruger 2014-05-06 12:42:43 +03:00
parent 22b5117c8b
commit 12b6800efe
6 changed files with 48 additions and 28 deletions

View File

@ -31,15 +31,14 @@ module Arm
end end
def word_load value , reg def word_load value , reg
e = Vm::Block.new("load_#{value}") mov( :left => reg , :right => value )
e.add_code( MoveInstruction.new( :left => reg , :right => value ) )
end end
def function_call call def function_call call
raise "Not FunctionCall #{call.inspect}" unless call.is_a? Vm::FunctionCall raise "Not FunctionCall #{call.inspect}" unless call.is_a? Vm::FunctionCall
bl( :left => call.function ) bl( :left => call.function )
end end
def main_entry def main_start
entry = Vm::Block.new("main_entry") entry = Vm::Block.new("main_entry")
entry.add_code mov( :left => :fp , :right => 0 ) entry.add_code mov( :left => :fp , :right => 0 )
end end
@ -47,6 +46,14 @@ module Arm
entry = Vm::Block.new("main_exit") entry = Vm::Block.new("main_exit")
entry.add_code syscall(0) entry.add_code syscall(0)
end end
def function_entry f_name
entry = Vm::Block.new("#{f_name}_entry")
entry.add_code push( :left => :lr )
end
def function_exit f_name
entry = Vm::Block.new("#{f_name}_exit")
entry.add_code pop( :left => :pc )
end
def syscall num def syscall num
[mov( :left => :r7 , :right => num ) , swi( :left => 0 )] [mov( :left => :r7 , :right => num ) , swi( :left => 0 )]
end end

View File

@ -71,7 +71,6 @@ module Arm
def assemble(io) def assemble(io)
build build
instuction_class = 0b00 # OPC_DATA_PROCESSING instuction_class = 0b00 # OPC_DATA_PROCESSING
puts inspect
val = @operand.is_a?(Symbol) ? reg_code(@operand) : @operand val = @operand.is_a?(Symbol) ? reg_code(@operand) : @operand
val |= (reg_code(@rd) << 12) val |= (reg_code(@rd) << 12)
val |= (reg_code(@rn) << 12+4) val |= (reg_code(@rn) << 12+4)

View File

@ -19,7 +19,7 @@ module Arm
@operand = 0 @operand = 0
@update_status_flag= 0 @update_status_flag= 0
@rn = reg "r0" # register zero = zero bit pattern @rn = :r0 # register zero = zero bit pattern
# downward growing, decrement before memory access # downward growing, decrement before memory access
# official ARM style stack as used by gas # official ARM style stack as used by gas
@write_base = 1 @write_base = 1
@ -40,10 +40,10 @@ module Arm
build build
instuction_class = 0b10 # OPC_STACK instuction_class = 0b10 # OPC_STACK
cond = @condition_code.is_a?(Symbol) ? COND_CODES[@condition_code] : @condition_code cond = @condition_code.is_a?(Symbol) ? COND_CODES[@condition_code] : @condition_code
rn = reg "sp" # sp register @rn = :sp # sp register
#assemble of old #assemble of old
val = operand val = @operand
val |= (rn.bits << 16) val |= (reg_code(@rn) << 16)
val |= (is_pop << 16+4) #20 val |= (is_pop << 16+4) #20
val |= (write_base << 16+4+ 1) val |= (write_base << 16+4+ 1)
val |= (update_status_flag << 16+4+ 1+1) val |= (update_status_flag << 16+4+ 1+1)
@ -57,13 +57,14 @@ module Arm
private private
# Build representation for source value # Build representation for source value
def build def build
if (args.is_a?(Array)) if (@args.is_a?(Array))
@operand = 0 @operand = 0
args.each do |reg | @args.each do |reg |
@operand |= (1 << reg.bits) next unless reg
@operand |= (1 << reg_code(reg))
end end
else else
raise "invalid operand argument #{args.inspect}" raise "invalid operand argument #{@args.inspect} #{inspect}"
end end
end end
end end

View File

@ -15,8 +15,8 @@ module Vm
def initialize(name , args = []) def initialize(name , args = [])
super(name) super(name)
@args = args @args = args
@entry = Block.new("entry_#{name}") @entry = Kernel::function_entry( name )
@exit = Block.new("exit_#{name}") @exit = Kernel::function_exit( name )
end end
attr_reader :args , :entry , :exit attr_reader :args , :entry , :exit

View File

@ -1,16 +1,29 @@
module Vm module Vm
module Kernel class Kernel
def self.start
#TODO extract args into array of strings #there are no Kernel instances, only class methods.
Machine.instance.main_entry # We use this module syntax to avoid the (ugly) self.
end module ClassMethods
def self.exit def main_start
# Machine.exit mov r7 , 0 + swi 0 #TODO extract args into array of strings
Machine.instance.main_exit Machine.instance.main_start
end end
def self.puts string def main_exit
# should unwrap from string to char* # Machine.exit mov r7 , 0 + swi 0
Machine.instance.puts string Machine.instance.main_exit
end
def function_entry f_name
Machine.instance.function_entry f_name
end
def function_exit f_name
Machine.instance.function_exit f_name
end
def self.puts string
# should unwrap from string to char*
Machine.instance.puts string
end
end end
extend ClassMethods
end end
end end

View File

@ -28,10 +28,10 @@ module Vm
@objects = [] @objects = []
# global functions # global functions
@functions = [] @functions = []
@entry = Vm::Kernel::start @entry = Vm::Kernel::main_start
#main gets executed between entry and exit #main gets executed between entry and exit
@main = nil @main = nil
@exit = Vm::Kernel::exit @exit = Vm::Kernel::main_exit
end end
attr_reader :context , :main , :functions attr_reader :context , :main , :functions