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
# An abstraction for conditionals.
#
# Example usage:
#
# function.if(condition) {
# # condition is true
# } .elsif(condition2) {
# # condition2 is true
# } .else {
# # condition1 and condition2 are false
# } .end
#
# Caution: if you omit end, then the generated code will have
# undefined behavior, but there will be no warning generated.
false_label = Label.new
insn_branch_if_not(cond, false_label)
block.call
insn_branch(end_label)
insn_label(false_label)
return If.new(self, end_label)
end
# An abstraction for an inverted conditional.
#
# Example usage:
#
# function.unless(condition) {
# # condition is false
# } .elsunless(condition2) {
# # condition2 is false
# } .else {
# # condition1 and condition2 are true
# } .end
#
# Caution: if you omit end, then the generated code will have
# undefined behavior, but there will be no warning generated.
true_label = Label.new
insn_branch_if(cond, true_label)
block.call
insn_branch(end_label)
insn_label(true_label)
return If.new(self, end_label)
end
# :nodoc:
@function = function
@end_label = end_label
end
block.call
return self
end
return @function.if(cond, @end_label, &block)
end
return @function.unless(cond, @end_label, &block)
end
@function.insn_label(@end_label)
end
end
# An abstraction for a multi-way conditional.
#
# Example usage:
#
# function.case(value1)
# .when(value2) {
# # value1 == value2
# }.when(value3) {
# # value1 == value3
# } .else {
# # all other cases fell through
# } .end
#
# Caution: if you omit end, then the generated code will have
# undefined behavior, but there will be no warning generated.
return Case.new(self, value)
end
# :nodoc:
@function = function
@value = value
@if = nil
end
if not @if then
@if = @function.if(value == @value, &block)
else
@if.elsif(value == @value, &block)
end
return self
end
@if.else(&block)
end
@if.end if @if
end
end
# Usage:
#
# until { <condition> }.do {
# # loop body
# } .end
#
start_label = Label.new
done_label = Label.new
insn_label(start_label)
insn_branch_if(block.call, done_label)
loop = Loop.new(self, start_label, done_label)
return loop
end
# Usage:
#
# while { <condition> }.do {
# # loop body
# } .end
#
start_label = Label.new
done_label = Label.new
insn_label(start_label)
insn_branch_if_not(block.call, done_label)
loop = Loop.new(self, start_label, done_label)
return loop
end
# :nodoc:
@function = function
@start_label = start_label
@redo_label = start_label
@done_label = done_label
end
block.call(self)
return self
end
@function.insn_branch(@start_label)
@function.insn_label(@done_label)
end
@function.insn_branch(@done_label)
end
@function.insn_branch(@redo_label)
end
@redo_label = JIT::Label.new
@function.insn_label(@redo_label)
end
end
# An alias for get_param
self.get_param(n)
end
# An alias for insn_return
self.insn_return(result)
end
# Create a JIT::Context and compile a new function within that
# context.
JIT::Context.build do
JIT::Function.compile(context, *args, &block)
end
end
end
end