forked from sampsyo/bril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimes-between.bril
58 lines (57 loc) · 1.41 KB
/
primes-between.bril
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
# ARGS: 1 1000
# Prints the primes in the interval [a, b]
@main(a : int, b : int) {
.for.outer.init: # start at interval unless a < 2, then start at 2.
t0 : int = const 2;
t1 : bool = lt a t0;
br t1 .true .false;
.true:
t2 : int = const 2; #i
jmp .for.outer.cond;
.false:
t2 : int = id a; # i
.for.outer.cond:
t3 : bool = le t2 b;
br t3 .for.outer.body .for.outer.end;
.for.outer.body:
t4 : int = const 1;
t5 : bool = eq t4 t4; # isPrime
.for.inner.init:
t6 : int = const 2; # j
t7 : int = const 2;
.for.inner.cond:
t8 : int = div t2 t7;
t9 : bool = le t6 t8;
br t9 .for.inner.body .for.inner.end;
.for.inner.body:
t10 : int = call @mod t2 t6;
t11 : int = const 0;
t12 : bool = eq t10 t11;
br t12 .if.inner.body .if.inner.end;
.if.inner.body:
t13 : int = const 1;
t14 : int = const 2;
t5 : bool = eq t13 t14; # t13 and t14 never equal to eachother
jmp .for.inner.end;
.if.inner.end:
t15 : int = const 1;
t6 : int = add t15 t6;
jmp .for.inner.cond;
.for.inner.end:
t16 : int = const 1;
t6 : int = add t6 t16;
br t5 .if.outer.body .if.outer.end;
.if.outer.body:
print t2;
.if.outer.end:
t17 : int = const 1;
t2 : int = add t2 t17;
jmp .for.outer.cond;
.for.outer.end:
}
@mod(a : int, b : int) : int {
t1 : int = div a b;
t2 : int = mul b t1;
t3 : int = sub a t2;
ret t3;
}