convert.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 rcsid[] = "$Id$";
  7. #endif
  8. #if __STDC__
  9. #include <stdarg.h>
  10. #endif
  11. /* This program converts either human-readable or compact EM
  12. assembly code to calls of the procedure-interface.
  13. It must be linked with two libraries:
  14. - a library to read EM code, according to read_em(3)
  15. - a library implementing the em_code(3) interface.
  16. Thus, this program could serve as an EM_encoder, an
  17. EM_decoder, or some code generator, depending on how it is
  18. linked.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "system.h"
  23. #include "em_pseu.h"
  24. #include "em_mnem.h"
  25. #include "em_spec.h"
  26. #include "em_flag.h"
  27. #include "em_ptyp.h"
  28. #include "em.h"
  29. #include "em_comp.h"
  30. #if __STDC__
  31. void error(char *fmt, ...);
  32. void fatal(char *fmt, ...);
  33. #else
  34. void error();
  35. void fatal();
  36. #endif
  37. char *filename; /* Name of input file */
  38. int errors; /* Number of errors */
  39. extern char *C_error;
  40. int main(int argc, char *argv[])
  41. {
  42. struct e_instr buf;
  43. struct e_instr *p = &buf;
  44. if (argc >= 2) {
  45. filename = argv[1];
  46. }
  47. else filename = 0;
  48. if (!EM_open(filename)) {
  49. fatal(EM_error);
  50. }
  51. EM_getinstr(p);
  52. C_init((arith) EM_wordsize, (arith) EM_pointersize);
  53. if (argc >= 3) {
  54. if (!C_open(argv[2])) {
  55. fatal("C_open failed");
  56. }
  57. }
  58. else if (!C_open( (char *) 0)) fatal("C_open failed");
  59. C_magic();
  60. while (p->em_type != EM_EOF) {
  61. if (p->em_type == EM_FATAL) {
  62. fatal("%s", EM_error);
  63. }
  64. if (EM_error) {
  65. error("%s", EM_error);
  66. }
  67. if (p->em_type != EM_ERROR && !C_out(p)) {
  68. error("%s", C_error);
  69. }
  70. EM_getinstr(p);
  71. }
  72. C_close();
  73. EM_close();
  74. exit(errors);
  75. return errors;
  76. }
  77. #if __STDC__
  78. void error(char *fmt, ...)
  79. {
  80. va_list ap;
  81. fprint(stderr,
  82. "%s, line %d: ",
  83. filename ? filename : "standard input",
  84. EM_lineno);
  85. va_start(ap, fmt);
  86. vfprintf(stderr, fmt, ap);
  87. fprintf(stderr, "\n");
  88. va_end(ap);
  89. }
  90. void fatal(char *fmt, ...)
  91. {
  92. va_list ap;
  93. if (C_busy()) C_close();
  94. fprint(stderr,
  95. "%s, line %d: ",
  96. filename ? filename : "standard input",
  97. EM_lineno);
  98. va_start(ap, fmt);
  99. vfprintf(stderr, fmt, ap);
  100. fprintf(stderr, "\n");
  101. va_end(ap);
  102. exit(1);
  103. }
  104. #else /* __STDC__ */
  105. /* VARARGS */
  106. error(s,a1,a2,a3,a4)
  107. char *s;
  108. {
  109. fprint(STDERR,
  110. "%s, line %d: ",
  111. filename ? filename : "standard input",
  112. EM_lineno);
  113. fprint(STDERR,s,a1,a2,a3,a4);
  114. fprint(STDERR, "\n");
  115. errors++;
  116. }
  117. /* VARARGS */
  118. fatal(s,a1,a2,a3,a4)
  119. char *s;
  120. {
  121. if (C_busy()) C_close();
  122. error(s,a1,a2,a3,a4);
  123. exit(1);
  124. }
  125. #endif /* __STDC__ */