-
Notifications
You must be signed in to change notification settings - Fork 0
/
9cc.h
214 lines (193 loc) · 3.81 KB
/
9cc.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
#ifndef __9CC_H__
#define __9CC_H__
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
TK_RESERVED,
TK_IDENT,
TK_NUM,
TK_IF,
TK_ELSE,
TK_WHILE,
TK_RETURN,
TK_FOR,
TK_SIZEOF,
TK_INT,
TK_STR,
TK_CHAR,
TK_STRUCT,
TK_DOT,
TK_ARROW,
TK_EOF
} TokenKind;
typedef struct Token Token;
struct Token {
TokenKind kind;
Token *next;
int val;
char *str;
int len;
};
typedef enum {
ND_ADD,
ND_SUB,
ND_MUL,
ND_DIV,
ND_MOD,
ND_NUM,
ND_EQ,
ND_NEQ,
ND_LT,
ND_LEQ,
ND_ASSIGN,
ND_ADDR,
ND_DEREF,
ND_LVAR,
ND_FUNC,
ND_GVAR,
ND_STR,
ND_CALL,
ND_IF,
ND_IFELSE,
ND_WHILE,
ND_FOR,
ND_BLOCK,
ND_RETURN,
ND_NOP,
} NodeKind;
typedef struct Node Node;
typedef struct Block Block;
typedef struct Variable Variable;
typedef struct Str Str;
typedef struct Arg Arg;
typedef struct Struct Struct;
typedef struct Type Type;
typedef struct Member Member;
typedef enum {
INT,
CHAR,
PTR,
ARRAY,
STRUCT,
VOID,
} TypeKind;
struct Type {
TypeKind ty;
Type *base;
Member *members;
int array_size; // tyがARRAYだった場合、その要素数 例えばint
// a[2][3][4]のとき、a[0]は3,a[0][0]は4
};
struct Member {
Member *next;
Type *ty;
char *name;
int len;
int offset;
};
struct Node {
NodeKind kind;
Node *lhs;
Node *rhs;
Node *target;
Node *condition;
Node *true_statement;
Node *false_statement;
Node *initialize;
Node *update;
Node *statement;
Block *block;
int val; // kindがND_NUMの場合のみ使う
int offset; // kindがND_LVARの場合のみ使う
char *gvarname; // kindがND_GVALの場合のみ使う
int gvarnamelen; // kindがND_GVALの場合のみ使う
char *funcname; // kindがND_CALLまたはND_FUNCの場合のみ使う
int funcnamelen; // kindがND_CALLまたはND_FUNCの場合のみ使う
Arg *args; // kindがND_CALLの場合のみ使う
int argnum; // kindがND_FUNCの場合のみ使う
Type *type; // そのノードを処理した結果の型を持つ
Type *type2; // ノードの型(いずれはこちらに切り替える)
Str *str; // kindがND_STRの場合のみ使う
};
struct Block {
Node *stmt_node;
Block *next;
};
struct Variable {
Variable *next;
char *name;
int len;
int offset;
int size;
Type *type;
};
struct Struct {
Struct *next;
char *name;
int len;
Type *type;
};
struct Str {
Str *next;
char *str;
int len;
int id;
};
struct Arg {
Node *node;
Arg *next;
};
void error(char *fmt, ...);
void program();
Type *decl_type();
Node *toplevel();
Node *stmt();
Node *expr();
Node *assign();
Node *equality();
Node *relational();
Node *add();
Node *mul();
Node *unary();
Node *term();
void gen(Node *node);
Node *new_node(NodeKind kind, Node *lhs, Node *rhs);
Token *tokenize(char *p);
Node *expr();
int is_numeric(Type *type);
void gen_globals();
void gen_strings();
void gen_gval(Node *node);
Variable *find_variable(Token *tok, Variable *root);
Member *find_member(Member *head, char *name);
Struct *find_struct(Token *tok);
Block *new_block();
Block *next_block(Block *block);
Arg *new_arg(Arg *cur);
Arg *next_arg(Arg *cur);
void print_type(Type *type);
int type_size(Type *type);
int get_array_offset(Type *type);
char *read_file(char *path);
int get_array_dimesion(Type *type);
void error(char *fmt, ...);
void error_at(char *loc, char *fmt, ...);
void node_name(NodeKind kind, char *s);
void dumpAST(FILE *fp, Node *node);
extern Token *token;
extern char *user_input;
extern Node *code[1024];
extern Variable *func_variables[256];
extern Variable *locals;
extern Variable *globals;
extern Str *strings;
extern Struct *structs;
extern int label;
extern char **arg_reg_32;
extern char **arg_reg_64;
#endif