desc.c 3.37 KB
Newer Older
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
1
2
#include <stdio.h>
#include <stdlib.h>
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
3
4
5
6
#include <stdbool.h>
#include <assert.h>

#include "desc.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
7
#include "khash.h"
8
#include "mem.h"
9
#include "thunk.h"
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
10
11
12
13
14
15
16
17
18
19
20
21

// shorthand way to get the key from hashtable or null (0) if not found
#define kh_get_val(kname, hash, key) ({k=kh_get(kname, hash, key);(k!=kh_end(hash)?kh_val(hash,k):0);})

// shorthand way to set value in hash with single line command.  Returns value
// returns 0=replaced existing item, 1=bucket empty (new key), 2-adding element previously deleted
#define kh_set(kname, hash, key, val) ({int ret; k = kh_put(kname, hash,key,&ret); kh_value(hash,k) = val; ret;})

// name part of init must be unique for the key, value types.
// in this instance 33 is arbitrary symbolic name for a hashtable
// that contains string keys and void* values.
const int khStrPtr = 33;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
22
KHASH_MAP_INIT_STR(khStrPtr, Desc*) // setup khash to handle string key with an arbitrary pointer payload
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
23
24

// create a hashtable
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
25
khash_t(khStrPtr) * funHash = kh_init(khStrPtr);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
26

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
27
28
29
void add_desc(char* fn, Desc* desc) {
    khiter_t k; // used by the macros   
    kh_set(khStrPtr, funHash, fn, desc);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
30
31
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
32
33
34
Desc* find_desc(char* fn) {
    khiter_t k; // used by the macros   
    return kh_get_val(khStrPtr, funHash, fn);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
35
36
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
37
Desc* get_slice(Desc* f, int nrargs) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
38
    assert(nrargs<=f->arity);    
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
39
    return &(((SliceEntry*) f)[-(f->arity - nrargs)].base);
40
41
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
42
FunEntry* alloc_prim(char* name, int unboxable) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
43
44
45
46
    int len = strlen(name);
    FunEntry* entry = (FunEntry*) alloc_desc(sizeof (FunEntry) + len + 1);
    entry->base.type = FT_BOXED_LIT;
    entry->base.arity = 0;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
47
48
    entry->base.thunk_size = sizeof(Thunk);
    entry->base.unboxable = unboxable;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
49
50
    memcpy(entry->name, name, len + 1);
    return entry;
51
52
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
53
54
55
56
57
void gen_slices(SliceEntry* dest, Desc* forward_ptr, int arity) {
    for (int i = 0; i < arity; i++) {
        SliceEntry* slice = dest + i;
        slice->base.type = FT_SLICE;
        slice->base.arity = i;
58
        slice->base.thunk_size = thunk_size_f(i);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
59
60
        slice->forward_ptr = forward_ptr;
    }
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
int printDesc(Desc* f) {
    switch (f->type) {
        case FT_SLICE:
            printDesc(((SliceEntry*) f)->forward_ptr);
            return f->arity;
        case FT_PRIM:
            printf("%s", ((PrimEntry*) f)->name);
            return f->arity;
        case FT_FUN:
        case FT_BOXED_LIT:
            printf("%s", ((FunEntry*) f)->name);
            return f->arity;
        case FT_ADT:
            printf("%s", ((ADTEntry*) f)->name);
            return f->arity;
        case FT_RECORD:
            printf("%s", ((RecordEntry*) f)->name);
            return f->arity;
        default:
            printf("printDesc: unhandled DESC %d\n", f->type);
            exit(-1);
    }
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
87
void init_desc() {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
88
89
90
91
92
93
94
95
96
97
98
99
100
    __INT__ = alloc_prim("INT", 1);
    __BOOL__ = alloc_prim("BOOL", 1);
    __CHAR__ = alloc_prim("CHAR", 1);
    __REAL__ = alloc_prim("REAL", 1);
    
    __STRING__ = alloc_prim("STRING", 0);
    __ARRAY__ = alloc_prim("ARRAY", 0);
    
    __FORWARD_PTR__ = alloc_prim("FWD", 0);
    
    // Avoid to be overwritten by updateF
    __STACK_PLACEHOLDER__ = alloc_prim("PLACEHOLDER", 0);
    __STACK_PLACEHOLDER__->base.thunk_size = 0;
101
102
103
104
105
106
}

struct FunEntry* __INT__;
struct FunEntry* __BOOL__;
struct FunEntry* __CHAR__;
struct FunEntry* __REAL__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
107

108
struct FunEntry* __STRING__;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
109
110
111
112
struct FunEntry* __ARRAY__;

struct FunEntry* __FORWARD_PTR__;
struct FunEntry* __STACK_PLACEHOLDER__;