111 lines
2.3 KiB
C
111 lines
2.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
static unsigned int * t_array = NULL;
|
|
static unsigned int t_count = 0;
|
|
|
|
extern void append_token (int t);
|
|
extern void append_instruction_t6 (int t6, int w, int d, int r, int s, int i);
|
|
extern void append_instruction_t1 (int t1);
|
|
|
|
static void dump (const char * file_name);
|
|
|
|
#include "eaxhla.h"
|
|
#include "eaxhla.yy.h"
|
|
#include "eaxhla.tab.h"
|
|
#include "assembler.h"
|
|
#include "unix.h"
|
|
|
|
extern void yyfree_leftovers(void);
|
|
|
|
char * yyfilename;
|
|
|
|
signed main(int argc, char * argv[]) {
|
|
if (argc < 2) {
|
|
printf("%s: <file>\n", argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
text_sector_byte = calloc (1440UL, sizeof (* text_sector_byte));
|
|
t_array = calloc (1440UL, sizeof (* t_array));
|
|
|
|
#if DEBUG == 1
|
|
yydebug = 1;
|
|
#endif
|
|
|
|
yyfilename = argv[1];
|
|
|
|
yyin = fopen(yyfilename, "r");
|
|
|
|
if (eaxhla_init()) {
|
|
puts("Initialization failed");
|
|
return 1;
|
|
}
|
|
|
|
yyparse();
|
|
|
|
if (!has_encountered_error) {
|
|
assemble (t_count, t_array);
|
|
dump ("test_me_please");
|
|
}
|
|
|
|
if (was_instruction_array_empty) {
|
|
issue_warning("the input did not contain any instructions");
|
|
}
|
|
|
|
yyfree_leftovers();
|
|
|
|
eaxhla_destroy();
|
|
|
|
free (text_sector_byte);
|
|
free (t_array);
|
|
|
|
return has_encountered_error;
|
|
}
|
|
|
|
void append_token (int t) {
|
|
t_array [t_count] = t;
|
|
t_count += 1;
|
|
}
|
|
|
|
void append_instruction_t6 (int t6, int w, int d, int r, int s, int i) {
|
|
append_token (t6); // operation
|
|
append_token (w); // width
|
|
append_token (d); // destination
|
|
append_token (r); // register
|
|
append_token (s); // source
|
|
append_token (i); // immediate
|
|
|
|
printf ("> MOV D32 REG %i IMM %i\n", r, i);
|
|
}
|
|
|
|
void append_instruction_t1 (int t1) {
|
|
append_token (t1); // operation
|
|
}
|
|
|
|
void dump (const char * file_name) {
|
|
elf_main_header (1, 1, 1, 0);
|
|
elf_text_sector (34);
|
|
elf_data_sector (34, 12);
|
|
|
|
char meme [1024] = "";
|
|
FILE * file = fopen (file_name, "w");
|
|
|
|
fwrite (elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file);
|
|
fwrite (elf_text_sector_byte, 1UL, ELF_TEXT_SECTOR_SIZE, file);
|
|
fwrite (elf_data_sector_byte, 1UL, ELF_DATA_SECTOR_SIZE, file);
|
|
|
|
//text
|
|
fwrite (text_sector_byte, sizeof (* text_sector_byte),
|
|
(size_t) text_sector_size, file);
|
|
|
|
// data
|
|
fwrite ("heyo world!\n", 1UL, 12UL, file);
|
|
|
|
snprintf (meme, 1023UL, "chmod +x %s", file_name);
|
|
|
|
system (meme);
|
|
|
|
fclose (file);
|
|
}
|