From d0b655d05f98f17cc2cab3b9973c36333e971f67 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 21 Nov 2015 14:20:25 +0200 Subject: [PATCH] implement the div10 as assembler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit easy after the some version worked because it’s not arm it probably twice as long as need be better (any?) optimisation would take care of that still, nice to see it works --- lib/register/boot.rb | 2 +- lib/register/builtin/integer.rb | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/register/boot.rb b/lib/register/boot.rb index 229ed9da..a6cfed7b 100644 --- a/lib/register/boot.rb +++ b/lib/register/boot.rb @@ -156,7 +156,7 @@ module Register end obj = @space.get_class_by_name(:Integer) - [ :putint, :mod4].each do |f| #mod4 is just a forward declaration + [ :putint, :mod4, :div10].each do |f| #mod4 is just a forward declaration obj.add_instance_method Builtin::Integer.send(f , nil) end end diff --git a/lib/register/builtin/integer.rb b/lib/register/builtin/integer.rb index dc8c8458..5b8942ba 100644 --- a/lib/register/builtin/integer.rb +++ b/lib/register/builtin/integer.rb @@ -14,6 +14,60 @@ module Register return compiler.method end + + def div10 context + s = "div_10" + compiler = Soml::Compiler.new.create_method(:Integer,:div10 ).init_method + me = compiler.process( s(:name , :self) ) + tmp = compiler.process( s(:name , :self) ) + q = compiler.process( s(:name , :self) ) + const = compiler.process( s(:int , 1) ) + # int tmp = self >> 1 + compiler.add_code Register.op( s , ">>" , tmp , const) + # int q = self >> 2 + compiler.add_code Register.load_constant( s , 2 , const) + compiler.add_code Register.op( s , ">>" , q , const) + # q = q + tmp + compiler.add_code Register.op( s , "+" , q , tmp ) + # tmp = q >> 4 + compiler.add_code Register.load_constant( s , 4 , const) + compiler.add_code Register.transfer( s, q , tmp) + compiler.add_code Register.op( s , ">>" , tmp , const) + # q = q + tmp + compiler.add_code Register.op( s , "+" , q , tmp ) + # tmp = q >> 8 + compiler.add_code Register.load_constant( s , 8 , const) + compiler.add_code Register.transfer( s, q , tmp) + compiler.add_code Register.op( s , ">>" , tmp , const) + # q = q + tmp + compiler.add_code Register.op( s , "+" , q , tmp ) + # tmp = q >> 16 + compiler.add_code Register.load_constant( s , 16 , const) + compiler.add_code Register.transfer( s, q , tmp) + compiler.add_code Register.op( s , ">>" , tmp , const) + # q = q + tmp + compiler.add_code Register.op( s , "+" , q , tmp ) + # q = q >> 3 + compiler.add_code Register.load_constant( s , 3 , const) + compiler.add_code Register.op( s , ">>" , q , const) + # tmp = q * 10 + compiler.add_code Register.load_constant( s , 10 , const) + compiler.add_code Register.transfer( s, q , tmp) + compiler.add_code Register.op( s , "*" , tmp , const) + # tmp = self - tmp + compiler.add_code Register.op( s , "-" , me , tmp ) + compiler.add_code Register.transfer( s , me , tmp) + # tmp = tmp + 6 + compiler.add_code Register.load_constant( s , 6 , const) + compiler.add_code Register.op( s , "+" , tmp , const ) + # tmp = tmp >> 4 + compiler.add_code Register.load_constant( s , 4 , const) + compiler.add_code Register.op( s , ">>" , tmp , const ) + # return q + tmp + compiler.add_code Register.op( s , "+" , q , tmp ) + compiler.add_code Register.set_slot( s , q , :message , :return_value) + return compiler.method + end end extend ClassMethods end