-
Notifications
You must be signed in to change notification settings - Fork 0
/
kcc.c
220 lines (187 loc) · 3.88 KB
/
kcc.c
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
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
TK_RESERVED,
TK_NUM,
TK_EOF,
} TokenKind;
typedef struct Token Token;
struct Token {
TokenKind kind;
Token *next;
int val;
char *str;
};
Token *token;
typedef enum {
ND_ADD,
ND_SUB,
ND_MUL,
ND_DIV,
ND_NUM,
} NodeKind;
char *user_input;
void error_at(char *loc, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
int pos = loc - user_input;
fprintf(stderr, "%s\n", user_input);
fprintf(stderr, "%*s", pos, " ");
fprintf(stderr, "^ ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
bool consume(char op) {
if (token->kind != TK_RESERVED || token->str[0] != op) return false;
token = token->next;
return true;
}
void expect(char op) {
if (token->kind != TK_RESERVED || token->str[0] != op)
error_at(token->str, "not '%c'", op);
token = token->next;
}
int expect_number() {
if (token->kind != TK_NUM) error_at(token->str, "not a number");
int val = token->val;
token = token->next;
return val;
}
bool at_eof() { return token->kind == TK_EOF; }
Token *new_token(TokenKind kind, Token *cur, char *str) {
Token *tok = calloc(1, sizeof(Token));
tok->kind = kind;
tok->str = str;
cur->next = tok;
return tok;
}
Token *tokenize() {
char *p = user_input;
Token head;
head.next = NULL;
Token *cur = &head;
while (*p) {
if (isspace(*p)) {
p++;
continue;
}
if (*p == '+' || *p == '-' || *p == '*' || *p == '/' || *p == '(' ||
*p == ')') {
cur = new_token(TK_RESERVED, cur, p++);
continue;
}
if (isdigit(*p)) {
cur = new_token(TK_NUM, cur, p);
cur->val = strtol(p, &p, 10);
continue;
}
error_at(p, "enable to tokenize");
}
new_token(TK_EOF, cur, p);
return head.next;
}
typedef struct Node Node;
struct Node {
NodeKind kind;
Node *lhs;
Node *rhs;
int val; // if kind == ND_NUM
};
Node *new_node(NodeKind kind, Node *lhs, Node *rhs) {
Node *node = calloc(1, sizeof(Node));
node->kind = kind;
node->lhs = lhs;
node->rhs = rhs;
return node;
}
Node *new_node_num(int val) {
Node *node = calloc(1, sizeof(Node));
node->kind = ND_NUM;
node->val = val;
return node;
}
Node *expr();
Node *primary() {
if (consume('(')) {
Node *node = expr();
expect(')');
return node;
}
return new_node_num(expect_number());
}
Node *unary() {
if (consume('+'))
return primary();
else if (consume('-'))
return new_node(ND_SUB, new_node_num(0), primary());
return primary();
}
Node *mul() {
Node *node = unary();
for (;;) {
if (consume('*'))
node = new_node(ND_MUL, node, unary());
else if (consume('/'))
node = new_node(ND_DIV, node, unary());
else
return node;
}
}
Node *expr() {
Node *node = mul();
for (;;) {
if (consume('+'))
node = new_node(ND_ADD, node, mul());
else if (consume('-'))
node = new_node(ND_SUB, node, mul());
else
return node;
}
}
void gen(Node *node) {
if (node->kind == ND_NUM) {
printf(" push %d\n", node->val);
return;
}
gen(node->lhs);
gen(node->rhs);
printf(" pop rdi\n");
printf(" pop rax\n");
switch (node->kind) {
case ND_ADD:
printf(" add rax, rdi\n");
break;
case ND_SUB:
printf(" sub rax, rdi\n");
break;
case ND_MUL:
printf(" imul rax, rdi\n");
break;
case ND_DIV:
printf(" cqo\n");
printf(" idiv rdi\n");
break;
}
printf(" push rax\n");
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "引数の個数が正しくない");
return 1;
}
user_input = argv[1];
token = tokenize();
Node *node = expr();
printf(".intel_syntax noprefix\n");
printf(".globl main\n");
printf("main:\n");
gen(node);
printf(" pop rax\n");
printf(" ret\n");
return 0;
}