using the next field of blocks for a consecutive code line
This commit is contained in:
parent
a76ba577f2
commit
cf87c5323d
@ -25,7 +25,7 @@ module Ast
|
|||||||
if( l_val ) #variable existed, move data there
|
if( l_val ) #variable existed, move data there
|
||||||
l_val = l_val.move( into , r_val)
|
l_val = l_val.move( into , r_val)
|
||||||
else
|
else
|
||||||
l_val = context.function.new_local.load( into , r_val )
|
l_val = context.function.new_local.move( into , r_val )
|
||||||
end
|
end
|
||||||
context.locals[left.name] = l_val
|
context.locals[left.name] = l_val
|
||||||
return l_val
|
return l_val
|
||||||
|
@ -20,18 +20,20 @@ module Vm
|
|||||||
|
|
||||||
class Block < Code
|
class Block < Code
|
||||||
|
|
||||||
def initialize(name , function)
|
def initialize(name , function , next_block = nil)
|
||||||
super()
|
super()
|
||||||
@function = function
|
@function = function
|
||||||
@name = name.to_sym
|
@name = name.to_sym
|
||||||
@next = nil
|
@next = next_block
|
||||||
@codes = []
|
@codes = []
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :name , :next , :codes , :function
|
attr_reader :name , :next , :codes , :function
|
||||||
|
|
||||||
def length
|
def length
|
||||||
@codes.inject(0) {| sum , item | sum + item.length}
|
cods = @codes.inject(0) {| sum , item | sum + item.length}
|
||||||
|
cods += @next.length if @next
|
||||||
|
cods
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_code(kode)
|
def add_code(kode)
|
||||||
@ -55,6 +57,10 @@ module Vm
|
|||||||
code.link_at(pos , context)
|
code.link_at(pos , context)
|
||||||
pos += code.length
|
pos += code.length
|
||||||
end
|
end
|
||||||
|
if @next
|
||||||
|
@next.link_at pos , context
|
||||||
|
pos += @next.length
|
||||||
|
end
|
||||||
pos
|
pos
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -62,6 +68,16 @@ module Vm
|
|||||||
@codes.each do |obj|
|
@codes.each do |obj|
|
||||||
obj.assemble io
|
obj.assemble io
|
||||||
end
|
end
|
||||||
|
@next.assemble(io) if @next
|
||||||
|
end
|
||||||
|
|
||||||
|
# create a new linear block after this block. Linear means there is no brach needed from this one
|
||||||
|
# to the new one. Usually the new one just serves as jump address for a control statement
|
||||||
|
# In code generation (assembly) , new new_block is written after this one, ie zero runtime cost
|
||||||
|
def new_block name
|
||||||
|
new_b = Block.new( name , @function , @next )
|
||||||
|
@next = new_b
|
||||||
|
return new_b
|
||||||
end
|
end
|
||||||
|
|
||||||
# to use the assignment syntax (see method_missing) the scope must be set, so variables can be resolved
|
# to use the assignment syntax (see method_missing) the scope must be set, so variables can be resolved
|
||||||
@ -74,13 +90,6 @@ module Vm
|
|||||||
@scope = where
|
@scope = where
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
# 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
|
|
||||||
def set_next block
|
|
||||||
@next = block
|
|
||||||
end
|
|
||||||
|
|
||||||
# sugar to create instructions easily. Actually just got double sweet with two versions:
|
# sugar to create instructions easily. Actually just got double sweet with two versions:
|
||||||
# 1 for any method that ends in = we evaluate the method name in the current scope (see scope())
|
# 1 for any method that ends in = we evaluate the method name in the current scope (see scope())
|
||||||
|
@ -7,15 +7,23 @@ module Vm
|
|||||||
# Functions also have arguments and a return. These are Value subclass instances, ie specify
|
# Functions also have arguments and a return. These are Value subclass instances, ie specify
|
||||||
# type (by class type) and register by instance
|
# type (by class type) and register by instance
|
||||||
|
|
||||||
# Functions have a exactly three blocks, entry, exit and body, which are created for you
|
# They also have local variables. Args take up the first n regs, then locals the rest. No
|
||||||
# with straight branches between them.
|
# direct manipulating of registers (ie specifying the number) should be done.
|
||||||
|
|
||||||
# Also remember that if your body exists of several blocks, they must be wrapped in a
|
# Code-wise Functions are made up from a list of Blocks, in a similar way blocks are made up of codes
|
||||||
# block as the function really only has the one, and blocks only assemble their codes,
|
# Four of the block have a special role:
|
||||||
# not their next links
|
# - entry/exit: are usually system specific
|
||||||
# This comes at zero runtime cost though, as the wrapper is just the sum of it's codes
|
# - body: the logical start of the function
|
||||||
|
# - return: the logical end, where ALL blocks must end
|
||||||
|
|
||||||
# If you change the body block to point elsewhere, remember to end up at exit
|
# Blocks can be linked in two ways:
|
||||||
|
# -linear: flow continues from one to the next as they are sequential both logically and "physically"
|
||||||
|
# use the block set_next for this.
|
||||||
|
# This "the straight line", there must be a continuous sequence from body to return
|
||||||
|
# Linear blocks may be created from an existing block with new_block
|
||||||
|
# - branched: You create new blocks using function.new_block which gets added "after" return
|
||||||
|
# These (eg if/while) blocks may themselves have linear blocks ,but the last of these
|
||||||
|
# MUST have an uncoditional branch. And remember, all roads lead to return.
|
||||||
|
|
||||||
class Function < Code
|
class Function < Code
|
||||||
|
|
||||||
@ -37,12 +45,13 @@ module Vm
|
|||||||
else
|
else
|
||||||
@return_type = @return_type.new(0)
|
@return_type = @return_type.new(0)
|
||||||
end
|
end
|
||||||
@entry = Core::Kernel::function_entry( Vm::Block.new("#{name}_entry" , self) ,name )
|
|
||||||
@exit = Core::Kernel::function_exit( Vm::Block.new("#{name}_exit" , self) , name )
|
@exit = Core::Kernel::function_exit( Vm::Block.new("#{name}_exit" , self) , name )
|
||||||
@body = Block.new("#{name}_body", self)
|
@return = Block.new("#{name}_return", self , @exit)
|
||||||
|
@body = Block.new("#{name}_body", self , @return)
|
||||||
|
@entry = Core::Kernel::function_entry( Vm::Block.new("#{name}_entry" , self , @body) ,name )
|
||||||
@locals = []
|
@locals = []
|
||||||
branch_body
|
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :args , :entry , :exit , :body , :name
|
attr_reader :args , :entry , :exit , :body , :name
|
||||||
attr_accessor :return_type
|
attr_accessor :return_type
|
||||||
|
|
||||||
@ -62,28 +71,17 @@ module Vm
|
|||||||
super #just sets the position
|
super #just sets the position
|
||||||
@entry.link_at address , context
|
@entry.link_at address , context
|
||||||
address += @entry.length
|
address += @entry.length
|
||||||
@body.link_at(address , context)
|
|
||||||
address += @body.length
|
|
||||||
@exit.link_at(address,context)
|
|
||||||
end
|
end
|
||||||
def position
|
def position
|
||||||
@entry.position
|
@entry.position
|
||||||
end
|
end
|
||||||
def length
|
def length
|
||||||
@entry.length + @exit.length + @body.length
|
@entry.length
|
||||||
end
|
end
|
||||||
|
|
||||||
def assemble io
|
def assemble io
|
||||||
@entry.assemble(io)
|
@entry.assemble(io)
|
||||||
@body.assemble(io)
|
|
||||||
@exit.assemble(io)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
# set up the braches from entry to body and body to exit (unless that exists, see set_body)
|
|
||||||
def branch_body
|
|
||||||
@entry.set_next(@body)
|
|
||||||
@body.set_next(@exit) if @body and !@body.next
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
x
Reference in New Issue
Block a user