gencode.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef NORCSID
  2. static char rcsid[] = "$Id$";
  3. #endif
  4. #include "assert.h"
  5. #include <stdio.h>
  6. #include "param.h"
  7. #include "tables.h"
  8. #include "types.h"
  9. #include <cgg_cg.h>
  10. #include "data.h"
  11. #include "result.h"
  12. #include "extern.h"
  13. #ifdef USE_TES
  14. #include "mach.h"
  15. #endif
  16. /*
  17. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  18. * See the copyright notice in the ACK home directory, in the file "Copyright".
  19. *
  20. * Author: Hans van Staveren
  21. */
  22. string mystrcpy();
  23. FILE *codefile;
  24. extern FILE *freopen();
  25. out_init(filename) char *filename; {
  26. #ifndef NDEBUG
  27. static char stderrbuff[BUFSIZ];
  28. if (Debug) {
  29. codefile = stderr;
  30. if (!isatty(2))
  31. setbuf(stderr,stderrbuff);
  32. } else {
  33. #endif
  34. if (filename == (char *) 0)
  35. codefile = stdout;
  36. else
  37. if ((codefile=freopen(filename,"w",stdout))==NULL)
  38. error("Can't create %s",filename);
  39. #ifndef NDEBUG
  40. }
  41. #endif
  42. }
  43. out_finish() {
  44. #ifndef NDEBUG
  45. if (Debug)
  46. fflush(stderr);
  47. else
  48. #endif
  49. if (codefile) fclose(codefile);
  50. #ifdef TABLEDEBUG
  51. termlset();
  52. #endif
  53. }
  54. tstoutput() {
  55. if (ferror(codefile))
  56. error("Write error on output");
  57. }
  58. genstr(stringno) {
  59. fputs(codestrings[stringno],codefile);
  60. }
  61. string ad2str(ad) addr_t ad; {
  62. static char buf[100];
  63. if (ad.ea_str==0)
  64. ad.ea_str="";
  65. if ((long)ad.ea_off==(long)0) {
  66. if(ad.ea_str[0]==0)
  67. return(mystrcpy("0")); /* don't return empty string */
  68. else
  69. return(mystrcpy(ad.ea_str));
  70. }
  71. sprintf(buf,"%s%c%ld",ad.ea_str,ad.ea_off>=0 ? '+' : ' ',(long)ad.ea_off);
  72. return(mystrcpy(buf));
  73. }
  74. praddr(ad) addr_t ad; {
  75. if (ad.ea_str==0 || *(ad.ea_str) == '\0')
  76. fprintf(codefile,WRD_FMT,ad.ea_off);
  77. else {
  78. fputs(ad.ea_str, codefile);
  79. if (ad.ea_off<0) {
  80. putc('-', codefile);
  81. fprintf(codefile,WRD_FMT,-ad.ea_off);
  82. }
  83. else if(ad.ea_off>0) {
  84. putc('+',codefile);
  85. fprintf(codefile,WRD_FMT,ad.ea_off);
  86. }
  87. }
  88. }
  89. gennl() {
  90. putc('\n',codefile);
  91. }
  92. prtoken(tp,leadingchar) token_p tp; {
  93. register c;
  94. register char *code;
  95. register tkdef_p tdp;
  96. putc(leadingchar,codefile);
  97. if (tp->t_token == -1) {
  98. fputs(codestrings[machregs[tp->t_att[0].ar].r_repr],codefile);
  99. return;
  100. }
  101. tdp = &tokens[tp->t_token];
  102. assert(tdp->t_format != -1);
  103. code = codestrings[tdp->t_format];
  104. while ((c = *code++) != 0) {
  105. if (c>=' ' && c<='~')
  106. putc(c,codefile);
  107. else {
  108. assert(c>0 && c<=TOKENSIZE);
  109. switch(tdp->t_type[c-1]) {
  110. default:
  111. assert(FALSE);
  112. case EV_INT:
  113. fprintf(codefile,WRD_FMT,tp->t_att[c-1].aw);
  114. break;
  115. case EV_ADDR:
  116. praddr(tp->t_att[c-1].aa);
  117. break;
  118. case EV_REG:
  119. fputs(codestrings[machregs[tp->t_att[c-1].ar].r_repr],codefile);
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. #ifdef USE_TES
  126. printlabel(labnum)
  127. int labnum;
  128. {
  129. newilb(dollar[labnum].e_v.e_addr.ea_str);
  130. }
  131. #endif