-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
71 lines (60 loc) · 1.57 KB
/
main.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
#include "./9cc.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
char *read_file(char *path)
{
FILE *fp = fopen(path, "r");
if (!fp)
error_at("cannot open %s: %s", path);
if (fseek(fp, 0, SEEK_END) == -1)
error_at("%s: fseek: %s", path);
size_t size = ftell(fp);
if (fseek(fp, 0, SEEK_SET) == -1)
error_at("%s: fseek: %s", path);
char *buf = calloc(1, size + 2);
fread(buf, size, 1, fp);
if (size == 0 || buf[size - 1] != '\n')
buf[size++] = '\n';
buf[size] = '\0';
fclose(fp);
return buf;
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "引数の個数が正しくありません\n");
return 1;
}
file_name = argv[1];
user_input = read_file(file_name);
token = tokenize();
program();
printf(".intel_syntax noprefix\n");
if (globals->name != NULL)
{
printf(".data\n");
for (LVar *var = globals; var->name != NULL; var = var->next)
{
char *name = malloc(globals->len + 1);
strncpy(name, globals->name, globals->len);
printf(".comm %s,%d,%d\n", name, globals->size, globals->size);
free(name);
}
}
printf(".text\n");
if (strings->name != NULL)
{
for (LVar *var = strings; var->name != NULL; var = var->next)
{
printf(".LC%d:\n", var->offset);
printf(" .string \"%s\"\n", var->name);
}
}
printf(".globl main\n");
for (int i = 0; code[i]; i++)
{
gen(code[i]);
}
}