no size burn in, exit WIP

This commit is contained in:
anon
2024-07-17 02:35:31 +02:00
parent 42cb6e8a0a
commit faf7a780ee
7 changed files with 59 additions and 8 deletions

View File

@ -5,6 +5,8 @@
#include <stdarg.h> #include <stdarg.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "eaxhla.tab.h"
#include "eaxhla.h" #include "eaxhla.h"
#include "assembler.h" #include "assembler.h"
#include "unix.h" #include "unix.h"
@ -53,11 +55,6 @@ void dump_variables_to_assembler(void) {
static static
int write_output(FILE * file) { int write_output(FILE * file) {
// XXX Where can i move these?
elf_main_header (1, 1, 1);
elf_text_sector (text_sector_size, 0x27); // HACK
elf_data_sector (text_sector_size, 0x27); // HACK
checked_fwrite(elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file); checked_fwrite(elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file);
checked_fwrite(elf_text_sector_byte, 1UL, ELF_TEXT_SECTOR_SIZE, file); checked_fwrite(elf_text_sector_byte, 1UL, ELF_TEXT_SECTOR_SIZE, file);
checked_fwrite(elf_data_sector_byte, 1UL, ELF_DATA_SECTOR_SIZE, file); checked_fwrite(elf_data_sector_byte, 1UL, ELF_DATA_SECTOR_SIZE, file);
@ -68,6 +65,15 @@ int write_output(FILE * file) {
return 0; return 0;
} }
static
int create_header() {
int total_reserved_size = variable_size_sum();
// XXX portability
elf_main_header(1, 1, 1);
elf_text_sector(text_sector_size, total_reserved_size);
elf_data_sector(text_sector_size, total_reserved_size);
}
static static
int make_executable(const char * const filename) { int make_executable(const char * const filename) {
int r = 0; int r = 0;
@ -86,6 +92,8 @@ int compile(void) {
assemble(token_count, token_array); assemble(token_count, token_array);
create_header();
FILE * output_file = fopen(output_file_name, "w"); FILE * output_file = fopen(output_file_name, "w");
check(write_output(output_file)); check(write_output(output_file));
fclose(output_file); fclose(output_file);
@ -107,7 +115,12 @@ void _append_instructions(const unsigned argc, ...) {
va_end(ap); va_end(ap);
} }
// my_label: extern void append_exit(int code) {
if (system_type == UNIX) {
append_instructions(MOV, R0, 60, MOV, R7, code, SYSCALL);
}
}
void append_label (int rel) { void append_label (int rel) {
append_instructions(ASMDIRMEM, rel); append_instructions(ASMDIRMEM, rel);
} }

View File

@ -11,7 +11,9 @@ extern int compile_deinit(void);
extern int compile(void); extern int compile(void);
extern void append_label (int rel); extern void append_label(int rel);
extern void append_exit(int code);
/* --- Token appending /* --- Token appending
* The core problem is that we want an interface where we can append {instructoin} * The core problem is that we want an interface where we can append {instructoin}

View File

@ -62,6 +62,7 @@ static
void debug_dump_symbols(void) { void debug_dump_symbols(void) {
debug_dump_variables(); debug_dump_variables();
debug_dump_functions(); debug_dump_functions();
printf("# Total variable size: '%d'\n", variable_size_sum());
} }
static static

View File

@ -159,6 +159,34 @@ int type2size(const int type) {
return -1; return -1;
} }
int size2bytes(const int size) {
switch (size) {
case D8: return 1;
case D16: return 2;
case D32: return 4;
case D64: return 8;
}
issue_error("internal error");
return -1;
}
static
void _variable_size_sum_iteration(void * i, void * data) {
symbol_t * variable = (symbol_t*)data;
if (variable->symbol_type != VARIABLE) { return; }
int * sum = i;
*sum += variable->elements * size2bytes(type2size(variable->type));
}
int variable_size_sum(void) {
int r = 0;
tommy_hashtable_foreach_arg(&symbol_table, _variable_size_sum_iteration, &r);
return r;
}
int validate_array_size(const int size) { int validate_array_size(const int size) {
if (size < 1) { if (size < 1) {
issue_error("cannot create an array of size '%d', because its less than 1", size); issue_error("cannot create an array of size '%d', because its less than 1", size);

View File

@ -66,6 +66,8 @@ extern void add_procedure(symbol_t procedure);
extern symbol_t * get_function(const char * const name); extern symbol_t * get_function(const char * const name);
extern int type2size(int type); extern int type2size(int type);
extern int size2bytes(const int size);
extern int variable_size_sum(void);
extern void issue_warning(const char * format, ...); extern void issue_warning(const char * format, ...);
extern void issue_error(const char * format, ...); extern void issue_error(const char * format, ...);

View File

@ -411,7 +411,7 @@ artimetric_operand: LITERAL
| IDENTIFIER { $$ = 0; /*XXX*/ } | IDENTIFIER { $$ = 0; /*XXX*/ }
; ;
exit: EXIT value exit: EXIT value { append_exit($2); }
; ;
/* XXX /* XXX

5
test/exit.eax Normal file
View File

@ -0,0 +1,5 @@
unix
program myexit
begin
exit 69
end program