Mercurial > repos > rhope
comparison number_c.rhope @ 92:e73a93fb5de1
Beginning of port of compiler to itself, some bugfixes and a refcount optimization
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 02 Aug 2010 00:58:55 -0400 |
parents | |
children | 7361d70fbba6 |
comparison
equal
deleted
inserted
replaced
91:bcdc326b3d6e | 92:e73a93fb5de1 |
---|---|
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 Comp Method[backend, op, type:out] | |
24 { | |
25 name <- [ [ [op]Index[0] ]Append["@"] ]Append[type] | |
26 backend func <- [op]Index[1] | |
27 type inst <- Type Instance[type] | |
28 func <- [[[[[[ [backend]Create Function[name,("a","b"), ("out"), "rhope"] | |
29 ]Set Input Type[type inst, 0] | |
30 ]Set Input Type[type inst, 1] | |
31 ]Set Output Type[Type Instance["Boolean"], 0] | |
32 ]Register Constant["Blueprint_Boolean", Type Instance["Boolean"]] | |
33 ]Call["Build", [()]Append[Constant["Blueprint_Boolean"]]] | |
34 ]Move[Result[0], "out"] | |
35 | |
36 ,ina <- [func]Read Field["a", "Num"] | |
37 { ,inb <- [~]Read Field["b", "Num"] | |
38 { ,outa <- [~]Write Field["out", "Val"] | |
39 { after op <- [backend func]Call[~, ina, inb, outa] }}} | |
40 | |
41 out <- [backend]Store Function[ [[after op]Release["a"]]Release["b"] ] | |
42 | |
43 } | |
44 | |
45 Compile Conversion Method[backend,intype,outtype,prefix:out] | |
46 { | |
47 func <- [[[[[[backend]Create Function[[prefix]Append[[[outtype]Append["@"]]Append[intype]], ("in"), ("out"), "rhope"] | |
48 ]Set Input Type[Type Instance[intype], 0] | |
49 ]Set Output Type[Type Instance[outtype], 0] | |
50 ]Register Constant[["Blueprint_"]Append[outtype], Type Instance[outtype]] | |
51 ]Call["Build", [()]Append[ Constant[["Blueprint_"]Append[outtype]] ]] | |
52 ]Move[Result[0], "out"] | |
53 | |
54 ,src <- [func]Read Field["in", "Num"] | |
55 { ,dst <- [~]Write Field["out", "Num"] | |
56 { ffunc <- [[~]Move[src, dst]]Release["in"] }} | |
57 | |
58 out <- [backend]Store Function[ffunc] | |
59 } | |
60 | |
61 Compile Abs UInt Method[backend,type:out] | |
62 { | |
63 outtype <- ["U"]Append[type] | |
64 func <- [[[[[[[[[backend]Create Function[["Abs UInt@"]Append[type], ("in"), ("out"), "rhope"] | |
65 ]Set Input Type[Type Instance[type], 0] | |
66 ]Set Output Type[Type Instance[["U"]Append[type]], 0] | |
67 ]Register Constant[["Blueprint_"]Append[outtype], Type Instance[outtype]] | |
68 ]Allocate Var["abs", Type Instance[type]] | |
69 ]Call["Build", [()]Append[ Constant[["Blueprint_"]Append[outtype]] ]] | |
70 ]Move[Result[0], "out"] | |
71 ]Call["Abs", [()]Append["in"]] | |
72 ]Move[Result[0], "abs"] | |
73 { Print["After Call to Abs"] } | |
74 | |
75 ,src <- [func]Read Field["abs", "Num"] | |
76 { ,dst <- [~]Write Field["out", "Num"] | |
77 { ffunc <- [[~]Move[src, dst]]Release["abs"] }} | |
78 | |
79 out <- [backend]Store Function[ffunc] | |
80 } | |
81 | |
82 _Generate Number Methods[backend, type:out] | |
83 { | |
84 //Old crappy parser doesn't like Worker literals in List literals, work around for now | |
85 opmap <- [[[[[[() | |
86 ]Append[ [("+")]Append[Add[?]] ] | |
87 ]Append[ [("-")]Append[Sub[?]] ] | |
88 ]Append[ [("*")]Append[Multiply[?]] ] | |
89 ]Append[ [("/")]Append[Divide[?]] ] | |
90 ]Append[ [("LShift")]Append[DoLShift[?]] ] | |
91 ]Append[ [("RShift")]Append[DoRShift[?]] ] | |
92 //(("+", Add[?]), ("-", Sub[?]), ("*", Multiply[?]), ("/", Divide[?]), ("LShift", DoLShift[?]), ("RShift", DoRShift[?])) | |
93 compops <- [[[[[[() | |
94 ]Append[ [("<")]Append[CompLess[?]] ] | |
95 ]Append[ [(">")]Append[CompGreater[?]] ] | |
96 ]Append[ [("=")]Append[CompEqual[?]] ] | |
97 ]Append[ [("<=")]Append[CompLessEqual[?]] ] | |
98 ]Append[ [(">=")]Append[CompGreaterEqual[?]] ] | |
99 ]Append[ [("!=")]Append[CompNotEqual[?]] ] | |
100 //(("<", CompLess[?]), (">", CompGreater[?]), ("=", CompEqual[?]), ("<=", CompLessEqual[?]), (">=", CompGreaterEqual[?]), ("!=", CompNotEqual[?])) | |
101 | |
102 Fold[Compile Number Method[?, ?, type], backend, opmap] | |
103 { Fold[Compile Number Comp Method[?, ?, type], ~, compops] | |
104 { Fold[Compile Conversion Method[?, type, ?, ""], ~, Legal Conversions[type]] | |
105 { | |
106 almost <- Fold[Compile Conversion Method[?, type, ?, "Trunc "], ~, Truncations[type]] | |
107 If[[type]Starts With["I"]] | |
108 { | |
109 out <- Compile Abs UInt Method[almost,type] | |
110 }{ | |
111 out <- Val[almost] | |
112 } | |
113 }}} | |
114 } | |
115 | |
116 Generate Number Methods[backend:out] | |
117 { | |
118 numtypes <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64") | |
119 | |
120 out <- Fold[_Generate Number Methods[?], backend, numtypes] | |
121 } | |
122 | |
123 Register Number Method[program, method, type, outtype:out] | |
124 { | |
125 name <- [[method]Append["@"]]Append[type] | |
126 out <- [[program]Register Worker[name, "rhope", 2, 1] | |
127 ]Bind Worker[name, | |
128 [[[[[NWorker["rhope"] | |
129 ]Inputs <<[("left","right")] | |
130 ]Input Types <<[ [[()]Append[ Type Instance[type]]]Append[Type Instance[type]] ] | |
131 ]Outputs <<[("out")] | |
132 ]Output Types <<[ [()]Append[Type Instance[outtype]] ] | |
133 ]Builtin? <<[Yes] | |
134 ] | |
135 } | |
136 | |
137 Register Conversion Method[program, intype, outtype,prefix:out] | |
138 { | |
139 name <- [prefix]Append[[[outtype]Append["@"]]Append[intype]] | |
140 out <- [[program]Register Worker[name, "rhope", 1, 1] | |
141 ]Bind Worker[name, | |
142 [[[[[NWorker["rhope"] | |
143 ]Inputs <<[("in")] | |
144 ]Input Types <<[ [()]Append[Type Instance[intype]] ] | |
145 ]Outputs <<[("out")] | |
146 ]Output Types <<[ [()]Append[Type Instance[outtype]] ] | |
147 ]Builtin? <<[Yes] | |
148 ] | |
149 } | |
150 | |
151 Legal Conversions[type:convs] | |
152 { | |
153 bigger <- Map[Filter[(16,32,64), >[?, size]], String[?]] | |
154 base convs <- Map[bigger, Append["Int", ?]] | |
155 If[[type]Starts With["U"]] | |
156 { | |
157 [type]Slice[4] {} | |
158 { size <- Int32[~] } | |
159 convs <- Concatenate[base convs, Map[bigger, Append["UInt", ?]]] | |
160 }{ | |
161 [type]Slice[3] {} | |
162 { size <- Int32[~] } | |
163 convs <- Val[base convs] | |
164 } | |
165 } | |
166 | |
167 Truncations[type:truncs] | |
168 { | |
169 u <- [type]Partition["Int"] {} {} | |
170 { size <- Int32[~] } | |
171 truncs <- Map[Map[Filter[(8,16,32), <[?, size]], String[?]] Append[[u]Append["Int"], ?]] | |
172 } | |
173 | |
174 _Register Number Methods[program,type:out] | |
175 { | |
176 methods <- ("+", "-", "*", "/", "LShift", "RShift") | |
177 compmethods <- ("<", ">", "=", "<=", ">=", "!=") | |
178 register <- Val[Register Number Method[?, ?, type]] | |
179 Fold[[register]Set Input[3, type], program, methods] | |
180 { Fold[[register]Set Input[3, "Boolean"], ~, compmethods] | |
181 { Fold[Register Conversion Method[?, type, ?, ""], ~, Legal Conversions[type]] | |
182 { | |
183 almost <- Fold[Register Conversion Method[?, type, ?, "Trunc "], ~, Truncations[type]] | |
184 If[[type]Starts With["I"]] | |
185 { | |
186 name <- ["Abs UInt@"]Append[type] | |
187 out <- [[almost]Register Worker[name, "rhope", 1, 1] | |
188 ]Bind Worker[name | |
189 [[[[[NWorker["rhope"] | |
190 ]Inputs <<[("in")] | |
191 ]Input Types <<[ [()]Append[Type Instance[type]] ] | |
192 ]Outputs <<[("out")] | |
193 ]Output Types <<[ [()]Append[Type Instance[["U"]Append[type]]] ] | |
194 ]Builtin? <<[Yes] | |
195 ] | |
196 }{ | |
197 out <- Val[almost] | |
198 } | |
199 }}} | |
200 } | |
201 | |
202 Register Number Methods[program:out] | |
203 { | |
204 numtypes <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64") | |
205 out <- Fold[_Register Number Methods[?], program, numtypes] | |
206 } | |
207 |