conversion.c 2.5 KB

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