4 * (C) Copyright 1989-1995
20 * The module asexpr.c contains the routines to evaluate
21 * arithmetic/numerical expressions. The functions in
22 * asexpr.c perform a recursive evaluation of the arithmetic
23 * expression read from the assembler-source text line.
24 * The expression may include binary/unary operators, brackets,
25 * symbols, labels, and constants in hexadecimal, decimal, octal
26 * and binary. Arithmetic operations are prioritized and
27 * evaluated by normal arithmetic conventions.
29 * asexpr.c contains the following functions:
38 * asexpr.c contains no local/static variables
41 /*)Function VOID expr(esp, n)
43 * expr * esp pointer to an expr structure
44 * int n a firewall priority; all top
45 * level calls (from the user)
46 * should be made with n set to 0.
48 * The function expr() evaluates an expression and
49 * stores its value and relocation information into
50 * the expr structure supplied by the user.
53 * int c current assembler-source
55 * int p current operator priority
56 * area * ap pointer to an area structure
57 * exp re internal expr structure
60 * char ctype[] array of character types, one per
64 * VOID abscheck() asexpr.c
65 * VOID clrexpr() asexpr.c
66 * VOID expr() asexpr.c
68 * int oprio() asexpr.c
69 * VOID qerr() assubr.c
70 * VOID rerr() assubr.c
71 * VOID term() asexpr.c
72 * VOID unget() aslex.c
76 * An expression is evaluated modifying the user supplied
77 * expr structure, a sym structure maybe created for an
78 * undefined symbol, and the parse of the expression may
79 * terminate if a 'q' error occurs.
84 register struct expr *esp;
92 while (ctype[c = getnb()] & BINOP) {
94 * Handle binary operators + - * / & | % ^ << >>
96 if ((p = oprio(c)) <= n)
98 if ((c == '>' || c == '<') && c != get())
102 esp->e_rlcf |= re.e_rlcf;
105 * esp + re, at least one must be absolute
107 if (esp->e_base.e_ap == NULL) {
109 * esp is absolute (constant),
112 esp->e_base.e_ap = re.e_base.e_ap;
114 if (re.e_base.e_ap) {
116 * re should be absolute (constant)
120 if (esp->e_flag && re.e_flag)
124 esp->e_addr += re.e_addr;
130 if ((ap = re.e_base.e_ap) != NULL) {
131 if (esp->e_base.e_ap == ap) {
132 esp->e_base.e_ap = NULL;
139 esp->e_addr -= re.e_addr;
142 * Both operands (esp and re) must be constants
144 /* SD :- moved the abscheck to each case
145 case and change the right shift operator.. if
146 right shift by 8 bits of a relocatable address then
147 the user wants the higher order byte. set the R_MSB
148 for the expression */
154 esp->e_addr *= re.e_addr;
160 esp->e_addr /= re.e_addr;
166 esp->e_addr &= re.e_addr;
172 esp->e_addr |= re.e_addr;
178 esp->e_addr %= re.e_addr;
184 esp->e_addr ^= re.e_addr;
190 esp->e_addr <<= re.e_addr;
196 /* if the left is a relative address &
197 the right side is == 8 then */
198 if (esp->e_base.e_ap && re.e_addr == 8) {
199 esp->e_rlcf |= R_MSB ;
202 /* else continue with the normal processing */
204 esp->e_addr >>= re.e_addr;
216 /*)Function addr_t absexpr()
218 * The function absexpr() evaluates an expression, verifies it
219 * is absolute (i.e. not position dependent or relocatable), and
223 * expr e expr structure
229 * VOID abscheck() asexpr.c
230 * VOID clrexpr() asexpr.c
231 * VOID expr() asexpr.c
234 * If the expression is not absolute then
235 * a 'r' error is reported.
249 /*)Function VOID term(esp)
251 * expr * esp pointer to an expr structure
253 * The function term() evaluates a single constant
254 * or symbol value prefaced by any unary operator
255 * ( +, -, ~, ', ", >, or < ). This routine is also
256 * responsible for setting the relocation type to symbol
257 * based (e.flag != 0) on global references.
260 * int c current character
261 * char id[] symbol name
262 * char * jp pointer to assembler-source text
263 * int n constant evaluation running sum
264 * int r current evaluation radix
265 * sym * sp pointer to a sym structure
266 * tsym * tp pointer to a tsym structure
267 * int v current digit evaluation
270 * char ctype[] array of character types, one per
272 * sym * symp pointer to a symbol structure
275 * VOID abscheck() asexpr.c
276 * int digit() asexpr.c
277 * VOID err() assubr.c
278 * VOID expr() asexpr.c
279 * int is_abs() asexpr.c
281 * VOID getid() aslex.c
282 * int getmap() aslex.c
283 * int getnb() aslex.c
284 * sym * lookup() assym.c
285 * VOID qerr() assubr.c
286 * VOID unget() aslex.c
289 * An arithmetic term is evaluated, a symbol structure
290 * may be created, term evaluation may be terminated
296 register struct expr *esp;
307 * Discard the unary '+' at this point and
308 * also any reference to numerical arguments
309 * associated with the '#' prefix.
311 while (c == '+' || c == '#') { c = getnb(); }
313 * Evaluate all binary operators
314 * by recursively calling expr().
318 if (getnb() != RTTERM)
325 esp->e_addr = -esp->e_addr;
331 esp->e_addr = ~esp->e_addr;
335 esp->e_mode = S_USER;
336 esp->e_addr = getmap(-1)&0377;
340 esp->e_mode = S_USER;
342 esp->e_addr = (getmap(-1)&0377)<<8;
343 esp->e_addr |= (getmap(-1)&0377);
345 esp->e_addr = (getmap(-1)&0377);
346 esp->e_addr |= (getmap(-1)&0377)<<8;
350 if (c == '>' || c == '<') {
354 * evaluate msb/lsb directly
362 * let linker perform msb/lsb, lsb is default
364 esp->e_rlcf |= R_BYT2;
366 esp->e_rlcf |= R_MSB;
371 * Evaluate digit sequences as local symbols
372 * if followed by a '$' or as constants.
374 if (ctype[c] & DIGIT) {
375 esp->e_mode = S_USER;
377 while (ctype[(int)*jp] & RAD10) {
382 while ((v = digit(c, 10)) >= 0) {
388 if (n == tp->t_num) {
389 esp->e_base.e_ap = tp->t_area;
390 esp->e_addr = tp->t_addr;
431 while ((v = digit(c, r)) >= 0) {
440 * Evaluate '$' sequences as a temporary radix
441 * if followed by a '%', '&', '#', or '$'.
445 if (c == '%' || c == '&' || c == '#' || c == '$') {
464 while ((v = digit(c, r)) >= 0) {
469 esp->e_mode = S_USER;
477 * Evaluate symbols and labels
479 if (ctype[c] & LETTER) {
480 esp->e_mode = S_USER;
483 if (sp->s_type == S_NEW) {
485 if (sp->s_flag&S_GBL) {
487 esp->e_base.e_sp = sp;
492 esp->e_mode = sp->s_type;
493 esp->e_addr = sp->s_addr;
494 esp->e_base.e_ap = sp->s_area;
504 /*)Function int digit(c, r)
506 * int c digit character
507 * int r current radix
509 * The function digit() returns the value of c
510 * in the current radix r. If the c value is not
511 * a number of the current radix then a -1 is returned.
517 * char ctype[] array of character types, one per
532 if (ctype[c] & RAD16) {
533 if (c >= 'A' && c <= 'F')
534 return (c - 'A' + 10);
535 if (c >= 'a' && c <= 'f')
536 return (c - 'a' + 10);
541 if (ctype[c] & RAD10)
555 /*)Function VOID abscheck(esp)
557 * expr * esp pointer to an expr structure
559 * The function abscheck() tests the evaluation of an
560 * expression to verify it is absolute. If the evaluation
561 * is relocatable then an 'r' error is noted and the expression
564 * Note: The area type (i.e. ABS) is not checked because
565 * the linker can be told to explicitly relocate an
575 * VOID rerr() assubr.c
578 * The expression may be changed to absolute and the
584 register struct expr *esp;
586 if (esp->e_flag || esp->e_base.e_ap) {
588 esp->e_base.e_ap = NULL;
593 /*)Function int is_abs(esp)
595 * expr * esp pointer to an expr structure
597 * The function is_abs() tests the evaluation of an
598 * expression to verify it is absolute. If the evaluation
599 * is absolute then 1 is returned, else 0 is returned.
601 * Note: The area type (i.e. ABS) is not checked because
602 * the linker can be told to explicitly relocate an
620 register struct expr *esp;
622 if (esp->e_flag || esp->e_base.e_ap) {
628 /*)Function int oprio(c)
630 * int c operator character
632 * The function oprio() returns a relative priority
633 * for all valid unary and binary operators.
652 if (c == '*' || c == '/' || c == '%')
654 if (c == '+' || c == '-')
656 if (c == '<' || c == '>')
667 /*)Function VOID clrexpr(esp)
669 * expr * esp pointer to expression structure
671 * The function clrexpr() clears the expression structure.
683 * expression structure cleared.
688 register struct expr *esp;
693 esp->e_base.e_ap = NULL;