rubyx/lib/register/machine.rb

94 lines
2.5 KiB
Ruby
Raw Normal View History

require_relative "collector"
2016-12-08 19:13:08 +01:00
2015-10-22 17:16:29 +02:00
module Register
2016-12-06 10:38:09 +01:00
# The Register Machine is an abstraction of the register level. This is seperate from the
# actual assembler level to allow for several cpu architectures.
# The Instructions (see class Instruction) define what the machine can do (ie load/store/maths)
2015-06-20 22:49:30 +02:00
# The ast is transformed to virtual-machine objects, some of which represent code, some data.
2014-06-25 01:33:44 +02:00
#
2015-06-20 22:49:30 +02:00
# The next step transforms to the register machine layer, which is quite close to what actually
# executes. The step after transforms to Arm, which creates executables.
2014-06-25 01:33:44 +02:00
#
2015-06-20 22:49:30 +02:00
class Machine
include Collector
include Logging
log_level :debug
def initialize
@objects = {}
@booted = false
2015-10-28 12:00:23 +01:00
@constants = []
end
attr_reader :constants , :init , :objects , :booted
# idea being that later method missing could catch translate_xxx and translate to target xxx
# now we just instantiate ArmTranslater and pass instructions
def translate_arm
methods = Parfait.object_space.collect_methods
translate_methods( methods )
label = @init.next
@init = Arm::Translator.new.translate( @init )
@init.append label
end
def translate_methods(methods)
translator = Arm::Translator.new
methods.each do |method|
log.debug "Method #{method.name}"
2015-10-28 20:39:59 +01:00
instruction = method.instructions
while instruction.next
nekst = instruction.next
t = translator.translate(nekst) # returning nil means no replace
if t
nekst = t.last
instruction.replace_next(t)
end
instruction = nekst
end
2015-06-12 17:52:06 +02:00
end
end
# Objects are data and get assembled after functions
def add_object o
return false if @objects[o.object_id]
return true if o.is_a? Fixnum
unless o.is_a? Parfait::Object or o.is_a? Symbol or o.is_a? Register::Label
raise "adding non parfait #{o.class}"
end
@objects[o.object_id] = o
true
end
2015-06-12 17:52:06 +02:00
def boot
initialize
2015-06-12 17:52:06 +02:00
boot_parfait!
@init = Branch.new( "__initial_branch__" , Parfait.object_space.get_init.instructions )
2015-06-12 17:52:06 +02:00
@booted = true
self
2015-06-12 17:52:06 +02:00
end
2014-06-25 01:33:44 +02:00
end
2015-06-12 17:52:06 +02:00
# Module function to retrieve singleton
def self.machine
unless defined?(@machine)
@machine = Machine.new
end
@machine
end
2015-06-12 17:52:06 +02:00
end
2016-12-12 22:38:55 +01:00
Parfait::TypedMethod.class_eval do
2015-07-18 15:12:50 +02:00
# for testing we need to reuse the main function (or do we?)
# so remove the code that is there
def clear_source
self.source.send :initialize , self
end
end
require_relative "boot"