write.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. extern char *outputname;
  29. register struct outhead *hd = &outhead;
  30. assert(! incore);
  31. if (! wr_open(outputname)) {
  32. fatal("cannot write %s", outputname);
  33. }
  34. wr_ohead(hd);
  35. wr_sect(outsect, hd->oh_nsect);
  36. off_char = OFF_CHAR(*hd);
  37. }
  38. static struct outname *
  39. sectname(sectindex)
  40. int sectindex;
  41. {
  42. static struct outname namebuf;
  43. namebuf.on_foff = (long)0; /* No string name. */
  44. namebuf.on_type = (S_MIN + sectindex) | S_SCT;
  45. namebuf.on_desc = 0;
  46. namebuf.on_valu = outsect[sectindex].os_base;
  47. return &namebuf;
  48. }
  49. /*
  50. * Write out the symbol table and the section names.
  51. */
  52. end_write()
  53. {
  54. register ushort cnt;
  55. register struct outname *name;
  56. register int sectindex;
  57. extern ushort NGlobals;
  58. extern long NGChars;
  59. assert(!incore);
  60. assert(!(flagword & SFLAG));
  61. cnt = NGlobals;
  62. name = (struct outname *)address(ALLOGLOB, (ind_t)0);
  63. namecpy(name, NGlobals, off_char);
  64. wr_name(name, NGlobals);
  65. wr_string(mems[ALLOGCHR].mem_base+1, NGChars);
  66. off_char += NGChars;
  67. for (sectindex = 0; sectindex < outhead.oh_nsect; sectindex++)
  68. wrt_name(sectname(sectindex), 1);
  69. }
  70. wrt_emit(emit, sectindex, cnt)
  71. char *emit;
  72. int sectindex;
  73. long cnt;
  74. {
  75. wr_outsect(sectindex);
  76. wr_emit(emit, cnt);
  77. }
  78. wrt_nulls(sectindex, cnt)
  79. register long cnt;
  80. {
  81. static char nullbuf[BUFSIZ];
  82. wr_outsect(sectindex);
  83. while (cnt) {
  84. register int n = cnt >= BUFSIZ ? BUFSIZ : cnt;
  85. wr_emit(nullbuf, (long)n);
  86. cnt -= n;
  87. }
  88. }
  89. wrt_name(name, writename)
  90. register struct outname *name;
  91. {
  92. assert(!incore);
  93. assert(!(flagword & SFLAG));
  94. if (name->on_mptr != (char *)0) {
  95. register long len = strlen(name->on_mptr) + 1;
  96. wr_string(name->on_mptr, len);
  97. name->on_foff = off_char;
  98. off_char += len;
  99. } else {
  100. name->on_foff = (long)0;
  101. }
  102. if (writename) wr_name(name, 1);
  103. }