Compiles, but has errors, needs sane refactoring!

This commit is contained in:
xolatile
2024-07-07 13:32:04 -04:00
parent b65e32bb0e
commit 3e1553b87b
5 changed files with 114 additions and 9 deletions

View File

@ -48,7 +48,8 @@ ${OBJECT.d}/%.yy.c: ${SOURCE.d}/%.l
flex ${FLEXFLAGS} --header-file=object/$(basename $(notdir $<)).yy.h -o $@ $< flex ${FLEXFLAGS} --header-file=object/$(basename $(notdir $<)).yy.h -o $@ $<
${OBJECT.d}/%.tab.c: ${SOURCE.d}/%.y ${OBJECT.d}/%.tab.c: ${SOURCE.d}/%.y
bison ${BISONFLAGS} --header=object/$(basename $(notdir $<)).tab.h -o $@ $< #~ bison ${BISONFLAGS} --header=object/$(basename $(notdir $<)).tab.h -o $@ $<
bison ${BISONFLAGS} --defines=object/$(basename $(notdir $<)).tab.h -o $@ $<
${OBJECT.d}/%.yy.o: ${OBJECT.d}/%.yy.c ${OBJECT.d}/%.yy.o: ${OBJECT.d}/%.yy.c
${COMPILE.c} -o $@ $< ${COMPILE.c} -o $@ $<

15
debug/heyo_world.eax Normal file
View File

@ -0,0 +1,15 @@
program heyo_world
begin
mov eax 1
mov edi 1
mov esi 4198610
mov edx 12
syscall
mov eax 60
mov edi 0
syscall
end program

View File

@ -2,20 +2,30 @@ format ELF64 executable 3
segment readable executable segment readable executable
mov al, 1 mov eax, 1
mov dil, 1 mov edi, 1
mov esi, heyo mov esi, heyo
mov dl, 5 mov edx, 5
syscall syscall
mov al, 60 mov eax, 60
mov dil, 0 mov edi, 0
syscall syscall
segment readable writable segment readable writable
heyo: db 72, 69, 89, 79, 10 heyo: db 72, 69, 89, 79, 10
;~B8 01 00 00 00
;~BF 01 00 00 00
;~BE D2 10 40 00
;~BA 05 00 00 00
;~0F 05
;~B8 3C 00 00 00
;~BF 00 00 00 00
;~0F 05
;~MOV D8 REG R0 IMM 1 ;~MOV D8 REG R0 IMM 1
;~MOV D8 REG R7 IMM 1 *** ;~MOV D8 REG R7 IMM 1 ***
;~MOV D32 REG R6 IMM 4198597 ;~MOV D32 REG R6 IMM 4198597

View File

@ -194,11 +194,11 @@ instruction: INOP { ; }
| IADD register register | IADD register register
| IADD register immediate | IADD register immediate
| IADD register memory | IADD register memory
| ISYSCALL | ISYSCALL { append_instruction_t1 (SYSCALL); }
| IMOV register register | IMOV register register
| IMOV memory register | IMOV memory register
| IMOV register memory | IMOV register memory
| IMOV register immediate | IMOV register immediate { append_instruction_t6 (MOV, D32, REG, $2, REG, $3); }
| IMOV memory immediate | IMOV memory immediate
| IXOR register register | IXOR register register
| IXOR register memory | IXOR register memory

View File

@ -1,8 +1,19 @@
#include <stdio.h> #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.h"
#include "eaxhla.yy.h" #include "eaxhla.yy.h"
#include "eaxhla.tab.h" #include "eaxhla.tab.h"
#include "assembler.h"
extern void yyfree_leftovers(void); extern void yyfree_leftovers(void);
@ -14,6 +25,9 @@ signed main(int argc, char * argv[]) {
return 1; return 1;
} }
token_array = calloc (1440UL, sizeof (* token_array));
t_array = calloc (1440UL, sizeof (* t_array));
#if DEBUG == 1 #if DEBUG == 1
yydebug = 1; yydebug = 1;
#endif #endif
@ -29,9 +43,74 @@ signed main(int argc, char * argv[]) {
yyparse(); yyparse();
assemble (t_count, t_array);
dump ("test_me_please");
yyfree_leftovers(); yyfree_leftovers();
eaxhla_destroy(); eaxhla_destroy();
free (token_array);
free (t_array);
return 0; return 0;
} }
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
}
void append_instruction_t1 (int t1) {
append_token (t1); // operation
}
void dump (const char * file_name) {
/* DO NOT TRY THIS AT HOME! */
#define hackery \
"\x7F\x45\x4C\x46\x02\x01\x01\x03"\
"\x00\x00\x00\x00\x00\x00\x00\x00"\
"\x02\x00\x3E\x00\x01\x00\x00\x00"\
"\xB0\x00\x40\x00\x00\x00\x00\x00"\
"\x40\x00\x00\x00\x00\x00\x00\x00"\
"\x00\x00\x00\x00\x00\x00\x00\x00"\
"\x00\x00\x00\x00\x40\x00\x38\x00"\
"\x02\x00\x40\x00\x00\x00\x00\x00"\
"\x01\x00\x00\x00\x05\x00\x00\x00"\
"\x00\x00\x00\x00\x00\x00\x00\x00"\
"\x00\x00\x40\x00\x00\x00\x00\x00"\
"\x00\x00\x40\x00\x00\x00\x00\x00"\
"\xC5\x00\x00\x00\x00\x00\x00\x00"\
"\xC5\x00\x00\x00\x00\x00\x00\x00"\
"\x00\x10\x00\x00\x00\x00\x00\x00"\
"\x01\x00\x00\x00\x06\x00\x00\x00"\
"\xC5\x00\x00\x00\x00\x00\x00\x00"\
"\xC5\x10\x40\x00\x00\x00\x00\x00"\
"\xC5\x10\x40\x00\x00\x00\x00\x00"\
"\x05\x00\x00\x00\x00\x00\x00\x00"\
"\x05\x00\x00\x00\x00\x00\x00\x00"\
"\x00\x10\x00\x00\x00\x00\x00\x00"
char meme [1024] = "";
FILE * file = fopen (file_name, "w");
fwrite (hackery, 1UL, 64UL + 2UL * 56UL, file);
fwrite (token_array, sizeof (* token_array), (size_t) token_count, file);
fwrite ("heyo world!\n", 1UL, 12UL, file);
snprintf (meme, 1023UL, "chmod +x %s", file_name);
system (meme);
fclose (file);
}