GitLab will be upgraded on 30 Jan 2023 from 2.00 pm (AEDT) to 3.00 pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

test_binop.rs 20.2 KB
Newer Older
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
1
// Copyright 2017 The Australian National University
qinsoon's avatar
qinsoon committed
2
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
3
4
5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
qinsoon's avatar
qinsoon committed
6
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
7
//     http://www.apache.org/licenses/LICENSE-2.0
qinsoon's avatar
qinsoon committed
8
//
Isaac Oscar Gariano's avatar
Isaac Oscar Gariano committed
9
10
11
12
13
14
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

qinsoon's avatar
qinsoon committed
15
16
17
18
19
20
21
22
23
extern crate mu;
extern crate log;
extern crate libloading;

use self::mu::ast::types::*;
use self::mu::ast::ir::*;
use self::mu::ast::inst::*;
use self::mu::ast::op::*;
use self::mu::vm::*;
qinsoon's avatar
qinsoon committed
24
use self::mu::linkutils;
25
use mu::utils::LinkedHashMap;
qinsoon's avatar
qinsoon committed
26

27
28
29
30
use std::sync::Arc;
use self::mu::linkutils::aot;
use self::mu::runtime::thread::check_result;
use self::mu::compiler::*;
qinsoon's avatar
qinsoon committed
31

32
33
use std::u64;
use std::i64;
qinsoon's avatar
qinsoon committed
34

35
36
37
38
#[test]
fn test_udiv() {
    build_and_run_test!(udiv, udiv_test1);
    build_and_run_test!(udiv, udiv_test2);
qinsoon's avatar
qinsoon committed
39
40
41
42
43
}

fn udiv() -> VM {
    let vm = VM::new();

44
    typedef!    ((vm) int64 = mu_int(64));
qinsoon's avatar
qinsoon committed
45

46
47
48
    funcsig!    ((vm) udiv_sig = (int64, int64) -> (int64));
    funcdecl!   ((vm) <udiv_sig> udiv);
    funcdef!    ((vm) <udiv_sig> udiv VERSION udiv_v1);
qinsoon's avatar
qinsoon committed
49
50

    // %entry(<@int64> %a, <@int64> %b):
51
52
53
    block!      ((vm, udiv_v1) blk_entry);
    ssa!        ((vm, udiv_v1) <int64> a);
    ssa!        ((vm, udiv_v1) <int64> b);
qinsoon's avatar
qinsoon committed
54
55

    // %r = UDIV %a %b
56
57
58
59
    ssa!        ((vm, udiv_v1) <int64> r);
    inst!       ((vm, udiv_v1) blk_entry_udiv:
        r = BINOP (BinOp::Udiv) a b
    );
qinsoon's avatar
qinsoon committed
60
61

    // RET %r
62
63
64
    inst!       ((vm, udiv_v1) blk_entry_ret:
        RET (r)
    );
qinsoon's avatar
qinsoon committed
65

66
67
68
    define_block!((vm, udiv_v1) blk_entry(a, b) {
        blk_entry_udiv,
        blk_entry_ret
qinsoon's avatar
qinsoon committed
69
70
    });

71
72
73
    define_func_ver!((vm) udiv_v1(entry: blk_entry) {
        blk_entry
    });
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    emit_test! ((vm)
        udiv, udiv_test1, udiv_test1_v1,
        Int, Int RET Int,
        EQ,
        udiv_sig,
        int64(22), int64(4) RET int64(5),
    );

    emit_test! ((vm)
        udiv, udiv_test2, udiv_test2_v1,
        Int, Int RET Int,
        EQ,
        udiv_sig,
        int64(27), int64(7) RET int64(3),
    );

qinsoon's avatar
sdiv    
qinsoon committed
91
92
93
94
95
    vm
}

#[test]
fn test_sdiv() {
96
97
    build_and_run_test!(sdiv, sdiv_test1);
    build_and_run_test!(sdiv, sdiv_test2);
qinsoon's avatar
sdiv    
qinsoon committed
98
99
100
101
102
}

fn sdiv() -> VM {
    let vm = VM::new();

103
    typedef!    ((vm) int64 = mu_int(64));
qinsoon's avatar
sdiv    
qinsoon committed
104

105
106
107
    funcsig!    ((vm) sdiv_sig = (int64, int64) -> (int64));
    funcdecl!   ((vm) <sdiv_sig> sdiv);
    funcdef!    ((vm) <sdiv_sig> sdiv VERSION sdiv_v1);
qinsoon's avatar
sdiv    
qinsoon committed
108
109

    // %entry(<@int64> %a, <@int64> %b):
110
111
112
113
114
115
116
117
118
    block!      ((vm, sdiv_v1) blk_entry);
    ssa!        ((vm, sdiv_v1) <int64> a);
    ssa!        ((vm, sdiv_v1) <int64> b);

    // %r = sdiv %a %b
    ssa!        ((vm, sdiv_v1) <int64> r);
    inst!       ((vm, sdiv_v1) blk_entry_sdiv:
        r = BINOP (BinOp::Sdiv) a b
    );
qinsoon's avatar
sdiv    
qinsoon committed
119
120

    // RET %r
121
122
123
    inst!       ((vm, sdiv_v1) blk_entry_ret:
        RET (r)
    );
qinsoon's avatar
sdiv    
qinsoon committed
124

125
126
127
    define_block!((vm, sdiv_v1) blk_entry(a, b) {
        blk_entry_sdiv,
        blk_entry_ret
qinsoon's avatar
sdiv    
qinsoon committed
128
129
    });

130
131
132
    define_func_ver!((vm) sdiv_v1(entry: blk_entry) {
        blk_entry
    });
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

    emit_test! ((vm)
        sdiv, sdiv_test1, sdiv_test1_v1,
        Int, Int RET Int,
        EQ,
        sdiv_sig,
        int64(8), int64(2) RET int64(4),
    );

    emit_test! ((vm)
        sdiv, sdiv_test2, sdiv_test2_v1,
        Int, Int RET Int,
        EQ,
        sdiv_sig,
        int64(8), int64(-3i64 as u64) RET int64(-2i64 as u64),
    );

qinsoon's avatar
shl    
qinsoon committed
150
151
152
153
154
    vm
}

#[test]
fn test_shl() {
155
156
    build_and_run_test!(shl, shl_test1);
    build_and_run_test!(shl, shl_test2);
qinsoon's avatar
shl    
qinsoon committed
157
158
159
160
161
}

fn shl() -> VM {
    let vm = VM::new();

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    typedef!    ((vm) int64 = mu_int(64));

    funcsig!    ((vm) shl_sig = (int64, int64) -> (int64));
    funcdecl!   ((vm) <shl_sig> shl);
    funcdef!    ((vm) <shl_sig> shl VERSION shl_v1);

    // %entry(<@int64> %a, <@int64> %b):
    block!      ((vm, shl_v1) blk_entry);
    ssa!        ((vm, shl_v1) <int64> a);
    ssa!        ((vm, shl_v1) <int64> b);

    // %r = shl %a %b
    ssa!        ((vm, shl_v1) <int64> r);
    inst!       ((vm, shl_v1) blk_entry_shl:
        r = BINOP (BinOp::Shl) a b
    );
qinsoon's avatar
shl    
qinsoon committed
178
179

    // RET %r
180
181
182
    inst!       ((vm, shl_v1) blk_entry_ret:
        RET (r)
    );
qinsoon's avatar
shl    
qinsoon committed
183

184
185
186
    define_block!((vm, shl_v1) blk_entry(a, b) {
        blk_entry_shl,
        blk_entry_ret
qinsoon's avatar
shl    
qinsoon committed
187
188
    });

189
190
191
    define_func_ver!((vm) shl_v1(entry: blk_entry) {
        blk_entry
    });
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

    emit_test! ((vm)
        shl, shl_test1, shl_test1_v1,
        Int, Int RET Int,
        EQ,
        shl_sig,
        int64(1), int64(2) RET int64(4),
    );
    emit_test! ((vm)
        shl, shl_test2, shl_test2_v1,
        Int, Int RET Int,
        EQ,
        shl_sig,
        int64(2), int64(2) RET int64(8),
    );

qinsoon's avatar
qinsoon committed
208
209
210
211
212
    vm
}

#[test]
fn test_lshr() {
213
    build_and_run_test!(lshr, lshr_test1);
qinsoon's avatar
qinsoon committed
214
215
216
217
218
}

fn lshr() -> VM {
    let vm = VM::new();

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    typedef!    ((vm) int64 = mu_int(64));

    funcsig!    ((vm) lshr_sig = (int64, int64) -> (int64));
    funcdecl!   ((vm) <lshr_sig> lshr);
    funcdef!    ((vm) <lshr_sig> lshr VERSION lshr_v1);

    // %entry(<@int64> %a, <@int64> %b):
    block!      ((vm, lshr_v1) blk_entry);
    ssa!        ((vm, lshr_v1) <int64> a);
    ssa!        ((vm, lshr_v1) <int64> b);

    // %r = lshr %a %b
    ssa!        ((vm, lshr_v1) <int64> r);
    inst!       ((vm, lshr_v1) blk_entry_lshr:
        r = BINOP (BinOp::Lshr) a b
    );
qinsoon's avatar
qinsoon committed
235
236

    // RET %r
237
238
239
    inst!       ((vm, lshr_v1) blk_entry_ret:
        RET (r)
    );
qinsoon's avatar
qinsoon committed
240

241
242
243
    define_block!((vm, lshr_v1) blk_entry(a, b) {
        blk_entry_lshr,
        blk_entry_ret
qinsoon's avatar
qinsoon committed
244
245
    });

246
247
248
    define_func_ver!((vm) lshr_v1(entry: blk_entry) {
        blk_entry
    });
249
250
251
252
253
254
255
256
257

    emit_test! ((vm)
        lshr, lshr_test1, lshr_test1_v1,
        Int, Int RET Int,
        EQ,
        lshr_sig,
        int64(8), int64(3) RET int64(1),
    );

qinsoon's avatar
qinsoon committed
258
    vm
259
}
qinsoon's avatar
qinsoon committed
260

261
262
#[test]
fn test_add_simple() {
263
    build_and_run_test! (add, add_test1);
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
}

fn add() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int64 = mu_int(64));

    funcsig!    ((vm) sig = (int64, int64) -> (int64));
    funcdecl!   ((vm) <sig> add);
    funcdef!    ((vm) <sig> add VERSION add_v1);

    block!      ((vm, add_v1) blk_entry);
    ssa!        ((vm, add_v1) <int64> a);
    ssa!        ((vm, add_v1) <int64> b);

    // sum = Add %a %b
    ssa!        ((vm, add_v1) <int64> sum);
    inst!       ((vm, add_v1) blk_entry_add:
        sum = BINOP (BinOp::Add) a b
    );

    inst!       ((vm, add_v1) blk_entry_ret:
        RET (sum)
    );

    define_block!   ((vm, add_v1) blk_entry(a, b) {
        blk_entry_add, blk_entry_ret
    });

    define_func_ver!((vm) add_v1 (entry: blk_entry) {blk_entry});
294
295
296
297
298
299
300
301
302

    emit_test! ((vm)
        add, add_test1, add_test1_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(22), int64(27) RET int64(49),
    );

303
304
305
    vm
}

qinsoon's avatar
qinsoon committed
306
307
#[test]
fn test_add_int64_n() {
308
309
310
311
    build_and_run_test! (add_int64_n, add_int64_n_test1);
    build_and_run_test! (add_int64_n, add_int64_n_test2);
    build_and_run_test! (add_int64_n, add_int64_n_test3);
    build_and_run_test! (add_int64_n, add_int64_n_test4);
qinsoon's avatar
qinsoon committed
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
}

fn add_int64_n() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int64 = mu_int(64));
    typedef!    ((vm) int1  = mu_int(1));

    funcsig!    ((vm) sig = (int64, int64) -> (int1));
    funcdecl!   ((vm) <sig> add_int64_n);
    funcdef!    ((vm) <sig> add_int64_n VERSION add_int64_n_v1);

    block!      ((vm, add_int64_n_v1) blk_entry);
    ssa!        ((vm, add_int64_n_v1) <int64> a);
    ssa!        ((vm, add_int64_n_v1) <int64> b);

    // (sum, flag_n) = Add #N %a %b
    ssa!        ((vm, add_int64_n_v1) <int64> sum);
    ssa!        ((vm, add_int64_n_v1) <int1> flag_n);
    inst!       ((vm, add_int64_n_v1) blk_entry_add:
        sum, flag_n = BINOP_STATUS (BinOp::Add) (BinOpStatus::n()) a b
    );

    inst!       ((vm, add_int64_n_v1) blk_entry_ret:
        RET (flag_n)
    );

    define_block!   ((vm, add_int64_n_v1) blk_entry(a, b) {
        blk_entry_add, blk_entry_ret
    });

    define_func_ver!((vm) add_int64_n_v1 (entry: blk_entry) {blk_entry});
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

    emit_test! ((vm)
        add_int64_n, add_int64_n_test1, add_int64_n_test1_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(1), int64(1) RET int1(0),
    );
    emit_test! ((vm)
        add_int64_n, add_int64_n_test2, add_int64_n_test2_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(1), int64(-2i64 as u64) RET int1(1),
    );
    emit_test! ((vm)
        add_int64_n, add_int64_n_test3, add_int64_n_test3_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(1), int64(-1i64 as u64) RET int1(0),
    );
    emit_test! ((vm)
        add_int64_n, add_int64_n_test4, add_int64_n_test4_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(-1i64 as u64), int64(-1i64 as u64) RET int1(1),
    );

qinsoon's avatar
qinsoon committed
374
375
376
377
378
    vm
}

#[test]
fn test_add_int64_z() {
379
380
381
    build_and_run_test! (add_int64_z, add_int64_z_test1);
    build_and_run_test! (add_int64_z, add_int64_z_test2);
    build_and_run_test! (add_int64_z, add_int64_z_test3);
qinsoon's avatar
qinsoon committed
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
}

fn add_int64_z() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int64 = mu_int(64));
    typedef!    ((vm) int1  = mu_int(1));

    funcsig!    ((vm) sig = (int64, int64) -> (int1));
    funcdecl!   ((vm) <sig> add_int64_z);
    funcdef!    ((vm) <sig> add_int64_z VERSION add_int64_z_v1);

    block!      ((vm, add_int64_z_v1) blk_entry);
    ssa!        ((vm, add_int64_z_v1) <int64> a);
    ssa!        ((vm, add_int64_z_v1) <int64> b);

    // (sum, flag_n) = Add #N %a %b
    ssa!        ((vm, add_int64_z_v1) <int64> sum);
    ssa!        ((vm, add_int64_z_v1) <int1> flag_z);
    inst!       ((vm, add_int64_z_v1) blk_entry_add:
        sum, flag_z = BINOP_STATUS (BinOp::Add) (BinOpStatus::z()) a b
    );

    inst!       ((vm, add_int64_z_v1) blk_entry_ret:
        RET (flag_z)
    );

    define_block!   ((vm, add_int64_z_v1) blk_entry(a, b) {
        blk_entry_add, blk_entry_ret
    });

    define_func_ver!((vm) add_int64_z_v1 (entry: blk_entry) {blk_entry});
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436

    emit_test! ((vm)
        add_int64_z, add_int64_z_test1, add_int64_z_test1_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(1), int64(1) RET int1(0),
    );
    emit_test! ((vm)
        add_int64_z, add_int64_z_test2, add_int64_z_test2_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(1), int64(-2i64 as u64) RET int1(0),
    );
    emit_test! ((vm)
        add_int64_z, add_int64_z_test3, add_int64_z_test3_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(1), int64(-1i64 as u64) RET int1(1),
    );

qinsoon's avatar
qinsoon committed
437
438
439
440
441
    vm
}

#[test]
fn test_add_int64_c() {
442
443
    build_and_run_test! (add_int64_c, add_int64_c_test1);
    build_and_run_test! (add_int64_c, add_int64_c_test2);
qinsoon's avatar
qinsoon committed
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
}

fn add_int64_c() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int64 = mu_int(64));
    typedef!    ((vm) int1  = mu_int(1));

    funcsig!    ((vm) sig = (int64, int64) -> (int1));
    funcdecl!   ((vm) <sig> add_int64_c);
    funcdef!    ((vm) <sig> add_int64_c VERSION add_int64_c_v1);

    block!      ((vm, add_int64_c_v1) blk_entry);
    ssa!        ((vm, add_int64_c_v1) <int64> a);
    ssa!        ((vm, add_int64_c_v1) <int64> b);

    // (sum, flag_n) = Add #N %a %b
    ssa!        ((vm, add_int64_c_v1) <int64> sum);
    ssa!        ((vm, add_int64_c_v1) <int1> flag_c);
    inst!       ((vm, add_int64_c_v1) blk_entry_add:
        sum, flag_c = BINOP_STATUS (BinOp::Add) (BinOpStatus::c()) a b
    );

    inst!       ((vm, add_int64_c_v1) blk_entry_ret:
        RET (flag_c)
    );

    define_block!   ((vm, add_int64_c_v1) blk_entry(a, b) {
        blk_entry_add, blk_entry_ret
    });

    define_func_ver!((vm) add_int64_c_v1 (entry: blk_entry) {blk_entry});
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491

    emit_test! ((vm)
        add_int64_c, add_int64_c_test1, add_int64_c_test1_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(u64::MAX), int64(1) RET int1(1),
    );
    emit_test! ((vm)
        add_int64_c, add_int64_c_test2, add_int64_c_test2_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(i64::MAX as u64), int64(0) RET int1(0),
    );

qinsoon's avatar
qinsoon committed
492
493
494
495
496
    vm
}

#[test]
fn test_add_int64_v() {
497
498
499
500
    build_and_run_test! (add_int64_v, add_int64_v_test1);
    build_and_run_test! (add_int64_v, add_int64_v_test2);
    build_and_run_test! (add_int64_v, add_int64_v_test3);
    build_and_run_test! (add_int64_v, add_int64_v_test4);
qinsoon's avatar
qinsoon committed
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
}

fn add_int64_v() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int64 = mu_int(64));
    typedef!    ((vm) int1  = mu_int(1));

    funcsig!    ((vm) sig = (int64, int64) -> (int1));
    funcdecl!   ((vm) <sig> add_int64_v);
    funcdef!    ((vm) <sig> add_int64_v VERSION add_int64_v_v1);

    block!      ((vm, add_int64_v_v1) blk_entry);
    ssa!        ((vm, add_int64_v_v1) <int64> a);
    ssa!        ((vm, add_int64_v_v1) <int64> b);

    // (sum, flag_n) = Add #N %a %b
    ssa!        ((vm, add_int64_v_v1) <int64> sum);
    ssa!        ((vm, add_int64_v_v1) <int1> flag_v);
    inst!       ((vm, add_int64_v_v1) blk_entry_add:
        sum, flag_v = BINOP_STATUS (BinOp::Add) (BinOpStatus::v()) a b
    );

    inst!       ((vm, add_int64_v_v1) blk_entry_ret:
        RET (flag_v)
    );

    define_block!   ((vm, add_int64_v_v1) blk_entry(a, b) {
        blk_entry_add, blk_entry_ret
    });

    define_func_ver!((vm) add_int64_v_v1 (entry: blk_entry) {blk_entry});
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562

    emit_test! ((vm)
        add_int64_v, add_int64_v_test1, add_int64_v_test1_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(i64::MAX as u64), int64(1) RET int1(1),
    );
    emit_test! ((vm)
        add_int64_v, add_int64_v_test2, add_int64_v_test2_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(i64::MAX as u64), int64(0) RET int1(0),
    );
    emit_test! ((vm)
        add_int64_v, add_int64_v_test3, add_int64_v_test3_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(i64::MIN as u64), int64(0) RET int1(0),
    );
    emit_test! ((vm)
        add_int64_v, add_int64_v_test4, add_int64_v_test4_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(i64::MIN as u64), int64(-1i64 as u64) RET int1(1),
    );

qinsoon's avatar
qinsoon committed
563
564
565
566
567
    vm
}

#[test]
fn test_add_int64_nzc() {
568
569
    build_and_run_test! (add_int64_nzc, add_int64_nzc_test1);
    build_and_run_test! (add_int64_nzc, add_int64_nzc_test2);
qinsoon's avatar
qinsoon committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
}

fn add_int64_nzc() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int64 = mu_int(64));
    typedef!    ((vm) int8  = mu_int(8));
    typedef!    ((vm) int1  = mu_int(1));

    constdef!   ((vm) <int8> int8_1 = Constant::Int(1));
    constdef!   ((vm) <int8> int8_2 = Constant::Int(2));
    constdef!   ((vm) <int8> int8_3 = Constant::Int(3));

    funcsig!    ((vm) sig = (int64, int64) -> (int1));
    funcdecl!   ((vm) <sig> add_int64_nzc);
    funcdef!    ((vm) <sig> add_int64_nzc VERSION add_int64_nzc_v1);

    block!      ((vm, add_int64_nzc_v1) blk_entry);
    ssa!        ((vm, add_int64_nzc_v1) <int64> a);
    ssa!        ((vm, add_int64_nzc_v1) <int64> b);

    // (sum, flag_n) = Add #N %a %b
    ssa!        ((vm, add_int64_nzc_v1) <int64> sum);
    ssa!        ((vm, add_int64_nzc_v1) <int1> flag_n);
    ssa!        ((vm, add_int64_nzc_v1) <int1> flag_z);
    ssa!        ((vm, add_int64_nzc_v1) <int1> flag_c);

    inst!       ((vm, add_int64_nzc_v1) blk_entry_add:
qinsoon's avatar
qinsoon committed
598
599
600
        sum, flag_n, flag_z, flag_c =
            BINOP_STATUS (BinOp::Add)
            (BinOpStatus{flag_n: true, flag_z: true, flag_c: true, flag_v: false}) a b
qinsoon's avatar
qinsoon committed
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
    );

    ssa!        ((vm, add_int64_nzc_v1) <int8> shift_z);
    consta!     ((vm, add_int64_nzc_v1) int8_1_local = int8_1);
    inst!       ((vm, add_int64_nzc_v1) blk_entry_shift_z:
        shift_z = BINOP (BinOp::Shl) flag_z int8_1_local
    );

    ssa!        ((vm, add_int64_nzc_v1) <int8> ret);
    inst!       ((vm, add_int64_nzc_v1) blk_entry_add_ret1:
        ret = BINOP (BinOp::Add) flag_n shift_z
    );

    ssa!        ((vm, add_int64_nzc_v1) <int8> shift_c);
    consta!     ((vm, add_int64_nzc_v1) int8_2_local = int8_2);
    inst!       ((vm, add_int64_nzc_v1) blk_entry_shift_c:
        shift_c = BINOP (BinOp::Shl) flag_c int8_2_local
    );

    ssa!        ((vm, add_int64_nzc_v1) <int8> ret2);
    inst!       ((vm, add_int64_nzc_v1) blk_entry_add_ret2:
        ret2 = BINOP (BinOp::Add) ret shift_c
    );

    inst!       ((vm, add_int64_nzc_v1) blk_entry_ret:
        RET (ret2)
    );

    define_block!   ((vm, add_int64_nzc_v1) blk_entry(a, b) {
qinsoon's avatar
qinsoon committed
630
631
        blk_entry_add, blk_entry_shift_z, blk_entry_add_ret1,
        blk_entry_shift_c, blk_entry_add_ret2, blk_entry_ret
qinsoon's avatar
qinsoon committed
632
633
634
    });

    define_func_ver!((vm) add_int64_nzc_v1 (entry: blk_entry) {blk_entry});
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

    emit_test! ((vm)
        add_int64_nzc, add_int64_nzc_test1, add_int64_nzc_test1_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(u64::MAX), int64(1) RET int8(0b110),
    );
    emit_test! ((vm)
        add_int64_nzc, add_int64_nzc_test2, add_int64_nzc_test2_v1,
        Int, Int RET Int,
        EQ,
        sig,
        int64(u64::MAX), int64(0) RET int8(0b001),
    );

qinsoon's avatar
qinsoon committed
651
    vm
qinsoon's avatar
qinsoon committed
652
}
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774

#[test]
fn test_nest_mul_simple() {
    VM::start_logging_trace();

    let lib = linkutils::aot::compile_fnc("nest_mul_simple", &nest_mul_simple);
    unsafe {
        let nest_mul_simple: libloading::Symbol<unsafe extern "C" fn(u64, u64, u64) -> u64> =
            lib.get(b"nest_mul_simple").unwrap();

        let res = nest_mul_simple(2, 3, 4);
        println!("mul(2, 3, 4) = {}", res);
        assert_eq!(res, 24);
    }
}

fn nest_mul_simple() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int64 = mu_int(64));

    funcsig!    ((vm) sig = (int64, int64, int64) -> (int64));
    funcdecl!   ((vm) <sig> nest_mul_simple);
    funcdef!    ((vm) <sig> nest_mul_simple VERSION nest_mul_simple_v1);

    // %entry(%x, %y, %z)
    block!      ((vm, nest_mul_simple_v1) blk_entry);
    ssa!        ((vm, nest_mul_simple_v1) <int64> x);
    ssa!        ((vm, nest_mul_simple_v1) <int64> y);
    ssa!        ((vm, nest_mul_simple_v1) <int64> z);

    // %a = MUL %x %y
    ssa!        ((vm, nest_mul_simple_v1) <int64> a);
    inst!       ((vm, nest_mul_simple_v1) blk_entry_mul1:
        a = BINOP (BinOp::Mul) x y
    );

    // %b = MUL %a %z
    ssa!        ((vm, nest_mul_simple_v1) <int64> b);
    inst!       ((vm, nest_mul_simple_v1) blk_entry_mul2:
        b = BINOP (BinOp::Mul) a z
    );

    // RET b
    inst!       ((vm, nest_mul_simple_v1) blk_entry_ret:
        RET (b)
    );

    define_block!((vm, nest_mul_simple_v1) blk_entry(x, y, z) {
        blk_entry_mul1,
        blk_entry_mul2,
        blk_entry_ret
    });

    define_func_ver!((vm) nest_mul_simple_v1(entry: blk_entry) {
        blk_entry
    });

    vm
}

#[test]
fn test_nest_mul_times_10() {
    VM::start_logging_trace();

    let lib = linkutils::aot::compile_fnc("nest_mul_times_10", &nest_mul_times_10);
    unsafe {
        let nest_mul: libloading::Symbol<unsafe extern "C" fn(u64, u64) -> u64> =
            lib.get(b"nest_mul_times_10").unwrap();

        let res = nest_mul(2, 3);
        println!("mul(2, 3) x 10 = {}", res);
        assert_eq!(res, 60);
    }
}

fn nest_mul_times_10() -> VM {
    let vm = VM::new();

    typedef!    ((vm) int64 = mu_int(64));

    constdef!   ((vm) <int64> int64_10 = Constant::Int(10));

    funcsig!    ((vm) sig = (int64, int64) -> (int64));
    funcdecl!   ((vm) <sig> nest_mul_times_10);
    funcdef!    ((vm) <sig> nest_mul_times_10 VERSION nest_mul_times_10_v1);

    // %entry(%x, %y)
    block!      ((vm, nest_mul_times_10_v1) blk_entry);
    ssa!        ((vm, nest_mul_times_10_v1) <int64> x);
    ssa!        ((vm, nest_mul_times_10_v1) <int64> y);
    consta!     ((vm, nest_mul_times_10_v1) int64_10_local = int64_10);

    // %a = MUL %x %y
    ssa!        ((vm, nest_mul_times_10_v1) <int64> a);
    inst!       ((vm, nest_mul_times_10_v1) blk_entry_mul1:
        a = BINOP (BinOp::Mul) x y
    );

    // %b = MUL 10 %a
    ssa!        ((vm, nest_mul_times_10_v1) <int64> b);
    inst!       ((vm, nest_mul_times_10_v1) blk_entry_mul2:
        b = BINOP (BinOp::Mul) int64_10_local a
    );

    // RET b
    inst!       ((vm, nest_mul_times_10_v1) blk_entry_ret:
        RET (b)
    );

    define_block!((vm, nest_mul_times_10_v1) blk_entry(x, y) {
        blk_entry_mul1,
        blk_entry_mul2,
        blk_entry_ret
    });

    define_func_ver!((vm) nest_mul_times_10_v1(entry: blk_entry) {
        blk_entry
    });

    vm
}