334
|
1 /*
|
|
2 lwexpr.c
|
|
3
|
|
4 Copyright © 2010 William Astle
|
|
5
|
|
6 This file is part of LWTOOLS.
|
|
7
|
|
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
|
|
9 terms of the GNU General Public License as published by the Free Software
|
|
10 Foundation, either version 3 of the License, or (at your option) any later
|
|
11 version.
|
|
12
|
|
13 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
16 more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License along with
|
|
19 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 #include <config.h>
|
|
23
|
|
24 #include <stdarg.h>
|
|
25 #include <stdio.h>
|
|
26 #include <string.h>
|
|
27
|
|
28 #define ___lw_expr_c_seen___
|
|
29 #include "lw_alloc.h"
|
|
30 #include "lw_expr.h"
|
|
31 #include "lw_error.h"
|
|
32 #include "lw_string.h"
|
|
33
|
|
34 lw_expr_t lw_expr_create(void)
|
|
35 {
|
|
36 lw_expr_t r;
|
|
37
|
|
38 r = lw_alloc(sizeof(struct lw_expr_priv));
|
|
39 r -> refcount = 1;
|
|
40 r -> operands = NULL;
|
|
41
|
|
42 return r;
|
|
43 }
|
|
44
|
|
45 /* useful for constant expression construction */
|
|
46 /* lw_expr_deref(lw_expr_create(...)) */
|
|
47 /* use of this function on an expression that is not already referenced by the caller */
|
|
48 lw_expr_t lw_expr_deref(lw_expr_t r)
|
|
49 {
|
|
50 r -> refcount--;
|
|
51 return r;
|
|
52 }
|
|
53
|
|
54 void lw_expr_destroy(lw_expr_t E)
|
|
55 {
|
|
56 E -> refcount--;
|
|
57 if (E -> refcount <= 0)
|
|
58 {
|
|
59 struct lw_expr_opers *o;
|
|
60 for (o = E -> operands; o; o = o -> next)
|
|
61 lw_expr_destroy(o -> p);
|
|
62 if (E -> type == lw_expr_type_var)
|
|
63 lw_free(E -> value2);
|
|
64 lw_free(E);
|
|
65 }
|
|
66 }
|
|
67
|
|
68 lw_expr_t lw_expr_copy(lw_expr_t E)
|
|
69 {
|
|
70 E -> refcount++;
|
|
71 return E;
|
|
72 }
|
|
73
|
|
74 void lw_expr_add_operand(lw_expr_t E, lw_expr_t O)
|
|
75 {
|
|
76 struct lw_expr_opers *o, *t;
|
|
77
|
|
78 o = lw_alloc(sizeof(struct lw_expr_opers));
|
|
79 o -> p = lw_expr_copy(O);
|
|
80 o -> next = NULL;
|
|
81 for (t = E -> operands; t && t -> next; t = t -> next)
|
|
82 /* do nothing */ ;
|
|
83
|
|
84 if (t)
|
|
85 t -> next = o;
|
|
86 else
|
|
87 E -> operands = o;
|
|
88 }
|
|
89
|
|
90 /* actually duplicates the entire expression */
|
|
91 lw_expr_t lw_expr_deepcopy(lw_expr_t E)
|
|
92 {
|
|
93 lw_expr_t r, t;
|
|
94 struct lw_expr_opers *o;
|
|
95
|
|
96 r = lw_alloc(sizeof(struct lw_expr_priv));
|
|
97 *r = *E;
|
|
98 r -> refcount = 1;
|
335
|
99 r -> operands = NULL;
|
|
100
|
334
|
101 if (E -> type == lw_expr_type_var)
|
|
102 r -> value2 = lw_strdup(E -> value2);
|
335
|
103 for (o = E -> operands; o; o = o -> next)
|
334
|
104 {
|
|
105 lw_expr_add_operand(r, lw_expr_deref(lw_expr_deepcopy(o -> p)));
|
|
106 }
|
|
107
|
|
108 return r;
|
|
109 }
|
|
110
|
335
|
111 lw_expr_t lw_expr_build_aux(int exprtype, va_list args)
|
334
|
112 {
|
|
113 lw_expr_t r;
|
|
114 int t;
|
|
115 void *p;
|
|
116
|
|
117 lw_expr_t te1, te2;
|
|
118
|
|
119 r = lw_expr_create();
|
|
120
|
|
121 switch (exprtype)
|
|
122 {
|
|
123 case lw_expr_type_int:
|
|
124 t = va_arg(args, int);
|
|
125 r -> type = lw_expr_type_int;
|
|
126 r -> value = t;
|
|
127 break;
|
|
128
|
|
129 case lw_expr_type_var:
|
|
130 p = va_arg(args, char *);
|
|
131 r -> type = lw_expr_type_var;
|
|
132 r -> value2 = lw_strdup(p);
|
|
133 break;
|
|
134
|
|
135 case lw_expr_type_special:
|
|
136 t = va_arg(args, int);
|
|
137 p = va_arg(args, char *);
|
|
138 r -> type = lw_expr_type_special;
|
|
139 r -> value2 = p;
|
|
140 break;
|
|
141
|
|
142 case lw_expr_type_oper:
|
|
143 t = va_arg(args, int);
|
|
144 te1 = va_arg(args, lw_expr_t);
|
|
145 if (t != lw_expr_oper_com && t != lw_expr_oper_neg)
|
|
146 te2 = va_arg(args, lw_expr_t);
|
|
147 else
|
|
148 te2 = NULL;
|
|
149
|
|
150 r -> type = lw_expr_type_oper;
|
|
151 r -> value = t;
|
|
152 lw_expr_add_operand(r, te1);
|
|
153 lw_expr_add_operand(r, te2);
|
|
154 break;
|
|
155
|
|
156 default:
|
|
157 lw_error("Invalid expression type specified to lw_expr_build");
|
|
158 }
|
|
159
|
335
|
160 return r;
|
|
161 }
|
|
162
|
|
163 lw_expr_t lw_expr_build(int exprtype, ...)
|
|
164 {
|
|
165 va_list args;
|
|
166 lw_expr_t r;
|
|
167
|
|
168 va_start(args, exprtype);
|
|
169 r = lw_expr_build_aux(exprtype, args);
|
|
170 va_end(args);
|
|
171 return r;
|
|
172 }
|
|
173
|
|
174 lw_expr_t lw_expr_build_noref(int exprtype, ...)
|
|
175 {
|
|
176 va_list args;
|
|
177 lw_expr_t r;
|
|
178
|
|
179 va_start(args, exprtype);
|
|
180 r = lw_expr_build_aux(exprtype, args);
|
|
181 r -> refcount--;
|
334
|
182 va_end(args);
|
|
183 return r;
|
|
184 }
|
|
185
|
|
186 void lw_expr_print(lw_expr_t E)
|
|
187 {
|
|
188 struct lw_expr_opers *o;
|
335
|
189 int c = 0;
|
|
190
|
334
|
191 for (o = E -> operands; o; o = o -> next)
|
|
192 {
|
335
|
193 c++;
|
334
|
194 lw_expr_print(o -> p);
|
|
195 }
|
|
196
|
|
197 switch (E -> type)
|
|
198 {
|
|
199 case lw_expr_type_int:
|
|
200 printf("%d ", E -> value);
|
|
201 break;
|
335
|
202
|
|
203 case lw_expr_type_var:
|
|
204 printf("V(%s) ", (char *)(E -> value2));
|
|
205 break;
|
|
206
|
|
207 case lw_expr_type_special:
|
|
208 printf("S(%d,%p) ", E -> value, E -> value2);
|
|
209 break;
|
|
210
|
334
|
211 case lw_expr_type_oper:
|
335
|
212 printf("[%d]", c);
|
334
|
213 switch (E -> value)
|
|
214 {
|
|
215 case lw_expr_oper_plus:
|
|
216 printf("+ ");
|
|
217 break;
|
|
218
|
|
219 case lw_expr_oper_minus:
|
|
220 printf("- ");
|
|
221 break;
|
|
222
|
|
223 case lw_expr_oper_times:
|
|
224 printf("* ");
|
|
225 break;
|
|
226
|
|
227 case lw_expr_oper_divide:
|
|
228 printf("/ ");
|
|
229 break;
|
|
230
|
|
231 case lw_expr_oper_mod:
|
|
232 printf("%% ");
|
|
233 break;
|
|
234
|
|
235 case lw_expr_oper_intdiv:
|
|
236 printf("\\ ");
|
|
237 break;
|
|
238
|
|
239 case lw_expr_oper_bwand:
|
|
240 printf("BWAND ");
|
|
241 break;
|
|
242
|
|
243 case lw_expr_oper_bwor:
|
|
244 printf("BWOR ");
|
|
245 break;
|
|
246
|
|
247 case lw_expr_oper_bwxor:
|
|
248 printf("BWXOR ");
|
|
249 break;
|
|
250
|
|
251 case lw_expr_oper_and:
|
|
252 printf("AND ");
|
|
253 break;
|
|
254
|
|
255 case lw_expr_oper_or:
|
|
256 printf("OR ");
|
|
257 break;
|
|
258
|
|
259 case lw_expr_oper_neg:
|
|
260 printf("NEG ");
|
|
261 break;
|
|
262
|
|
263 case lw_expr_oper_com:
|
|
264 printf("COM ");
|
|
265 break;
|
|
266
|
|
267 default:
|
|
268 printf("OPER ");
|
|
269 break;
|
|
270 }
|
|
271 break;
|
|
272 default:
|
|
273 printf("ERR ");
|
|
274 break;
|
|
275 }
|
|
276 }
|
|
277
|
|
278 /*
|
|
279 Return:
|
|
280 nonzero if expressions are the same (identical pointers or matching values)
|
|
281 zero if expressions are not the same
|
|
282
|
|
283 */
|
|
284 int lw_expr_compare(lw_expr_t E1, lw_expr_t E2)
|
|
285 {
|
|
286 struct lw_expr_opers *o1, *o2;
|
|
287
|
|
288 if (E1 == E2)
|
|
289 return 1;
|
|
290
|
|
291 if (!(E1 -> type == E2 -> type && E1 -> value == E2 -> value))
|
|
292 return 0;
|
|
293
|
|
294 if (E1 -> type == lw_expr_type_var)
|
|
295 {
|
|
296 if (!strcmp(E1 -> value2, E2 -> value2))
|
|
297 return 1;
|
|
298 else
|
|
299 return 0;
|
|
300 }
|
|
301
|
|
302 if (E1 -> type == lw_expr_type_special)
|
|
303 {
|
|
304 if (E1 -> value2 == E2 -> value2)
|
|
305 return 1;
|
|
306 else
|
|
307 return 0;
|
|
308 }
|
|
309
|
|
310 for (o1 = E1 -> operands, o2 = E2 -> operands; o1 && o2; o1 = o1 -> next, o2 = o2 -> next)
|
|
311 if (lw_expr_compare(o1 -> p, o2 -> p) == 0)
|
|
312 return 0;
|
|
313 if (o1 || o2)
|
|
314 return 0;
|
|
315
|
|
316 return 1;
|
|
317 }
|
|
318
|
|
319 /* return true if E is an operator of type oper */
|
|
320 int lw_expr_isoper(lw_expr_t E, int oper)
|
|
321 {
|
|
322 if (E -> type == lw_expr_type_oper && E -> value == oper)
|
|
323 return 1;
|
|
324 return 0;
|
|
325 }
|
335
|
326
|
|
327
|
|
328 void lw_expr_simplify_sortconstfirst(lw_expr_t E)
|
|
329 {
|
|
330 struct lw_expr_opers *o;
|
|
331
|
|
332 for (o = E -> operands; o; o = o -> next)
|
|
333 lw_expr_simplify_sortconstfirst(o -> p);
|
|
334
|
|
335 for (o = E -> operands; o; o = o -> next)
|
|
336 {
|
|
337 if (o -> p -> type == lw_expr_type_int && o != E -> operands)
|
|
338 {
|
|
339 struct lw_expr_opers *o2;
|
|
340 for (o2 = E -> operands; o2 -> next != o; o2 = o2 -> next)
|
|
341 /* do nothing */ ;
|
|
342 o2 -> next = o -> next;
|
|
343 o -> next = E -> operands;
|
|
344 E -> operands = o;
|
|
345 o = o2;
|
|
346 }
|
|
347 }
|
|
348 }
|
|
349
|
|
350 // return 1 if the operand lists match, 0 if not
|
|
351 // may re-order the argument lists
|
|
352 int lw_expr_simplify_compareoperandlist(struct lw_expr_opers **ol1, struct lw_expr_opers **ol2)
|
|
353 {
|
|
354 struct lw_expr_opers *o1, *o2;
|
|
355
|
|
356 lw_expr_sortoperandlist(ol1);
|
|
357 lw_expr_sortoperandlist(ol2);
|
|
358
|
|
359 for (o1 = *ol1, o2 = *ol2; o1 && o2; o1 = o1 -> next, o2 = o2 -> next)
|
|
360 {
|
|
361 if (!lw_expr_compare(o1 -> p, o2 -> p))
|
|
362 return 0;
|
|
363 }
|
|
364 if (o1 || o2)
|
|
365 return 0;
|
|
366 return 1;
|
|
367 }
|
|
368
|
|
369 void lw_expr_simplify(lw_expr_t E)
|
|
370 {
|
|
371 struct lw_expr_opers *o;
|
|
372
|
|
373 // sort "constants" to the start of each operand list for + and *
|
|
374 lw_expr_simplify_sortconstfirst(E);
|
|
375
|
|
376 // non-operators have no simplification to do!
|
|
377 if (E -> type != lw_expr_type_oper)
|
|
378 return;
|
|
379
|
|
380 if (E -> value == lw_expr_oper_times)
|
|
381 {
|
|
382 for (o = E -> operands; o; o = o -> next)
|
|
383 {
|
|
384 if (o -> p -> type == lw_expr_type_int && o -> p -> value == 0)
|
|
385 {
|
|
386 // one operand of times is 0, replace operation with 0
|
|
387 while (E -> operands)
|
|
388 {
|
|
389 o = E -> operands;
|
|
390 E -> operands = o -> next;
|
|
391 lw_expr_destroy(o -> p);
|
|
392 lw_free(o);
|
|
393 }
|
|
394 E -> type = lw_expr_type_int;
|
|
395 E -> value = 0;
|
|
396 return;
|
|
397 }
|
|
398 }
|
|
399 }
|
|
400
|
|
401 // look for like terms and collect them together
|
|
402 if (E -> value == lw_expr_oper_plus)
|
|
403 {
|
|
404 for (o = E -> operands; o; o = o -> next)
|
|
405 {
|
|
406 if (o -> p -> type == lw_expr_type_oper && o -> p -> value == lw_expr_oper_times)
|
|
407 {
|
|
408 // we have a "times" here
|
|
409 // find first non-const operand
|
|
410 struct lw_expr_opers *op1, *op2, *o2;
|
|
411 for (op1 = o -> p -> operands; op1; op1 = op1 -> next)
|
|
412 if (op1 -> p -> type != lw_expr_type_int)
|
|
413 break;
|
|
414
|
|
415 for (o2 = o -> next; o2; o2 = o2 -> next)
|
|
416 {
|
|
417 if (o2 -> p -> type == lw_expr_type_oper && o2 -> p -> value == lw_expr_oper_times)
|
|
418 {
|
|
419 // another "times"
|
|
420 for (op2 = o2 -> p -> operands; op2; op2 = op2 -> next)
|
|
421 if (op2 -> p -> type != lw_expr_type_int)
|
|
422 break;
|
|
423
|
|
424 if (lw_expr_compareoperandlist(&op1, &op2))
|
|
425 {
|
|
426 // we have like terms here
|
|
427 // do something about it
|
|
428 }
|
|
429 }
|
|
430 }
|
|
431 }
|
|
432 }
|
|
433 }
|
|
434
|
|
435
|
|
436 for (o = E -> operands; o; o = o -> next)
|
|
437 lw_expr_simplify(o -> p);
|
|
438
|
|
439 if (E -> value == lw_expr_oper_plus)
|
|
440 {
|
|
441 int c = 0, t = 0;
|
|
442 for (o = E -> operands; o; o = o -> next)
|
|
443 {
|
|
444 t++;
|
|
445 if (!(o -> p -> type == lw_expr_type_int && o -> p -> value == 0))
|
|
446 {
|
|
447 c++;
|
|
448 }
|
|
449 }
|
|
450 if (c == 1)
|
|
451 {
|
|
452 lw_expr_t r;
|
|
453 // find the value and "move it up"
|
|
454 while (E -> operands)
|
|
455 {
|
|
456 o = E -> operands;
|
|
457 if (o -> p -> type != lw_expr_type_int || o -> p -> value != 0)
|
|
458 {
|
|
459 r = lw_expr_deepcopy(o -> p);
|
|
460 }
|
|
461 E -> operands = o -> next;
|
|
462 lw_expr_destroy(o -> p);
|
|
463 lw_free(o);
|
|
464 }
|
|
465 *E = *r;
|
|
466 return;
|
|
467 }
|
|
468 else if (c == 0)
|
|
469 {
|
|
470 // replace with 0
|
|
471 while (E -> operands)
|
|
472 {
|
|
473 o = E -> operands;
|
|
474 E -> operands = o -> next;
|
|
475 lw_expr_destroy(o -> p);
|
|
476 lw_free(o);
|
|
477 }
|
|
478 E -> type = lw_expr_type_int;
|
|
479 E -> value = 0;
|
|
480 return;
|
|
481 }
|
|
482 else if (c != t)
|
|
483 {
|
|
484 // collapse out zero terms
|
|
485 struct lw_expr_opers *o2;
|
|
486
|
|
487 for (o = E -> operands; o; o = o -> next)
|
|
488 {
|
|
489 if (o -> p -> type == lw_expr_type_int && o -> p -> value == 0)
|
|
490 {
|
|
491 if (o == E -> operands)
|
|
492 {
|
|
493 E -> operands = o -> next;
|
|
494 lw_expr_destroy(o -> p);
|
|
495 lw_free(o);
|
|
496 o = E -> operands;
|
|
497 }
|
|
498 else
|
|
499 {
|
|
500 for (o2 = E -> operands; o2 -> next == o; o2 = o2 -> next)
|
|
501 /* do nothing */ ;
|
|
502 o2 -> next = o -> next;
|
|
503 lw_expr_destroy(o -> p);
|
|
504 lw_free(o);
|
|
505 o = o2;
|
|
506 }
|
|
507 }
|
|
508 }
|
|
509 }
|
|
510 return;
|
|
511 }
|
|
512 }
|