rename machine to c_machine

This commit is contained in:
Torsten Ruger 2014-05-13 17:06:42 +03:00
parent d7f31e7f39
commit b0302948dd
10 changed files with 21 additions and 20 deletions

View File

@ -1,4 +1,4 @@
require "vm/machine" require "vm/c_machine"
require_relative "stack_instruction" require_relative "stack_instruction"
require_relative "logic_instruction" require_relative "logic_instruction"
require_relative "move_instruction" require_relative "move_instruction"
@ -7,7 +7,7 @@ require_relative "memory_instruction"
require_relative "call_instruction" require_relative "call_instruction"
module Arm module Arm
class ArmMachine < Vm::Machine class ArmMachine < Vm::CMachine
# defines a method in the current class, with the name inst (first erg) # defines a method in the current class, with the name inst (first erg)
# the method instantiates an instruction of the given class which gets passed a single hash as arg # the method instantiates an instruction of the given class which gets passed a single hash as arg

View File

@ -6,21 +6,21 @@ module Core
module ClassMethods module ClassMethods
def main_start def main_start
#TODO extract args into array of strings #TODO extract args into array of strings
Vm::Machine.instance.main_start Vm::CMachine.instance.main_start
end end
def main_exit def main_exit
# Machine.exit mov r7 , 0 + swi 0 # Machine.exit mov r7 , 0 + swi 0
Vm::Machine.instance.main_exit Vm::CMachine.instance.main_exit
end end
def function_entry f_name def function_entry f_name
Vm::Machine.instance.function_entry f_name Vm::CMachine.instance.function_entry f_name
end end
def function_exit f_name def function_exit f_name
Vm::Machine.instance.function_exit f_name Vm::CMachine.instance.function_exit f_name
end end
def putstring def putstring
# should unwrap from string to char* # should unwrap from string to char*
Vm::Machine.instance.putstring Vm::CMachine.instance.putstring
end end
end end

View File

@ -4,6 +4,6 @@ require "elf/object_writer"
require 'parser/crystal' require 'parser/crystal'
require 'parser/transform' require 'parser/transform'
require "vm/context" require "vm/context"
require "vm/machine" require "vm/c_machine"
require "vm/program" require "vm/program"
require "stream_reader" require "stream_reader"

View File

@ -8,17 +8,17 @@ module Support
puts "NO -#{args.length} BLOCK #{block_given?}" puts "NO -#{args.length} BLOCK #{block_given?}"
super super
else else
name = name.to_s sname = name.to_s
if args.length == 1 #must be assignemnt for ir attr= val if args.length == 1 #must be assignemnt for ir attr= val
if name.include? "=" if sname.include? "="
#puts "setting :#{name.chop}:#{args[0]}" #puts "setting :#{name.chop}:#{args[0]}"
return @attributes[name.chop.to_sym] = args[0] return @attributes[sname.chop.to_sym] = args[0]
else else
super super
end end
else else
#puts "getting :#{name}:#{@attributes[name.to_sym]}" #puts "getting :#{name}:#{@attributes[name.to_sym]}"
return @attributes[name.to_sym] return @attributes[sname.to_sym]
end end
end end
end end

View File

@ -69,7 +69,7 @@ module Vm
# (hopefully an instruction) added as code # (hopefully an instruction) added as code
def method_missing(meth, *args, &block) def method_missing(meth, *args, &block)
if args.length == 1 if args.length == 1
add_code Machine.instance.send(meth , *args) add_code CMachine.instance.send(meth , *args)
else else
super super
end end

View File

@ -1,6 +1,6 @@
module Vm module Vm
# Our virtual machine has a number of registers of a given size and uses a stack # Our virtual c-machine has a number of registers of a given size and uses a stack
# So much so standard # So much so standard
# But our machine is oo, meaning that the register contents is typed. # But our machine is oo, meaning that the register contents is typed.
# Off course current hardware does not have that (a perceived issue), but for our machine we pretend. # Off course current hardware does not have that (a perceived issue), but for our machine we pretend.
@ -22,7 +22,7 @@ module Vm
# Instructions work with options, so you can pass anything in, and the only thing the functions does # Instructions work with options, so you can pass anything in, and the only thing the functions does
# is save you typing the clazz.new. It passes the function name as the :opcode # is save you typing the clazz.new. It passes the function name as the :opcode
class Machine class CMachine
# hmm, not pretty but for now # hmm, not pretty but for now
@@instance = nil @@instance = nil

View File

@ -12,6 +12,7 @@ module Vm
attr_reader :function , :args , :name attr_reader :function , :args , :name
def load_args into def load_args into
raise args.inspect
args.each_with_index do |arg , index| args.each_with_index do |arg , index|
into.add_code arg.load("r#{index}".to_sym) into.add_code arg.load("r#{index}".to_sym)
end end

View File

@ -19,13 +19,13 @@ module Vm
class Program < Block class Program < Block
# Initialize with a string for cpu. Naming conventions are: for Machine XXX there exists a module XXX # 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 # with a XXXMachine in it that derives from Vm::CMachine
def initialize machine = nil def initialize machine = nil
super("start") super("start")
machine = RbConfig::CONFIG["host_cpu"] unless machine machine = RbConfig::CONFIG["host_cpu"] unless machine
machine = "intel" if machine == "x86_64" machine = "intel" if machine == "x86_64"
machine = machine.capitalize machine = machine.capitalize
Machine.instance = eval("#{machine}::#{machine}Machine").new CMachine.instance = eval("#{machine}::#{machine}Machine").new
@context = Context.new(self) @context = Context.new(self)
#global objects (data) #global objects (data)
@objects = [] @objects = []

View File

@ -26,11 +26,11 @@ class TestRunner < MiniTest::Test
# file is a list of expressions, al but the last must be a function # file is a list of expressions, al but the last must be a function
# and the last is wrapped as a main # and the last is wrapped as a main
parts.each_with_index do |part,index| parts.each_with_index do |part,index|
puts "parsing #{index}=#{part.inspect}"
expr = part.compile( program.context )
if index = parts.length if index = parts.length
expr = part.compile( program.context , nil )
program.main = expr program.main = expr
else else
expr = part.compile( program.context , program.main )
raise "should be function definition for now" unless expr.is_a? Function raise "should be function definition for now" unless expr.is_a? Function
program.add_function expr program.add_function expr
end end

View File

@ -28,7 +28,7 @@ class TestSmallProg < MiniTest::Test
end end
def test_hello def test_hello
hello = Vm::StringLiteral.new "Hello Raisa\n" hello = Vm::StringConstant.new "Hello Raisa\n"
@program.add_object hello @program.add_object hello
@program.main.instance_eval do @program.main.instance_eval do
mov :left =>:r7, :right => 4 # 4 == write mov :left =>:r7, :right => 4 # 4 == write