Updated PCI IDs to latest snapshot.
[tangerine.git] / compiler / startup / startup.c
blobebe30176a7ec64dfa69950514e0c089cf01e39e5
2 /*
3 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
4 $Id$
6 Desc: Common startup code
7 Lang: english
8 */
9 #define DEBUG 0
11 #include <aros/config.h>
12 #include <setjmp.h>
13 #include <dos/dos.h>
14 #include <exec/memory.h>
15 #include <workbench/startup.h>
16 #include <proto/exec.h>
17 #include <proto/dos.h>
18 #include <aros/asmcall.h>
19 #include <aros/debug.h>
20 #include <aros/symbolsets.h>
21 #include <aros/startup.h>
23 #include "etask.h"
25 THIS_PROGRAM_HANDLES_SYMBOLSETS
27 /* Don't define symbols before the entry point. */
28 extern struct ExecBase *SysBase;
29 extern struct WBStartup *WBenchMsg;
30 extern int main(int argc, char ** argv);
31 int (*__main_function_ptr)(int argc, char ** argv) __attribute__((__weak__)) = main;
33 DECLARESET(INIT);
34 DECLARESET(EXIT);
35 DECLARESET(CTORS);
36 DECLARESET(DTORS);
37 DECLARESET(PROGRAM_ENTRIES);
40 This won't work for normal AmigaOS because you can't expect SysBase to be
41 in A6. The correct way is to use *(struct ExecBase **)4 and because GCC
42 emits strings for a certain function _before_ the code the program will
43 crash immediately because the first element in the code won't be valid
44 assembler code.
47 extern char *__argstr;
48 extern ULONG __argsize;
49 extern char **__argv;
50 extern int __argc;
51 extern char __stdiowin[];
52 extern struct DosLibrary *DOSBase;
54 static struct aros_startup __aros_startup;
56 AROS_UFP3(static LONG, __startup_entry,
57 AROS_UFHA(char *,argstr,A0),
58 AROS_UFHA(ULONG,argsize,D0),
59 AROS_UFHA(struct ExecBase *,sysbase,A6)
60 ) __attribute__((section(".aros.startup")));
62 #warning TODO: reset and initialize the FPU
63 #warning TODO: resident startup
64 AROS_UFH3(static LONG, __startup_entry,
65 AROS_UFHA(char *,argstr,A0),
66 AROS_UFHA(ULONG,argsize,D0),
67 AROS_UFHA(struct ExecBase *,sysbase,A6)
70 AROS_USERFUNC_INIT
72 struct Process *myproc;
73 BPTR win = NULL;
74 BPTR old_in, old_out, old_err;
76 SysBase = sysbase;
79 No one program will be able to do anything useful without the dos.library,
80 so we open it here instead of using the automatic opening system
82 DOSBase = (struct DosLibrary *)OpenLibrary(DOSNAME, 39);
83 if (!DOSBase) return RETURN_FAIL;
85 __argstr = argstr;
86 __argsize = argsize;
88 myproc = (struct Process *)FindTask(NULL);
90 GetIntETask(myproc)->iet_startup = &__aros_startup;
92 /* Do we have a CLI structure? */
93 if (!myproc->pr_CLI)
95 /* Workbench startup. Get WBenchMsg and pass it to main() */
97 WaitPort(&myproc->pr_MsgPort);
98 WBenchMsg = (struct WBStartup *)GetMsg(&myproc->pr_MsgPort);
99 __argv = (char **) WBenchMsg;
100 __argc = 0;
102 D(bug("[startup] Started from Workbench\n"));
103 if(__stdiowin[0]) {
104 D(bug("[startup] Opening console window: %s\n", __stdiowin));
105 win = Open(__stdiowin, MODE_OLDFILE);
106 if (win) {
107 D(bug("[startup] Success!\n"));
108 old_in = SelectInput(win);
109 old_out = SelectOutput(win);
110 old_err = SelectError(win);
115 __aros_startup.as_startup_error = RETURN_FAIL;
117 if (set_open_libraries())
121 setjmp(__aros_startup.as_startup_jmp_buf) == 0 &&
122 set_call_funcs(SETNAME(INIT), 1, 1)
125 /* ctors/dtors get called in inverse order than init funcs */
126 set_call_funcs(SETNAME(CTORS), -1, 0);
128 /* Invoke the main function. A weak symbol is used as function name so that
129 it can be overridden (for *nix stuff, for instance). */
130 __aros_startup.as_startup_error = (*__main_function_ptr) (__argc, __argv);
132 set_call_funcs(SETNAME(DTORS), 1, 0);
134 set_call_funcs(SETNAME(EXIT), -1, 0);
136 set_close_libraries();
139 /* Reply startup message to Workbench */
140 if (WBenchMsg)
142 Forbid(); /* make sure we're not UnLoadseg()ed before we're really done */
143 ReplyMsg((struct Message *) WBenchMsg);
146 if (win) {
147 SelectInput(old_in);
148 SelectOutput(old_out);
149 SelectError(old_err);
150 Close(win);
152 CloseLibrary((struct Library *)DOSBase);
154 return __aros_startup.as_startup_error;
156 AROS_USERFUNC_EXIT
157 } /* entry */
159 /* if the programmer hasn't defined a symbol with the name __nocommandline
160 then the code to handle the commandline will be included from the autoinit.lib
162 extern int __nocommandline;
163 asm(".set __importcommandline, __nocommandline");
165 /* pass these values to the command line handling function */
166 char *__argstr;
167 ULONG __argsize;
169 /* the command line handling functions will pass these values back to us */
170 char **__argv;
171 int __argc;
173 struct ExecBase *SysBase;
174 struct DosLibrary *DOSBase;
175 struct WBStartup *WBenchMsg;
177 DEFINESET(CTORS);
178 DEFINESET(DTORS);
179 DEFINESET(INIT);
180 DEFINESET(EXIT);
181 DEFINESET(PROGRAM_ENTRIES);
182 ADD2SET(__startup_entry, program_entries, 0);
186 Stub function for GCC __main().
188 The __main() function is originally used for C++ style constructors
189 and destructors in C. This replacement does nothing and gets rid of
190 linker-errors about references to __main().
192 #ifdef AROS_NEEDS___MAIN
193 void __main(void)
195 /* Do nothing. */
197 #endif