2015-10-18 18:27:02 +02:00
|
|
|
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-10-24 10:42:36 +02:00
|
|
|
|
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
|
|
|
|
2014-06-25 15:00:24 +02:00
|
|
|
class Machine
|
2015-10-18 18:27:02 +02:00
|
|
|
include Collector
|
2016-12-29 20:24:11 +01:00
|
|
|
include Logging
|
|
|
|
log_level :debug
|
2015-07-18 10:53:04 +02:00
|
|
|
|
2014-06-25 15:00:24 +02:00
|
|
|
def initialize
|
2015-07-21 14:40:25 +02:00
|
|
|
@objects = {}
|
2015-06-01 07:40:17 +02:00
|
|
|
@booted = false
|
2015-10-28 12:00:23 +01:00
|
|
|
@constants = []
|
2014-06-25 15:00:24 +02:00
|
|
|
end
|
2016-12-30 13:04:59 +01:00
|
|
|
attr_reader :constants , :init , :objects , :booted
|
2015-10-24 10:42:36 +02:00
|
|
|
|
|
|
|
# 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
|
2016-12-30 13:10:49 +01:00
|
|
|
methods = Parfait.object_space.collect_methods
|
2016-12-29 20:24:11 +01:00
|
|
|
translate_methods( methods )
|
2016-12-15 18:20:54 +01:00
|
|
|
label = @init.next
|
2016-12-29 20:24:11 +01:00
|
|
|
@init = Arm::Translator.new.translate( @init )
|
2016-12-15 18:20:54 +01:00
|
|
|
@init.append label
|
|
|
|
end
|
|
|
|
|
2016-12-29 20:24:11 +01:00
|
|
|
def translate_methods(methods)
|
|
|
|
translator = Arm::Translator.new
|
2015-10-24 10:42:36 +02:00
|
|
|
methods.each do |method|
|
2016-12-29 20:24:11 +01:00
|
|
|
log.debug "Method #{method.name}"
|
2015-10-28 20:39:59 +01:00
|
|
|
instruction = method.instructions
|
2015-10-24 16:11:18 +02:00
|
|
|
while instruction.next
|
2015-10-24 10:42:36 +02:00
|
|
|
nekst = instruction.next
|
2015-10-24 16:11:18 +02:00
|
|
|
t = translator.translate(nekst) # returning nil means no replace
|
|
|
|
if t
|
|
|
|
nekst = t.last
|
|
|
|
instruction.replace_next(t)
|
|
|
|
end
|
2015-10-24 10:42:36 +02:00
|
|
|
instruction = nekst
|
2015-10-24 16:11:18 +02:00
|
|
|
end
|
2015-06-12 17:52:06 +02:00
|
|
|
end
|
2015-05-12 14:36:44 +02:00
|
|
|
end
|
|
|
|
|
2015-05-31 12:02:29 +02:00
|
|
|
# Objects are data and get assembled after functions
|
2016-12-30 17:39:49 +01:00
|
|
|
def add_object objekt
|
|
|
|
return false if @objects[objekt.object_id]
|
|
|
|
return true if objekt.is_a? Fixnum
|
|
|
|
unless objekt.is_a?( Parfait::Object) or objekt.is_a?( Symbol) or objekt.is_a?( Register::Label)
|
|
|
|
raise "adding non parfait #{objekt.class}"
|
2015-11-03 10:22:26 +01:00
|
|
|
end
|
2016-12-30 17:39:49 +01:00
|
|
|
#raise "Method #{objekt.name}" if objekt.is_a? Parfait::TypedMethod
|
|
|
|
@objects[objekt.object_id] = objekt
|
2015-05-31 12:02:29 +02:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-06-12 17:52:06 +02:00
|
|
|
def boot
|
2015-11-09 09:04:37 +01:00
|
|
|
initialize
|
2015-06-12 17:52:06 +02:00
|
|
|
boot_parfait!
|
2016-12-30 13:10:49 +01:00
|
|
|
@init = Branch.new( "__initial_branch__" , Parfait.object_space.get_init.instructions )
|
2015-06-12 17:52:06 +02:00
|
|
|
@booted = true
|
2015-07-18 10:53:04 +02:00
|
|
|
self
|
2015-06-12 17:52:06 +02:00
|
|
|
end
|
|
|
|
|
2014-06-25 01:33:44 +02:00
|
|
|
end
|
2015-06-01 07:40:17 +02:00
|
|
|
|
2015-06-12 17:52:06 +02:00
|
|
|
# Module function to retrieve singleton
|
2015-06-01 07:40:17 +02:00
|
|
|
def self.machine
|
|
|
|
unless defined?(@machine)
|
|
|
|
@machine = Machine.new
|
|
|
|
end
|
|
|
|
@machine
|
|
|
|
end
|
2015-06-12 17:52:06 +02:00
|
|
|
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
2015-05-19 19:29:33 +02:00
|
|
|
|
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
|
|
|
|
|
2015-05-19 19:29:33 +02:00
|
|
|
require_relative "boot"
|