code.h 1.4 KB
Newer Older
1
2
3
#ifndef __CODE_H
#define __CODE_H

4
#include "thunk.h"
5

6
enum CodeType {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
7
    CT_VAR, CT_VAR_STRICT, 
8
9
    CT_APP_PRIM1, CT_APP_PRIM2,
    CT_APP_THUNK, CT_APP_FUN, CT_APP_DYN, 
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
10
11
    CT_SELECT, CT_IF, 
    CT_THUNK
12
};
13

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
14
struct Code {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
15
    CodeType type : 4;
16
17
18
    unsigned int nr_args : 5;       // used in AppEntry
    unsigned int nr_cases : 5;      // used in SelectEntry
    unsigned int strict : 1;        // used in VarEntry
19
    struct Thunk* (*create_thunk)(Code*, int);
20
21
};

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
22
struct ThunkEntry {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
23
    struct Code base;
24
    struct Thunk thunk;
25
26
};

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
27
28
struct VarEntry {
    struct Code base;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
29
    int index; // index on the stack
30
31
};

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
32
33
struct AppEntry {
    struct Code base;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
34
35
36
37
    union {
        struct VarEntry var;
        struct Desc* f;
    };
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
38
    struct Code* args[];
39
40
};

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
41
42
43
44
#define SC_CONS			1
#define SC_LIT			2
#define SC_DEFAULT		3

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
45
46
47
48
49
50
struct SelectCaseEntry {
    int type;
    struct Code* body;

    union {
        struct ADTEntry* cons;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
51
        struct ThunkEntry* lit;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
52
    };
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
53
54
};

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
55
56
57
58
struct SelectEntry {
    struct Code base;
    struct Code* expr;
    struct SelectCaseEntry cases[];
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
59
60
};

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
61
62
63
64
65
struct IfEntry {
    struct Code base;
    struct Code* cond;
    struct Code* texpr;
    struct Code* fexpr;
66
67
};

68
69
typedef Thunk* (*create_thunk_fun)(Code*, int);

70
void init_code();
71
create_thunk_fun get_create_thunk_fun(CodeType type);
72

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
73
void exec(Code* expr, int frame_ptr, int root_frame_ptr);
74
struct Thunk* eval(Thunk* thunk);
75

76
#endif // __CODE_H