2014-05-15 15:54:23 +02:00
|
|
|
require_relative "context"
|
2014-05-03 14:13:44 +02:00
|
|
|
require_relative "function"
|
2014-05-13 20:15:02 +02:00
|
|
|
require_relative "call_site"
|
2014-05-03 14:13:44 +02:00
|
|
|
require "arm/arm_machine"
|
2014-05-15 15:54:23 +02:00
|
|
|
require "core/kernel"
|
2014-05-03 14:13:44 +02:00
|
|
|
|
|
|
|
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
|
2014-05-21 18:43:46 +02:00
|
|
|
# with a XXXMachine in it that derives from Vm::RegisterMachine
|
2014-05-08 18:30:46 +02:00
|
|
|
def initialize machine = nil
|
2014-05-21 15:42:36 +02:00
|
|
|
super("start" , nil)
|
2014-05-08 18:30:46 +02:00
|
|
|
machine = RbConfig::CONFIG["host_cpu"] unless machine
|
|
|
|
machine = "intel" if machine == "x86_64"
|
|
|
|
machine = machine.capitalize
|
2014-05-21 18:43:46 +02:00
|
|
|
RegisterMachine.instance = eval("#{machine}::#{machine}Machine").new
|
2014-05-03 14:13:44 +02:00
|
|
|
@context = Context.new(self)
|
2014-05-05 10:03:43 +02:00
|
|
|
#global objects (data)
|
2014-05-03 14:13:44 +02:00
|
|
|
@objects = []
|
2014-05-05 10:03:43 +02:00
|
|
|
# global functions
|
|
|
|
@functions = []
|
2014-05-21 15:42:36 +02:00
|
|
|
@entry = Core::Kernel::main_start Vm::Block.new("main_entry",nil)
|
2014-05-05 10:03:43 +02:00
|
|
|
#main gets executed between entry and exit
|
2014-05-21 15:42:36 +02:00
|
|
|
@main = Block.new("main",nil)
|
|
|
|
@exit = Core::Kernel::main_exit Vm::Block.new("main_exit",nil)
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
2014-05-14 15:15:47 +02:00
|
|
|
attr_reader :context , :main , :functions , :entry , :exit
|
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
|
|
|
|
|
2014-05-10 14:24:56 +02:00
|
|
|
def add_function function
|
|
|
|
raise "not a function #{function}" unless function.is_a? Function
|
2014-05-13 20:06:12 +02:00
|
|
|
raise "syserr " unless function.name.is_a? Symbol
|
2014-05-10 14:24:56 +02:00
|
|
|
@functions << function
|
|
|
|
end
|
|
|
|
|
2014-05-03 14:13:44 +02:00
|
|
|
def get_function name
|
2014-05-10 14:24:56 +02:00
|
|
|
name = name.to_sym
|
2014-05-13 20:06:12 +02:00
|
|
|
@functions.detect{ |f| f.name == name }
|
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
|
2014-05-15 15:54:23 +02:00
|
|
|
fun = Core::Kernel.send(name , context)
|
2014-05-13 20:06:12 +02:00
|
|
|
raise "no such function '#{name}'" if fun == nil
|
2014-05-05 10:03:43 +02:00
|
|
|
@functions << fun
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
fun
|
|
|
|
end
|
|
|
|
|
2014-05-05 14:59:29 +02:00
|
|
|
# linking entry , main , exit
|
|
|
|
# functions , objects
|
2014-05-05 10:03:43 +02:00
|
|
|
def link_at( start , context)
|
|
|
|
@position = start
|
|
|
|
@entry.link_at( start , context )
|
|
|
|
start += @entry.length
|
|
|
|
@main.link_at( start , context )
|
|
|
|
start += @main.length
|
2014-05-05 14:59:29 +02:00
|
|
|
@exit.link_at( start , context)
|
|
|
|
start += @exit.length
|
2014-05-05 10:03:43 +02:00
|
|
|
@functions.each do |function|
|
|
|
|
function.link_at(start , context)
|
|
|
|
start += function.length
|
2014-05-05 08:35:40 +02:00
|
|
|
end
|
|
|
|
@objects.each do |o|
|
2014-05-05 10:03:43 +02:00
|
|
|
o.link_at(start , context)
|
|
|
|
start += o.length
|
2014-05-05 08:35:40 +02:00
|
|
|
end
|
2014-05-05 14:59:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# assemble in the same order as linked
|
|
|
|
def assemble( io )
|
2014-05-18 11:34:53 +02:00
|
|
|
link_at( @position , nil) #second link in case of forward declarations
|
2014-05-05 14:59:29 +02:00
|
|
|
@entry.assemble( io )
|
|
|
|
@main.assemble( io )
|
|
|
|
@exit.assemble( io )
|
|
|
|
@functions.each do |function|
|
|
|
|
function.assemble(io)
|
|
|
|
end
|
|
|
|
@objects.each do |o|
|
|
|
|
o.assemble(io)
|
|
|
|
end
|
|
|
|
io
|
2014-05-05 08:35:40 +02:00
|
|
|
end
|
|
|
|
|
2014-05-05 10:03:43 +02:00
|
|
|
def main= code
|
|
|
|
@main = code
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
2014-05-05 14:59:29 +02:00
|
|
|
|
2014-05-03 14:13:44 +02:00
|
|
|
end
|
|
|
|
end
|