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