makecldef.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. /* MAKECLASSDEF
  10. *
  11. * This program is used by several phases of the optimizer
  12. * to make the file classdefs.h. It reads two files:
  13. * - the em_mnem,h file, containing the definitions of the
  14. * EM mnemonics
  15. * - the class-file, containing tuples:
  16. * (mnemonic, src_class, res_class)
  17. * where src_class and res_class are integers telling how
  18. * to compute the number of bytes popped and pushed
  19. * by the instruction.
  20. * The output (standard output) is a C array.
  21. */
  22. #define TRUE 1
  23. #define FALSE 0
  24. void error(char *);
  25. void convert(FILE *mnemfile, FILE *classfile)
  26. {
  27. char mnem1[10], mnem2[10],def[10];
  28. int src,res,newcl,opc;
  29. newcl = TRUE;
  30. printf("struct class classtab[] = {\n");
  31. printf("\t{ NOCLASS,\tNOCLASS },\n");
  32. /* EM mnemonics start at 1, arrays in C at 0 */
  33. for (;;) {
  34. fscanf(mnemfile,"%s%s%d",def,mnem1,&opc);
  35. /* read a line like "#define op_aar 1" */
  36. if (feof(mnemfile)) break;
  37. if (strcmp(def,"#define") != 0) {
  38. error("bad mnemonic file, #define expected");
  39. }
  40. if (newcl) {
  41. fscanf(classfile,"%s%d%d",mnem2,&src,&res);
  42. /* read a line like "op_loc 8 1" */
  43. }
  44. if (feof(classfile) || strcmp(mnem1,mnem2) != 0) {
  45. /* there is no line for this mnemonic, so
  46. * it has no class.
  47. */
  48. printf("\t { NOCLASS,\tNOCLASS },\n");
  49. newcl = FALSE;
  50. } else {
  51. printf("\t { CLASS%d,\t\tCLASS%d },\n",src,res);
  52. /* print a line like "CLASS8, CLASS1," */
  53. newcl = TRUE;
  54. }
  55. }
  56. printf("};\n");
  57. }
  58. void error(char *s)
  59. {
  60. fprintf(stderr,"%s\n",s);
  61. exit(-1);
  62. }
  63. int main(int argc, char *argv[])
  64. {
  65. FILE *f1,*f2;
  66. if (argc != 3) {
  67. error("usage: makeclassdef mnemfile classfile");
  68. }
  69. if ((f1 = fopen(argv[1],"r")) == NULL) {
  70. error("cannot open mnemonic file");
  71. }
  72. if ((f2 = fopen(argv[2],"r")) == NULL) {
  73. error("cannot open class file");
  74. }
  75. convert(f1,f2);
  76. exit(0);
  77. }