gencode.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * Author: Hans van Staveren
  6. */
  7. #include "assert.h"
  8. #include <unistd.h>
  9. #include <stdio.h>
  10. #include "param.h"
  11. #include "tables.h"
  12. #include "types.h"
  13. #include <cgg_cg.h>
  14. #include "data.h"
  15. #include "result.h"
  16. #include "extern.h"
  17. #include "compute.h"
  18. #include "utils.h"
  19. #include "gencode.h"
  20. #ifdef USE_TES
  21. #include "mach.h"
  22. #endif
  23. FILE *codefile;
  24. void out_init(char *filename)
  25. {
  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. void out_finish()
  44. {
  45. #ifndef NDEBUG
  46. if (Debug)
  47. fflush(stderr);
  48. else
  49. #endif
  50. if (codefile) fclose(codefile);
  51. #ifdef TABLEDEBUG
  52. termlset();
  53. #endif
  54. }
  55. void tstoutput()
  56. {
  57. if (ferror(codefile))
  58. error("Write error on output");
  59. }
  60. void genstr(int stringno)
  61. {
  62. fputs(codestrings[stringno],codefile);
  63. }
  64. string ad2str(addr_t ad)
  65. {
  66. static char buf[100];
  67. if (ad.ea_str==0)
  68. ad.ea_str="";
  69. if ((long)ad.ea_off==(long)0) {
  70. if(ad.ea_str[0]==0)
  71. return(mystrcpy("0")); /* don't return empty string */
  72. else
  73. return(mystrcpy(ad.ea_str));
  74. }
  75. sprintf(buf,"%s%c%ld",ad.ea_str,ad.ea_off>=0 ? '+' : ' ',(long)ad.ea_off);
  76. return(mystrcpy(buf));
  77. }
  78. void praddr(addr_t ad)
  79. {
  80. if (ad.ea_str==0 || *(ad.ea_str) == '\0')
  81. fprintf(codefile,WRD_FMT,ad.ea_off);
  82. else {
  83. fputs(ad.ea_str, codefile);
  84. if (ad.ea_off<0) {
  85. putc('-', codefile);
  86. fprintf(codefile,WRD_FMT,-ad.ea_off);
  87. }
  88. else if(ad.ea_off>0) {
  89. putc('+',codefile);
  90. fprintf(codefile,WRD_FMT,ad.ea_off);
  91. }
  92. }
  93. }
  94. void gennl()
  95. {
  96. putc('\n',codefile);
  97. }
  98. void prtoken(token_p tp, int leadingchar)
  99. {
  100. int c;
  101. char *code;
  102. tkdef_p tdp;
  103. putc(leadingchar,codefile);
  104. if (tp->t_token == -1) {
  105. fputs(codestrings[machregs[tp->t_att[0].ar].r_repr],codefile);
  106. return;
  107. }
  108. tdp = &tokens[tp->t_token];
  109. assert(tdp->t_format != -1);
  110. code = codestrings[tdp->t_format];
  111. while ((c = *code++) != 0) {
  112. if (c>=' ' && c<='~')
  113. putc(c,codefile);
  114. else {
  115. assert(c>0 && c<=TOKENSIZE);
  116. switch(tdp->t_type[c-1]) {
  117. default:
  118. assert(FALSE);
  119. case EV_INT:
  120. fprintf(codefile,WRD_FMT,tp->t_att[c-1].aw);
  121. break;
  122. case EV_ADDR:
  123. praddr(tp->t_att[c-1].aa);
  124. break;
  125. case EV_REG:
  126. fputs(codestrings[machregs[tp->t_att[c-1].ar].r_repr],codefile);
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. #ifdef USE_TES
  133. void printlabel(int labnum)
  134. {
  135. newilb(dollar[labnum].e_v.e_addr.ea_str);
  136. }
  137. #endif