-
Notifications
You must be signed in to change notification settings - Fork 19
/
yabfc.c
274 lines (233 loc) · 10.4 KB
/
yabfc.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "yabfc.h"
/**
* Function which takes care of parsing command line switches
* @param key Argument switch
* @param arg Argument data string
* @param state The state of the current arguments
* @return error_t on error
*/
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
struct argumentStruct *arguments = state->input;
switch (key) { // Switch for setting the argument values
case 'q':
case 's': arguments->silent = 1; break;
case 'v': arguments->verbose = arg ? atoi(arg) : 2; break;
case 'o': arguments->output_file = arg; break;
case 'O': arguments->optimize = arg ? atoi(arg) : 2; break;
case ARGP_KEY_NO_ARGS:
argp_usage(state);
case ARGP_KEY_ARGS:
arguments->inputFiles = &state->argv[state->next];
state->next = state->argc;
break;
case ARGP_KEY_END:
if (state->arg_num < 1) argp_usage(state); // Not enough files
break;
default:
return ARGP_ERR_UNKNOWN; // Error
}
return 0; // No error
}
/**
* Main function
* @param argc Argument count
* @param argv arguments
* @return Program return value
*/
int main(int argc, char *argv[]) {
int numFiles;
char *outputFilename;
FILE *readFile, *writeFile; // Read and write file pointers
argp_parse(&argp, argc, argv, 0, 0, &arguments); // Parse the command line arguments
for (numFiles = 0; arguments.inputFiles[numFiles]; numFiles++) // Count the number of input files
;
globalOptions.optimize = arguments.optimize;
globalOptions.verbose = arguments.verbose;
globalOptions.silent = arguments.silent;
globalOptions.outputFile = arguments.output_file;
debugPrintf(1, "Optimization level: %i\nVerbosity level: %i\nSilent mode: %s\nOutput file: %s\n",
globalOptions.optimize,
globalOptions.verbose,
globalOptions.silent ? "true" : "false",
globalOptions.outputFile);
if (numFiles > 1) {
debugPrintf(1, "WARNING: Multiple files specified, ignoring output file flag\n");
globalOptions.outputFile = "";
}
for (int i = 0; arguments.inputFiles[i]; i++) { // Loop through the input files
debugPrintf(1, "Opening file %s\n", arguments.inputFiles[i]);
readFile = fopen(arguments.inputFiles[i], "r"); // Open file for reading
if (readFile == NULL) {
perror("Error opening file: ");
exit(1);
}
if (strcmp(globalOptions.outputFile, "") != 0) {
writeFile = fopen(globalOptions.outputFile, "w+");
chmod(globalOptions.outputFile, 0755);
} else {
outputFilename = filenameWithoutExtension(arguments.inputFiles[i]);
debugPrintf(1, "Output file: %s\n", outputFilename);
writeFile = fopen(outputFilename, "w+");
chmod(outputFilename, 0755);
free(outputFilename);
}
SECTION text, data, stringTable; // Set up sections
text.size = 0;
text.bytes = malloc(text.size * sizeof(uint8_t));
data.size = 0;
data.bytes = malloc(data.size * sizeof(uint8_t));
stringTable.size = 0;
stringTable.bytes = malloc(stringTable.size * sizeof(uint8_t));
Elf64_Ehdr ELFHeader; // Initialize the ELF header
setupELFHeader(&ELFHeader,
ENTRY_POINT,
PGM_HEADER_TBL_LOC,
0x0, /*Section header location*/
PGM_HEADER_SIZE, PGM_HEADER_NUM,
SEC_HEADER_SIZE, SEC_HEADER_NUM); // Mostly set up the ELF header
debugPrintf(2, "Constructing .text section\n");
INSTRUCTIONS instructions = {
.size = 0,
.instruction = malloc(0)};
debugPrintf(2, "Program as read from file: ");
char readCharacter = 0;
while ((readCharacter = fgetc(readFile)) != EOF) {
if (strchr(VALID_COMMANDS, readCharacter) != NULL) {
debugPrintf(2, "%c", readCharacter);
INSTRUCTION tempInstruction = {
.type = readCharacter,
.bracketMatch = -1};
instructions.instruction = (INSTRUCTION *)realloc(instructions.instruction, (instructions.size + 1) * sizeof(INSTRUCTION));
instructions.instruction[instructions.size++] = tempInstruction;
}
}
debugPrintf(2, "\n");
debugPrintf(2, "Starting machine code generation\n");
CODE code = {
.size = 0,
.bytes = malloc(0)};
construct_START(&code);
int relativeBracket;
for (int i = 0; i < instructions.size; i++) {
if (optimize_compress_PTR(&instructions, &i, &code) | // Bitwise OR to prevent short circuit evaluation
optimize_compress_ADDSUB(&instructions, &i, &code) |
optimize_clear_loop(&instructions, &i, &code) |
optimize_multiplication(&instructions, &i, &code)) {
continue;
}
switch (instructions.instruction[i].type) {
case '+':
construct_ADD(&code, 1);
break;
case '-':
construct_SUB(&code, 1);
break;
case '<':
construct_ADDESP(&code, SIZEOFSTACKADDRESS);
break;
case '>':
construct_SUBESP(&code, SIZEOFSTACKADDRESS);
break;
case '[':
if ((relativeBracket = get_matching_bracket(&instructions, i, true)) == -1) {
fprintf(stderr, "Opening bracket does not have a matching closing bracket at position %i!\n", i);
exit(1);
}
instructions.instruction[i].bracketMatch = relativeBracket;
instructions.instruction[i + relativeBracket].bracketMatch = relativeBracket;
debugPrintf(3, "Opening bracket @ %i jumps forward by %i\n", i, relativeBracket);
construct_LPSTART(&code);
break;
case ']':
if (instructions.instruction[i].bracketMatch == -1) {
fprintf(stderr, "Closing bracket does not have a matching opening bracket at position %i!\n", i);
exit(1);
}
debugPrintf(3, "Closing bracket @ %i jumps backward by %i\n", i, instructions.instruction[i].bracketMatch);
construct_LPEND(&code);
break;
case ',':
construct_INPUT(&code);
break;
case '.':
construct_PRINT(&code);
break;
}
}
construct_END(&code);
addSectionData(&text, code.bytes, code.size); // Add the .text
debugPrintf(2, "Optimization results:\n"
"\tADDSUB compression: %i\n"
"\tPTR compression: %i\n"
"\tLOOPCLEAR optimization: %i\n"
"\tMULTIPLY optimization: %i\n",
total_ADDSUB_compress, total_PTR_compress, total_CLEARLOOP, total_MULTIPLY);
debugPrintf(2, "Code bytesize: %i\nSection bytesize: %i\n", code.size, text.size);
debugPrintf(2, "Constructing .data section\n");
uint8_t tempData = '0';
for (int i = 0; i < 0; i++)
addSectionData(&data, (uint8_t *)&tempData, sizeof(tempData)); // Add some example data
debugPrintf(2, "Constructing .shrtrab section\n");
uint8_t stringData[] = "\0.text\0.data\0.shrtrab\0"; // Set up the string table section with the appropriate names
addSectionData(&stringTable, (uint8_t *)&stringData, sizeof(stringData));
Elf64_Phdr programHeaderTable[PGM_HEADER_NUM];
setupprogramHeader(&programHeaderTable[0], /* .text segment */
PF_R + PF_X, /* Segment permissions */
TEXT_FILE_LOC, /* File offset for the contents of the segment */
TEXT_MEM_LOC, /* Virtual address where the segment will be loaded */
text.size /* Segment size */
);
setupprogramHeader(&programHeaderTable[1], /* .data segment */
PF_R + PF_W + PF_X,
TEXT_FILE_LOC + text.size,
TEXT_MEM_LOC + text.size,
data.size);
Elf64_Shdr sectionHeaderTable[SEC_HEADER_NUM];
setupSectionHeader(§ionHeaderTable[0], 0, SHT_NULL, 0, 0, 0, 0); // Set up null section header
setupSectionHeader(§ionHeaderTable[1], /* .text header */
stringIndexFromSectionIndex(stringTable.bytes, 1), /* String table index */
SHT_PROGBITS, /* Type of segment*/
SHF_ALLOC + SHF_EXECINSTR, /* Section permissions */
TEXT_MEM_LOC, /* Section memory location */
TEXT_FILE_LOC, /* Section file location */
text.size /* Segment size */
);
setupSectionHeader(§ionHeaderTable[2], /* .data header */
stringIndexFromSectionIndex(stringTable.bytes, 2),
SHT_PROGBITS,
SHF_ALLOC + SHF_WRITE,
TEXT_MEM_LOC + text.size,
TEXT_FILE_LOC + text.size,
data.size);
setupSectionHeader(§ionHeaderTable[3], /* .shrtrab header */
stringIndexFromSectionIndex(stringTable.bytes, 3),
SHT_STRTAB,
0,
0,
TEXT_FILE_LOC + text.size + data.size,
stringTable.size);
ELFHeader.e_shstrndx = 3; // Set which section header describes the string table
ELFHeader.e_shoff = SEC_HEADER_NUM == 0 ? 0x0 : TEXT_FILE_LOC + text.size + data.size + stringTable.size; // Finally determine the section header offset
debugPrintf(2, "Writing ELF header\n");
fwrite(&ELFHeader, 1, sizeof(ELFHeader), writeFile); // Write header information
debugPrintf(2, "Writing program header table\n");
fwrite(&programHeaderTable, 1, sizeof(programHeaderTable), writeFile); // Write program header table
debugPrintf(2, "Writing .text section of size %i bytes\n", text.size);
fwrite(text.bytes, 1, text.size, writeFile); // Write .text
debugPrintf(2, "Writing .data section of size %i bytes\n", data.size);
fwrite(data.bytes, 1, data.size, writeFile); // Write .data
debugPrintf(2, "Writing .shrtrab section of size %i bytes\n", stringTable.size);
fwrite(stringTable.bytes, 1, stringTable.size, writeFile); // Write .shrtrab
debugPrintf(2, "Writing section header, %i sections\n", ELFHeader.e_shnum);
fwrite(§ionHeaderTable, 1, sizeof(sectionHeaderTable), writeFile); // Write section header table
debugPrintf(1, "Entry point: %#08x\n", ENTRY_POINT);
debugPrintf(1, "Done processing file %s\n", arguments.inputFiles[i]);
free(instructions.instruction); // Free allocated memory
free(code.bytes);
free(text.bytes);
free(data.bytes);
free(stringTable.bytes);
fclose(readFile); // Close read file pointer
fclose(writeFile); // Close write file pointer
}
}