[project @ 2000-03-08 17:48:24 by simonmar]
[ghc-hetmet.git] / ghc / rts / StgStartup.hc
1 /* -----------------------------------------------------------------------------
2  * $Id: StgStartup.hc,v 1.6 2000/03/08 17:48:24 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Code for starting, stopping and restarting threads.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #include "Rts.h"
11 #include "StgRun.h" /* StgReturn */
12 #include "StgStartup.h"
13
14 /*
15  * This module contains the two entry points and the final exit point
16  * to/from the Haskell world.  We can enter either by:
17  *
18  *   a) returning to the address on the top of the stack, or
19  *   b) entering the closure on the top of the stack
20  *
21  * the function stg_stop_thread_entry is the final exit for a
22  * thread: it is the last return address on the stack.  It returns
23  * to the scheduler marking the thread as finished.
24  */
25
26 #define CHECK_SENSIBLE_REGS() \
27     ASSERT(Hp != (P_)0);                        \
28     ASSERT(Sp != (P_)0);                        \
29     ASSERT(Su != (StgUpdateFrame *)0);          \
30     ASSERT(SpLim != (P_)0);                     \
31     ASSERT(HpLim != (P_)0);                     \
32     ASSERT(Sp <= (P_)Su);                       \
33     ASSERT(SpLim - RESERVED_STACK_WORDS <= Sp); \
34     ASSERT(HpLim >= Hp);
35
36 /* -----------------------------------------------------------------------------
37    Returning from the STG world.
38
39    This is a polymorphic return address, meaning that any old constructor
40    can be returned, we don't care (actually, it's probably going to be
41    an IOok constructor, which will indirect through the vector table
42    slot 0).
43    -------------------------------------------------------------------------- */
44
45 EXTFUN(stg_stop_thread_entry);
46
47 #ifdef PROFILING
48 #define STOP_THREAD_BITMAP 1
49 #else
50 #define STOP_THREAD_BITMAP 0
51 #endif
52
53 /* VEC_POLY_INFO expects to see these names - but they should all be the same. */
54 #define stg_stop_thread_0_entry stg_stop_thread_entry 
55 #define stg_stop_thread_1_entry stg_stop_thread_entry 
56 #define stg_stop_thread_2_entry stg_stop_thread_entry 
57 #define stg_stop_thread_3_entry stg_stop_thread_entry 
58 #define stg_stop_thread_4_entry stg_stop_thread_entry 
59 #define stg_stop_thread_5_entry stg_stop_thread_entry 
60 #define stg_stop_thread_6_entry stg_stop_thread_entry 
61 #define stg_stop_thread_7_entry stg_stop_thread_entry 
62
63 VEC_POLY_INFO_TABLE(stg_stop_thread,STOP_THREAD_BITMAP,0,0,0,STOP_FRAME,,EF_);
64
65 STGFUN(stg_stop_thread_entry)
66 {
67     FB_
68
69     /* 
70      * The final exit.
71      *
72      * The top-top-level closures (e.g., "main") are of type "IO a".
73      * When entered, they perform an IO action and return an 'a' in R1.
74      *
75      * We save R1 on top of the stack where the scheduler can find it,
76      * tidy up the registers and return to the scheduler.
77     */
78
79     /* Move Su just off the end of the stack, we're about to spam the
80      * STOP_FRAME with the return value.
81      */
82     Su = (StgUpdateFrame *)(Sp+1);  
83     Sp[0] = R1.w;
84
85     SaveThreadState();  /* inline! */
86
87     /* R1 contains the return value of the thread */
88     R1.p = (P_)ThreadFinished;
89
90     JMP_(StgReturn);
91     FE_
92 }
93
94 /* -----------------------------------------------------------------------------
95    Start a thread from the scheduler by returning to the address on
96    the top of the stack  (and popping the address).  This is used for
97    returning to the slow entry point of a function after a garbage collection
98    or re-schedule.  The slow entry point expects the stack to contain the
99    pending arguments only.
100    -------------------------------------------------------------------------- */
101
102 STGFUN(stg_returnToStackTop)
103 {
104   FB_
105   LoadThreadState();
106   CHECK_SENSIBLE_REGS();
107   Sp++;
108   JMP_(ENTRY_CODE(Sp[-1]));
109   FE_
110 }
111
112 /* -----------------------------------------------------------------------------
113    Start a thread from the scheduler by entering the closure pointed
114    to by the word on the top of the stack.
115    -------------------------------------------------------------------------- */
116
117 STGFUN(stg_enterStackTop)
118 {
119   FB_
120   LoadThreadState();
121   CHECK_SENSIBLE_REGS();
122   /* don't count this enter for ticky-ticky profiling */
123   R1.p = (P_)Sp[0];
124   Sp++;
125   JMP_(GET_ENTRY(R1.cl));
126   FE_
127 }
128
129   
130 /* -----------------------------------------------------------------------------
131    Special STG entry points for module registration.
132    -------------------------------------------------------------------------- */
133
134 STGFUN(stg_init_ret)
135 {
136   FB_
137   JMP_(StgReturn);
138   FE_
139 }
140
141 STGFUN(stg_init)
142 {
143   EF_(__init_Main);
144   EF_(__init_Prelude);
145   FB_
146   PUSH_INIT_STACK(stg_init_ret);
147   PUSH_INIT_STACK(__init_Prelude);
148   JMP_(__init_Main);
149   FE_
150 }
151
152 /* PrelGHC doesn't really exist... */
153
154 START_MOD_INIT(__init_PrelGHC);
155 END_MOD_INIT();