Rizin
unix-like reverse engineering framework and cli tools
expressions.c File Reference

Go to the source code of this file.

Functions

static int do_rd_expr (const char **p, char delimiter, int *valid, int level, int *check, int print_errors)
 
static int rd_number (const char **p, const char **endp, int base)
 
static int rd_otherbasenumber (const char **p, int *valid, int print_errors)
 
static int rd_character (const char **p, int *valid, int print_errors)
 
static int check_label (struct label *labels, const char **p, struct label **ret, struct label **previous, int force_skip)
 
static int rd_label (const char **p, int *exists, struct label **previous, int level, int print_errors)
 
static int rd_value (const char **p, int *valid, int level, int *check, int print_errors)
 
static int rd_factor (const char **p, int *valid, int level, int *check, int print_errors)
 
static int rd_term (const char **p, int *valid, int level, int *check, int print_errors)
 
static int rd_expr_shift (const char **p, int *valid, int level, int *check, int print_errors)
 
static int rd_expr_unequal (const char **p, int *valid, int level, int *check, int print_errors)
 
static int rd_expr_equal (const char **p, int *valid, int level, int *check, int print_errors)
 
static int rd_expr_and (const char **p, int *valid, int level, int *check, int print_errors)
 
static int rd_expr_xor (const char **p, int *valid, int level, int *check, int print_errors)
 
static int rd_expr_or (const char **p, int *valid, int level, int *check, int print_errors)
 
static int rd_expr (const char **p, char delimiter, int *valid, int level, int print_errors)
 

Function Documentation

◆ check_label()

static int check_label ( struct label labels,
const char **  p,
struct label **  ret,
struct label **  previous,
int  force_skip 
)
static

Definition at line 189 of file expressions.c.

191 {
192  struct label *l;
193  const char *c;
194  unsigned s2;
195  *p = delspc (*p);
196  for (c = *p; isalnum ((const unsigned char)*c) || *c == '_' || *c == '.'; c++)
197  {
198  }
199  s2 = c - *p;
200  for (l = labels; l; l = l->next)
201  {
202  unsigned s1, s;
203  int cmp;
204  s1 = strlen (l->name);
205  s = s1 < s2 ? s1 : s2;
206  cmp = strncmp (l->name, *p, s);
207  if (cmp > 0 || (cmp == 0 && s1 > s))
208  {
209  if (force_skip)
210  *p = c;
211  return 0;
212  }
213  if (cmp < 0 || s2 > s)
214  {
215  if (previous)
216  *previous = l;
217  continue;
218  }
219  *p = c;
220  /* if label is not valid, compute it */
221  if (l->ref)
222  {
223  compute_ref (l->ref, 1);
224  if (!l->ref->done)
225  {
226  /* label was not valid, and isn't computable. tell the
227  * caller that it doesn't exist, so it will try again later.
228  * Set ret to show actual existence. */
229  if (verbose >= 6)
230  fprintf (stderr,
231  "%5d (0x%04x): returning invalid label %s.\n",
232  stack[sp].line, addr, l->name);
233  *ret = l;
234  return 0;
235  }
236  }
237  *ret = l;
238  return 1;
239  }
240  if (force_skip)
241  *p = c;
242  return 0;
243 }
static RzILOpEffect * cmp(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:942
void * p
Definition: libc.cpp:67
line
Definition: setup.py:34
static RzSocket * s
Definition: rtr.c:28
#define isalnum(c)
Definition: safe-ctype.h:127
#define c(i)
Definition: sha256.c:43
#define s1(x)
Definition: sha256.c:60
Definition: dis.h:35
struct reference * ref
Definition: z80asm.h:89
char name[1]
Definition: z80asm.h:90
struct label * next
Definition: z80asm.h:85
int done
Definition: z80asm.h:166
Definition: z80asm.h:140
static int sp
Definition: z80asm.c:91
static int compute_ref(struct reference *ref, int allow_invalid)
Definition: z80asm.c:255
static const char * delspc(const char *ptr)
Definition: z80asm.c:117
static int verbose
Definition: z80asm.c:73
static int addr
Definition: z80asm.c:58

References addr, c, cmp(), compute_ref(), delspc(), reference::done, isalnum, setup::line, label::name, label::next, p, label::ref, s, s1, s2, sp, and verbose.

Referenced by rd_label().

◆ do_rd_expr()

static int do_rd_expr ( const char **  p,
char  delimiter,
int valid,
int  level,
int check,
int  print_errors 
)
static

Definition at line 689 of file expressions.c.

691 {
692  /* read an expression. delimiter can _not_ be '?' */
693  int result = 0;
694  if (verbose >= 6)
695  fprintf (stderr,
696  "%5d (0x%04x): Starting to read expression "
697  "(string=%s, delimiter=%c).\n", stack[sp].line, addr, *p,
698  delimiter ? delimiter : ' ');
699  *p = delspc (*p);
700  if (!**p || **p == delimiter)
701  {
702  if (valid)
703  *valid = 0;
704  else if (print_errors)
705  printerr (1, "expression expected (not %s)\n", *p);
706  return 0;
707  }
708  result = rd_expr_or (p, valid, level, check, print_errors);
709  *p = delspc (*p);
710  if (**p == '?')
711  {
712  *check = 0;
713  (*p)++;
714  if (result)
715  {
716  result = do_rd_expr (p, ':', valid, level, check, print_errors);
717  if (**p)
718  (*p)++;
719  do_rd_expr (p, delimiter, valid, level, check, print_errors);
720  }
721  else
722  {
723  do_rd_expr (p, ':', valid, level, check, print_errors);
724  if (**p)
725  (*p)++;
726  result = do_rd_expr (p, delimiter, valid, level, check,
727  print_errors);
728  }
729  }
730  *p = delspc (*p);
731  if (**p && **p != delimiter)
732  {
733  if (valid)
734  *valid = 0;
735  else if (print_errors)
736  printerr (1, "junk at end of expression: %s\n", *p);
737  }
738  if (verbose >= 7)
739  {
740  fprintf (stderr, "%5d (0x%04x): rd_expr returned %d (%04x).\n",
741  stack[sp].line, addr, result, result);
742  if (valid && !*valid)
743  fprintf (stderr, "%5d (0x%04x): Returning invalid result.\n",
744  stack[sp].line, addr);
745  }
746  return result;
747 }
lzma_check check
Definition: container.h:292
static int do_rd_expr(const char **p, char delimiter, int *valid, int level, int *check, int print_errors)
Definition: expressions.c:689
static int rd_expr_or(const char **p, int *valid, int level, int *check, int print_errors)
Definition: expressions.c:664
bool valid
Definition: core.c:77
static int level
Definition: vmenus.c:2424
static void printerr(int error, const char *fmt,...)
Definition: z80asm.c:98

References addr, check, delspc(), level, setup::line, p, printerr(), rd_expr_or(), sp, valid, and verbose.

Referenced by rd_expr(), and rd_value().

◆ rd_character()

static int rd_character ( const char **  p,
int valid,
int  print_errors 
)
static

Definition at line 106 of file expressions.c.

107 {
108  int i;
109  if (verbose >= 6)
110  fprintf (stderr,
111  "%5d (0x%04x): Starting to read character (string=%s).\n",
112  stack[sp].line, addr, *p);
113  i = **p;
114  if (!i)
115  {
116  if (valid)
117  *valid = 0;
118  else if (print_errors)
119  printerr (1, "unexpected end of line in string constant\n");
120  return 0;
121  }
122  if (i == '\\')
123  {
124  (*p)++;
125  if (**p >= '0' && **p <= '7')
126  {
127  int b, num_digits;
128  i = 0;
129  if ((*p)[1] >= '0' && (*p)[1] <= '7')
130  {
131  if (**p <= '3' && (*p)[2] >= '0' && (*p)[2] <= '7')
132  num_digits = 3;
133  else
134  num_digits = 2;
135  }
136  else
137  num_digits = 1;
138  for (b = 0; b < num_digits; b++)
139  {
140  int bit = (*p)[num_digits - 1 - b] - '0';
141  i += (1 << (b * 3)) * bit;
142  }
143  *p += num_digits;
144  }
145  else
146  {
147  switch (**p)
148  {
149  case 'n':
150  i = 10;
151  break;
152  case 'r':
153  i = 13;
154  break;
155  case 't':
156  i = 9;
157  break;
158  case 'a':
159  i = 7;
160  break;
161  case '\'':
162  if (valid)
163  *valid = 0;
164  else if (print_errors)
165  printerr (1, "empty literal character\n");
166  return 0;
167  case 0:
168  if (valid)
169  *valid = 0;
170  else if (print_errors)
171  printerr (1, "unexpected end of line after "
172  "backslash in string constant\n");
173  return 0;
174  default:
175  i = **p;
176  }
177  (*p)++;
178  }
179  }
180  else
181  (*p)++;
182  if (verbose >= 7)
183  fprintf (stderr, "%5d (0x%04x): rd_character returned %d (%c).\n",
184  stack[sp].line, addr, i, i);
185  return i;
186 }
lzma_index ** i
Definition: index.h:629
RzCryptoSelector bit
Definition: crypto.c:16
#define b(i)
Definition: sha256.c:42

References addr, b, bit, i, setup::line, p, printerr(), sp, valid, and verbose.

Referenced by assemble(), and rd_value().

◆ rd_expr()

static int rd_expr ( const char **  p,
char  delimiter,
int valid,
int  level,
int  print_errors 
)
static

Definition at line 750 of file expressions.c.

752 {
753  int check = 1;
754  int result;
755  if (valid)
756  *valid = 1;
757  result = do_rd_expr (p, delimiter, valid, level, &check, print_errors);
758  if (print_errors && (!valid || *valid) && check)
759  printerr (0, "expression fully enclosed in parenthesis\n");
760  return result;
761 }

References check, do_rd_expr(), level, p, printerr(), and valid.

Referenced by assemble(), compute_ref(), indx(), and skipword().

◆ rd_expr_and()

static int rd_expr_and ( const char **  p,
int valid,
int  level,
int check,
int  print_errors 
)
static

Definition at line 617 of file expressions.c.

619 {
620  int result;
621  if (verbose >= 6)
622  fprintf (stderr, "%5d (0x%04x): Starting to read and expression "
623  "(string=%s).\n", stack[sp].line, addr, *p);
624  result = rd_expr_equal (p, valid, level, check, print_errors);
625  *p = delspc (*p);
626  if (**p == '&')
627  {
628  *check = 0;
629  (*p)++;
630  result &= rd_expr_and (p, valid, level, check, print_errors);
631  }
632  if (verbose >= 7)
633  fprintf (stderr, "%5d (0x%04x): rd_expr_and returned %d (%04x).\n",
634  stack[sp].line, addr, result, result);
635  return result;
636 }
static int rd_expr_and(const char **p, int *valid, int level, int *check, int print_errors)
Definition: expressions.c:617
static int rd_expr_equal(const char **p, int *valid, int level, int *check, int print_errors)
Definition: expressions.c:587

References addr, check, delspc(), level, setup::line, p, rd_expr_equal(), sp, valid, and verbose.

Referenced by rd_expr_xor().

◆ rd_expr_equal()

static int rd_expr_equal ( const char **  p,
int valid,
int  level,
int check,
int  print_errors 
)
static

Definition at line 587 of file expressions.c.

589 {
590  int result;
591  if (verbose >= 6)
592  fprintf (stderr, "%5d (0x%04x): Starting to read equality epression "
593  "(string=%s).\n", stack[sp].line, addr, *p);
594  result = rd_expr_unequal (p, valid, level, check, print_errors);
595  *p = delspc (*p);
596  if (**p == '=')
597  {
598  *check = 0;
599  ++*p;
600  if (**p == '=')
601  ++ * p;
602  return result == rd_expr_equal (p, valid, level, check, print_errors);
603  }
604  else if (**p == '!' && (*p)[1] == '=')
605  {
606  *check = 0;
607  (*p) += 2;
608  return result != rd_expr_equal (p, valid, level, check, print_errors);
609  }
610  if (verbose >= 7)
611  fprintf (stderr, "%5d (0x%04x): rd_equal returned %d (%04x).\n",
612  stack[sp].line, addr, result, result);
613  return result;
614 }
static int rd_expr_unequal(const char **p, int *valid, int level, int *check, int print_errors)
Definition: expressions.c:546

References addr, check, delspc(), level, setup::line, p, rd_expr_unequal(), sp, valid, and verbose.

Referenced by rd_expr_and().

◆ rd_expr_or()

static int rd_expr_or ( const char **  p,
int valid,
int  level,
int check,
int  print_errors 
)
static

Definition at line 664 of file expressions.c.

666 {
667  int result;
668  if (verbose >= 6)
669  fprintf (stderr, "%5d (0x%04x): Starting to read or expression "
670  "(string=%s).\n", stack[sp].line, addr, *p);
671  result = rd_expr_xor (p, valid, level, check, print_errors);
672  if (verbose >= 7)
673  fprintf (stderr, "%5d (0x%04x): rd_expr_or: rd_expr_xor returned %d "
674  "(%04x).\n", stack[sp].line, addr, result, result);
675  *p = delspc (*p);
676  if (**p == '|')
677  {
678  *check = 0;
679  (*p)++;
680  result |= rd_expr_or (p, valid, level, check, print_errors);
681  }
682  if (verbose >= 7)
683  fprintf (stderr, "%5d (0x%04x): rd_expr_or returned %d (%04x).\n",
684  stack[sp].line, addr, result, result);
685  return result;
686 }
static int rd_expr_xor(const char **p, int *valid, int level, int *check, int print_errors)
Definition: expressions.c:639

References addr, check, delspc(), level, setup::line, p, rd_expr_xor(), sp, valid, and verbose.

Referenced by do_rd_expr().

◆ rd_expr_shift()

static int rd_expr_shift ( const char **  p,
int valid,
int  level,
int check,
int  print_errors 
)
static

Definition at line 515 of file expressions.c.

517 {
518  int result;
519  if (verbose >= 6)
520  fprintf (stderr, "%5d (0x%04x): Starting to read shift expression "
521  "(string=%s).\n", stack[sp].line, addr, *p);
522  result = rd_term (p, valid, level, check, print_errors);
523  *p = delspc (*p);
524  while ((**p == '<' || **p == '>') && (*p)[1] == **p)
525  {
526  *check = 0;
527  if (**p == '<')
528  {
529  (*p) += 2;
530  result <<= rd_term (p, valid, level, check, print_errors);
531  }
532  else if (**p == '>')
533  {
534  (*p) += 2;
535  result >>= rd_term (p, valid, level, check, print_errors);
536  }
537  *p = delspc (*p);
538  }
539  if (verbose >= 7)
540  fprintf (stderr, "%5d (0x%04x): rd_shift returned %d (%04x).\n",
541  stack[sp].line, addr, result, result);
542  return result;
543 }
static int rd_term(const char **p, int *valid, int level, int *check, int print_errors)
Definition: expressions.c:484

References addr, check, delspc(), level, setup::line, p, rd_term(), sp, valid, and verbose.

Referenced by rd_expr_unequal().

◆ rd_expr_unequal()

static int rd_expr_unequal ( const char **  p,
int valid,
int  level,
int check,
int  print_errors 
)
static

Definition at line 546 of file expressions.c.

548 {
549  int result;
550  if (verbose >= 6)
551  fprintf (stderr, "%5d (0x%04x): Starting to read "
552  "unequality expression (string=%s).\n", stack[sp].line, addr,
553  *p);
554  result = rd_expr_shift (p, valid, level, check, print_errors);
555  *p = delspc (*p);
556  if (**p == '<' && (*p)[1] == '=')
557  {
558  *check = 0;
559  (*p) += 2;
560  return result <= rd_expr_unequal (p, valid, level, check, print_errors);
561  }
562  else if (**p == '>' && (*p)[1] == '=')
563  {
564  *check = 0;
565  (*p) += 2;
566  return result >= rd_expr_unequal (p, valid, level, check, print_errors);
567  }
568  if (**p == '<' && (*p)[1] != '<')
569  {
570  *check = 0;
571  (*p)++;
572  return result < rd_expr_unequal (p, valid, level, check, print_errors);
573  }
574  else if (**p == '>' && (*p)[1] != '>')
575  {
576  *check = 0;
577  (*p)++;
578  return result > rd_expr_unequal (p, valid, level, check, print_errors);
579  }
580  if (verbose >= 7)
581  fprintf (stderr, "%5d (0x%04x): rd_shift returned %d (%04x).\n",
582  stack[sp].line, addr, result, result);
583  return result;
584 }
static int rd_expr_shift(const char **p, int *valid, int level, int *check, int print_errors)
Definition: expressions.c:515

References addr, check, delspc(), level, setup::line, p, rd_expr_shift(), sp, valid, and verbose.

Referenced by rd_expr_equal().

◆ rd_expr_xor()

static int rd_expr_xor ( const char **  p,
int valid,
int  level,
int check,
int  print_errors 
)
static

Definition at line 639 of file expressions.c.

641 {
642  int result;
643  if (verbose >= 6)
644  fprintf (stderr, "%5d (0x%04x): Starting to read xor expression "
645  "(string=%s).\n", stack[sp].line, addr, *p);
646  result = rd_expr_and (p, valid, level, check, print_errors);
647  if (verbose >= 7)
648  fprintf (stderr, "%5d (0x%04x): rd_expr_xor: rd_expr_and returned %d "
649  "(%04x).\n", stack[sp].line, addr, result, result);
650  *p = delspc (*p);
651  if (**p == '^')
652  {
653  *check = 0;
654  (*p)++;
655  result ^= rd_expr_xor (p, valid, level, check, print_errors);
656  }
657  if (verbose >= 7)
658  fprintf (stderr, "%5d (0x%04x): rd_expr_xor returned %d (%04x).\n",
659  stack[sp].line, addr, result, result);
660  return result;
661 }

References addr, check, delspc(), level, setup::line, p, rd_expr_and(), sp, valid, and verbose.

Referenced by rd_expr_or().

◆ rd_factor()

static int rd_factor ( const char **  p,
int valid,
int  level,
int check,
int  print_errors 
)
static

Definition at line 448 of file expressions.c.

449 {
450  /* read a factor of an expression */
451  int result;
452  if (verbose >= 6)
453  fprintf (stderr, "%5d (0x%04x): Starting to read factor (string=%s).\n",
454  stack[sp].line, addr, *p);
455  result = rd_value (p, valid, level, check, print_errors);
456  *p = delspc (*p);
457  while (**p == '*' || **p == '/')
458  {
459  *check = 0;
460  if (**p == '*')
461  {
462  (*p)++;
463  result *= rd_value (p, valid, level, check, print_errors);
464  }
465  else if (**p == '/')
466  {
467  (*p)++;
468  int value = rd_value (p, valid, level, check, print_errors);
469  if (value == 0){
470  printerr (1, "division by zero\n");
471  return -1;
472  }
473  result /= value;
474  }
475  *p = delspc (*p);
476  }
477  if (verbose >= 7)
478  fprintf (stderr, "%5d (0x%04x): rd_factor returned %d (%04x).\n",
479  stack[sp].line, addr, result, result);
480  return result;
481 }
static int value
Definition: cmd_api.c:93
static int rd_value(const char **p, int *valid, int level, int *check, int print_errors)
Definition: expressions.c:285

References addr, check, delspc(), level, setup::line, p, printerr(), rd_value(), sp, valid, value, and verbose.

Referenced by rd_term().

◆ rd_label()

static int rd_label ( const char **  p,
int exists,
struct label **  previous,
int  level,
int  print_errors 
)
static

Definition at line 246 of file expressions.c.

248 {
249  struct label *l = NULL;
250  int s;
251  if (exists)
252  *exists = 0;
253  if (previous)
254  *previous = NULL;
255  if (verbose >= 6)
256  fprintf (stderr, "%5d (0x%04x): Starting to read label (string=%s).\n",
257  stack[sp].line, addr, *p);
258  for (s = level; s >= 0; --s)
259  {
260  if (check_label (stack[s].labels, p, &l,
261  (**p == '.' && s == sp) ? previous : NULL, 0))
262  break;
263  }
264  if (s < 0)
265  {
266  /* not yet found */
267  const char *old_p = *p;
268  /* label does not exist, or is invalid. This is an error if there
269  * is no existence check. */
270  if (!exists && print_errors)
271  printerr (1, "using undefined label %.*s\n", *p - old_p, old_p);
272  /* Return a value to discriminate between non-existing and invalid */
273  if (verbose >= 7)
274  fprintf (stderr, "rd_label returns invalid value\n");
275  return l != NULL;
276  }
277  if (exists)
278  *exists = 1;
279  if (verbose >= 7)
280  fprintf (stderr, "rd_label returns valid value 0x%x\n", l->value);
281  return l->value;
282 }
#define NULL
Definition: cris-opc.c:27
static int check_label(struct label *labels, const char **p, struct label **ret, struct label **previous, int force_skip)
Definition: expressions.c:189
int value
Definition: z80asm.h:86

References addr, check_label(), level, setup::line, NULL, p, printerr(), s, sp, label::value, and verbose.

Referenced by rd_value(), and readlabel().

◆ rd_number()

static int rd_number ( const char **  p,
const char **  endp,
int  base 
)
static

Definition at line 46 of file expressions.c.

47 {
48  int result = 0, i;
49  char *c, num[] = "0123456789abcdefghijklmnopqrstuvwxyz";
50  if (verbose >= 6)
51  fprintf (stderr, "%5d (0x%04x): Starting to read number of base %d"
52  "(string=%s).\n", stack[sp].line, addr, base, *p);
53  num[base] = '\0';
54  *p = delspc (*p);
55  while (**p && (c = strchr (num, tolower ((const unsigned char)**p))))
56  {
57  i = c - num;
58  if (verbose >= 7)
59  fprintf (stderr, "%5d (0x%04x): Digit found:%1x.\n", stack[sp].line,
60  addr, i);
61  result = result * base + i;
62  (*p)++;
63  }
64  if (endp)
65  *endp = *p;
66  *p = delspc (*p);
67  if (verbose >= 7)
68  fprintf (stderr, "%5d (0x%04x): rd_number returned %d (%04x).\n",
69  stack[sp].line, addr, result, result);
70  return result;
71 }
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
#define tolower(c)
Definition: safe-ctype.h:149

References addr, c, delspc(), i, setup::line, num, p, sp, tolower, and verbose.

Referenced by rd_otherbasenumber(), and rd_value().

◆ rd_otherbasenumber()

static int rd_otherbasenumber ( const char **  p,
int valid,
int  print_errors 
)
static

Definition at line 74 of file expressions.c.

75 {
76  char c;
77  if (verbose >= 6)
78  fprintf (stderr,
79  "%5d (0x%04x): Starting to read basenumber (string=%s).\n",
80  stack[sp].line, addr, *p);
81  (*p)++;
82  if (!**p)
83  {
84  if (valid)
85  *valid = 0;
86  else if (print_errors)
87  printerr (1, "unexpected end of line after `@'\n");
88  return 0;
89  }
90  if (**p == '0' || !isalnum ((const unsigned char)**p))
91  {
92  if (valid)
93  *valid = 0;
94  else if (print_errors)
95  printerr (1, "base must be between 1 and z\n");
96  return 0;
97  }
98  c = **p;
99  (*p)++;
100  if (isalpha ((const unsigned char)**p))
101  return rd_number (p, NULL, tolower ((unsigned char)c) - 'a' + 1);
102  return rd_number (p, NULL, c - '0' + 1);
103 }
static int rd_number(const char **p, const char **endp, int base)
Definition: expressions.c:46
#define isalpha(c)
Definition: safe-ctype.h:125

References addr, c, isalnum, isalpha, setup::line, NULL, p, printerr(), rd_number(), sp, tolower, valid, and verbose.

Referenced by rd_value().

◆ rd_term()

static int rd_term ( const char **  p,
int valid,
int  level,
int check,
int  print_errors 
)
static

Definition at line 484 of file expressions.c.

485 {
486  /* read a term of an expression */
487  int result;
488  if (verbose >= 6)
489  fprintf (stderr, "%5d (0x%04x): Starting to read term (string=%s).\n",
490  stack[sp].line, addr, *p);
491  result = rd_factor (p, valid, level, check, print_errors);
492  *p = delspc (*p);
493  while (**p == '+' || **p == '-')
494  {
495  *check = 0;
496  if (**p == '+')
497  {
498  (*p)++;
499  result += rd_factor (p, valid, level, check, print_errors);
500  }
501  else if (**p == '-')
502  {
503  (*p)++;
504  result -= rd_factor (p, valid, level, check, print_errors);
505  }
506  *p = delspc (*p);
507  }
508  if (verbose >= 7)
509  fprintf (stderr, "%5d (0x%04x): rd_term returned %d (%04x).\n",
510  stack[sp].line, addr, result, result);
511  return result;
512 }
static int rd_factor(const char **p, int *valid, int level, int *check, int print_errors)
Definition: expressions.c:448

References addr, check, delspc(), level, setup::line, p, rd_factor(), sp, valid, and verbose.

Referenced by rd_expr_shift().

◆ rd_value()

static int rd_value ( const char **  p,
int valid,
int  level,
int check,
int  print_errors 
)
static

Definition at line 285 of file expressions.c.

286 {
287  int sign = 1, not = 0, base, v;
288  const char *p0, *p1, *p2;
289  if (verbose >= 6)
290  fprintf (stderr, "%5d (0x%04x): Starting to read value (string=%s).\n",
291  stack[sp].line, addr, *p);
292  *p = delspc (*p);
293  while (**p && strchr ("+-~", **p))
294  {
295  if (**p == '-')
296  sign = -sign;
297  else if (**p == '~')
298  not = ~not;
299  (*p)++;
300  *p = delspc (*p);
301  }
302  base = 10; /* Default base for suffixless numbers */
303 
304  /* Check for parenthesis around full expression: not if no parenthesis */
305  if (**p != '(')
306  *check = 0;
307 
308  switch (**p)
309  {
310  int exist, retval;
311  char quote;
312  int dummy_check;
313  case '(':
314  (*p)++;
315  dummy_check = 0;
316  retval = not ^ (sign * do_rd_expr (p, ')', valid, level, &dummy_check,
317  print_errors));
318  ++*p;
319  return retval;
320  case '0':
321  if ((*p)[1] == 'x')
322  {
323  (*p) += 2;
324  return not ^ (sign * rd_number (p, NULL, 0x10));
325  }
326  base = 8; /* If first digit it 0, assume octal unless suffix */
327  /* fall through */
328  case '1':
329  case '2':
330  case '3':
331  case '4':
332  case '5':
333  case '6':
334  case '7':
335  case '8':
336  case '9':
337  p0 = *p;
338  rd_number (p, &p1, 36); /* Advance to end of numeric string */
339  p1--; /* Last character in numeric string */
340  switch (*p1)
341  {
342  case 'h':
343  case 'H':
344  base = 16;
345  break;
346  case 'b':
347  case 'B':
348  base = 2;
349  break;
350  case 'o':
351  case 'O':
352  case 'q':
353  case 'Q':
354  base = 8;
355  break;
356  case 'd':
357  case 'D':
358  base = 10;
359  break;
360  default: /* No suffix */
361  p1++;
362  break;
363  }
364  v = rd_number (&p0, &p2, base);
365  if (p1 != p2)
366  {
367  if (valid)
368  *valid = 0;
369  else if (print_errors)
370  printerr (1, "invalid character in number: \'%c\'\n", *p2);
371  }
372  return not ^ (sign * v);
373  case '$':
374  ++*p;
375  *p = delspc (*p);
376  p0 = *p;
377  v = rd_number (&p0, &p2, 0x10);
378  if (p2 == *p)
379  {
380  v = baseaddr;
381  }
382  else
383  *p = p2;
384  return not ^ (sign * v);
385  case '%':
386  (*p)++;
387  return not ^ (sign * rd_number (p, NULL, 2));
388  case '\'':
389  case '"':
390  quote = **p;
391  ++*p;
392  retval = not ^ (sign * rd_character (p, valid, print_errors));
393  if (**p != quote)
394  {
395  if (valid)
396  *valid = 0;
397  else if (print_errors)
398  printerr (1, "missing closing quote (%c)\n", quote);
399  return 0;
400  }
401  ++*p;
402  return retval;
403  case '@':
404  return not ^ (sign * rd_otherbasenumber (p, valid, print_errors));
405  case '?':
406  rd_label (p, &exist, NULL, level, 0);
407  return not ^ (sign * exist);
408  case '&':
409  {
410  ++*p;
411  switch (**p)
412  {
413  case 'h':
414  case 'H':
415  base = 0x10;
416  break;
417  case 'o':
418  case 'O':
419  base = 010;
420  break;
421  case 'b':
422  case 'B':
423  base = 2;
424  break;
425  default:
426  if (valid)
427  *valid = 0;
428  else if (print_errors)
429  printerr (1, "invalid literal starting with &%c\n", **p);
430  return 0;
431  }
432  ++*p;
433  return not ^ (sign * rd_number (p, NULL, base));
434  }
435  default:
436  {
437  int value;
438  exist = 1;
439  value = rd_label (p, valid ? &exist : NULL, NULL, level, print_errors);
440  if (!exist)
441  *valid = 0;
442  return not ^ (sign * value);
443  }
444  }
445 }
const char * v
Definition: dsignal.c:12
static int rd_label(const char **p, int *exists, struct label **previous, int level, int print_errors)
Definition: expressions.c:246
static int rd_otherbasenumber(const char **p, int *valid, int print_errors)
Definition: expressions.c:74
static int rd_character(const char **p, int *valid, int print_errors)
Definition: expressions.c:106
static int baseaddr
Definition: z80asm.c:79

References addr, baseaddr, check, delspc(), do_rd_expr(), level, setup::line, NULL, p, printerr(), rd_character(), rd_label(), rd_number(), rd_otherbasenumber(), sp, v, valid, value, and verbose.

Referenced by rd_factor().