blocks.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /* B L O C K S T O R I N G A N D L O A D I N G */
  7. #include "lint.h"
  8. #ifndef LINT
  9. #include <em.h>
  10. #include <em_reg.h>
  11. #include "arith.h"
  12. #include "sizes.h"
  13. #include "atw.h"
  14. #include "align.h"
  15. #ifndef STB
  16. #include "label.h"
  17. #include "stack.h"
  18. #include "Lpars.h"
  19. #define LocalPtrVar() NewLocal(pointer_size, pointer_align, reg_pointer, REGISTER)
  20. #define LocalIntVar() NewLocal(int_size, int_align, reg_any, REGISTER)
  21. static void copy_loop(arith sz, arith src, arith dst);
  22. #endif /* STB */
  23. /* Because EM does not support the loading and storing of
  24. objects having other sizes than word fragment and multiple,
  25. we need to have a way of transferring these objects, whereby
  26. we simulate "loi" and "sti": the address of the source resp.
  27. destination is located on top of stack and a call is done
  28. to load_block() resp. store_block().
  29. ===============================================================
  30. # Loadblock() works on the stack as follows: ([ ] indicates the
  31. # position of the stackpointer)
  32. # lower address--->
  33. # 1) | &object
  34. # 2) | ... ATW(sz) bytes ... | sz | &stack_block | &object
  35. # 3) | ... ATW(sz) bytes ...
  36. ===============================================================
  37. Loadblock() pushes ATW(sz) bytes directly onto the stack!
  38. Store_block() works on the stack as follows:
  39. lower address--->
  40. 1) | ... ATW(sz) bytes ... | &object
  41. 2) | ... ATW(sz) bytes ... | &object | &stack_block | sz
  42. 3) <empty>
  43. If sz is a legal argument for "loi" or "sti", just one EM
  44. instruction is generated.
  45. In the other cases, the notion of alignment is taken into account:
  46. we only push an object of the size accepted by EM onto the stack,
  47. while we need a loop to store the stack block into a memory object.
  48. */
  49. static int suitable_sz(arith sz, int al)
  50. {
  51. return ((int)sz % (int)word_size == 0 && al % word_align == 0) ||
  52. (
  53. word_size % sz == 0 &&
  54. (al >= (int)sz || al >= word_align)
  55. /* Lots of Irritating Stupid Parentheses */
  56. );
  57. }
  58. void store_block(arith sz, int al)
  59. {
  60. if (suitable_sz(sz, al))
  61. C_sti(sz);
  62. else {
  63. #ifndef STB
  64. arith src, dst;
  65. /* allocate two pointer temporaries */
  66. src = LocalPtrVar();
  67. dst = LocalPtrVar();
  68. /* load the addresses */
  69. StoreLocal(dst, pointer_size);
  70. C_lor((arith)1); /* push current sp */
  71. StoreLocal(src, pointer_size);
  72. copy_loop(sz, src, dst);
  73. C_asp(ATW(sz));
  74. FreeLocal(dst);
  75. FreeLocal(src);
  76. #else /* STB */
  77. /* address of destination lies on the stack */
  78. /* push address of first byte of block on stack onto
  79. the stack by computing it from the current stack
  80. pointer position
  81. */
  82. C_lor((arith)1); /* push current sp */
  83. C_adp(pointer_size); /* set & to 1st byte of block */
  84. C_loc(sz); /* number of bytes to transfer */
  85. C_cal("__stb"); /* call transfer routine */
  86. C_asp(pointer_size + pointer_size + int_size + ATW(sz));
  87. #endif /* STB */
  88. }
  89. }
  90. void load_block(arith sz, int al)
  91. {
  92. if (suitable_sz(sz, al))
  93. C_loi(sz);
  94. else {
  95. #ifndef STB
  96. arith src, dst;
  97. /* allocate two pointer temporaries */
  98. src = LocalPtrVar();
  99. dst = LocalPtrVar();
  100. StoreLocal(src, pointer_size);
  101. C_asp(-ATW(sz)); /* allocate stack block */
  102. C_lor((arith)1); /* push & of stack block as dst */
  103. StoreLocal(dst, pointer_size);
  104. copy_loop(sz, src, dst);
  105. FreeLocal(dst);
  106. FreeLocal(src);
  107. #else /* STB */
  108. arith esz = ATW(sz) - pointer_size;
  109. C_asp(-esz); /* allocate stack block */
  110. C_lor((arith)1); /* push & of stack block as dst */
  111. C_dup(pointer_size); /* fetch source address */
  112. C_adp(esz);
  113. C_loi(pointer_size);
  114. C_loc(sz); /* # bytes to copy */
  115. C_cal("__stb"); /* library copy routine */
  116. C_asp(int_size + pointer_size + pointer_size);
  117. #endif /* STB */
  118. }
  119. }
  120. void copy_block(arith sz, int al)
  121. {
  122. if (suitable_sz(sz, al))
  123. C_blm(sz);
  124. else {
  125. #ifndef STB
  126. arith src, dst;
  127. /* allocate two pointer temporaries */
  128. src = LocalPtrVar();
  129. dst = LocalPtrVar();
  130. StoreLocal(dst, pointer_size);
  131. StoreLocal(src, pointer_size);
  132. copy_loop(sz, src, dst);
  133. FreeLocal(dst);
  134. FreeLocal(src);
  135. #else /* STB */
  136. C_loc(sz); /* # bytes to copy */
  137. C_cal("__stb"); /* library copy routine */
  138. C_asp(int_size + pointer_size + pointer_size);
  139. #endif /* STB */
  140. }
  141. }
  142. #ifndef STB
  143. static void copy_loop(arith sz, arith src, arith dst)
  144. {
  145. /* generate inline byte-copy loop */
  146. label l_cont = text_label(), l_stop = text_label();
  147. arith tmp_sz = LocalIntVar();
  148. C_loc(sz); /* amount of bytes */
  149. StoreLocal(tmp_sz, int_size);
  150. C_df_ilb(l_cont);
  151. LoadLocal(tmp_sz, int_size);
  152. C_zle(l_stop);
  153. C_del(tmp_sz);
  154. LoadLocal(src, pointer_size);
  155. C_dup(pointer_size);
  156. C_adp((arith)1);
  157. StoreLocal(src, pointer_size);
  158. C_loi((arith)1);
  159. LoadLocal(dst, pointer_size);
  160. C_dup(pointer_size);
  161. C_adp((arith)1);
  162. StoreLocal(dst, pointer_size);
  163. C_sti((arith)1);
  164. C_bra(l_cont);
  165. C_df_ilb(l_stop);
  166. FreeLocal(tmp_sz);
  167. }
  168. #endif /* STB */
  169. #endif /* LINT */