comm7.c 6.9 KB

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