comparison cbackend.rhope @ 14:69dfca29565f

C backend close to being useable
author Mike Pavone <pavone@retrodev.com>
date Tue, 26 May 2009 23:52:32 +0000
parents 31f8182f3433
children ea991f95ae1f
comparison
equal deleted inserted replaced
12:31f8182f3433 14:69dfca29565f
10 } 10 }
11 11
12 Blueprint Def[name] 12 Blueprint Def[name]
13 { 13 {
14 out <- [[[[Build["Blueprint Def"]]Name <<[name]]Fixed Size <<[0]]Fields <<[()]]Methods <<[New@Dictionary[]] 14 out <- [[[[Build["Blueprint Def"]]Name <<[name]]Fixed Size <<[0]]Fields <<[()]]Methods <<[New@Dictionary[]]
15 }
16
17 Blueprint C Method Registry
18 {
19 Lookup
20 Next ID
21 }
22
23 C Method Registry[:out]
24 {
25 builtins <- [[[[[[[[[[[[[New@Dictionary[]
26 ]Set["+", "METHOD_ADD"]
27 ]Set["-", "METHOD_SUB"]
28 ]Set["/", "METHOD_DIV"]
29 ]Set["*", "METHOD_MUL"]
30 ]Set["LShift", "METHOD_LSHIFT"]
31 ]Set["RShift", "METHOD_RSHIFT"]
32 ]Set["=", "METHOD_EQUALS"]
33 ]Set[">", "METHOD_GREATER"]
34 ]Set["<", "METHOD_LESS"]
35 ]Set["If", "METHOD_IF"]
36 ]Set["Set Missing Field", "METHOD_SETFIELDMISSING"]
37 ]Set["Get Missing Field", "METHOD_GETFIELDMISSING"]
38 ]Set["Missing Method", "METHOD_MISSING"]
39 out <- [[Build["C Method Registry"]]Lookup <<[builtins]]Next ID<<[0]
40
41 }
42
43 Register Method@C Method Registry[reg,method:out]
44 {
45 [[reg]Lookup >>]Index[method]
46 {
47 out <- reg
48 }{
49 method ID <- [reg]Next ID>>
50 new lookup <- [[reg]Lookup >>]Set[method, ["METHOD_FIRST_USER+"]Append[method ID]]
51 out <- [[reg]Lookup <<[new lookup]]Next ID <<[[method ID]+[1]]
52 }
53 }
54
55 Method ID@C Method Registry[reg,method:out]
56 {
57 out <- [[reg]Lookup >>]Index[method]
15 } 58 }
16 59
17 Blueprint C Function 60 Blueprint C Function
18 { 61 {
19 Name 62 Name
20 Inputs 63 Inputs
21 Outputs 64 Outputs
22 Convention 65 Convention
23 Variables 66 Variables
24 Statements 67 Statements
68 Method Registry
25 } 69 }
26 70
27 C Function[name,inputs,outputs,convention:out] 71 C Function[name,inputs,outputs,convention:out]
28 { 72 {
29 out <- [[[[[[Build["C Function"] 73 out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[]]
74 }
75
76 C Function With Registry[name,inputs,outputs,convention,registry:out]
77 {
78 out <- [[[[[[[Build["C Function"]
30 ]Name <<[name] 79 ]Name <<[name]
31 ]Inputs <<[inputs] 80 ]Inputs <<[inputs]
32 ]Outputs <<[outputs] 81 ]Outputs <<[outputs]
33 ]Convention <<[convention] 82 ]Convention <<[convention]
34 ]Variables <<[New@Dictionary[]] 83 ]Variables <<[New@Dictionary[]]
35 ]Statements <<[()] 84 ]Statements <<[()]
85 ]Method Registry <<[registry]
36 } 86 }
37 87
38 Allocate Var@C Function[func,name,type:out] 88 Allocate Var@C Function[func,name,type:out]
39 { 89 {
40 out <- [func]Variables <<[ [[func]Variables >>]Set[name,type] ] 90 out <- [func]Variables <<[ [[func]Variables >>]Set[name,type] ]
43 Add Statement@C Function[func,statement:out] 93 Add Statement@C Function[func,statement:out]
44 { 94 {
45 out <- [func]Statements <<[ [[func]Statements >>]Append[["\t"]Append[[statement]Append[";\n"]]] ] 95 out <- [func]Statements <<[ [[func]Statements >>]Append[["\t"]Append[[statement]Append[";\n"]]] ]
46 } 96 }
47 97
48 Add Operator Statement@C Function[func,source1,source2,dest,op:out] 98 Add Raw Line@C Function[func,line:out]
49 { 99 {
100 out <- [func]Statements <<[ [[func]Statements >>]Append[["\t"]Append[[line]Append["\n"]]] ]
101 }
102
103 Add Operator Statement@C Function[func,psource1,psource2,pdest,op:out]
104 {
105 source1 <- [psource1]Make Op[func]
106 source2 <- [psource2]Make Op[func]
107 dest <- [pdest]Make Op[func]
50 out <- [func]Add Statement[[[[[dest]Append[" = "]]Append[source1]]Append[op]]Append[source2]] 108 out <- [func]Add Statement[[[[[dest]Append[" = "]]Append[source1]]Append[op]]Append[source2]]
51 } 109 }
52 110
53 Add@C Function[func,source1,source2,dest:out] 111 Add@C Function[func,source1,source2,dest:out]
54 { 112 {
68 Divide@C Function[func,source1,source2,dest:out] 126 Divide@C Function[func,source1,source2,dest:out]
69 { 127 {
70 out <- [func]Add Operator Statement[source1,source2,dest," / "] 128 out <- [func]Add Operator Statement[source1,source2,dest," / "]
71 } 129 }
72 130
73 Move@C Function[func,source,dest:out] 131 Move@C Function[func,psource,pdest:out]
74 { 132 {
133 source <- [psource]Make Op[func]
134 dest <- [pdest]Make Op[func]
75 out <- [func]Add Statement[[[dest]Append[" = "]]Append[source]] 135 out <- [func]Add Statement[[[dest]Append[" = "]]Append[source]]
76 } 136 }
77 137
78 Init Outputs@C Function[func] 138 AddRef@C Function[func,psource,pdest:out]
139 {
140 source <- [psource]Make Op[func]
141 dest <- [pdest]Make Op[func]
142 out <- [func]Add Statement[[[[dest]Append[" = add_ref("]]Append[source]]Append[")"]]
143 }
144
145 Release@C Function[func,psource:out]
146 {
147 source <- [psource]Make Op[func]
148 out <- [func]Add Statement[[["release_ref("]Append[source]]Append[")"]]
149 }
150
151 Null@C Function[func,pdest:out]
152 {
153 dest <- [pdest]Make Op[func]
154 out <- [func]Add Statement[[dest]Append[" = NULL;"]]
155 }
156
157 _Method Arg[func,val,inputnum:out]
158 {
159 out <- [func]Add Statement[
160 [[["call->params["
161 ]Append[inputnum]
162 ]Append["] = "]
163 ]Append[val]
164 ]
165 }
166
167 Method Call@C Function[func,method,args:out]
168 {
169 rargs <- Map[args, ["Make Op"]Set Input[1, func]]
170 out <- [Fold["_Method Arg", func, rargs]
171 ]Add Raw Line[[[[["MCall("]Append[ [[func]Method Registry >>]Method ID[method] ]]Append[", "]]Append[ [rargs]Length ]]Append[")"]]
172 }
173
174 Resolve@C Function[func,op:out]
175 {
176 [[func]Inputs >>]Find[op]
177 {
178 out <- [["cdata->params["]Append[~]]Append["]"]
179 }{
180 out <- ["locals->"]Append[Escape Rhope Name[op]]
181 }
182 }
183
184 Instruction Stream@C Function[func:out]
185 {
186 out <- [func]Statements <<[()]
187 }
188
189 _If C[func, statement:out]
190 {
191 out <- [func]Statements <<[ [[func]Statements >>]Append[ ["\t"]Append[statement] ] ]
192 }
193
194 Do If@C Function[func,condition,stream:out]
195 {
196 cond <- [condition]Make Op[func]
197 out <- [Fold["_If C", [[func
198 ]Add Raw Line[ [["if("]Append[cond]]Append[")"] ]
199 ]Add Raw Line["{"], [stream]Statements >>]
200 ]Add Raw Line["}"]
201 { Print["Do if done"] }
202
203 }
204
205 Result Reference@C Function[func,output:out]
206 {
207 out <- [["call->params["]Append[output]]Append["]"]
208 }
209
210 Init Outputs@C Function[func:out]
79 { 211 {
80 If[[[[func]Outputs >>]Length ] > [0]] 212 If[[[[func]Outputs >>]Length ] > [0]]
81 { 213 {
82 out <- [["\tlocals->"]Append[ [[func]Outputs >>]Join[" = NULL;\n\tlocals->"] ]]Append[" = NULL;\n"] 214 out <- [["\tlocals->"]Append[ [[func]Outputs >>]Join[" = NULL;\n\tlocals->"] ]]Append[" = NULL;\n"]
83 }{ 215 }{
84 out <- "" 216 out <- ""
85 } 217 }
86 } 218 }
87 219
88 Release Inputs@C Function[func] 220 _Release Inputs[string,inputname,inputnum:out]
221 {
222 out <- [[[string
223 ]Append["\trelease_ref(cdata->params["]
224 ]Append[inputnum]
225 ]Append["]);\n"]
226 }
227
228 Release Inputs@C Function[func:out]
89 { 229 {
90 If[[[[func]Inputs >>]Length ] > [0]] 230 If[[[[func]Inputs >>]Length ] > [0]]
91 { 231 {
92 out <- [["\trelease_ref(->"]Append[ [[func]Outputs >>]Join[" = NULL;\n\tlocals->"] ]]Append[" = NULL;\n"] 232 out <- Fold["_Release Inputs", "", [func]Inputs >>]
93 }{ 233 }{
94 out <- "" 234 out <- ""
95 } 235 }
96 } 236 }
97 237
238 _Set Outputs C[string,inputname,inputnum:out]
239 {
240 out <- [string]Append[[[ [ ["\tRet("]Append[inputnum] ]Append[", locals->"]]Append[inputname]]Append[")\n"]]
241 }
242
243 Set Outputs@C Function[func:out]
244 {
245 If[[[[func]Outputs >>]Length ] > [0]]
246 {
247 out <- Fold["_Set Outputs C", "", [func]Outputs >>]
248 }{
249 out <- ""
250 }
251 }
252 _Output Defs C[string,varname:out]
253 {
254 Print[varname]
255 out <- [[[string]Append["\tobject *"]]Append[Escape Rhope Name[varname]]]Append[";\n"]
256 }
257 _Var Defs C[string,type,varname:out]
258 {
259 Print[type]
260 {Print[varname]}
261 out <- [[[string]Append["\tobject *"]]Append[Escape Rhope Name[varname]]]Append[";\n"]
262 }
263
98 Definitions@C Function[func:out] 264 Definitions@C Function[func:out]
99 { 265 {
100 266 Print["Definitions"]
267 out <- [[[Fold["_Definitions C", Fold["_Definitions","typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} l_"]]Append[Escape Rhope Name[[func]Name >>]]]Append[";\n"]
101 } 268 }
102 269
103 Text@C Function[func:out] 270 Text@C Function[func:out]
104 { 271 {
105 cname <- Escape Rhope Name[[func]Name >>] 272 cname <- Escape Rhope Name[[func]Name >>]
106 out <- [[[[[[["Func(" 273 out <- [[[[[[[[["Func("
107 ]Append[cname] 274 ]Append[cname]
108 ]Append[",\n\tNumParams "] 275 ]Append[",\n\tNumParams "]
109 ]Append[ [[func]Inputs >>]Length ] 276 ]Append[ [[func]Inputs >>]Length ]
110 ]Append[",\n\tCallSpace 32,\n\t"]//TODO: Fill in with calculated callspace value 277 ]Append[",\n\tCallSpace 32,\n\t"]//TODO: Fill in with calculated callspace value
111 ]Append[["l_"]Append[cname]] 278 ]Append[["l_"]Append[cname]]
112 ]Append[")\n\n"] 279 ]Append[")\n\n"]
113 ]Append[[func]Init Outputs] 280 ]Append[ [[func]Statements >>]Join[""] ]
114 } 281 ]Append[[func]Set Outputs]
282 ]Append["EndFunc"]
283
284 }
285