annotate old-trunk/lwasm/old/expr.h @ 348:11a95c6414b4

Added third func to instab to split resolve and emit logic
author lost@starbug
date Sat, 27 Mar 2010 22:15:07 -0600
parents eb230fa7d28e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
1 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
2 expr.h
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
3 Copyright © 2008 William Astle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5 This file is part of LWASM.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
20
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
21 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22 Definitions for expression evaluator
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25 #ifndef __expr_h_seen__
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #define __expr_h_seen__
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
28 #ifndef __expr_c_seen__
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
29 #define __expr_E__ extern
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
30 #else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31 #define __expr_E__
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 #endif
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34 // term types
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 #define LWASM_TERM_NONE 0
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36 #define LWASM_TERM_OPER 1 // an operator
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 #define LWASM_TERM_INT 2 // 32 bit signed integer
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 #define LWASM_TERM_SYM 3 // symbol reference
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39 #define LWASM_TERM_SECBASE 4 // section base reference
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 // operator types
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 #define LWASM_OPER_NONE 0
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 #define LWASM_OPER_PLUS 1 // +
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 #define LWASM_OPER_MINUS 2 // -
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 #define LWASM_OPER_TIMES 3 // *
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 #define LWASM_OPER_DIVIDE 4 // /
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 #define LWASM_OPER_MOD 5 // %
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 #define LWASM_OPER_INTDIV 6 // \ (don't end line with \)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49 #define LWASM_OPER_BWAND 7 // bitwise AND
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 #define LWASM_OPER_BWOR 8 // bitwise OR
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 #define LWASM_OPER_BWXOR 9 // bitwise XOR
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52 #define LWASM_OPER_AND 10 // boolean AND
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
53 #define LWASM_OPER_OR 11 // boolean OR
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54 #define LWASM_OPER_NEG 12 // - unary negation (2's complement)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55 #define LWASM_OPER_COM 13 // ^ unary 1's complement
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 // term structure
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 typedef struct lwasm_expr_term_s
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
61 int term_type; // type of term (see above)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 char *symbol; // name of a symbol
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 int value; // value of the term (int) or operator number (OPER)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64 } lwasm_expr_term_t;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66 // type for an expression evaluation stack
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 typedef struct lwasm_expr_stack_node_s lwasm_expr_stack_node_t;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 struct lwasm_expr_stack_node_s
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70 lwasm_expr_term_t *term;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71 lwasm_expr_stack_node_t *prev;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
72 lwasm_expr_stack_node_t *next;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 };
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 typedef struct lwasm_expr_stack_s
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 lwasm_expr_stack_node_t *head;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78 lwasm_expr_stack_node_t *tail;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 } lwasm_expr_stack_t;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 __expr_E__ void lwasm_expr_term_free(lwasm_expr_term_t *t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82 __expr_E__ lwasm_expr_term_t *lwasm_expr_term_create_oper(int oper);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 __expr_E__ lwasm_expr_term_t *lwasm_expr_term_create_sym(char *sym);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 __expr_E__ lwasm_expr_term_t *lwasm_expr_term_create_int(int val);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 __expr_E__ lwasm_expr_term_t *lwasm_expr_term_create_secbase(void);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 __expr_E__ lwasm_expr_term_t *lwasm_expr_term_dup(lwasm_expr_term_t *t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 __expr_E__ void lwasm_expr_stack_free(lwasm_expr_stack_t *s);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 __expr_E__ lwasm_expr_stack_t *lwasm_expr_stack_create(void);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 __expr_E__ void lwasm_expr_stack_push(lwasm_expr_stack_t *s, lwasm_expr_term_t *t);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92 __expr_E__ lwasm_expr_term_t *lwasm_expr_stack_pop(lwasm_expr_stack_t *s);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 Evaluate an expression. The result is an lwasm_expr_stack_t pointer. If the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 expression evaluates to a constant result, the stack will contain exactly one
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 value which will be a constant. Otherwise, the stack will contain the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98 expression with all operations that can be evaluated completely evaluated.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 You must call lwasm_expr_stack_free() on the result when you are finished
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 with it.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 __expr_E__ lwasm_expr_stack_t *lwasm_expr_eval(const char *inp, const char **outp, lwasm_expr_stack_t *(*sfunc)(char *sym, void *state), void *state);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
104 // simplify expression
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105 __expr_E__ int lwasm_expr_reval(lwasm_expr_stack_t *s, lwasm_expr_stack_t *(*sfunc)(char *sym, void *state), void *state);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 // useful macros
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
108 // is the expression "simple" (one term)?
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 #define lwasm_expr_is_simple(s) ((s) -> head == (s) -> tail)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 // is the expression constant?
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112 #define lwasm_expr_is_constant(s) (lwasm_expr_is_simple(s) && (!((s) -> head) || (s) -> head -> term -> term_type == LWASM_TERM_INT))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114 // get the constant value of an expression or 0 if not constant
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115 #define lwasm_expr_get_value(s) (lwasm_expr_is_constant(s) ? ((s) -> head ? (s) -> head -> term -> value : 0) : 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 #undef __expr_E__
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
119 #endif // __expr_h_seen__