crti.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (C) 2001-2022 Free Software Foundation, Inc.
  2. Contributed by Hans-Peter Nilsson <hp@bitrange.com>
  3. This file is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 3, or (at your option) any
  6. later version.
  7. This file is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. % This is the crt0 equivalent for mmix-knuth-mmixware, for setting up
  19. % things for compiler-generated assembly-code and for setting up things
  20. % between where the simulator calls and main, and shutting things down on
  21. % the way back. There's an actual crt0.o elsewhere, but that's a dummy.
  22. % This file and the GCC output are supposed to be *reasonably*
  23. % mmixal-compatible to enable people to re-use output with Knuth's mmixal.
  24. % However, forward references are used more freely: we are using the
  25. % binutils tools. Users of mmixal beware; you will sometimes have to
  26. % re-order things or use temporary variables.
  27. % Users of mmixal will want to set up 8H and 9H to be .text and .data
  28. % respectively, so the compiler can switch between them pretending they're
  29. % segments.
  30. % This little treasure (some contents) is required so the 32 lowest
  31. % address bits of user data will not be zero. Because of truncation,
  32. % that would cause testcase gcc.c-torture/execute/980701-1.c to
  33. % incorrectly fail.
  34. .data ! mmixal:= 8H LOC Data_Segment
  35. .p2align 3
  36. dstart OCTA 2009
  37. .text ! mmixal:= 9H LOC 8B; LOC #100
  38. .global Main
  39. % The __Stack_start symbol is provided by the link script.
  40. stackpp OCTA __Stack_start
  41. crtstxt OCTA _init % Assumed to be the lowest executed address.
  42. OCTA __etext % Assumed to be beyond the highest executed address.
  43. crtsdat OCTA dstart % Assumed to be the lowest accessed address.
  44. OCTA _end % Assumed to be beyond the highest accessed address.
  45. % "Main" is the magic symbol the simulator jumps to. We want to go
  46. % on to "main".
  47. % We need to set rG explicitly to avoid hard-to-debug situations.
  48. Main SETL $255,32
  49. PUT rG,$255
  50. % Make sure we have valid memory for addresses in .text and .data (and
  51. % .bss, but we include this in .data), for the benefit of mmo-using
  52. % simulators that require validation of addresses for which contents
  53. % is not present. Due to its implicit-zero nature, zeros in contents
  54. % may be left out in the mmo format, but we don't know the boundaries
  55. % of those zero-chunks; for mmo files from binutils, they correspond
  56. % to the beginning and end of sections in objects before linking. We
  57. % validate the contents by executing PRELD (0; one byte) on each
  58. % 2048-byte-boundary of our .text .data, and we assume this size
  59. % matches the magic lowest-denominator chunk-size for all
  60. % validation-requiring simulators. The effect of the PRELD (any size)
  61. % is assumed to be the same as initial loading of the contents, as
  62. % long as the PRELD happens before the first PUSHJ/PUSHGO. If it
  63. % happens after that, we'll need to distinguish between
  64. % access-for-execution and read/write access.
  65. GETA $255,crtstxt
  66. LDOU $2,$255,0
  67. ANDNL $2,#7ff % Align the start at a 2048-boundary.
  68. LDOU $3,$255,8
  69. SETL $4,2048
  70. 0H PRELD 0,$2,0
  71. ADDU $2,$2,$4
  72. CMP $255,$2,$3
  73. BN $255,0B
  74. GETA $255,crtsdat
  75. LDOU $2,$255,0
  76. ANDNL $2,#7ff
  77. LDOU $3,$255,8
  78. 0H PRELD 0,$2,0
  79. ADDU $2,$2,$4
  80. CMP $255,$2,$3
  81. BN $255,0B
  82. % Initialize the stack pointer. It is supposedly made a global
  83. % zero-initialized (allowed to change) register in crtn.S; we use the
  84. % explicit number.
  85. GETA $255,stackpp
  86. LDOU $254,$255,0
  87. PUSHJ $2,_init
  88. #ifdef __MMIX_ABI_GNU__
  89. % Copy argc and argv from their initial position to argument registers
  90. % where necessary.
  91. SET $231,$0
  92. SET $232,$1
  93. #else
  94. % For the mmixware ABI, we need to move arguments. The return value will
  95. % appear in $0.
  96. SET $2,$1
  97. SET $1,$0
  98. #endif
  99. PUSHJ $0,main
  100. JMP exit
  101. % Provide the first part of _init and _fini. Save the return address on the
  102. % register stack. We eventually ignore the return address of these
  103. % PUSHJ:s, so it doesn't matter that whether .init and .fini code calls
  104. % functions or where they store rJ. We shouldn't get there, so die
  105. % (TRAP Halt) if that happens.
  106. .section .init,"ax",@progbits
  107. .global _init
  108. _init:
  109. GET $0,:rJ
  110. PUSHJ $1,0F
  111. SETL $255,255
  112. TRAP 0,0,0
  113. 0H IS @
  114. % Register _fini to be executed as the last atexit function.
  115. #ifdef __MMIX_ABI_GNU__
  116. GETA $231,_fini
  117. #else
  118. GETA $1,_fini
  119. #endif
  120. PUSHJ $0,atexit
  121. .section .fini,"ax",@progbits
  122. .global _fini
  123. _fini:
  124. GET $0,:rJ
  125. PUSHJ $1,0F
  126. SETL $255,255
  127. TRAP 0,0,0
  128. 0H IS @