ch3mon.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /* SEMANTIC ANALYSIS (CHAPTER 3.3) -- MONADIC OPERATORS */
  7. #include "botch_free.h"
  8. #include "debug.h"
  9. #include <alloc.h>
  10. #include "nobitfield.h"
  11. #include "Lpars.h"
  12. #include <flt_arith.h>
  13. #include "arith.h"
  14. #include "type.h"
  15. #include "label.h"
  16. #include "expr.h"
  17. #include "idf.h"
  18. #include "def.h"
  19. #include "sizes.h"
  20. #include "ch3.h"
  21. #include "ch3mon.h"
  22. #include "error.h"
  23. #include "expr_loc.h"
  24. #include "stab.h"
  25. #include "util.h"
  26. #include "struct_loc.h"
  27. #include "dataflow.h"
  28. #include <symbol2str.h>
  29. extern char options[];
  30. extern arith full_mask[/*MAXSIZE + 1*/]; /* cstoper.c */
  31. void ch3mon(int oper, struct expr **expp)
  32. {
  33. /* The monadic prefix operator oper is applied to *expp.
  34. */
  35. register struct expr *expr;
  36. if (oper != PLUSPLUS && oper != MINMIN)
  37. any2opnd(expp, oper);
  38. switch (oper) {
  39. case '*': /* 3.3.3.2 */
  40. /* no FIELD type allowed */
  41. expr = *expp;
  42. if (expr->ex_type->tp_fund != POINTER) {
  43. if (expr->ex_type != error_type) {
  44. expr_error(expr,
  45. "* applied to non-pointer (%s)",
  46. symbol2str(expr->ex_type->tp_fund));
  47. }
  48. } else {
  49. if (is_ld_cst(expr))
  50. /* dereference in administration only */
  51. expr->ex_type = expr->ex_type->tp_up;
  52. else /* runtime code */
  53. *expp = new_oper(expr->ex_type->tp_up, NILEXPR,
  54. '*', expr);
  55. expr = *expp;
  56. expr->ex_lvalue = (
  57. expr->ex_type->tp_fund != ARRAY &&
  58. expr->ex_type->tp_fund != FUNCTION
  59. );
  60. if (expr->ex_lvalue && expr->ex_type->tp_size <= 0) {
  61. expr_error(expr, "incomplete type in expression");
  62. }
  63. if (expr->ex_type->tp_typequal & TQ_CONST)
  64. expr->ex_flags |= EX_READONLY;
  65. if (expr->ex_type->tp_typequal & TQ_VOLATILE)
  66. expr->ex_flags |= EX_VOLATILE;
  67. expr->ex_flags &= ~EX_ILVALUE;
  68. }
  69. break;
  70. case ADDRESSOF:
  71. if ((*expp)->ex_type->tp_fund == ARRAY) {
  72. (*expp)->ex_type = pointer_to((*expp)->ex_type, 0);
  73. }
  74. else
  75. if ((*expp)->ex_type->tp_fund == FUNCTION) {
  76. (*expp)->ex_type = pointer_to((*expp)->ex_type, 0);
  77. }
  78. else
  79. #ifndef NOBITFIELD
  80. if ((*expp)->ex_type->tp_fund == FIELD)
  81. expr_error(*expp, "& applied to field variable");
  82. else
  83. #endif /* NOBITFIELD */
  84. if (!(*expp)->ex_lvalue)
  85. expr_error(*expp, "& applied to non-lvalue");
  86. else if ((*expp)->ex_flags & EX_ILVALUE)
  87. expr_error(*expp, "& applied to illegal lvalue");
  88. else {
  89. /* assume that enums are already filtered out */
  90. if (ISNAME(*expp)) {
  91. register struct def *def =
  92. (*expp)->VL_IDF->id_def;
  93. /* &<var> indicates that <var>
  94. cannot be used as register
  95. anymore
  96. */
  97. if (def->df_sc == REGISTER) {
  98. expr_error(*expp,
  99. "& on register variable not allowed");
  100. break; /* break case ADDRESSOF */
  101. }
  102. }
  103. (*expp)->ex_type = pointer_to((*expp)->ex_type,
  104. (*expp)->ex_type->tp_typequal);
  105. (*expp)->ex_lvalue = 0;
  106. (*expp)->ex_flags &= ~(EX_READONLY | EX_VOLATILE);
  107. }
  108. break;
  109. case '~':
  110. {
  111. int fund = (*expp)->ex_type->tp_fund;
  112. if (fund == FLOAT || fund == DOUBLE || fund == LNGDBL) {
  113. expr_error( *expp,
  114. "~ not allowed on %s operands",
  115. symbol2str(fund));
  116. erroneous2int(expp);
  117. break;
  118. }
  119. /* FALLTHROUGH */
  120. }
  121. case '-':
  122. any2arith(expp, oper);
  123. if (is_cp_cst(*expp)) {
  124. arith o1 = (*expp)->VL_VALUE;
  125. (*expp)->VL_VALUE = (oper == '-') ? -o1 :
  126. ((*expp)->ex_type->tp_unsigned ?
  127. (~o1) & full_mask[(int)(*expp)->ex_type->tp_size] :
  128. ~o1
  129. );
  130. }
  131. else
  132. if (is_fp_cst(*expp))
  133. switch_sign_fp(*expp);
  134. else
  135. *expp = new_oper((*expp)->ex_type,
  136. NILEXPR, oper, *expp);
  137. break;
  138. case '!':
  139. opnd2test(expp, '!');
  140. if (is_cp_cst(*expp)) {
  141. (*expp)->VL_VALUE = !((*expp)->VL_VALUE);
  142. (*expp)->ex_type = int_type; /* a cast ???(EB) */
  143. }
  144. else
  145. *expp = new_oper(int_type, NILEXPR, oper, *expp);
  146. (*expp)->ex_flags |= EX_LOGICAL;
  147. break;
  148. case PLUSPLUS:
  149. case MINMIN:
  150. ch3incr(expp, oper);
  151. break;
  152. case SIZEOF:
  153. if (ISNAME(*expp) && (*expp)->VL_IDF->id_def->df_formal_array)
  154. expr_warning(*expp, "sizeof formal array %s is sizeof pointer!",
  155. (*expp)->VL_IDF->id_text);
  156. expr = intexpr((*expp)->ex_class == String ?
  157. (arith)((*expp)->SG_LEN) :
  158. size_of_type((*expp)->ex_type,
  159. symbol2str((*expp)->ex_type->tp_fund))
  160. , UNSIGNED);
  161. expr->ex_flags |= EX_SIZEOF;
  162. free_expression(*expp);
  163. *expp = expr;
  164. break;
  165. }
  166. }