Skip to content

Commit

Permalink
Replaced Bignum#~, Bignum#-@ primitives.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Jan 7, 2020
1 parent 820ba47 commit 0c0028b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 52 deletions.
10 changes: 0 additions & 10 deletions core/bignum.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
class Bignum < Integer
# unary operators

def ~
Rubinius.primitive :bignum_invert
raise PrimitiveFailure, "Bignum#~ primitive failed"
end

def -@
Rubinius.primitive :bignum_neg
raise PrimitiveFailure, "Bignum#-@ primitive failed"
end

# binary math operators

def bit_length
Expand Down
42 changes: 0 additions & 42 deletions core/fixnum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,6 @@ def self.===(obj)

# unary operators

def !
false
end

def ~
Rubinius.asm do
push_self

r0 = new_register

r_load_stack r0
pop

r_load_int r0, r0
n_inot r0, r0
r_store_int r0, r0

r_ret r0

# TODO: teach the bytecode compiler better
push_true
end
end

def -@
Rubinius.asm do
push_self

r0 = new_register

r_load_stack r0
pop

n_ineg_o r0, r0

r_ret r0

# TODO: teach the bytecode compiler better
push_true
end
end

# binary math operators

def divmod(other)
Expand Down
99 changes: 99 additions & 0 deletions core/integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,105 @@ def self.induced_from(obj)
end
end

# unary operators

def !
false
end

def ~
Rubinius.asm do
eint = new_label
val = new_label
done = new_label

r0 = new_register
r1 = new_register
r2 = new_register
r3 = new_register
r4 = new_register

push_self
r_load_stack r0
pop

n_promote r1, r0, r0

r_load_1 r2
n_iadd r3, r2, r2
n_ieq r4, r1, r3
b_if r4, eint

n_iadd r3, r3, r2
n_ine r4, r1, r3
b_if r4, done

r_load_int r0, r0
n_inot r0, r0
r_store_int r0, r0
goto val

eint.set!
n_enot r0, r0

val.set!
r_ret r0

done.set!

# TODO: teach the bytecode compiler better
push_true
end

super
end

def -@
Rubinius.asm do
eint = new_label
val = new_label
done = new_label

r0 = new_register
r1 = new_register
r2 = new_register
r3 = new_register
r4 = new_register

push_self
r_load_stack r0
pop

n_promote r1, r0, r0

r_load_1 r2
n_iadd r3, r2, r2
n_ieq r4, r1, r3
b_if r4, eint

n_iadd r3, r3, r2
n_ine r4, r1, r3
b_if r4, done

n_ineg_o r0, r0
goto val

eint.set!
n_eneg r0, r0

val.set!
r_ret r0

done.set!

# TODO: teach the bytecode compiler better
push_true
end

super
end


# binary math operators

def +(other)
Expand Down
3 changes: 3 additions & 0 deletions machine/instructions/n_eneg.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "instructions.hpp"

#include "class/bignum.hpp"

namespace rubinius {
namespace instructions {
inline void n_eneg(STATE, CF, R0, R1) {
RVAL(r0) = as<Bignum>(RVAL(r1))->neg(state);
}
}
}
3 changes: 3 additions & 0 deletions machine/instructions/n_enot.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "instructions.hpp"

#include "class/bignum.hpp"

namespace rubinius {
namespace instructions {
inline void n_enot(STATE, CF, R0, R1) {
RVAL(r0) = as<Bignum>(RVAL(r1))->invert(state);
}
}
}

0 comments on commit 0c0028b

Please sign in to comment.