save.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /*
  6. * If everything is kept in core, we must save some things for the second pass.
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "arch.h"
  12. #include "out.h"
  13. #include "const.h"
  14. #include "assert.h"
  15. #include "memory.h"
  16. #include "defs.h"
  17. extern bool incore;
  18. void savemagic()
  19. {
  20. char *p;
  21. if (!incore)
  22. return;
  23. if ((p = core_alloc(ALLOMODL, (long)sizeof(int))) != (char *)0) {
  24. *(unsigned short *)p = AALMAG;
  25. core_position += sizeof(int);
  26. }
  27. }
  28. void savehdr(struct ar_hdr * hdr)
  29. {
  30. char *p;
  31. if (!incore)
  32. return;
  33. if ((p=core_alloc(ALLOMODL,(long)sizeof(struct ar_hdr)))!=(char *)0) {
  34. *(struct ar_hdr *)p = *hdr;
  35. core_position += int_align(sizeof(struct ar_hdr));
  36. }
  37. }
  38. long NLChars = 0; /* Size of string area for local names. */
  39. long NGChars = 0; /* Idem for global names. */
  40. /*
  41. * Put the string in cp into the block allocated for the string area.
  42. * Return its offset in this area. We don't use the first char of the string
  43. * area, so that empty strings can be distinguished from the first string.
  44. */
  45. ind_t savechar(int piece, ind_t off)
  46. {
  47. long len;
  48. ind_t newoff;
  49. if (off == (ind_t)0)
  50. return 0;
  51. len = strlen(modulptr(off)) + 1;
  52. if (piece == ALLOLCHR) {
  53. NLChars += len;
  54. if (!incore || (newoff = alloc(piece, len)) == BADOFF)
  55. return BADOFF;
  56. } else {
  57. assert(piece == ALLOGCHR);
  58. NGChars += len;
  59. if ((newoff = hard_alloc(piece, len)) == BADOFF)
  60. return BADOFF;
  61. }
  62. strcpy(address(piece, newoff), modulptr(off));
  63. return newoff;
  64. }
  65. /*
  66. * Put the local in `name' in the piece allocated for local names that must
  67. * be saved. `Name' points to a private copy, so will not become invalid after
  68. * allocation, but the string of which name->on_foff is the offset may be
  69. * destroyed, so we save that first.
  70. */
  71. void savelocal(struct outname *name)
  72. {
  73. ind_t savindex;
  74. struct outname *new;
  75. if ((savindex = savechar(ALLOLCHR, (ind_t)name->on_foff)) == BADOFF)
  76. return;
  77. new = (struct outname *)
  78. core_alloc(ALLOLOCL, (long)sizeof(struct outname));
  79. if (new != (struct outname *)0) {
  80. *new = *name;
  81. new->on_foff = savindex;
  82. }
  83. }