2 /* --------------------------------------------------------------------------
5 * Copyright (c) The University of Nottingham and Yale University, 1994-1997.
6 * All rights reserved. See NOTICE for details and conditions of use etc...
7 * Hugs version 1.4, December 1997
11 * $Date: 1999/02/03 17:08:39 $
12 * ------------------------------------------------------------------------*/
19 #include "link.h" /* for nameTrue/False */
20 #include "Assembler.h" /* for AsmRep and primops */
22 /* --------------------------------------------------------------------------
24 * ------------------------------------------------------------------------*/
26 int stgConTag( StgDiscr d )
34 internal("stgConTag");
38 void* stgConInfo( StgDiscr d )
42 return asmMkInfo(cfunOf(d),name(d).arity);
44 return asmMkInfo(0,tupleOf(d));
46 internal("stgConInfo");
50 /* ToDo: identical to stgConTag */
51 int stgDiscrTag( StgDiscr d )
59 internal("stgDiscrTag");
63 /* --------------------------------------------------------------------------
64 * Utility functions for manipulating STG syntax trees.
65 * ------------------------------------------------------------------------*/
67 List makeArgs( Int n )
71 args = cons(mkStgVar(NIL,NIL),args);
76 StgExpr makeStgLambda( List args, StgExpr body )
81 if (whatIs(body) == LAMBDA) {
82 return mkStgLambda(dupOnto(args,stgLambdaArgs(body)),
85 return mkStgLambda(args,body);
90 StgExpr makeStgApp( StgVar fun, List args )
95 return mkStgApp(fun,args);
99 StgExpr makeStgLet( List binds, StgExpr body )
104 return mkStgLet(binds,body);
108 StgExpr makeStgIf( StgExpr cond, StgExpr e1, StgExpr e2 )
110 if (cond == nameTrue) {
112 } else if (cond == nameFalse) {
115 return mkStgCase(cond,doubleton(mkStgCaseAlt(nameTrue,NIL,e1),
116 mkStgCaseAlt(nameFalse,NIL,e2)));
147 StgVar mkStgVar( StgRhs rhs, Cell info )
149 return ap(STGVAR,triple(rhs,mkStgRep(PTR_REP),info));
152 /*-------------------------------------------------------------------------*/
154 /* --------------------------------------------------------------------------
157 * Copyright (c) The University of Nottingham and Yale University, 1994-1997.
158 * All rights reserved. See NOTICE for details and conditions of use etc...
159 * Hugs version 1.4, December 1997
161 * $RCSfile: stg.c,v $
163 * $Date: 1999/02/03 17:08:39 $
164 * ------------------------------------------------------------------------*/
166 /* --------------------------------------------------------------------------
168 * ------------------------------------------------------------------------*/
170 static Void local pIndent Args((Int));
171 static Void local unlexVar Args((Text));
172 static Void local unlexCharConst Args((Cell));
173 static Void local unlexStrConst Args((Text));
175 static Void local putStgVar Args((StgVar));
176 static Void local putStgVars Args((List));
177 static Void local putStgAtom Args((StgAtom a));
178 static Void local putStgAtoms Args((List as));
179 static Void local putStgBinds Args((List));
180 static Void local putStgExpr Args((StgExpr));
181 static Void local putStgRhs Args((StgRhs));
182 static Void local putStgPat Args((StgPat));
183 static Void local putStgPrimPat Args((StgPrimPat));
185 /* --------------------------------------------------------------------------
186 * Basic output routines:
187 * ------------------------------------------------------------------------*/
189 static FILE *outputStream; /* current output stream */
190 static Int outColumn = 0; /* current output column number */
192 static Void local putChr( Int c );
193 static Void local putStr( String s );
194 static Void local putInt( Int n );
195 static Void local putPtr( Ptr p );
197 static Void local putChr(c) /* print single character */
199 Putc(c,outputStream);
203 static Void local putStr(s) /* print string */
206 Putc(*s,outputStream);
211 static Void local putInt(n) /* print integer */
213 static char intBuf[16];
214 sprintf(intBuf,"%d",n);
218 static Void local putPtr(p) /* print pointer */
220 static char intBuf[16];
221 sprintf(intBuf,"%p",p);
225 /* --------------------------------------------------------------------------
226 * Indentation and showing names/constants
227 * ------------------------------------------------------------------------*/
229 static Void local pIndent(n) /* indent to particular position */
233 Putc(' ',outputStream);
237 static Void local unlexVar(t) /* print text as a variable name */
238 Text t; { /* operator symbols must be enclosed*/
239 String s = textToStr(t); /* in parentheses... except [] ... */
241 if ((isascii(s[0]) && isalpha(s[0])) || s[0]=='_' || s[0]=='[' || s[0]=='(')
250 static Void local unlexCharConst(c)
253 putStr(unlexChar(c,'\''));
257 static Void local unlexStrConst(t)
259 String s = textToStr(t);
260 static Char SO = 14; /* ASCII code for '\SO' */
261 Bool lastWasSO = FALSE;
262 Bool lastWasDigit = FALSE;
263 Bool lastWasEsc = FALSE;
267 String ch = unlexChar(*s,'\"');
270 if ((lastWasSO && *ch=='H') ||
271 (lastWasEsc && lastWasDigit && isascii(*ch) && isdigit(*ch)))
274 lastWasEsc = (*ch=='\\');
275 lastWasSO = (*s==SO);
276 for (; *ch; c = *ch++)
278 lastWasDigit = (isascii(c) && isdigit(c));
283 /* --------------------------------------------------------------------------
284 * Pretty printer for stg code:
285 * ------------------------------------------------------------------------*/
287 static Void putStgAlts ( Int left, List alts );
288 static Void putStgPrimAlt ( Int left, List vs, StgExpr body );
290 static Void local putStgVar(StgVar v)
293 unlexVar(name(v).text);
300 static Void local putStgVars( List vs )
302 for(; nonNull(vs); vs=tl(vs)) {
308 static Void local putStgAtom( StgAtom a )
316 unlexCharConst(charOf(a));
324 putStr(bignumToString(a));
328 putStr(floatToString(a));
332 unlexStrConst(textOf(a));
339 fprintf(stderr,"\nYoiks: "); printExp(stderr,a);
340 internal("putStgAtom");
344 Void putStgAtoms( List as )
347 while (nonNull(as)) {
357 Void putStgPat( StgPat pat )
360 if (nonNull(stgVarBody(pat))) {
361 StgDiscr d = stgConCon(stgVarBody(pat));
362 List vs = stgConArgs(stgVarBody(pat));
367 unlexVar(name(d).text);
368 for (; nonNull(vs); vs=tl(vs)) {
379 while (nonNull(vs)) {
388 fprintf(stderr,"\nYoiks: "); printExp(stderr,d);
389 internal("putStgPat");
394 Void putStgPrimPat( StgPrimPat pat )
397 if (nonNull(stgVarBody(pat))) {
398 StgExpr d = stgVarBody(pat);
408 fprintf(stderr,"\nYoiks: "); printExp(stderr,d);
409 internal("putStgPrimPat");
415 Void putStgBinds(binds) /* pretty print locals */
417 Int left = outColumn;
420 while (nonNull(binds)) {
421 Cell bind = hd(binds);
424 putStgRhs(stgVarBody(bind));
434 static Void putStgAlts( Int left, List alts )
436 if (length(alts) == 1) {
437 StgCaseAlt alt = hd(alts);
439 putStgPat(stgCaseAltPat(alt));
442 putStgExpr(stgCaseAltBody(alt));
446 for (; nonNull(alts); alts=tl(alts)) {
447 StgCaseAlt alt = hd(alts);
449 putStgPat(stgCaseAltPat(alt));
451 putStgExpr(stgCaseAltBody(alt));
459 static Void putStgPrimAlts( Int left, List alts )
461 if (length(alts) == 1) {
462 StgPrimAlt alt = hd(alts);
464 mapProc(putStgPrimPat,stgPrimAltPats(alt));
467 putStgExpr(stgPrimAltBody(alt));
471 for (; nonNull(alts); alts=tl(alts)) {
472 StgPrimAlt alt = hd(alts);
474 mapProc(putStgPrimPat,stgPrimAltPats(alt));
476 putStgExpr(stgPrimAltBody(alt));
484 Void putStgExpr( StgExpr e ) /* pretty print expr */
488 putStgBinds(stgLetBinds(e));
489 putStgExpr(stgLetBody(e));
493 Int left = outColumn;
495 putStgVars(stgLambdaArgs(e));
498 putStgExpr(stgLambdaBody(e));
503 Int left = outColumn;
505 putStgExpr(stgCaseScrut(e));
507 putStgAlts(left,stgCaseAlts(e));
512 Int left = outColumn;
514 putStgExpr(stgPrimCaseScrut(e));
516 putStgPrimAlts(left,stgPrimCaseAlts(e));
521 Cell op = stgPrimOp(e);
522 unlexVar(name(op).text);
523 putStgAtoms(stgPrimArgs(e));
527 putStgVar(stgAppFun(e));
528 putStgAtoms(stgAppArgs(e));
535 fprintf(stderr,"\nYoiks: "); printExp(stderr,e);
536 internal("putStgExpr");
540 Void putStgRhs( StgRhs e ) /* print lifted definition */
545 Name con = stgConCon(e);
548 putInt(tupleOf(con));
550 unlexVar(name(con).text);
552 putStgAtoms(stgConArgs(e));
561 static void beginStgPP( FILE* fp );
562 static void endStgPP( FILE* fp );
564 static void beginStgPP( FILE* fp )
571 static void endStgPP( FILE* fp )
576 Void printStg(fp,b) /* Pretty print sc defn on fp */
583 putStgRhs(stgVarBody(b));
589 Void ppStg( StgVar v )
596 Void ppStgExpr( StgExpr e )
605 Void ppStgRhs( StgRhs rhs )
614 Void ppStgAlts( List alts )
623 extern Void ppStgPrimAlts( List alts )
627 putStgPrimAlts(0,alts);
632 extern Void ppStgVars( List vs )
644 /*-------------------------------------------------------------------------*/