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/22 18:11:00 $
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",
66 textToStr(mt), textToStr(name(n).text) );
67 p = lookupOTabName ( m, buf );
69 ERRMSG(0) "Can't find object symbol %s", buf
75 static Bool varHasClosure( StgVar v )
77 return asmObjectHasClosure((AsmClosure*)ptrOf(stgVarInfo(v)));
80 /* should be AsmClosure* */
81 void* closureOfVar( StgVar v )
83 return asmClosureOfObject((AsmClosure*)ptrOf(stgVarInfo(v)));
86 char* lookupHugsName( void* closure )
90 for( nm=NAMEMIN; nm<nameHw; ++nm ) {
91 StgVar v = name(nm).stgVar;
93 && isPtr(stgVarInfo(v))
95 && closureOfVar(v) == closure) {
96 return textToStr(name(nm).text);
102 /* called at the start of GC */
103 void markHugsObjects( void )
107 for( nm=NAMEMIN; nm<nameHw; ++nm ) {
108 StgVar v = name(nm).stgVar;
109 if (isStgVar(v) && isPtr(stgVarInfo(v))) {
110 asmMarkObject((AsmClosure*)ptrOf(stgVarInfo(v)));
115 static void cgBindRep( AsmBCO bco, StgVar v, AsmRep rep )
117 setPos(v,asmBind(bco,rep));
120 static void cgBind( AsmBCO bco, StgVar v )
122 cgBindRep(bco,v,repOf(v));
125 static Void pushVar( AsmBCO bco, StgVar v )
129 if (!(isStgVar(v) || isCPtr(v))) {
130 assert(isStgVar(v) || isCPtr(v));
134 asmGHCClosure(bco, cptrOf(v));
136 info = stgVarInfo(v);
138 asmClosure(bco,ptrOf(info));
139 } else if (isInt(info)) {
140 asmVar(bco,intOf(info),repOf(v));
147 static Void pushAtom( AsmBCO bco, StgAtom e )
154 if (nonNull(name(e).stgVar))
155 pushVar(bco,name(e).stgVar); else
156 pushVar(bco,cptrFromName(e));
159 asmConstChar(bco,charOf(e));
162 asmConstInt(bco,intOf(e));
165 asmConstInteger(bco,bignumToString(e));
168 asmConstDouble(bco,floatOf(e));
171 #if USE_ADDR_FOR_STRINGS
172 asmConstAddr(bco,textToStr(textOf(e)));
174 asmClosure(bco,asmStringObj(textToStr(textOf(e))));
178 asmGHCClosure(bco,cptrOf(e));
181 asmConstAddr(bco,ptrOf(e));
184 fprintf(stderr,"\nYoiks: "); printExp(stderr,e);
185 internal("pushAtom");
189 static AsmBCO cgAlts( AsmSp root, AsmSp sp, List alts )
191 #ifdef CRUDE_PROFILING
192 AsmBCO bco = asmBeginContinuation(sp, currentTop + 1000000000);
194 AsmBCO bco = asmBeginContinuation(sp, alts);
197 = length(alts) == 2 &&
198 isDefaultAlt(hd(tl(alts))) &&
199 !isDefaultAlt(hd(alts));
201 /* refine the condition */
205 con = stgCaseAltCon(hd(alts));
207 /* special case: dictionary constructors */
208 if (strncmp("Make.",textToStr(name(con).text),5)==0) {
212 /* special case: Tuples */
213 if (isTuple(con) || (isName(con) && con==nameUnit)) {
218 t = name(con).parent;
219 if (tycon(t).what == DATATYPE) {
220 if (length(tycon(t).defn) == 1) omit_test = TRUE;
226 for(; nonNull(alts); alts=tl(alts)) {
227 StgCaseAlt alt = hd(alts);
228 if (isDefaultAlt(alt)) {
229 cgBind(bco,stgDefaultVar(alt));
230 cgExpr(bco,root,stgDefaultBody(alt));
231 asmEndContinuation(bco);
232 return bco; /* ignore any further alternatives */
234 StgDiscr con = stgCaseAltCon(alt);
235 List vs = stgCaseAltVars(alt);
236 AsmSp begin = asmBeginAlt(bco);
238 if (omit_test) fix=-1; else fix = asmTest(bco,stgDiscrTag(con));
240 asmBind(bco,PTR_REP); /* Adjust simulated sp to acct for return value */
241 if (isBoxingCon(con)) {
242 setPos(hd(vs),asmUnbox(bco,boxingConRep(con)));
245 map1Proc(cgBind,bco,reverse(vs));
248 cgExpr(bco,root,stgCaseAltBody(alt));
249 asmEndAlt(bco,begin);
250 if (fix != -1) asmFixBranch(bco,fix);
253 /* if we got this far and didn't match, panic! */
255 asmEndContinuation(bco);
259 static void testPrimPats( AsmBCO bco, AsmSp root, List pats, StgExpr e )
264 StgVar pat = hd(pats);
265 if (isInt(stgVarBody(pat))) {
266 /* asmTestInt leaves stack unchanged - so no need to adjust it */
267 AsmPc tst = asmTestInt(bco,getPos(pat),intOf(stgVarBody(pat)));
268 assert(repOf(pat) == INT_REP);
269 testPrimPats(bco,root,tl(pats),e);
270 asmFixBranch(bco,tst);
272 testPrimPats(bco,root,tl(pats),e);
277 #if 0 /* appears to be unused */
278 static void cgPrimAlt( AsmBCO bco, AsmSp root, List vs, StgExpr e )
280 assert(0); /* ToDo: test for patterns */
281 map1Proc(cgBind,bco,vs); /* ToDo: are these in right order? */
287 static AsmBCO cgLambda( StgExpr e )
289 AsmBCO bco = asmBeginBCO(e);
291 AsmSp root = asmBeginArgCheck(bco);
292 map1Proc(cgBind,bco,reverse(stgLambdaArgs(e)));
293 asmEndArgCheck(bco,root);
296 cgExpr(bco,root,stgLambdaBody(e));
302 static AsmBCO cgRhs( StgRhs rhs )
304 AsmBCO bco = asmBeginBCO(rhs );
306 AsmSp root = asmBeginArgCheck(bco);
307 asmEndArgCheck(bco,root);
309 /* ppStgExpr(rhs); */
310 cgExpr(bco,root,rhs);
317 static Void cgExpr( AsmBCO bco, AsmSp root, StgExpr e )
319 //printf("cgExpr:");ppStgExpr(e);printf("\n");
323 List binds = stgLetBinds(e);
324 map1Proc(alloc,bco,binds);
325 map1Proc(build,bco,binds);
326 cgExpr(bco,root,stgLetBody(e));
331 AsmSp begin = asmBeginEnter(bco);
332 asmClosure(bco,cgLambda(e));
333 asmEndEnter(bco,begin,root);
338 List alts = stgCaseAlts(e);
339 AsmSp sp = asmBeginCase(bco);
340 AsmSp caseroot = asmContinuation(bco,cgAlts(root,sp,alts));
341 cgExpr(bco,caseroot,stgCaseScrut(e));
347 StgExpr scrut = stgPrimCaseScrut(e);
348 List alts = stgPrimCaseAlts(e);
349 if (whatIs(scrut) == STGPRIM) { /* this is an optimisation */
351 /* No need to use return address or to Slide */
352 AsmSp beginPrim = asmBeginPrim(bco);
353 map1Proc(pushAtom,bco,reverse(stgPrimArgs(scrut)));
354 asmEndPrim(bco,(AsmPrim*)name(stgPrimOp(scrut)).primop,beginPrim);
356 for(; nonNull(alts); alts=tl(alts)) {
357 StgPrimAlt alt = hd(alts);
358 List pats = stgPrimAltVars(alt);
359 StgExpr body = stgPrimAltBody(alt);
360 AsmSp altBegin = asmBeginAlt(bco);
361 map1Proc(cgBind,bco,reverse(pats));
362 testPrimPats(bco,root,pats,body);
363 asmEndAlt(bco,altBegin);
365 /* if we got this far and didn't match, panic! */
368 } else if (whatIs(scrut) == STGVAR) { /* another optimisation */
370 /* No need to use return address or to Slide */
372 /* only part different from primop code... todo */
373 AsmSp beginCase = asmBeginCase(bco);
375 asmEndAlt(bco,beginCase); /* hack, hack - */
377 for(; nonNull(alts); alts=tl(alts)) {
378 StgPrimAlt alt = hd(alts);
379 List pats = stgPrimAltVars(alt);
380 StgExpr body = stgPrimAltBody(alt);
381 AsmSp altBegin = asmBeginAlt(bco);
382 map1Proc(cgBind,bco,pats);
383 testPrimPats(bco,root,pats,body);
384 asmEndAlt(bco,altBegin);
386 /* if we got this far and didn't match, panic! */
390 /* ToDo: implement this code... */
392 /* asmPushRet(bco,delayPrimAlt( stgPrimCaseVars(e), stgPrimCaseBody(e))); */
393 /* cgExpr( bco,root,scrut ); */
397 case STGAPP: /* Tail call */
399 AsmSp env = asmBeginEnter(bco);
400 map1Proc(pushAtom,bco,reverse(stgAppArgs(e)));
401 pushAtom(bco,stgAppFun(e));
402 asmEndEnter(bco,env,root);
405 case NAME: /* Tail call (with no args) */
407 AsmSp env = asmBeginEnter(bco);
408 pushVar(bco,name(e).stgVar);
409 asmEndEnter(bco,env,root);
412 case STGVAR: /* Tail call (with no args), plus unboxed return */
418 AsmSp env = asmBeginEnter(bco);
420 asmEndEnter(bco,env,root);
425 /* cgTailCall(bco,singleton(e)); */
426 /* asmReturnInt(bco); */
429 internal("cgExpr StgVar");
432 case STGPRIM: /* Tail call again */
434 AsmSp beginPrim = asmBeginPrim(bco);
435 map1Proc(pushAtom,bco,reverse(stgPrimArgs(e)));
436 asmEndPrim(bco,(AsmPrim*)name(e).primop,beginPrim);
437 /* map1Proc(cgBind,bco,rs_vars); */
438 assert(0); /* asmReturn_retty(); */
442 fprintf(stderr,"\nYoiks: "); printExp(stderr,e);
447 #define M_ITBLNAMES 35000
449 void* itblNames[M_ITBLNAMES];
452 /* allocate space for top level variable
453 * any change requires a corresponding change in 'build'.
455 static Void alloc( AsmBCO bco, StgVar v )
457 StgRhs rhs = stgVarBody(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 char* cc = malloc(10);
479 sprintf(cc, "Tuple%d", tupleOf(con) );
480 itblNames[nItblNames++] = vv;
481 itblNames[nItblNames++] = cc;
483 assert ( /* cant identify constructor name */ 0 );
485 setPos(v,asmAllocCONSTR(bco, vv));
491 List bs = stgAppArgs(rhs);
492 for (; nonNull(bs); bs=tl(bs)) {
493 if (isName(hd(bs))) {
496 ASSERT(whatIs(hd(bs))==STGVAR);
497 totSizeW += asmRepSizeW( charOf(stgVarRep(hd(bs))) );
500 setPos(v,asmAllocAP(bco,totSizeW));
501 //ORIGINALLY:setPos(v,asmAllocAP(bco,length(stgAppArgs(rhs))));
504 case LAMBDA: /* optimisation */
505 setObj(v,cgLambda(rhs));
508 setPos(v,asmAllocAP(bco,0));
513 static Void build( AsmBCO bco, StgVar v )
515 StgRhs rhs = stgVarBody(v);
518 switch (whatIs(rhs)) {
521 StgDiscr con = stgConCon(rhs);
522 List args = stgConArgs(rhs);
523 if (isBoxingCon(con)) {
524 doNothing(); /* already done in alloc */
526 AsmSp start = asmBeginPack(bco);
527 map1Proc(pushAtom,bco,reverse(args));
528 asmEndPack(bco,getPos(v),start,stgConInfo(con));
535 StgVar fun = stgAppFun(rhs);
537 List args = stgAppArgs(rhs);
539 if (nonNull(name(fun).stgVar))
540 fun = name(fun).stgVar; else
541 fun = cptrFromName(fun);
545 assert(isName(fun0));
546 itsaPAP = name(fun0).arity > length(args);
547 fprintf ( stderr, "nativeCall: name %s, arity %d, args %d\n",
548 nameFromOPtr(cptrOf(fun)), name(fun0).arity, length(args) );
551 if (nonNull(stgVarBody(fun))
552 && whatIs(stgVarBody(fun)) == LAMBDA
553 && length(stgLambdaArgs(stgVarBody(fun))) > length(args)
559 AsmSp start = asmBeginMkPAP(bco);
560 map1Proc(pushAtom,bco,reverse(args));
562 asmEndMkPAP(bco,getPos(v),start); /* optimisation */
564 AsmSp start = asmBeginMkAP(bco);
565 map1Proc(pushAtom,bco,reverse(args));
567 asmEndMkAP(bco,getPos(v),start);
571 case LAMBDA: /* optimisation */
572 doNothing(); /* already pushed in alloc */
575 /* These two cases look almost identical to the default but they're really
576 * special cases of STGAPP. The essential thing here is that we can't call
577 * cgRhs(rhs) because that expects the rhs to have no free variables when,
578 * in fact, the rhs is _always_ a free variable.
580 * ToDo: a simple optimiser would eliminate all examples
581 * of this except "let x = x in ..."
584 rhs = name(rhs).stgVar;
587 AsmSp start = asmBeginMkAP(bco);
589 asmEndMkAP(bco,getPos(v),start);
594 AsmSp start = asmBeginMkAP(bco); /* make it updateable! */
595 asmClosure(bco,cgRhs(rhs));
596 asmEndMkAP(bco,getPos(v),start);
602 /* --------------------------------------------------------------------------
603 * Top level variables
605 * ToDo: these should be handled by allocating a dynamic unentered CAF
606 * for each top level variable - this should be simpler!
607 * ------------------------------------------------------------------------*/
609 #if 0 /* appears to be unused */
610 static void cgAddVar( AsmObject obj, StgAtom v )
616 asmAddPtr(obj,getObj(v));
621 /* allocate AsmObject for top level variables
622 * any change requires a corresponding change in endTop
624 static void beginTop( StgVar v )
630 switch (whatIs(rhs)) {
633 //List as = stgConArgs(rhs);
634 setObj(v,asmBeginCon(stgConInfo(stgConCon(rhs))));
638 #ifdef CRUDE_PROFILING
639 setObj(v,asmBeginBCO(currentTop));
641 setObj(v,asmBeginBCO(rhs));
645 setObj(v,asmBeginCAF());
650 static void endTop( StgVar v )
652 StgRhs rhs = stgVarBody(v);
654 switch (whatIs(rhs)) {
657 List as = stgConArgs(rhs);
658 AsmCon con = (AsmCon)getObj(v);
659 for( ; nonNull(as); as=tl(as)) {
663 /* should be a delayed combinator! */
664 asmAddPtr(con,(AsmObject)getObj(a));
668 StgVar var = name(a).stgVar;
670 asmAddPtr(con,(AsmObject)getObj(a));
673 #if !USE_ADDR_FOR_STRINGS
675 asmAddPtr(con,asmStringObj(textToStr(textOf(a))));
679 /* asmAddPtr(con,??); */
687 case LAMBDA: /* optimisation */
689 /* ToDo: merge this code with cgLambda */
690 AsmBCO bco = (AsmBCO)getObj(v);
691 AsmSp root = asmBeginArgCheck(bco);
692 map1Proc(cgBind,bco,reverse(stgLambdaArgs(rhs)));
693 asmEndArgCheck(bco,root);
695 cgExpr(bco,root,stgLambdaBody(rhs));
700 default: /* updateable caf */
702 AsmCAF caf = (AsmCAF)getObj(v);
703 asmEndCAF(caf,cgRhs(rhs));
709 static void zap( StgVar v )
712 // stgVarBody(v) = NIL;
715 /* external entry point */
716 Void cgBinds( List binds )
722 if (lastModule() != modulePrelude) {
723 printf("\n\ncgBinds: before ll\n\n" );
724 for (b=binds; nonNull(b); b=tl(b)) {
725 printStg ( stdout, hd(b) ); printf("\n\n");
730 binds = liftBinds(binds);
733 if (lastModule() != modulePrelude) {
734 printf("\n\ncgBinds: after ll\n\n" );
735 for (b=binds; nonNull(b); b=tl(b)) {
736 printStg ( stdout, hd(b) ); printf("\n\n");
741 for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
745 for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
746 //printf("endTop %s\n", maybeName(hd(b)));
750 //mapProc(zap,binds);
753 /* --------------------------------------------------------------------------
754 * Code Generator control:
755 * ------------------------------------------------------------------------*/
761 /* deliberate fall though */
770 /*-------------------------------------------------------------------------*/