body.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 "misc.h"
  12. #include "node.h"
  13. #include "scope.h"
  14. #include "type.h"
  15. MarkDef(nd, flags, on)
  16. register struct node *nd;
  17. unsigned short flags;
  18. {
  19. while( nd && nd->nd_class != Def ) {
  20. if( (nd->nd_class == Arrsel) ||
  21. (nd->nd_class == LinkDef) )
  22. nd = nd->nd_left;
  23. else if( nd->nd_class == Arrow )
  24. nd = nd->nd_right;
  25. else break;
  26. }
  27. if( nd && (nd->nd_class == Def) ) {
  28. if( (flags & D_SET) && on &&
  29. BlockScope != nd->nd_def->df_scope )
  30. nd->nd_def->df_flags |= D_SETINHIGH;
  31. if( on ) {
  32. /*
  33. if( (flags & D_SET) &&
  34. (nd->nd_def->df_flags & D_WITH) )
  35. node_warning(nd,
  36. "variable \"%s\" already referenced in with",
  37. nd->nd_def->df_idf->id_text);
  38. */
  39. nd->nd_def->df_flags |= flags;
  40. }
  41. else
  42. nd->nd_def->df_flags &= ~flags;
  43. }
  44. }
  45. AssertStat(expp, line)
  46. register struct node *expp;
  47. unsigned short line;
  48. {
  49. struct desig dsr;
  50. if( !ChkExpression(expp) )
  51. return;
  52. if( expp->nd_type != bool_type ) {
  53. node_error(expp, "type of assertion should be boolean");
  54. return;
  55. }
  56. if( !options['a'] && !err_occurred ) {
  57. dsr = InitDesig;
  58. CodeExpr(expp, &dsr, NO_LABEL);
  59. C_loc((arith)line);
  60. C_cal("_ass");
  61. }
  62. }
  63. AssignStat(left, right)
  64. register struct node *left, *right;
  65. {
  66. register struct type *ltp, *rtp;
  67. int retval = 0;
  68. struct desig dsr;
  69. retval = ChkExpression(right);
  70. MarkUsed(right);
  71. retval &= ChkLhs(left);
  72. ltp = left->nd_type;
  73. rtp = right->nd_type;
  74. MarkDef(left, (unsigned short)D_SET, 1);
  75. if( !retval ) return;
  76. if( ltp == int_type && rtp == long_type ) {
  77. right = MkNode(IntReduc, NULLNODE, right, &dot);
  78. right->nd_type = int_type;
  79. }
  80. else if( ltp == long_type && rtp == int_type ) {
  81. right = MkNode(IntCoerc, NULLNODE, right, &dot);
  82. right->nd_type = long_type;
  83. }
  84. if( !TstAssCompat(ltp, rtp) ) {
  85. node_error(left, "type incompatibility in assignment");
  86. return;
  87. }
  88. if( left->nd_class == Def &&
  89. (left->nd_def->df_flags & D_INLOOP) ) {
  90. node_error(left, "assignment to a control variable");
  91. return;
  92. }
  93. if( rtp == emptyset_type )
  94. right->nd_type = ltp;
  95. if( !err_occurred ) {
  96. dsr = InitDesig;
  97. CodeExpr(right, &dsr, NO_LABEL);
  98. if( rtp->tp_fund & (T_ARRAY | T_RECORD) )
  99. CodeAddress(&dsr);
  100. else {
  101. CodeValue(&dsr, rtp);
  102. if( ltp == real_type && BaseType(rtp) == int_type )
  103. Int2Real(rtp->tp_size);
  104. RangeCheck(ltp, rtp);
  105. }
  106. CodeMove(&dsr, left, rtp);
  107. }
  108. FreeNode(left);
  109. FreeNode(right);
  110. }
  111. ProcStat(nd)
  112. register struct node *nd;
  113. {
  114. if( !ChkCall(nd) ) return;
  115. if( nd->nd_type ) {
  116. node_error(nd, "procedure call expected");
  117. return;
  118. }
  119. }
  120. ChkForStat(nd)
  121. register struct node *nd;
  122. {
  123. register struct def *df;
  124. int retvar = 0;
  125. retvar = ChkVariable(nd);
  126. retvar &= ChkExpression(nd->nd_left);
  127. MarkUsed(nd->nd_left);
  128. retvar &= ChkExpression(nd->nd_right);
  129. MarkUsed(nd->nd_right);
  130. if( !retvar ) return;
  131. assert(nd->nd_class == Def);
  132. df = nd->nd_def;
  133. if( df->df_scope != BlockScope ) {
  134. node_error(nd, "for loop: control variable must be local");
  135. return;
  136. }
  137. assert(df->df_kind == D_VARIABLE);
  138. if( df->df_scope != GlobalScope && df->var_off >= 0 ) {
  139. node_error(nd,
  140. "for loop: control variable can't be a parameter");
  141. MarkDef(nd,(unsigned short)(D_LOOPVAR | D_SET | D_USED), 1);
  142. return;
  143. }
  144. if( !(df->df_type->tp_fund & T_ORDINAL) ) {
  145. node_error(nd, "for loop: control variable must be ordinal");
  146. MarkDef(nd,(unsigned short)(D_LOOPVAR | D_SET | D_USED), 1);
  147. return;
  148. }
  149. if( !TstCompat(df->df_type, nd->nd_left->nd_type) )
  150. node_error(nd,
  151. "for loop: initial value incompatible with control variable");
  152. if( !TstCompat(df->df_type, nd->nd_right->nd_type) )
  153. node_error(nd,
  154. "for loop: final value incompatible with control variable");
  155. if( df->df_type == long_type )
  156. node_error(nd, "for loop: control variable can not be a long");
  157. if( df->df_flags & D_INLOOP )
  158. node_error(nd, "for loop: control variable already used");
  159. if( df->df_flags & D_SETINHIGH )
  160. node_error(nd,
  161. "for loop: control variable already set in block");
  162. MarkDef(nd,(unsigned short) (D_LOOPVAR | D_INLOOP | D_SET | D_USED), 1);
  163. return;
  164. }
  165. EndForStat(nd)
  166. register struct node *nd;
  167. {
  168. register struct def *df;
  169. df = nd->nd_def;
  170. if( (df->df_scope != BlockScope) ||
  171. (df->df_scope != GlobalScope && df->var_off >= 0) ||
  172. !(df->df_type->tp_fund & T_ORDINAL)
  173. )
  174. return;
  175. MarkDef(nd,(unsigned short) (D_INLOOP | D_SET), 0);
  176. }
  177. arith
  178. CodeInitFor(nd, priority)
  179. register struct node *nd;
  180. {
  181. /* Push final-value, the value may only be evaluated
  182. once, so generate a temporary for it, when not a constant.
  183. */
  184. arith tmp;
  185. CodePExpr(nd);
  186. if( nd->nd_class != Value ) {
  187. tmp = NewInt(priority);
  188. C_dup(int_size);
  189. C_stl(tmp);
  190. return tmp;
  191. }
  192. return (arith) 0;
  193. }
  194. CodeFor(nd, stepsize, l1, l2)
  195. struct node *nd;
  196. label l1, l2;
  197. {
  198. /* Test if loop has to be done */
  199. if( stepsize == 1 ) /* TO */
  200. C_bgt(l2);
  201. else /* DOWNTO */
  202. C_blt(l2);
  203. /* Label at begin of the body */
  204. C_df_ilb(l1);
  205. RangeCheck(nd->nd_type, nd->nd_left->nd_type);
  206. CodeDStore(nd);
  207. }
  208. CodeEndFor(nd, stepsize, l1, l2, tmp2)
  209. struct node *nd;
  210. label l1, l2;
  211. arith tmp2;
  212. {
  213. /* Test if loop has to be done once more */
  214. CodePExpr(nd);
  215. C_dup(int_size);
  216. if( tmp2 )
  217. C_lol(tmp2);
  218. else
  219. CodePExpr(nd->nd_right);
  220. C_beq(l2);
  221. /* Increment/decrement the control-variable */
  222. if( stepsize == 1 ) /* TO */
  223. C_inc();
  224. else /* DOWNTO */
  225. C_dec();
  226. C_bra(l1);
  227. /* Exit label */
  228. C_df_ilb(l2);
  229. C_asp(int_size);
  230. }
  231. WithStat(nd)
  232. struct node *nd;
  233. {
  234. struct withdesig *wds;
  235. struct desig ds;
  236. struct scopelist *scl;
  237. if( nd->nd_type->tp_fund != T_RECORD ) {
  238. node_error(nd, "record variable expected");
  239. return;
  240. }
  241. MarkDef(nd, (unsigned short)(D_USED | D_SET | D_WITH), 1);
  242. /*
  243. if( (nd->nd_class == Arrow) &&
  244. (nd->nd_right->nd_type->tp_fund & T_FILE) ) {
  245. nd->nd_right->nd_def->df_flags |= D_WITH;
  246. }
  247. */
  248. scl = new_scopelist();
  249. scl->sc_scope = nd->nd_type->rec_scope;
  250. scl->next = CurrVis;
  251. CurrVis = scl;
  252. if( err_occurred ) return;
  253. /* Generate code */
  254. CodeDAddress(nd);
  255. wds = new_withdesig();
  256. wds->w_next = WithDesigs;
  257. WithDesigs = wds;
  258. wds->w_scope = scl->sc_scope;
  259. /* create a desig structure for the temporary */
  260. ds.dsg_kind = DSG_FIXED;
  261. ds.dsg_offset = NewPtr(1);
  262. ds.dsg_name = 0;
  263. /* need some pointertype to store pointer */
  264. CodeStore(&ds, nil_type);
  265. /* record is indirectly available */
  266. ds.dsg_kind = DSG_PFIXED;
  267. wds->w_desig = ds;
  268. }
  269. EndWith(saved_scl, nd)
  270. struct scopelist *saved_scl;
  271. struct node *nd;
  272. {
  273. /* restore scope, and release structures */
  274. struct scopelist *scl;
  275. struct withdesig *wds;
  276. struct node *nd1;
  277. while( CurrVis != saved_scl ) {
  278. /* release scopelist */
  279. scl = CurrVis;
  280. CurrVis = CurrVis->next;
  281. free_scopelist(scl);
  282. if( WithDesigs == 0 )
  283. continue; /* we didn't generate any code */
  284. /* release temporary */
  285. FreePtr(WithDesigs->w_desig.dsg_offset);
  286. /* release withdesig */
  287. wds = WithDesigs;
  288. WithDesigs = WithDesigs->w_next;
  289. free_withdesig(wds);
  290. }
  291. for( nd1 = nd; nd1 != NULLNODE; nd1 = nd1->nd_right ) {
  292. MarkDef(nd1->nd_left, (unsigned short)(D_WITH), 0);
  293. }
  294. FreeNode(nd);
  295. }