var things

This commit is contained in:
anon 2024-07-16 02:22:58 +02:00
parent 0ee309e25d
commit 56fde73d74
6 changed files with 87 additions and 22 deletions

View File

@ -29,6 +29,7 @@ int table_compare_unsigned(const void * arg, const void * obj) {
}
void add_variable(variable_t variable) {
static unsigned vid = 0;
if (get_variable(variable.name)) {
// XXX: this should say the varname, but this function does not know it
// in fact this source file should not be reporting errors,
@ -36,6 +37,7 @@ void add_variable(variable_t variable) {
issue_error("variable declared twice");
return;
}
variable._id = vid++;
// XXX this is cursed
variable_t * heap_variable = malloc(sizeof(variable));
memcpy(heap_variable, &variable, sizeof(variable));
@ -87,6 +89,25 @@ int can_fit(int type, long long value) {
return value > 0 ? (unsigned long long)value <= max : value >= min;
}
int type2size(int type) {
switch (type) {
case U8:
case S8:
return D8;
case U16:
case S16:
return D16;
case U32:
case S32:
return D32;
case U64:
case S64:
return D64;
}
return -1;
}
int validate_array_size(int size) {
if (size < 1) {
issue_error("cannot create an array of size '%d', because its less than 1", size);
@ -181,8 +202,21 @@ void issue_error(const char * const format, ...) {
free(msg);
}
extern unsigned int * t_array;
extern unsigned int t_count;
static
void dump_variable(void * data) {
variable_t * variable = (variable_t*)data;
append_instruction_t4(ASMDIRMEM, variable->_id, ASMDIRIMM, type2size(variable->type));
if (variable->elements == 1) {
append_instruction_t1(variable->value);
} else {
memcpy(t_array + t_count, variable->array_value, variable->elements);
t_count += variable->elements;
}
}
void dump_variables(void) {
tommy_hashtable_foreach(&variable_table, dump_variable);
}
static
void append_token (int t) {

View File

@ -11,6 +11,8 @@ typedef struct {
};
unsigned long long elements;
char * name;
int type;
unsigned _id;
unsigned _hash;
tommy_node _node;
} variable_t;
@ -32,6 +34,9 @@ extern int has_encountered_error;
extern char * scope;
extern unsigned int * t_array;
extern unsigned int t_count;
extern int eaxhla_init(void);
extern int eaxhla_destroy(void);
@ -46,6 +51,8 @@ extern void append_instruction_t1 (int t1);
extern void append_instruction_t4 (int t4, int w, int d, int r);
extern void append_instruction_t6 (int t6, int w, int d, int r, int s, int i);
extern int type2size(int type);
/*
-- THIS WILL BE REMOVED, HELPER COMMENT FOR ME...
@ -112,6 +119,8 @@ extern void append_fastcall_begin (int rel);
extern void append_fastcall_end (void);
extern void append_fastcall_arguments (int rel, int wid, int imm);
extern void dump_variables(void);
extern void issue_warning(const char * format, ...);
extern void issue_error(const char * format, ...);

View File

@ -20,6 +20,10 @@
%union{
long intval;
char * strval;
struct {
int type;
int value;
} argval;
struct {
unsigned long long len;
void * data;
@ -47,7 +51,7 @@
%token<strval> IDENTIFIER LABEL
%type<intval> immediate
%type<argval> immediate
%type<intval> memory dereference
%type<intval> artimetric_block artimetric_expression artimetric_operand
%type<intval> value
@ -139,11 +143,13 @@ declaration_section: %empty
declaration:
variable_specifier type IDENTIFIER {
$$.type = $2;
$$.name = make_scoped_name(scope, $3);
$$.elements = 1;
add_variable($$);
}
| variable_specifier type IDENTIFIER '=' LITERAL {
$$.type = $2;
$$.name = make_scoped_name(scope, $3);
if (!can_fit($2, $5)) {
issue_warning("the value \033[1m'%lld'\033[0m will overflow in assignement", $5);
@ -153,6 +159,7 @@ declaration:
add_variable($$);
}
| variable_specifier type '<' value '>' IDENTIFIER {
$$.type = $2;
if (validate_array_size($4)) {
break;
}
@ -161,6 +168,7 @@ declaration:
add_variable($$);
}
| variable_specifier type '<' value '>' IDENTIFIER '=' ARRAY_LITERAL {
$$.type = $2;
if (validate_array_size($4)) {
break;
}
@ -173,6 +181,7 @@ declaration:
add_variable($$);
}
| variable_specifier type '<' '>' IDENTIFIER '=' ARRAY_LITERAL {
$$.type = $2;
$$.name = make_scoped_name(scope, $5);
$$.elements = $7.len;
$$.array_value = $7.data;
@ -194,13 +203,19 @@ type: S8 { $$ = S8; }
| U64 { $$ = U64; }
;
immediate: LITERAL
| IDENTIFIER { $$ = 0; /* XXX: how the fuck do i dereference? */}
immediate: LITERAL { $$.type = IMM; $$.value = $1; }
| IDENTIFIER {
char * varname = make_scoped_name(scope, $1);
variable_t * variable = get_variable(varname);
$$.type = REL;
$$.value = variable->_id;
free(varname);
free($1);
}
;
memory: artimetric_block
| dereference
| IDENTIFIER
;
dereference: '[' IDENTIFIER ']' { $$ = 0; /* XXX: how the fuck do i dereference? */ }
@ -276,10 +291,12 @@ machine_code: %empty
| IDENTIFIER machine_code { free($1); }
;
call: calltype IDENTIFIER arguments { free($2); }
;
calltype: FASTCALL
call: FASTCALL IDENTIFIER arguments {
//append_fastcall_begin(/**/);
//append_fastcall_arguments();
append_fastcall_end();
free($2);
}
;
arguments: %empty
@ -437,9 +454,9 @@ instruction: INOP { append_instruction_t1(NOP); }
| ITSUB register register { append_instruction_t6( SUB, $2.size, REG, $2.number, REG, $3.number ); }
| ITXOR register register { append_instruction_t6( XOR, $2.size, REG, $2.number, REG, $3.number ); }
| ITCMP register register { append_instruction_t6( CMP, $2.size, REG, $2.number, REG, $3.number ); }
| ITSAR register immediate { append_instruction_t6( SAR, $2.size, REG, $2.number, IMM, (int)$3 ); }
| ITSAR register immediate { append_instruction_t6( SAR, $2.size, REG, $2.number, $3.type, $3.value ); }
| ITMOV register register { append_instruction_t6( MOV, $2.size, REG, $2.number, REG, $3.number ); }
| ITMOV register immediate { append_instruction_t6( MOV, $2.size, REG, $2.number, IMM, (int)$3 ); }
| ITMOV register immediate { append_instruction_t6( MOV, $2.size, REG, $2.number, $3.type, $3.value ); }
// #placeholder<instruction_parser_rules> END
;

View File

@ -5,6 +5,7 @@ unsigned int * t_array = NULL;
unsigned int t_count = 0;
static void dump (const char * file_name);
void dump_variables();
#include "eaxhla.h"
#include "eaxhla.yy.h"
@ -51,6 +52,7 @@ signed main(int argc, char * argv[]) {
yyparse();
if (!has_encountered_error) {
dump_variables();
assemble (t_count, t_array);
debug_puts("Dumping output...");
dump ("a.out");

View File

@ -1,20 +1,22 @@
unix program heyo_world
/*s8 heyo = "Heyo world!\n"
s8 cyaa = "Cyaa world!\n"*/
s8 <> heyo = "Heyo world!"
begin
/*
nop mov eax 1
nop mov edi 1
nop mov esi 4198610
nop mov edx 12
nop syscall
/*mov eax 1
*/
mov eax 1
mov edi 1
mov esi cyaa
mov esi heyo
mov edx 12
syscall*/
nop mov eax 60
nop mov edi 60
nop syscall
syscall
mov eax 60
mov edi 60
syscall
end program
/*

View File

@ -10,8 +10,9 @@ proc make_parser_rules {is} {
dict set r size "\$$n.size"
}
"immediate" {
dict set r enum "IMM"
dict set r value "(int)$$n"
dict set r enum "$$n.type"
dict set r value "$$n.value"
# XXX
dict set r size "32"
}
"memory" {