write.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. #ifndef lint
  6. static char rcsid[] = "$Header$";
  7. #endif
  8. #include <out.h>
  9. #include <stdio.h>
  10. #include "const.h"
  11. #include "assert.h"
  12. #include "memory.h"
  13. extern struct outhead outhead;
  14. extern struct outsect outsect[];
  15. extern int flagword;
  16. extern bool incore;
  17. wr_fatal()
  18. {
  19. fatal("write error");
  20. }
  21. static long off_char;
  22. /*
  23. * Open the output file according to the chosen strategy.
  24. * Write away the header and section table: they will not change anymore.
  25. */
  26. begin_write()
  27. {
  28. register struct outhead *hd = &outhead;
  29. assert(! incore);
  30. wr_ohead(hd);
  31. wr_sect(outsect, hd->oh_nsect);
  32. off_char = OFF_CHAR(*hd);
  33. }
  34. static struct outname *
  35. sectname(sectindex)
  36. int sectindex;
  37. {
  38. static struct outname namebuf;
  39. namebuf.on_foff = (long)0; /* No string name. */
  40. namebuf.on_type = (S_MIN + sectindex) | S_SCT;
  41. namebuf.on_desc = 0;
  42. namebuf.on_valu = outsect[sectindex].os_base;
  43. return &namebuf;
  44. }
  45. /*
  46. * Write out the symbol table and the section names.
  47. */
  48. end_write()
  49. {
  50. register struct outname *name;
  51. register int sectindex;
  52. extern unsigned short NGlobals;
  53. extern long NGChars;
  54. assert(!incore);
  55. assert(!(flagword & SFLAG));
  56. name = (struct outname *)address(ALLOGLOB, (ind_t)0);
  57. namecpy(name, NGlobals, off_char);
  58. wr_name(name, NGlobals);
  59. wr_string(mems[ALLOGCHR].mem_base+1, NGChars);
  60. off_char += NGChars;
  61. for (sectindex = 0; sectindex < outhead.oh_nsect; sectindex++)
  62. wrt_name(sectname(sectindex), 1);
  63. }
  64. wrt_emit(emit, sectindex, cnt)
  65. char *emit;
  66. int sectindex;
  67. long cnt;
  68. {
  69. wr_outsect(sectindex);
  70. wr_emit(emit, cnt);
  71. }
  72. wrt_nulls(sectindex, cnt)
  73. register long cnt;
  74. {
  75. static char nullbuf[BUFSIZ];
  76. wr_outsect(sectindex);
  77. while (cnt) {
  78. register int n = cnt >= BUFSIZ ? BUFSIZ : cnt;
  79. wr_emit(nullbuf, (long)n);
  80. cnt -= n;
  81. }
  82. }
  83. wrt_name(name, writename)
  84. register struct outname *name;
  85. {
  86. assert(!incore);
  87. assert(!(flagword & SFLAG));
  88. if (name->on_mptr != (char *)0) {
  89. register long len = strlen(name->on_mptr) + 1;
  90. wr_string(name->on_mptr, len);
  91. name->on_foff = off_char;
  92. off_char += len;
  93. } else {
  94. name->on_foff = (long)0;
  95. }
  96. if (writename) wr_name(name, 1);
  97. }