1
1
class Fixnum < Integer
2
2
3
- def initialize
4
- # Can't use any Ruby expressions that use integers here,
5
- # directly or indirectly, so best not use *any*, because
6
- # it would cause recursion until running out of memory.
7
- %s(assign @value 0)
8
- end
9
-
10
- def self . allocate
11
- %s(assign ob (__array_leaf 2))
12
- %s(assign (index ob 0) self)
13
- ob
3
+ def class
4
+ Fixnum
14
5
end
15
6
16
7
def % other
17
8
%s(assign r (callm other __get_raw))
18
- %s(assign m (mod @value r))
9
+ %s(assign m (mod (sar self) r))
19
10
%s(if (eq (ge m 0) (lt r 0))
20
11
(assign m (add m r)))
21
12
%s(__int m)
22
13
end
23
14
24
- def __set_raw ( value )
25
- @value = value
26
- end
27
-
28
15
def __get_raw
29
- @value
16
+ %s(sar self)
30
17
end
31
18
32
19
def to_i
@@ -75,31 +62,32 @@ def inspect
75
62
end
76
63
77
64
def chr
78
- %s(let (buf)
79
- (assign buf (__alloc_leaf 2))
80
- (snprintf buf 2 "%c" @value )
65
+ %s(let (buf)
66
+ (assign buf (__alloc_leaf 2))
67
+ (snprintf buf 2 "%c" (sar self) )
81
68
(__get_string buf)
82
- )
69
+ )
83
70
end
84
71
85
72
def + other
86
- %s(call __int (( add @value (callm other __get_raw) )))
73
+ %s(__int (add (sar self) (callm other __get_raw)))
87
74
end
88
75
89
76
def - other
90
- %s(call __int (( sub @value (callm other __get_raw) )))
77
+ %s(__int (sub (sar self) (callm other __get_raw)))
91
78
end
92
79
80
+
93
81
def <= other
94
- %s(if (le @value (callm other __get_raw)) true false)
82
+ %s(if (le (sar self) (callm other __get_raw)) true false)
95
83
end
96
84
97
85
def == other
98
86
if other . nil?
99
87
return false
100
88
end
101
89
return false if !other . is_a? ( Numeric )
102
- %s(if (eq @value (callm other __get_raw)) true false)
90
+ %s(if (eq (sar self) (callm other __get_raw)) true false)
103
91
end
104
92
105
93
# FIXME: I don't know why '!' seems to get an argument...
@@ -110,19 +98,19 @@ def ! *args
110
98
def != other
111
99
return true if !other . is_a? ( Numeric )
112
100
other = other . to_i
113
- %s(if (ne @value (callm other __get_raw)) true false)
101
+ %s(if (ne (sar self) (callm other __get_raw)) true false)
114
102
end
115
103
116
104
def < other
117
- %s(if (lt @value (callm other __get_raw)) true false)
105
+ %s(if (lt (sar self) (callm other __get_raw)) true false)
118
106
end
119
107
120
108
def > other
121
- %s(if (gt @value (callm other __get_raw)) true false)
109
+ %s(if (gt (sar self) (callm other __get_raw)) true false)
122
110
end
123
111
124
112
def >= other
125
- %s(if (ge @value (callm other __get_raw)) true false)
113
+ %s(if (ge (sar self) (callm other __get_raw)) true false)
126
114
end
127
115
128
116
def <=> other
@@ -137,22 +125,22 @@ def <=> other
137
125
end
138
126
139
127
def div other
140
- %s(call __int (( div @value (callm other __get_raw) )))
128
+ %s(__int (div (sar self) (sar other)))
141
129
end
142
130
143
131
def mul other
144
- %s(call __int (( mul @value (callm other __get_raw) )))
132
+ %s(__int (mul (sar self) (sar other)))
145
133
end
146
134
147
135
# These two definitions are only acceptable temporarily,
148
136
# because we will for now only deal with integers
149
137
150
138
def * other
151
- mul ( other )
139
+ %s(__int ( mul (sar self) (sar other)) )
152
140
end
153
141
154
142
def / other
155
- div ( other )
143
+ %s(__int ( div (sar self) (sar other)) )
156
144
end
157
145
158
146
def ord
@@ -168,27 +156,7 @@ def times
168
156
end
169
157
end
170
158
171
- %s(assign FixNum_cache_size 1000)
172
- %s(assign FixNum_cache (__array_leaf (mul FixNum_cache_size 2)))
173
-
174
159
%s(defun __int (val)
175
- (let (num)
176
- (if (and (ge val 0) (lt val FixNum_cache_size))
177
- (do
178
- # 32 bit class-ptr + 32 bit int; Naughty assumptions again. FIXME
179
- (assign num (add FixNum_cache (mul val 8)))
180
- (if (eq (index num 0) 0) (do
181
- (assign (index num 0) Fixnum) # class-ptr
182
- (callm num __set_raw (val))
183
- (return num)
184
- ))
185
- (return num)
186
- )
187
- )
188
- (assign num (callm Fixnum allocate))
189
- (callm num __set_raw (val))
190
- (return num)
191
- )
192
- )
193
-
194
- %s(__compiler_internal integer_list)
160
+ (add (shl val) 1)
161
+ )
162
+
0 commit comments