machine blocks FINALLY work

This commit is contained in:
anon
2024-07-17 22:50:41 +02:00
parent 78755df0a4
commit bec336d276
9 changed files with 77 additions and 58 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ eaxcc
gdb.txt
*.pp
library/tommyds/
library/sds/
tmp-*
.cmdtest-filter
*.pluglock

View File

@ -93,4 +93,4 @@ clean:
-rm a.out
${OUT}: ${PLUGLOCK} ${GENSOURCE} ${GENOBJECT} ${OBJECT} ${LIB}
${LINK.c} -o $@ ${OBJECT} ${GENOBJECT} ${LDLIBS} library/tommyds/tommy.o
${LINK.c} -o $@ ${OBJECT} ${GENOBJECT} ${LDLIBS} library/tommyds/tommy.o library/sds/sds.o

View File

@ -1,7 +1,9 @@
#!/bin/sh
# this script requires git.
cd "$(dirname "$(readlink -f "$0")")"
git clone https://github.com/amadvance/tommyds/ --depth=1
make -C tommyds
mkdir -v -p ../object
ln -v -s -f tommyds/tommy.o ../object/
git clone https://github.com/jcorporation/sds.git --depth=1
cc -O0 -ggdb -fno-inline -Wall -Wextra -Wpedantic -DDEBUG -c -o sds/sds.o sds/sds.c

View File

@ -55,6 +55,7 @@ void dump_variables_to_assembler(void) {
static
int write_output(FILE * file) {
// XXX portability
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_data_sector_byte, 1UL, ELF_DATA_SECTOR_SIZE, file);
@ -67,13 +68,17 @@ int write_output(FILE * file) {
static
int create_header() {
if (system_type == UNIX) {
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);
}
return 0;
}
static
int make_executable(const char * const filename) {
int r = 0;
@ -103,6 +108,11 @@ int compile(void) {
return 0;
}
void append_instructions_from_mem(void * src, unsigned n) {
memcpy(token_array + token_count, src, n);
token_count += n;
}
void _append_instructions(const unsigned argc, ...) {
va_list ap;
va_start(ap, argc);

View File

@ -15,6 +15,8 @@ extern void append_label(int rel);
extern void append_exit(int code);
extern void append_instructions_from_mem(void * src, unsigned n);
/* --- Token appending
* The core problem is that we want an interface where we can append {instructoin}
* tokens without modifying the counter as that gets old fast and error prone

View File

@ -6,6 +6,9 @@
#include <stdarg.h>
#include "eaxhla.h"
static
void breakpoint(void) { ; }
# define debug_puts(msg) do { puts(msg); } while (0)
static // this is less horid than macro varargs

View File

@ -1,25 +1,12 @@
%{
#include <stdlib.h>
#include "sds/sds.h"
#include "eaxhla.tab.h"
char * string_literal_buffer;
int string_litral_buffer_size = 0;
int string_litral_buffer_capacity = 128;
void string_literal_ensure_surplus(int s) {
if (string_litral_buffer_size + s > string_litral_buffer_capacity) {
string_litral_buffer_capacity *= 2;
void * ignore = realloc(string_literal_buffer, string_litral_buffer_capacity);
(void)ignore;
}
}
void string_literal_buffer_append_char(char c) {
string_literal_ensure_surplus(1);
string_literal_buffer[string_litral_buffer_size] = c;
string_litral_buffer_size += 1;
}
sds string_literal_buffer;
#define YY_USER_INIT \
string_literal_buffer = malloc(128);
string_literal_buffer = sdsnew("");
%}
%option noyywrap
@ -216,43 +203,51 @@ library { BEGIN INITIAL; return END_LIBRARY; }
<IN_STRING>{
/* XXX: multiline strings will die */
/* XXX: the first WORD_SIZE_IN_BYTES bytes should be 0'd */
/* XXX: i wanted short strings to be literals;
this however clashes with with the sanity of machine blocks;
those should be moved to '' (exactly like in Holy C)
*/
\" {
BEGIN INITIAL;
if (string_litral_buffer_size <= WORD_SIZE_IN_BYTES) {
memcpy(&yylval.intval, yytext, WORD_SIZE_IN_BYTES);
return LITERAL;
}
yylval.blobval.data = malloc(sdslen(string_literal_buffer));
memcpy(yylval.blobval.data, string_literal_buffer, sdslen(string_literal_buffer));
yylval.blobval.len = sdslen(string_literal_buffer);
string_literal_buffer[0] = '\0';
sdsupdatelen(string_literal_buffer);
yylval.blobval.data = malloc(string_litral_buffer_size);
memcpy(yylval.blobval.data, string_literal_buffer, string_litral_buffer_size);
yylval.blobval.len = string_litral_buffer_size;
string_litral_buffer_size = 0;
return ARRAY_LITERAL;
}
\\n { string_literal_buffer_append_char('\n'); }
\\a { string_literal_buffer_append_char('\a'); }
\\b { string_literal_buffer_append_char('\b'); }
\\f { string_literal_buffer_append_char('\f'); }
\\r { string_literal_buffer_append_char('\r'); }
\\t { string_literal_buffer_append_char('\t'); }
\\v { string_literal_buffer_append_char('\v'); }
\\0 { string_literal_buffer_append_char('\0'); }
\\\" { string_literal_buffer_append_char('\"'); }
\\\\ { string_literal_buffer_append_char('\\'); }
\\n { string_literal_buffer = sdscat(string_literal_buffer, "\n");}
\\a { string_literal_buffer = sdscat(string_literal_buffer, "\a"); }
\\b { string_literal_buffer = sdscat(string_literal_buffer, "\b"); }
\\f { string_literal_buffer = sdscat(string_literal_buffer, "\f"); }
\\r { string_literal_buffer = sdscat(string_literal_buffer, "\r"); }
\\t { string_literal_buffer = sdscat(string_literal_buffer, "\t"); }
\\v { string_literal_buffer = sdscat(string_literal_buffer, "\v"); }
\\\" { string_literal_buffer = sdscat(string_literal_buffer, "\""); }
\\\\ { string_literal_buffer = sdscat(string_literal_buffer, "\\"); }
\\0 { string_literal_buffer = sdscatlen(string_literal_buffer, "\0", 1); }
\\x({hex}|{uhex})+ {
long v = strtol(yytext + 2, NULL, 16);
string_literal_ensure_surplus(sizeof(long));
memcpy(string_literal_buffer + string_litral_buffer_size, &v, sizeof(long));
string_litral_buffer_size += sizeof(long);
if (v > 255
|| v < -255) {
issue_warning("hex escapes are char sized. '%ld' will be truncated", v);
}
string_literal_buffer = sdscatlen(string_literal_buffer, &v, 1);
}
\n {
issue_error("string was never terminated");
yylval.blobval.data = strdup("");
yylval.blobval.len = 0;
string_litral_buffer_size = 0;
string_literal_buffer[0] = '\0';
sdsupdatelen(string_literal_buffer);
return ARRAY_LITERAL;
}
. { string_literal_buffer_append_char(yytext[0]); }
. { string_literal_buffer = sdscat(string_literal_buffer, yytext); }
}
<IN_COMMENT>{
@ -307,5 +302,5 @@ void yyfree_leftovers(void) {
yylex_destroy();
free(string_literal_buffer);
sdsfree(string_literal_buffer);
}

View File

@ -9,6 +9,7 @@
#include "assembler.h"
#include "compile.h"
#include "eaxhla.h"
#include "debug.h"
extern void yyfree_leftovers(void);
extern char * yyfilename;
@ -291,14 +292,16 @@ machine: MACHINE machine_code END_MACHINE
;
machine_code: %empty
| LITERAL machine_code { append_instructions(ASMDIRIMM, D8, 1, $1); }
| ARRAY_LITERAL machine_code {
append_instructions(ASMDIRIMM, D8, $1.len);
for (unsigned long long i = 0; i < $1.len; i++) {
append_instructions((int)*((char*)$1.data + i));
| machine_code LITERAL {
append_instructions(ASMDIRIMM, D8, 1, $2);
}
| machine_code ARRAY_LITERAL {
append_instructions(ASMDIRIMM, D8, $2.len);
for (unsigned long long i = 0; i < $2.len; i++) {
append_instructions(((char*)$2.data)[i]);
}
}
| IDENTIFIER machine_code { free($1); }
| machine_code IDENTIFIER { free($2); }
;
call: FASTCALL IDENTIFIER arguments {

View File

@ -1,9 +1,12 @@
program mymachine
begin
machine
0xB8 0x3C 0x00 0x00
0x00 0xBF 0x45 0x00
0x00 0x00 0x0F 0x05
//"\xb8" /*D8*/ 60 /*D32*/ "\xbf" /*D8*/ 69 /*D32*/ "\x0f\x05"
# 0xB8 0x3C 0x00 0x00
# 0x00 0xBF 0x45 0x00
# 0x00 0x00 0x0F 0x05
#"\xB8\x3C\x00\x00\x00\xBF\x45\x00\x00\x00\x0F\x05"
"\xB8\x3C\x00\x00"
"\x00\xBF\x45\x00"
"\x00\x00\x0F\x05"
end machine
end program