-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9cc.h
161 lines (137 loc) · 2.9 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
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#include <stdio.h>
typedef struct Type Type;
// tokenize.c
typedef enum {
TK_RESERVED, // punctuators
TK_INDENT, // 識別子
TK_NUM, // number
TK_EOF, // end of file marker
TK_STR, // string
} TokenKind;
typedef struct Token Token;
struct Token {
TokenKind kind;
Token *next;
int val;
char *str;
int len;
char *contents;
int cont_len;
};
void error(char *fmt, ...);
void error_at(char *loc, char *fmt, ...);
void error_tok(Token *tok, char *fmt, ...);
Token *peek(char *s);
Token *tokenize(char *input);
Token *consume(char *op);
Token *consume_ident();
long expect_number();
void expect(char *op);
bool at_eof();
char *expect_ident();
extern char *filename;
extern Token *token;
typedef struct Var Var;
struct Var {
char *name;
Type *ty;
bool is_local;
int offset;
char *contents;
int cont_len;
};
typedef struct VarList VarList;
struct VarList {
VarList *next;
Var *var;
};
// parser
typedef enum {
ND_ADD, // num + num
ND_PTR_ADD, // ptr + num or num + ptr
ND_SUB, // num - num
ND_PTR_SUB, // ptr - num
ND_PTR_DIFF, // ptr - ptr
ND_MUL,
ND_DIV,
ND_NUM,
ND_EQ, // ==
ND_NE, // !=
ND_LT, // <
ND_LE, // <=
ND_ASSIGN, // =
ND_VAR, // Variable
ND_RETURN, // Return
ND_IF, // IF
ND_WHILE, // WHILE
ND_FOR, // FOR
ND_BLOCK, // Block {}
ND_FUNCALL, // Function cal
ND_EXPR_STMT, // Expression statement
ND_ADDR, // *
ND_DEREF, // &
ND_NULL, // empty statement
} NodeKind;
// Ast Node
typedef struct Node Node;
struct Node {
NodeKind kind; //種別
Node *lhs; // 左辺
Node *rhs; // 右辺
Node *next; // 次のNode
Token *tok; // Token
Var *var; // ND_VARの時に使う 変数の名前
long val; // 値
Node *cond; // if cond
Node *then; // if then
Node *els; // if else
Node *init; // for init
Node *inc; // for increment
Node *body; // {} Block
char *funcname; // funcion call name
Node *args; // function args
Type *ty; // type, e.g. int or pointer to int
};
typedef struct Function Function;
struct Function {
Function *next;
char *name;
VarList *params;
Node *node;
VarList *locals;
int stack_size;
};
typedef struct {
VarList *globals;
Function *fns;
} Program;
Program *program();
typedef enum {
TY_INT,
TY_PTR,
TY_ARRAY,
TY_CHAR,
} TypeKind;
struct Type {
TypeKind kind;
int size; // sizeof() value
Type *base; // pointer
int array_len;
};
extern Type *int_type;
extern Type *char_type;
bool is_integer(Type *ty);
Type *pointer_to(Type *base);
Type *array_of(Type *base, int size);
void add_type(Node *node);
// codegen
void codegen(Program *prog);