ginger.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright 1996 by Jutta Degener and Carsten Bormann, Technische
  3. * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
  4. * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
  5. */
  6. /*$Header*/
  7. /* Generate code to pack a bit array from a name:#bits description */
  8. #include <stdio.h>
  9. #include "taste.h"
  10. #include "proto.h"
  11. #include <limits.h>
  12. /* This module is the opposite of sour. Sweet was already taken,
  13. * that's why it's called ginger. (Add one point if that reminds
  14. * you of Gary Larson.)
  15. */
  16. #define WORD_BITS 16 /* sizeof(uword) * CHAR_BIT on the
  17. * target architecture---if this isn't 16,
  18. * you're in trouble with this library anyway.
  19. */
  20. #define BYTE_BITS 8 /* CHAR_BIT on the target architecture---
  21. * if this isn't 8, you're in *deep* trouble.
  22. */
  23. void write_code P2((s_spex, n_spex), struct spex * s_spex, int n_spex)
  24. {
  25. struct spex * sp = s_spex;
  26. int n_in = 0;
  27. printf("uword sr = 0;\n");
  28. for (; n_spex > 0; n_spex--, sp++) {
  29. while (n_in < sp->varsize) {
  30. if (n_in) printf("sr |= (uword)*c++ << %d;\n", n_in);
  31. else printf("sr = *c++;\n");
  32. n_in += BYTE_BITS;
  33. }
  34. printf("%s = sr & %#x; sr >>= %d;\n",
  35. sp->var, ~(~0U << sp->varsize), sp->varsize);
  36. n_in -= sp->varsize;
  37. }
  38. if (n_in > 0) {
  39. fprintf(stderr, "%d bits left over\n", n_in);
  40. }
  41. }