pswt.c 2.2 KB

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