annotate lwlib/lw_expr.h @ 583:000381ee2d5c default tip

Guard against single operand multiplication when detecting like terms This *shouldn't* happen, but it apparently does in some pathological cases so guard against a single operand multiplication to prevent a crash.
author William Astle <lost@l-w.ca>
date Mon, 04 Nov 2024 23:48:23 -0700
parents 6153cb49403c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1 /*
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
2 lwexpr.h
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
3
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
4 Copyright © 2010 William Astle
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
5
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
6 This file is part of LWTOOLS.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
7
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
11 version.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
12
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
16 more details.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
17
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
20 */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
21
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
22 #ifndef ___lw_expr_h_seen___
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
23 #define ___lw_expr_h_seen___
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
24
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
25 #include <stdio.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
26
361
4130ffdeb5c8 Add contributed support for building with Microsoft's compiler
William Astle <lost@l-w.ca>
parents: 339
diff changeset
27 #ifdef _MSC_VER
4130ffdeb5c8 Add contributed support for building with Microsoft's compiler
William Astle <lost@l-w.ca>
parents: 339
diff changeset
28 #include "lw_win.h" // windows build
4130ffdeb5c8 Add contributed support for building with Microsoft's compiler
William Astle <lost@l-w.ca>
parents: 339
diff changeset
29 #endif
4130ffdeb5c8 Add contributed support for building with Microsoft's compiler
William Astle <lost@l-w.ca>
parents: 339
diff changeset
30
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
31 enum
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
32 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
33 lw_expr_type_oper, // operator term
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
34 lw_expr_type_int, // integer
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
35 lw_expr_type_var, // a "variable" (string for the name)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
36 lw_expr_type_special // a "special" reference (user defined)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
37 };
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
38
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
39 enum
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
40 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
41 lw_expr_oper_plus = 1, // addition
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
42 lw_expr_oper_minus, // subtraction
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
43 lw_expr_oper_times, // multiplication
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
44 lw_expr_oper_divide, // division
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
45 lw_expr_oper_mod, // modulus
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
46 lw_expr_oper_intdiv, // integer division
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
47 lw_expr_oper_bwand, // bitwise and
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
48 lw_expr_oper_bwor, // bitwise or
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
49 lw_expr_oper_bwxor, // bitwise xor
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
50 lw_expr_oper_and, // boolean and
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
51 lw_expr_oper_or, // boolean or
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
52 lw_expr_oper_neg, // unary negation, 2's complement
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
53 lw_expr_oper_com, // unary 1's complement
366
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 361
diff changeset
54 lw_expr_oper_com8, // 8 bit complement
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
55 lw_expr_oper_none = 0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
56 };
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
57
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
58 typedef struct lw_expr_priv * lw_expr_t;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
59
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
60 struct lw_expr_opers
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
61 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
62 lw_expr_t p;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
63 struct lw_expr_opers *next;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
64 };
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
65
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
66 struct lw_expr_priv
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
67 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
68 int type; // type of term
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
69 int value; // integer value
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
70 void *value2; // misc pointer value
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
71 struct lw_expr_opers *operands; // ptr to list of operands (for operators)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
72 };
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
73
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
74 typedef lw_expr_t lw_expr_fn_t(int t, void *ptr, void *priv);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
75 typedef lw_expr_t lw_expr_fn2_t(char *var, void *priv);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
76 typedef lw_expr_t lw_expr_fn3_t(char **p, void *priv);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
77 typedef int lw_expr_testfn_t(lw_expr_t e, void *priv);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
78
374
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
79 lw_expr_t lwexpr_create(void);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
80 void lw_expr_destroy(lw_expr_t E);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
81 lw_expr_t lw_expr_copy(lw_expr_t E);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
82 void lw_expr_add_operand(lw_expr_t E, lw_expr_t O);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
83 lw_expr_t lw_expr_build(int exprtype, ...);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
84 char *lw_expr_print(lw_expr_t E);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
85 int lw_expr_compare(lw_expr_t E1, lw_expr_t E2);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
86 void lw_expr_simplify(lw_expr_t E, void *priv);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
87
374
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
88 void lw_expr_set_special_handler(lw_expr_fn_t *fn);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
89 void lw_expr_set_var_handler(lw_expr_fn2_t *fn);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
90 void lw_expr_set_term_parser(lw_expr_fn3_t *fn);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
91
374
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
92 lw_expr_t lw_expr_parse(char **p, void *priv);
399
6153cb49403c Initial commit of pragma newsource
William Astle <lost@l-w.ca>
parents: 392
diff changeset
93 lw_expr_t lw_expr_parse_compact(char **p, void *priv);
374
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
94 int lw_expr_istype(lw_expr_t e, int t);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
95 int lw_expr_intval(lw_expr_t e);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
96 int lw_expr_specint(lw_expr_t e);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
97 void *lw_expr_specptr(lw_expr_t e);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
98 int lw_expr_whichop(lw_expr_t e);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
99
374
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
100 int lw_expr_type(lw_expr_t e);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
101
374
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
102 int lw_expr_operandcount(lw_expr_t e);
12
6b9991fb39b6 Brought forward patch to fix bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 0
diff changeset
103
374
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
104 void lw_expr_setwidth(int w);
366
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 361
diff changeset
105
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
106 // run a function on all terms in an expression; if the function
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
107 // returns non-zero for any term, return non-zero, else return
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
108 // zero
374
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
109 int lw_expr_testterms(lw_expr_t e, lw_expr_testfn_t *fn, void *priv);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
110
374
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 366
diff changeset
111 void lw_expr_setdivzero(void (*fn)(void *priv));
249
1f1a28b797e1 Add trap for divide by zero in expression library
William Astle <lost@l-w.ca>
parents: 12
diff changeset
112
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
113 #endif /* ___lw_expr_h_seen___ */