1 /* GNU CHILL compiler regression test file
2 Copyright (C) 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
29 /* some allocation/reallocation functions */
35 void *tmp = malloc (size);
39 fprintf (stderr, "Out of heap space.\n");
50 void *tmp = realloc (ptr, size);
54 fprintf (stderr, "Out of heap space.\n");
60 /* the necessary data */
61 #define MAX_NUMBER 100
62 typedef char UsedValues[MAX_NUMBER];
64 #define MAX_COPIES 100
66 #define MAX_PER_ITEM 20
67 typedef struct TASKINGSTRUCTLIST
69 struct TASKINGSTRUCTLIST *forward;
71 TaskingStruct *data[MAX_PER_ITEM];
72 char copies[MAX_COPIES];
76 static TaskingStructList *task_array[LAST_AND_UNUSED];
77 static UsedValues used_values[LAST_AND_UNUSED];
80 get_next_free_number (vals)
84 for (i = 1; i < MAX_NUMBER; i++)
92 fprintf (stderr, "There are no more free numbers.\n");
96 /* function search for the next available copy number */
98 get_next_copy_number (p)
103 for (i = 0; i < MAX_COPIES; i++)
111 fprintf (stderr, "No more copies available for \"%s\".\n",
116 /* function registers a tasking entry from a module and assign
117 a value to the type */
120 __register_tasking (t)
123 TaskingStructList *p;
125 /* check first if a value was provided and if it is in range */
126 if (t->value_defined && *t->value >= MAX_NUMBER)
128 fprintf (stderr, "Value %d out of range.\n", *t->value);
132 /* look for item defined */
133 p = task_array[t->type];
136 if (!strcmp (p->data[0]->name, t->name))
144 TaskingStructList *wrk = (TaskingStructList *)&task_array[t->type];
146 /* this is a new one -- allocate space */
147 p = xmalloc (sizeof (TaskingStructList));
148 memset (p->copies, 0, sizeof (p->copies));
160 if (p->num >= MAX_PER_ITEM)
162 fprintf (stderr, "Too many registrations of \"%s\".\n", t->name);
165 p->data[p->num++] = t;
169 /* define all the entries for the runtime system. They will be
170 needed by chillrt0.o */
172 typedef char *(*fetch_names) ();
173 typedef int (*fetch_numbers) ();
175 static char tmp_for_fetch_name[100];
178 __fetch_name (number)
181 TaskingStructList *p = task_array[Process];
185 if (*(p->data[0]->value) == number)
186 return (p->data[0]->name);
189 sprintf (tmp_for_fetch_name, "%d", number);
190 return (tmp_for_fetch_name);
192 fetch_names __RTS_FETCH_NAMES__ = __fetch_name;
195 __fetch_number (name)
198 TaskingStructList *p = task_array[Process];
202 if (!strcmp (p->data[0]->name, name))
203 return (*(p->data[0]->value));
208 fetch_numbers __RTS_FETCH_NUMBERS__ = __fetch_number;
211 /* here we go to check all registered items */
216 TaskingStructList *p;
218 for (i = Process; i <= Event; i++)
223 TaskingStruct *t = 0;
227 for (j = 0; j < p->num; j++)
229 if (p->data[j]->value_defined)
233 if (*(t->value) != *(p->data[j]->value))
235 fprintf (stderr, "Different values (%d & %d) for \"%s\".",
236 *(t->value), *(p->data[j]->value), t->name);
250 if (used_values[t->type][val])
252 fprintf (stderr, "Value %d for \"%s\" is already used.\n",
256 used_values[t->type][val] = 1;
260 /* we have to create a new value */
261 val = get_next_free_number (used_values[p->data[0]->type]);
264 for (j = 0; j < p->num; j++)
266 p->data[j]->value_defined = 1;
267 *(p->data[j]->value) = val;
274 EntryPoint __RTS_INIT__ = __rts_init;
276 /* define the start process queue */
277 typedef struct STARTENTRY
279 struct STARTENTRY *forward;
286 static StartEntry *start_queue = 0;
287 static StartEntry *current_process = 0;
289 /* the jump buffer for the main loop */
290 static jmp_buf jump_buffer;
291 static int jump_buffer_initialized = 0;
293 /* look for entries in start_queue and start the process */
301 if (setjmp (jump_buffer) == 0)
303 jump_buffer_initialized = 1;
308 start_queue = s->forward;
310 /* call the process */
311 (*s->entry) (s->data);
314 /* when queue empty we have finished */
320 if (current_process->data)
321 free (current_process->data);
322 free (current_process);
327 EntryPoint __RTS_MAIN_LOOP__ = __rts_main_loop;
331 __start_process (ptype, pcopy, arg_size, args, ins)
338 TaskingStructList *p = task_array[Process];
341 short this_copy = pcopy;
344 /* search for the process */
347 if (*(p->data[0]->value) == ptype)
353 fprintf (stderr, "Cannot find a process with type %d.\n", ptype);
357 /* search for the entry point */
358 for (i = 0; i < p->num; i++)
360 if (p->data[i]->entry)
362 pc = p->data[i]->entry;
368 fprintf (stderr, "Process \"%s\" doesn't have an entry point.\n",
374 if (pcopy >= MAX_COPIES)
376 fprintf (stderr, "Copy number (%d) out of range.\n", pcopy);
381 /* search for a copy number */
382 this_copy = get_next_copy_number (p);
386 if (p->copies[pcopy])
388 /* FIXME: should be exception 'startfail' */
389 fprintf (stderr, "Copy number %d already in use for \"%s\".\n",
390 pcopy, p->data[0]->name);
393 p->copies[this_copy = pcopy] = 1;
396 /* ready to build start_queue entry */
397 s = xmalloc (sizeof (StartEntry));
399 s->whoami.pcopy = this_copy;
400 s->whoami.ptype = ptype;
402 s->datalen = arg_size;
405 s->data = xmalloc (arg_size);
406 memcpy (s->data, args, arg_size);
411 /* queue that stuff in */
412 wrk = (StartEntry *)&start_queue;
417 /* if we have a pointer to ins -- set it */
421 ins->pcopy = this_copy;
428 if (!jump_buffer_initialized)
430 fprintf (stderr, "STOP called before START.\n");
433 longjmp (jump_buffer, 1);
437 /* function returns INSTANCE of current process */
443 whoami = current_process->whoami;
459 typedef struct SIGNALQUEUE
461 struct SIGNALQUEUE *forward;
469 /* define the signal queue */
470 static SignalQueue *msg_queue = 0;
474 __send_signal (s, to, prio, with_len, with)
481 SignalQueue *wrk = (SignalQueue *)&msg_queue;
483 TaskingStructList *t = task_array[Process];
485 /* search for process is defined and running */
488 if (*(t->data[0]->value) == to.ptype)
492 if (!t || !t->copies[to.pcopy])
494 fprintf (stderr, "Can't find instance [%d,%d].\n",
499 /* go to the end of the msg_queue */
503 p = xmalloc (sizeof (SignalQueue));
505 if (p->data_len = s->data_len)
507 p->data = xmalloc (s->data_len);
508 memcpy (p->data, s->data, s->data_len);
513 p->from = __whoami ();
519 start_signal_timeout (i, s, j)
524 __send_signal (s, __whoami (), 0, 0, 0);
528 /* receive a signal */
530 __wait_signal_timed (sig_got, nsigs, sigptr, datap,
531 datalen, ins, else_branche,
532 to, filename, lineno)
544 INSTANCE me = __whoami ();
545 SignalQueue *wrk, *p = msg_queue;
549 /* search for a signal to `me' */
550 wrk = (SignalQueue *)&msg_queue;
554 if (p->to.ptype == me.ptype
555 && p->to.pcopy == me.pcopy)
563 fprintf (stderr, "No signal for [%d,%d].\n",
568 /* queue the message out */
569 wrk->forward = p->forward;
571 /* now look for signal in list */
572 for (i = 0; i < nsigs; i++)
573 if (*(sigptr[i]) == p->sc)
576 if (i >= nsigs && ! else_branche)
577 /* signal not in list and no ELSE in code */
578 __cause_exception ("signalfail", __FILE__, __LINE__);
582 /* signal not in list */
593 /* we have found a signal in the list */
596 if (datalen >= p->data_len
598 memcpy (datap, p->data, p->data_len);
600 __cause_exception ("spacefail", __FILE__, __LINE__);
613 /* wait a certain amount of seconds */
615 __sleep_till (abstime, reltime, fname, lineno)
625 /* set up an alarm */
626 static int timeout_flag = 0;
628 static void alarm_handler ()
634 __define_timeout (howlong, filename, lineno)
635 unsigned long howlong; /* comes in millisecs */
639 unsigned int prev_alarm_value;
641 signal (SIGALRM, alarm_handler);
642 prev_alarm_value = alarm ((unsigned int)(howlong / 1000));
643 return &timeout_flag;
646 /* wait till timeout expires */
648 __wait_timeout (toid, filename, lineno)