main.c 2.0 KB

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