forked from oriansj/M2-Planet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc.c
206 lines (192 loc) · 5.94 KB
/
cc.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
/* Copyright (C) 2016 Jeremiah Orians
* Copyright (C) 2020 deesix <[email protected]>
* This file is part of M2-Planet.
*
* M2-Planet is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* M2-Planet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
*/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"cc.h"
/* The core functions */
void initialize_types();
struct token_list* read_all_tokens(FILE* a, struct token_list* current, char* filename);
struct token_list* reverse_list(struct token_list* head);
struct token_list* remove_line_comments(struct token_list* head);
struct token_list* remove_line_comment_tokens(struct token_list* head);
struct token_list* remove_preprocessor_directives(struct token_list* head);
void eat_newline_tokens();
void preprocess();
void program();
void recursive_output(struct token_list* i, FILE* out);
void output_tokens(struct token_list *i, FILE* out);
int numerate_string(char *a);
int main(int argc, char** argv)
{
MAX_STRING = 4096;
BOOTSTRAP_MODE = FALSE;
PREPROCESSOR_MODE = FALSE;
int DEBUG = FALSE;
FILE* in = stdin;
FILE* destination_file = stdout;
Architecture = KNIGHT_NATIVE; /* Assume Knight-native */
char* arch;
char* name;
int i = 1;
while(i <= argc)
{
if(NULL == argv[i])
{
i = i + 1;
}
else if(match(argv[i], "-f") || match(argv[i], "--file"))
{
if(NULL == hold_string)
{
hold_string = calloc(MAX_STRING, sizeof(char));
require(NULL != hold_string, "Impossible Exhustion has occured\n");
}
name = argv[i + 1];
in = fopen(name, "r");
if(NULL == in)
{
file_print("Unable to open for reading file: ", stderr);
file_print(name, stderr);
file_print("\n Aborting to avoid problems\n", stderr);
exit(EXIT_FAILURE);
}
global_token = read_all_tokens(in, global_token, name);
fclose(in);
i = i + 2;
}
else if(match(argv[i], "-o") || match(argv[i], "--output"))
{
destination_file = fopen(argv[i + 1], "w");
if(NULL == destination_file)
{
file_print("Unable to open for writing file: ", stderr);
file_print(argv[i + 1], stderr);
file_print("\n Aborting to avoid problems\n", stderr);
exit(EXIT_FAILURE);
}
i = i + 2;
}
else if(match(argv[i], "-A") || match(argv[i], "--architecture"))
{
arch = argv[i + 1];
if(match("knight-native", arch)) Architecture = KNIGHT_NATIVE;
else if(match("knight-posix", arch)) Architecture = KNIGHT_POSIX;
else if(match("x86", arch)) Architecture = X86;
else if(match("amd64", arch)) Architecture = AMD64;
else if(match("armv7l", arch)) Architecture = ARMV7L;
else if(match("aarch64", arch)) Architecture = AARCH64;
else
{
file_print("Unknown architecture: ", stderr);
file_print(arch, stderr);
file_print(" know values are: knight-native, knight-posix, x86, amd64, armv7l and aarch64", stderr);
exit(EXIT_FAILURE);
}
i = i + 2;
}
else if(match(argv[i], "--max-string"))
{
MAX_STRING = numerate_string(argv[i+1]);
require(0 < MAX_STRING, "Not a valid string size\nAbort and fix your --max-string\n");
i = i + 2;
}
else if(match(argv[i], "--bootstrap-mode"))
{
BOOTSTRAP_MODE = TRUE;
i = i + 1;
}
else if(match(argv[i], "-g") || match(argv[i], "--debug"))
{
DEBUG = TRUE;
i = i + 1;
}
else if(match(argv[i], "-h") || match(argv[i], "--help"))
{
file_print(" -f input file\n -o output file\n --help for this message\n --version for file version\n", stdout);
exit(EXIT_SUCCESS);
}
else if(match(argv[i], "-E"))
{
PREPROCESSOR_MODE = TRUE;
i = i + 1;
}
else if(match(argv[i], "-V") || match(argv[i], "--version"))
{
file_print("M2-Planet v1.7.0\n", stderr);
exit(EXIT_SUCCESS);
}
else
{
file_print("UNKNOWN ARGUMENT\n", stdout);
exit(EXIT_FAILURE);
}
}
/* Deal with special case of wanting to read from standard input */
if(stdin == in)
{
hold_string = calloc(MAX_STRING, sizeof(char));
require(NULL != hold_string, "Impossible Exhustion has occured\n");
global_token = read_all_tokens(in, global_token, "STDIN");
}
if(NULL == global_token)
{
file_print("Either no input files were given or they were empty\n", stderr);
exit(EXIT_FAILURE);
}
global_token = reverse_list(global_token);
if (BOOTSTRAP_MODE)
{
global_token = remove_line_comment_tokens(global_token);
global_token = remove_preprocessor_directives(global_token);
}
else
{
global_token = remove_line_comments(global_token);
preprocess();
}
if (PREPROCESSOR_MODE)
{
file_print("\n/* Preprocessed source */\n", destination_file);
output_tokens(global_token, destination_file);
goto exit_success;
}
/* the main parser doesn't know how to handle newline tokens */
eat_newline_tokens();
initialize_types();
reset_hold_string();
output_list = NULL;
program();
/* Output the program we have compiled */
file_print("\n# Core program\n", destination_file);
recursive_output(output_list, destination_file);
if(KNIGHT_NATIVE == Architecture) file_print("\n", destination_file);
else if(DEBUG) file_print("\n:ELF_data\n", destination_file);
file_print("\n# Program global variables\n", destination_file);
recursive_output(globals_list, destination_file);
file_print("\n# Program strings\n", destination_file);
recursive_output(strings_list, destination_file);
if(KNIGHT_NATIVE == Architecture) file_print("\n:STACK\n", destination_file);
else if(!DEBUG) file_print("\n:ELF_end\n", destination_file);
exit_success:
if (destination_file != stdout)
{
fclose(destination_file);
}
return EXIT_SUCCESS;
}