cgen-run.c: new mainloop for cgen
[external/binutils.git] / sim / common / sim-engine.c
1 /* Generic simulator halt/restart.
2    Copyright (C) 1997 Free Software Foundation, Inc.
3    Contributed by Cygnus Support.
4
5 This file is part of GDB, the GNU debugger.
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 2, or (at your option)
10 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 along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include <stdio.h>
22 #include <signal.h>
23
24 #include "sim-main.h"
25 #include "sim-assert.h"
26
27
28 /* Generic halt */
29
30 void
31 sim_engine_halt (SIM_DESC sd,
32                  sim_cpu *last_cpu,
33                  sim_cpu *next_cpu, /* NULL - use default */
34                  sim_cia cia,
35                  enum sim_stop reason,
36                  int sigrc)
37 {
38   sim_engine *engine = STATE_ENGINE (sd);
39   ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
40   if (engine->jmpbuf != NULL)
41     {
42       jmp_buf *halt_buf = engine->jmpbuf;
43       engine->last_cpu = last_cpu;
44       engine->next_cpu = next_cpu;
45       engine->reason = reason;
46       engine->sigrc = sigrc;
47       SIM_ENGINE_HALT_HOOK (sd, last_cpu, cia);
48       longjmp(*halt_buf, 1);
49     }
50   else
51     sim_io_error (sd, "sim_halt - bad long jump");
52 }
53
54
55 /* Generic restart */
56
57 void
58 sim_engine_restart (SIM_DESC sd,
59                     sim_cpu *last_cpu,
60                     sim_cpu *next_cpu,
61                     sim_cia cia)
62 {
63   sim_engine *engine = STATE_ENGINE (sd);
64   ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
65   if (engine->jmpbuf != NULL)
66     {
67       jmp_buf *halt_buf = engine->jmpbuf;
68       engine->last_cpu = last_cpu;
69       engine->next_cpu = next_cpu;
70       SIM_ENGINE_RESTART_HOOK (sd, last_cpu, cia);
71       longjmp(*halt_buf, 2);
72     }
73   else
74     sim_io_error (sd, "sim_restart - bad long jump");
75 }
76
77
78 /* Generic error code */
79
80 void
81 sim_engine_abort (SIM_DESC sd,
82                   sim_cpu *cpu,
83                   sim_cia cia,
84                   const char *fmt,
85                   ...)
86 {
87   ASSERT (sd == NULL || STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
88   if (sd == NULL)
89     {
90       va_list ap;
91       va_start(ap, fmt);
92       vfprintf (stderr, fmt, ap);
93       va_end(ap);
94       fprintf (stderr, "\nQuit\n");
95       abort ();
96     }
97   else if (STATE_ENGINE (sd)->jmpbuf == NULL)
98     {
99       va_list ap;
100       va_start(ap, fmt);
101       sim_io_evprintf (sd, fmt, ap);
102       va_end(ap);
103       sim_io_eprintf (sd, "\n");
104       sim_io_error (sd, "Quit Simulator");
105     }
106   else
107     {
108       va_list ap;
109       va_start(ap, fmt);
110       sim_io_evprintf (sd, fmt, ap);
111       va_end(ap);
112       sim_io_eprintf (sd, "\n");
113       sim_engine_halt (sd, cpu, NULL, cia, sim_stopped, SIGABRT);
114     }
115 }
116
117
118 /* Generic next/last cpu */
119
120 int
121 sim_engine_last_cpu_nr (SIM_DESC sd)
122 {
123   sim_engine *engine = STATE_ENGINE (sd);
124   if (engine->last_cpu != NULL)
125     return engine->last_cpu - STATE_CPU (sd, 0);
126   else
127     return MAX_NR_PROCESSORS;
128 }
129
130 int
131 sim_engine_next_cpu_nr (SIM_DESC sd)
132 {
133   sim_engine *engine = STATE_ENGINE (sd);
134   if (engine->next_cpu != NULL)
135     return engine->next_cpu - STATE_CPU (sd, 0);
136   else
137     return sim_engine_last_cpu_nr (sd) + 1;
138 }