2 /* --------------------------------------------------------------------------
5 * Copyright (c) The University of Nottingham and Yale University, 1994-1997.
6 * All rights reserved. See NOTICE for details and conditions of use etc...
7 * Hugs version 1.4, December 1997
9 * $RCSfile: codegen.c,v $
11 * $Date: 1999/04/27 10:06:48 $
12 * ------------------------------------------------------------------------*/
19 #include "Assembler.h"
22 #include "Rts.h" /* IF_DEBUG */
25 /* --------------------------------------------------------------------------
26 * Local function prototypes:
27 * ------------------------------------------------------------------------*/
29 #define getPos(v) intOf(stgVarInfo(v))
30 #define setPos(v,sp) stgVarInfo(v) = mkInt(sp)
31 #define getObj(v) ptrOf(stgVarInfo(v))
32 #define setObj(v,obj) stgVarInfo(v) = mkPtr(obj)
34 #define repOf(x) charOf(stgVarRep(x))
36 static void cgBind ( AsmBCO bco, StgVar v );
37 static Void pushVar ( AsmBCO bco, StgVar v );
38 static Void pushAtom ( AsmBCO bco, StgAtom atom );
39 static Void alloc ( AsmBCO bco, StgRhs rhs );
40 static Void build ( AsmBCO bco, StgRhs rhs );
41 static Void cgExpr ( AsmBCO bco, AsmSp root, StgExpr e );
43 static AsmBCO cgAlts ( AsmSp root, AsmSp sp, List alts );
44 static void testPrimPats ( AsmBCO bco, AsmSp root, List pats, StgExpr e );
45 //static void cgPrimAlt ( AsmBCO bco, AsmSp root, List vs, StgExpr e );
46 static AsmBCO cgLambda ( StgExpr e );
47 static AsmBCO cgRhs ( StgRhs rhs );
48 static void beginTop ( StgVar v );
49 static void endTop ( StgVar v );
51 static StgVar currentTop;
53 /* --------------------------------------------------------------------------
55 * ------------------------------------------------------------------------*/
57 static Bool varHasClosure( StgVar v )
59 return asmObjectHasClosure((AsmClosure*)ptrOf(stgVarInfo(v)));
62 /* should be AsmClosure* */
63 void* closureOfVar( StgVar v )
65 return asmClosureOfObject((AsmClosure*)ptrOf(stgVarInfo(v)));
68 char* lookupHugsName( void* closure )
72 for( nm=NAMEMIN; nm<nameHw; ++nm ) {
73 StgVar v = name(nm).stgVar;
75 && isPtr(stgVarInfo(v))
77 && closureOfVar(v) == closure) {
78 return textToStr(name(nm).text);
84 /* called at the start of GC */
85 void markHugsObjects( void )
89 for( nm=NAMEMIN; nm<nameHw; ++nm ) {
90 StgVar v = name(nm).stgVar;
91 if (isStgVar(v) && isPtr(stgVarInfo(v))) {
92 asmMarkObject((AsmClosure*)ptrOf(stgVarInfo(v)));
97 static void cgBindRep( AsmBCO bco, StgVar v, AsmRep rep )
99 setPos(v,asmBind(bco,rep));
102 static void cgBind( AsmBCO bco, StgVar v )
104 cgBindRep(bco,v,repOf(v));
107 static Void pushVar( AsmBCO bco, StgVar v )
109 Cell info = stgVarInfo(v);
112 asmClosure(bco,ptrOf(info));
113 } else if (isInt(info)) {
114 asmVar(bco,intOf(info),repOf(v));
120 static Void pushAtom( AsmBCO bco, StgAtom e )
127 pushVar(bco,name(e).stgVar);
130 asmConstChar(bco,charOf(e));
133 asmConstInt(bco,intOf(e));
136 asmConstInteger(bco,bignumToString(e));
140 asmConstFloat(bco,e); /* ToDo: support both float and double! */
142 asmConstDouble(bco,floatOf(e));
147 asmConstDouble(bco,doubleOf(e));
151 #if USE_ADDR_FOR_STRINGS
152 asmConstAddr(bco,textToStr(textOf(e)));
154 asmClosure(bco,asmStringObj(textToStr(textOf(e))));
158 asmConstAddr(bco,ptrOf(e));
161 fprintf(stderr,"\nYoiks: "); printExp(stderr,e);
162 internal("pushAtom");
166 static AsmBCO cgAlts( AsmSp root, AsmSp sp, List alts )
168 #ifdef CRUDE_PROFILING
169 AsmBCO bco = asmBeginContinuation(sp, currentTop + 1000000000);
171 AsmBCO bco = asmBeginContinuation(sp, alts);
173 /* ppStgAlts(alts); */
174 for(; nonNull(alts); alts=tl(alts)) {
175 StgCaseAlt alt = hd(alts);
176 if (isDefaultAlt(alt)) {
177 cgBind(bco,stgDefaultVar(alt));
178 cgExpr(bco,root,stgDefaultBody(alt));
179 asmEndContinuation(bco);
180 return bco; /* ignore any further alternatives */
182 StgDiscr con = stgCaseAltCon(alt);
183 List vs = stgCaseAltVars(alt);
184 AsmSp begin = asmBeginAlt(bco);
185 AsmPc fix = asmTest(bco,stgDiscrTag(con));
186 /* ToDo: omit in single constructor types! */
187 asmBind(bco,PTR_REP); /* Adjust simulated sp to acct for return value */
188 if (isBoxingCon(con)) {
189 setPos(hd(vs),asmUnbox(bco,boxingConRep(con)));
192 map1Proc(cgBind,bco,reverse(vs));
195 cgExpr(bco,root,stgCaseAltBody(alt));
196 asmEndAlt(bco,begin);
197 asmFixBranch(bco,fix);
200 /* if we got this far and didn't match, panic! */
202 asmEndContinuation(bco);
206 static void testPrimPats( AsmBCO bco, AsmSp root, List pats, StgExpr e )
211 StgVar pat = hd(pats);
212 if (isInt(stgVarBody(pat))) {
213 /* asmTestInt leaves stack unchanged - so no need to adjust it */
214 AsmPc tst = asmTestInt(bco,getPos(pat),intOf(stgVarBody(pat)));
215 assert(repOf(pat) == INT_REP);
216 testPrimPats(bco,root,tl(pats),e);
217 asmFixBranch(bco,tst);
219 testPrimPats(bco,root,tl(pats),e);
224 #if 0 /* appears to be unused */
225 static void cgPrimAlt( AsmBCO bco, AsmSp root, List vs, StgExpr e )
227 assert(0); /* ToDo: test for patterns */
228 map1Proc(cgBind,bco,vs); /* ToDo: are these in right order? */
234 static AsmBCO cgLambda( StgExpr e )
236 AsmBCO bco = asmBeginBCO(e);
238 AsmSp root = asmBeginArgCheck(bco);
239 map1Proc(cgBind,bco,reverse(stgLambdaArgs(e)));
240 asmEndArgCheck(bco,root);
243 cgExpr(bco,root,stgLambdaBody(e));
249 static AsmBCO cgRhs( StgRhs rhs )
251 AsmBCO bco = asmBeginBCO(rhs );
253 AsmSp root = asmBeginArgCheck(bco);
254 asmEndArgCheck(bco,root);
256 /* ppStgExpr(rhs); */
257 cgExpr(bco,root,rhs);
264 static Void cgExpr( AsmBCO bco, AsmSp root, StgExpr e )
266 //printf("cgExpr:");ppStgExpr(e);printf("\n");
270 List binds = stgLetBinds(e);
271 map1Proc(alloc,bco,binds);
272 map1Proc(build,bco,binds);
273 cgExpr(bco,root,stgLetBody(e));
278 AsmSp begin = asmBeginEnter(bco);
279 asmClosure(bco,cgLambda(e));
280 asmEndEnter(bco,begin,root);
285 List alts = stgCaseAlts(e);
286 AsmSp sp = asmBeginCase(bco);
287 AsmSp caseroot = asmContinuation(bco,cgAlts(root,sp,alts));
288 cgExpr(bco,caseroot,stgCaseScrut(e));
294 StgExpr scrut = stgPrimCaseScrut(e);
295 List alts = stgPrimCaseAlts(e);
296 if (whatIs(scrut) == STGPRIM) { /* this is an optimisation */
298 /* No need to use return address or to Slide */
299 AsmSp beginPrim = asmBeginPrim(bco);
300 map1Proc(pushAtom,bco,reverse(stgPrimArgs(scrut)));
301 asmEndPrim(bco,(AsmPrim*)name(stgPrimOp(scrut)).primop,beginPrim);
303 for(; nonNull(alts); alts=tl(alts)) {
304 StgPrimAlt alt = hd(alts);
305 List pats = stgPrimAltVars(alt);
306 StgExpr body = stgPrimAltBody(alt);
307 AsmSp altBegin = asmBeginAlt(bco);
308 map1Proc(cgBind,bco,reverse(pats));
309 testPrimPats(bco,root,pats,body);
310 asmEndAlt(bco,altBegin);
312 /* if we got this far and didn't match, panic! */
315 } else if (whatIs(scrut) == STGVAR) { /* another optimisation */
317 /* No need to use return address or to Slide */
319 /* only part different from primop code... todo */
320 AsmSp beginCase = asmBeginCase(bco);
322 asmEndAlt(bco,beginCase); /* hack, hack - */
324 for(; nonNull(alts); alts=tl(alts)) {
325 StgPrimAlt alt = hd(alts);
326 List pats = stgPrimAltVars(alt);
327 StgExpr body = stgPrimAltBody(alt);
328 AsmSp altBegin = asmBeginAlt(bco);
329 map1Proc(cgBind,bco,pats);
330 testPrimPats(bco,root,pats,body);
331 asmEndAlt(bco,altBegin);
333 /* if we got this far and didn't match, panic! */
337 /* ToDo: implement this code... */
339 /* asmPushRet(bco,delayPrimAlt( stgPrimCaseVars(e), stgPrimCaseBody(e))); */
340 /* cgExpr( bco,root,scrut ); */
344 case STGAPP: /* Tail call */
346 AsmSp env = asmBeginEnter(bco);
347 map1Proc(pushAtom,bco,reverse(stgAppArgs(e)));
348 pushAtom(bco,stgAppFun(e));
349 asmEndEnter(bco,env,root);
352 case NAME: /* Tail call (with no args) */
354 AsmSp env = asmBeginEnter(bco);
355 pushVar(bco,name(e).stgVar);
356 asmEndEnter(bco,env,root);
359 case STGVAR: /* Tail call (with no args), plus unboxed return */
365 AsmSp env = asmBeginEnter(bco);
367 asmEndEnter(bco,env,root);
372 /* cgTailCall(bco,singleton(e)); */
373 /* asmReturnInt(bco); */
376 internal("cgExpr StgVar");
379 case STGPRIM: /* Tail call again */
381 AsmSp beginPrim = asmBeginPrim(bco);
382 map1Proc(pushAtom,bco,reverse(stgPrimArgs(e)));
383 asmEndPrim(bco,(AsmPrim*)name(e).primop,beginPrim);
384 /* map1Proc(cgBind,bco,rs_vars); */
385 assert(0); /* asmReturn_retty(); */
389 fprintf(stderr,"\nYoiks: "); printExp(stderr,e);
394 #define M_ITBLNAMES 35000
396 void* itblNames[M_ITBLNAMES];
399 /* allocate space for top level variable
400 * any change requires a corresponding change in 'build'.
402 static Void alloc( AsmBCO bco, StgVar v )
404 StgRhs rhs = stgVarBody(v);
406 switch (whatIs(rhs)) {
409 StgDiscr con = stgConCon(rhs);
410 List args = stgConArgs(rhs);
411 if (isBoxingCon(con)) {
412 pushAtom(bco,hd(args));
413 setPos(v,asmBox(bco,boxingConRep(con)));
416 void* vv = stgConInfo(con);
417 if (!(nItblNames < (M_ITBLNAMES-2)))
418 internal("alloc -- M_ITBLNAMES too small");
420 itblNames[nItblNames++] = vv;
421 itblNames[nItblNames++] = textToStr(name(con).text);
424 char* cc = malloc(10);
426 sprintf(cc, "Tuple%d", tupleOf(con) );
427 itblNames[nItblNames++] = vv;
428 itblNames[nItblNames++] = cc;
430 assert ( /* cant identify constructor name */ 0 );
432 setPos(v,asmAllocCONSTR(bco, vv));
438 List bs = stgAppArgs(rhs);
439 for (; nonNull(bs); bs=tl(bs)) {
440 if (isName(hd(bs))) {
443 ASSERT(whatIs(hd(bs))==STGVAR);
444 totSizeW += asmRepSizeW( charOf(stgVarRep(hd(bs))) );
447 setPos(v,asmAllocAP(bco,totSizeW));
448 //ORIGINALLY:setPos(v,asmAllocAP(bco,length(stgAppArgs(rhs))));
451 case LAMBDA: /* optimisation */
452 setObj(v,cgLambda(rhs));
455 setPos(v,asmAllocAP(bco,0));
460 static Void build( AsmBCO bco, StgVar v )
462 StgRhs rhs = stgVarBody(v);
465 switch (whatIs(rhs)) {
468 StgDiscr con = stgConCon(rhs);
469 List args = stgConArgs(rhs);
470 if (isBoxingCon(con)) {
471 doNothing(); /* already done in alloc */
473 AsmSp start = asmBeginPack(bco);
474 map1Proc(pushAtom,bco,reverse(args));
475 asmEndPack(bco,getPos(v),start,stgConInfo(con));
481 StgVar fun = stgAppFun(rhs);
482 List args = stgAppArgs(rhs);
484 fun = name(fun).stgVar;
486 if (nonNull(stgVarBody(fun))
487 && whatIs(stgVarBody(fun)) == LAMBDA
488 && length(stgLambdaArgs(stgVarBody(fun))) > length(args)) {
489 AsmSp start = asmBeginMkPAP(bco);
490 map1Proc(pushAtom,bco,reverse(args));
492 asmEndMkPAP(bco,getPos(v),start); /* optimisation */
494 AsmSp start = asmBeginMkAP(bco);
495 map1Proc(pushAtom,bco,reverse(args));
497 asmEndMkAP(bco,getPos(v),start);
501 case LAMBDA: /* optimisation */
502 doNothing(); /* already pushed in alloc */
505 /* These two cases look almost identical to the default but they're really
506 * special cases of STGAPP. The essential thing here is that we can't call
507 * cgRhs(rhs) because that expects the rhs to have no free variables when,
508 * in fact, the rhs is _always_ a free variable.
510 * ToDo: a simple optimiser would eliminate all examples
511 * of this except "let x = x in ..."
514 rhs = name(rhs).stgVar;
517 AsmSp start = asmBeginMkAP(bco);
519 asmEndMkAP(bco,getPos(v),start);
524 AsmSp start = asmBeginMkAP(bco); /* make it updateable! */
525 asmClosure(bco,cgRhs(rhs));
526 asmEndMkAP(bco,getPos(v),start);
532 /* --------------------------------------------------------------------------
533 * Top level variables
535 * ToDo: these should be handled by allocating a dynamic unentered CAF
536 * for each top level variable - this should be simpler!
537 * ------------------------------------------------------------------------*/
539 #if 0 /* appears to be unused */
540 static void cgAddVar( AsmObject obj, StgAtom v )
546 asmAddPtr(obj,getObj(v));
551 /* allocate AsmObject for top level variables
552 * any change requires a corresponding change in endTop
554 static void beginTop( StgVar v )
560 switch (whatIs(rhs)) {
563 //List as = stgConArgs(rhs);
564 setObj(v,asmBeginCon(stgConInfo(stgConCon(rhs))));
568 #ifdef CRUDE_PROFILING
569 setObj(v,asmBeginBCO(currentTop));
571 setObj(v,asmBeginBCO(rhs));
575 setObj(v,asmBeginCAF());
580 static void endTop( StgVar v )
582 StgRhs rhs = stgVarBody(v);
584 switch (whatIs(rhs)) {
587 List as = stgConArgs(rhs);
588 AsmCon con = (AsmCon)getObj(v);
589 for( ; nonNull(as); as=tl(as)) {
593 /* should be a delayed combinator! */
594 asmAddPtr(con,(AsmObject)getObj(a));
598 StgVar var = name(a).stgVar;
600 asmAddPtr(con,(AsmObject)getObj(a));
603 #if !USE_ADDR_FOR_STRINGS
605 asmAddPtr(con,asmStringObj(textToStr(textOf(a))));
609 /* asmAddPtr(con,??); */
617 case LAMBDA: /* optimisation */
619 /* ToDo: merge this code with cgLambda */
620 AsmBCO bco = (AsmBCO)getObj(v);
621 AsmSp root = asmBeginArgCheck(bco);
622 map1Proc(cgBind,bco,reverse(stgLambdaArgs(rhs)));
623 asmEndArgCheck(bco,root);
625 cgExpr(bco,root,stgLambdaBody(rhs));
630 default: /* updateable caf */
632 AsmCAF caf = (AsmCAF)getObj(v);
633 asmEndCAF(caf,cgRhs(rhs));
639 static void zap( StgVar v )
642 // stgVarBody(v) = NIL;
645 /* external entry point */
646 Void cgBinds( List binds )
652 if (lastModule() != modulePrelude) {
653 printf("\n\ncgBinds: before ll\n\n" );
654 for (b=binds; nonNull(b); b=tl(b)) {
655 printStg ( stdout, hd(b) ); printf("\n\n");
660 binds = liftBinds(binds);
663 if (lastModule() != modulePrelude) {
664 printf("\n\ncgBinds: after ll\n\n" );
665 for (b=binds; nonNull(b); b=tl(b)) {
666 printStg ( stdout, hd(b) ); printf("\n\n");
671 for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
675 for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
676 //printf("endTop %s\n", maybeName(hd(b)));
680 //mapProc(zap,binds);
683 /* --------------------------------------------------------------------------
684 * Code Generator control:
685 * ------------------------------------------------------------------------*/
691 /* deliberate fall though */
700 /*-------------------------------------------------------------------------*/