lexh.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*% c99 -O3 -Wall -o # %
  2. */
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <limits.h>
  7. #include <stdint.h>
  8. char *tok[] = {
  9. "add", "sub", "neg", "div", "rem", "udiv", "urem", "mul",
  10. "and", "or", "xor", "sar", "shr", "shl", "stored",
  11. "stores", "storel", "storew", "storeh", "storeb",
  12. "load", "loadsw", "loaduw", "loadsh", "loaduh",
  13. "loadsb", "loadub", "extsw", "extuw", "extsh",
  14. "extuh", "extsb", "extub", "exts", "truncd",
  15. "stosi", "dtosi", "stoui", "dtoui", "uwtof",
  16. "ultof", "swtof", "sltof", "cast", "copy",
  17. "alloc4", "alloc8", "alloc16", "culew", "cultw",
  18. "cslew", "csltw", "csgtw", "csgew", "cugtw",
  19. "cugew", "ceqw", "cnew", "culel", "cultl", "cslel",
  20. "csltl", "csgtl", "csgel", "cugtl", "cugel",
  21. "ceql", "cnel", "cles", "clts", "cgts", "cges",
  22. "cnes", "ceqs", "cos", "cuos", "cled", "cltd",
  23. "cgtd", "cged", "cned", "ceqd", "cod", "cuod",
  24. "vaarg", "vastart", "...", "env", "dbgloc",
  25. "call", "phi", "jmp", "jnz", "ret", "hlt", "export",
  26. "function", "type", "data", "section", "align", "dbgfile",
  27. "blit", "l", "w", "sh", "uh", "h", "sb", "ub", "b",
  28. "d", "s", "z", "loadw", "loadl", "loads", "loadd",
  29. "alloc1", "alloc2", "thread", "common",
  30. };
  31. enum {
  32. Ntok = sizeof tok / sizeof tok[0]
  33. };
  34. uint32_t th[Ntok];
  35. uint32_t
  36. hash(char *s)
  37. {
  38. uint32_t h;
  39. h = 0;
  40. for (; *s; ++s)
  41. h = *s + 17*h;
  42. return h;
  43. }
  44. int
  45. main()
  46. {
  47. char *bmap;
  48. uint32_t h, M, K;
  49. int i, j;
  50. bmap = malloc(1u << 31);
  51. for (i=0; i<Ntok; ++i) {
  52. h = hash(tok[i]);
  53. for (j=0; j<i; ++j)
  54. if (th[j] == h) {
  55. printf("error: hash()\n");
  56. printf("\t%s\n", tok[i]);
  57. printf("\t%s\n", tok[j]);
  58. exit(1);
  59. }
  60. th[i] = h;
  61. }
  62. for (i=9; 1<<i < Ntok; ++i);
  63. M = 32 - i;
  64. for (;; --M) {
  65. printf("trying M=%d...\n", M);
  66. K = 1;
  67. do {
  68. memset(bmap, 0, 1 << (32 - M));
  69. for (i=0; i<Ntok; ++i) {
  70. h = (th[i]*K) >> M;
  71. if (bmap[h])
  72. break;
  73. bmap[h] = 1;
  74. }
  75. if (i==Ntok) {
  76. printf("found K=%d for M=%d\n", K, M);
  77. exit(0);
  78. }
  79. K += 2;
  80. } while (K != 1);
  81. }
  82. }