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

#include "desc.h"
5
#include "thunk.h"
6
7
8
9

#define CT_LIT	 			1
#define CT_VAR 				2
#define CT_APP 				3
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
10
#define CT_SELECT			4
11
#define CT_IF				5
12

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
13
struct Code
14
15
16
{
	unsigned int type  		: 3;
	unsigned int local_type : 3;
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
17
18
	unsigned int nr_args	: 5;	// used in AppEntry
	unsigned int nr_cases	: 5;	// used in SelectEntry
19
20
21
22
23
24
25
26
27
28
29
};

#define LIT_INT   			1
#define LIT_REAL   			2
#define LIT_CHAR			3
#define LIT_BOOL			4
#define LIT_STRING			5

struct CleanString
{
	int length;
30
	char chars[];
31
32
33
34
};

struct LitEntry
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
35
	struct Code base;
36
37
38
39
40
41
42
43
44
45
	union 
	{
		int _int;
		double _real;
		char _char;
		int _bool;
		struct CleanString _string;
	};
};

46
47
48
#define VAR_ARG			1
#define VAR_LOCAL		2
#define VAR_FN			3
49
50
51

struct VarEntry
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
52
	struct Code base;
53
54
55
	union
	{
		int index; // index on the stack
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
56
		struct Desc* f;
57
58
59
60
61
	};
};

struct AppEntry
{
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
62
	struct Code base;
63
	struct VarEntry* var;  // TODO: remove * here (embed VarEntry to save a space of one pointer)
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
64
	struct Code* args[];
65
66
};

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#define SC_CONS			1
#define SC_LIT			2
#define SC_DEFAULT		3

struct SelectCaseEntry
{
	int type;
	struct Code* body;
	union
	{
		struct ADTEntry* cons;
		struct LitEntry* lit;
	};	
};

struct SelectEntry
{
	struct Code base;
	struct Code* expr;
	struct SelectCaseEntry cases[];
};

89
90
91
92
93
94
95
96
struct IfEntry
{
	struct Code base;
	struct Code* cond;
	struct Code* texpr;
	struct Code* fexpr;
};

97
struct Thunk* exec(Code* expr, int frame_ptr, Thunk* target);
98

99
#endif // __CODE_H