2014-05-03 14:13:44 +02:00
|
|
|
require_relative "values"
|
|
|
|
|
|
|
|
module Vm
|
|
|
|
|
|
|
|
# Think flowcharts: blocks are the boxes. The smallest unit of linear code
|
|
|
|
|
|
|
|
# Blocks must end in control instructions (jump/call/return).
|
|
|
|
# And the only valid argument for a jump is a Block
|
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
# Blocks form a linked list
|
2014-05-03 14:13:44 +02:00
|
|
|
|
|
|
|
# There are four ways for a block to get data (to work on)
|
|
|
|
# - hard coded constants (embedded in code)
|
|
|
|
# - memory move
|
|
|
|
# - values passed in (from previous blocks. ie local variables)
|
|
|
|
|
|
|
|
# See Value description on how to create code/instructions
|
|
|
|
|
2014-05-05 10:03:43 +02:00
|
|
|
# Codes then get assembled into bytes (after linking)
|
2014-05-05 08:35:40 +02:00
|
|
|
|
2014-05-03 21:18:04 +02:00
|
|
|
class Block < Code
|
2014-05-03 14:13:44 +02:00
|
|
|
|
|
|
|
def initialize(name)
|
|
|
|
super()
|
|
|
|
@name = name.to_sym
|
2014-05-03 17:51:47 +02:00
|
|
|
@next = nil
|
|
|
|
@codes = []
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
|
2014-05-05 10:03:43 +02:00
|
|
|
attr_reader :name , :next , :codes
|
2014-05-03 14:13:44 +02:00
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
def length
|
|
|
|
@codes.inject(0) {| sum , item | sum + item.length}
|
|
|
|
end
|
2014-05-05 10:03:43 +02:00
|
|
|
|
2014-05-03 21:18:04 +02:00
|
|
|
def add_code(kode)
|
2014-05-05 23:12:04 +02:00
|
|
|
if( kode.is_a? Array )
|
|
|
|
kode.each { |code| @codes << code }
|
|
|
|
else
|
|
|
|
@codes << kode
|
|
|
|
end
|
2014-05-05 08:35:40 +02:00
|
|
|
self
|
2014-05-03 21:18:04 +02:00
|
|
|
end
|
|
|
|
|
2014-05-05 10:03:43 +02:00
|
|
|
def link_at pos , context
|
|
|
|
@position = pos
|
|
|
|
@codes.each do |code|
|
|
|
|
code.link_at(pos , context)
|
|
|
|
pos += code.length
|
2014-05-05 08:35:40 +02:00
|
|
|
end
|
2014-05-05 10:03:43 +02:00
|
|
|
pos
|
2014-05-05 08:35:40 +02:00
|
|
|
end
|
2014-05-05 10:03:43 +02:00
|
|
|
|
2014-05-03 21:18:04 +02:00
|
|
|
def assemble(io)
|
|
|
|
@codes.each do |obj|
|
|
|
|
obj.assemble io
|
|
|
|
end
|
|
|
|
end
|
2014-05-06 20:36:28 +02:00
|
|
|
|
2014-05-03 17:51:47 +02:00
|
|
|
# set the next executed block after self.
|
|
|
|
# why is this useful? if it's unconditional, why not merge them:
|
|
|
|
# So the second block can be used as a jump target. You standard loop needs a block to setup
|
|
|
|
# and at least one to do the calculation
|
2014-05-06 20:36:28 +02:00
|
|
|
def set_next block
|
2014-05-05 08:35:40 +02:00
|
|
|
@next = block
|
2014-05-03 17:51:47 +02:00
|
|
|
end
|
2014-05-05 08:35:40 +02:00
|
|
|
|
2014-05-08 13:14:15 +02:00
|
|
|
# sugar to create instructions easily. Any method with one arg is sent to the machine and the result
|
|
|
|
# (hopefully an instruction) added as code
|
|
|
|
def method_missing(meth, *args, &block)
|
|
|
|
if args.length == 1
|
|
|
|
add_code Machine.instance.send(meth , *args)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|