desig.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /* D E S I G N A T O R E V A L U A T I O N */
  2. /* Code generation for designators.
  3. This file contains some routines that generate code common to address
  4. as well as value computations, and leave a description in a "desig"
  5. structure. It also contains routines to load an address, load a value
  6. or perform a store.
  7. */
  8. #include "debug.h"
  9. #include <assert.h>
  10. #include <em.h>
  11. #include "LLlex.h"
  12. #include "def.h"
  13. #include "desig.h"
  14. #include "main.h"
  15. #include "node.h"
  16. #include "scope.h"
  17. #include "type.h"
  18. struct desig InitDesig = {DSG_INIT, 0, 0, NULLDEF, 0};
  19. struct withdesig *WithDesigs;
  20. STATIC int
  21. properly(ds, size, al)
  22. register struct desig *ds;
  23. arith size;
  24. {
  25. /* Check if it is allowed to load or store the value indicated
  26. by "ds" with LOI/STI.
  27. - if the size is not either a multiple or a dividor of the
  28. wordsize, then not.
  29. - if the alignment is at least "word" then OK.
  30. - if size is dividor of word_size and alignment >= size then OK.
  31. - otherwise check alignment of address. This can only be done
  32. with DSG_FIXED.
  33. */
  34. arith szmodword = size % word_size; /* 0 if multiple of wordsize */
  35. arith wordmodsz = word_size % size; /* 0 if dividor of wordsize */
  36. if( szmodword && wordmodsz ) return 0;
  37. if( al >= word_align ) return 1;
  38. if( szmodword && al >= szmodword ) return 1;
  39. return ds->dsg_kind == DSG_FIXED &&
  40. ((! szmodword && ds->dsg_offset % word_align == 0) ||
  41. (! wordmodsz && ds->dsg_offset % size == 0));
  42. }
  43. CodeCopy(lhs, rhs, sz, psize)
  44. register struct desig *lhs, *rhs;
  45. arith sz, *psize;
  46. {
  47. struct desig l, r;
  48. l = *lhs;
  49. r = *rhs;
  50. *psize -= sz;
  51. lhs->dsg_offset += sz;
  52. rhs->dsg_offset += sz;
  53. CodeAddress(&r);
  54. C_loi(sz);
  55. CodeAddress(&l);
  56. C_sti(sz);
  57. }
  58. CodeMove(rhs, left, rtp)
  59. register struct desig *rhs;
  60. register struct node *left;
  61. struct type *rtp;
  62. {
  63. struct desig dsl;
  64. register struct desig *lhs = &dsl;
  65. register struct type *ltp = left->nd_type;
  66. dsl = InitDesig;
  67. /* Generate code for an assignment. Testing of type
  68. compatibility and the like is already done.
  69. Go through some (considerable) trouble to see if
  70. a BLM can be generated.
  71. */
  72. switch( rhs->dsg_kind ) {
  73. case DSG_LOADED:
  74. CodeDesig(left, lhs);
  75. if( rtp->tp_fund == T_STRING ) {
  76. CodeAddress(lhs);
  77. C_blm(lhs->dsg_packed ? ltp->tp_psize : ltp->tp_size);
  78. return;
  79. }
  80. CodeStore(lhs, ltp);
  81. return;
  82. case DSG_PLOADED:
  83. case DSG_PFIXED:
  84. CodeAddress(rhs);
  85. CodeValue(rhs, rtp);
  86. CodeDStore(left);
  87. return;
  88. case DSG_FIXED: {
  89. arith tpsize;
  90. CodeDesig(left, lhs);
  91. tpsize = lhs->dsg_packed ? ltp->tp_psize : ltp->tp_size;
  92. if( lhs->dsg_kind == DSG_FIXED &&
  93. lhs->dsg_offset % word_size == rhs->dsg_offset % word_size
  94. ) {
  95. arith size = tpsize;
  96. if( size > 6 * word_size ) {
  97. /* Do a block move
  98. */
  99. struct desig l, r;
  100. l = *lhs;
  101. r = *rhs;
  102. CodeAddress(&r);
  103. CodeAddress(&l);
  104. C_blm(size);
  105. }
  106. else {
  107. register arith sz;
  108. for( sz = 2 * word_size; sz; sz -= word_size) {
  109. while( size >= sz )
  110. /* Then copy dwords, words.
  111. Depend on peephole optimizer
  112. */
  113. CodeCopy(lhs, rhs, sz, &size);
  114. }
  115. }
  116. return;
  117. }
  118. if( lhs->dsg_kind == DSG_PLOADED ||
  119. lhs->dsg_kind == DSG_INDEXED ) {
  120. CodeAddress(lhs);
  121. }
  122. }
  123. default:
  124. crash("(CodeMove)");
  125. /*NOTREACHED*/
  126. }
  127. }
  128. CodeValue(ds, tp)
  129. register struct desig *ds;
  130. register struct type *tp;
  131. {
  132. /* Generate code to load the value of the designator described
  133. in "ds"
  134. */
  135. arith size = ds->dsg_packed ? tp->tp_psize : tp->tp_size;
  136. int align = ds->dsg_packed ? tp->tp_palign : tp->tp_align;
  137. switch( ds->dsg_kind ) {
  138. case DSG_LOADED:
  139. break;
  140. case DSG_FIXED:
  141. if( ds->dsg_offset % word_size == 0 && size == word_size ) {
  142. if( ds->dsg_name )
  143. C_loe_dnam(ds->dsg_name, ds->dsg_offset);
  144. else
  145. C_lol(ds->dsg_offset);
  146. break;
  147. }
  148. /* Fall through */
  149. case DSG_PLOADED:
  150. case DSG_PFIXED:
  151. if( properly(ds, size, align) ) {
  152. CodeAddress(ds);
  153. C_loi(size);
  154. break;
  155. }
  156. printf("(CodeValue) : not properly");
  157. break;
  158. case DSG_INDEXED:
  159. C_lar(word_size);
  160. break;
  161. default:
  162. crash("(CodeValue)");
  163. /*NOTREACHED*/
  164. }
  165. ds->dsg_kind = DSG_LOADED;
  166. }
  167. CodeStore(ds, tp)
  168. register struct desig *ds;
  169. register struct type *tp;
  170. {
  171. /* Generate code to store the value on the stack in the designator
  172. described in "ds"
  173. */
  174. struct desig save;
  175. arith size = ds->dsg_packed ? tp->tp_psize : tp->tp_size;
  176. int align = ds->dsg_packed ? tp->tp_palign : tp->tp_align;
  177. save = *ds;
  178. switch( ds->dsg_kind ) {
  179. case DSG_FIXED:
  180. if( ds->dsg_offset % word_size == 0 && size == word_size ) {
  181. if( ds->dsg_name )
  182. C_ste_dnam(ds->dsg_name, ds->dsg_offset);
  183. else
  184. C_stl(ds->dsg_offset);
  185. break;
  186. }
  187. /* Fall through */
  188. case DSG_PLOADED:
  189. case DSG_PFIXED:
  190. CodeAddress(&save);
  191. if( properly(ds, size, align) ) {
  192. C_sti(size);
  193. break;
  194. }
  195. printf("(CodeStore) : not properly");
  196. break;
  197. case DSG_INDEXED:
  198. C_sar(word_size);
  199. break;
  200. default:
  201. crash("(CodeStore)");
  202. /*NOTREACHED*/
  203. }
  204. ds->dsg_kind = DSG_INIT;
  205. }
  206. CodeAddress(ds)
  207. register struct desig *ds;
  208. {
  209. /* Generate code to load the address of the designator described
  210. in "ds"
  211. */
  212. switch( ds->dsg_kind ) {
  213. case DSG_PLOADED:
  214. if( ds->dsg_offset )
  215. C_adp(ds->dsg_offset);
  216. break;
  217. case DSG_FIXED:
  218. if( ds->dsg_name ) {
  219. C_lae_dnam(ds->dsg_name, ds->dsg_offset);
  220. break;
  221. }
  222. C_lal(ds->dsg_offset);
  223. if( ds->dsg_def )
  224. ds->dsg_def->df_flags |= D_NOREG;
  225. break;
  226. case DSG_PFIXED:
  227. if( ds->dsg_name )
  228. C_loe_dnam(ds->dsg_name, ds->dsg_offset);
  229. else
  230. C_lol(ds->dsg_offset);
  231. break;
  232. case DSG_INDEXED:
  233. C_aar(word_size);
  234. break;
  235. default:
  236. crash("(CodeAddress)");
  237. /*NOTREACHED*/
  238. }
  239. ds->dsg_offset = 0;
  240. ds->dsg_kind = DSG_PLOADED;
  241. }
  242. CodeFieldDesig(df, ds)
  243. register struct def *df;
  244. register struct desig *ds;
  245. {
  246. /* Generate code for a field designator. Only the code common for
  247. address as well as value computation is generated, and the
  248. resulting information on where to find the designator is placed
  249. in "ds". "df" indicates the definition of the field.
  250. */
  251. if( ds->dsg_kind == DSG_INIT ) {
  252. /* In a WITH statement. We must find the designator in the
  253. WITH statement, and act as if the field is a selection
  254. of this designator.
  255. So, first find the right WITH statement, which is the
  256. first one of the proper record type, which is
  257. recognized by its scope indication.
  258. */
  259. register struct withdesig *wds = WithDesigs;
  260. assert(wds != 0);
  261. while( wds->w_scope != df->df_scope ) {
  262. wds = wds->w_next;
  263. assert(wds != 0);
  264. }
  265. /* Found it. Now, act like it was a selection.
  266. */
  267. *ds = wds->w_desig;
  268. assert(ds->dsg_kind == DSG_PFIXED);
  269. }
  270. switch( ds->dsg_kind ) {
  271. case DSG_PLOADED:
  272. case DSG_FIXED:
  273. ds->dsg_offset += df->fld_off;
  274. break;
  275. case DSG_PFIXED:
  276. case DSG_INDEXED:
  277. CodeAddress(ds);
  278. ds->dsg_kind = DSG_PLOADED;
  279. ds->dsg_offset = df->fld_off;
  280. break;
  281. default:
  282. crash("(CodeFieldDesig)");
  283. }
  284. ds->dsg_packed = df->fld_flags & F_PACKED;
  285. }
  286. CodeVarDesig(df, ds)
  287. register struct def *df;
  288. register struct desig *ds;
  289. {
  290. /* Generate code for a variable represented by a "def" structure.
  291. Of course, there are numerous cases: the variable is local,
  292. it is a value parameter, it is a var parameter, it is one of
  293. those of an enclosing procedure, or it is global.
  294. */
  295. register struct scope *sc = df->df_scope;
  296. assert(ds->dsg_kind == DSG_INIT);
  297. if( df->var_name ) {
  298. /* this variable has been given a name, so it is global.
  299. It is directly accessible.
  300. */
  301. ds->dsg_name = df->var_name;
  302. ds->dsg_offset = 0;
  303. ds->dsg_kind = DSG_FIXED;
  304. return;
  305. }
  306. if( sc->sc_level != proclevel ) {
  307. /* the variable is local to a statically enclosing procedure.
  308. */
  309. assert(proclevel > sc->sc_level);
  310. df->df_flags |= D_NOREG;
  311. if( df->df_flags & (D_VARPAR|D_VALPAR) ) {
  312. /* value or var parameter
  313. */
  314. C_lxa((arith) (proclevel - sc->sc_level));
  315. if( (df->df_flags & D_VARPAR) ||
  316. IsConformantArray(df->df_type) ) {
  317. /* var parameter or conformant array.
  318. For conformant array's, the address is
  319. passed.
  320. */
  321. C_adp(df->var_off);
  322. C_loi(pointer_size);
  323. ds->dsg_offset = 0;
  324. ds->dsg_kind = DSG_PLOADED;
  325. return;
  326. }
  327. }
  328. else
  329. C_lxl((arith) (proclevel - sc->sc_level));
  330. ds->dsg_kind = DSG_PLOADED;
  331. ds->dsg_offset = df->var_off;
  332. return;
  333. }
  334. /* Now, finally, we have a local variable or a local parameter
  335. */
  336. if( (df->df_flags & D_VARPAR) || IsConformantArray(df->df_type) )
  337. /* a var parameter; address directly accessible. */
  338. ds->dsg_kind = DSG_PFIXED;
  339. else
  340. ds->dsg_kind = DSG_FIXED;
  341. ds->dsg_offset = df->var_off;
  342. ds->dsg_def = df;
  343. }
  344. CodeBoundDesig(df, ds)
  345. register struct def *df;
  346. register struct desig *ds;
  347. {
  348. /* Generate code for the lower- and upperbound of a conformant array */
  349. assert(ds->dsg_kind == DSG_INIT);
  350. if( df->df_scope->sc_level < proclevel ) {
  351. C_lxa((arith) (proclevel - df->df_scope->sc_level));
  352. if( df->df_kind == D_UBOUND ) {
  353. C_ldf(df->bnd_type->arr_cfdescr);
  354. C_adi(word_size);
  355. }
  356. else
  357. C_lof(df->bnd_type->arr_cfdescr);
  358. }
  359. else {
  360. if( df->df_kind == D_UBOUND ) {
  361. C_ldl(df->bnd_type->arr_cfdescr);
  362. C_adi(word_size);
  363. }
  364. else
  365. C_lol(df->bnd_type->arr_cfdescr);
  366. }
  367. ds->dsg_kind = DSG_LOADED;
  368. }
  369. CodeFuncDesig(df, ds)
  370. register struct def *df;
  371. register struct desig *ds;
  372. {
  373. /* generate code to store the function result */
  374. if( df->df_scope->sc_level + 1 < proclevel ) {
  375. /* Assignment to function-identifier in the declaration-part of
  376. the function (i.e. in the statement-part of a nested function
  377. or procedure).
  378. */
  379. C_lxl((arith) (proclevel - df->df_scope->sc_level - 1));
  380. ds->dsg_kind = DSG_PLOADED;
  381. }
  382. else {
  383. /* Assignment to function-identifier in the statement-part of
  384. the function.
  385. */
  386. ds->dsg_kind = DSG_FIXED;
  387. }
  388. assert(df->prc_res < 0);
  389. ds->dsg_offset = df->prc_res;
  390. }
  391. CodeDesig(nd, ds)
  392. register struct node *nd;
  393. register struct desig *ds;
  394. {
  395. /* Generate code for a designator. Use divide and conquer
  396. principle
  397. */
  398. register struct def *df;
  399. switch( nd->nd_class ) { /* Divide */
  400. case Def:
  401. df = nd->nd_def;
  402. switch( df->df_kind ) {
  403. case D_FIELD:
  404. CodeFieldDesig(df, ds);
  405. break;
  406. case D_VARIABLE:
  407. CodeVarDesig(df, ds);
  408. break;
  409. case D_LBOUND:
  410. case D_UBOUND:
  411. CodeBoundDesig(df, ds);
  412. break;
  413. case D_FUNCTION:
  414. CodeFuncDesig(df, ds);
  415. break;
  416. default:
  417. crash("(CodeDesig) Def");
  418. }
  419. break;
  420. case LinkDef:
  421. assert(nd->nd_symb == '.');
  422. CodeDesig(nd->nd_left, ds);
  423. CodeFieldDesig(nd->nd_def, ds);
  424. break;
  425. case Arrsel: {
  426. struct type *tp;
  427. assert(nd->nd_symb == '[');
  428. CodeDesig(nd->nd_left, ds);
  429. CodeAddress(ds);
  430. CodePExpr(nd->nd_right);
  431. /* Now load address of descriptor
  432. */
  433. tp = nd->nd_left->nd_type;
  434. if( IsConformantArray(tp) ) {
  435. if( tp->arr_sclevel < proclevel ) {
  436. C_lxa((arith) (proclevel - tp->arr_sclevel));
  437. C_adp(tp->arr_cfdescr);
  438. }
  439. else
  440. C_lal(tp->arr_cfdescr);
  441. }
  442. else
  443. C_lae_dlb(tp->arr_ardescr, (arith) 0);
  444. ds->dsg_kind = DSG_INDEXED;
  445. ds->dsg_packed = IsPacked(tp);
  446. break;
  447. }
  448. case Arrow:
  449. assert(nd->nd_symb == '^');
  450. if( nd->nd_right->nd_type->tp_fund == T_FILE ) {
  451. CodeDAddress(nd->nd_right);
  452. C_cal("_wdw");
  453. C_asp(pointer_size);
  454. C_lfr(pointer_size);
  455. ds->dsg_kind = DSG_PLOADED;
  456. ds->dsg_packed = 1;
  457. break;
  458. }
  459. CodeDesig(nd->nd_right, ds);
  460. switch(ds->dsg_kind) {
  461. case DSG_LOADED:
  462. ds->dsg_kind = DSG_PLOADED;
  463. break;
  464. case DSG_INDEXED:
  465. case DSG_PLOADED:
  466. case DSG_PFIXED:
  467. CodeValue(ds, nd->nd_right->nd_type);
  468. ds->dsg_kind = DSG_PLOADED;
  469. ds->dsg_offset = 0;
  470. break;
  471. case DSG_FIXED:
  472. ds->dsg_kind = DSG_PFIXED;
  473. break;
  474. default:
  475. crash("(CodeDesig) Uoper");
  476. }
  477. break;
  478. default:
  479. crash("(CodeDesig) class");
  480. }
  481. }