main.c 2.1 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. /* This is the main program for the stand-alone version of the
  6. peephole optimizer.
  7. */
  8. #include <stdarg.h>
  9. #include "nopt.h"
  10. char *filename; /* Name of input file */
  11. int errors; /* Number of errors */
  12. int main(int argc, char *argv[])
  13. {
  14. static struct e_instr buff;
  15. p_instr p = &buff;
  16. if (argc >= 2) {
  17. filename = argv[1];
  18. }
  19. else filename = 0;
  20. if (!EM_open(filename)) {
  21. fatal(EM_error);
  22. }
  23. EM_getinstr(p);
  24. O_init((arith) EM_wordsize, (arith) EM_pointersize);
  25. if (argc >= 3) {
  26. if (!O_open(argv[2])) {
  27. fatal("O_open failed");
  28. }
  29. }
  30. else if (!O_open( (char *) 0)) fatal("O_open failed");
  31. O_magic();
  32. C_out(p);
  33. for(;;) {
  34. EM_getinstr(p=GETNXTPATT());
  35. switch(p->em_type) {
  36. case EM_DEFILB:
  37. p->em_opcode=op_lab;
  38. break;
  39. case EM_MNEM:
  40. switch(p->em_argtype) {
  41. case sof_ptyp:
  42. p->em_dnam = OO_freestr(p->em_dnam);
  43. break;
  44. case pro_ptyp:
  45. p->em_pnam = OO_freestr(p->em_pnam);
  46. break;
  47. case str_ptyp:
  48. case ico_ptyp:
  49. case uco_ptyp:
  50. case fco_ptyp:
  51. p->em_string = OO_freestr(p->em_string);
  52. break;
  53. }
  54. break;
  55. case EM_PSEU:
  56. switch(p->em_opcode) {
  57. case ps_pro:
  58. case ps_end:
  59. break;
  60. default:
  61. C_out(p);
  62. OO_nxtpatt--;
  63. continue;
  64. }
  65. break;
  66. default:
  67. C_out(p);
  68. OO_nxtpatt--;
  69. continue;
  70. case EM_EOF:
  71. goto got_eof;
  72. case EM_ERROR:
  73. error("%s", EM_error);
  74. continue;
  75. case EM_FATAL:
  76. fatal("%s", EM_error);
  77. }
  78. OO_dfa(p->em_opcode);
  79. }
  80. got_eof:
  81. O_close();
  82. EM_close();
  83. exit(errors);
  84. }
  85. /*VARARGS1*/
  86. static int verror(char *s, va_list ap)
  87. {
  88. fprintf(stderr,
  89. "%s, line %d: ",
  90. filename ? filename : "standard input",
  91. EM_lineno);
  92. vfprintf(stderr, s, ap);
  93. fprintf(stderr, "\n");
  94. errors++;
  95. return 0;
  96. }
  97. int error(char *s, ...)
  98. {
  99. va_list ap;
  100. va_start(ap, s);
  101. verror(s, ap);
  102. va_end(ap);
  103. return 0;
  104. }
  105. /*VARARGS1*/
  106. int fatal(char *s, ...)
  107. {
  108. va_list ap;
  109. va_start(ap, s);
  110. verror(s, ap);
  111. va_end(ap);
  112. exit(1);
  113. return 0;
  114. }