db_expr.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* $OpenBSD: db_expr.c,v 1.11 2014/09/14 14:17:24 jsg Exp $ */
  2. /* $NetBSD: db_expr.c,v 1.5 1996/02/05 01:56:58 christos Exp $ */
  3. /*
  4. * Mach Operating System
  5. * Copyright (c) 1993,1992,1991,1990 Carnegie Mellon University
  6. * All Rights Reserved.
  7. *
  8. * Permission to use, copy, modify and distribute this software and its
  9. * documentation is hereby granted, provided that both the copyright
  10. * notice and this permission notice appear in all copies of the
  11. * software, derivative works or modified versions, and any portions
  12. * thereof, and that both notices appear in supporting documentation.
  13. *
  14. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  15. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  16. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  17. *
  18. * Carnegie Mellon requests users of this software to return to
  19. *
  20. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  21. * School of Computer Science
  22. * Carnegie Mellon University
  23. * Pittsburgh PA 15213-3890
  24. *
  25. * any improvements or extensions that they make and grant Carnegie Mellon
  26. * the rights to redistribute these changes.
  27. *
  28. * Author: David B. Golub, Carnegie Mellon University
  29. * Date: 7/90
  30. */
  31. #include <sys/param.h>
  32. #include <machine/db_machdep.h>
  33. #include <ddb/db_lex.h>
  34. #include <ddb/db_access.h>
  35. #include <ddb/db_command.h>
  36. #include <ddb/db_sym.h>
  37. #include <ddb/db_extern.h>
  38. #include <ddb/db_variables.h>
  39. boolean_t
  40. db_term(db_expr_t *valuep)
  41. {
  42. int t;
  43. t = db_read_token();
  44. if (t == tIDENT) {
  45. if (!db_value_of_name(db_tok_string, valuep)) {
  46. db_error("Symbol not found\n");
  47. /*NOTREACHED*/
  48. }
  49. return (TRUE);
  50. }
  51. if (t == tNUMBER) {
  52. *valuep = db_tok_number;
  53. return (TRUE);
  54. }
  55. if (t == tDOT) {
  56. *valuep = (db_expr_t)db_dot;
  57. return (TRUE);
  58. }
  59. if (t == tDOTDOT) {
  60. *valuep = (db_expr_t)db_prev;
  61. return (TRUE);
  62. }
  63. if (t == tPLUS) {
  64. *valuep = (db_expr_t) db_next;
  65. return (TRUE);
  66. }
  67. if (t == tDITTO) {
  68. *valuep = (db_expr_t)db_last_addr;
  69. return (TRUE);
  70. }
  71. if (t == tDOLLAR) {
  72. if (!db_get_variable(valuep))
  73. return (FALSE);
  74. return (TRUE);
  75. }
  76. if (t == tLPAREN) {
  77. if (!db_expression(valuep)) {
  78. db_error("Syntax error\n");
  79. /*NOTREACHED*/
  80. }
  81. t = db_read_token();
  82. if (t != tRPAREN) {
  83. db_error("Syntax error\n");
  84. /*NOTREACHED*/
  85. }
  86. return (TRUE);
  87. }
  88. db_unread_token(t);
  89. return (FALSE);
  90. }
  91. boolean_t
  92. db_unary(db_expr_t *valuep)
  93. {
  94. int t;
  95. t = db_read_token();
  96. if (t == tMINUS) {
  97. if (!db_unary(valuep)) {
  98. db_error("Syntax error\n");
  99. /*NOTREACHED*/
  100. }
  101. *valuep = -*valuep;
  102. return (TRUE);
  103. }
  104. if (t == tSTAR) {
  105. /* indirection */
  106. if (!db_unary(valuep)) {
  107. db_error("Syntax error\n");
  108. /*NOTREACHED*/
  109. }
  110. *valuep = db_get_value((db_addr_t)*valuep, sizeof(db_addr_t), FALSE);
  111. return (TRUE);
  112. }
  113. db_unread_token(t);
  114. return (db_term(valuep));
  115. }
  116. boolean_t
  117. db_mult_expr(db_expr_t *valuep)
  118. {
  119. db_expr_t lhs, rhs;
  120. int t;
  121. if (!db_unary(&lhs))
  122. return (FALSE);
  123. t = db_read_token();
  124. while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH) {
  125. if (!db_term(&rhs)) {
  126. db_error("Syntax error\n");
  127. /*NOTREACHED*/
  128. }
  129. if (t == tSTAR)
  130. lhs *= rhs;
  131. else {
  132. if (rhs == 0) {
  133. db_error("Divide by 0\n");
  134. /*NOTREACHED*/
  135. }
  136. if (t == tSLASH)
  137. lhs /= rhs;
  138. else if (t == tPCT)
  139. lhs %= rhs;
  140. else
  141. lhs = ((lhs+rhs-1)/rhs)*rhs;
  142. }
  143. t = db_read_token();
  144. }
  145. db_unread_token(t);
  146. *valuep = lhs;
  147. return (TRUE);
  148. }
  149. boolean_t
  150. db_add_expr(db_expr_t *valuep)
  151. {
  152. db_expr_t lhs, rhs;
  153. int t;
  154. if (!db_mult_expr(&lhs))
  155. return (FALSE);
  156. t = db_read_token();
  157. while (t == tPLUS || t == tMINUS) {
  158. if (!db_mult_expr(&rhs)) {
  159. db_error("Syntax error\n");
  160. /*NOTREACHED*/
  161. }
  162. if (t == tPLUS)
  163. lhs += rhs;
  164. else
  165. lhs -= rhs;
  166. t = db_read_token();
  167. }
  168. db_unread_token(t);
  169. *valuep = lhs;
  170. return (TRUE);
  171. }
  172. boolean_t
  173. db_shift_expr(db_expr_t *valuep)
  174. {
  175. db_expr_t lhs, rhs;
  176. int t;
  177. if (!db_add_expr(&lhs))
  178. return (FALSE);
  179. t = db_read_token();
  180. while (t == tSHIFT_L || t == tSHIFT_R) {
  181. if (!db_add_expr(&rhs)) {
  182. db_error("Syntax error\n");
  183. /*NOTREACHED*/
  184. }
  185. if (rhs < 0) {
  186. db_error("Negative shift amount\n");
  187. /*NOTREACHED*/
  188. }
  189. if (t == tSHIFT_L)
  190. lhs <<= rhs;
  191. else {
  192. /* Shift right is unsigned */
  193. lhs = (unsigned) lhs >> rhs;
  194. }
  195. t = db_read_token();
  196. }
  197. db_unread_token(t);
  198. *valuep = lhs;
  199. return (TRUE);
  200. }
  201. int
  202. db_expression(db_expr_t *valuep)
  203. {
  204. return (db_shift_expr(valuep));
  205. }