makecldef.c 2.0 KB

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