conversion.c 2.6 KB

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