-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccc.h
303 lines (266 loc) · 7.47 KB
/
ccc.h
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
/*
* data structures for the compiler.
*
* we don't have any other non-generated includes.
* so, everything is right here
*
* nested includes are a bit ugly, but it means that i can just include ccc.h
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*
* generated files
*/
#include "debug.h"
#include "token.h"
/*
* basic types - everything externally visible has one of these
*/
typedef char *cstring; // counted string - first char is length
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned long dword;
typedef unsigned char boolean;
/*
* we just want the error symbols
* error.h is generated, and contains actual error strings if DEF_ERRMSG.
*/
#undef DEF_ERRMSG
#include "error.h"
/*
* expressions hold computations
*/
struct expr {
int flags;
#define E_CONST 0x01
#define E_RESOLVED 0x02
#define E_FUNARG 0x04
char op;
struct expr *left;
struct expr *right;
struct expr *up;
struct expr *prev;
struct expr *next;
struct type *type;
struct var *var;
unsigned long v;
char location;
char cost;
char regs;
struct stmt *stmt;
struct inst *inst;
};
// expression priority - used to control the precedence
#define PRI_ALL 0
extern struct expr *cfold(struct expr *e);
extern struct expr *parse_expr(char priority, struct stmt *);
int parse_const(char priority);
extern struct expr *new_expr(char op);
extern void destroy_expr(struct expr *e);
#ifdef DEBUG
extern void dump_expr(struct expr *e);
#endif
/*
* a statement is the basic execution unit that is managed by the compiler
* it has a parent statement, and is linked to all the other statements that
* belong to the same parent
*/
struct stmt {
struct stmt *parent;
struct stmt *next;
struct expr *left;
struct expr *right;
char op;
int flags;
char *label;
};
extern struct stmt *new_stmt(char op, struct expr *left);
extern void destroy_stmt(struct stmt *s);
#ifdef DEBUG
extern void dump_stmt(struct stmt *s);
#endif
/*
* synthetic type information like enum, struct, union, etc goes away as soon
* as the scope does. that means that by the time we get to expression trees
* we only have references to primitive types, sizes and offsets.
*
* there are dialects of C that have polluted the lexical scope, for example
* where struct elements or tags leak upwards. this isn't one of them.
*
* if you have a c source that depends on this kind of thing, fix the source.
*/
/*
* how big a pointer is
*/
#define TS_PTR 2
/*
* a function is an odd type, since it has a return type and argument types,
* so there needs to be a collection of the latter. also, a declaration of
* an instance of a function type can and will have the arguments with different
* names than a prototype or forward reference. old style forward function
* declarations don't have argument types.
*/
/*
* this is a handle for types.
*/
struct type {
char *name; // the type name
int size; // how big is one of me
int count; // if we are an array, how many
struct name *elem; // element list
struct type *sub; // pointer to what, array of what, etc.
unsigned char flags;
struct arglist *args; // if a function
struct type *next;
};
#define TF_AGGREGATE 0x01
#define TF_INCOMPLETE 0x02
#define TF_UNSIGNED 0x04
#define TF_FUNC 0x08
#define TF_POINTER 0x10
#define TF_ARRAY 0x20
#define TF_FLOAT 0x40
#define TF_OLD 0x80 // no argument list - K&R
extern struct type *getbasetype();
extern void dump_type(struct type *t, int lv);
struct type *get_type(int flags, struct type *sub, struct arglist *args, int count);
/*
* this structure is a unique ordered list of function argument types
*/
struct arglist {
struct type *type;
struct arglist *next;
};
typedef enum { prim, etag, stag, utag, var, elem, tdef } kind;
/*
* note that at the same scope, you can have
* multiple instances of the same name with different namespaces.
* this is a container for types, functions, variables, constants, and fields
*/
struct name {
char *name;
boolean is_tag; // true if (enum, struct, union),
// else false for var,enum elem,typedef
int level; // lexical level
struct name *next; // all names in same container
struct type *type;
char visibility;
char sclass;
int offset; // if inside a struct
int bitoff;
int width;
struct expr *init; // value of constant or initializer
struct stmt *body; // function body
kind kind;
int flags;
#define V_BITFIELD 0x01
#define V_FUNARG 0x02
#define V_LOCAL 0x04
};
#define MAXBITS 32 // maximum size of bitfield
extern struct name *new_name(char *name, kind k, struct type *t, boolean is_tag);
extern struct name *lookup_name(char *name, boolean is_tag);
extern struct name *lookup_element(char *name, struct type *t);
extern void dump_name(struct name *s);
extern struct type *inttype;
void parse();
/* kw.c */
extern unsigned char cppkw[];
extern unsigned char ckw[];
extern unsigned char asmkw[];
extern char kwlook(unsigned char *str, unsigned char *table);
/* lex.c */
extern cstring nextstr;
struct token { // lexeme
token_t type;
union {
long numeric; // char, short, int, long
char *name; // if we have a symbol
cstring str; // counted literal string
} v;
};
extern struct token cur, next;
extern void lexinit();
extern int write_cpp_file;
extern int cpp_file;
extern char *cpp_file_name;
extern char strbuf[];
extern char match(token_t t);
extern void gettoken();
extern void skipwhite1();
extern void skipwhite();
extern char issym();
/* io.c */
extern void pushfile(char *name);
extern void insertmacro(char *name, char *macbuf);
extern void insertfile(char *name, int sysdirs);
extern void advance();
void iodump();
void ioinit();
void add_include(char *name);
void cpp_flush();
void cpp_out(char *s, int len);
extern char curchar;
extern char nextchar;
extern int lineno;
extern char *filename;
extern int column;
/* cc1.c */
extern void err(error_t errcode);
extern void fatal(error_t errcode);
extern void recover(error_t errcode, token_t skipto);
extern void need(token_t check, token_t skipto, error_t errcode);
extern void err(error_t errcode);
int main(int argc, char **argv);
void process(char *f);
void usage(char *complaint, char *p);
/* macro.c */
struct macro {
char parmcount;
char *name;
char **parms;
char *mactext;
struct macro *next;
};
extern struct macro *macros;
extern char *macbuffer;
void macdefine(char *s);
void macundefine(char *s);
int macexpand(char *s);
struct macro *maclookup(char *s);
void add_define(char *s);
/* util.c */
extern unsigned char lookupc(char *s, char c);
extern void hexdump(char *tag, char *s, int len);
int iswhite(char c);
char *bitdef(unsigned char v, char **defs);
int quoted_string(char *d, char *s);
int longout(char *d, long v);
/* declare.c */
extern struct name *declare(struct type **btp);
/* tokenlist.c */
extern char *tokenname[];
extern char *detoken[];
/* debug options */
#define VERBOSE(x) (verbose & (x))
extern int verbose;
#ifdef __SDCC
/*
* this is a minimal unix library header file for use on compilers
* that don't have unixlike libraries and includes
*/
char *strdup(char *s);
int open(char *filename, int mode);
int close(int fd);
int creat(char *filename, int mode);
void perror(char *msg);
void exit(int exitcode);
int read(int fd, char *buf, int len);
int write(int fd, char *buf, int len);
long strtol(char *str, char **endptr, int base);
void bcopy(void *src, void *dst, int len);
#endif
/*
* vim: tabstop=4 shiftwidth=4 expandtab:
*/