body.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "debug.h"
  2. #include <alloc.h>
  3. #include <assert.h>
  4. #include <em.h>
  5. #include "LLlex.h"
  6. #include "chk_expr.h"
  7. #include "def.h"
  8. #include "desig.h"
  9. #include "idf.h"
  10. #include "main.h"
  11. #include "node.h"
  12. #include "scope.h"
  13. #include "type.h"
  14. AssignStat(left, right)
  15. register struct node *left, *right;
  16. {
  17. register struct type *ltp, *rtp;
  18. struct desig dsr;
  19. if( !(ChkExpression(right) && ChkLhs(left)) )
  20. return;
  21. ltp = left->nd_type;
  22. rtp = right->nd_type;
  23. if( !TstAssCompat(ltp, rtp) ) {
  24. node_error(left, "type incompatibility in assignment");
  25. return;
  26. }
  27. if( rtp == emptyset_type )
  28. right->nd_type = ltp;
  29. if( !err_occurred ) {
  30. dsr = InitDesig;
  31. CodeExpr(right, &dsr, NO_LABEL);
  32. if( rtp->tp_fund & (T_ARRAY | T_RECORD) )
  33. CodeAddress(&dsr);
  34. else {
  35. CodeValue(&dsr, rtp);
  36. if( ltp == real_type && BaseType(rtp) == int_type )
  37. Int2Real();
  38. RangeCheck(ltp, rtp);
  39. }
  40. CodeMove(&dsr, left, rtp);
  41. }
  42. FreeNode(left);
  43. FreeNode(right);
  44. }
  45. ProcStat(nd)
  46. register struct node *nd;
  47. {
  48. if( !ChkCall(nd) ) return;
  49. if( nd->nd_type ) {
  50. node_error(nd, "procedure call expected");
  51. return;
  52. }
  53. }
  54. ChkForStat(nd)
  55. register struct node *nd;
  56. {
  57. register struct def *df;
  58. if( !(ChkVariable(nd) && ChkExpression(nd->nd_left) &&
  59. ChkExpression(nd->nd_right)) )
  60. return;
  61. assert(nd->nd_class == Def);
  62. df = nd->nd_def;
  63. if( df->df_scope != BlockScope ) {
  64. node_error(nd, "for loop: control variable must be local");
  65. return;
  66. }
  67. assert(df->df_kind == D_VARIABLE);
  68. if( df->df_scope != GlobalScope && df->var_off >= 0 ) {
  69. node_error(nd,"for loop: control variable can't be a parameter");
  70. return;
  71. }
  72. if( !(df->df_type->tp_fund & T_ORDINAL) ) {
  73. node_error(nd, "for loop: control variable must be ordinal");
  74. return;
  75. }
  76. if( !TstCompat(df->df_type, nd->nd_left->nd_type) )
  77. node_error(nd,
  78. "for loop: initial value incompatible with control variable");
  79. if( !TstCompat(df->df_type, nd->nd_right->nd_type) )
  80. node_error(nd,
  81. "for loop: final value incompatible with control variable");
  82. df->df_flags |= D_LOOPVAR;
  83. return;
  84. }
  85. arith
  86. CodeInitFor(nd, priority)
  87. register struct node *nd;
  88. {
  89. /* Push init-value or final-value, the value may only be evaluated
  90. once, so generate a temporary for it, when not a constant.
  91. */
  92. arith tmp;
  93. CodePExpr(nd);
  94. if( nd->nd_class != Value ) {
  95. tmp = NewInt(priority);
  96. C_dup(int_size);
  97. C_stl(tmp);
  98. return tmp;
  99. }
  100. return (arith) 0;
  101. }
  102. CodeFor(nd, stepsize, l1, l2, tmp1)
  103. struct node *nd;
  104. label l1, l2;
  105. arith tmp1;
  106. {
  107. /* Test if loop has to be done */
  108. if( stepsize == 1 ) /* TO */
  109. C_bgt(l2);
  110. else /* DOWNTO */
  111. C_blt(l2);
  112. /* Store init-value in control-variable */
  113. if( tmp1 )
  114. C_lol(tmp1);
  115. else
  116. CodePExpr(nd->nd_left);
  117. /* Label at begin of the body */
  118. C_df_ilb(l1);
  119. RangeCheck(nd->nd_type, nd->nd_left->nd_type);
  120. CodeDStore(nd);
  121. }
  122. CodeEndFor(nd, stepsize, l1, l2, tmp2)
  123. struct node *nd;
  124. label l1, l2;
  125. arith tmp2;
  126. {
  127. /* Test if loop has to be done once more */
  128. CodePExpr(nd);
  129. C_dup(int_size);
  130. if( tmp2 )
  131. C_lol(tmp2);
  132. else
  133. CodePExpr(nd->nd_right);
  134. C_beq(l2);
  135. /* Increment/decrement the control-variable */
  136. if( stepsize == 1 ) /* TO */
  137. C_inc();
  138. else /* DOWNTO */
  139. C_dec();
  140. C_bra(l1);
  141. /* Exit label */
  142. C_df_ilb(l2);
  143. }
  144. WithStat(nd)
  145. struct node *nd;
  146. {
  147. struct withdesig *wds;
  148. struct desig ds;
  149. struct scopelist *scl;
  150. if( nd->nd_type->tp_fund != T_RECORD ) {
  151. node_error(nd, "record variable expected");
  152. return;
  153. }
  154. if( err_occurred ) return;
  155. /* Generate code */
  156. CodeDAddress(nd);
  157. wds = new_withdesig();
  158. wds->w_next = WithDesigs;
  159. WithDesigs = wds;
  160. wds->w_scope = nd->nd_type->rec_scope;
  161. /* create a desig structure for the temporary */
  162. ds.dsg_kind = DSG_FIXED;
  163. ds.dsg_offset = NewPtr(1);
  164. ds.dsg_name = 0;
  165. /* need some pointertype to store pointer */
  166. CodeStore(&ds, nil_type);
  167. /* record is indirectly available */
  168. ds.dsg_kind = DSG_PFIXED;
  169. wds->w_desig = ds;
  170. scl = new_scopelist();
  171. scl->sc_scope = wds->w_scope;
  172. scl->next = CurrVis;
  173. CurrVis = scl;
  174. }
  175. EndWith(saved_scl, nd)
  176. struct scopelist *saved_scl;
  177. struct node *nd;
  178. {
  179. /* restore scope, and release structures */
  180. struct scopelist *scl;
  181. struct withdesig *wds;
  182. while( CurrVis != saved_scl ) {
  183. /* release scopelist */
  184. scl = CurrVis;
  185. CurrVis = CurrVis->next;
  186. free_scopelist(scl);
  187. /* release temporary */
  188. FreePtr(WithDesigs->w_desig.dsg_offset);
  189. /* release withdesig */
  190. wds = WithDesigs;
  191. WithDesigs = WithDesigs->w_next;
  192. free_withdesig(wds);
  193. }
  194. FreeNode(nd);
  195. }