convert.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /* 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 <system.h>
  18. #include <em_pseu.h>
  19. #include <em_mnem.h>
  20. #include <em_spec.h>
  21. #include <em_flag.h>
  22. #include <em_ptyp.h>
  23. #include <em.h>
  24. #include <em_comp.h>
  25. char *filename; /* Name of input file */
  26. int errors; /* Number of errors */
  27. int main(int argc, char *argv[])
  28. {
  29. struct e_instr buf;
  30. struct e_instr *p = &buf;
  31. if (argc >= 2) {
  32. filename = argv[1];
  33. }
  34. else filename = 0;
  35. if (!EM_open(filename)) {
  36. fatal(EM_error);
  37. }
  38. EM_getinstr(p);
  39. C_init((arith) EM_wordsize, (arith) EM_pointersize);
  40. if (argc >= 3) {
  41. if (!C_open(argv[2])) {
  42. fatal("C_open failed");
  43. }
  44. }
  45. else if (!C_open( (char *) 0)) fatal("C_open failed");
  46. C_magic();
  47. while (p->em_type != EM_EOF) {
  48. if (p->em_type == EM_FATAL) {
  49. fatal("%s", EM_error);
  50. }
  51. if (EM_error) {
  52. error("%s", EM_error);
  53. }
  54. if (p->em_type != EM_ERROR) {
  55. EM_mkcalls(p);
  56. }
  57. EM_getinstr(p);
  58. }
  59. C_close();
  60. EM_close();
  61. exit(errors);
  62. }
  63. /* VARARGS */
  64. static int verror(char *s, va_list ap)
  65. {
  66. fprintf(stderr,
  67. "%s, line %d: ",
  68. filename ? filename : "standard input",
  69. EM_lineno);
  70. vfprintf(stderr, s, ap);
  71. fprintf(stderr, "\n");
  72. errors++;
  73. return 0;
  74. }
  75. int error(char *s, ...)
  76. {
  77. va_list ap;
  78. va_start(ap, s);
  79. verror(s, ap);
  80. va_end(ap);
  81. return 0;
  82. }
  83. /*VARARGS1*/
  84. int fatal(char *s, ...)
  85. {
  86. va_list ap;
  87. va_start(ap, s);
  88. verror(s, ap);
  89. va_end(ap);
  90. exit(1);
  91. return 0;
  92. }