summaryrefslogtreecommitdiff
path: root/compiler/ex-compiler.h
blob: 9328353ac00d413d99d9cd81da019b80c93d6833 (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
#include <stdlib.h>
#include <assert.h>

typedef struct ExSpec ExSpec;
typedef struct ExUsesClause ExUsesClause;
typedef struct ExDefinition ExDefinition;
typedef struct ExNamespace ExNamespace;
typedef struct ExExtension ExExtension;
typedef struct ExRequest ExRequest;
typedef struct ExEvent ExEvent;
typedef struct ExError ExError;
typedef struct ExTypeDef ExTypeDef;
typedef struct ExType ExType;
typedef struct ExIdentifier ExIdentifier;
typedef struct ExField ExField;
typedef struct ExHashTable ExHashTable;

/* types and utilities */

#define EX_TRUE 1
#define EX_FALSE 0

typedef int exboolean;
typedef void * expointer;
typedef unsigned long exulong;

char *    ex_strdup      (const char *str);
void      ex_parse_error (int         line,
			  int         pos,
			  const char *str);
void      ex_fatal_error (int         line,
			  int         pos,
			  const char *str);
expointer ex_malloc      (size_t      n_bytes);

#define ex_new(type,n) (type*)(ex_malloc(sizeof(type)*n))
#define ex_assert(e) assert(e)

/* Hash Table */
ExHashTable *ex_hash_table_new    (void);
void         ex_hash_table_insert (ExHashTable *table,
				   const char  *str,
				   expointer    data);
expointer    ex_hash_table_lookup (ExHashTable *table,
				   const char  *str);


/* AST nodes */
struct ExSpec
{
    ExHashTable *	symbols;
    ExUsesClause *	uses_clauses;
    ExDefinition *	definitions;
};

struct ExUsesClause
{
    char *		name;
    ExUsesClause *	next;
};

typedef enum
{
    EX_DEFINITION_NAMESPACE,
    EX_DEFINITION_EXTENSION,
    EX_DEFINITION_REQUEST,
    EX_DEFINITION_TYPE_DEF,
    EX_DEFINITION_ERROR,
    EX_DEFINITION_EVENT
} ExDefinitionType;

struct ExDefinition
{
    ExDefinitionType type;
    union
    {
	ExNamespace *namespace;
	ExExtension *extension;
	ExRequest *request;
	ExTypeDef *type_def;
	ExError *error;
	ExEvent *event;
    } u;
    
    ExDefinition *next;
};

struct ExField
{
    ExType *	type;
    char *	name;
    exboolean	is_created;
    ExField *	next;
};

struct ExNamespace
{
    char *name;
    ExDefinition *definitions;
};

struct ExExtension
{
    char *name;
    ExDefinition *definitions;
};

struct ExIdentifier
{
    char *name;
    ExIdentifier *next;
};

struct ExTypeDef
{
    char *name;
    ExType *type;
};

struct ExRequest
{
    char *name;
    int opcode;
    ExField *parameters;
    ExField *reply;
    ExIdentifier *errors;
};

struct ExEvent
{
    char *name;
    ExField *fields;
};

typedef enum
{
    EX_TYPE_CARD8,
    EX_TYPE_CARD16,
    EX_TYPE_CARD32,
    EX_TYPE_CARD64,
    EX_TYPE_INT8,
    EX_TYPE_INT16,
    EX_TYPE_INT32,
    EX_TYPE_INT64,
    EX_TYPE_BOOLEAN,
    
    EX_TYPE_ENUM,
    EX_TYPE_STRUCT,
    EX_TYPE_BITS,
    EX_TYPE_DERIVED_BITS,
    EX_TYPE_MASKED_LIST,
    EX_TYPE_IDENTIFIER,
    EX_TYPE_UNION,
    EX_TYPE_LIST,
    EX_TYPE_XID
} ExTypeType;

struct ExType
{
    ExTypeType type;
    
    union {
	struct
	{
	    ExIdentifier *	identifiers;
	} _enum;
	
	struct
	{
	    ExField *		fields;
	} _struct;
	
	struct
	{
	    exulong		mask;
	    ExIdentifier *	fields;
	} bits;
	
	struct
	{
	    char *		derived_from_name;
	    exulong		mask;
	} derived_bits;
	
	struct
	{
	    ExField *		fields;
	} masked_list;
	
	struct
	{
	    char *		name;
	    exboolean		can_be_null;
	} identifier;
	
	struct
	{
	    ExField *		fields;
	} _union;
	
	struct
	{
	    ExType *		type;
	} _list;
    } u;
};

struct ExError
{
    char *name;
};

/* constructors */

ExSpec *      ex_spec_new                 (ExUsesClause  *clauses,
					   ExDefinition  *definitions);
ExUsesClause *ex_uses_clause_new          (const char    *name);
ExDefinition *ex_definition_new_namespace (ExNamespace   *namespace);
ExDefinition *ex_definition_new_extension (ExExtension   *extension);
ExDefinition *ex_definition_new_type_def  (ExTypeDef     *type_def);
ExDefinition *ex_definition_new_request   (ExRequest     *request);
ExDefinition *ex_definition_new_event     (ExEvent       *event);
ExDefinition *ex_definition_new_error     (ExError       *error);
ExNamespace * ex_namespace_new            (const char    *name,
					   ExDefinition  *definitions);
ExExtension * ex_extension_new		  (const char    *name,
					   ExDefinition  *definitions);
ExTypeDef *   ex_type_def_new             (const char    *name,
					   ExType        *type);
ExType *      ex_type_new_enum            (ExIdentifier  *identifiers);
ExType *      ex_type_new_struct          (ExField       *fields);
ExType *      ex_type_bits_new            (unsigned long  mask,
					   ExIdentifier  *identifiers);
ExType *      ex_type_derived_bits_new    (const char    *name,
					   unsigned long  mask);
ExType *      ex_type_masked_list_new     (ExField       *fields);
ExType *      ex_type_identifier_new      (const char    *name,
					   exboolean	  can_be_none);
ExType *      ex_type_union_new           (ExField       *fields);
ExType *      ex_type_list_new            (ExType        *type);
ExType *      ex_type_basic_new           (ExTypeType     type);
ExIdentifier *ex_identifier_new		  (const char    *name);
ExError *     ex_error_new		  (const char    *name);
ExField *     ex_field_new		  (ExType	 *type,
					   const char    *name,
					   exboolean      is_created);
ExRequest *   ex_request_new		  (const char	 *name,
					   int		  opcode,
					   ExField	 *parameters,
					   ExField	 *reply,
					   ExIdentifier  *errors);
ExEvent *     ex_event_new		  (const char    *name,
					   ExField       *fields);

/* phases */
void    ex_init_scanner   (void);
ExSpec *ex_parse	  (const char *filename);
void    ex_dump           (ExSpec     *spec);
void	ex_weed		  (ExSpec     *spec);
void    ex_gather_symbols (ExSpec     *spec);