macro.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* macro.c - expand macros for assembler */
  2. #include "syshead.h"
  3. #include "const.h"
  4. #include "type.h"
  5. #include "globvar.h"
  6. #include "scan.h"
  7. #undef EXTERN
  8. #define EXTERN
  9. #include "macro.h"
  10. /*
  11. Enter macro: stack macro and get its parameters.
  12. Parameters form a linked list of null-terminated strings of form
  13. next:string. The first string is the macro number in 4 bytes.
  14. */
  15. PUBLIC void entermac(symptr)
  16. struct sym_s *symptr;
  17. {
  18. if (maclevel >= MAXMAC)
  19. error(MACOV);
  20. else if (macpar + 2 > macptop)
  21. error(PAROV); /* no room for 0th param */
  22. /* (2 structs to fit it!) */
  23. else
  24. {
  25. char ch;
  26. struct schain_s *param1;
  27. register char *reglineptr;
  28. register char *stringptr;
  29. ++maclevel;
  30. (--macstak)->text = (char *) symptr->value_reg_or_op.value;
  31. macstak->parameters = param1 = macpar;
  32. param1->next = NUL_PTR;
  33. *(stringptr = build_number(++macnum, 3, param1->string)) = 0;
  34. macpar = (struct schain_s *) (stringptr + 1);
  35. /* TODO: alignment */
  36. getsym();
  37. if (sym == EOLSYM)
  38. return; /* no other params */
  39. if (sym != LPAREN)
  40. reglineptr = symname;
  41. else
  42. reglineptr = lineptr;
  43. stringptr = macpar->string;
  44. while (TRUE)
  45. {
  46. if (stringptr >= (char *) macptop)
  47. {
  48. symname = reglineptr;
  49. error(PAROV);
  50. return;
  51. }
  52. ch = *reglineptr++;
  53. if (ch == '\\')
  54. /* escaped means no special meaning for slash, comma, paren */
  55. ch = *reglineptr++;
  56. else if (ch == ',' || ch == ')' || ch == '!' || ch == ';'
  57. || ch == '\n' || ch == 0)
  58. {
  59. if (stringptr >= (char *) macptop)
  60. {
  61. symname = reglineptr;
  62. error(PAROV); /* no room for null */
  63. return;
  64. }
  65. *stringptr = 0;
  66. param1->next = macpar; /* ptr from previous */
  67. (param1 = macpar)->next = NUL_PTR;
  68. /* this goes nowhere */
  69. macpar = (struct schain_s *) (stringptr + 1);
  70. /* but is finished OK - TODO align */
  71. stringptr = macpar->string;
  72. if (ch != ',')
  73. return;
  74. continue;
  75. }
  76. if ((*stringptr++ = ch) == 0)
  77. {
  78. symname = reglineptr;
  79. error(RPEXP);
  80. return;
  81. }
  82. }
  83. }
  84. }
  85. /* MACRO pseudo-op */
  86. PUBLIC void pmacro()
  87. {
  88. bool_t saving;
  89. bool_t savingc;
  90. struct sym_s *symptr=0;
  91. int maclen = 8;
  92. int macoff = 0;
  93. char * macbuf = asalloc(8);
  94. saving = /* prepare for bad macro */
  95. savingc = FALSE; /* normally don't save comments */
  96. macload = TRUE; /* show loading */
  97. if (label != NUL_PTR)
  98. error(ILLAB);
  99. else if (sym != IDENT)
  100. error(LABEXP);
  101. else
  102. {
  103. symptr = gsymptr;
  104. if (symptr->type & MNREGBIT)
  105. error(LABEXP);
  106. else if (symptr->type & LABIT || symptr->data & FORBIT)
  107. error(RELAB);
  108. else if (pass != last_pass || symptr->type & REDBIT)
  109. /* copy on pass 0, also pass 1 if redefined */
  110. {
  111. saving = TRUE;
  112. if (symptr->type & MACBIT)
  113. symptr->type |= REDBIT;
  114. else
  115. symptr->type |= MACBIT;
  116. symptr->data = UNDBIT; /* undefined till end */
  117. symptr->value_reg_or_op.value = (offset_t) macbuf;
  118. getsym_nolookup(); /* test for "C" */
  119. if (sym == IDENT && lineptr == symname + 1 && *symname == 'C')
  120. savingc = TRUE;
  121. }
  122. }
  123. while (TRUE)
  124. {
  125. skipline();
  126. listline();
  127. readline();
  128. if (!macload)
  129. break; /* macload cleared to show eof */
  130. getsym_nolookup();
  131. if (sym == IDENT)
  132. {
  133. if (lineptr == symname + 4 &&
  134. ( strncmp(symname, "MEND", 4) == 0 || strncmp(symname, "mend", 4) == 0) )
  135. {
  136. getsym();
  137. break;
  138. }
  139. }
  140. else if (sym != MACROARG)
  141. {
  142. if (!savingc)
  143. continue; /* don't save comment */
  144. }
  145. if (!saving)
  146. continue;
  147. {
  148. char * p = strchr(linebuf, EOLCHAR);
  149. int len = (p-linebuf+1);
  150. if ( macoff + len > maclen-4 )
  151. {
  152. maclen = maclen * 2 + len;
  153. macbuf = asrealloc(macbuf, maclen);
  154. }
  155. memcpy(macbuf+macoff, linebuf, len);
  156. macoff += len;
  157. }
  158. }
  159. macload = FALSE;
  160. if (saving)
  161. {
  162. macbuf[macoff] = ETB;
  163. symptr->value_reg_or_op.value = (offset_t) macbuf;
  164. symptr->data = 0;
  165. }
  166. }