mockup demo compiles

This commit is contained in:
anon 2024-07-02 18:52:34 +02:00
parent 47a569f057
commit 4a5d02bbe4
9 changed files with 145 additions and 15 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
*.o
*.out
*.tab.*
*.yy.*
eaxhla
eaxcc
.gdb_history

52
Makefile Normal file
View File

@ -0,0 +1,52 @@
.PHONY: clean test
ifeq (${DEBUG}, 1)
LFLAGS += --debug --trace
CFLAGS += -Wall -Wextra -Wpedantic
CFLAGS += -DDEBUG -O0 -ggdb -fno-inline
WRAP := valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all
else
CFLAGS += -O3 -fno-stack-protector
endif
OUT := eaxhla
SOURCE.d := source/
OBJECT.d := object/
SOURCE := main.c assembler.c
OBJECT := $(addprefix ${OBJECT.d}/,${SOURCE})
OBJECT := ${OBJECT:.c=.o}
GENSOURCE := eaxhla.yy.c eaxhla.tab.c
GENSOURCE := $(addprefix ${OBJECT.d}/,${GENSOURCE})
GENOBJECT := $(subst .c,.o,${GENSOURCE})
CFLAGS += -I${OBJECT.d}/ -I${SOURCE.d}/
${OBJECT.d}/%.yy.c: ${SOURCE.d}/%.l
flex --header-file=object/$(basename $(notdir $<)).yy.h -o $@ $<
${OBJECT.d}/%.tab.c: ${SOURCE.d}/%.y
bison --header=object/$(basename $(notdir $<)).tab.h -o $@ $<
${OBJECT.d}/%.yy.o: ${OBJECT.d}/%.yy.c
${COMPILE.c} -o $@ $<
${OBJECT.d}/%.tab.o: ${OBJECT.d}/%.tab.c
${COMPILE.c} -o $@ $<
${OBJECT.d}/%.o: ${SOURCE.d}/%.c
${COMPILE.c} -o $@ $<
${OUT}: ${GENSOURCE} ${GENOBJECT} ${OBJECT}
${LINK.c} -o $@ ${OBJECT} ${GENOBJECT} ${LDLIBS}
test: ${OUT}
./${OUT} debug/test.hla
clean:
-rm ${OUT}
-rm ${OBJECT}
-rm ${GENOBJECT}
-rm ${GENSOURCE}

4
debug/test.hla Normal file
View File

@ -0,0 +1,4 @@
program example
begin
xor $rax $rbx
end program

0
object/.gitkeep Normal file
View File

View File

@ -9,8 +9,8 @@ static void byte_push (byte data) {
byte_count += 1;
}
static form lower (form data) { return ((data => 0) && (data <= 7)); }
static form upper (form data) { return ((data => 8) && (data <= 15)); }
static form lower (form data) { return ((data >= 0) && (data <= 7)); }
static form upper (form data) { return ((data >= 8) && (data <= 15)); }
static void format_prefix (size_index size,
type_index type,
@ -62,10 +62,10 @@ static void format_modifier (size_index size,
byte_push (format);
}
static void assemble_xor (size_index size,
type_index type,
form destination,
form source) {
void assemble_xor (size_index size,
type_index type,
form destination,
form source) {
if (size == size_256b) exit (EXIT_FAILURE);
if (size == size_128b) exit (EXIT_FAILURE);

View File

@ -5,12 +5,12 @@
typedef enum {
size_256b, size_128b, size_64b,
size_32b, size_16b, size_8b
size_32b, size_16b, size_8b,
} size_index;
typedef enum {
type_register_register, type_register_variable, type_register_constant,
type_variable_register, type_variable_constant
type_variable_register, type_variable_constant,
} type_index;
typedef enum {
@ -21,7 +21,7 @@ typedef enum {
operation_is, operation_above, operation_below,
operation_if, operation_increment, operation_decrement,
operation_system, operation_call, operation_return,
operation_enter, operation_leave, operation_exit
operation_enter, operation_leave, operation_exit,
} operation_index;
typedef enum {
@ -30,7 +30,7 @@ typedef enum {
operand_rsp, operand_rbp, operand_r8,
operand_r9, operand_r10, operand_r11,
operand_r12, operand_r13, operand_r14,
operand_r15
operand_r15,
} operand_64b_index;
typedef enum {
@ -39,7 +39,7 @@ typedef enum {
operand_esp, operand_ebp, operand_r8d,
operand_r9d, operand_r10d, operand_r11d,
operand_r12d, operand_r13d, operand_r14d,
operand_r15d
operand_r15d,
} operand_32b_index;
typedef enum {
@ -48,7 +48,7 @@ typedef enum {
operand_sp, operand_bp, operand_r8w,
operand_r9w, operand_r10w, operand_r11w,
operand_r12w, operand_r13w, operand_r14w,
operand_r15w
operand_r15w,
} operand_16b_index;
typedef enum {
@ -57,17 +57,20 @@ typedef enum {
operand_spl, operand_bpl, operand_r8b,
operand_r9b, operand_r10b, operand_r11b,
operand_r12b, operand_r13b, operand_r14b,
operand_r15b
operand_r15b,
} operand_8b_index;
typedef enum {
command_operation, command_size, command_operand_d,
command_operand_s
command_length
command_operand_s,
command_length,
} command_index;
typedef signed int form;
typedef unsigned int next;
typedef unsigned char byte;
void assemble_xor (size_index size, type_index type, form destination, form source);
#endif

18
source/eaxhla.l Normal file
View File

@ -0,0 +1,18 @@
%{
#include "eaxhla.tab.h"
%}
%option noyywrap
identifier [A-z_][A-z0-9_]*
wsnl [ \t\r\v\f\n]
%%
program{wsnl}+{identifier}{wsnl}+begin{wsnl}+ { return PROGRAM_HEAD; }
end{wsnl}+program{wsnl}+ { return PROGRAM_TAIL; }
xor{wsnl}+ { return XOR; }
\$rax{wsnl}+ { return RAX; }
\$rbx{wsnl}+ { return RBX; }
%%

31
source/eaxhla.y Normal file
View File

@ -0,0 +1,31 @@
%{
#include <stdio.h>
#include "eaxhla.yy.h"
#include "assembler.h"
void yyerror() { ; }
%}
%union{
int intval;
}
%token PROGRAM_HEAD PROGRAM_TAIL
%token RAX RBX
%token XOR
%type<intval> register
%%
program: PROGRAM_HEAD code PROGRAM_TAIL { puts("this is in fact a program"); }
;
code: %empty
| XOR register register { assemble_xor(size_64b, type_register_register, $2, $3); }
;
register: RAX { $$ = 0x01; }
| RBX { $$ = 0x02; }
;
%%

15
source/main.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include "eaxhla.yy.h"
#include "eaxhla.tab.h"
signed main(int argc, char * argv[]) {
if (argc < 2) {
printf("%s <file>", argv[0]);
}
yyin = fopen(argv[1], "r");
yyparse();
return 0;
}