Mercurial > repos > rhope
comparison number.rhope @ 184:c6ba9fe45910
Strip _c from filenames of current compiler
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 27 Jul 2011 21:28:43 -0700 |
parents | number_c.rhope@fd06fb07762a |
children |
comparison
equal
deleted
inserted
replaced
183:24c6f8767190 | 184:c6ba9fe45910 |
---|---|
1 | |
2 | |
3 Compile Number Method[backend, op, type:out] | |
4 { | |
5 name <- [[[op]Index[0]]Append["@"]]Append[type] | |
6 backend func <- [op]Index[1] | |
7 type inst <- Type Instance[type] | |
8 func <- [[[[backend]Create Function[name,("a","b"), ("out"), "rhope"] | |
9 ]Set Input Type[type inst, 0] | |
10 ]Set Input Type[type inst, 1] | |
11 ]Set Output Type[type inst, 0] | |
12 | |
13 ,ina <- [[func]Copy["a"] | |
14 ]Read Field["a", "Num"] | |
15 { ,inb <- [~]Read Field["b", "Num"] | |
16 { ,outa <- [~]Write Field["a", "Num"] | |
17 { after op <- [backend func]Call[~, ina, inb, outa] }}} | |
18 | |
19 out <- [backend]Store Function[ [[after op]Move["a","out"]]Release["b"] ] | |
20 | |
21 } | |
22 | |
23 Compile Number Inline[func,op,type,a,b,outvar:out] | |
24 { | |
25 ,ina <- [ | |
26 [[func]Move[a, outvar] | |
27 ]Copy[outvar] | |
28 ]Read Field[Cast[outvar, type], "Num"] | |
29 { ,inb <- [~]Read Field[Cast[Strip Addref[b], type], "Num"] | |
30 { ,outa <- [~]Write Field[Cast[outvar, type], "Num"] | |
31 { out <- [op]Call[~, ina, inb, outa] }}} | |
32 } | |
33 | |
34 Compile Number Comp Inline[func,op,type,a,b,outvar:out] | |
35 { | |
36 ,ina <- [ | |
37 [func]Allocate Var["compres", [Type Instance["Int32"]]Set Variant["Naked"]] | |
38 ]Read Field[Cast[Strip Addref[a], type], "Num"] | |
39 { ,inb <- [~]Read Field[Cast[Strip Addref[b], type], "Num"] | |
40 { after op <- [op]Call[~, ina, inb, "compres"] }} | |
41 | |
42 [after op]Do If["compres", [[after op]Instruction Stream]Move[Constant["Boolean_Yes"], outvar]] | |
43 { out <- [~]Do If[NotCond["compres"], [[after op]Instruction Stream]Move[Constant["Boolean_No"], outvar]] } | |
44 } | |
45 | |
46 Compile Number Inline Check[func,op,type a,type b,a,b,outvar:out,no inline] | |
47 { | |
48 opmap <- [[[[[[[[Dictionary[] | |
49 ]Set["+", Add[?]] | |
50 ]Set["-", Sub[?]] | |
51 ]Set["*", Multiply[?]] | |
52 ]Set["/", Divide[?]] | |
53 ]Set["LShift", DoLShift[?]] | |
54 ]Set["RShift", DoRShift[?]] | |
55 ]Set["&", BitAnd[?]] | |
56 ]Set["|", BitOr[?]] | |
57 backend op <- [opmap]Index[op] | |
58 { inline func <- Val[Compile Number Inline[?]] } | |
59 { | |
60 comp opmap <- [[[[[[Dictionary[] | |
61 ]Set["<", CompLess[?]] | |
62 ]Set[">", CompGreater[?]] | |
63 ]Set["=", CompEqual[?]] | |
64 ]Set["<=", CompLessEqual[?]] | |
65 ]Set[">=", CompGreaterEqual[?]] | |
66 ]Set["!=", CompNotEqual[?]] | |
67 backend op, no inline <- [comp opmap]Index[op] | |
68 { inline func <- Val[Compile Number Comp Inline[?]] } | |
69 } | |
70 | |
71 | |
72 Val[backend op] | |
73 { | |
74 signed?,int,size,no inline <- [[type a]Name >>]Partition["Int"] | |
75 { | |
76 ,no inline <- If[[[signed?] = ["U"]] Or [[signed?] = [""]]] | |
77 { | |
78 ,no inline <- [("8","16","32","64")]Find[=[size, ?]] | |
79 { | |
80 //TODO: Do an inline type check/conversion rather than falling back on a standard call | |
81 ,no inline <- If[[type b] = [type a]] | |
82 { | |
83 out <- [inline func]Call[func, backend op, type a, a, b, outvar] | |
84 } | |
85 } | |
86 } | |
87 } | |
88 } | |
89 } | |
90 | |
91 Compile Number Comp Method[backend, op, type:out] | |
92 { | |
93 name <- [ [ [op]Index[0] ]Append["@"] ]Append[type] | |
94 backend func <- [op]Index[1] | |
95 type inst <- Type Instance[type] | |
96 func <- [[[[[[ [backend]Create Function[name,("a","b"), ("out"), "rhope"] | |
97 ]Set Input Type[type inst, 0] | |
98 ]Set Input Type[type inst, 1] | |
99 ]Set Output Type[Type Instance["Boolean"], 0] | |
100 ]Register Constant["Boolean_Yes", Yes] | |
101 ]Register Constant["Boolean_No", No] | |
102 ]Allocate Var["compres", [Type Instance["Int32"]]Set Variant["Naked"]] | |
103 | |
104 ,ina <- [func]Read Field["a", "Num"] | |
105 { ,inb <- [~]Read Field["b", "Num"] | |
106 { after op <- [backend func]Call[~, ina, inb, "compres"] }} | |
107 | |
108 [after op]Do If["compres", [[after op]Instruction Stream]Move[Constant["Boolean_Yes"], "out"]] | |
109 { after out <- [~]Do If[NotCond["compres"], [[after op]Instruction Stream]Move[Constant["Boolean_No"], "out"]] } | |
110 | |
111 out <- [backend]Store Function[ [[after out]Release["a"]]Release["b"] ] | |
112 | |
113 } | |
114 | |
115 Compile Conversion Method[backend,intype,outtype,prefix:out] | |
116 { | |
117 func <- [[[[[[backend]Create Function[[prefix]Append[[[outtype]Append["@"]]Append[intype]], ("in"), ("out"), "rhope"] | |
118 ]Set Input Type[Type Instance[intype], 0] | |
119 ]Set Output Type[Type Instance[outtype], 0] | |
120 ]Register Constant[["Blueprint_"]Append[outtype], Type Instance[outtype]] | |
121 ]Call["Build", [()]Append[ Constant[["Blueprint_"]Append[outtype]] ]] | |
122 ]Move[Result[0], "out"] | |
123 | |
124 ,src <- [func]Read Field["in", "Num"] | |
125 { ,dst <- [~]Write Field["out", "Num"] | |
126 { ffunc <- [[~]Move[src, dst]]Release["in"] }} | |
127 | |
128 out <- [backend]Store Function[ffunc] | |
129 } | |
130 | |
131 Compile Abs UInt Method[backend,type:out] | |
132 { | |
133 outtype <- ["U"]Append[type] | |
134 func <- [[[[[[[[[backend]Create Function[["Abs UInt@"]Append[type], ("in"), ("out"), "rhope"] | |
135 ]Set Input Type[Type Instance[type], 0] | |
136 ]Set Output Type[Type Instance[["U"]Append[type]], 0] | |
137 ]Register Constant[["Blueprint_"]Append[outtype], Type Instance[outtype]] | |
138 ]Allocate Var["abs", Type Instance[type]] | |
139 ]Call["Build", [()]Append[ Constant[["Blueprint_"]Append[outtype]] ]] | |
140 ]Move[Result[0], "out"] | |
141 ]Call["Abs", [()]Append["in"]] | |
142 ]Move[Result[0], "abs"] | |
143 { Print["After Call to Abs"] } | |
144 | |
145 ,src <- [func]Read Field["abs", "Num"] | |
146 { ,dst <- [~]Write Field["out", "Num"] | |
147 { ffunc <- [[~]Move[src, dst]]Release["abs"] }} | |
148 | |
149 out <- [backend]Store Function[ffunc] | |
150 } | |
151 | |
152 Supports Type?[backend,type:out] | |
153 { | |
154 [[backend]Supported Number Types]Find[=[type,?]] | |
155 { out <- Yes } | |
156 { out <- No } | |
157 } | |
158 | |
159 _Generate Number Methods[backend, type:out] | |
160 { | |
161 //Old crappy parser doesn't like Worker literals in List literals, work around for now | |
162 opmap <- [[[[[[[[() | |
163 ]Append[ [("+")]Append[Add[?]] ] | |
164 ]Append[ [("-")]Append[Sub[?]] ] | |
165 ]Append[ [("*")]Append[Multiply[?]] ] | |
166 ]Append[ [("/")]Append[Divide[?]] ] | |
167 ]Append[ [("LShift")]Append[DoLShift[?]] ] | |
168 ]Append[ [("RShift")]Append[DoRShift[?]] ] | |
169 ]Append[ [("&")]Append[BitAnd[?]] ] | |
170 ]Append[ [("|")]Append[BitOr[?]] ] | |
171 //(("+", Add[?]), ("-", Sub[?]), ("*", Multiply[?]), ("/", Divide[?]), ("LShift", DoLShift[?]), ("RShift", DoRShift[?])) | |
172 compops <- [[[[[[() | |
173 ]Append[ [("<")]Append[CompLess[?]] ] | |
174 ]Append[ [(">")]Append[CompGreater[?]] ] | |
175 ]Append[ [("=")]Append[CompEqual[?]] ] | |
176 ]Append[ [("<=")]Append[CompLessEqual[?]] ] | |
177 ]Append[ [(">=")]Append[CompGreaterEqual[?]] ] | |
178 ]Append[ [("!=")]Append[CompNotEqual[?]] ] | |
179 //(("<", CompLess[?]), (">", CompGreater[?]), ("=", CompEqual[?]), ("<=", CompLessEqual[?]), (">=", CompGreaterEqual[?]), ("!=", CompNotEqual[?])) | |
180 | |
181 Fold[Compile Number Method[?, ?, type], backend, opmap] | |
182 { Fold[Compile Number Comp Method[?, ?, type], ~, compops] | |
183 { Fold[Compile Conversion Method[?, type, ?, ""], ~, Filter[Legal Conversions[type], [backend]Supports Type?[?]]] | |
184 { | |
185 almost <- Fold[Compile Conversion Method[?, type, ?, "Trunc "], ~, Filter[Truncations[type], [backend]Supports Type?[?]]] | |
186 If[[type]Starts With["I"]] | |
187 { | |
188 out <- Compile Abs UInt Method[almost,type] | |
189 }{ | |
190 out <- Val[almost] | |
191 } | |
192 }}} | |
193 } | |
194 | |
195 Generate Number Methods[backend:out] | |
196 { | |
197 out <- Fold[_Generate Number Methods[?], backend, [backend]Supported Number Types] | |
198 } | |
199 | |
200 Register Number Method[program, method, type, outtype:out] | |
201 { | |
202 name <- [[method]Append["@"]]Append[type] | |
203 out <- [[program]Register Worker[name, "rhope", 2, 1] | |
204 ]Bind Worker[name, | |
205 [[[[[NWorker["rhope"] | |
206 ]Inputs <<[("left","right")] | |
207 ]Input Types <<[ [[()]Append[ Type Instance[type]]]Append[Type Instance[type]] ] | |
208 ]Outputs <<[("out")] | |
209 ]Output Types <<[ [()]Append[Type Instance[outtype]] ] | |
210 ]Builtin? <<[Yes] | |
211 ] | |
212 } | |
213 | |
214 Register Conversion Method[program, intype, outtype,prefix:out] | |
215 { | |
216 name <- [prefix]Append[[[outtype]Append["@"]]Append[intype]] | |
217 out <- [[program]Register Worker[name, "rhope", 1, 1] | |
218 ]Bind Worker[name, | |
219 [[[[[NWorker["rhope"] | |
220 ]Inputs <<[("in")] | |
221 ]Input Types <<[ [()]Append[Type Instance[intype]] ] | |
222 ]Outputs <<[("out")] | |
223 ]Output Types <<[ [()]Append[Type Instance[outtype]] ] | |
224 ]Builtin? <<[Yes] | |
225 ] | |
226 } | |
227 | |
228 Legal Conversions[type:convs] | |
229 { | |
230 bigger <- Map[Filter[(16,32,64), >[?, size]], String[?]] | |
231 base convs <- Map[bigger, Append["Int", ?]] | |
232 If[[type]Starts With["U"]] | |
233 { | |
234 [type]Slice[4] {} | |
235 { size <- Int32[~] } | |
236 convs <- Concatenate[base convs, Map[bigger, Append["UInt", ?]]] | |
237 }{ | |
238 [type]Slice[3] {} | |
239 { size <- Int32[~] } | |
240 convs <- Val[base convs] | |
241 } | |
242 } | |
243 | |
244 Truncations[type:truncs] | |
245 { | |
246 u <- [type]Partition["Int"] {} {} | |
247 { size <- Int32[~] } | |
248 truncs <- Map[Map[Filter[(8,16,32), <[?, size]], String[?]] Append[[u]Append["Int"], ?]] | |
249 } | |
250 | |
251 _Register Number Methods[program,type:out] | |
252 { | |
253 methods <- ("+", "-", "*", "/", "LShift", "RShift", "&", "|") | |
254 compmethods <- ("<", ">", "=", "<=", ">=", "!=") | |
255 register <- Val[Register Number Method[?, ?, type]] | |
256 Fold[[register]Set Input[3, type], program, methods] | |
257 { Fold[[register]Set Input[3, "Boolean"], ~, compmethods] | |
258 { Fold[Register Conversion Method[?, type, ?, ""], ~, Legal Conversions[type]] | |
259 { | |
260 almost <- Fold[Register Conversion Method[?, type, ?, "Trunc "], ~, Truncations[type]] | |
261 If[[type]Starts With["I"]] | |
262 { | |
263 name <- ["Abs UInt@"]Append[type] | |
264 out <- [[almost]Register Worker[name, "rhope", 1, 1] | |
265 ]Bind Worker[name | |
266 [[[[[NWorker["rhope"] | |
267 ]Inputs <<[("in")] | |
268 ]Input Types <<[ [()]Append[Type Instance[type]] ] | |
269 ]Outputs <<[("out")] | |
270 ]Output Types <<[ [()]Append[Type Instance[["U"]Append[type]]] ] | |
271 ]Builtin? <<[Yes] | |
272 ] | |
273 }{ | |
274 out <- Val[almost] | |
275 } | |
276 }}} | |
277 } | |
278 | |
279 Register Number Methods[program:out] | |
280 { | |
281 out <- Fold[_Register Number Methods[?], program, [program]Supported Number Types] | |
282 } | |
283 |