2014-05-03 14:13:44 +02:00
|
|
|
require_relative "function"
|
|
|
|
require_relative "function_call"
|
|
|
|
require "arm/arm_machine"
|
|
|
|
|
|
|
|
module Vm
|
|
|
|
# A Program represents an executable that we want to build
|
|
|
|
# it has a list of functions and (global) objects
|
|
|
|
|
|
|
|
# The main entry is a function called (of all things) "main", This _must be supplied by the compling
|
|
|
|
# There is a start and exit block that call main, which receives an array of strings
|
|
|
|
|
|
|
|
# While data "ususally" would live in a .data section, we may also "inline" it into the code
|
|
|
|
# in an oo system all data is represented as objects
|
|
|
|
|
|
|
|
# in terms of variables and their visibility, things are simple. They are either local or global
|
|
|
|
|
|
|
|
# throwing in a context for unspecified use (well one is to pass the programm/globals around)
|
|
|
|
|
|
|
|
class Program < Block
|
|
|
|
|
2014-05-03 17:51:47 +02:00
|
|
|
# Initialize with a string for cpu. Naming conventions are: for Machine XXX there exists a module XXX
|
|
|
|
# with a XXXMachine in it that derives from Vm::Machine
|
|
|
|
def initialize machine
|
2014-05-03 14:13:44 +02:00
|
|
|
super("start")
|
2014-05-03 17:51:47 +02:00
|
|
|
Machine.instance = eval("#{machine}::#{machine}Machine").new
|
2014-05-03 14:13:44 +02:00
|
|
|
|
|
|
|
@context = Context.new(self)
|
|
|
|
@objects = []
|
|
|
|
@entry = Vm::Kernel::start
|
|
|
|
@exit = Vm::Kernel::exit
|
|
|
|
end
|
|
|
|
attr_reader :context
|
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
def functions
|
|
|
|
@values
|
|
|
|
end
|
2014-05-03 14:13:44 +02:00
|
|
|
def add_object o
|
2014-05-05 08:35:40 +02:00
|
|
|
return if @objects.include? o
|
2014-05-03 14:13:44 +02:00
|
|
|
@objects << o # TODO check type , no basic values allowed (must be wrapped)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_function name
|
2014-05-05 08:35:40 +02:00
|
|
|
@values.detect{ |f| (f.name == name) && (f.class == Function) }
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# preferred way of creating new functions (also forward declarations, will flag unresolved later)
|
|
|
|
def get_or_create_function name
|
|
|
|
fun = get_function name
|
|
|
|
unless fun
|
|
|
|
fun = Function.new(name)
|
2014-05-05 08:35:40 +02:00
|
|
|
@values << fun
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
fun
|
|
|
|
end
|
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
def compile(context)
|
|
|
|
@values.each do |function|
|
|
|
|
function.compile(context)
|
|
|
|
end
|
|
|
|
@entry.compile(context)
|
|
|
|
super(context)
|
|
|
|
@exit.compile(context)
|
|
|
|
#string_table
|
|
|
|
@objects.each do |o|
|
|
|
|
o.compile(context)
|
|
|
|
end
|
|
|
|
fix_positions
|
|
|
|
fix_positions # second time for forward references
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
def fix_positions
|
|
|
|
@position = 0
|
|
|
|
@entry.at( @position )
|
|
|
|
@position += @entry.length
|
|
|
|
@values.each do |function|
|
|
|
|
function.at(@position)
|
|
|
|
@position += function.length
|
|
|
|
end
|
|
|
|
@objects.each do |o|
|
|
|
|
o.at(@position)
|
|
|
|
@position += o.length
|
|
|
|
end
|
|
|
|
@exit.at( @position )
|
|
|
|
@position += @exit.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def wrap_as_main block
|
|
|
|
# #blockify
|
|
|
|
unless block.is_a? Block
|
|
|
|
raise "No block, start coding torsten"
|
|
|
|
end
|
|
|
|
add_value block
|
|
|
|
@entry.next block
|
|
|
|
block.next @exit
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
def verify
|
2014-05-05 08:35:40 +02:00
|
|
|
@values.each do |funct|
|
2014-05-03 14:13:44 +02:00
|
|
|
funct.verify
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
# the main function
|
|
|
|
def create_main
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|