field.c 4.8 KB

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