-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
211 lines (186 loc) · 6.47 KB
/
main.c
File metadata and controls
211 lines (186 loc) · 6.47 KB
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
/*
Autor: Benjamin Herrera Navarro
6:20AM Implementando una gramatica simple, como prueba del assembler
*/
#include <argp.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "libs/terminal_colors.h"
#include "libs/file_table.h"
#include "libs/symbol_table.h"
#include "internals.h"
#include "obj_file.h"
#include "debug.h"
const char *argp_program_version = "dasm";
const char *argp_program_bug_address = "<bitglitcher@github.com>";
static char doc[] = "DASM a portable Assembler.";
static char args_doc[] = "[FILENAME]...";
static struct argp_option options[] = {
{ "output", 'o', "FILE", 0, "Place output file as <file>."},
{ "raw", 'r', 0, 0, "Output raw bytes."},
{ "table", 't', 0, 0, "Display symbol table."},
{ 0 }
};
struct arguments {
char* output_file;
char* input_file;
bool table_display;
bool raw;
};
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
struct arguments *arguments = state->input;
switch (key) {
case 'o': arguments->output_file = strdup(arg); break;
case 't': arguments->table_display = true; break;
case 'r': arguments->raw = true; break;
case ARGP_KEY_ARG: arguments->input_file = strdup(arg); return 0;
default: return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };
//Lex and Bison Stuff
extern int yyparse();
extern int yy_scan_bytes ( const char *, int);
extern int yylex();
extern int yylex_destroy(void);
extern void init_internals();
//FILE TABLE
FILE_TABLE file_table;
SYMBOL_TABLE symbol_table;
//Parsing variables
bool dry_run = true;
//Bin buffer
BIN_BUFFER bin_buffer;
//Ins buffer
void print_symbol_table(SYMBOL_TABLE* symbol_table)
{
printf("\n\n------------SYMBOL_TABLE------------\n");
printf("size: %d\n", symbol_table->size);
printf("capacity: %d\n", symbol_table->capacity);
printf("wait_slot: %d\n", symbol_table->wait_slot);
for(int i = 0;i <= symbol_table->size;i++)
{
printf("NODE: 0x%x\n", i);
printf("\taddr %d\n", symbol_table->data[i].addr);
printf("\tdomain %s\n", symbol_table->data[i].domain);
printf("\tname %s\n", symbol_table->data[i].name);
printf("\ttype %d\n", symbol_table->data[i].type);
}
}
int main(int argc, char *argv[])
{
struct arguments arguments;
arguments.output_file = NULL;
arguments.input_file = NULL;
arguments.table_display = false;
arguments.raw = false;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
if(arguments.input_file != NULL)
{
printf("Input File %s\n", arguments.input_file);
if(arguments.output_file == NULL)
{
if(arguments.raw)
{
arguments.output_file = "ROM.bin";
}
else
{
printf("Using default output -> obj.bin\n");
arguments.output_file = "obj.o";
}
}
else
{
printf("Using specified output -> %s\n", arguments.output_file);
}
//File table
init_file_table(&file_table);
init_symbol_table(&symbol_table);
init_internals();
//Init Bin Buffer
bin_buffer.size = 0;
bin_buffer.capacity = 8;
bin_buffer.wait_slot = true;
bin_buffer.data = malloc(sizeof(char) * bin_buffer.capacity);
append_file(&file_table, arguments.input_file);
//Parse first file
FILE_NODE* file_node = search_file(&file_table, arguments.input_file);
if(file_node)
{
dry_run = true;
yy_scan_bytes(file_node->data, file_node->size);
yyparse();
yylex_destroy();
dry_run = false;
yy_scan_bytes(file_node->data, file_node->size);
yyparse();
if(arguments.table_display) print_symbol_table(&symbol_table);
//Now print the contents of the assembled file
const int n_col = 4;
int col_cnt = 0;
printf("Size of Bin_buffer %d\n", bin_buffer.size);
printf("Contents of Bin_Buffer:\n\n");
for(int i = 0; i <= bin_buffer.size + 1;i++)
{
if(col_cnt == n_col)
{
printf("\n");
col_cnt = 0;
}
printf("0x%02x ", bin_buffer.data[i] & 0xff);
col_cnt++;
}
printf("\n");
if(arguments.raw)
{
FILE* raw_output = fopen(arguments.output_file, "w+");
if(raw_output == NULL)
{
printf("dasm: " ANSI_COLOR_RED "fatal error" ANSI_COLOR_RESET ": creating object file\n");
return -1;
}
else
{
if(raw_output == NULL)
{
printf("dasm: " ANSI_COLOR_RED "fatal error" ANSI_COLOR_RESET ": creating object file\n");
return -1;
}
else
{
//Write file
fwrite(bin_buffer.data, sizeof(char), bin_buffer.size, raw_output);
}
}
}
else
{
printf("\n");
//Transfer bin_buffer to the object file structure
OBJ_FILE obj;
obj.size = bin_buffer.size + 1;
obj.binary_data = malloc(sizeof(char) * obj.size);
obj.reloc_table.reloc = malloc(sizeof(RELOC) * 1);
obj.reloc_table.size = 1;
obj.reloc_table.reloc[0].name = "hola";
obj.reloc_table.reloc[0].size = strlen(obj.reloc_table.reloc[0].name);
memcpy(obj.binary_data, bin_buffer.data, obj.size);
serialize(arguments.output_file, &obj);
}
}
else
{
printf("dasm: " ANSI_COLOR_RED "fatal error" ANSI_COLOR_RESET ": empty file_node\n");
}
}
else
{
printf("dasm: " ANSI_COLOR_RED "fatal error" ANSI_COLOR_RESET ": no input file\n");
}
//FILE* main_file;
//fopen(main_file, )
}