refactor builtin object, some machine too
This commit is contained in:
parent
94c423c2b3
commit
5ea6bfed27
28
lib/register/builtin/compile_helper.rb
Normal file
28
lib/register/builtin/compile_helper.rb
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
module Register
|
||||||
|
module Builtin
|
||||||
|
module CompileHelper
|
||||||
|
|
||||||
|
def self_and_arg(compiler , source)
|
||||||
|
#Load self by "calling" on_name
|
||||||
|
me = compiler.process( Typed::Tree::NameExpression.new( :self) )
|
||||||
|
# Load the argument
|
||||||
|
index = compiler.use_reg :Integer
|
||||||
|
compiler.add_code Register.get_slot(source , :message , Parfait::Message.get_indexed(1), index )
|
||||||
|
return me , index
|
||||||
|
end
|
||||||
|
|
||||||
|
def compiler_for( method_name , args)
|
||||||
|
Typed::Compiler.new.create_method(:Object , method_name , args ).init_method
|
||||||
|
end
|
||||||
|
|
||||||
|
# Load the value
|
||||||
|
def do_load(compiler, source)
|
||||||
|
value = compiler.use_reg :Integer
|
||||||
|
compiler.add_code Register.get_slot(source , :message , Parfait::Message.get_indexed(2), value )
|
||||||
|
value
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,22 +1,18 @@
|
|||||||
require "ast/sexp"
|
require_relative "compile_helper"
|
||||||
|
|
||||||
module Register
|
module Register
|
||||||
module Builtin
|
module Builtin
|
||||||
class Object
|
class Object
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
include AST::Sexp
|
include CompileHelper
|
||||||
|
|
||||||
# self[index] basically. Index is the first arg
|
# self[index] basically. Index is the first arg
|
||||||
# return is stored in return_value
|
# return is stored in return_value
|
||||||
# (this method returns a new method off course, like all builtin)
|
# (this method returns a new method off course, like all builtin)
|
||||||
def get_internal_word context
|
def get_internal_word context
|
||||||
compiler = Typed::Compiler.new.create_method(:Object , :get_internal_word , {:index => :Integer}).init_method
|
compiler = compiler_for(:get_internal_word , {:index => :Integer})
|
||||||
source = "get_internal_word"
|
source = "get_internal_word"
|
||||||
#Load self by "calling" on_name
|
me , index = self_and_arg(compiler,source)
|
||||||
me = compiler.process( Typed::Tree::NameExpression.new( :self) )
|
|
||||||
# Load the argument
|
|
||||||
index = compiler.use_reg :Integer
|
|
||||||
compiler.add_code Register.get_slot(source , :message , Parfait::Message.get_indexed(1), index )
|
|
||||||
# reduce me to me[index]
|
# reduce me to me[index]
|
||||||
compiler.add_code GetSlot.new( source , me , index , me)
|
compiler.add_code GetSlot.new( source , me , index , me)
|
||||||
# and put it back into the return value
|
# and put it back into the return value
|
||||||
@ -27,17 +23,11 @@ module Register
|
|||||||
# self[index] = val basically. Index is the first arg , value the second
|
# self[index] = val basically. Index is the first arg , value the second
|
||||||
# no return
|
# no return
|
||||||
def set_internal_word context
|
def set_internal_word context
|
||||||
compiler = Typed::Compiler.new.create_method(:Object , :set_internal_word ,
|
compiler = compiler_for(:set_internal_word , {:index => :Integer, :value => :Object} )
|
||||||
{:index => :Integer, :value => :Object} ).init_method
|
|
||||||
source = "set_internal_word"
|
source = "set_internal_word"
|
||||||
#Load self by "calling" on_name
|
me , index = self_and_arg(compiler,source)
|
||||||
me = compiler.process( Typed::Tree::NameExpression.new( :self) )
|
value = do_load(compiler,source)
|
||||||
# Load the index
|
|
||||||
index = compiler.use_reg :Integer
|
|
||||||
compiler.add_code Register.get_slot(source , :message , Parfait::Message.get_indexed(1), index )
|
|
||||||
# Load the value
|
|
||||||
value = compiler.use_reg :Integer
|
|
||||||
compiler.add_code Register.get_slot(source , :message , Parfait::Message.get_indexed(2), value )
|
|
||||||
# do the set
|
# do the set
|
||||||
compiler.add_code SetSlot.new( source , value , me , index)
|
compiler.add_code SetSlot.new( source , value , me , index)
|
||||||
return compiler.method
|
return compiler.method
|
||||||
|
@ -26,14 +26,27 @@ module Register
|
|||||||
# now we just instantiate ArmTranslater and pass instructions
|
# now we just instantiate ArmTranslater and pass instructions
|
||||||
def translate_arm
|
def translate_arm
|
||||||
translator = Arm::Translator.new
|
translator = Arm::Translator.new
|
||||||
|
methods = collect_methods
|
||||||
|
translate_methods( methods , translator)
|
||||||
|
label = @init.next
|
||||||
|
@init = translator.translate( @init)
|
||||||
|
@init.append label
|
||||||
|
end
|
||||||
|
|
||||||
|
def collect_methods
|
||||||
methods = []
|
methods = []
|
||||||
self.space.types.each do |hash , t|
|
self.space.types.each do |hash , t|
|
||||||
t.instance_methods.each do |f|
|
t.instance_methods.each do |f|
|
||||||
methods << f
|
methods << f
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
methods
|
||||||
|
end
|
||||||
|
|
||||||
|
def translate_methods(methods , translator )
|
||||||
methods.each do |method|
|
methods.each do |method|
|
||||||
instruction = method.instructions
|
instruction = method.instructions
|
||||||
|
puts method.name
|
||||||
while instruction.next
|
while instruction.next
|
||||||
nekst = instruction.next
|
nekst = instruction.next
|
||||||
t = translator.translate(nekst) # returning nil means no replace
|
t = translator.translate(nekst) # returning nil means no replace
|
||||||
@ -44,12 +57,8 @@ module Register
|
|||||||
instruction = nekst
|
instruction = nekst
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
label = @init.next
|
|
||||||
@init = translator.translate( @init)
|
|
||||||
@init.append label
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# Objects are data and get assembled after functions
|
# Objects are data and get assembled after functions
|
||||||
def add_object o
|
def add_object o
|
||||||
return false if @objects[o.object_id]
|
return false if @objects[o.object_id]
|
||||||
|
@ -138,6 +138,9 @@ module Typed
|
|||||||
unless instruction.is_a?(Register::Instruction)
|
unless instruction.is_a?(Register::Instruction)
|
||||||
raise instruction.to_s
|
raise instruction.to_s
|
||||||
end
|
end
|
||||||
|
if( instruction.class.name.split("::").first == "Arm")
|
||||||
|
raise instruction.to_s
|
||||||
|
end
|
||||||
@current.insert(instruction) #insert after current
|
@current.insert(instruction) #insert after current
|
||||||
@current = instruction
|
@current = instruction
|
||||||
self
|
self
|
||||||
|
Loading…
Reference in New Issue
Block a user