forked from tomhea/flip-jump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runlib.fj
118 lines (75 loc) · 2.02 KB
/
runlib.fj
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
// Every line is (running) bananas!
// This file contains a width-independent code
// This file contains constants and labels used by other standard library files.
// Everything after // is ignored, and every whitespace is ignored (besides new line)
// An opcode is: F;J F; ;J or just ; (no F => F=0; no J => J=next)
// Complexity note: phi is log2 of the total number of fj operations, counting wflips as one op.
// so 2 wflips with value=label (while label is padded with 2^n) causes phi-n space/time complexity
// The complexities are not updated in this file (should be lower/faster).
// w = ?? // memory and operands width. Is defined at compile time.
dww = #w // double-w-width (log2(2w))
ww = dww-1 // w-width (log2(w))
dw = 2 * w // double word size
dbit = w + dww // bit-distance from variable start to bit value (w+dww)
// ---------- Startup Code
def startup > IO, code_start {
;code_start // 0w;1w : code start
IO:
;0 // 2w;3w : now points to io_handler
code_start:
// 4w;5w : start of code
}
def startup in0_handler, in1_handler {
startup
default_input in0_handler, in1_handler
}
// ---------- Basic Functionality
def zero_op {
0;0
}
def fj f, j {
f;j
}
def wflip_macro dst, val {
wflip dst, val
}
def wflip_macro dst, val, jmp_addr {
wflip dst, val, jmp_addr
}
def pad x @ pad_start {
pad_start:
rep((0-pad_start/(2*w))%x, i) zero_op
}
// ---------- Compilation Time:
// Complexity: 1
def comp_if expr, l0, l1 {
; expr ? l1 : l0
}
def comp_if0 expr, l0 @ l1 {
comp_if expr, l0, l1
l1:
}
def comp_if1 expr, l1 @ l0 {
comp_if expr, l0, l1
l0:
}
def comp_flip_if bit, expr {
(expr ? bit : 0);
}
// ---------- Unconditional Jump
// Complexity: 1
def skip {
;$ + dw
}
def loop {
;$ - dw
}
// ---------- Input Handler
def default_input in0_handler, in1_handler @ io_handler, end < IO {
wflip IO+w, io_handler, end
pad 2
io_handler:
;in0_handler
;in1_handler
end:
}