field.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /* BITFIELD EXPRESSION EVALUATOR */
  7. #include "lint.h"
  8. #ifndef LINT
  9. #include "nobitfield.h"
  10. #ifndef NOBITFIELD
  11. #include <em.h>
  12. #include <em_reg.h>
  13. #include "debug.h"
  14. #include <flt_arith.h>
  15. #include "arith.h"
  16. #include "type.h"
  17. #include "idf.h"
  18. #include "label.h"
  19. #include "code.h"
  20. #include "assert.h"
  21. #include "expr.h"
  22. #include "sizes.h"
  23. #include "align.h"
  24. #include "Lpars.h"
  25. #include "field.h"
  26. #include "conversion.h"
  27. arith NewLocal(); /* util.c */
  28. extern arith full_mask[]; /* cstoper.c */
  29. /* Eval_field() evaluates expressions involving bit fields.
  30. The various instructions are not yet optimised in the expression
  31. tree and are therefore dealt with in this function.
  32. The actions taken at any operation are described clearly by the
  33. code for this actions.
  34. Notes
  35. [1] the bitfields are packed in target machine integers!
  36. [2] op is either an assignment operator or an increment/
  37. decrement operator
  38. [3] atype: the type in which the bitfield arithmetic is done;
  39. and in which bitfields are stored!
  40. */
  41. eval_field(expr, code)
  42. struct expr *expr;
  43. int code;
  44. {
  45. int op = expr->OP_OPER;
  46. register struct expr *leftop = expr->OP_LEFT;
  47. register struct expr *rightop = expr->OP_RIGHT;
  48. register struct field *fd = leftop->ex_type->tp_field;
  49. struct type *tp = leftop->ex_type->tp_up;
  50. arith tmpvar = 0;
  51. struct type *atype = ( tp->tp_unsigned
  52. && fd->fd_width >= 8 * (int)word_size)
  53. ? uword_type
  54. : word_type;
  55. /* First some assertions to be sure that the rest is legal */
  56. ASSERT(atype->tp_size == word_size); /* make sure that C_loc() is legal */
  57. ASSERT(leftop->ex_type->tp_fund == FIELD);
  58. leftop->ex_type = atype; /* this is cheating but it works... */
  59. if (op == '=') {
  60. /* F = E: f = ((E & mask)<<shift) | (~(mask<<shift) & f) */
  61. ASSERT(tp == rightop->ex_type);
  62. EVAL(rightop, RVAL, TRUE, NO_LABEL, NO_LABEL);
  63. conversion(tp, atype);
  64. store_field(fd, tp->tp_unsigned, code, leftop, (arith) 0);
  65. }
  66. else { /* treat ++F as F += 1 and --F as F -= 1 */
  67. /* F op= e: f = (((((f>>shift)&mask) op e)&mask)<<shift)|
  68. (f&~(mask<<shift))
  69. */
  70. if (leftop->ex_depth == 0) /* simple case */
  71. load_val(leftop, RVAL);
  72. else { /* complex case */
  73. tmpvar = NewLocal(pointer_size, pointer_align,
  74. reg_pointer, 0);
  75. EVAL(leftop, LVAL, TRUE, NO_LABEL, NO_LABEL);
  76. C_dup(pointer_size);
  77. StoreLocal(tmpvar, pointer_size);
  78. C_loi(word_size);
  79. }
  80. if (tp->tp_unsigned) {
  81. C_loc((arith)fd->fd_shift);
  82. C_sru(word_size);
  83. C_loc(fd->fd_mask);
  84. C_and(word_size);
  85. }
  86. else {
  87. arith sft = (int)word_size * 8 - fd->fd_width;
  88. C_loc(sft - fd->fd_shift);
  89. C_sli(word_size);
  90. C_loc(sft);
  91. C_sri(word_size);
  92. }
  93. if (code == TRUE && (op == POSTINCR || op == POSTDECR))
  94. C_dup(word_size);
  95. conversion(atype, rightop->ex_type);
  96. EVAL(rightop, RVAL, TRUE, NO_LABEL, NO_LABEL);
  97. /* the 'op' operation: */
  98. if (op == PLUSPLUS || op == POSTINCR)
  99. assop(rightop->ex_type, PLUSAB);
  100. else
  101. if (op == MINMIN || op == POSTDECR)
  102. assop(rightop->ex_type, MINAB);
  103. else
  104. assop(rightop->ex_type, op);
  105. conversion(rightop->ex_type, atype);
  106. store_field(fd, atype->tp_unsigned,
  107. code == TRUE && op != POSTINCR && op != POSTDECR,
  108. leftop, tmpvar);
  109. }
  110. if (code == TRUE) {
  111. /* Take care that the effective value stored in
  112. the bit field (i.e. the value that is got on
  113. retrieval) is on top of stack.
  114. */
  115. if (tp->tp_unsigned == 0) { /* sign extension */
  116. register arith shift = (int)word_size * 8 - fd->fd_width;
  117. C_loc(shift);
  118. C_sli(word_size);
  119. C_loc(shift);
  120. C_sri(word_size);
  121. }
  122. conversion(atype, expr->ex_type);
  123. }
  124. }
  125. store_field(fd, uns, code, leftop, tmpvar)
  126. register struct field *fd;
  127. int uns;
  128. int code;
  129. register struct expr *leftop;
  130. arith tmpvar;
  131. {
  132. C_loc(fd->fd_mask);
  133. C_and(word_size);
  134. if (code == TRUE)
  135. C_dup(word_size);
  136. C_loc((arith)fd->fd_shift);
  137. if (uns)
  138. C_slu(word_size);
  139. else
  140. C_sli(word_size);
  141. C_loc(~((fd->fd_mask << fd->fd_shift) | ~full_mask[(int)word_size]));
  142. if (leftop->ex_depth == 0) { /* simple case */
  143. load_val(leftop, RVAL);
  144. C_and(word_size);
  145. C_ior(word_size);
  146. store_val(&(leftop->EX_VALUE), uns ? uword_type : word_type);
  147. }
  148. else { /* complex case */
  149. if (! tmpvar) {
  150. tmpvar = NewLocal(pointer_size, pointer_align,
  151. reg_pointer, 0);
  152. EVAL(leftop, LVAL, TRUE, NO_LABEL, NO_LABEL);
  153. StoreLocal(tmpvar, pointer_size);
  154. }
  155. LoadLocal(tmpvar, pointer_size);
  156. C_loi(word_size);
  157. C_and(word_size);
  158. C_ior(word_size);
  159. LoadLocal(tmpvar, pointer_size);
  160. C_sti(word_size);
  161. FreeLocal(tmpvar);
  162. }
  163. }
  164. #endif /* NOBITFIELD */
  165. #endif /* LINT */