-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathprimitives_spec.cr
392 lines (316 loc) · 8.29 KB
/
primitives_spec.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
require "../../spec_helper"
describe "Code gen: primitives" do
it "codegens bool" do
run("true").to_b.should be_true
run("false").to_b.should be_false
end
it "codegens int" do
run("1").to_i.should eq(1)
end
it "codegens long" do
run("1_i64").to_i.should eq(1)
end
it "codegens int128" do
# LLVM's JIT doesn't seem to support 128
# bit integers well regarding GenericValue
run(%(
require "prelude"
1_i128.to_i
)).to_i.should eq(1)
end
it "codegens uint128" do
# LLVM's JIT doesn't seem to support 128
# bit integers well regarding GenericValue
run(%(
require "prelude"
1_u128.to_i
)).to_i.should eq(1)
end
it "codegens char" do
run("'a'").to_i.should eq('a'.ord)
end
it "codegens char ord" do
run("'a'.ord").to_i.should eq('a'.ord)
end
it "codegens f32" do
run("2.5_f32").to_f32.should eq(2.5_f32)
end
it "codegens f64" do
run("2.5_f64").to_f64.should eq(2.5_f64)
end
it "codegens string" do
run(%("foo")).to_string.should eq("foo")
end
describe "arithmetic primitives" do
# more detailed tests are done through the `primitives_spec` suite on a new
# generation of the compiler
it "codegens 1 + 2" do
run(%(require "prelude"; 1 + 2)).to_i.should eq(3)
end
it "codegens 1 &+ 2" do
run(%(1 &+ 2)).to_i.should eq(3)
end
it "codegens 1 - 2" do
run(%(require "prelude"; 1 - 2)).to_i.should eq(-1)
end
it "codegens 1 &- 2" do
run(%(1 &- 2)).to_i.should eq(-1)
end
it "codegens 2 * 3" do
run(%(require "prelude"; 2 * 3)).to_i.should eq(6)
end
it "codegens 2 &* 3" do
run(%(2 &* 3)).to_i.should eq(6)
end
it "codegens 8.unsafe_div 3" do
run(%(8.unsafe_div 3)).to_i.should eq(2)
end
it "codegens 8.unsafe_mod 3" do
run(%(10.unsafe_mod 3)).to_i.should eq(1)
end
it "codegens 16.unsafe_shr 2" do
run(%(16.unsafe_shr 2)).to_i.should eq(4)
end
it "codegens 16.unsafe_shl 2" do
run(%(16.unsafe_shl 2)).to_i.should eq(64)
end
it "codegens 1.to_i16!" do
run("1.to_i16!").to_i.should eq(1)
end
it "codegens 1.to_i16" do
run(%(require "prelude"; 1.to_i16)).to_i.should eq(1)
end
it "codegens 1.to_f!" do
run("1.to_f!").to_f64.should eq(1.0)
end
it "codegens 1.to_f" do
run(%(require "prelude"; 1.to_f)).to_f64.should eq(1.0)
end
it "skips bounds checking when to_i produces same type" do
run("1.to_i32").to_i.should eq(1)
end
end
it "defined method that calls primitive (bug)" do
run("
struct Int64
def foo
to_u64!
end
end
a = 1_i64
a.foo.to_i!
").to_i.should eq(1)
end
it "codegens __LINE__" do
run("
__LINE__
", inject_primitives: false).to_i.should eq(3)
end
it "codegens crystal_type_id with union type" do
run("
class Foo
end
class Bar < Foo
end
f = Foo.allocate || Bar.allocate
f.crystal_type_id == Foo.allocate.crystal_type_id
").to_b.should be_true
end
it "doesn't treat `(1 == 1) == true` as `1 == 1 == true` (#328)" do
run("(1 == 1) == true").to_b.should be_true
end
it "passes issue #328" do
run("((1 == 1) != (2 == 2))").to_b.should be_false
end
pending "codegens pointer of int" do
run(%(
ptr = Pointer(Int).malloc(1_u64)
ptr.value = 1
ptr.value = 2_u8
ptr.value = 3_u16
ptr.value = 4_u32
(ptr.value + 1).to_i32
)).to_i.should eq(5)
end
pending "sums two numbers out of an [] of Number" do
run(%(
p = Pointer(Number).malloc(2_u64)
p.value = 1
(p + 1_i64).value = 1.5
(p.value + (p + 1_i64).value).to_f32
)).to_f32.should eq(2.5)
end
it "codegens crystal_type_id for class" do
codegen(%(String.crystal_type_id))
end
it "can invoke cast on primitive typedef (#614)" do
codegen(%(
lib Test
type K = Int32
fun foo : K
end
Test.foo.to_i!
))
end
it "can invoke binary on primitive typedef (#614)" do
codegen(%(
lib Test
type K = Int32
fun foo : K
end
Test.foo &+ 1
))
end
it "allows redefining a primitive method" do
run(%(
struct Int32
def *(other : Int32)
42
end
end
1 * 2
)).to_i.should eq(42)
end
it "doesn't optimize away call whose obj is not passed as self (#2226)" do
run(%(
class Global
@@x = 0
def self.x=(@@x)
end
def self.x
@@x
end
end
def foo
Global.x = 2
3
end
foo.class.crystal_type_id
Global.x
)).to_i.should eq(2)
end
it "uses built-in llvm function that returns a tuple" do
run(%(
lib Intrinsics
fun sadd_i32_with_overflow = "llvm.sadd.with.overflow.i32"(a : Int32, b : Int32) : {Int32, Bool}
end
x, o = Intrinsics.sadd_i32_with_overflow(1, 2)
x
)).to_i.should eq(3)
end
it "gets crystal class instance type id" do
run(%(
class Foo
end
Foo.new.crystal_type_id == Foo.crystal_instance_type_id
)).to_b.should be_true
end
describe "va_arg" do
# On Windows and AArch64 llvm's va_arg instruction works incorrectly.
{% unless flag?(:win32) || flag?(:aarch64) %}
it "uses llvm's va_arg instruction" do
mod = codegen(%(
struct VaList
@[Primitive(:va_arg)]
def next(type)
end
end
list = VaList.new
list.next(Int32)
))
type = {% if LibLLVM::IS_LT_150 %} "%VaList*" {% else %} "ptr" {% end %}
str = mod.to_s
str.should contain("va_arg #{type} %list")
end
it "works with C code" do
test_c(
%(
extern int foo_f(int,...);
int foo() {
return foo_f(3,1,2,3);
}
),
%(
lib LibFoo
fun foo() : LibC::Int
end
fun foo_f(count : Int32, ...) : LibC::Int
sum = 0
VaList.open do |list|
count.times do |i|
sum += list.next(Int32)
end
end
sum
end
LibFoo.foo
), &.to_i.should eq(6))
end
{% end %}
end
describe "atomicrmw" do
it "codegens atomicrmw with enums" do
run(<<-CRYSTAL).to_i.should eq(3)
enum RMWBinOp
Add = 1
end
enum Ordering
SequentiallyConsistent = 7
end
@[Primitive(:atomicrmw)]
def atomicrmw(op : RMWBinOp, ptr : Int32*, val : Int32, ordering : Ordering, singlethread : Bool) : Int32
end
x = 1
atomicrmw(:add, pointerof(x), 2, :sequentially_consistent, false)
x
CRYSTAL
end
it "codegens atomicrmw with enums" do
run(<<-CRYSTAL).to_i.should eq(3)
enum RMWBinOp
Add = 1
end
enum Ordering
SequentiallyConsistent = 7
end
@[Primitive(:atomicrmw)]
def atomicrmw(op : RMWBinOp, ptr : Int32*, val : Int32, ordering : Ordering, singlethread : Bool) : Int32
end
x = 1
atomicrmw(RMWBinOp::Add, pointerof(x), 2, Ordering::SequentiallyConsistent, false)
x
CRYSTAL
end
# TODO: remove once support for 1.4 is dropped
it "codegens atomicrmw with symbols" do
run(<<-CRYSTAL).to_i.should eq(3)
@[Primitive(:atomicrmw)]
def atomicrmw(op : Symbol, ptr : Int32*, val : Int32, ordering : Symbol, singlethread : Bool) : Int32
end
x = 1
atomicrmw(:add, pointerof(x), 2, :sequentially_consistent, false)
x
CRYSTAL
end
end
it "allows @[Primitive] on method that has body" do
run(%(
module Moo
@[Primitive(:symbol_to_s)]
def self.symbol_to_s(symbol : Symbol) : String
untyped
end
end
Moo.symbol_to_s(:hello)
)).to_string.should eq("hello")
end
it "allows @[Primitive] on fun declarations" do
run(%(
lib LibFoo
@[Primitive(:enum_value)]
fun enum_value(x : Int32) : Int32
end
LibFoo.enum_value(1)
)).to_i.should eq(1)
end
end