2 /* --------------------------------------------------------------------------
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.
11 * $RCSfile: codegen.c,v $
13 * $Date: 1999/11/29 18:59:25 $
14 * ------------------------------------------------------------------------*/
21 #include "Assembler.h"
24 #include "Rts.h" /* IF_DEBUG */
27 /* --------------------------------------------------------------------------
28 * Local function prototypes:
29 * ------------------------------------------------------------------------*/
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)
36 #define repOf(x) charOf(stgVarRep(x))
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 );
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 );
53 static StgVar currentTop;
55 /* --------------------------------------------------------------------------
57 * ------------------------------------------------------------------------*/
59 static Cell cptrFromName ( Name n )
63 Module m = name(n).mod;
64 Text mt = module(m).text;
65 sprintf(buf,"%s_%s_closure",
67 textToStr( enZcodeThenFindText ( textToStr (name(n).text) ) ) );
68 p = lookupOTabName ( m, buf );
70 ERRMSG(0) "Can't find object symbol %s", buf
76 static Bool varHasClosure( StgVar v )
78 return asmObjectHasClosure((AsmClosure*)ptrOf(stgVarInfo(v)));
81 /* should be AsmClosure* */
82 void* closureOfVar( StgVar v )
84 return asmClosureOfObject((AsmClosure*)ptrOf(stgVarInfo(v)));
87 char* lookupHugsName( void* closure )
91 for( nm=NAMEMIN; nm<nameHw; ++nm ) {
92 StgVar v = name(nm).stgVar;
94 && isPtr(stgVarInfo(v))
96 && closureOfVar(v) == closure) {
97 return textToStr(name(nm).text);
103 /* called at the start of GC */
104 void markHugsObjects( void )
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)));
116 static void cgBindRep( AsmBCO bco, StgVar v, AsmRep rep )
118 setPos(v,asmBind(bco,rep));
121 static void cgBind( AsmBCO bco, StgVar v )
123 cgBindRep(bco,v,repOf(v));
126 static Void pushVar( AsmBCO bco, StgVar v )
130 if (!(isStgVar(v) || isCPtr(v))) {
131 assert(isStgVar(v) || isCPtr(v));
135 asmGHCClosure(bco, cptrOf(v));
137 info = stgVarInfo(v);
139 asmClosure(bco,ptrOf(info));
140 } else if (isInt(info)) {
141 asmVar(bco,intOf(info),repOf(v));
148 static Void pushAtom( AsmBCO bco, StgAtom e )
155 if (nonNull(name(e).stgVar))
156 pushVar(bco,name(e).stgVar); else
157 pushVar(bco,cptrFromName(e));
160 asmConstChar(bco,charOf(e));
163 asmConstInt(bco,intOf(e));
166 asmConstInteger(bco,bignumToString(e));
169 asmConstDouble(bco,floatOf(e));
172 #if USE_ADDR_FOR_STRINGS
173 asmConstAddr(bco,textToStr(textOf(e)));
175 asmClosure(bco,asmStringObj(textToStr(textOf(e))));
179 asmGHCClosure(bco,cptrOf(e));
182 asmConstAddr(bco,ptrOf(e));
185 fprintf(stderr,"\nYoiks: "); printExp(stderr,e);
186 internal("pushAtom");
190 static AsmBCO cgAlts( AsmSp root, AsmSp sp, List alts )
192 #ifdef CRUDE_PROFILING
193 AsmBCO bco = asmBeginContinuation(sp, currentTop + 1000000000);
195 AsmBCO bco = asmBeginContinuation(sp, alts);
198 = length(alts) == 2 &&
199 isDefaultAlt(hd(tl(alts))) &&
200 !isDefaultAlt(hd(alts));
202 /* refine the condition */
206 con = stgCaseAltCon(hd(alts));
208 /* special case: dictionary constructors */
209 if (strncmp(":D",textToStr(name(con).text),2)==0) {
213 /* special case: Tuples */
214 if (isTuple(con) || (isName(con) && con==nameUnit)) {
219 t = name(con).parent;
220 if (tycon(t).what == DATATYPE) {
221 if (length(tycon(t).defn) == 1) omit_test = TRUE;
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 */
235 StgDiscr con = stgCaseAltCon(alt);
236 List vs = stgCaseAltVars(alt);
237 AsmSp begin = asmBeginAlt(bco);
239 if (omit_test) fix=-1; else fix = asmTest(bco,stgDiscrTag(con));
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)));
246 map1Proc(cgBind,bco,reverse(vs));
249 cgExpr(bco,root,stgCaseAltBody(alt));
250 asmEndAlt(bco,begin);
251 if (fix != -1) asmFixBranch(bco,fix);
254 /* if we got this far and didn't match, panic! */
256 asmEndContinuation(bco);
260 static void testPrimPats( AsmBCO bco, AsmSp root, List pats, StgExpr e )
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);
273 testPrimPats(bco,root,tl(pats),e);
278 #if 0 /* appears to be unused */
279 static void cgPrimAlt( AsmBCO bco, AsmSp root, List vs, StgExpr e )
281 assert(0); /* ToDo: test for patterns */
282 map1Proc(cgBind,bco,vs); /* ToDo: are these in right order? */
288 static AsmBCO cgLambda( StgExpr e )
290 AsmBCO bco = asmBeginBCO(e);
292 AsmSp root = asmBeginArgCheck(bco);
293 map1Proc(cgBind,bco,reverse(stgLambdaArgs(e)));
294 asmEndArgCheck(bco,root);
297 cgExpr(bco,root,stgLambdaBody(e));
303 static AsmBCO cgRhs( StgRhs rhs )
305 AsmBCO bco = asmBeginBCO(rhs );
307 AsmSp root = asmBeginArgCheck(bco);
308 asmEndArgCheck(bco,root);
310 /* ppStgExpr(rhs); */
311 cgExpr(bco,root,rhs);
318 static Void cgExpr( AsmBCO bco, AsmSp root, StgExpr e )
320 //printf("cgExpr:");ppStgExpr(e);printf("\n");
324 List binds = stgLetBinds(e);
325 map1Proc(alloc,bco,binds);
326 map1Proc(build,bco,binds);
327 cgExpr(bco,root,stgLetBody(e));
332 AsmSp begin = asmBeginEnter(bco);
333 asmClosure(bco,cgLambda(e));
334 asmEndEnter(bco,begin,root);
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));
348 StgExpr scrut = stgPrimCaseScrut(e);
349 List alts = stgPrimCaseAlts(e);
350 if (whatIs(scrut) == STGPRIM) { /* this is an optimisation */
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);
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);
366 /* if we got this far and didn't match, panic! */
369 } else if (whatIs(scrut) == STGVAR) { /* another optimisation */
371 /* No need to use return address or to Slide */
373 /* only part different from primop code... todo */
374 AsmSp beginCase = asmBeginCase(bco);
376 asmEndAlt(bco,beginCase); /* hack, hack - */
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);
387 /* if we got this far and didn't match, panic! */
391 /* ToDo: implement this code... */
393 /* asmPushRet(bco,delayPrimAlt( stgPrimCaseVars(e),
394 stgPrimCaseBody(e))); */
395 /* cgExpr( bco,root,scrut ); */
399 case STGAPP: /* Tail call */
401 AsmSp env = asmBeginEnter(bco);
402 map1Proc(pushAtom,bco,reverse(stgAppArgs(e)));
403 pushAtom(bco,stgAppFun(e));
404 asmEndEnter(bco,env,root);
407 case NAME: /* Tail call (with no args) */
409 AsmSp env = asmBeginEnter(bco);
410 pushVar(bco,name(e).stgVar);
411 asmEndEnter(bco,env,root);
414 case STGVAR: /* Tail call (with no args), plus unboxed return */
420 AsmSp env = asmBeginEnter(bco);
422 asmEndEnter(bco,env,root);
427 /* cgTailCall(bco,singleton(e)); */
428 /* asmReturnInt(bco); */
431 internal("cgExpr StgVar");
434 case STGPRIM: /* Tail call again */
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(); */
444 fprintf(stderr,"\nYoiks: "); printExp(stderr,e);
449 #define M_ITBLNAMES 35000
451 void* itblNames[M_ITBLNAMES];
454 /* allocate space for top level variable
455 * any change requires a corresponding change in 'build'.
457 static Void alloc( AsmBCO bco, StgVar v )
459 StgRhs rhs = stgVarBody(v);
461 switch (whatIs(rhs)) {
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)));
471 void* vv = stgConInfo(con);
472 if (!(nItblNames < (M_ITBLNAMES-2)))
473 internal("alloc -- M_ITBLNAMES too small");
475 itblNames[nItblNames++] = vv;
476 itblNames[nItblNames++] = textToStr(name(con).text);
479 char* cc = malloc(10);
481 sprintf(cc, "Tuple%d", tupleOf(con) );
482 itblNames[nItblNames++] = vv;
483 itblNames[nItblNames++] = cc;
485 assert ( /* cant identify constructor name */ 0 );
487 setPos(v,asmAllocCONSTR(bco, vv));
493 List bs = stgAppArgs(rhs);
494 for (; nonNull(bs); bs=tl(bs)) {
495 if (isName(hd(bs))) {
498 ASSERT(whatIs(hd(bs))==STGVAR);
499 totSizeW += asmRepSizeW( charOf(stgVarRep(hd(bs))) );
502 setPos(v,asmAllocAP(bco,totSizeW));
503 //ORIGINALLY:setPos(v,asmAllocAP(bco,length(stgAppArgs(rhs))));
506 case LAMBDA: /* optimisation */
507 setObj(v,cgLambda(rhs));
510 setPos(v,asmAllocAP(bco,0));
515 static Void build( AsmBCO bco, StgVar v )
517 StgRhs rhs = stgVarBody(v);
520 switch (whatIs(rhs)) {
523 StgDiscr con = stgConCon(rhs);
524 List args = stgConArgs(rhs);
525 if (isBoxingCon(con)) {
526 doNothing(); /* already done in alloc */
528 AsmSp start = asmBeginPack(bco);
529 map1Proc(pushAtom,bco,reverse(args));
530 asmEndPack(bco,getPos(v),start,stgConInfo(con));
537 StgVar fun = stgAppFun(rhs);
539 List args = stgAppArgs(rhs);
541 if (nonNull(name(fun).stgVar))
542 fun = name(fun).stgVar; else
543 fun = cptrFromName(fun);
547 assert(isName(fun0));
548 itsaPAP = name(fun0).arity > length(args);
549 fprintf ( stderr, "nativeCall: name %s, arity %d, args %d\n",
550 nameFromOPtr(cptrOf(fun)), name(fun0).arity, length(args) );
553 if (nonNull(stgVarBody(fun))
554 && whatIs(stgVarBody(fun)) == LAMBDA
555 && length(stgLambdaArgs(stgVarBody(fun))) > length(args)
561 AsmSp start = asmBeginMkPAP(bco);
562 map1Proc(pushAtom,bco,reverse(args));
564 asmEndMkPAP(bco,getPos(v),start); /* optimisation */
566 AsmSp start = asmBeginMkAP(bco);
567 map1Proc(pushAtom,bco,reverse(args));
569 asmEndMkAP(bco,getPos(v),start);
573 case LAMBDA: /* optimisation */
574 doNothing(); /* already pushed in alloc */
577 /* These two cases look almost identical to the default but they're really
578 * special cases of STGAPP. The essential thing here is that we can't call
579 * cgRhs(rhs) because that expects the rhs to have no free variables when,
580 * in fact, the rhs is _always_ a free variable.
582 * ToDo: a simple optimiser would eliminate all examples
583 * of this except "let x = x in ..."
586 rhs = name(rhs).stgVar;
589 AsmSp start = asmBeginMkAP(bco);
591 asmEndMkAP(bco,getPos(v),start);
596 AsmSp start = asmBeginMkAP(bco); /* make it updateable! */
597 asmClosure(bco,cgRhs(rhs));
598 asmEndMkAP(bco,getPos(v),start);
604 /* --------------------------------------------------------------------------
605 * Top level variables
607 * ToDo: these should be handled by allocating a dynamic unentered CAF
608 * for each top level variable - this should be simpler!
609 * ------------------------------------------------------------------------*/
611 #if 0 /* appears to be unused */
612 static void cgAddVar( AsmObject obj, StgAtom v )
618 asmAddPtr(obj,getObj(v));
623 /* allocate AsmObject for top level variables
624 * any change requires a corresponding change in endTop
626 static void beginTop( StgVar v )
632 switch (whatIs(rhs)) {
635 //List as = stgConArgs(rhs);
636 setObj(v,asmBeginCon(stgConInfo(stgConCon(rhs))));
640 #ifdef CRUDE_PROFILING
641 setObj(v,asmBeginBCO(currentTop));
643 setObj(v,asmBeginBCO(rhs));
647 setObj(v,asmBeginCAF());
652 static void endTop( StgVar v )
654 StgRhs rhs = stgVarBody(v);
656 switch (whatIs(rhs)) {
659 List as = stgConArgs(rhs);
660 AsmCon con = (AsmCon)getObj(v);
661 for( ; nonNull(as); as=tl(as)) {
665 /* should be a delayed combinator! */
666 asmAddPtr(con,(AsmObject)getObj(a));
670 StgVar var = name(a).stgVar;
672 asmAddPtr(con,(AsmObject)getObj(a));
675 #if !USE_ADDR_FOR_STRINGS
677 asmAddPtr(con,asmStringObj(textToStr(textOf(a))));
681 /* asmAddPtr(con,??); */
689 case LAMBDA: /* optimisation */
691 /* ToDo: merge this code with cgLambda */
692 AsmBCO bco = (AsmBCO)getObj(v);
693 AsmSp root = asmBeginArgCheck(bco);
694 map1Proc(cgBind,bco,reverse(stgLambdaArgs(rhs)));
695 asmEndArgCheck(bco,root);
697 cgExpr(bco,root,stgLambdaBody(rhs));
702 default: /* updateable caf */
704 AsmCAF caf = (AsmCAF)getObj(v);
705 asmEndCAF(caf,cgRhs(rhs));
711 static void zap( StgVar v )
714 // stgVarBody(v) = NIL;
717 /* external entry point */
718 Void cgBinds( List binds )
724 if (lastModule() != modulePrelude) {
725 printf("\n\ncgBinds: before ll\n\n" );
726 for (b=binds; nonNull(b); b=tl(b)) {
727 printStg ( stdout, hd(b) ); printf("\n\n");
732 binds = liftBinds(binds);
735 if (lastModule() != modulePrelude) {
736 printf("\n\ncgBinds: after ll\n\n" );
737 for (b=binds; nonNull(b); b=tl(b)) {
738 printStg ( stdout, hd(b) ); printf("\n\n");
743 for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
747 for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
748 //printf("endTop %s\n", maybeName(hd(b)));
752 //mapProc(zap,binds);
755 /* --------------------------------------------------------------------------
756 * Code Generator control:
757 * ------------------------------------------------------------------------*/
763 /* deliberate fall though */
772 /*-------------------------------------------------------------------------*/