comm6.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* @(#)comm6.c 1.7 */
  7. /*
  8. * implement pseudo instructions
  9. */
  10. #include "comm0.h"
  11. #include "comm1.h"
  12. #include "y.tab.h"
  13. void newequate(item_t *ip, int typ)
  14. {
  15. typ &= ~S_EXT;
  16. if (typ & S_COM)
  17. typ = S_UND;
  18. else if ((typ & S_VAR) && (typ & S_TYP) != S_ABS)
  19. typ = S_UND;
  20. #ifdef THREE_PASS
  21. else if (pass == PASS_1 && typ == S_UND)
  22. typ = S_VAR;
  23. else if (pass == PASS_2 && (ip->i_type & S_TYP) == S_UND)
  24. ip->i_type |= typ;
  25. #endif /* THREE_PASS */
  26. if (typ == S_UND)
  27. serror("illegal equate");
  28. if (pass == PASS_3)
  29. assert((ip->i_type & S_TYP) == (typ & S_TYP));
  30. newident(ip, typ);
  31. }
  32. void newident(item_t *ip, int typ)
  33. {
  34. int flag;
  35. #ifdef GENLAB
  36. static char genlab[] = GENLAB;
  37. #endif /* GENLAB */
  38. if (pass == PASS_1) {
  39. /* printf("declare %s: %o\n", ip->i_name, typ); */
  40. if (ip->i_type & ~S_EXT)
  41. serror("multiple declared");
  42. else
  43. --unresolved;
  44. ip->i_type |= typ;
  45. }
  46. if (PASS_SYMB == 0)
  47. return;
  48. #ifdef THREE_PASS
  49. if (ip->i_type & S_EXT)
  50. flag = SYM_EXT;
  51. else
  52. flag = SYM_LOC;
  53. #else
  54. flag = SYM_EXT|SYM_LOC; /* S_EXT not stable in PASS_1 */
  55. #endif /* THREE_PASS */
  56. #ifdef GENLAB
  57. if (!(flag & SYM_EXT) &&
  58. strncmp(ip->i_name, genlab, sizeof(genlab)-1) == 0)
  59. flag = SYM_LAB;
  60. #endif /* GENLAB */
  61. if (sflag & flag)
  62. newsymb(
  63. ip->i_name,
  64. ip->i_type & (S_EXT|S_TYP),
  65. 0,
  66. load(ip)
  67. );
  68. }
  69. void newlabel(item_t *ip)
  70. {
  71. #if DEBUG != 0
  72. #ifdef THREE_PASS
  73. ADDR_T oldval = ip->i_valu;
  74. #endif
  75. #endif
  76. if (DOTSCT == NULL)
  77. nosect();
  78. ip->i_type &= ~S_TYP;
  79. ip->i_type |= DOTTYP;
  80. if (store(ip, (valu_t) DOTVAL) == 0)
  81. return;
  82. #ifdef THREE_PASS
  83. assert(pass != PASS_2 || oldval - (ADDR_T) ip->i_valu == DOTGAIN);
  84. #endif
  85. }
  86. void newsect(item_t *ip)
  87. {
  88. int typ;
  89. sect_t *sp = NULL;
  90. typ = ip->i_type & S_TYP;
  91. if (typ == S_UND) {
  92. /*
  93. * new section
  94. */
  95. assert(pass == PASS_1);
  96. --unresolved;
  97. typ = outhead.oh_nsect + S_MIN;
  98. outhead.oh_nsect++;
  99. if (outhead.oh_nsect > SECTMAX || typ > S_MAX)
  100. fatal("too many sections");
  101. sp = &sect[typ - S_MIN];
  102. sp->s_item = ip;
  103. sp->s_lign = ALIGNSECT;
  104. #ifndef ASLD
  105. ip->i_type = typ;
  106. #else
  107. ip->i_type = typ | S_EXT;
  108. #endif
  109. ip->i_valu = 0;
  110. } else if (typ >= S_MIN) {
  111. sp = &sect[typ - S_MIN];
  112. if (sp->s_item != ip)
  113. sp = NULL;
  114. }
  115. if (sp == NULL)
  116. serror("multiple declared");
  117. else
  118. switchsect(typ);
  119. }
  120. /*ARGSUSED*/
  121. void newbase(valu_t base)
  122. {
  123. #ifdef ASLD
  124. sect_t *sp;
  125. if ((sp = DOTSCT) == NULL)
  126. nosect();
  127. if (sp->s_flag & BASED)
  128. serror("already based");
  129. sp->s_base = base;
  130. sp->s_flag |= BASED;
  131. DOTVAL += base;
  132. #else
  133. warning(".base ignored");
  134. #endif
  135. }
  136. /*
  137. * NOTE: A rather different solution is used for ASLD and not ASLD:
  138. * ASLD, or local commons:
  139. * - maximum length of .comm is recorded in i_valu during PASS_1
  140. * - address of .comm is recorded in i_valu in later passes:
  141. * assigned at end of PASS_1, corrected for s_gain at end of PASS_2
  142. * not ASLD:
  143. * - maximum length of .comm is recorded in i_valu during PASS_1
  144. * - i_valu is used for relocation info during PASS_3
  145. */
  146. void newcomm(item_t *ip, valu_t val)
  147. {
  148. if (pass == PASS_1) {
  149. if (DOTSCT == NULL)
  150. nosect();
  151. if (val == 0)
  152. serror("bad size");
  153. /* printf("declare %s: %o\n", ip->i_name, DOTTYP); */
  154. if ((ip->i_type & ~S_EXT) == S_UND) {
  155. --unresolved;
  156. ip->i_type = S_COM|DOTTYP|(ip->i_type&S_EXT);
  157. ip->i_valu = val;
  158. new_common(ip);
  159. } else if (ip->i_type == (S_COM|DOTTYP|(ip->i_type&S_EXT))) {
  160. if (ip->i_valu < val)
  161. ip->i_valu = val;
  162. } else
  163. serror("multiple declared");
  164. }
  165. }
  166. void switchsect(int newtyp)
  167. {
  168. sect_t *sp;
  169. if ( (sp = DOTSCT) )
  170. sp->s_size = DOTVAL - sp->s_base;
  171. if (newtyp == S_UND) {
  172. DOTSCT = NULL;
  173. DOTTYP = newtyp;
  174. return;
  175. }
  176. assert(newtyp >= S_MIN);
  177. sp = &sect[newtyp - S_MIN];
  178. DOTVAL = sp->s_size + sp->s_base;
  179. DOTSCT = sp;
  180. DOTTYP = newtyp;
  181. }
  182. void align(valu_t bytes)
  183. {
  184. valu_t gap;
  185. sect_t *sp;
  186. if ((sp = DOTSCT) == NULL)
  187. nosect();
  188. if (bytes == 0)
  189. bytes = ALIGNWORD;
  190. if (sp->s_lign % bytes)
  191. {
  192. if (bytes % sp->s_lign)
  193. {
  194. serror("illegal alignment");
  195. }
  196. else
  197. {
  198. sp->s_lign = bytes;
  199. }
  200. }
  201. if (pass == PASS_1)
  202. {
  203. /*
  204. * be pessimistic: biggest gap possible
  205. */
  206. gap = bytes - 1;
  207. }
  208. else
  209. {
  210. /*
  211. * calculate gap correctly;
  212. * will be the same in PASS_2 and PASS_3
  213. */
  214. if ((gap = DOTVAL % bytes) != 0)
  215. gap = bytes - gap;
  216. #ifdef THREE_PASS
  217. if (pass == PASS_2)
  218. /*
  219. * keep track of gain with respect to PASS_1
  220. */
  221. DOTGAIN += (bytes - 1) - gap;
  222. #endif
  223. }
  224. DOTVAL += gap;
  225. sp->s_zero += gap;
  226. }
  227. #ifdef RELOCATION
  228. void newrelo(int s, int n)
  229. {
  230. int iscomm;
  231. struct outrelo outrelo;
  232. if (rflag == 0)
  233. return;
  234. if (PASS_RELO == 0)
  235. return;
  236. s &= ~S_DOT;
  237. assert((s & ~(S_COM|S_VAR|S_TYP)) == 0);
  238. #ifdef ASLD
  239. #ifndef THREE_PASS
  240. if (s == S_UND)
  241. serror("bad relocation");
  242. #endif
  243. #endif
  244. /*
  245. * always relocation info if S_VAR to solve problems with:
  246. * move b,d0
  247. * b=a
  248. * a: .data2 0
  249. * but no relocation info if S_VAR is set, but type is S_ABS.
  250. */
  251. iscomm = s & S_COM;
  252. s &= ~S_COM;
  253. if ((n & RELPC) == 0 && ((s & ~S_VAR) == S_ABS))
  254. return;
  255. if ((n & RELPC) != 0 && s == DOTTYP
  256. #ifndef ASLD
  257. && ! iscomm
  258. #endif
  259. )
  260. return;
  261. if (pass != PASS_3) {
  262. outhead.oh_nrelo++;
  263. return;
  264. }
  265. s &= ~S_VAR;
  266. outrelo.or_type = (char)n;
  267. outrelo.or_sect = (char)DOTTYP;
  268. #ifndef ASLD
  269. if (s == S_UND || iscomm) {
  270. assert(relonami != 0);
  271. outrelo.or_nami = relonami-1;
  272. relonami = 0;
  273. } else
  274. #endif
  275. if (s < S_MIN)
  276. {
  277. assert(s == S_ABS);
  278. /*
  279. * use first non existing entry (argh)
  280. */
  281. outrelo.or_nami = outhead.oh_nname;
  282. }
  283. else
  284. {
  285. /*
  286. * section symbols are at the end
  287. */
  288. outrelo.or_nami = outhead.oh_nname
  289. - outhead.oh_nsect
  290. + (s - S_MIN)
  291. ;
  292. }
  293. outrelo.or_addr = (long)DOTVAL;
  294. wr_relo(&outrelo, 1);
  295. }
  296. #endif
  297. long new_string(char *s)
  298. {
  299. long r = 0;
  300. if (s) {
  301. long len = strlen(s) + 1;
  302. r = outhead.oh_nchar;
  303. if (pass == PASS_3) wr_string(s, len);
  304. outhead.oh_nchar += len;
  305. }
  306. return r;
  307. }
  308. void newsymb(char *name, int type, int desc, valu_t valu)
  309. {
  310. struct outname outname;
  311. if (name && *name == 0)
  312. name = 0;
  313. assert(PASS_SYMB);
  314. if (pass != PASS_3) {
  315. new_string(name);
  316. outhead.oh_nname++;
  317. return;
  318. }
  319. nname++;
  320. outname.on_foff = new_string(name);
  321. outname.on_type = type;
  322. outname.on_desc = desc;
  323. outname.on_valu = valu;
  324. if (sizeof(valu) != sizeof(long))
  325. outname.on_valu &= ~(((0xFFFFFFFF)<<(4*sizeof(valu_t)))<<(4*sizeof(valu_t)));
  326. wr_name(&outname, 1);
  327. }
  328. void new_common(item_t *ip)
  329. {
  330. struct common_t *cp;
  331. static nleft = 0;
  332. static struct common_t *next;
  333. if (--nleft < 0) {
  334. next = (struct common_t *) malloc(MEMINCR);
  335. if (next == 0) {
  336. fatal("out of memory");
  337. }
  338. nleft += (MEMINCR / sizeof (struct common_t));
  339. }
  340. cp = next++;
  341. cp->c_next = commons;
  342. cp->c_it = ip;
  343. commons = cp;
  344. }