[project @ 2000-11-10 15:12:50 by simonpj]
[ghc-hetmet.git] / ghc / interpreter / connect.h
1
2 /* --------------------------------------------------------------------------
3  * Connections between components of the Hugs system
4  *
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.
10  *
11  * $RCSfile: connect.h,v $
12  * $Revision: 1.44 $
13  * $Date: 2000/06/28 10:42:17 $
14  * ------------------------------------------------------------------------*/
15
16 /* --------------------------------------------------------------------------
17  * Connections to Prelude entities:
18  * Texts, Names, Instances, Classes, Types, Kinds and Modules
19  * ------------------------------------------------------------------------*/
20
21 extern Text  textPrelPrim;
22 extern Text  textPrelude;
23 extern Text  textNum;                   /* used to process default decls   */
24 extern Text  textCcall;                 /* used to process foreign import  */
25 extern Text  textStdcall;               /*         ... and foreign export  */
26 extern Text  textPlus;                  /* Used to recognise n+k patterns  */
27
28
29 extern Name  nameFalse, nameTrue;
30 extern Name  nameNil,   nameCons;
31 extern Name  nameJust,  nameNothing;
32 extern Name  nameLeft,  nameRight;
33 extern Name  nameUnit;
34 extern Name  nameLT,      nameEQ;
35 extern Name  nameGT;
36 extern Name  nameFst,     nameSnd;      /* standard combinators            */
37 extern Name  nameId,      nameOtherwise;
38 extern Name  nameNegate,  nameFlip;     /* primitives reqd for parsing     */
39 extern Name  nameFrom,    nameFromThen;
40 extern Name  nameFromTo,  nameFromThenTo;
41 extern Name  nameFatbar,  nameFail;     /* primitives reqd for translation */
42 extern Name  nameIf,      nameSel;
43 extern Name  nameCompAux;
44 extern Name  namePmInt,   namePmFlt;    /* primitives for pattern matching */
45 extern Name  namePmInteger;
46 extern Name  namePmNpk,   namePmSub;    /* primitives for (n+k) patterns   */
47 extern Name  nameError;                 /* For runtime error messages      */
48 extern Name  nameUndefined;             /* A generic undefined value       */
49 extern Name  nameBlackHole;             /* For GC-detected black hole      */
50 extern Name  nameInd;                   /* For dict indirection            */
51 extern Name  nameAnd,     nameOr;       /* For optimisation of && and ||   */
52 extern Name  nameFromInt, nameFromDouble;/*coercion of numerics            */
53 extern Name  nameFromInteger;
54 extern Name  nameEq,      nameCompare;  /* names used for deriving         */
55 extern Name  nameMinBnd,  nameMaxBnd;
56 extern Name  nameIndex,   nameInRange;
57 extern Name  nameRange;
58 extern Name  nameLe,      nameGt;
59 extern Name  nameShowsPrec, nameReadsPrec;
60 extern Name  nameMult,    namePlus;
61 extern Name  nameComp,    nameApp;      /* composition and append          */
62 extern Name  nameShowField;             /* display single field            */
63 extern Name  nameShowParen;             /* wrap with parens                */
64 extern Name  nameReadField;             /* read single field               */
65 extern Name  nameReadParen;             /* unwrap from parens              */
66 extern Name  nameLex;                   /* lexer                           */
67 extern Name  nameRangeSize;             /* calculate size of index range   */
68 extern Name  nameReturn,  nameBind;     /* for translating monad comps     */
69 extern Name  nameMFail;
70 extern Name  nameListMonad;             /* builder function for List Monad */
71 extern Name  namePrint;                 /* printing primitive              */
72 extern Name  nameCreateAdjThunk;        /* f-x-dyn: create adjustor thunk  */
73 extern Name  nameShow;
74 extern Name  namePutStr;
75 extern Name  nameRunIO_toplevel;
76
77 /* The following data constructors are used to make boxed but 
78  * unpointed values pointed and require no special treatment
79  * by the code generator. */
80 extern Name nameMkInteger;
81 extern Name nameMkPrimArray;            
82 extern Name nameMkPrimByteArray;
83 extern Name nameMkRef;                  
84 extern Name nameMkPrimMutableArray;     
85 extern Name nameMkPrimMutableByteArray; 
86 extern Name nameMkThreadId;  
87 extern Name nameMkPrimMVar;  
88 #ifdef PROVIDE_FOREIGN
89 extern Name nameMkForeign;   
90 #endif
91 #ifdef PROVIDE_WEAK
92 extern Name nameMkWeak;
93 #endif
94
95 /* The following data constructors are used to box unboxed
96  * arguments and are treated differently by the code generator.
97  * That is, they have primop `elem` {INT_REP,FLOAT_REP,...}. */
98 #define boxingConRep(con) ((AsmRep)(name(con).primop))
99 #define isBoxingCon(con) (isName(con) && boxingConRep(con) != 0)
100 extern Name nameMkC;
101 extern Name nameMkI;
102 extern Name nameMkW;
103 extern Name nameMkA;
104 extern Name nameMkF;
105 extern Name nameMkD;
106 extern Name nameMkStable;    
107
108 /* used while desugaring */
109 extern Name nameId;
110 extern Name nameOtherwise;
111 extern Name nameUndefined;              /* generic undefined value         */
112
113 /* used in pattern match */
114 extern Name namePmSub;
115 extern Name nameSel;
116
117 /* used in translation */
118 extern Name nameEq;     
119 extern Name namePMFail;
120 extern Name nameEqChar;
121 extern Name nameEqInteger;
122 extern Name namePmInt;
123 extern Name namePmInteger;
124 extern Name namePmDouble;
125 extern Name namePmLe;
126 extern Name namePmSubtract;
127 extern Name namePmFromInteger;
128 extern Name nameMkIO;
129 extern Name nameUnpackString;
130 extern Name namePrimSeq;
131 extern Name nameMap;
132 extern Name nameMinus;
133
134 /* assertion and exceptions */
135 extern Name nameAssert;
136 extern Name nameAssertError;
137 extern Name nameTangleMessage;
138 extern Name nameIrrefutPatError;
139 extern Name nameNoMethodBindingError;
140 extern Name nameNonExhaustiveGuardsError;
141 extern Name namePatError;
142 extern Name nameRecSelError;
143 extern Name nameRecConError;
144 extern Name nameRecUpdError;
145
146
147 extern Class classMonad;                /* Monads                          */
148 extern Class classEq;                   /* `standard' classes              */
149 extern Class classOrd;
150 extern Class classShow;
151 extern Class classRead;
152 extern Class classIx;
153 extern Class classEnum;
154 extern Class classBounded;
155 extern Class classReal;                 /* `numeric' classes               */
156 extern Class classIntegral;
157 extern Class classRealFrac;
158 extern Class classRealFloat;
159 extern Class classFractional;
160 extern Class classFloating;
161 extern Class classNum;
162
163
164 extern Type typeProgIO;                 /* For the IO monad, IO a          */
165 extern Type typeArrow;                  /* Builtin type constructors       */
166 extern Type typeList;
167 extern Type typeUnit;
168 extern Type typeInt64;
169 extern Type typeWord;
170 extern Type typeFloat;
171 extern Type typePrimArray;
172 extern Type typePrimByteArray;
173 extern Type typeRef;
174 extern Type typePrimMutableArray;
175 extern Type typePrimMutableByteArray;
176 extern Type typeStable;
177 extern Type typeWeak;
178 extern Type typeIO;
179 extern Type typeForeign;
180 extern Type typeMVar;
181 extern Type typeThreadId;
182 extern Type typeException;
183 extern Type typeIO;
184 extern Type typeST;
185 extern Type typeOrdering;
186 extern List  stdDefaults;               /* List of standard default types  */
187
188 /* For every primitive type provided by the runtime system,
189  * we construct a Haskell type using a declaration of the form:
190  *
191  *   data Int  -- no constructors given
192  */
193 extern Type typeChar;
194 extern Type typeInt;
195 extern Type typeInteger;
196 extern Type typeWord;
197 extern Type typeAddr;
198 extern Type typePrimArray;            
199 extern Type typePrimByteArray;
200 extern Type typeRef;                  
201 extern Type typePrimMutableArray;     
202 extern Type typePrimMutableByteArray; 
203 extern Type typeFloat;
204 extern Type typeDouble;
205 extern Type typeStable;
206 extern Type typeThreadId;
207 extern Type typeMVar;
208 #ifdef PROVIDE_WEAK
209 extern Type typeWeak;
210 #endif
211 #ifdef PROVIDE_FOREIGN
212 extern Type typeForeign;
213 #endif
214
215 /* And a smaller number of types defined in plain Haskell */
216 extern Type typeList;
217 extern Type typeUnit;
218 extern Type typeString;
219 extern Type typeBool;
220 extern Type typeST;
221 extern Type typeIO;
222 extern Type typeException;
223
224 extern Module modulePrelPrim;
225 extern Module modulePrelude;
226
227 extern Kind   starToStar;                /* Type -> Type                    */
228
229
230 #if TREX
231 extern Name  nameRecExt;                /* Extend a record                 */
232 extern Name  nameRecBrk;                /* Break a record                  */
233 extern Name  nameAddEv;                 /* Addition of evidence values     */
234 extern Name  nameRecSel;                /* Select a record                 */
235 extern Name  nameRecShw;                /* Show a record                   */
236 extern Name  nameShowRecRow;            /* Used to output rows             */
237 extern Name  nameRecEq;                 /* Compare records                 */
238 extern Name  nameEqRecRow;              /* Used to compare rows            */
239 extern Name  nameInsFld;                /* Field insertion routine         */
240 extern Name  nameNoRec;                 /* The empty record                */
241 extern Type  typeNoRow;                 /* The empty row                   */
242 extern Type  typeRec;                   /* Record formation                */
243 extern Kind  extKind;                   /* Kind of extension, *->row->row  */
244 #endif
245
246
247 /* --------------------------------------------------------------------------
248  * Constructions from the above names, types, etc.
249  * ------------------------------------------------------------------------*/
250
251
252 extern Type  arrow;                     /* mkOffset(0) -> mkOffset(1)      */
253 extern Type  listof;                    /* [ mkOffset(0) ]                 */
254 extern Cell  predNum;                   /* Num (mkOffset(0))               */
255 extern Cell  predFractional;            /* Fractional (mkOffset(0))        */
256 extern Cell  predIntegral;              /* Integral (mkOffset(0))          */
257 extern Cell  predMonad;                 /* Monad (mkOffset(0))             */
258
259 extern Type  arrow;                     /* mkOffset(0) -> mkOffset(1)      */
260 extern Type  boundPair;                 /* (mkOffset(0),mkOffset(0))       */
261 extern Type  listof;                    /* [ mkOffset(0) ]                 */
262 extern Type  typeVarToVar;              /* mkOffset(0) -> mkOffset(0)      */
263
264 extern Cell  predNum;                   /* Num (mkOffset(0))               */
265 extern Cell  predFractional;            /* Fractional (mkOffset(0))        */
266 extern Cell  predIntegral;              /* Integral (mkOffset(0))          */
267 extern Kind  starToStar;                /* Type -> Type                    */
268 extern Cell  predMonad;                 /* Monad (mkOffset(0))             */
269
270 #define fn(from,to)  ap(ap(typeArrow,from),to)  /* make type: from -> to   */
271
272 #define aVar            mkOffset(0)     /* Simple skeleton for type var    */
273 extern Type boundPair;                  /* (mkOffset(0),mkOffset(0))       */
274
275 #define consChar(c) ap(nameCons,mkChar(c))
276
277 /* --------------------------------------------------------------------------
278  * Umm ....
279  * ------------------------------------------------------------------------*/
280
281 extern Bool   haskell98;                /* TRUE => Haskell 98 compatibility*/
282 extern Bool   combined;                 /* TRUE => combined operation      */
283 extern Bool   debugSC;                  /* TRUE => print SC to screen  */
284 extern Bool   kindExpert;               /* TRUE => display kind errors in  */
285                                         /*         full detail             */
286 extern Bool   allowOverlap;             /* TRUE => allow overlapping insts */
287
288 extern String repeatStr;                /* Repeat last command string      */
289 extern String hugsEdit;                 /* String for editor command       */
290 extern String hugsPath;                 /* String for file search path     */
291 extern String projectPath;              /* String for project search path  */
292
293 extern Cell*  CStackBase;               /* pointer to base of C stack      */
294
295 extern List   tyconDefns;               /* list of type constructor defns  */
296 extern List   typeInDefns;              /* list of synonym restrictions    */
297 extern List   valDefns;                 /* list of value definitions       */
298 extern List   classDefns;               /* list of class definitions       */
299 extern List   instDefns;                /* list of instance definitions    */
300 extern List   selDefns;                 /* list of selector lists          */
301 extern List   genDefns;                 /* list of generated defns         */
302 extern List   primDefns;                /* list of primitive definitions   */
303 extern List   unqualImports;            /* unqualified import list         */
304 extern List   defaultDefns;             /* default definitions (if any)    */
305 extern Int    defaultLine;              /* line in which default defs occur*/
306 extern List   evalDefaults;             /* defaults for evaluator          */
307 extern Cell   inputExpr;                /* evaluator input expression      */
308 extern Cell   inputContext;             /* evaluator input expression      */
309
310 extern Cell   whnfHead;                 /* head of term in whnf            */
311 extern Int    whnfInt;                  /* integer value of term in whnf   */
312 extern Float  whnfFloat;                /* float value of term in whnf     */
313 extern Long   numCells;                 /* number of cells allocated       */
314 extern Int    numGcs;                   /* number of garbage collections   */
315 extern int    numEnters;                /* number of enters                */
316 extern Bool   preludeLoaded;            /* TRUE => prelude has been loaded */
317 extern Bool   flagAssert;               /* TRUE => assert False <e> causes
318                                                    an assertion failure    */
319
320 extern Bool   gcMessages;               /* TRUE => print GC messages       */
321 extern Bool   literateScripts;          /* TRUE => default lit scripts     */
322 extern Bool   literateErrors;           /* TRUE => report errs in lit scrs */
323 extern Bool   showInstRes;              /*TRUE => show instance resolution */
324
325 extern Int    cutoff;                   /* Constraint Cutoff depth         */
326
327 extern List   diVars;                   /* deriving: cache of names        */
328 extern Int    diNum;                    /* also for deriving               */
329 extern List   cfunSfuns;                /* List of (Cfun,[SelectorVar])    */
330
331 extern Module moduleBeingParsed;        /* so the parser (topModule) knows */
332
333
334 #if USE_PREPROCESSOR
335 extern String preprocessor;             /* preprocessor command            */
336 #endif
337
338
339 /* --------------------------------------------------------------------------
340  * Function prototypes etc...
341  * ------------------------------------------------------------------------*/
342
343
344
345 #define RESET    1            /* reset subsystem                           */
346 #define MARK     2            /* mark parts of graph in use by subsystem   */
347 #define PREPREL  3            /* do startup actions before Prelude loading */
348 #define POSTPREL 4            /* do startup actions after Prelude loading  */
349 #define EXIT     5            /* Take action immediately before exit()     */
350 #define BREAK    6            /* Take action after program break           */
351 #define GCDONE   7            /* Restore subsystem invariants after GC     */
352
353 /* PREPREL was formerly called INSTALL.  POSTPREL doesn't have an analogy
354    in the old Hugs. 
355 */
356 extern  Void   everybody        ( Int );
357 extern  Void   linkControl      ( Int );
358 extern  Void   deriveControl    ( Int );
359 extern  Void   translateControl ( Int );
360 extern  Void   codegen          ( Int );
361 extern  Void   machdep          ( Int );
362 extern  Void   liftControl      ( Int );
363 extern  Void   substitution     ( Int );
364 extern  Void   typeChecker      ( Int );
365 extern  Void   interfayce       ( Int );
366 extern  Void   storage          ( Int );
367
368
369
370 typedef long   Target;
371 extern  Void   setGoal          ( String, Target );
372 extern  Void   soFar            ( Target );
373 extern  Void   done             ( Void );
374 extern  String fromEnv          ( String,String );
375 extern  Bool   chase            ( List );
376
377 extern  Void   input            ( Int );
378 extern  Void   consoleInput     ( String );
379 extern  Void   projInput        ( String );
380 extern  Void   stringInput      ( String );
381 extern  Cell   parseModule      ( String,Long );
382 extern  Void   parseExp         ( Void );
383 #if EXPLAIN_INSTANCE_RESOLUTION
384 extern  Void   parseContext     ( Void );
385 #endif
386 extern  String readFilename     ( Void );
387 extern  String readLine         ( Void );
388 extern  Syntax defaultSyntax    ( Text );
389 extern  Syntax syntaxOf         ( Name );
390 extern  String unlexChar        ( Char,Char );
391 extern  Void   printString      ( String );
392
393
394 extern  Void   staticAnalysis   ( Int );
395 extern  Void   startModule      ( Module );
396 extern  Void   setExportList    ( List );
397 extern  Void   setExports       ( List );
398 extern  Void   addQualImport    ( Text,Text );
399 extern  Void   addUnqualImport  ( Text,List );
400
401 extern  Void   tyconDefn        ( Int,Cell,Cell,Cell );
402 extern  Void   setTypeIns       ( List );
403 extern  Void   clearTypeIns     ( Void );
404 extern  Type   fullExpand       ( Type );
405 extern  Bool   isAmbiguous      ( Type );
406 extern  Void   ambigError       ( Int,String,Cell,Type );
407 extern  Void   classDefn        ( Int,Cell,List,List );
408 extern  Void   instDefn         ( Int,Cell,Cell );
409 extern  Void   addTupInst       ( Class,Int );
410 extern  Name   newDSel          ( Class,Int );
411 #if TREX
412 extern  Inst   addRecShowInst   ( Class,Ext );
413 extern  Inst   addRecEqInst     ( Class,Ext );
414 #endif
415 extern  List   offsetTyvarsIn   ( Type,List );
416
417
418 extern  List   typeVarsIn       ( Cell,List,List,List );
419 extern  List   oclose           ( List,List );
420 extern  List   zonkTyvarsIn     ( Type,List );
421 extern  Type   zonkTyvar        ( Int );
422 extern  Type   zonkType         ( Type,Int );
423 extern  Void   primDefn         ( Cell,List,Cell );
424 extern  Void   defaultDefn      ( Int,List );
425 extern  Void   checkExp         ( Void );
426 extern  Type   conToTagType     ( Tycon );
427 extern  Type   tagToConType     ( Tycon );
428 extern  Int    visitClass       ( Class );
429
430 #if EXPLAIN_INSTANCE_RESOLUTION
431 extern  Void   checkContext     ( Void );
432 #endif
433 extern  Void   checkDefns       ( Module );
434 extern  Bool   h98Pred          ( Bool,Cell );
435 extern  Cell   h98Context       ( Bool,List );
436 extern  Void   h98CheckCtxt     ( Int,String,Bool,List,Inst );
437 extern  Void   h98CheckType     ( Int,String,Cell,Type );
438 extern  Void   h98DoesntSupport ( Int,String );
439
440 extern Int     userArity        ( Name );
441 extern List    deriveEq         ( Tycon );
442 extern List    deriveOrd        ( Tycon );
443 extern List    deriveEnum       ( Tycon );
444 extern List    deriveIx         ( Tycon );
445 extern List    deriveShow       ( Tycon );
446 extern List    deriveRead       ( Cell );
447 extern List    deriveBounded    ( Tycon );
448 extern List    checkPrimDefn    ( Triple );
449
450 extern  Void  foreignImport     ( Cell,Text,Pair,Cell,Cell );
451 extern  Void  foreignExport     ( Cell,Text,Cell,Cell,Cell );
452
453 extern  Void  implementForeignImport ( Name );
454 extern  Text  makeTypeDescrText      ( Type );
455 extern  Void  implementForeignExport ( Name );
456
457 extern  List  foreignExports;            /* foreign export declarations     */
458 extern  List  foreignImports;            /* foreign import declarations     */
459
460 extern  Type   primType         ( Int /*AsmMonad*/ monad, 
461                                   String a_kinds, String r_kinds );
462
463 extern  Type   typeCheckExp     ( Bool );
464 extern  Void   typeCheckDefns   ( Void );
465 extern  Cell   provePred        ( Kinds,List,Cell );
466 extern  List   simpleContext    ( List,Int );
467 extern  Cell   rhsExpr          ( Cell );
468 extern  Int    rhsLine          ( Cell );
469 extern  Bool   isProgType       ( List,Type );
470 extern  Cell   superEvid        ( Cell,Class,Class );
471 extern  Void   linkPreludeTC    ( Void );
472 extern  Void   linkPreludeCM    ( Void );
473 extern  Void   linkPrimNames    ( Void );
474
475 extern  Void   compiler         ( Int );
476 extern  Void   compileDefns     ( Void );
477 extern  Void   compileExp       ( Void );
478 extern  Bool   failFree         ( Cell );
479 extern  Int    discrArity       ( Cell );
480
481 extern  Addr   codeGen          ( Name,Int,Cell );
482 extern  Void   evalExp          ( Void );
483 extern  Int    shellEsc         ( String );
484 extern  Int    getTerminalWidth ( Void );
485 extern  Void   normalTerminal   ( Void );
486 extern  Void   noechoTerminal   ( Void );
487 extern  Int    readTerminalChar ( Void );
488 extern  Void   gcStarted        ( Void );
489 extern  Void   gcScanning       ( Void );
490 extern  Void   gcRecovered      ( Int );
491 extern  Void   gcCStack         ( Void );
492 extern  Void   needPrims        ( Int ); 
493 extern  List   calcFunDepsPreds ( List );
494 extern  Inst   findInstFor      ( Cell,Int );
495 #if MULTI_INST
496 extern  List   findInstsFor     ( Cell,Int );
497 #endif
498
499
500 /*---------------------------------------------------------------------------
501  * Debugging printers, and output-ery
502  *-------------------------------------------------------------------------*/
503
504 extern Void ppScripts           ( Void );
505 extern Void ppModules           ( Void );
506
507 extern Void printStg            ( FILE *fp, Cell /*StgVar*/ b);
508             
509 extern Void ppStg               ( Cell /*StgVar*/ v );
510 extern Void ppStgExpr           ( Cell /*StgExpr*/ e );
511 extern Void ppStgRhs            ( Cell /*StgRhs*/ rhs );
512 extern Void ppStgAlts           ( List alts );
513 extern Void ppStgPrimAlts       ( List alts );
514 extern Void ppStgVars           ( List vs );
515
516 extern Void putChr              ( Int );
517 extern Void putStr              ( String );
518 extern Void putInt              ( Int );
519 extern Void putPtr              ( Ptr );
520
521 extern Void unlexCharConst      ( Cell );
522 extern Void unlexStrConst       ( Text );
523 extern Void unlexVar            ( Text );
524 extern Void unlexVarStr         ( String );
525
526 extern FILE *outputStream;             /* current output stream            */
527 extern Int  outColumn;                 /* current output column number     */
528
529
530 /*---------------------------------------------------------------------------
531  * For dynamic.c and general object-related stuff
532  *-------------------------------------------------------------------------*/
533
534 extern void*     getDLLSymbol   ( Int,String,String );
535 extern Bool      stdcallAllowed ( void );
536
537 #if LEADING_UNDERSCORE
538 #define MAYBE_LEADING_UNDERSCORE(sss)     _##sss
539 #define MAYBE_LEADING_UNDERSCORE_STR(sss) "_" sss
540 #else
541 #define MAYBE_LEADING_UNDERSCORE(sss)     sss
542 #define MAYBE_LEADING_UNDERSCORE_STR(sss) sss
543 #endif
544
545
546 /*---------------------------------------------------------------------------
547  * Interrupting execution (signals, allowBreak):
548  *-------------------------------------------------------------------------*/
549
550 typedef
551    enum { HugsIgnoreBreak, HugsLongjmpOnBreak, HugsRtsInterrupt }
552    HugsBreakAction;
553
554 extern HugsBreakAction currentBreakAction;
555 extern HugsBreakAction setBreakAction ( HugsBreakAction );
556
557
558 #ifndef SIGBREAK /* Sigh, not defined in cygwin32 beta release 16 */
559 # define SIGBREAK 21
560 #endif
561
562 /* ctrlbrk: set the interrupt handler.
563    Hugs relies on being able to do sigprocmask, since some of
564    the signal handlers do longjmps, and this zaps the previous
565    signal mask.  So setHandler needs to do sigprocmask in order
566    to get the signal mask to a sane state each time.
567 */
568 #include <signal.h>
569
570 #if !defined(mingw32_TARGET_OS)
571
572 #define setHandler(bh)   { sigset_t mask; \
573                            signal(SIGINT,bh); \
574                            sigemptyset(&mask); \
575                            sigaddset(&mask, SIGINT); \
576                            sigprocmask(SIG_UNBLOCK, &mask, NULL); \
577                          }
578
579 #else
580
581 #define setHandler(bh)  { void* old_hdlr = signal(SIGINT,bh);\
582                           if (old_hdlr == SIG_ERR) internal("setHandler"); \
583                          }
584
585 #endif /* !defined(mingw32_TARGET_OS) */
586
587 /*---------------------------------------------------------------------------
588  * Environment variables and the registry
589  *-------------------------------------------------------------------------*/
590
591 #define N_INSTALLDIR 200
592 extern char installDir[N_INSTALLDIR];
593
594
595 /*---------------------------------------------------------------------------
596  * File operations:
597  *-------------------------------------------------------------------------*/
598
599 #if HAVE_UNISTD_H
600 # include <sys/types.h>
601 # include <unistd.h>
602 #endif
603
604 extern int      chdir           ( const char* );
605
606 #if HAVE_STDLIB_H
607 # include <stdlib.h>
608 #else
609 extern int      system          ( const char * );
610 extern double   atof            ( const char * );
611 extern void     exit            ( int );
612 #endif
613
614 #ifndef FILENAME_MAX       /* should already be defined in an ANSI compiler*/
615 #define FILENAME_MAX 256
616 #else
617 #if     FILENAME_MAX < 256
618 #undef  FILENAME_MAX
619 #define FILENAME_MAX 256
620 #endif
621 #endif
622
623 /* Hack, hack: if you have dos.h, you probably have a DOS filesystem */
624 #define DOS_FILENAMES              HAVE_DOS_H
625 /* ToDo: can we replace this with a feature test? */
626 #define MAC_FILENAMES              SYMANTEC_C
627
628 #define CASE_INSENSITIVE_FILENAMES (DOS_FILENAMES | RISCOS)
629
630 #if CASE_INSENSITIVE_FILENAMES
631 # if HAVE_STRCASECMP
632 #  define filenamecmp(s1,s2) strcasecmp(s1,s2)
633 # elif HAVE__STRICMP
634 #  define filenamecmp(s1,s2) _stricmp(s1,s2)
635 # elif HAVE_STRICMP
636 #  define filenamecmp(s1,s2) stricmp(s1,s2)
637 # elif HAVE_STRCMPI
638 #  define filenamecmp(s1,s2) strcmpi(s1,s2)
639 # endif
640 #else
641 # define filenamecmp(s1,s2) strcmp(s1,s2)
642 #endif
643
644 #define HI_ENDING ".u_hi"
645
646
647 /*---------------------------------------------------------------------------
648  * Pipe-related operations:
649  *
650  * On Windows, many standard Unix names acquire a leading underscore.
651  * Irritating, but easy to work around.
652  *-------------------------------------------------------------------------*/
653
654 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
655 #define popen(x,y) _popen(x,y)
656 #endif
657 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
658 #define pclose(x) _pclose(x)
659 #endif
660
661
662 /*---------------------------------------------------------------------------
663  * Bit manipulation:
664  *-------------------------------------------------------------------------*/
665
666 #define bitArraySize(n)    ((n)/bitsPerWord + 1)
667 #define placeInSet(n)      ((-(n)-1)>>wordShift)
668 #define maskInSet(n)       (1<<((-(n)-1)&wordMask))
669
670
671 /*---------------------------------------------------------------------------
672  * Function prototypes for code in machdep.c
673  *-------------------------------------------------------------------------*/
674
675 extern  String findMPathname    ( String,String,String );
676 extern  String findPathname     ( String,String );
677 extern  Int    shellEsc         ( String );
678 extern  Int    getTerminalWidth ( Void );
679 extern  Void   normalTerminal   ( Void );
680 extern  Void   noechoTerminal   ( Void );
681 extern  Int    readTerminalChar ( Void );
682 extern  Void   gcStarted        ( Void );
683 extern  Void   gcScanning       ( Void );
684 extern  Void   gcRecovered      ( Int );
685 extern  Void   gcCStack         ( Void );
686
687
688 /*---------------------------------------------------------------------------
689  * To do with reading interface and object files
690  *-------------------------------------------------------------------------*/
691
692 extern Cell   parseInterface        ( String,Long );
693 extern List   getInterfaceImports   ( Cell );
694 extern void   processInterfaces     ( List );
695 extern Void   getFileSize           ( String, Long * );
696 extern Void   ifLinkConstrItbl      ( Name n );
697 extern Void   hi_o_namesFromSrcName ( String,String*,String* oName );
698 extern void*  lookupObjName         ( char* );
699
700 extern String getExtraObjectInfo    ( String primaryObjectName,
701                                       String extraFileName,
702                                       Int*   extraFileSize );
703
704 extern List /* of ZTriple(I_INTERFACE, 
705                           Text--name of obj file, 
706                           Int--size of obj file) */
707              ifaces_outstanding;
708
709
710 /* --------------------------------------------------------------------------
711  * Interpreter command structure
712  * ------------------------------------------------------------------------*/
713
714 typedef Int Command;
715
716 struct cmd {
717     String cmdString;
718     Command cmdCode;
719 };
720
721 extern Command readCommand      ( struct cmd *, Char, Char );
722
723 #define EDIT    0
724 #define FIND    1
725 #define LOAD    2
726 #define ALSO    3
727 #define PROJECT 4
728 #define RELOAD  5
729 #define EVAL    6
730 #define TYPEOF  7
731 #define HELP    8
732 #define NAMES   9
733 #define BADCMD  10
734 #define SET     11
735 #define QUIT    12
736 #define SYSTEM  13
737 #define CHGDIR  14
738 #define INFO    15
739 #define COLLECT 16
740 #define SETMODULE 17
741 #define DUMP    18
742 #define STATS   19
743 #define BROWSE  20
744 #define XPLAIN  21
745 #define PNTVER  22
746 #define NOCMD   23
747
748
749 /* --------------------------------------------------------------------------
750  * STG Syntax:
751  * 
752  *   Rhs     -> STGCON   (Con, [Atom])
753  *            | STGAPP   (Var, [Atom])     -- delayed application
754  *            | Expr                       
755  *                                         
756  *   Expr    -> LETREC   ([Var],Expr)      -- Vars contain their bound value
757  *            | LAMBDA   ([Var],Expr)      -- all vars bound to NIL
758  *            | CASE     (Expr,[Alt])      -- algebraic case
759  *            | PRIMCASE (Expr,[PrimAlt])  -- primitive case
760  *            | STGPRIM  (Prim,[Atom])     
761  *            | STGAPP   (Var, [Atom])     -- tail call
762  *            | Var                        -- Abbreviation for STGAPP(Var,[])
763  *                                         
764  *   Atom    -> Var                        
765  *            | CHAR                       -- unboxed
766  *            | INT                        -- unboxed
767  *            | BIGNUM                     -- unboxed
768  *            | FLOAT                      -- unboxed
769  *            | ADDR                       -- unboxed
770  *            | STRING                     -- boxed
771  *                                         
772  *   Var     -> STGVAR   (Rhs,StgRep,info) -- let, case or lambda bound
773  *            | Name                       -- let-bound (effectively)
774  *                                         -- always unboxed (PTR_REP)
775  *
776  *   Alt     -> DEEFALT (Var,Expr)         -- var bound to NIL
777  *            | CASEALT (Con,[Var],Expr)   -- vars bound to NIL; 
778  *                                         -- Con is Name or TUPLE
779  *   PrimAlt -> PRIMALT ([Var],Expr)       -- vars bound to NIL or int
780  * 
781  * We use pointer equality to distinguish variables.
782  * The info field of a Var is used as follows in various phases:
783  * 
784  * Translation:      unused (set to NIL on output)
785  * Freevar analysis: list of free vars after
786  * Lambda lifting:   freevar list or UNIT on input, discarded after
787  * Code generation:  unused
788  * ------------------------------------------------------------------------*/
789
790 typedef Cell   StgRhs;
791 typedef Cell   StgExpr;
792 typedef Cell   StgAtom;
793 typedef Cell   StgVar;       /* Could be a Name or an STGVAR */
794 typedef Cell   StgCaseAlt;
795 typedef Cell   StgPrimAlt;
796 typedef Cell   StgDiscr;
797 typedef Cell   StgRep;  /* PTR_REP | .. DOUBLE_REP */
798
799 #define mkStgLet(binds,body)       ap(LETREC,pair(binds,body))
800 #define stgLetBinds(e)             fst(snd(e))
801 #define stgLetBody(e)              snd(snd(e))
802
803 #define mkStgPrimVar(rhs,rep,info) ap(STGVAR,triple(rhs,rep,info))
804 #define stgVarBody(e)              fst3(snd(e))
805 #define stgVarRep(e)               snd3(snd(e))
806 #define stgVarInfo(e)              thd3(snd(e))
807
808 #define mkStgCase(scrut,alts)      ap(CASE,pair(scrut,alts))
809 #define stgCaseScrut(e)            fst(snd(e))
810 #define stgCaseAlts(e)             snd(snd(e))
811
812 #define mkStgCaseAlt(con,vs,e)     ap(CASEALT,triple(con,vs,e))
813 #define stgCaseAltCon(alt)         fst3(snd(alt))
814 #define stgCaseAltVars(alt)        snd3(snd(alt))
815 #define stgCaseAltBody(alt)        thd3(snd(alt))
816
817 #define mkStgDefault(v,e)          ap(DEEFALT,pair(v,e))
818 #define stgDefaultVar(alt)         fst(snd(alt))
819 #define stgDefaultBody(alt)        snd(snd(alt))
820 #define isDefaultAlt(alt)          (fst(alt)==DEEFALT)
821
822 #define mkStgPrimCase(scrut,alts)  ap(PRIMCASE,pair(scrut,alts))
823 #define stgPrimCaseScrut(e)        fst(snd(e))
824 #define stgPrimCaseAlts(e)         snd(snd(e))
825
826 #define mkStgPrimAlt(vs,body)      ap(PRIMALT,pair(vs,body))
827 #define stgPrimAltVars(alt)        fst(snd(alt))
828 #define stgPrimAltBody(alt)        snd(snd(alt))
829
830 #define mkStgApp(fun,args)         ap(STGAPP,pair(fun,args))
831 #define stgAppFun(e)               fst(snd(e))
832 #define stgAppArgs(e)              snd(snd(e))
833
834 #define mkStgPrim(op,args)         ap(STGPRIM,pair(op,args))
835 #define stgPrimOp(e)               fst(snd(e))
836 #define stgPrimArgs(e)             snd(snd(e))
837
838 #define mkStgCon(con,args)         ap(STGCON,pair(con,args))
839 #define stgConCon(e)               fst(snd(e))
840 #define stgConArgs(e)              snd(snd(e))
841
842 #define mkStgLambda(args,body)     ap(LAMBDA,pair(args,body))
843 #define stgLambdaArgs(e)           fst(snd(e))
844 #define stgLambdaBody(e)           snd(snd(e))
845
846
847 /* --------------------------------------------------------------------------
848  * Utility functions for manipulating STG syntax trees.
849  * ------------------------------------------------------------------------*/
850
851 extern int     stgConTag        ( StgDiscr d );
852 extern void*   stgConInfo       ( StgDiscr d );
853 extern int     stgDiscrTag      ( StgDiscr d );
854
855 extern List    makeArgs         ( Int );
856 extern StgExpr makeStgLambda    ( List args,  StgExpr body );
857 extern StgExpr makeStgApp       ( StgVar fun, List args );
858 extern StgExpr makeStgLet       ( List binds, StgExpr body );
859 extern StgExpr makeStgIf        ( StgExpr cond, StgExpr e1, StgExpr e2 );
860 extern Bool    isStgVar         ( StgRhs rhs );
861 extern Bool    isAtomic         ( StgRhs rhs );
862 extern StgVar  mkStgVar         ( StgRhs rhs, Cell info );
863
864 #define mkStgRep(c) mkChar(c)
865
866
867 /* --------------------------------------------------------------------------
868  * STG/backendish functions
869  * ------------------------------------------------------------------------*/
870
871 extern  Void  stgDefn                ( Name n, Int arity, Cell e );
872
873 extern  Void  implementForeignImport ( Name );
874 extern  Void  implementForeignExport ( Name );
875 extern  Void  implementCfun          ( Name, List );
876 extern  Void  implementConToTag      ( Tycon );
877 extern  Void  implementTagToCon      ( Tycon );
878 extern  Void  implementPrim          ( Name );
879 extern  Void  implementTuple         ( Int );
880 #if TREX                         
881 extern  Name  implementRecShw        ( Text );
882 extern  Name  implementRecEq         ( Text );
883 #endif
884
885 extern void    liftModule       ( Module );
886 extern StgExpr substExpr        ( List sub, StgExpr e );
887 extern List    freeVarsBind     ( List, StgVar );
888
889
890 extern Void    cgModule         ( Module );
891 extern char*   lookupHugsName   ( void* );
892
893
894 /* --------------------------------------------------------------------------
895  * Definitions for substitution data structure and operations.
896  * ------------------------------------------------------------------------*/
897
898 typedef struct {                        /* Each type variable contains:    */
899     Type bound;                         /* A type skeleton (unbound==NIL)  */
900     Int  offs;                          /* Offset for skeleton             */
901     Kind kind;                          /* kind annotation                 */
902 } Tyvar;
903
904 extern  Tyvar           *tyvars;        /* storage for type variables      */
905 extern  Int             typeOff;        /* offset of result type           */
906 extern  Type            typeIs;         /* skeleton of result type         */
907 extern  Int             typeFree;       /* freedom in instantiated type    */
908 extern  List            predsAre;       /* list of predicates in type      */
909 extern  List            genericVars;    /* list of generic vars            */
910 extern  List            btyvars;        /* explicitly scoped type vars     */
911
912 #define tyvar(n)        (tyvars+(n))    /* nth type variable               */
913 #define tyvNum(t)       ((t)-tyvars)    /* and the corresp. inverse funct. */
914 #define isBound(t)      (((t)->bound) && ((t)->bound!=SKOLEM))
915 #define aVar            mkOffset(0)     /* Simple skeletons for type vars  */
916 #define bVar            mkOffset(1)
917 #define enterBtyvs()    btyvars = cons(NIL,btyvars)
918 #define leaveBtyvs()    btyvars = tl(btyvars)
919
920 #define deRef(tyv,t,o)  while ((tyv=getTypeVar(t,o)) && isBound(tyv)) { \
921                             t = tyv->bound;                             \
922                             o = tyv->offs;                              \
923                         }
924
925                                         /* offs values when isNull(bound): */
926 #define FIXED_TYVAR     0               /* fixed in current assumption     */
927 #define UNUSED_GENERIC  1               /* not fixed, not yet encountered  */
928 #define GENERIC         2               /* GENERIC+n==nth generic var found*/
929
930 extern  char            *unifyFails;    /* Unification error message       */
931
932 extern Void  emptySubstitution  ( Void );
933 extern Int   newTyvars          ( Int );
934 #define      newKindvars(n)     newTyvars(n)
935 extern Int   newKindedVars      ( Kind );
936 extern Kind  simpleKind         ( Int );
937 extern Void  instantiate        ( Type );
938
939 extern Pair  findBtyvs          ( Text );
940 extern Void  markBtyvs          ( Void );
941 extern Type  localizeBtyvs      ( Type );
942
943 extern Tyvar *getTypeVar        ( Type,Int );
944 extern Void  tyvarType          ( Int );
945 extern Void  bindTv             ( Int,Type,Int );
946 extern Cell  getDerefHead       ( Type,Int );
947 extern Void  expandSyn          ( Tycon, Int, Type *, Int * );
948
949 extern Void  clearMarks         ( Void );
950 extern Void  markAllVars        ( Void );
951 extern Void  resetGenerics      ( Void );
952 extern Void  markTyvar          ( Int );
953 extern Void  markType           ( Type,Int );
954 extern Void  markPred           ( Cell );
955
956 extern Type  copyTyvar          ( Int );
957 extern Type  copyType           ( Type,Int );
958 extern Cell  copyPred           ( Cell,Int );
959 extern Type  dropRank2          ( Type,Int,Int );
960 extern Type  dropRank1          ( Type,Int,Int );
961 extern Void  liftRank2Args      ( List,Int,Int );
962 extern Type  liftRank2          ( Type,Int,Int );
963 extern Type  liftRank1          ( Type,Int,Int );
964 #ifdef DEBUG_TYPES
965 extern Type  debugTyvar         ( Int );
966 extern Type  debugType          ( Type,Int );
967 #endif
968 extern Kind  copyKindvar        ( Int );
969 extern Kind  copyKind           ( Kind,Int );
970
971 extern Bool  eqKind             ( Kind,Kind );
972 extern Kind  getKind            ( Cell,Int );
973
974 extern List  genvarTyvar        ( Int,List );
975 extern List  genvarType         ( Type,Int,List );
976
977 extern Bool  doesntOccurIn      ( Tyvar*,Type,Int );
978 extern Bool  unify              ( Type,Int,Type,Int );
979 extern Bool  kunify             ( Kind,Int,Kind,Int );
980
981 extern Void  typeTuple          ( Cell );
982 extern Void  varKind            ( Int );
983
984 extern Bool  samePred           ( Cell,Int,Cell,Int );
985 extern Bool  matchPred          ( Cell,Int,Cell,Int );
986 extern Bool  unifyPred          ( Cell,Int,Cell,Int );
987 extern Inst  findInstFor        ( Cell,Int );
988
989 extern Void  improve            ( Int,List,List );
990 extern Void  improve1           ( Int,List,Cell,Int );
991
992 extern Bool  sameSchemes        ( Type,Type );
993 extern Bool  sameType           ( Type,Int,Type,Int );
994 extern Bool  matchType          ( Type,Int,Type,Int );
995 extern Bool  typeMatches        ( Type,Type );
996
997 #ifdef DEBUG
998 extern Void  checkBytecodeCount  ( Void );
999 #endif
1000 /*-------------------------------------------------------------------------*/