Avoid crash in varobj deletion
[binutils-gdb.git] / sim / cris / mloop.in
1 # Simulator main loop for CRIS. -*- C -*-
2 # Copyright (C) 2004-2021 Free Software Foundation, Inc.
3 # Contributed by Axis Communications.
4 #
5 # This file is part of the GNU simulators.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 # Based on the fr30 file.
21
22 # Syntax:
23 # /bin/sh mainloop.in command
24 #
25 # Command is one of:
26 #
27 # init
28 # support
29 # extract-{simple,scache,pbb}
30 # {full,fast}-exec-{simple,scache,pbb}
31 #
32 # A target need only provide a "full" version of one of simple,scache,pbb.
33 # If the target wants it can also provide a fast version of same.
34 # It can't provide more than this, however for illustration's sake the CRIS
35 # port provides examples of all.
36
37 # ??? After a few more ports are done, revisit.
38 # Will eventually need to machine generate a lot of this.
39
40 case "x$1" in
41
42 xsupport)
43
44 cat <<EOF
45 #include <stdlib.h>
46
47 /* It seems we don't have a templated header file corresponding to
48 cris-tmpl.c, so we have to get out declarations the hackish way. */
49 extern void @cpu@_specific_init (SIM_CPU *current_cpu);
50
51 static INLINE const IDESC *
52 extract (SIM_CPU *current_cpu, PCADDR pc, CGEN_INSN_INT insn, ARGBUF *abuf,
53 int fast_p)
54 {
55 const IDESC *id = @cpu@_decode (current_cpu, pc, insn,
56 #if CGEN_INT_INSN_P
57 insn,
58 #endif
59 abuf);
60 @cpu@_fill_argbuf (current_cpu, abuf, id, pc, fast_p);
61 if (! fast_p)
62 {
63 int trace_p = PC_IN_TRACE_RANGE_P (current_cpu, pc);
64 int profile_p = PC_IN_PROFILE_RANGE_P (current_cpu, pc);
65 @cpu@_fill_argbuf_tp (current_cpu, abuf, trace_p, profile_p);
66 }
67 return id;
68 }
69
70 static INLINE SEM_PC
71 execute (SIM_CPU *current_cpu, SCACHE *sc, int fast_p)
72 {
73 SEM_PC vpc;
74
75 if (fast_p)
76 {
77 #if ! WITH_SEM_SWITCH_FAST
78 #if WITH_SCACHE
79 vpc = (*sc->argbuf.semantic.sem_fast) (current_cpu, sc);
80 #else
81 vpc = (*sc->argbuf.semantic.sem_fast) (current_cpu, &sc->argbuf);
82 #endif
83 #else
84 abort ();
85 #endif /* WITH_SEM_SWITCH_FAST */
86 }
87 else
88 {
89 #if ! WITH_SEM_SWITCH_FULL
90 ARGBUF *abuf = &sc->argbuf;
91 const IDESC *idesc = abuf->idesc;
92 #if WITH_SCACHE_PBB
93 int virtual_p = CGEN_ATTR_VALUE (NULL, idesc->attrs, CGEN_INSN_VIRTUAL);
94 #else
95 int virtual_p = 0;
96 #endif
97
98 if (! virtual_p)
99 {
100 /* FIXME: call x-before */
101 if (ARGBUF_PROFILE_P (abuf))
102 PROFILE_COUNT_INSN (current_cpu, abuf->addr, idesc->num);
103 /* FIXME: Later make cover macros: PROFILE_INSN_{INIT,FINI}. */
104 if (PROFILE_MODEL_P (current_cpu)
105 && ARGBUF_PROFILE_P (abuf))
106 @cpu@_model_insn_before (current_cpu, 1 /*first_p*/);
107 CGEN_TRACE_INSN_INIT (current_cpu, abuf, 1);
108 CGEN_TRACE_INSN (current_cpu, idesc->idata,
109 (const struct argbuf *) abuf, abuf->addr);
110 }
111 #if WITH_SCACHE
112 vpc = (*sc->argbuf.semantic.sem_full) (current_cpu, sc);
113 #else
114 vpc = (*sc->argbuf.semantic.sem_full) (current_cpu, abuf);
115 #endif
116 if (! virtual_p)
117 {
118 /* FIXME: call x-after */
119 if (PROFILE_MODEL_P (current_cpu)
120 && ARGBUF_PROFILE_P (abuf))
121 {
122 int cycles;
123
124 cycles = (*idesc->timing->model_fn) (current_cpu, sc);
125 @cpu@_model_insn_after (current_cpu, 1 /*last_p*/, cycles);
126 }
127 CGEN_TRACE_INSN_FINI (current_cpu, abuf, 1);
128 }
129 #else
130 abort ();
131 #endif /* WITH_SEM_SWITCH_FULL */
132 }
133
134 return vpc;
135 }
136
137 EOF
138
139 ;;
140
141 xinit)
142
143 cat <<EOF
144 /* This seemed the only sane location to emit a call to a
145 model-specific init function. It may not work for all simulator
146 types. FIXME: Introduce a model-init hook. */
147
148 /* We use the same condition as the code that's expected to follow, so
149 GCC can consolidate the code with only one conditional. */
150 if (! CPU_IDESC_SEM_INIT_P (current_cpu))
151 @cpu@_specific_init (current_cpu);
152 EOF
153
154 ;;
155
156 xextract-simple | xextract-scache)
157
158 # Inputs: current_cpu, vpc, sc, FAST_P
159 # Outputs: sc filled in
160
161 cat <<EOF
162 {
163 CGEN_INSN_INT insn = GETIMEMUHI (current_cpu, vpc);
164 extract (current_cpu, vpc, insn, SEM_ARGBUF (sc), FAST_P);
165 }
166 EOF
167
168 ;;
169
170 xextract-pbb)
171
172 # Inputs: current_cpu, pc, sc, max_insns, FAST_P
173 # Outputs: sc, pc
174 # sc must be left pointing past the last created entry.
175 # pc must be left pointing past the last created entry.
176 # If the pbb is terminated by a cti insn, SET_CTI_VPC(sc) must be called
177 # to record the vpc of the cti insn.
178 # SET_INSN_COUNT(n) must be called to record number of real insns.
179
180 cat <<EOF
181 {
182 const IDESC *idesc;
183 int icount = 0;
184
185 /* Make sure the buffer doesn't overflow for profiled insns if
186 max_insns happens to not be a multiple of 3. */
187 if (!FAST_P)
188 max_insns -= 2 + 3;
189 else
190 /* There might be two real insns handled per loop. */
191 max_insns--;
192
193 while (max_insns > 0)
194 {
195 UHI insn = GETIMEMUHI (current_cpu, pc);
196 int trace_p = PC_IN_TRACE_RANGE_P (current_cpu, pc);
197 int profile_p = PC_IN_PROFILE_RANGE_P (current_cpu, pc);
198 int befaft_p = profile_p || trace_p;
199
200 if (befaft_p)
201 {
202 @cpu@_emit_before (current_cpu, sc, pc, 1);
203 ++sc;
204 sc->argbuf.trace_p = trace_p;
205 sc->argbuf.profile_p = profile_p;
206 --max_insns;
207 }
208
209 idesc = extract (current_cpu, pc, insn, &sc->argbuf, FAST_P);
210 ++sc;
211 --max_insns;
212 ++icount;
213
214 if (befaft_p)
215 {
216 @cpu@_emit_after (current_cpu, sc, pc);
217 ++sc;
218 --max_insns;
219 }
220
221 pc += idesc->length;
222
223 if (IDESC_CTI_P (idesc))
224 {
225 SET_CTI_VPC (sc - 1);
226
227 /* Delay slot? Ignore for zero-instructions (bcc .+2) since
228 those are treated as exit insns to avoid runaway sessions
229 for invalid programs. */
230 if (insn != 0 && CGEN_ATTR_VALUE (NULL, idesc->attrs, CGEN_INSN_DELAY_SLOT))
231 {
232 UHI insn;
233 trace_p = PC_IN_TRACE_RANGE_P (current_cpu, pc);
234 profile_p = PC_IN_PROFILE_RANGE_P (current_cpu, pc);
235 befaft_p = profile_p || trace_p;
236
237 if (befaft_p)
238 {
239 @cpu@_emit_before (current_cpu, sc, pc, 1);
240 ++sc;
241 sc->argbuf.trace_p = trace_p;
242 sc->argbuf.profile_p = profile_p;
243 --max_insns;
244 }
245
246 insn = GETIMEMUHI (current_cpu, pc);
247 idesc = extract (current_cpu, pc, insn, &sc->argbuf, FAST_P);
248 ++sc;
249 --max_insns;
250 ++icount;
251
252 if (befaft_p)
253 {
254 @cpu@_emit_after (current_cpu, sc, pc);
255 ++sc;
256 --max_insns;
257 }
258 pc += idesc->length;
259 }
260 break;
261 }
262 }
263
264 Finish:
265 SET_INSN_COUNT (icount);
266 }
267 EOF
268
269 ;;
270
271 xfull-exec-* | xfast-exec-*)
272
273 # Inputs: current_cpu, sc, FAST_P
274 # Outputs: vpc
275 # vpc contains the address of the next insn to execute
276
277 cat <<EOF
278 {
279 #if (! FAST_P && WITH_SEM_SWITCH_FULL) || (FAST_P && WITH_SEM_SWITCH_FAST)
280 #define DEFINE_SWITCH
281 #include "sem@cpu@-switch.c"
282 #else
283 vpc = execute (current_cpu, vpc, FAST_P);
284 #endif
285 }
286 EOF
287
288 ;;
289
290 *)
291 echo "Invalid argument to mainloop.in: $1" >&2
292 exit 1
293 ;;
294
295 esac