field.c 4.4 KB

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