libgcc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* The following code comes from gcc-4.3. */
  19. #include <grub/types.h>
  20. #include <grub/symbol.h>
  21. #include <grub/misc.h>
  22. #include <grub/cache.h>
  23. GRUB_EXPORT(__ashldi3);
  24. GRUB_EXPORT(__ashrdi3);
  25. GRUB_EXPORT(__lshrdi3);
  26. GRUB_EXPORT(__ucmpdi2);
  27. #define DWtype long long
  28. #define UDWtype unsigned long long
  29. #define Wtype int
  30. #define UWtype unsigned int
  31. #define BITS_PER_UNIT 8
  32. #define shift_count_type int
  33. #define cmp_return_type int
  34. #ifdef GRUB_CPU_WORDS_BIGENDIAN
  35. struct DWstruct {Wtype high, low;};
  36. #else
  37. struct DWstruct {Wtype low, high;};
  38. #endif
  39. typedef union
  40. {
  41. struct DWstruct s;
  42. DWtype ll;
  43. } DWunion;
  44. DWtype __ashldi3 (DWtype u, shift_count_type b);
  45. DWtype __ashrdi3 (DWtype u, shift_count_type b);
  46. DWtype __lshrdi3 (DWtype u, shift_count_type b);
  47. void __trampoline_setup (grub_uint32_t* stack, int size,
  48. void* func, void* local_ptr);
  49. cmp_return_type __ucmpdi2 (DWtype a, DWtype b);
  50. DWtype
  51. __ashldi3 (DWtype u, shift_count_type b)
  52. {
  53. if (b == 0)
  54. return u;
  55. const DWunion uu = {.ll = u};
  56. const shift_count_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
  57. DWunion w;
  58. if (bm <= 0)
  59. {
  60. w.s.low = 0;
  61. w.s.high = (UWtype) uu.s.low << -bm;
  62. }
  63. else
  64. {
  65. const UWtype carries = (UWtype) uu.s.low >> bm;
  66. w.s.low = (UWtype) uu.s.low << b;
  67. w.s.high = ((UWtype) uu.s.high << b) | carries;
  68. }
  69. return w.ll;
  70. }
  71. DWtype
  72. __ashrdi3 (DWtype u, shift_count_type b)
  73. {
  74. if (b == 0)
  75. return u;
  76. const DWunion uu = {.ll = u};
  77. const shift_count_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
  78. DWunion w;
  79. if (bm <= 0)
  80. {
  81. /* w.s.high = 1..1 or 0..0 */
  82. w.s.high = uu.s.high >> (sizeof (Wtype) * BITS_PER_UNIT - 1);
  83. w.s.low = uu.s.high >> -bm;
  84. }
  85. else
  86. {
  87. const UWtype carries = (UWtype) uu.s.high << bm;
  88. w.s.high = uu.s.high >> b;
  89. w.s.low = ((UWtype) uu.s.low >> b) | carries;
  90. }
  91. return w.ll;
  92. }
  93. DWtype
  94. __lshrdi3 (DWtype u, shift_count_type b)
  95. {
  96. if (b == 0)
  97. return u;
  98. const DWunion uu = {.ll = u};
  99. const shift_count_type bm = (sizeof (Wtype) * BITS_PER_UNIT) - b;
  100. DWunion w;
  101. if (bm <= 0)
  102. {
  103. w.s.high = 0;
  104. w.s.low = (UWtype) uu.s.high >> -bm;
  105. }
  106. else
  107. {
  108. const UWtype carries = (UWtype) uu.s.high << bm;
  109. w.s.high = (UWtype) uu.s.high >> b;
  110. w.s.low = ((UWtype) uu.s.low >> b) | carries;
  111. }
  112. return w.ll;
  113. }
  114. #if 0
  115. void
  116. __trampoline_setup (grub_uint32_t* stack, int size,
  117. void* func, void* local_ptr)
  118. {
  119. if (size < 40)
  120. grub_abort();
  121. /* create trampoline */
  122. stack[0] = 0x7c0802a6; /* mflr r0 */
  123. stack[1] = 0x4800000d; /* bl Lbase */
  124. stack[2] = (grub_uint32_t) func;
  125. stack[3] = (grub_uint32_t) local_ptr;
  126. stack[4] = 0x7d6802a6; /* Lbase: mflr r11 */
  127. stack[5] = 0x818b0000; /* lwz r12,0(r11) */
  128. stack[6] = 0x7c0803a6; /* mtlr r0 */
  129. stack[7] = 0x7d8903a6; /* mtctr r12 */
  130. stack[8] = 0x816b0004; /* lwz r11,4(r11) */
  131. stack[9] = 0x4e800420; /* bctr */
  132. grub_arch_sync_caches (stack, 40);
  133. }
  134. #endif
  135. cmp_return_type
  136. __ucmpdi2 (DWtype a, DWtype b)
  137. {
  138. const DWunion au = {.ll = a};
  139. const DWunion bu = {.ll = b};
  140. if ((UWtype) au.s.high < (UWtype) bu.s.high)
  141. return 0;
  142. else if ((UWtype) au.s.high > (UWtype) bu.s.high)
  143. return 2;
  144. if ((UWtype) au.s.low < (UWtype) bu.s.low)
  145. return 0;
  146. else if ((UWtype) au.s.low > (UWtype) bu.s.low)
  147. return 2;
  148. return 1;
  149. }