summaryrefslogtreecommitdiff
path: root/ghc/interpreter/link.c
blob: f5bfdfd3de8c1f319ca011e3f8c77fbd47b57f78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611

/* --------------------------------------------------------------------------
 * Load symbols required from the Prelude
 *
 * Copyright (c) The University of Nottingham and Yale University, 1994-1997.
 * All rights reserved. See NOTICE for details and conditions of use etc...
 * Hugs version 1.4, December 1997
 *
 * $RCSfile: link.c,v $
 * $Revision: 1.8 $
 * $Date: 1999/10/15 11:02:15 $
 * ------------------------------------------------------------------------*/

#include "prelude.h"
#include "storage.h"
#include "backend.h"
#include "connect.h"
#include "errors.h"
#include "Assembler.h" /* for asmPrimOps and AsmReps */

#include "link.h"


Type typeArrow;                         /* Function spaces                 */

Type typeChar;
Type typeInt;
Type typeInteger;
Type typeWord;
Type typeAddr;
Type typePrimArray;            
Type typePrimByteArray;
Type typeRef;                  
Type typePrimMutableArray;     
Type typePrimMutableByteArray; 
Type typeFloat;
Type typeDouble;
Type typeStable;
#ifdef PROVIDE_WEAK
Type typeWeak;
#endif
#ifdef PROVIDE_FOREIGN
Type typeForeign;
#endif
#ifdef PROVIDE_CONCURRENT
Type typeThreadId;
Type typeMVar;
#endif

Type typeList;
Type typeUnit;
Type typeString;
Type typeBool;
Type typeST;
Type typeIO;
Type typeException;

Class classEq;                          /* `standard' classes              */
Class classOrd;
Class classShow;
Class classRead;
Class classIx;
Class classEnum;
Class classBounded;

Class classReal;                        /* `numeric' classes               */
Class classIntegral;
Class classRealFrac;
Class classRealFloat;
Class classFractional;
Class classFloating;
Class classNum;
Class classMonad;                       /* Monads and monads with a zero   */

List stdDefaults;                       /* standard default values         */

Name nameTrue;    
Name nameFalse;            /* primitive boolean constructors  */
Name nameNil;     
Name nameCons;             /* primitive list constructors     */
Name nameUnit;                          /* primitive Unit type constructor */

Name nameEq;    
Name nameFromInt;
Name nameFromDouble;       /* coercion of numerics            */
Name nameFromInteger;
Name nameReturn;  
Name nameBind;             /* for translating monad comps     */
Name nameZero;                          /* for monads with a zero          */

Name nameId;
Name nameRunIO;
Name namePrint;

Name nameOtherwise;
Name nameUndefined;                     /* generic undefined value         */
#if NPLUSK
Name namePmSub; 
#endif
Name namePMFail;
Name nameEqChar;
Name nameEqInt;
#if !OVERLOADED_CONSTANTS
Name nameEqInteger;
#endif
Name nameEqDouble;
Name namePmInt;
Name namePmInteger;
Name namePmDouble;
Name namePmLe;
Name namePmSubtract;
Name namePmFromInteger;
Name nameMkIO;
Name nameRunST;
Name nameUnpackString;
Name nameError;
Name nameInd;
Name nameCreateAdjThunk;

Name nameAnd;
Name nameConCmp;
Name nameCompAux;
Name nameEnFrTh;
Name nameEnFrTo;
Name nameEnFrom;
Name nameEnFrEn;
Name nameEnToEn;
Name nameEnInRng;
Name nameEnIndex;
Name nameEnRange;
Name nameRangeSize;
Name nameComp;
Name nameShowField;
Name nameApp;
Name nameShowParen;
Name nameReadParen;
Name nameLex;
Name nameReadField;
Name nameFlip;

Name namePrimSeq;
Name namePrimCatch;
Name namePrimRaise;

Name nameFromTo;
Name nameFromThen;
Name nameFrom;
Name nameFromThenTo;
Name nameNegate;

/* these names are required before we've had a chance to do the right thing */
Name nameSel;
Name nameUnsafeUnpackCString;

/* constructors used during translation and codegen */
Name nameMkC;                           /* Char#        -> Char           */
Name nameMkI;                           /* Int#         -> Int            */
Name nameMkInteger;                     /* Integer#     -> Integer        */
Name nameMkW;                           /* Word#        -> Word           */
Name nameMkA;                           /* Addr#        -> Addr            */
Name nameMkF;                           /* Float#       -> Float           */
Name nameMkD;                           /* Double#      -> Double          */
Name nameMkPrimArray;            
Name nameMkPrimByteArray;
Name nameMkRef;                  
Name nameMkPrimMutableArray;     
Name nameMkPrimMutableByteArray; 
Name nameMkStable;                      /* StablePtr# a -> StablePtr a     */
#ifdef PROVIDE_WEAK
Name nameMkWeak;                        /* Weak# a      -> Weak a          */
#endif
#ifdef PROVIDE_FOREIGN
Name nameMkForeign;                     /* ForeignObj#  -> ForeignObj      */
#endif
#ifdef PROVIDE_CONCURRENT
Name nameMkThreadId;                    /* ThreadId#    -> ThreadId        */
Name nameMkMVar;                        /* MVar#        -> MVar            */
#endif



Name nameMinBnd;
Name nameMaxBnd;
Name nameCompare;
Name nameShowsPrec;
Name nameIndex;
Name nameReadsPrec; 
Name nameRange;
Name nameEQ;
Name nameInRange;
Name nameGt;
Name nameLe;
Name namePlus;
Name nameMult;
Name nameMFail;
Type typeOrdering;
Module modulePrelude;
Name nameMap;
Name nameMinus;


/* --------------------------------------------------------------------------
 * Frequently used type skeletons:
 * ------------------------------------------------------------------------*/

Type  arrow;                     /* mkOffset(0) -> mkOffset(1)      */
Type  boundPair;                 /* (mkOffset(0),mkOffset(0))       */
Type  listof;                    /* [ mkOffset(0) ]                 */
Type  typeVarToVar;              /* mkOffset(0) -> mkOffset(0)      */

Cell  predNum;                   /* Num (mkOffset(0))               */
Cell  predFractional;            /* Fractional (mkOffset(0))        */
Cell  predIntegral;              /* Integral (mkOffset(0))          */
Kind  starToStar;                /* Type -> Type                    */
Cell  predMonad;                 /* Monad (mkOffset(0))             */

/* --------------------------------------------------------------------------
 * 
 * ------------------------------------------------------------------------*/

static Tycon linkTycon ( String s );
static Tycon linkClass ( String s );
static Name  linkName  ( String s );
static Void  mkTypes   ( void );
static Name  predefinePrim ( String s );


static Tycon linkTycon( String s )
{
    Tycon tc = findTycon(findText(s));
    if (nonNull(tc)) {
        return tc;
    }
    ERRMSG(0) "Prelude does not define standard type \"%s\"", s
    EEND;
}

static Class linkClass( String s )
{
    Class cc = findClass(findText(s));
    if (nonNull(cc)) {
        return cc;
    }
    ERRMSG(0) "Prelude does not define standard class \"%s\"", s
    EEND;
}

static Name linkName( String s )
{
    Name n = findName(findText(s));
    if (nonNull(n)) {
        return n;
    }
    ERRMSG(0) "Prelude does not define standard name \"%s\"", s
    EEND;
}

static Name predefinePrim ( String s )
{
    Name nm;
    Text t = findText(s);
    nm = findName(t);
    if (nonNull(nm)) {
       //fprintf(stderr, "predefinePrim: %s already exists\n", s );
    } else {
       nm = newName(t,NIL);
       name(nm).defn=PREDEFINED;
    }
    return nm;
}

Void linkPreludeTC(void) {              /* Hook to tycons and classes in   */
    static Bool initialised = FALSE;    /* prelude when first loaded       */
    if (!initialised) {
        Int i;
        initialised = TRUE;
        setCurrModule(modulePrelude);

        typeChar         = linkTycon("Char");
        typeInt          = linkTycon("Int");
        typeInteger      = linkTycon("Integer");
        typeWord         = linkTycon("Word");
        typeAddr         = linkTycon("Addr");
        typePrimArray            = linkTycon("PrimArray");
        typePrimByteArray        = linkTycon("PrimByteArray");
        typeRef                  = linkTycon("Ref");
        typePrimMutableArray     = linkTycon("PrimMutableArray");
        typePrimMutableByteArray = linkTycon("PrimMutableByteArray");
        typeFloat        = linkTycon("Float");
        typeDouble       = linkTycon("Double");
        typeStable       = linkTycon("StablePtr");
#ifdef PROVIDE_WEAK
        typeWeak         = linkTycon("Weak");
#endif
#ifdef PROVIDE_FOREIGN
        typeForeign      = linkTycon("ForeignObj");
#endif
#ifdef PROVIDE_CONCURRENT
        typeThreadId     = linkTycon("ThreadId");
        typeMVar         = linkTycon("MVar");
#endif

        typeBool         = linkTycon("Bool");
        typeST           = linkTycon("ST");
        typeIO           = linkTycon("IO");
        typeException    = linkTycon("Exception");
        typeString       = linkTycon("String");
        typeOrdering     = linkTycon("Ordering");

        classEq          = linkClass("Eq");
        classOrd         = linkClass("Ord");
        classIx          = linkClass("Ix");
        classEnum        = linkClass("Enum");
        classShow        = linkClass("Show");
        classRead        = linkClass("Read");
        classBounded     = linkClass("Bounded");
        classReal        = linkClass("Real");
        classIntegral    = linkClass("Integral");
        classRealFrac    = linkClass("RealFrac");
        classRealFloat   = linkClass("RealFloat");
        classFractional  = linkClass("Fractional");
        classFloating    = linkClass("Floating");
        classNum         = linkClass("Num");
        classMonad       = linkClass("Monad");

        stdDefaults     = NIL;
        stdDefaults     = cons(typeDouble,stdDefaults);
#if DEFAULT_BIGNUM
        stdDefaults     = cons(typeInteger,stdDefaults);
#else
        stdDefaults     = cons(typeInt,stdDefaults);
#endif
        mkTypes();

        nameMkC          = addPrimCfunREP(findText("C#"),1,0,CHAR_REP);
        nameMkI          = addPrimCfunREP(findText("I#"),1,0,INT_REP);
        nameMkW          = addPrimCfunREP(findText("W#"),1,0,WORD_REP);
        nameMkA          = addPrimCfunREP(findText("A#"),1,0,ADDR_REP);
        nameMkF          = addPrimCfunREP(findText("F#"),1,0,FLOAT_REP);
        nameMkD          = addPrimCfunREP(findText("D#"),1,0,DOUBLE_REP);
        nameMkStable     = addPrimCfunREP(findText("Stable#"),1,0,STABLE_REP);
        nameMkInteger    = addPrimCfunREP(findText("Integer#"),1,0,0);
#ifdef PROVIDE_FOREIGN
        nameMkForeign    = addPrimCfunREP(findText("Foreign#"),1,0,0);
#endif
#ifdef PROVIDE_WEAK
        nameMkWeak       = addPrimCfunREP(findText("Weak#"),1,0,0);
#endif
        nameMkPrimArray            = addPrimCfunREP(findText("PrimArray#"),1,0,0);
        nameMkPrimByteArray        = addPrimCfunREP(findText("PrimByteArray#"),1,0,0);
        nameMkRef                  = addPrimCfunREP(findText("Ref#"),1,0,0);
        nameMkPrimMutableArray     = addPrimCfunREP(findText("PrimMutableArray#"),1,0,0);
        nameMkPrimMutableByteArray = addPrimCfunREP(findText("PrimMutableByteArray#"),1,0,0);
#ifdef PROVIDE_CONCURRENT
        nameMkThreadId   = addPrimCfun(findTextREP("ThreadId#"),1,0,0);
        nameMkMVar       = addPrimCfun(findTextREP("MVar#"),1,0,0);
#endif
        /* The following primitives are referred to in derived instances and
         * hence require types; the following types are a little more general
         * than we might like, but they are the closest we can get without a
         * special datatype class.
         */
        name(nameConCmp).type
            = mkPolyType(starToStar,fn(aVar,fn(aVar,typeOrdering)));
        name(nameEnRange).type
            = mkPolyType(starToStar,fn(boundPair,listof));
        name(nameEnIndex).type
            = mkPolyType(starToStar,fn(boundPair,fn(aVar,typeInt)));
        name(nameEnInRng).type
            = mkPolyType(starToStar,fn(boundPair,fn(aVar,typeBool)));
        name(nameEnToEn).type
            = mkPolyType(starToStar,fn(aVar,fn(typeInt,aVar)));
        name(nameEnFrEn).type
            = mkPolyType(starToStar,fn(aVar,typeInt));
        name(nameEnFrom).type
            = mkPolyType(starToStar,fn(aVar,listof));
        name(nameEnFrTo).type
            = name(nameEnFrTh).type
            = mkPolyType(starToStar,fn(aVar,fn(aVar,listof)));

        name(namePrimSeq).type
            = primType(MONAD_Id, "ab", "b");
        name(namePrimCatch).type
            = primType(MONAD_Id, "aH", "a");
        name(namePrimRaise).type
            = primType(MONAD_Id, "E", "a");

        for (i=2; i<=NUM_DTUPLES; i++) {/* Add derived instances of tuples */
            addTupInst(classEq,i);
            addTupInst(classOrd,i);
            addTupInst(classIx,i);
            addTupInst(classShow,i);
            addTupInst(classRead,i);
            addTupInst(classBounded,i);
        }
    }
}

static Void mkTypes ( void )
{
        predNum        = ap(classNum,aVar);
        predFractional = ap(classFractional,aVar);
        predIntegral   = ap(classIntegral,aVar);
        predMonad      = ap(classMonad,aVar);
}

Void linkPreludeCM(void) {              /* Hook to cfuns and mfuns in      */
    static Bool initialised = FALSE;    /* prelude when first loaded       */
    if (!initialised) {
        Int i;
        initialised = TRUE;

        setCurrModule(modulePrelude);

        /* constructors */
        nameFalse        = linkName("False");
        nameTrue         = linkName("True");

        /* members */
        nameEq           = linkName("==");
        nameFromInt      = linkName("fromInt");
        nameFromInteger  = linkName("fromInteger");
        nameFromDouble   = linkName("fromDouble");
        nameReturn       = linkName("return");
        nameBind         = linkName(">>=");
        nameLe           = linkName("<=");
        nameGt           = linkName(">");
        nameShowsPrec    = linkName("showsPrec");
        nameReadsPrec    = linkName("readsPrec");
        nameEQ           = linkName("EQ");
        nameCompare      = linkName("compare");
        nameMinBnd       = linkName("minBound");
        nameMaxBnd       = linkName("maxBound");
        nameRange        = linkName("range");
        nameIndex        = linkName("index");
        namePlus         = linkName("+");
        nameMult         = linkName("*");
        nameRangeSize    = linkName("rangeSize");
        nameInRange      = linkName("inRange");
        nameMinus        = linkName("-");
        /* These come before calls to implementPrim */
        for(i=0; i<NUM_TUPLES; ++i) {
            implementTuple(i);
        }
    }
}

Void linkPreludeNames(void) {           /* Hook to names defined in Prelude */
    static Bool initialised = FALSE;
    if (!initialised) {
        Int i;
        initialised = TRUE;

        setCurrModule(modulePrelude);

        /* primops */
        nameMkIO           = linkName("primMkIO");
        for (i=0; asmPrimOps[i].name; ++i) {
            Text t = findText(asmPrimOps[i].name);
            Name n = findName(t);
            if (isNull(n)) {
                n = newName(t,NIL);
            }
            name(n).line   = 0;
            name(n).defn   = NIL;
            name(n).type   = primType(asmPrimOps[i].monad,
                                      asmPrimOps[i].args,
                                      asmPrimOps[i].results);
            name(n).arity  = strlen(asmPrimOps[i].args);
            name(n).primop = &(asmPrimOps[i]);
            implementPrim(n);
        }

        nameRunST         = linkName("primRunST");

        /* static(tidyInfix)                        */
        nameNegate        = linkName("negate");
        /* user interface                           */
        nameRunIO         = linkName("primRunIO");
        namePrint         = linkName("print");
        /* desugar                                  */
        nameOtherwise     = linkName("otherwise");
        nameUndefined     = linkName("undefined");
        /* pmc                                      */
#if NPLUSK                      
        namePmSub         = linkName("primPmSub");
#endif                          
        /* translator                               */
        nameEqChar        = linkName("primEqChar");
        nameEqInt         = linkName("primEqInt");
nameCreateAdjThunk = linkName("primCreateAdjThunk");
#if !OVERLOADED_CONSTANTS
        nameEqInteger     = linkName("primEqInteger");
#endif /* !OVERLOADED_CONSTANTS */
        nameEqDouble      = linkName("primEqDouble");
        namePmInt         = linkName("primPmInt");
        name(namePmInt).inlineMe = TRUE;
    }
}


/* ToDo: fix pFun (or eliminate its use) */
#define pFun(n,s) n = predefinePrim(s)

Void linkControl(what)
Int what; {
    switch (what) {
        case RESET   :
        case MARK    : 
                       break;

        case INSTALL : linkControl(RESET);

                       modulePrelude = newModule(textPrelude);
                       setCurrModule(modulePrelude);

                       typeArrow = addPrimTycon(findText("(->)"),
                                                pair(STAR,pair(STAR,STAR)),
                                                2,DATATYPE,NIL);

                       /* newtype and USE_NEWTYPE_FOR_DICTS     */
                       pFun(nameId,             "id");

                       /* desugaring                            */
                       pFun(nameInd,            "_indirect");
                       name(nameInd).number = DFUNNAME;

                       /* pmc                                   */
                       pFun(nameSel,            "_SEL");

                       /* strict constructors                   */
                       pFun(nameFlip,           "flip"     );

                       /* parser                                */
                       pFun(nameFromTo,         "enumFromTo");
                       pFun(nameFromThenTo,     "enumFromThenTo");
                       pFun(nameFrom,           "enumFrom");
                       pFun(nameFromThen,       "enumFromThen");

                       /* deriving                              */
                       pFun(nameApp,            "++");
                       pFun(nameReadParen,      "readParen");
                       pFun(nameShowParen,      "showParen");
                       pFun(nameLex,            "lex");
                       pFun(nameEnToEn,         "toEnumPR"); //not sure
                       pFun(nameEnFrEn,         "fromEnum"); //not sure
                       pFun(nameEnFrom,         "enumFrom"); //not sure
                       pFun(nameEnFrTh,         "enumFromThen"); //not sure
                       pFun(nameEnFrTo,         "enumFromTo"); //not sure
                       pFun(nameEnRange,        "range"); //not sure
                       pFun(nameEnIndex,        "index"); //not sure
                       pFun(nameEnInRng,        "inRange"); //not sure
                       pFun(nameConCmp,         "_concmp"); //very not sure
                       pFun(nameComp,           ".");
                       pFun(nameAnd,            "&&");
                       pFun(nameCompAux,        "primCompAux");
                       name(nameCompAux).inlineMe = TRUE;
                       pFun(nameMap,            "map");

                       /* implementTagToCon                     */
                       pFun(namePMFail,         "primPmFail");
		       pFun(nameError,          "error");
		       pFun(nameUnpackString,   "primUnpackString");

		       //                       /* foreign export dynamic */
		       //pFun(nameCreateAdjThunk, "primCreateAdjThunk");

                       /* hooks for handwritten bytecode */
                       pFun(namePrimSeq,        "primSeq");
                       pFun(namePrimCatch,      "primCatch");
                       pFun(namePrimRaise,      "primRaise");
                       {
                          StgVar vv = mkStgVar(NIL,NIL);
                          Name n = namePrimSeq;
                          name(n).line = 0;
                          name(n).arity = 1;
                          name(n).type = NIL;
                          vv = mkStgVar(NIL,NIL);
                          stgVarInfo(vv) = mkPtr ( asm_BCO_seq() );
                          name(n).stgVar = vv;
                          stgGlobals=cons(pair(n,vv),stgGlobals);
                          namePrimSeq = n;
                       }
                       {
                          StgVar vv = mkStgVar(NIL,NIL);
                          Name n = namePrimCatch;
                          name(n).line = 0;
                          name(n).arity = 2;
                          name(n).type = NIL;
                          stgVarInfo(vv) = mkPtr ( asm_BCO_catch() );
                          name(n).stgVar = vv;
                          stgGlobals=cons(pair(n,vv),stgGlobals);
                       }
                       {
                          StgVar vv = mkStgVar(NIL,NIL);
                          Name n = namePrimRaise;
                          name(n).line = 0;
                          name(n).arity = 1;
                          name(n).type = NIL;
                          stgVarInfo(vv) = mkPtr ( asm_BCO_raise() );
                          name(n).stgVar = vv;
                          stgGlobals=cons(pair(n,vv),stgGlobals);
                       }

                       break;
    }
}
#undef pFun


/*-------------------------------------------------------------------------*/