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/03/10 20:03:36 $
14 * ------------------------------------------------------------------------*/
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=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 printf ( "pushVar: %d ", v ); fflush(stdout);
131 print(v,10);printf("\n");
133 assert(isStgVar(v) || isCPtr(v));
136 asmGHCClosure(bco, cptrOf(v));
138 info = stgVarInfo(v);
140 asmClosure(bco,ptrOf(info));
141 } else if (isInt(info)) {
142 asmVar(bco,intOf(info),repOf(v));
149 static Void pushAtom( AsmBCO bco, StgAtom e )
152 printf ( "pushAtom: %d ", e ); fflush(stdout);
153 print(e,10);printf("\n");
160 if (nonNull(name(e).stgVar)) {
161 pushVar(bco,name(e).stgVar);
163 Cell /*CPtr*/ addr = cptrFromName(e);
165 fprintf ( stderr, "nativeAtom: name %s\n",
166 nameFromOPtr(cptrOf(addr)) );
172 asmConstChar(bco,charOf(e));
175 asmConstInt(bco,intOf(e));
178 asmConstInteger(bco,bignumToString(e));
181 asmConstDouble(bco,floatOf(e));
184 #if USE_ADDR_FOR_STRINGS
185 asmConstAddr(bco,textToStr(textOf(e)));
187 asmClosure(bco,asmStringObj(textToStr(textOf(e))));
191 asmGHCClosure(bco,cptrOf(e));
194 asmConstAddr(bco,ptrOf(e));
197 fprintf(stderr,"\nYoiks1: "); printExp(stderr,e);
198 internal("pushAtom");
202 static AsmBCO cgAlts( AsmSp root, AsmSp sp, List alts )
204 #ifdef CRUDE_PROFILING
205 AsmBCO bco = asmBeginContinuation(sp, currentTop + 1000000000);
207 AsmBCO bco = asmBeginContinuation(sp, alts);
210 = length(alts) == 2 &&
211 isDefaultAlt(hd(tl(alts))) &&
212 !isDefaultAlt(hd(alts));
214 /* refine the condition */
218 con = stgCaseAltCon(hd(alts));
220 /* special case: dictionary constructors */
221 if (strncmp(":D",textToStr(name(con).text),2)==0) {
225 /* special case: Tuples */
226 if (isTuple(con) || (isName(con) && con==nameUnit)) {
231 t = name(con).parent;
232 if (tycon(t).what == DATATYPE) {
233 if (length(tycon(t).defn) == 1) omit_test = TRUE;
239 for(; nonNull(alts); alts=tl(alts)) {
240 StgCaseAlt alt = hd(alts);
241 if (isDefaultAlt(alt)) {
242 cgBind(bco,stgDefaultVar(alt));
243 cgExpr(bco,root,stgDefaultBody(alt));
244 asmEndContinuation(bco);
245 return bco; /* ignore any further alternatives */
247 StgDiscr con = stgCaseAltCon(alt);
248 List vs = stgCaseAltVars(alt);
249 AsmSp begin = asmBeginAlt(bco);
251 if (omit_test) fix=-1; else fix = asmTest(bco,stgDiscrTag(con));
253 asmBind(bco,PTR_REP); /* Adjust simulated sp to acct for return value */
254 if (isBoxingCon(con)) {
255 setPos(hd(vs),asmUnbox(bco,boxingConRep(con)));
258 map1Proc(cgBind,bco,reverse(vs));
261 cgExpr(bco,root,stgCaseAltBody(alt));
262 asmEndAlt(bco,begin);
263 if (fix != -1) asmFixBranch(bco,fix);
266 /* if we got this far and didn't match, panic! */
268 asmEndContinuation(bco);
272 static void testPrimPats( AsmBCO bco, AsmSp root, List pats, StgExpr e )
277 StgVar pat = hd(pats);
278 if (isInt(stgVarBody(pat))) {
279 /* asmTestInt leaves stack unchanged - so no need to adjust it */
280 AsmPc tst = asmTestInt(bco,getPos(pat),intOf(stgVarBody(pat)));
281 assert(repOf(pat) == INT_REP);
282 testPrimPats(bco,root,tl(pats),e);
283 asmFixBranch(bco,tst);
285 testPrimPats(bco,root,tl(pats),e);
291 static AsmBCO cgLambda( StgExpr e )
293 AsmBCO bco = asmBeginBCO(e);
295 AsmSp root = asmBeginArgCheck(bco);
296 map1Proc(cgBind,bco,reverse(stgLambdaArgs(e)));
297 asmEndArgCheck(bco,root);
300 cgExpr(bco,root,stgLambdaBody(e));
306 static AsmBCO cgRhs( StgRhs rhs )
308 AsmBCO bco = asmBeginBCO(rhs );
310 AsmSp root = asmBeginArgCheck(bco);
311 asmEndArgCheck(bco,root);
313 /* ppStgExpr(rhs); */
314 cgExpr(bco,root,rhs);
321 static Void cgExpr( AsmBCO bco, AsmSp root, StgExpr e )
324 printf("cgExpr:");ppStgExpr(e);printf("\n");
329 List binds = stgLetBinds(e);
330 map1Proc(alloc,bco,binds);
331 map1Proc(build,bco,binds);
332 cgExpr(bco,root,stgLetBody(e));
337 AsmSp begin = asmBeginEnter(bco);
338 asmClosure(bco,cgLambda(e));
339 asmEndEnter(bco,begin,root);
344 List alts = stgCaseAlts(e);
345 AsmSp sp = asmBeginCase(bco);
346 AsmSp caseroot = asmContinuation(bco,cgAlts(root,sp,alts));
347 cgExpr(bco,caseroot,stgCaseScrut(e));
353 StgExpr scrut = stgPrimCaseScrut(e);
354 List alts = stgPrimCaseAlts(e);
355 if (whatIs(scrut) == STGPRIM) { /* this is an optimisation */
357 /* No need to use return address or to Slide */
358 AsmSp beginPrim = asmBeginPrim(bco);
359 map1Proc(pushAtom,bco,reverse(stgPrimArgs(scrut)));
360 asmEndPrim(bco,(AsmPrim*)name(stgPrimOp(scrut)).primop,beginPrim);
362 for(; nonNull(alts); alts=tl(alts)) {
363 StgPrimAlt alt = hd(alts);
364 List pats = stgPrimAltVars(alt);
365 StgExpr body = stgPrimAltBody(alt);
366 AsmSp altBegin = asmBeginAlt(bco);
367 map1Proc(cgBind,bco,reverse(pats));
368 testPrimPats(bco,root,pats,body);
369 asmEndAlt(bco,altBegin);
371 /* if we got this far and didn't match, panic! */
374 } else if (whatIs(scrut) == STGVAR) { /* another optimisation */
376 /* No need to use return address or to Slide */
378 /* only part different from primop code... todo */
379 AsmSp beginCase = asmBeginCase(bco);
381 asmEndAlt(bco,beginCase); /* hack, hack - */
383 for(; nonNull(alts); alts=tl(alts)) {
384 StgPrimAlt alt = hd(alts);
385 List pats = stgPrimAltVars(alt);
386 StgExpr body = stgPrimAltBody(alt);
387 AsmSp altBegin = asmBeginAlt(bco);
388 map1Proc(cgBind,bco,pats);
389 testPrimPats(bco,root,pats,body);
390 asmEndAlt(bco,altBegin);
392 /* if we got this far and didn't match, panic! */
396 /* ToDo: implement this code... */
398 /* asmPushRet(bco,delayPrimAlt( stgPrimCaseVars(e),
399 stgPrimCaseBody(e))); */
400 /* cgExpr( bco,root,scrut ); */
404 case STGAPP: /* Tail call */
406 AsmSp env = asmBeginEnter(bco);
407 map1Proc(pushAtom,bco,reverse(stgAppArgs(e)));
408 pushAtom(bco,stgAppFun(e));
409 asmEndEnter(bco,env,root);
412 case NAME: /* Tail call (with no args) */
414 AsmSp env = asmBeginEnter(bco);
415 /* JRS 000112: next line used to be: pushVar(bco,name(e).stgVar); */
417 asmEndEnter(bco,env,root);
420 case STGVAR: /* Tail call (with no args), plus unboxed return */
426 AsmSp env = asmBeginEnter(bco);
428 asmEndEnter(bco,env,root);
433 /* cgTailCall(bco,singleton(e)); */
434 /* asmReturnInt(bco); */
437 internal("cgExpr StgVar");
440 case STGPRIM: /* Tail call again */
442 AsmSp beginPrim = asmBeginPrim(bco);
443 map1Proc(pushAtom,bco,reverse(stgPrimArgs(e)));
444 asmEndPrim(bco,(AsmPrim*)name(e).primop,beginPrim);
445 /* map1Proc(cgBind,bco,rs_vars); */
446 assert(0); /* asmReturn_retty(); */
450 fprintf(stderr,"\nYoiks2: "); printExp(stderr,e);
455 #define M_ITBLNAMES 35000
457 void* itblNames[M_ITBLNAMES];
460 /* allocate space for top level variable
461 * any change requires a corresponding change in 'build'.
463 static Void alloc( AsmBCO bco, StgVar v )
465 StgRhs rhs = stgVarBody(v);
468 printf("alloc: ");ppStgExpr(v);
470 switch (whatIs(rhs)) {
473 StgDiscr con = stgConCon(rhs);
474 List args = stgConArgs(rhs);
475 if (isBoxingCon(con)) {
476 pushAtom(bco,hd(args));
477 setPos(v,asmBox(bco,boxingConRep(con)));
480 void* vv = stgConInfo(con);
481 if (!(nItblNames < (M_ITBLNAMES-2)))
482 internal("alloc -- M_ITBLNAMES too small");
484 itblNames[nItblNames++] = vv;
485 itblNames[nItblNames++] = textToStr(name(con).text);
488 itblNames[nItblNames++] = vv;
489 itblNames[nItblNames++] = textToStr(ghcTupleText(con));
491 assert ( /* cant identify constructor name */ 0 );
492 setPos(v,asmAllocCONSTR(bco, vv));
498 List bs = stgAppArgs(rhs);
499 for (; nonNull(bs); bs=tl(bs)) {
500 if (isName(hd(bs))) {
503 ASSERT(whatIs(hd(bs))==STGVAR);
504 totSizeW += asmRepSizeW( charOf(stgVarRep(hd(bs))) );
507 setPos(v,asmAllocAP(bco,totSizeW));
508 //ORIGINALLY:setPos(v,asmAllocAP(bco,length(stgAppArgs(rhs))));
511 case LAMBDA: /* optimisation */
512 setObj(v,cgLambda(rhs));
515 setPos(v,asmAllocAP(bco,0));
520 static Void build( AsmBCO bco, StgVar v )
522 StgRhs rhs = stgVarBody(v);
525 switch (whatIs(rhs)) {
528 StgDiscr con = stgConCon(rhs);
529 List args = stgConArgs(rhs);
530 if (isBoxingCon(con)) {
531 doNothing(); /* already done in alloc */
533 AsmSp start = asmBeginPack(bco);
534 map1Proc(pushAtom,bco,reverse(args));
535 asmEndPack(bco,getPos(v),start,stgConInfo(con));
542 StgVar fun = stgAppFun(rhs);
544 List args = stgAppArgs(rhs);
546 if (nonNull(name(fun).stgVar))
547 fun = name(fun).stgVar; else
548 fun = cptrFromName(fun);
552 assert(isName(fun0));
553 itsaPAP = name(fun0).arity > length(args);
555 fprintf ( stderr, "nativeCall: name %s, arity %d, args %d\n",
556 nameFromOPtr(cptrOf(fun)), name(fun0).arity,
561 if (nonNull(stgVarBody(fun))
562 && whatIs(stgVarBody(fun)) == LAMBDA
563 && length(stgLambdaArgs(stgVarBody(fun))) > length(args)
569 AsmSp start = asmBeginMkPAP(bco);
570 map1Proc(pushAtom,bco,reverse(args));
572 asmEndMkPAP(bco,getPos(v),start); /* optimisation */
574 AsmSp start = asmBeginMkAP(bco);
575 map1Proc(pushAtom,bco,reverse(args));
577 asmEndMkAP(bco,getPos(v),start);
581 case LAMBDA: /* optimisation */
582 doNothing(); /* already pushed in alloc */
585 /* These two cases look almost identical to the default but they're really
586 * special cases of STGAPP. The essential thing here is that we can't call
587 * cgRhs(rhs) because that expects the rhs to have no free variables when,
588 * in fact, the rhs is _always_ a free variable.
590 * ToDo: a simple optimiser would eliminate all examples
591 * of this except "let x = x in ..."
594 if (nonNull(name(rhs).stgVar))
595 rhs = name(rhs).stgVar; else
596 rhs = cptrFromName(rhs);
600 AsmSp start = asmBeginMkAP(bco);
602 asmEndMkAP(bco,getPos(v),start);
607 AsmSp start = asmBeginMkAP(bco); /* make it updateable! */
608 asmClosure(bco,cgRhs(rhs));
609 asmEndMkAP(bco,getPos(v),start);
615 /* --------------------------------------------------------------------------
616 * Top level variables
618 * ToDo: these should be handled by allocating a dynamic unentered CAF
619 * for each top level variable - this should be simpler!
620 * ------------------------------------------------------------------------*/
622 #if 0 /* appears to be unused */
623 static void cgAddVar( AsmObject obj, StgAtom v )
629 asmAddPtr(obj,getObj(v));
634 /* allocate AsmObject for top level variables
635 * any change requires a corresponding change in endTop
637 static void beginTop( StgVar v )
643 switch (whatIs(rhs)) {
646 //List as = stgConArgs(rhs);
647 setObj(v,asmBeginCon(stgConInfo(stgConCon(rhs))));
651 #ifdef CRUDE_PROFILING
652 setObj(v,asmBeginBCO(currentTop));
654 setObj(v,asmBeginBCO(rhs));
658 setObj(v,asmBeginCAF());
663 static void endTop( StgVar v )
665 StgRhs rhs = stgVarBody(v);
667 switch (whatIs(rhs)) {
670 List as = stgConArgs(rhs);
671 AsmCon con = (AsmCon)getObj(v);
672 for( ; nonNull(as); as=tl(as)) {
676 /* should be a delayed combinator! */
677 asmAddPtr(con,(AsmObject)getObj(a));
681 StgVar var = name(a).stgVar;
683 asmAddPtr(con,(AsmObject)getObj(a));
686 #if !USE_ADDR_FOR_STRINGS
688 asmAddPtr(con,asmStringObj(textToStr(textOf(a))));
692 /* asmAddPtr(con,??); */
700 case LAMBDA: /* optimisation */
702 /* ToDo: merge this code with cgLambda */
703 AsmBCO bco = (AsmBCO)getObj(v);
704 AsmSp root = asmBeginArgCheck(bco);
705 map1Proc(cgBind,bco,reverse(stgLambdaArgs(rhs)));
706 asmEndArgCheck(bco,root);
708 cgExpr(bco,root,stgLambdaBody(rhs));
713 default: /* updateable caf */
715 AsmCAF caf = (AsmCAF)getObj(v);
716 asmEndCAF(caf,cgRhs(rhs));
722 static void zap( StgVar v )
725 // stgVarBody(v) = NIL;
728 /* external entry point */
729 Void cgBinds( List binds )
735 if (lastModule() != modulePrelude) {
736 printf("\n\ncgBinds: before ll\n\n" );
737 for (b=binds; nonNull(b); b=tl(b)) {
738 printStg ( stdout, hd(b) ); printf("\n\n");
743 binds = liftBinds(binds);
746 if (lastModule() != modulePrelude) {
747 printf("\n\ncgBinds: after ll\n\n" );
748 for (b=binds; nonNull(b); b=tl(b)) {
749 printStg ( stdout, hd(b) ); printf("\n\n");
754 for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
758 for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
759 //printStg( stdout, hd(b) ); printf( "\n\n");
763 //mapProc(zap,binds);
766 /* --------------------------------------------------------------------------
767 * Code Generator control:
768 * ------------------------------------------------------------------------*/
782 /*-------------------------------------------------------------------------*/