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
149 esp->e_addr *= re.e_addr;
153 esp->e_addr /= re.e_addr;
157 esp->e_addr &= re.e_addr;
161 esp->e_addr |= re.e_addr;
165 esp->e_addr %= re.e_addr;
169 esp->e_addr ^= re.e_addr;
173 esp->e_addr <<= re.e_addr;
177 esp->e_addr >>= re.e_addr;
189 /*)Function Addr_T absexpr()
191 * The function absexpr() evaluates an expression, verifies it
192 * is absolute (i.e. not position dependent or relocatable), and
196 * expr e expr structure
202 * VOID abscheck() asexpr.c
203 * VOID clrexpr() asexpr.c
204 * VOID expr() asexpr.c
207 * If the expression is not absolute then
208 * a 'r' error is reported.
222 /*)Function VOID term(esp)
224 * expr * esp pointer to an expr structure
226 * The function term() evaluates a single constant
227 * or symbol value prefaced by any unary operator
228 * ( +, -, ~, ', ", >, or < ). This routine is also
229 * responsible for setting the relocation type to symbol
230 * based (e.flag != 0) on global references.
233 * int c current character
234 * char id[] symbol name
235 * char * jp pointer to assembler-source text
236 * int n constant evaluation running sum
237 * int r current evaluation radix
238 * sym * sp pointer to a sym structure
239 * tsym * tp pointer to a tsym structure
240 * int v current digit evaluation
243 * char ctype[] array of character types, one per
245 * sym * symp pointer to a symbol structure
248 * VOID abscheck() asexpr.c
249 * int digit() asexpr.c
250 * VOID err() assubr.c
251 * VOID expr() asexpr.c
252 * int is_abs() asexpr.c
254 * VOID getid() aslex.c
255 * int getmap() aslex.c
256 * int getnb() aslex.c
257 * sym * lookup() assym.c
258 * VOID qerr() assubr.c
259 * VOID unget() aslex.c
262 * An arithmetic term is evaluated, a symbol structure
263 * may be created, term evaluation may be terminated
269 register struct expr *esp;
280 * Discard the unary '+' at this point and
281 * also any reference to numerical arguments
282 * associated with the '#' prefix.
284 while (c == '+' || c == '#') { c = getnb(); }
286 * Evaluate all binary operators
287 * by recursively calling expr().
291 if (getnb() != RTTERM)
298 esp->e_addr = 0 - esp->e_addr;
304 esp->e_addr = ~esp->e_addr;
308 esp->e_mode = S_USER;
309 esp->e_addr = getmap(-1)&0377;
313 esp->e_mode = S_USER;
315 esp->e_addr = (getmap(-1)&0377)<<8;
316 esp->e_addr |= (getmap(-1)&0377);
318 esp->e_addr = (getmap(-1)&0377);
319 esp->e_addr |= (getmap(-1)&0377)<<8;
323 if (c == '>' || c == '<') {
327 * evaluate msb/lsb directly
335 * let linker perform msb/lsb, lsb is default
337 esp->e_rlcf |= R_BYT2;
339 esp->e_rlcf |= R_MSB;
344 * Evaluate digit sequences as local symbols
345 * if followed by a '$' or as constants.
347 if (ctype[c] & DIGIT) {
348 esp->e_mode = S_USER;
350 while (ctype[(unsigned char)(*jp)] & RAD10) {
355 while ((v = digit(c, 10)) >= 0) {
361 if (n == tp->t_num) {
362 esp->e_base.e_ap = tp->t_area;
363 esp->e_addr = tp->t_addr;
404 while ((v = digit(c, r)) >= 0) {
413 * Evaluate '$' sequences as a temporary radix
414 * if followed by a '%', '&', '#', or '$'.
418 if (c == '%' || c == '&' || c == '#' || c == '$') {
437 while ((v = digit(c, r)) >= 0) {
442 esp->e_mode = S_USER;
450 * Evaluate symbols and labels
452 if (ctype[c] & LETTER) {
453 esp->e_mode = S_USER;
456 if (sp->s_type == S_NEW) {
457 if (sp->s_flag&S_GBL) {
459 esp->e_base.e_sp = sp;
464 esp->e_mode = sp->s_type;
465 esp->e_addr = sp->s_addr;
466 esp->e_base.e_ap = sp->s_area;
476 /*)Function int digit(c, r)
478 * int c digit character
479 * int r current radix
481 * The function digit() returns the value of c
482 * in the current radix r. If the c value is not
483 * a number of the current radix then a -1 is returned.
489 * char ctype[] array of character types, one per
504 if (ctype[c] & RAD16) {
505 if (c >= 'A' && c <= 'F')
506 return (c - 'A' + 10);
507 if (c >= 'a' && c <= 'f')
508 return (c - 'a' + 10);
513 if (ctype[c] & RAD10)
527 /*)Function VOID abscheck(esp)
529 * expr * esp pointer to an expr structure
531 * The function abscheck() tests the evaluation of an
532 * expression to verify it is absolute. If the evaluation
533 * is relocatable then an 'r' error is noted and the expression
536 * Note: The area type (i.e. ABS) is not checked because
537 * the linker can be told to explicitly relocate an
547 * VOID rerr() assubr.c
550 * The expression may be changed to absolute and the
556 register struct expr *esp;
558 if (esp->e_flag || esp->e_base.e_ap) {
560 esp->e_base.e_ap = NULL;
565 /*)Function int is_abs(esp)
567 * expr * esp pointer to an expr structure
569 * The function is_abs() tests the evaluation of an
570 * expression to verify it is absolute. If the evaluation
571 * is absolute then 1 is returned, else 0 is returned.
573 * Note: The area type (i.e. ABS) is not checked because
574 * the linker can be told to explicitly relocate an
592 register struct expr *esp;
594 if (esp->e_flag || esp->e_base.e_ap) {
600 /*)Function int oprio(c)
602 * int c operator character
604 * The function oprio() returns a relative priority
605 * for all valid unary and binary operators.
624 if (c == '*' || c == '/' || c == '%')
626 if (c == '+' || c == '-')
628 if (c == '<' || c == '>')
639 /*)Function VOID clrexpr(esp)
641 * expr * esp pointer to expression structure
643 * The function clrexpr() clears the expression structure.
655 * expression structure cleared.
660 register struct expr *esp;
665 esp->e_base.e_ap = NULL;