pswt.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "gc.h"
  2. int
  3. swcmp(const void *a1, const void *a2)
  4. {
  5. C1 *p1, *p2;
  6. p1 = (C1*)a1;
  7. p2 = (C1*)a2;
  8. if(p1->val < p2->val)
  9. return -1;
  10. return p1->val > p2->val;
  11. }
  12. void
  13. doswit(Node *n)
  14. {
  15. Case *c;
  16. C1 *q, *iq;
  17. long def, nc, i;
  18. def = 0;
  19. nc = 0;
  20. for(c = cases; c->link != C; c = c->link) {
  21. if(c->def) {
  22. if(def)
  23. diag(n, "more than one default in switch");
  24. def = c->label;
  25. continue;
  26. }
  27. nc++;
  28. }
  29. iq = alloc(nc*sizeof(C1));
  30. q = iq;
  31. for(c = cases; c->link != C; c = c->link) {
  32. if(c->def)
  33. continue;
  34. q->label = c->label;
  35. q->val = c->val;
  36. q++;
  37. }
  38. qsort(iq, nc, sizeof(C1), swcmp);
  39. if(debug['W'])
  40. for(i=0; i<nc; i++)
  41. print("case %2ld: = %.8llux\n", i, (vlong)iq[i].val);
  42. for(i=0; i<nc-1; i++)
  43. if(iq[i].val == iq[i+1].val)
  44. diag(n, "duplicate cases in switch %lld", (vlong)iq[i].val);
  45. if(def == 0) {
  46. def = breakpc;
  47. nbreak++;
  48. }
  49. swit1(iq, nc, def, n);
  50. }
  51. void
  52. cas(void)
  53. {
  54. Case *c;
  55. c = alloc(sizeof(*c));
  56. c->link = cases;
  57. cases = c;
  58. }
  59. long
  60. outlstring(ushort *s, long n)
  61. {
  62. char buf[2];
  63. int c;
  64. long r;
  65. if(suppress)
  66. return nstring;
  67. while(nstring & 1)
  68. outstring("", 1);
  69. r = nstring;
  70. while(n > 0) {
  71. c = *s++;
  72. if(align(0, types[TCHAR], Aarg1)) {
  73. buf[0] = c>>8;
  74. buf[1] = c;
  75. } else {
  76. buf[0] = c;
  77. buf[1] = c>>8;
  78. }
  79. outstring(buf, 2);
  80. n -= sizeof(ushort);
  81. }
  82. return r;
  83. }
  84. void
  85. nullwarn(Node *l, Node *r)
  86. {
  87. warn(Z, "result of operation not used");
  88. if(l != Z)
  89. cgen(l, Z);
  90. if(r != Z)
  91. cgen(r, Z);
  92. }
  93. void
  94. ieeedtod(Ieee *ieee, double native)
  95. {
  96. double fr, ho, f;
  97. int exp;
  98. if(native < 0) {
  99. ieeedtod(ieee, -native);
  100. ieee->h |= 0x80000000L;
  101. return;
  102. }
  103. if(native == 0) {
  104. ieee->l = 0;
  105. ieee->h = 0;
  106. return;
  107. }
  108. fr = frexp(native, &exp);
  109. f = 2097152L; /* shouldnt use fp constants here */
  110. fr = modf(fr*f, &ho);
  111. ieee->h = ho;
  112. ieee->h &= 0xfffffL;
  113. ieee->h |= (exp+1022L) << 20;
  114. f = 65536L;
  115. fr = modf(fr*f, &ho);
  116. ieee->l = ho;
  117. ieee->l <<= 16;
  118. ieee->l |= (long)(fr*f);
  119. }