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