[project @ 1999-12-10 15:59:41 by sewardj]
[ghc-hetmet.git] / ghc / interpreter / codegen.c
1
2 /* --------------------------------------------------------------------------
3  * Code generator
4  *
5  * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
6  * Yale Haskell Group, and the Oregon Graduate Institute of Science and
7  * Technology, 1994-1999, All rights reserved.  It is distributed as
8  * free software under the license in the file "License", which is
9  * included in the distribution.
10  *
11  * $RCSfile: codegen.c,v $
12  * $Revision: 1.14 $
13  * $Date: 1999/12/10 15:59:41 $
14  * ------------------------------------------------------------------------*/
15
16 #include "prelude.h"
17 #include "storage.h"
18 #include "backend.h"
19 #include "connect.h"
20 #include "errors.h"
21 #include "Assembler.h"
22 #include "link.h"
23
24 #include "Rts.h"    /* IF_DEBUG */
25 #include "RtsFlags.h"
26
27 /* --------------------------------------------------------------------------
28  * Local function prototypes:
29  * ------------------------------------------------------------------------*/
30
31 #define getPos(v)     intOf(stgVarInfo(v))
32 #define setPos(v,sp)  stgVarInfo(v) = mkInt(sp)
33 #define getObj(v)     ptrOf(stgVarInfo(v))
34 #define setObj(v,obj) stgVarInfo(v) = mkPtr(obj)
35
36 #define repOf(x)      charOf(stgVarRep(x))
37
38 static void  cgBind        ( AsmBCO bco, StgVar v );
39 static Void  pushVar       ( AsmBCO bco, StgVar v );
40 static Void  pushAtom      ( AsmBCO bco, StgAtom atom );
41 static Void  alloc         ( AsmBCO bco, StgRhs rhs );
42 static Void  build         ( AsmBCO bco, StgRhs rhs );
43 static Void  cgExpr        ( AsmBCO bco, AsmSp root, StgExpr e );
44              
45 static AsmBCO cgAlts       ( AsmSp root, AsmSp sp, List alts );
46 static void   testPrimPats ( AsmBCO bco, AsmSp root, List pats, StgExpr e );
47 //static void   cgPrimAlt    ( AsmBCO bco, AsmSp root, List vs, StgExpr e );
48 static AsmBCO cgLambda     ( StgExpr e );
49 static AsmBCO cgRhs        ( StgRhs rhs );
50 static void   beginTop     ( StgVar v );
51 static void   endTop       ( StgVar v );
52
53 static StgVar currentTop;
54
55 /* --------------------------------------------------------------------------
56  * 
57  * ------------------------------------------------------------------------*/
58
59 static Cell cptrFromName ( Name n )
60 {
61    char  buf[1000];
62    void* p;
63    Module m = name(n).mod;
64    Text  mt = module(m).text;
65    sprintf(buf,"%s_%s_closure", 
66                textToStr(mt), 
67                textToStr( enZcodeThenFindText ( textToStr (name(n).text) ) ) );
68    p = lookupOTabName ( m, buf );
69    if (!p) {
70       ERRMSG(0) "Can't find object symbol %s", buf
71       EEND;
72    }
73    return mkCPtr(p);
74 }
75
76 static Bool varHasClosure( StgVar v )
77 {
78     return asmObjectHasClosure((AsmClosure*)ptrOf(stgVarInfo(v)));
79 }
80
81 /* should be AsmClosure* */
82 void* closureOfVar( StgVar v )
83 {
84     return asmClosureOfObject((AsmClosure*)ptrOf(stgVarInfo(v)));
85 }
86
87 char* lookupHugsName( void* closure )
88 {
89     extern Name nameHw;
90     Name nm;
91     for( nm=NAMEMIN; nm<nameHw; ++nm ) {
92         StgVar v  = name(nm).stgVar;
93         if (isStgVar(v) 
94             && isPtr(stgVarInfo(v)) 
95             && varHasClosure(v)
96             && closureOfVar(v) == closure) {
97             return textToStr(name(nm).text);
98         }
99     }
100     return 0;
101 }
102
103 /* called at the start of GC */
104 void markHugsObjects( void )
105 {
106     extern Name nameHw;
107     Name nm;
108     for( nm=NAMEMIN; nm<nameHw; ++nm ) {
109         StgVar v  = name(nm).stgVar;
110         if (isStgVar(v) && isPtr(stgVarInfo(v))) {
111             asmMarkObject((AsmClosure*)ptrOf(stgVarInfo(v)));
112         }
113     }
114 }
115
116 static void cgBindRep( AsmBCO bco, StgVar v, AsmRep rep )
117 {
118     setPos(v,asmBind(bco,rep));
119 }
120
121 static void cgBind( AsmBCO bco, StgVar v )
122 {
123     cgBindRep(bco,v,repOf(v));
124 }
125
126 static Void pushVar( AsmBCO bco, StgVar v )
127 {
128     Cell info;
129
130     if (!(isStgVar(v) || isCPtr(v))) {
131     assert(isStgVar(v) || isCPtr(v));
132     }
133
134     if (isCPtr(v)) {
135        asmGHCClosure(bco, cptrOf(v));
136     } else {
137        info = stgVarInfo(v);
138        if (isPtr(info)) {
139            asmClosure(bco,ptrOf(info));
140        } else if (isInt(info)) {
141            asmVar(bco,intOf(info),repOf(v));
142        } else {
143            internal("pushVar");
144        }        
145     }
146 }
147
148 static Void pushAtom( AsmBCO bco, StgAtom e )
149 {
150     switch (whatIs(e)) {
151     case STGVAR: 
152             pushVar(bco,e);
153             break;
154     case NAME: 
155             if (nonNull(name(e).stgVar))
156                pushVar(bco,name(e).stgVar); else
157                pushVar(bco,cptrFromName(e));
158             break;
159     case CHARCELL: 
160             asmConstChar(bco,charOf(e));
161             break;
162     case INTCELL: 
163             asmConstInt(bco,intOf(e));
164             break;
165     case BIGCELL:
166             asmConstInteger(bco,bignumToString(e)); 
167             break;
168     case FLOATCELL: 
169             asmConstDouble(bco,floatOf(e));
170             break;
171     case STRCELL: 
172 #if USE_ADDR_FOR_STRINGS
173             asmConstAddr(bco,textToStr(textOf(e)));
174 #else
175             asmClosure(bco,asmStringObj(textToStr(textOf(e))));
176 #endif
177             break;
178     case CPTRCELL:
179             asmGHCClosure(bco,cptrOf(e));
180             break;
181     case PTRCELL: 
182             asmConstAddr(bco,ptrOf(e));
183             break;
184     default: 
185             fprintf(stderr,"\nYoiks: "); printExp(stderr,e);
186             internal("pushAtom");
187     }
188 }
189
190 static AsmBCO cgAlts( AsmSp root, AsmSp sp, List alts )
191 {
192 #ifdef CRUDE_PROFILING
193     AsmBCO bco = asmBeginContinuation(sp, currentTop + 1000000000);
194 #else
195     AsmBCO bco = asmBeginContinuation(sp, alts);
196 #endif
197     Bool omit_test
198        = length(alts) == 2 &&
199          isDefaultAlt(hd(tl(alts))) &&
200          !isDefaultAlt(hd(alts));
201     if (omit_test) {
202        /* refine the condition */              
203        Name con;
204        Tycon t;
205        omit_test = FALSE;
206        con = stgCaseAltCon(hd(alts));
207
208        /* special case: dictionary constructors */
209        if (strncmp(":D",textToStr(name(con).text),2)==0) {
210           omit_test = TRUE;
211           goto xyzzy;
212        }
213        /* special case: Tuples */
214        if (isTuple(con) || (isName(con) && con==nameUnit)) {
215           omit_test = TRUE;
216           goto xyzzy;
217        }          
218
219        t = name(con).parent;
220        if (tycon(t).what == DATATYPE) {
221           if (length(tycon(t).defn) == 1) omit_test = TRUE;
222        }
223     }
224
225     xyzzy:
226
227     for(; nonNull(alts); alts=tl(alts)) {
228         StgCaseAlt alt  = hd(alts);
229         if (isDefaultAlt(alt)) {
230             cgBind(bco,stgDefaultVar(alt));
231             cgExpr(bco,root,stgDefaultBody(alt));
232             asmEndContinuation(bco);
233             return bco; /* ignore any further alternatives */
234         } else {
235             StgDiscr con   = stgCaseAltCon(alt);
236             List     vs    = stgCaseAltVars(alt);
237             AsmSp    begin = asmBeginAlt(bco);
238             AsmPc    fix;
239             if (omit_test) fix=-1; else fix = asmTest(bco,stgDiscrTag(con)); 
240
241             asmBind(bco,PTR_REP); /* Adjust simulated sp to acct for return value */
242             if (isBoxingCon(con)) {
243                 setPos(hd(vs),asmUnbox(bco,boxingConRep(con)));
244             } else {
245                 asmBeginUnpack(bco);
246                 map1Proc(cgBind,bco,reverse(vs));
247                 asmEndUnpack(bco);
248             }
249             cgExpr(bco,root,stgCaseAltBody(alt));
250             asmEndAlt(bco,begin);
251             if (fix != -1) asmFixBranch(bco,fix);
252         }
253     }
254     /* if we got this far and didn't match, panic! */
255     asmPanic(bco);
256     asmEndContinuation(bco);
257     return bco;
258 }
259
260 static void testPrimPats( AsmBCO bco, AsmSp root, List pats, StgExpr e )
261 {
262     if (isNull(pats)) {
263         cgExpr(bco,root,e);
264     } else {
265         StgVar pat = hd(pats);
266         if (isInt(stgVarBody(pat))) {
267             /* asmTestInt leaves stack unchanged - so no need to adjust it */
268             AsmPc tst = asmTestInt(bco,getPos(pat),intOf(stgVarBody(pat)));
269             assert(repOf(pat) == INT_REP);
270             testPrimPats(bco,root,tl(pats),e);
271             asmFixBranch(bco,tst);
272         } else {
273             testPrimPats(bco,root,tl(pats),e);
274         }
275     }
276 }
277
278 #if 0  /* appears to be unused */
279 static void cgPrimAlt( AsmBCO bco, AsmSp root, List vs, StgExpr e )
280 {
281     assert(0); /* ToDo: test for patterns */
282     map1Proc(cgBind,bco,vs); /* ToDo: are these in right order? */
283     cgExpr(bco,root,e);
284 }
285 #endif
286
287
288 static AsmBCO cgLambda( StgExpr e )
289 {
290     AsmBCO bco = asmBeginBCO(e);
291
292     AsmSp root = asmBeginArgCheck(bco);
293     map1Proc(cgBind,bco,reverse(stgLambdaArgs(e)));
294     asmEndArgCheck(bco,root);
295
296     /* ppStgExpr(e); */
297     cgExpr(bco,root,stgLambdaBody(e));
298
299     asmEndBCO(bco);
300     return bco;
301 }
302
303 static AsmBCO cgRhs( StgRhs rhs )
304 {
305     AsmBCO bco = asmBeginBCO(rhs );
306
307     AsmSp root = asmBeginArgCheck(bco);
308     asmEndArgCheck(bco,root);
309
310     /* ppStgExpr(rhs); */
311     cgExpr(bco,root,rhs);
312
313     asmEndBCO(bco);
314     return bco;
315 }
316
317
318 static Void cgExpr( AsmBCO bco, AsmSp root, StgExpr e )
319 {
320   //printf("cgExpr:");ppStgExpr(e);printf("\n");
321     switch (whatIs(e)) {
322     case LETREC:
323         {
324             List binds = stgLetBinds(e);
325             map1Proc(alloc,bco,binds);
326             map1Proc(build,bco,binds);
327             cgExpr(bco,root,stgLetBody(e));
328             break;
329         }
330     case LAMBDA:
331         {
332             AsmSp begin = asmBeginEnter(bco);
333             asmClosure(bco,cgLambda(e));
334             asmEndEnter(bco,begin,root);
335             break;
336         }
337     case CASE:
338         {
339             List  alts     = stgCaseAlts(e);
340             AsmSp sp       = asmBeginCase(bco);
341             AsmSp caseroot = asmContinuation(bco,cgAlts(root,sp,alts));
342             cgExpr(bco,caseroot,stgCaseScrut(e));
343             asmEndCase(bco);
344             break;
345         }
346     case PRIMCASE:
347         {
348             StgExpr scrut = stgPrimCaseScrut(e);
349             List alts = stgPrimCaseAlts(e);
350             if (whatIs(scrut) == STGPRIM) {  /* this is an optimisation */
351
352                 /* No need to use return address or to Slide */
353                 AsmSp beginPrim = asmBeginPrim(bco);
354                 map1Proc(pushAtom,bco,reverse(stgPrimArgs(scrut)));
355                 asmEndPrim(bco,(AsmPrim*)name(stgPrimOp(scrut)).primop,beginPrim);
356
357                 for(; nonNull(alts); alts=tl(alts)) {
358                     StgPrimAlt alt = hd(alts);
359                     List    pats = stgPrimAltVars(alt);
360                     StgExpr body = stgPrimAltBody(alt);
361                     AsmSp altBegin = asmBeginAlt(bco);
362                     map1Proc(cgBind,bco,reverse(pats));
363                     testPrimPats(bco,root,pats,body);
364                     asmEndAlt(bco,altBegin);
365                 }
366                 /* if we got this far and didn't match, panic! */
367                 asmPanic(bco);
368                 
369             } else if (whatIs(scrut) == STGVAR) { /* another optimisation */
370
371                 /* No need to use return address or to Slide */
372
373                 /* only part different from primop code... todo */
374                 AsmSp beginCase = asmBeginCase(bco);
375                 pushVar(bco,scrut);
376                 asmEndAlt(bco,beginCase); /* hack, hack -  */
377
378                 for(; nonNull(alts); alts=tl(alts)) {
379                     StgPrimAlt alt = hd(alts);
380                     List    pats = stgPrimAltVars(alt);
381                     StgExpr body = stgPrimAltBody(alt);
382                     AsmSp altBegin = asmBeginAlt(bco);
383                     map1Proc(cgBind,bco,pats);
384                     testPrimPats(bco,root,pats,body);
385                     asmEndAlt(bco,altBegin);
386                 }
387                 /* if we got this far and didn't match, panic! */
388                 asmPanic(bco);
389                                 
390             } else {
391                 /* ToDo: implement this code...  */
392                 assert(0);
393                 /* asmPushRet(bco,delayPrimAlt( stgPrimCaseVars(e), 
394                                                 stgPrimCaseBody(e))); */
395                 /* cgExpr( bco,root,scrut ); */
396             }
397             break;
398         }
399     case STGAPP: /* Tail call */
400         {
401             AsmSp env = asmBeginEnter(bco);
402             map1Proc(pushAtom,bco,reverse(stgAppArgs(e)));
403             pushAtom(bco,stgAppFun(e));
404             asmEndEnter(bco,env,root);
405             break;
406         }
407     case NAME: /* Tail call (with no args) */
408         {
409             AsmSp env = asmBeginEnter(bco);
410             pushVar(bco,name(e).stgVar);
411             asmEndEnter(bco,env,root);
412             break;
413         }
414     case STGVAR: /* Tail call (with no args), plus unboxed return */
415             switch (repOf(e)) {
416             case PTR_REP:
417             case ALPHA_REP:
418             case BETA_REP:
419                 {
420                     AsmSp env = asmBeginEnter(bco);
421                     pushVar(bco,e);
422                     asmEndEnter(bco,env,root);
423                     break;
424                 }
425             case INT_REP:
426                     assert(0);
427                     /* cgTailCall(bco,singleton(e)); */
428                     /* asmReturnInt(bco); */
429                     break;
430             default:
431                     internal("cgExpr StgVar");
432             }
433             break;
434     case STGPRIM: /* Tail call again */
435         {
436             AsmSp beginPrim = asmBeginPrim(bco);
437             map1Proc(pushAtom,bco,reverse(stgPrimArgs(e)));
438             asmEndPrim(bco,(AsmPrim*)name(e).primop,beginPrim);
439             /* map1Proc(cgBind,bco,rs_vars); */
440             assert(0); /* asmReturn_retty(); */
441             break;
442         }
443     default:
444             fprintf(stderr,"\nYoiks: "); printExp(stderr,e);
445             internal("cgExpr");
446     }
447 }
448
449 #define M_ITBLNAMES 35000
450
451 void* itblNames[M_ITBLNAMES];
452 int   nItblNames = 0;
453
454 /* allocate space for top level variable
455  * any change requires a corresponding change in 'build'.
456  */
457 static Void alloc( AsmBCO bco, StgVar v )
458 {
459     StgRhs rhs = stgVarBody(v);
460     assert(isStgVar(v));
461     switch (whatIs(rhs)) {
462     case STGCON:
463         {
464             StgDiscr con  = stgConCon(rhs);
465             List     args = stgConArgs(rhs);
466             if (isBoxingCon(con)) {
467                 pushAtom(bco,hd(args));
468                 setPos(v,asmBox(bco,boxingConRep(con)));
469             } else {
470
471                 void* vv = stgConInfo(con);
472                 if (!(nItblNames < (M_ITBLNAMES-2))) 
473                    internal("alloc -- M_ITBLNAMES too small");
474                 if (isName(con)) {
475                    itblNames[nItblNames++] = vv;
476                    itblNames[nItblNames++] = textToStr(name(con).text);
477                 } else
478                 if (isTuple(con)) {
479                    itblNames[nItblNames++] = vv;
480                    itblNames[nItblNames++] = textToStr(ghcTupleText(con));
481                 } else
482                 assert ( /* cant identify constructor name */ 0 );
483                 setPos(v,asmAllocCONSTR(bco, vv));
484             }
485             break;
486         }
487     case STGAPP: {
488             Int  totSizeW = 0;
489             List bs       = stgAppArgs(rhs);
490             for (; nonNull(bs); bs=tl(bs)) {
491                if (isName(hd(bs))) {
492                   totSizeW += 1;
493                } else {
494                   ASSERT(whatIs(hd(bs))==STGVAR);
495                   totSizeW += asmRepSizeW( charOf(stgVarRep(hd(bs))) );
496                }
497             }
498             setPos(v,asmAllocAP(bco,totSizeW));
499             //ORIGINALLY:setPos(v,asmAllocAP(bco,length(stgAppArgs(rhs))));
500             break;
501          }
502     case LAMBDA: /* optimisation */
503             setObj(v,cgLambda(rhs));
504             break;
505     default: 
506             setPos(v,asmAllocAP(bco,0));
507             break;
508     }
509 }
510
511 static Void build( AsmBCO bco, StgVar v )
512 {
513     StgRhs rhs = stgVarBody(v);
514     assert(isStgVar(v));
515
516     switch (whatIs(rhs)) {
517     case STGCON:
518         {
519             StgDiscr con  = stgConCon(rhs);
520             List     args = stgConArgs(rhs);
521             if (isBoxingCon(con)) {
522                 doNothing();  /* already done in alloc */
523             } else {
524                 AsmSp start = asmBeginPack(bco);
525                 map1Proc(pushAtom,bco,reverse(args));
526                 asmEndPack(bco,getPos(v),start,stgConInfo(con));
527             }
528             return;
529         }
530     case STGAPP: 
531         {
532             Bool   itsaPAP;
533             StgVar fun  = stgAppFun(rhs);
534             StgVar fun0 = fun;
535             List   args = stgAppArgs(rhs);
536             if (isName(fun)) {
537                 if (nonNull(name(fun).stgVar))
538                    fun = name(fun).stgVar; else
539                    fun = cptrFromName(fun);
540             }
541
542             if (isCPtr(fun)) {
543                assert(isName(fun0));
544                itsaPAP = name(fun0).arity > length(args);
545 fprintf ( stderr, "nativeCall: name %s, arity %d, args %d\n",
546                nameFromOPtr(cptrOf(fun)), name(fun0).arity, length(args) );
547             } else {
548                itsaPAP = FALSE;
549                if (nonNull(stgVarBody(fun))
550                    && whatIs(stgVarBody(fun)) == LAMBDA 
551                    && length(stgLambdaArgs(stgVarBody(fun))) > length(args)
552                   )
553                   itsaPAP = TRUE;
554             }
555
556             if (itsaPAP) {
557                 AsmSp  start = asmBeginMkPAP(bco);
558                 map1Proc(pushAtom,bco,reverse(args));
559                 pushAtom(bco,fun);
560                 asmEndMkPAP(bco,getPos(v),start); /* optimisation */
561             } else {
562                 AsmSp  start = asmBeginMkAP(bco);
563                 map1Proc(pushAtom,bco,reverse(args));
564                 pushAtom(bco,fun);
565                 asmEndMkAP(bco,getPos(v),start);
566             }
567             return;
568         }
569     case LAMBDA: /* optimisation */
570             doNothing(); /* already pushed in alloc */
571             break;
572
573     /* These two cases look almost identical to the default but they're really
574      * special cases of STGAPP.  The essential thing here is that we can't call
575      * cgRhs(rhs) because that expects the rhs to have no free variables when, 
576      * in fact, the rhs is _always_ a free variable.
577      *
578      * ToDo: a simple optimiser would eliminate all examples
579      * of this except "let x = x in ..."
580      */
581     case NAME:
582             rhs = name(rhs).stgVar;
583     case STGVAR:
584         {
585             AsmSp  start = asmBeginMkAP(bco);
586             pushAtom(bco,rhs);
587             asmEndMkAP(bco,getPos(v),start);
588         }
589         return;
590     default:
591         {
592             AsmSp start = asmBeginMkAP(bco);   /* make it updateable! */
593             asmClosure(bco,cgRhs(rhs));
594             asmEndMkAP(bco,getPos(v),start);
595             return;
596         }
597     }
598 }
599
600 /* --------------------------------------------------------------------------
601  * Top level variables
602  *
603  * ToDo: these should be handled by allocating a dynamic unentered CAF
604  * for each top level variable - this should be simpler!
605  * ------------------------------------------------------------------------*/
606
607 #if 0   /* appears to be unused */
608 static void cgAddVar( AsmObject obj, StgAtom v )
609 {
610     if (isName(v)) {
611         v = name(v).stgVar;
612     }
613     assert(isStgVar(v));
614     asmAddPtr(obj,getObj(v));
615 }
616 #endif
617
618
619 /* allocate AsmObject for top level variables
620  * any change requires a corresponding change in endTop
621  */
622 static void beginTop( StgVar v )
623 {
624     StgRhs rhs;
625     assert(isStgVar(v));
626     currentTop = v;
627     rhs = stgVarBody(v);
628     switch (whatIs(rhs)) {
629     case STGCON:
630         {
631             //List as = stgConArgs(rhs);
632             setObj(v,asmBeginCon(stgConInfo(stgConCon(rhs))));
633             break;
634         }
635     case LAMBDA:
636 #ifdef CRUDE_PROFILING
637             setObj(v,asmBeginBCO(currentTop));
638 #else
639             setObj(v,asmBeginBCO(rhs));
640 #endif
641             break;
642     default:
643             setObj(v,asmBeginCAF());
644             break;
645     }
646 }
647
648 static void endTop( StgVar v )
649 {
650     StgRhs rhs = stgVarBody(v);
651     currentTop = v;
652     switch (whatIs(rhs)) {
653     case STGCON:
654         {
655             List as = stgConArgs(rhs);
656             AsmCon con = (AsmCon)getObj(v);
657             for( ; nonNull(as); as=tl(as)) {
658                 StgAtom a = hd(as);
659                 switch (whatIs(a)) {
660                 case STGVAR: 
661                         /* should be a delayed combinator! */
662                         asmAddPtr(con,(AsmObject)getObj(a));
663                         break;
664                 case NAME: 
665                     {
666                         StgVar var = name(a).stgVar;
667                         assert(var);
668                         asmAddPtr(con,(AsmObject)getObj(a));
669                         break;
670                     }
671 #if !USE_ADDR_FOR_STRINGS
672                 case STRCELL:
673                         asmAddPtr(con,asmStringObj(textToStr(textOf(a))));
674                         break;
675 #endif
676                 default: 
677                         /* asmAddPtr(con,??); */
678                         assert(0);
679                         break;
680                 }
681             }
682             asmEndCon(con);
683             break;
684         }
685     case LAMBDA: /* optimisation */
686         {
687             /* ToDo: merge this code with cgLambda */
688             AsmBCO bco = (AsmBCO)getObj(v);
689             AsmSp root = asmBeginArgCheck(bco);
690             map1Proc(cgBind,bco,reverse(stgLambdaArgs(rhs)));
691             asmEndArgCheck(bco,root);
692             
693             cgExpr(bco,root,stgLambdaBody(rhs));
694             
695             asmEndBCO(bco);
696             break;
697         }
698     default:   /* updateable caf */
699         {
700             AsmCAF caf = (AsmCAF)getObj(v);
701             asmEndCAF(caf,cgRhs(rhs));
702             break;
703         }
704     }
705 }
706
707 static void zap( StgVar v )
708 {
709   // ToDo: reinstate
710   //    stgVarBody(v) = NIL;
711 }
712
713 /* external entry point */
714 Void cgBinds( List binds )
715 {
716     List b;
717     int i;
718
719 #if 0
720     if (lastModule() != modulePrelude) {
721         printf("\n\ncgBinds: before ll\n\n" );
722         for (b=binds; nonNull(b); b=tl(b)) {
723            printStg ( stdout, hd(b) ); printf("\n\n");
724         }
725     }
726 #endif
727
728     binds = liftBinds(binds);
729
730 #if 0
731     if (lastModule() != modulePrelude) {
732         printf("\n\ncgBinds: after ll\n\n" );
733         for (b=binds; nonNull(b); b=tl(b)) {
734            printStg ( stdout, hd(b) ); printf("\n\n");
735         }
736     }
737 #endif
738
739     for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
740        beginTop(hd(b));
741     }
742
743     for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
744        //printStg( stdout, hd(b) ); printf( "\n\n");
745        endTop(hd(b));
746     }
747
748     //mapProc(zap,binds);
749 }
750
751 /* --------------------------------------------------------------------------
752  * Code Generator control:
753  * ------------------------------------------------------------------------*/
754
755 Void codegen(what)
756 Int what; {
757     switch (what) {
758        case PREPREL:
759        case RESET: 
760        case MARK: 
761        case POSTPREL:
762           break;
763     }
764     liftControl(what);
765 }
766
767 /*-------------------------------------------------------------------------*/