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