comm7.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* $Header$ */
  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. /* @(#)comm7.c 1.10 */
  7. /*
  8. * miscellaneous
  9. */
  10. #include "comm0.h"
  11. #include "comm1.h"
  12. #include "y.tab.h"
  13. valu_t
  14. load(ip)
  15. register item_t *ip;
  16. {
  17. #ifdef ASLD
  18. register short typ;
  19. typ = ip->i_type & S_TYP;
  20. if ((typ -= S_MIN) < 0) /* S_UND or S_ABS */
  21. return(ip->i_valu);
  22. return(ip->i_valu + sect[typ].s_base);
  23. #else
  24. if ((ip->i_type & S_TYP) == S_UND || (ip->i_type & S_COM)) {
  25. if (pass == PASS_3) {
  26. if (relonami != 0)
  27. serror("relocation error");
  28. relonami = ip->i_valu+1;
  29. }
  30. return(0);
  31. }
  32. return(ip->i_valu);
  33. #endif
  34. }
  35. store(ip, val)
  36. register item_t *ip;
  37. valu_t val;
  38. {
  39. #ifdef ASLD
  40. register short typ;
  41. typ = ip->i_type & S_TYP;
  42. if ((typ -= S_MIN) >= 0)
  43. val -= sect[typ].s_base;
  44. #else
  45. if ((ip->i_type & S_TYP) == S_UND)
  46. return(0);
  47. #endif
  48. assert(pass != PASS_3 || (ip->i_type & S_VAR) || ip->i_valu == val);
  49. ip->i_valu = val;
  50. return(1);
  51. }
  52. char *
  53. remember(s)
  54. register char *s;
  55. {
  56. register char *p;
  57. register n;
  58. static nleft = 0;
  59. static char *next;
  60. p = s;
  61. n = 0;
  62. do
  63. n++;
  64. while (*p++);
  65. if ((nleft -= n) < 0) {
  66. next = sbrk(MEMINCR);
  67. if ((int) next == -1)
  68. fatal("out of memory");
  69. nleft = (MEMINCR / sizeof(char)) - n;
  70. assert(nleft >= 0);
  71. }
  72. p = next;
  73. while (*p++ = *s++)
  74. ;
  75. s = next;
  76. next = p;
  77. return(s);
  78. }
  79. combine(typ1, typ2, op)
  80. register typ1, typ2;
  81. {
  82. switch (op) {
  83. case '+':
  84. if (typ1 == S_ABS)
  85. return(typ2);
  86. if (typ2 == S_ABS)
  87. return(typ1);
  88. break;
  89. case '-':
  90. if (typ2 == S_ABS)
  91. return(typ1);
  92. if ((typ1 & ~S_DOT) == (typ2 & ~S_DOT) && typ1 != S_UND)
  93. return(S_ABS|S_VAR);
  94. break;
  95. case '>':
  96. if (typ1 == S_ABS && typ2 == S_ABS)
  97. return(S_ABS);
  98. if (
  99. ((typ1 & ~S_DOT) == (typ2 & ~S_DOT) && typ1 != S_UND)
  100. || (typ1 == S_ABS)
  101. || (typ2 == S_ABS)
  102. )
  103. return(S_ABS|S_VAR);
  104. break;
  105. default:
  106. if (typ1 == S_ABS && typ2 == S_ABS)
  107. return(S_ABS);
  108. break;
  109. }
  110. if (pass != PASS_1)
  111. serror("illegal operator");
  112. return(S_UND);
  113. }
  114. #ifdef LISTING
  115. printx(ndig, val)
  116. valu_t val;
  117. {
  118. static char buf[8];
  119. register char *p;
  120. register c, n;
  121. p = buf; n = ndig;
  122. do {
  123. *p++ = (int) val & 017;
  124. val >>= 4;
  125. } while (--n);
  126. do {
  127. c = "0123456789ABCDEF"[*--p];
  128. putchar(c);
  129. } while (p > buf);
  130. return(ndig);
  131. }
  132. #endif
  133. #ifdef LISTING
  134. listline(textline)
  135. {
  136. register c;
  137. if ((listflag & 4) && (c = getc(listfile)) != '\n' && textline) {
  138. if (listcolm >= 24)
  139. printf(" \\\n\t\t\t");
  140. else
  141. do {
  142. putchar('\t');
  143. listcolm += 8;
  144. } while (listcolm < 24);
  145. do {
  146. assert(c != EOF);
  147. putchar(c);
  148. } while ((c = getc(listfile)) != '\n');
  149. }
  150. if (listflag & 7)
  151. putchar('\n');
  152. listeoln = 1;
  153. listcolm = 0;
  154. listflag = listtemp;
  155. }
  156. #endif LISTING
  157. /* ---------- code optimization ---------- */
  158. #ifdef THREE_PASS
  159. small(fitsmall, gain)
  160. {
  161. register bit;
  162. register char *p;
  163. if (DOTSCT == NULL)
  164. nosect();
  165. if (bflag)
  166. return(0);
  167. if ((bit = nbits++) >= BITMAX) {
  168. if (bit != BITMAX)
  169. nbits--; /* prevent wraparound */
  170. else if (pass == PASS_1)
  171. warning("bit table overflow");
  172. return(0);
  173. }
  174. p = &bittab[bit>>3];
  175. bit = 1 << (bit&7);
  176. switch (pass) {
  177. case PASS_1:
  178. return(0);
  179. case PASS_2:
  180. if (fitsmall) {
  181. DOTGAIN += gain;
  182. *p |= bit;
  183. }
  184. return(fitsmall);
  185. case PASS_3:
  186. assert(fitsmall || (*p & bit) == 0);
  187. return(*p & bit);
  188. }
  189. /*NOTREACHED*/
  190. }
  191. #endif
  192. /* ---------- output ---------- */
  193. emit1(arg)
  194. char arg;
  195. {
  196. #ifdef LISTING
  197. if (listeoln) {
  198. if (listflag & 1) {
  199. listcolm += printx(VALWIDTH, DOTVAL);
  200. listcolm++;
  201. putchar(' ');
  202. }
  203. listeoln = 0;
  204. }
  205. if (listflag & 2)
  206. listcolm += printx(2, (valu_t) arg);
  207. #endif
  208. switch (pass) {
  209. case PASS_1:
  210. if (DOTSCT == NULL)
  211. nosect();
  212. /* no break */
  213. case PASS_2:
  214. DOTSCT->s_zero = 0;
  215. break;
  216. case PASS_3:
  217. wr_outsect(DOTTYP-S_MIN);
  218. while (DOTSCT->s_zero) {
  219. wr_putc(0);
  220. DOTSCT->s_zero--;
  221. }
  222. wr_putc(arg);
  223. break;
  224. }
  225. DOTVAL++;
  226. }
  227. emit2(arg)
  228. short arg;
  229. {
  230. #ifdef BYTES_REVERSED
  231. emit1((char)(arg>>8)); emit1((char)arg);
  232. #else
  233. emit1((char)arg); emit1((char)(arg>>8));
  234. #endif
  235. }
  236. emit4(arg)
  237. long arg;
  238. {
  239. #ifdef WORDS_REVERSED
  240. emit2((short)(arg>>16)); emit2((short)(arg));
  241. #else
  242. emit2((short)(arg)); emit2((short)(arg>>16));
  243. #endif
  244. }
  245. emitx(val, n)
  246. valu_t val;
  247. int n;
  248. {
  249. switch (n) {
  250. case 1:
  251. emit1((char)val); break;
  252. case 2:
  253. emit2((short)val); break;
  254. case 4:
  255. emit4((long)val); break;
  256. default:
  257. assert(0);
  258. }
  259. }
  260. emitstr(zero)
  261. {
  262. register i;
  263. register char *p;
  264. p = stringbuf;
  265. i = *p++ & 0377;
  266. while (--i >= 0)
  267. emit1(*p++);
  268. if (zero)
  269. emit1(0);
  270. }
  271. /* ---------- Error checked file I/O ---------- */
  272. ffreopen(s, f)
  273. char *s;
  274. FILE *f;
  275. {
  276. if (freopen(s, "r", f) == NULL)
  277. fatal("can't reopen %s", s);
  278. }
  279. FILE *
  280. ffcreat(s)
  281. char *s;
  282. {
  283. FILE *f;
  284. if ((f = fopen(s, "w")) == NULL)
  285. fatal("can't create %s", s);
  286. return(f);
  287. }
  288. FILE *
  289. fftemp(path, tail)
  290. char *path, *tail;
  291. {
  292. register char *dir;
  293. if ((dir = getenv("TMPDIR")) == NULL)
  294. #ifdef TMPDIR
  295. dir = TMPDIR;
  296. #else
  297. dir = "/tmp";
  298. #endif
  299. sprintf(path, "%s/%s", dir, tail);
  300. return(ffcreat(mktemp(path)));
  301. }
  302. /* ---------- Error handling ---------- */
  303. yyerror(){} /* we will do our own error printing */
  304. nosect()
  305. {
  306. fatal("no sections");
  307. }
  308. wr_fatal()
  309. {
  310. fatal("write error");
  311. }
  312. /* VARARGS1 */
  313. fatal(s, a1, a2, a3, a4)
  314. char *s;
  315. {
  316. nerrors++;
  317. diag(" (fatal)\n", s, a1, a2, a3, a4);
  318. stop();
  319. }
  320. #if DEBUG == 2
  321. assert2(file, line)
  322. char *file;
  323. {
  324. fatal("assertion failed (%s, %d)", file, line);
  325. }
  326. #endif
  327. #if DEBUG == 1
  328. assert1()
  329. {
  330. diag(" (fatal)\n", "assertion failed");
  331. abort();
  332. }
  333. #endif
  334. /* VARARGS1 */
  335. serror(s, a1, a2, a3, a4)
  336. char *s;
  337. {
  338. nerrors++;
  339. diag("\n", s, a1, a2, a3, a4);
  340. }
  341. /* VARARGS1 */
  342. warning(s, a1, a2, a3, a4)
  343. char *s;
  344. {
  345. diag(" (warning)\n", s, a1, a2, a3, a4);
  346. }
  347. /* VARARGS1 */
  348. diag(tail, s, a1, a2, a3, a4)
  349. char *tail, *s;
  350. {
  351. fflush(stdout);
  352. if (modulename)
  353. fprintf(stderr, "\"%s\", line %d: ", modulename, lineno);
  354. else
  355. fprintf(stderr, "%s: ", progname);
  356. fprintf(stderr, s, a1, a2, a3, a4);
  357. fprintf(stderr, tail);
  358. }
  359. nofit()
  360. {
  361. if (pass == PASS_3)
  362. warning("too big");
  363. }