pswt.c 2.2 KB

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