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/11 16:27:30 $
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);
196 /* ppStgAlts(alts); */
197 for(; nonNull(alts); alts=tl(alts)) {
198 StgCaseAlt alt = hd(alts);
199 if (isDefaultAlt(alt)) {
200 cgBind(bco,stgDefaultVar(alt));
201 cgExpr(bco,root,stgDefaultBody(alt));
202 asmEndContinuation(bco);
203 return bco; /* ignore any further alternatives */
205 StgDiscr con = stgCaseAltCon(alt);
206 List vs = stgCaseAltVars(alt);
207 AsmSp begin = asmBeginAlt(bco);
208 AsmPc fix = asmTest(bco,stgDiscrTag(con));
209 /* ToDo: omit in single constructor types! */
210 asmBind(bco,PTR_REP); /* Adjust simulated sp to acct for return value */
211 if (isBoxingCon(con)) {
212 setPos(hd(vs),asmUnbox(bco,boxingConRep(con)));
215 map1Proc(cgBind,bco,reverse(vs));
218 cgExpr(bco,root,stgCaseAltBody(alt));
219 asmEndAlt(bco,begin);
220 asmFixBranch(bco,fix);
223 /* if we got this far and didn't match, panic! */
225 asmEndContinuation(bco);
229 static void testPrimPats( AsmBCO bco, AsmSp root, List pats, StgExpr e )
234 StgVar pat = hd(pats);
235 if (isInt(stgVarBody(pat))) {
236 /* asmTestInt leaves stack unchanged - so no need to adjust it */
237 AsmPc tst = asmTestInt(bco,getPos(pat),intOf(stgVarBody(pat)));
238 assert(repOf(pat) == INT_REP);
239 testPrimPats(bco,root,tl(pats),e);
240 asmFixBranch(bco,tst);
242 testPrimPats(bco,root,tl(pats),e);
247 #if 0 /* appears to be unused */
248 static void cgPrimAlt( AsmBCO bco, AsmSp root, List vs, StgExpr e )
250 assert(0); /* ToDo: test for patterns */
251 map1Proc(cgBind,bco,vs); /* ToDo: are these in right order? */
257 static AsmBCO cgLambda( StgExpr e )
259 AsmBCO bco = asmBeginBCO(e);
261 AsmSp root = asmBeginArgCheck(bco);
262 map1Proc(cgBind,bco,reverse(stgLambdaArgs(e)));
263 asmEndArgCheck(bco,root);
266 cgExpr(bco,root,stgLambdaBody(e));
272 static AsmBCO cgRhs( StgRhs rhs )
274 AsmBCO bco = asmBeginBCO(rhs );
276 AsmSp root = asmBeginArgCheck(bco);
277 asmEndArgCheck(bco,root);
279 /* ppStgExpr(rhs); */
280 cgExpr(bco,root,rhs);
287 static Void cgExpr( AsmBCO bco, AsmSp root, StgExpr e )
289 //printf("cgExpr:");ppStgExpr(e);printf("\n");
293 List binds = stgLetBinds(e);
294 map1Proc(alloc,bco,binds);
295 map1Proc(build,bco,binds);
296 cgExpr(bco,root,stgLetBody(e));
301 AsmSp begin = asmBeginEnter(bco);
302 asmClosure(bco,cgLambda(e));
303 asmEndEnter(bco,begin,root);
308 List alts = stgCaseAlts(e);
309 AsmSp sp = asmBeginCase(bco);
310 AsmSp caseroot = asmContinuation(bco,cgAlts(root,sp,alts));
311 cgExpr(bco,caseroot,stgCaseScrut(e));
317 StgExpr scrut = stgPrimCaseScrut(e);
318 List alts = stgPrimCaseAlts(e);
319 if (whatIs(scrut) == STGPRIM) { /* this is an optimisation */
321 /* No need to use return address or to Slide */
322 AsmSp beginPrim = asmBeginPrim(bco);
323 map1Proc(pushAtom,bco,reverse(stgPrimArgs(scrut)));
324 asmEndPrim(bco,(AsmPrim*)name(stgPrimOp(scrut)).primop,beginPrim);
326 for(; nonNull(alts); alts=tl(alts)) {
327 StgPrimAlt alt = hd(alts);
328 List pats = stgPrimAltVars(alt);
329 StgExpr body = stgPrimAltBody(alt);
330 AsmSp altBegin = asmBeginAlt(bco);
331 map1Proc(cgBind,bco,reverse(pats));
332 testPrimPats(bco,root,pats,body);
333 asmEndAlt(bco,altBegin);
335 /* if we got this far and didn't match, panic! */
338 } else if (whatIs(scrut) == STGVAR) { /* another optimisation */
340 /* No need to use return address or to Slide */
342 /* only part different from primop code... todo */
343 AsmSp beginCase = asmBeginCase(bco);
345 asmEndAlt(bco,beginCase); /* hack, hack - */
347 for(; nonNull(alts); alts=tl(alts)) {
348 StgPrimAlt alt = hd(alts);
349 List pats = stgPrimAltVars(alt);
350 StgExpr body = stgPrimAltBody(alt);
351 AsmSp altBegin = asmBeginAlt(bco);
352 map1Proc(cgBind,bco,pats);
353 testPrimPats(bco,root,pats,body);
354 asmEndAlt(bco,altBegin);
356 /* if we got this far and didn't match, panic! */
360 /* ToDo: implement this code... */
362 /* asmPushRet(bco,delayPrimAlt( stgPrimCaseVars(e), stgPrimCaseBody(e))); */
363 /* cgExpr( bco,root,scrut ); */
367 case STGAPP: /* Tail call */
369 AsmSp env = asmBeginEnter(bco);
370 map1Proc(pushAtom,bco,reverse(stgAppArgs(e)));
371 pushAtom(bco,stgAppFun(e));
372 asmEndEnter(bco,env,root);
375 case NAME: /* Tail call (with no args) */
377 AsmSp env = asmBeginEnter(bco);
378 pushVar(bco,name(e).stgVar);
379 asmEndEnter(bco,env,root);
382 case STGVAR: /* Tail call (with no args), plus unboxed return */
388 AsmSp env = asmBeginEnter(bco);
390 asmEndEnter(bco,env,root);
395 /* cgTailCall(bco,singleton(e)); */
396 /* asmReturnInt(bco); */
399 internal("cgExpr StgVar");
402 case STGPRIM: /* Tail call again */
404 AsmSp beginPrim = asmBeginPrim(bco);
405 map1Proc(pushAtom,bco,reverse(stgPrimArgs(e)));
406 asmEndPrim(bco,(AsmPrim*)name(e).primop,beginPrim);
407 /* map1Proc(cgBind,bco,rs_vars); */
408 assert(0); /* asmReturn_retty(); */
412 fprintf(stderr,"\nYoiks: "); printExp(stderr,e);
417 #define M_ITBLNAMES 35000
419 void* itblNames[M_ITBLNAMES];
422 /* allocate space for top level variable
423 * any change requires a corresponding change in 'build'.
425 static Void alloc( AsmBCO bco, StgVar v )
427 StgRhs rhs = stgVarBody(v);
429 switch (whatIs(rhs)) {
432 StgDiscr con = stgConCon(rhs);
433 List args = stgConArgs(rhs);
434 if (isBoxingCon(con)) {
435 pushAtom(bco,hd(args));
436 setPos(v,asmBox(bco,boxingConRep(con)));
439 void* vv = stgConInfo(con);
440 if (!(nItblNames < (M_ITBLNAMES-2)))
441 internal("alloc -- M_ITBLNAMES too small");
443 itblNames[nItblNames++] = vv;
444 itblNames[nItblNames++] = textToStr(name(con).text);
447 char* cc = malloc(10);
449 sprintf(cc, "Tuple%d", tupleOf(con) );
450 itblNames[nItblNames++] = vv;
451 itblNames[nItblNames++] = cc;
453 assert ( /* cant identify constructor name */ 0 );
455 setPos(v,asmAllocCONSTR(bco, vv));
461 List bs = stgAppArgs(rhs);
462 for (; nonNull(bs); bs=tl(bs)) {
463 if (isName(hd(bs))) {
466 ASSERT(whatIs(hd(bs))==STGVAR);
467 totSizeW += asmRepSizeW( charOf(stgVarRep(hd(bs))) );
470 setPos(v,asmAllocAP(bco,totSizeW));
471 //ORIGINALLY:setPos(v,asmAllocAP(bco,length(stgAppArgs(rhs))));
474 case LAMBDA: /* optimisation */
475 setObj(v,cgLambda(rhs));
478 setPos(v,asmAllocAP(bco,0));
483 static Void build( AsmBCO bco, StgVar v )
485 StgRhs rhs = stgVarBody(v);
488 switch (whatIs(rhs)) {
491 StgDiscr con = stgConCon(rhs);
492 List args = stgConArgs(rhs);
493 if (isBoxingCon(con)) {
494 doNothing(); /* already done in alloc */
496 AsmSp start = asmBeginPack(bco);
497 map1Proc(pushAtom,bco,reverse(args));
498 asmEndPack(bco,getPos(v),start,stgConInfo(con));
505 StgVar fun = stgAppFun(rhs);
507 List args = stgAppArgs(rhs);
509 if (nonNull(name(fun).stgVar))
510 fun = name(fun).stgVar; else
511 fun = cptrFromName(fun);
515 assert(isName(fun0));
516 itsaPAP = name(fun0).arity > length(args);
517 fprintf ( stderr, "nativeCall: name %s, arity %d, args %d\n",
518 nameFromOPtr(cptrOf(fun)), name(fun0).arity, length(args) );
521 if (nonNull(stgVarBody(fun))
522 && whatIs(stgVarBody(fun)) == LAMBDA
523 && length(stgLambdaArgs(stgVarBody(fun))) > length(args)
529 AsmSp start = asmBeginMkPAP(bco);
530 map1Proc(pushAtom,bco,reverse(args));
532 asmEndMkPAP(bco,getPos(v),start); /* optimisation */
534 AsmSp start = asmBeginMkAP(bco);
535 map1Proc(pushAtom,bco,reverse(args));
537 asmEndMkAP(bco,getPos(v),start);
541 case LAMBDA: /* optimisation */
542 doNothing(); /* already pushed in alloc */
545 /* These two cases look almost identical to the default but they're really
546 * special cases of STGAPP. The essential thing here is that we can't call
547 * cgRhs(rhs) because that expects the rhs to have no free variables when,
548 * in fact, the rhs is _always_ a free variable.
550 * ToDo: a simple optimiser would eliminate all examples
551 * of this except "let x = x in ..."
554 rhs = name(rhs).stgVar;
557 AsmSp start = asmBeginMkAP(bco);
559 asmEndMkAP(bco,getPos(v),start);
564 AsmSp start = asmBeginMkAP(bco); /* make it updateable! */
565 asmClosure(bco,cgRhs(rhs));
566 asmEndMkAP(bco,getPos(v),start);
572 /* --------------------------------------------------------------------------
573 * Top level variables
575 * ToDo: these should be handled by allocating a dynamic unentered CAF
576 * for each top level variable - this should be simpler!
577 * ------------------------------------------------------------------------*/
579 #if 0 /* appears to be unused */
580 static void cgAddVar( AsmObject obj, StgAtom v )
586 asmAddPtr(obj,getObj(v));
591 /* allocate AsmObject for top level variables
592 * any change requires a corresponding change in endTop
594 static void beginTop( StgVar v )
600 switch (whatIs(rhs)) {
603 //List as = stgConArgs(rhs);
604 setObj(v,asmBeginCon(stgConInfo(stgConCon(rhs))));
608 #ifdef CRUDE_PROFILING
609 setObj(v,asmBeginBCO(currentTop));
611 setObj(v,asmBeginBCO(rhs));
615 setObj(v,asmBeginCAF());
620 static void endTop( StgVar v )
622 StgRhs rhs = stgVarBody(v);
624 switch (whatIs(rhs)) {
627 List as = stgConArgs(rhs);
628 AsmCon con = (AsmCon)getObj(v);
629 for( ; nonNull(as); as=tl(as)) {
633 /* should be a delayed combinator! */
634 asmAddPtr(con,(AsmObject)getObj(a));
638 StgVar var = name(a).stgVar;
640 asmAddPtr(con,(AsmObject)getObj(a));
643 #if !USE_ADDR_FOR_STRINGS
645 asmAddPtr(con,asmStringObj(textToStr(textOf(a))));
649 /* asmAddPtr(con,??); */
657 case LAMBDA: /* optimisation */
659 /* ToDo: merge this code with cgLambda */
660 AsmBCO bco = (AsmBCO)getObj(v);
661 AsmSp root = asmBeginArgCheck(bco);
662 map1Proc(cgBind,bco,reverse(stgLambdaArgs(rhs)));
663 asmEndArgCheck(bco,root);
665 cgExpr(bco,root,stgLambdaBody(rhs));
670 default: /* updateable caf */
672 AsmCAF caf = (AsmCAF)getObj(v);
673 asmEndCAF(caf,cgRhs(rhs));
679 static void zap( StgVar v )
682 // stgVarBody(v) = NIL;
685 /* external entry point */
686 Void cgBinds( List binds )
692 if (lastModule() != modulePrelude) {
693 printf("\n\ncgBinds: before ll\n\n" );
694 for (b=binds; nonNull(b); b=tl(b)) {
695 printStg ( stdout, hd(b) ); printf("\n\n");
700 binds = liftBinds(binds);
703 if (lastModule() != modulePrelude) {
704 printf("\n\ncgBinds: after ll\n\n" );
705 for (b=binds; nonNull(b); b=tl(b)) {
706 printStg ( stdout, hd(b) ); printf("\n\n");
711 for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
715 for (b=binds,i=0; nonNull(b); b=tl(b),i++) {
716 //printf("endTop %s\n", maybeName(hd(b)));
720 //mapProc(zap,binds);
723 /* --------------------------------------------------------------------------
724 * Code Generator control:
725 * ------------------------------------------------------------------------*/
731 /* deliberate fall though */
740 /*-------------------------------------------------------------------------*/