1 | # Copyright (c) 2001, Stanford University
|
---|
2 | # All rights reserved.
|
---|
3 | #
|
---|
4 | # See the file LICENSE.txt for information on redistributing this software
|
---|
5 |
|
---|
6 | import sys
|
---|
7 |
|
---|
8 | def main():
|
---|
9 | name = sys.argv[1]
|
---|
10 | Name = sys.argv[2]
|
---|
11 |
|
---|
12 | print """/* This code is AUTOGENERATED!!! */
|
---|
13 |
|
---|
14 | #include "state.h"
|
---|
15 | #include "state_internals.h\""""
|
---|
16 |
|
---|
17 | print """
|
---|
18 | void crState%(Name)sDiff(CR%(Name)sBits *b, CRbitvalue *bitID,
|
---|
19 | CRContext *fromCtx, CRContext *toCtx)
|
---|
20 | {
|
---|
21 | CR%(Name)sState *from = &(fromCtx->%(name)s);
|
---|
22 | CR%(Name)sState *to = &(toCtx->%(name)s);"""%vars()
|
---|
23 | gendiffcode("state_%s.txt"%(name.lower()), name, docopy=1, doinvalid=0)
|
---|
24 | print """}
|
---|
25 |
|
---|
26 | void crState%(Name)sSwitch(CR%(Name)sBits *b, CRbitvalue *bitID,
|
---|
27 | CRContext *fromCtx, CRContext *toCtx)
|
---|
28 | {
|
---|
29 | CR%(Name)sState *from = &(fromCtx->%(name)s);
|
---|
30 | CR%(Name)sState *to = &(toCtx->%(name)s);"""%vars()
|
---|
31 | gendiffcode("state_%s.txt"%(Name.lower()), Name, docopy=0, doinvalid=1)
|
---|
32 | print "}\n"
|
---|
33 |
|
---|
34 | def gendiffcode(fname, state_name, docopy, doinvalid):
|
---|
35 | target = "to"
|
---|
36 | current = "from"
|
---|
37 | bit = "b"
|
---|
38 | extrabit = ""
|
---|
39 | tab = "\t"
|
---|
40 | current_guard = ""
|
---|
41 | current_dependency = ""
|
---|
42 |
|
---|
43 | v_types = {
|
---|
44 | 'l': 'GLboolean',
|
---|
45 | 'b': 'GLbyte',
|
---|
46 | 'ub': 'GLubyte',
|
---|
47 | 's': 'GLshort',
|
---|
48 | 'us': 'GLushort',
|
---|
49 | 'i': 'GLint',
|
---|
50 | 'ui': 'GLuint',
|
---|
51 | 'f': 'GLfloat',
|
---|
52 | 'd': 'GLdouble'
|
---|
53 | }
|
---|
54 |
|
---|
55 | FILE = open(sys.argv[3]+"/"+fname, "r")
|
---|
56 |
|
---|
57 | print """ unsigned int j, i;
|
---|
58 | CRbitvalue nbitID[CR_MAX_BITARRAY];
|
---|
59 | for (j=0;j<CR_MAX_BITARRAY;j++)
|
---|
60 | nbitID[j] = ~bitID[j];
|
---|
61 | i = 0; /* silence compiler */"""
|
---|
62 |
|
---|
63 | import re
|
---|
64 | for line in FILE.xreadlines():
|
---|
65 | line = line.rstrip()
|
---|
66 |
|
---|
67 | if re.match("#", line):
|
---|
68 | continue
|
---|
69 |
|
---|
70 | ## Handle text dump
|
---|
71 | m = re.match("\+(.*)", line)
|
---|
72 | if m:
|
---|
73 | if doinvalid:
|
---|
74 | continue
|
---|
75 | line = m.group(1)
|
---|
76 |
|
---|
77 | else:
|
---|
78 | m = re.match("-(.*)", line)
|
---|
79 | if m:
|
---|
80 | if docopy:
|
---|
81 | continue
|
---|
82 | line = m.group(1)
|
---|
83 |
|
---|
84 | m = re.match(">(.*)", line)
|
---|
85 | if m:
|
---|
86 | text = m.group(1)
|
---|
87 | if re.search("}", line):
|
---|
88 | tab = tab[:-1]
|
---|
89 | print tab+text
|
---|
90 | if re.search("{", line):
|
---|
91 | tab = tab+"\t"
|
---|
92 | continue
|
---|
93 |
|
---|
94 | ## Handle commands
|
---|
95 |
|
---|
96 | m = re.search("%target=(\w*)", line)
|
---|
97 | if m:
|
---|
98 | target = m.group(1)
|
---|
99 | m = re.search("%current=(\w*)", line)
|
---|
100 | if m:
|
---|
101 | current = m.group(1)
|
---|
102 | m = re.search("%bit=(\w*)", line)
|
---|
103 | if m:
|
---|
104 | bit = m.group(1)
|
---|
105 | m = re.search("%extrabit=(\w*)", line)
|
---|
106 | if m:
|
---|
107 | extrabit = m.group(1)
|
---|
108 |
|
---|
109 | if re.search("%flush", line):
|
---|
110 | if current_guard != "":
|
---|
111 | print tab+"CLEARDIRTY(%(bit)s->%(current_guard)s, nbitID);"%vars()
|
---|
112 | tab = tab[:-1]
|
---|
113 | print tab+"}"
|
---|
114 | if docopy and current_dependency != "":
|
---|
115 | tab = tab[:-1]
|
---|
116 | print tab+"}"
|
---|
117 | current_guard = ""
|
---|
118 | current_dependency = ""
|
---|
119 | if re.search("%", line):
|
---|
120 | continue
|
---|
121 |
|
---|
122 | ## Load the line
|
---|
123 | (dependency, guardbit, members, func) = \
|
---|
124 | (re.split(":", line) + ["", ""])[0:4]
|
---|
125 | func = func.rstrip()
|
---|
126 |
|
---|
127 | ## Close the guardbit and dependency
|
---|
128 | if current_guard != "" and current_guard != guardbit:
|
---|
129 | print tab+"CLEARDIRTY(%(bit)s->%(current_guard)s, nbitID);"%vars()
|
---|
130 | tab = tab[:-1]
|
---|
131 | print tab+"}"
|
---|
132 | if docopy and current_dependency != "" and current_dependency != dependency:
|
---|
133 | tab = tab[:-1]
|
---|
134 | print tab+"}"
|
---|
135 |
|
---|
136 | ## Open the dependency if
|
---|
137 | if docopy and current_dependency != dependency and dependency != "":
|
---|
138 | print tab+"if (%(target)s->%(dependency)s)\n%(tab)s{"%vars()
|
---|
139 | tab = tab+"\t"
|
---|
140 | current_dependency = dependency
|
---|
141 |
|
---|
142 | ## Open the guard if
|
---|
143 | if docopy and current_dependency != dependency and dependency != "":
|
---|
144 | print tab+"if ($(target)s->%(dependency)s)\n%(tab)s{"%vars()
|
---|
145 | tab = tab+"\t"
|
---|
146 |
|
---|
147 | if current_guard != guardbit and guardbit != "":
|
---|
148 | print tab+"if (CHECKDIRTY(%(bit)s->%(guardbit)s, bitID))\n%(tab)s{"%vars()
|
---|
149 | tab = tab+"\t"
|
---|
150 | if members[0] != "*" and guardbit[0:6] == "enable":
|
---|
151 | print tab+"glAble able[2];"
|
---|
152 | print tab+"able[0] = diff_api.Disable;"
|
---|
153 | print tab+"able[1] = diff_api.Enable;"
|
---|
154 |
|
---|
155 | current_dependency = dependency
|
---|
156 | current_guard = guardbit
|
---|
157 |
|
---|
158 | ## Handle text dump
|
---|
159 | if members[0] == "*":
|
---|
160 | print tab+members[1:]
|
---|
161 | else:
|
---|
162 | ## Parse the members variable
|
---|
163 | mainelem = re.split(",", members)
|
---|
164 | elems = re.split("\|", members)
|
---|
165 | if len(elems) > 1:
|
---|
166 | mainelem = [""]
|
---|
167 | mainelem[0] = elems[0]
|
---|
168 | elems = re.split(",", elems[1])
|
---|
169 | newelems = []
|
---|
170 | for elem in elems:
|
---|
171 | elem = mainelem[0] + "." + elem
|
---|
172 | newelems += [elem]
|
---|
173 | elems = newelems
|
---|
174 | else:
|
---|
175 | elems = re.split(",", members)
|
---|
176 |
|
---|
177 | ## Check member values
|
---|
178 | if guardbit != "extensions":
|
---|
179 | sys.stdout.write(tab+"if (")
|
---|
180 | first = 1
|
---|
181 | for elem in elems:
|
---|
182 | if first != 1:
|
---|
183 | print " ||\n"+tab+" ",
|
---|
184 | first = 0
|
---|
185 | sys.stdout.write("%(current)s->%(elem)s != %(target)s->%(elem)s"%vars())
|
---|
186 | print ")\n"+tab+"{"
|
---|
187 | tab = tab+"\t"
|
---|
188 |
|
---|
189 | ## Handle text function
|
---|
190 | if func[0] == "*":
|
---|
191 | func = func[1:]
|
---|
192 | print tab+func
|
---|
193 | else:
|
---|
194 | if func != "":
|
---|
195 | ## Call the glhw function
|
---|
196 | if guardbit[0:6] == "enable":
|
---|
197 | print tab+"able["+target+"->"+elems[0]+"]("+func+");"
|
---|
198 | elif guardbit == "extensions":
|
---|
199 | print tab+"crState$state_name",
|
---|
200 | if docopy == 1:
|
---|
201 | print "Diff",
|
---|
202 | else:
|
---|
203 | print "Switch",
|
---|
204 | print "Extensions( from, to );"
|
---|
205 | else:
|
---|
206 | funcargs = re.split(",", func)
|
---|
207 | #print "// funcargs:",funcargs
|
---|
208 | func = funcargs.pop(0)
|
---|
209 |
|
---|
210 | if func[-1] == "v":
|
---|
211 | v_type = func[-2:-1]
|
---|
212 | num_elems = len(elems)
|
---|
213 | print tab+v_types[v_type]+" varg["+str(num_elems)+"];"
|
---|
214 | i = 0
|
---|
215 | for elem in elems:
|
---|
216 | print tab+"varg["+str(i)+"] = "+target+"->"+elem+";"
|
---|
217 | i += 1
|
---|
218 | elif func[-3:] == "vNV":
|
---|
219 | v_type = func[-4:-3]
|
---|
220 | num_elems = len(elems)
|
---|
221 | print tab+v_types[v_type]+" varg["+str(num_elems)+"];"
|
---|
222 | i = 0
|
---|
223 | for elem in elems:
|
---|
224 | print tab+"varg["+str(i)+"] = "+target+"->"+elem+";"
|
---|
225 | i += 1
|
---|
226 |
|
---|
227 | sys.stdout.write(tab+"diff_api.%(func)s ("%vars())
|
---|
228 | for funcarg in funcargs:
|
---|
229 | sys.stdout.write(funcarg+", ")
|
---|
230 |
|
---|
231 | ## Handle vargs
|
---|
232 | if func[-1] == "v" or func[-3:] == "vNV":
|
---|
233 | sys.stdout.write("varg")
|
---|
234 | else:
|
---|
235 | first = 1
|
---|
236 | for elem in elems:
|
---|
237 | if first != 1:
|
---|
238 | sys.stdout.write(",\n"+tab+" ")
|
---|
239 | first = 0
|
---|
240 | sys.stdout.write(target+"->"+elem)
|
---|
241 | print ");"
|
---|
242 |
|
---|
243 | ## Do the sync if necessary
|
---|
244 | if docopy and guardbit != "extensions":
|
---|
245 | for elem in mainelem:
|
---|
246 | print tab+current+"->"+elem+" = "+target+"->"+elem+";"
|
---|
247 |
|
---|
248 | ## Do the clear if necessary
|
---|
249 | if doinvalid:
|
---|
250 | if guardbit != "":
|
---|
251 | print tab+"FILLDIRTY(%(bit)s->%(guardbit)s);"%vars()
|
---|
252 | print tab+"FILLDIRTY(%(bit)s->dirty);"%vars()
|
---|
253 | if extrabit != "":
|
---|
254 | print tab+"FILLDIRTY(%(extrabit)s->dirty);"%vars()
|
---|
255 |
|
---|
256 | ## Close the compare
|
---|
257 | if guardbit != "extensions":
|
---|
258 | tab = tab[:-1]
|
---|
259 | print tab+"}"
|
---|
260 |
|
---|
261 | ## Do final closures
|
---|
262 | if current_guard != "":
|
---|
263 | print tab+"CLEARDIRTY(%(bit)s->%(current_guard)s, nbitID);"%vars()
|
---|
264 | tab = tab[:-1]
|
---|
265 | print tab+"}"
|
---|
266 | if docopy and current_dependency != "":
|
---|
267 | tab = tab[:-1]
|
---|
268 | print tab+"} /*%(current_dependency)s*/"%vars()
|
---|
269 |
|
---|
270 | print tab+"CLEARDIRTY(%(bit)s->dirty, nbitID);"%vars()
|
---|
271 |
|
---|
272 | main()
|
---|