conversion.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* $Header$ */
  2. /* C O N V E R S I O N - C O D E G E N E R A T O R */
  3. #include "nofloat.h"
  4. #include <em.h>
  5. #include "arith.h"
  6. #include "type.h"
  7. #include "sizes.h"
  8. #include "Lpars.h"
  9. #define T_SIGNED 1
  10. #define T_UNSIGNED 2
  11. #ifndef NOFLOAT
  12. #define T_FLOATING 3
  13. #endif NOFLOAT
  14. /* conversion() generates the EM code for a conversion between
  15. the types char, short, int, long, float, double and pointer.
  16. In case of integral type, the notion signed / unsigned is
  17. taken into account.
  18. The EM code to obtain this conversion looks like:
  19. LOC sizeof(from_type)
  20. LOC sizeof(to_type)
  21. C??
  22. */
  23. conversion(from_type, to_type)
  24. register struct type *from_type, *to_type;
  25. {
  26. register arith from_size = from_type->tp_size;
  27. register arith to_size = to_type->tp_size;
  28. if (from_type == to_type) /* a little optimisation */
  29. return;
  30. if (to_size < word_size) to_size = word_size;
  31. switch (fundamental(from_type)) {
  32. case T_SIGNED:
  33. switch (fundamental(to_type)) {
  34. case T_SIGNED:
  35. C_loc(from_size);
  36. C_loc(to_size);
  37. C_cii();
  38. break;
  39. case T_UNSIGNED:
  40. if (from_size < word_size) {
  41. C_loc(from_size);
  42. C_loc(word_size);
  43. C_cii();
  44. from_size = word_size;
  45. }
  46. C_loc(from_size);
  47. C_loc(to_size);
  48. C_ciu();
  49. break;
  50. #ifndef NOFLOAT
  51. case T_FLOATING:
  52. if (from_size < word_size) {
  53. C_loc(from_size);
  54. C_loc(word_size);
  55. C_cii();
  56. from_size = word_size;
  57. }
  58. C_loc(from_size);
  59. C_loc(to_size);
  60. C_cif();
  61. break;
  62. #endif NOFLOAT
  63. }
  64. break;
  65. case T_UNSIGNED:
  66. if (from_size < word_size) from_size = word_size;
  67. C_loc(from_size);
  68. C_loc(to_size);
  69. switch (fundamental(to_type)) {
  70. case T_SIGNED:
  71. C_cui();
  72. break;
  73. case T_UNSIGNED:
  74. C_cuu();
  75. break;
  76. #ifndef NOFLOAT
  77. case T_FLOATING:
  78. C_cuf();
  79. break;
  80. #endif NOFLOAT
  81. }
  82. break;
  83. #ifndef NOFLOAT
  84. case T_FLOATING:
  85. C_loc(from_size);
  86. C_loc(to_size);
  87. switch (fundamental(to_type)) {
  88. case T_SIGNED:
  89. C_cfi();
  90. break;
  91. case T_UNSIGNED:
  92. C_cfu();
  93. break;
  94. case T_FLOATING:
  95. C_cff();
  96. break;
  97. }
  98. break;
  99. #endif NOFLOAT
  100. default:
  101. crash("(conversion) illegal type conversion");
  102. }
  103. }
  104. /* fundamental() returns in which category a given type falls:
  105. signed, unsigned or floating
  106. */
  107. int
  108. fundamental(tp)
  109. register struct type *tp;
  110. {
  111. switch (tp->tp_fund) {
  112. case CHAR:
  113. case SHORT:
  114. case INT:
  115. case ERRONEOUS:
  116. case LONG:
  117. case ENUM:
  118. return tp->tp_unsigned ? T_UNSIGNED : T_SIGNED;
  119. #ifndef NOFLOAT
  120. case FLOAT:
  121. case DOUBLE:
  122. return T_FLOATING;
  123. #endif NOFLOAT
  124. case POINTER: /* pointer : signed / unsigned ??? */
  125. return T_SIGNED;
  126. }
  127. return 0;
  128. }