OSDN Git Service

* Makefile.am: Override gctest_OBJECTS so tests/test.c can be built.
[pf3gnuchains/gcc-fork.git] / libchill / chillrt0.c
1 /* Implement runtime actions for CHILL.
2    Copyright (C) 1992,1993 Free Software Foundation, Inc.
3    Author: Wilfried Moser
4
5 This file is part of GNU CC.
6
7 GNU CC 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 GNU CC 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 GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* As a special exception, if you link this library with other files,
23    some of which are compiled with GCC, to produce an executable,
24    this library does not by itself cause the resulting executable
25    to be covered by the GNU General Public License.
26    This exception does not however invalidate any other reasons why
27    the executable file might be covered by the GNU General Public License.  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <string.h>
33
34 #include "rtltypes.h"
35 #include "iomodes.h"
36 \f
37 /* type definitions */
38 typedef void (*init_ptr) ();
39 typedef void (*rts_init_ptr) (int *argc, char *argv []);
40
41 typedef struct INIT_LIST
42 {
43   init_ptr             code;
44   struct INIT_LIST     *forward;
45 } InitList;
46
47 InitList    *_ch_init_list = 0;
48
49 /* force linker to get correct RTS functions */
50 extern rts_init_ptr     __RTS_INIT__;
51 extern init_ptr         __RTS_MAIN_LOOP__;
52 extern init_ptr         __RTS_FETCH_NUMBERS__;
53 extern init_ptr         __RTS_FETCH_NAMES__;
54 static init_ptr         *rts_dummies[4] = 
55 {
56   &__RTS_INIT__,
57   &__RTS_MAIN_LOOP__,
58   &__RTS_FETCH_NUMBERS__,
59   &__RTS_FETCH_NAMES__,
60 };
61
62 /* chill argc and argv */
63 int                     chill_argc = 0;
64 TVaryingCharType        **chill_argv = NULL;
65
66 /* the program name for debugging purpose */
67 char                    *progname = 0;
68 \f
69 extern void *__xmalloc_ ();
70
71 /*
72  * function __xrealloc_
73  *
74  * parameter:
75  *   ptr                pointer to reallocate
76  *   size               new number of bytes
77  *
78  * returns:
79  *  void*
80  *
81  * abstract:
82  *  This is the general reallocation routine for libchill
83  *
84  */
85
86 void *
87 __xrealloc_ (ptr, size)
88 void *ptr;
89 int size;
90 {
91   void  *tmp = realloc (ptr, size);
92   
93   if (!tmp)
94     {
95       fprintf (stderr, "ChillLib: Out of heap space.\n");
96       fflush (stderr);
97       exit (ENOMEM);
98     }
99   return (tmp);
100 } /* __xrealloc_ */
101 \f
102 static void
103 setup_argc_argv (argc, argv)
104 int argc;
105 char *argv[];
106 {
107   int           i;
108   
109   chill_argv = __xmalloc_ ((argc + 1) * sizeof (TVaryingCharType *));
110   for (i = 0; i < argc; i++)
111     {
112       chill_argv[i] = __xmalloc_ (sizeof (TVaryingCharType) + strlen (argv[i]) + 1);
113       chill_argv[i]->len = strlen (argv[i]);
114       strcpy (chill_argv[i]->body, argv[i]);
115     }
116   chill_argv[chill_argc = argc] = NULL;
117   
118   if ((progname = strrchr (argv[0], '/')) == 0)
119     progname = argv[0];
120   else
121     progname++;
122   
123 } /* setup_argc_argv */
124
125 extern void __setexceptionStack ();
126
127 /*--------- main entry for each CHILL - program ----------*/
128 int
129 main (argc, argv)
130      int argc;
131      char *argv [];
132 {
133   /* call look up for tasking */
134   (*__RTS_INIT__) (&argc, argv);
135
136   /* setup argc and argv */
137   setup_argc_argv (argc, argv);
138
139   /* clear exception stack */
140   __setexceptionStack (0);
141
142   /* now call code at module level */
143   while (_ch_init_list)
144     {
145       if (_ch_init_list->code)
146         (*(_ch_init_list->code)) ();
147       _ch_init_list = _ch_init_list->forward;
148     }
149
150   /* if we have rts linked, something will be done, else just return */
151   (*__RTS_MAIN_LOOP__) ();
152   
153   return (0);
154   
155 } /* main */