field.c 4.5 KB

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