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: 2000/04/05 10:25:08 $
14 * ------------------------------------------------------------------------*/
16 #include "hugsbasictypes.h"
21 #include "Assembler.h"
22 #include "Rts.h" /* IF_DEBUG */
25 /*#define DEBUG_CODEGEN*/
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 AsmBCO cgLambda ( StgExpr e );
48 static AsmBCO cgRhs ( StgRhs rhs );
49 static void beginTop ( StgVar v );
50 static void endTop ( StgVar v );
52 static StgVar currentTop;
54 /* --------------------------------------------------------------------------
56 * ------------------------------------------------------------------------*/
58 static Cell cptrFromName ( Name n )
62 Module m = name(n).mod;
63 Text mt = module(m).text;
64 sprintf(buf, MAYBE_LEADING_UNDERSCORE_STR("%s_%s_closure"),
66 textToStr( enZcodeThenFindText (
67 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 = NAME_BASE_ADDR;
92 nm < NAME_BASE_ADDR+tabNameSz; ++nm )
93 if (tabName[nm-NAME_BASE_ADDR].inUse) {
94 StgVar v = name(nm).stgVar;
96 && isPtr(stgVarInfo(v))
98 && closureOfVar(v) == closure) {
99 return textToStr(name(nm).text);
105 static void cgBindRep( AsmBCO bco, StgVar v, AsmRep rep )
107 setPos(v,asmBind(bco,rep));
110 static void cgBind( AsmBCO bco, StgVar v )
112 cgBindRep(bco,v,repOf(v));
115 static Void pushVar( AsmBCO bco, StgVar v )
119 printf ( "pushVar: %d ", v ); fflush(stdout);
120 print(v,10);printf("\n");
122 assert(isStgVar(v) || isCPtr(v));
125 asmGHCClosure(bco, cptrOf(v));
127 info = stgVarInfo(v);
129 asmClosure(bco,ptrOf(info));
130 } else if (isInt(info)) {
131 asmVar(bco,intOf(info),repOf(v));
138 static Void pushAtom( AsmBCO bco, StgAtom e )
141 printf ( "pushAtom: %d ", e ); fflush(stdout);
142 print(e,10);printf("\n");
149 if (nonNull(name(e).stgVar)) {
150 pushVar(bco,name(e).stgVar);
152 Cell /*CPtr*/ addr = cptrFromName(e);
154 fprintf ( stderr, "nativeAtom: name %s\n",
155 nameFromOPtr(cptrOf(addr)) );
161 asmConstChar(bco,charOf(e));
164 asmConstInt(bco,intOf(e));
167 asmConstInteger(bco,bignumToString(e));
170 asmConstDouble(bco,floatOf(e));
173 #if USE_ADDR_FOR_STRINGS
174 asmConstAddr(bco,textToStr(textOf(e)));
176 asmClosure(bco,asmStringObj(textToStr(textOf(e))));
180 asmGHCClosure(bco,cptrOf(e));
183 asmConstAddr(bco,ptrOf(e));
186 fprintf(stderr,"\nYoiks1: "); printExp(stderr,e);
187 internal("pushAtom");
191 static AsmBCO cgAlts( AsmSp root, AsmSp sp, List alts )
193 #ifdef CRUDE_PROFILING
194 AsmBCO bco = asmBeginContinuation(sp, currentTop + 1000000000);
196 AsmBCO bco = asmBeginContinuation(sp, alts);
199 = length(alts) == 2 &&
200 isDefaultAlt(hd(tl(alts))) &&
201 !isDefaultAlt(hd(alts));
203 /* refine the condition */
207 con = stgCaseAltCon(hd(alts));
209 /* special case: dictionary constructors */
210 if (isName(con) && strncmp(":D",textToStr(name(con).text),2)==0) {
214 /* special case: Tuples */
215 if (isTuple(con) || (isName(con) && con==nameUnit)) {
220 t = name(con).parent;
221 if (tycon(t).what == DATATYPE) {
222 if (length(tycon(t).defn) == 1) omit_test = TRUE;
228 for(; nonNull(alts); alts=tl(alts)) {
229 StgCaseAlt alt = hd(alts);
230 if (isDefaultAlt(alt)) {
231 cgBind(bco,stgDefaultVar(alt));
232 cgExpr(bco,root,stgDefaultBody(alt));
233 asmEndContinuation(bco);
234 return bco; /* ignore any further alternatives */
236 StgDiscr con = stgCaseAltCon(alt);
237 List vs = stgCaseAltVars(alt);
238 AsmSp begin = asmBeginAlt(bco);
240 if (omit_test) fix=-1; else fix = asmTest(bco,stgDiscrTag(con));
242 asmBind(bco,PTR_REP); /* Adjust simulated sp to acct for return value */
243 if (isBoxingCon(con)) {
244 setPos(hd(vs),asmUnbox(bco,boxingConRep(con)));
247 map1Proc(cgBind,bco,reverse(vs));
250 cgExpr(bco,root,stgCaseAltBody(alt));
251 asmEndAlt(bco,begin);
252 if (fix != -1) asmFixBranch(bco,fix);
255 /* if we got this far and didn't match, panic! */
257 asmEndContinuation(bco);
261 static void testPrimPats( AsmBCO bco, AsmSp root, List pats, StgExpr e )
266 StgVar pat = hd(pats);
267 if (isInt(stgVarBody(pat))) {
268 /* asmTestInt leaves stack unchanged - so no need to adjust it */
269 AsmPc tst = asmTestInt(bco,getPos(pat),intOf(stgVarBody(pat)));
270 assert(repOf(pat) == INT_REP);
271 testPrimPats(bco,root,tl(pats),e);
272 asmFixBranch(bco,tst);
274 testPrimPats(bco,root,tl(pats),e);
280 static AsmBCO cgLambda( StgExpr e )
282 AsmBCO bco = asmBeginBCO(e);
284 AsmSp root = asmBeginArgCheck(bco);
285 map1Proc(cgBind,bco,reverse(stgLambdaArgs(e)));
286 asmEndArgCheck(bco,root);
289 cgExpr(bco,root,stgLambdaBody(e));
295 static AsmBCO cgRhs( StgRhs rhs )
297 AsmBCO bco = asmBeginBCO(rhs );
299 AsmSp root = asmBeginArgCheck(bco);
300 asmEndArgCheck(bco,root);
302 /* ppStgExpr(rhs); */
303 cgExpr(bco,root,rhs);
310 static Void cgExpr( AsmBCO bco, AsmSp root, StgExpr e )
313 printf("cgExpr:");ppStgExpr(e);printf("\n");
318 List binds = stgLetBinds(e);
319 map1Proc(alloc,bco,binds);
320 map1Proc(build,bco,binds);
321 cgExpr(bco,root,stgLetBody(e));
326 AsmSp begin = asmBeginEnter(bco);
327 asmClosure(bco,cgLambda(e));
328 asmEndEnter(bco,begin,root);
333 List alts = stgCaseAlts(e);
334 AsmSp sp = asmBeginCase(bco);
335 AsmSp caseroot = asmContinuation(bco,cgAlts(root,sp,alts));
336 cgExpr(bco,caseroot,stgCaseScrut(e));
342 StgExpr scrut = stgPrimCaseScrut(e);
343 List alts = stgPrimCaseAlts(e);
344 if (whatIs(scrut) == STGPRIM) { /* this is an optimisation */
346 /* No need to use return address or to Slide */
347 AsmSp beginPrim = asmBeginPrim(bco);
348 map1Proc(pushAtom,bco,reverse(stgPrimArgs(scrut)));
349 asmEndPrim(bco,(AsmPrim*)name(stgPrimOp(scrut)).primop,beginPrim);
351 for(; nonNull(alts); alts=tl(alts)) {
352 StgPrimAlt alt = hd(alts);
353 List pats = stgPrimAltVars(alt);
354 StgExpr body = stgPrimAltBody(alt);
355 AsmSp altBegin = asmBeginAlt(bco);
356 map1Proc(cgBind,bco,reverse(pats));
357 testPrimPats(bco,root,pats,body);
358 asmEndAlt(bco,altBegin);
360 /* if we got this far and didn't match, panic! */
363 } else if (whatIs(scrut) == STGVAR) { /* another optimisation */
365 /* No need to use return address or to Slide */
367 /* only part different from primop code... todo */
368 AsmSp beginCase = asmBeginCase(bco);
370 asmEndAlt(bco,beginCase); /* hack, hack - */
372 for(; nonNull(alts); alts=tl(alts)) {
373 StgPrimAlt alt = hd(alts);
374 List pats = stgPrimAltVars(alt);
375 StgExpr body = stgPrimAltBody(alt);
376 AsmSp altBegin = asmBeginAlt(bco);
377 map1Proc(cgBind,bco,pats);
378 testPrimPats(bco,root,pats,body);
379 asmEndAlt(bco,altBegin);
381 /* if we got this far and didn't match, panic! */
385 /* ToDo: implement this code... */
387 /* asmPushRet(bco,delayPrimAlt( stgPrimCaseVars(e),
388 stgPrimCaseBody(e))); */
389 /* cgExpr( bco,root,scrut ); */
393 case STGAPP: /* Tail call */
395 AsmSp env = asmBeginEnter(bco);
396 map1Proc(pushAtom,bco,reverse(stgAppArgs(e)));
397 pushAtom(bco,stgAppFun(e));
398 asmEndEnter(bco,env,root);
401 case NAME: /* Tail call (with no args) */
403 AsmSp env = asmBeginEnter(bco);
404 /* JRS 000112: next line used to be: pushVar(bco,name(e).stgVar); */
406 asmEndEnter(bco,env,root);
409 case STGVAR: /* Tail call (with no args), plus unboxed return */
415 AsmSp env = asmBeginEnter(bco);
417 asmEndEnter(bco,env,root);
422 /* cgTailCall(bco,singleton(e)); */
423 /* asmReturnInt(bco); */
426 internal("cgExpr StgVar");
429 case STGPRIM: /* Tail call again */
431 AsmSp beginPrim = asmBeginPrim(bco);
432 map1Proc(pushAtom,bco,reverse(stgPrimArgs(e)));
433 asmEndPrim(bco,(AsmPrim*)name(e).primop,beginPrim);
434 /* map1Proc(cgBind,bco,rs_vars); */
435 assert(0); /* asmReturn_retty(); */
439 fprintf(stderr,"\nYoiks2: "); printExp(stderr,e);
444 #define M_ITBLNAMES 35000
446 void* itblNames[M_ITBLNAMES];
449 /* allocate space for top level variable
450 * any change requires a corresponding change in 'build'.
452 static Void alloc( AsmBCO bco, StgVar v )
454 StgRhs rhs = stgVarBody(v);
457 printf("alloc: ");ppStgExpr(v);
459 switch (whatIs(rhs)) {
462 StgDiscr con = stgConCon(rhs);
463 List args = stgConArgs(rhs);
464 if (isBoxingCon(con)) {
465 pushAtom(bco,hd(args));
466 setPos(v,asmBox(bco,boxingConRep(con)));
469 void* vv = stgConInfo(con);
470 if (!(nItblNames < (M_ITBLNAMES-2)))
471 internal("alloc -- M_ITBLNAMES too small");
473 itblNames[nItblNames++] = vv;
474 itblNames[nItblNames++] = textToStr(name(con).text);
477 itblNames[nItblNames++] = vv;
478 itblNames[nItblNames++] = textToStr(ghcTupleText(con));
480 assert ( /* cant identify constructor name */ 0 );
481 setPos(v,asmAllocCONSTR(bco, vv));
487 List bs = stgAppArgs(rhs);
488 for (; nonNull(bs); bs=tl(bs)) {
489 if (isName(hd(bs))) {
492 ASSERT(whatIs(hd(bs))==STGVAR);
493 totSizeW += asmRepSizeW( charOf(stgVarRep(hd(bs))) );
496 setPos(v,asmAllocAP(bco,totSizeW));
497 //ORIGINALLY:setPos(v,asmAllocAP(bco,length(stgAppArgs(rhs))));
500 case LAMBDA: /* optimisation */
501 setObj(v,cgLambda(rhs));
504 setPos(v,asmAllocAP(bco,0));
509 static Void build( AsmBCO bco, StgVar v )
511 StgRhs rhs = stgVarBody(v);
514 switch (whatIs(rhs)) {
517 StgDiscr con = stgConCon(rhs);
518 List args = stgConArgs(rhs);
519 if (isBoxingCon(con)) {
520 doNothing(); /* already done in alloc */
522 AsmSp start = asmBeginPack(bco);
523 map1Proc(pushAtom,bco,reverse(args));
524 asmEndPack(bco,getPos(v),start,stgConInfo(con));
531 StgVar fun = stgAppFun(rhs);
533 List args = stgAppArgs(rhs);
535 if (nonNull(name(fun).stgVar))
536 fun = name(fun).stgVar; else
537 fun = cptrFromName(fun);
541 assert(isName(fun0));
542 itsaPAP = name(fun0).arity > length(args);
544 fprintf ( stderr, "nativeCall: name %s, arity %d, args %d\n",
545 nameFromOPtr(cptrOf(fun)), name(fun0).arity,
550 if (nonNull(stgVarBody(fun))
551 && whatIs(stgVarBody(fun)) == LAMBDA
552 && length(stgLambdaArgs(stgVarBody(fun))) > length(args)
558 AsmSp start = asmBeginMkPAP(bco);
559 map1Proc(pushAtom,bco,reverse(args));
561 asmEndMkPAP(bco,getPos(v),start); /* optimisation */
563 AsmSp start = asmBeginMkAP(bco);
564 map1Proc(pushAtom,bco,reverse(args));
566 asmEndMkAP(bco,getPos(v),start);
570 case LAMBDA: /* optimisation */
571 doNothing(); /* already pushed in alloc */
574 /* These two cases look almost identical to the default but they're really
575 * special cases of STGAPP. The essential thing here is that we can't call
576 * cgRhs(rhs) because that expects the rhs to have no free variables when,
577 * in fact, the rhs is _always_ a free variable.
579 * ToDo: a simple optimiser would eliminate all examples
580 * of this except "let x = x in ..."
583 if (nonNull(name(rhs).stgVar))
584 rhs = name(rhs).stgVar; else
585 rhs = cptrFromName(rhs);
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++) {
744 /* printStg( stdout, hd(b) ); printf( "\n\n"); */
748 for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
749 /* printStg( stdout, hd(b) ); printf( "\n\n"); */
753 /* mapProc(zap,binds); */
756 /* Called by the evaluator's GC to tell Hugs to mark stuff in the
759 void markHugsObjects( void )
763 for ( nm = NAME_BASE_ADDR;
764 nm < NAME_BASE_ADDR+tabNameSz; ++nm )
765 if (tabName[nm-NAME_BASE_ADDR].inUse) {
766 StgVar v = name(nm).stgVar;
767 if (isStgVar(v) && isPtr(stgVarInfo(v))) {
768 asmMarkObject(ptrOf(stgVarInfo(v)));
773 /* --------------------------------------------------------------------------
774 * Code Generator control:
775 * ------------------------------------------------------------------------*/
789 /*-------------------------------------------------------------------------*/