add macros and preprocessor

This commit is contained in:
sfja 2026-11-06 01:50:14 +01:00
parent 58a486334c
commit dd8f47eb76
7 changed files with 724 additions and 113 deletions

View File

@ -13,7 +13,7 @@ add_subdirectory(boot)
add_custom_target(
run
DEPENDS emu boot/boot.bin
DEPENDS emu build-boot.bin
COMMAND emu boot/boot.bin
VERBATIM
USES_TERMINAL

View File

@ -1,5 +1,4 @@
add_custom_command(
OUTPUT boot.bin
DEPENDS asm ${PROJECT_SOURCE_DIR}/boot/boot.vc6asm
@ -8,7 +7,7 @@ add_custom_command(
)
add_custom_target(
boot.bin
build-boot.bin
ALL
DEPENDS boot.bin
)

23
boot/arch.vc6asm Normal file
View File

@ -0,0 +1,23 @@
@guard ARCH_VC6ASM
@const FL_EQ 0x1
@const FL_LT 0x2
@const FL_BE 0x2
@const FL_CARRY 0x2
@const KBD_STATUS 0xff00
@const KBD_SCANCODE 0xff01
@const KBD_KEYDOWN_HANDLER 0xff02
@const KBD_KEYUP_HANDLER 0xff03
@const VCD_BEGIN 0xfb00
@const DISK_STATUS 0xff10
@const DISK_CONTROL 0xff11
@const DISK_BLOCK_ID 0xff12
@const DISK_ADDR 0xff13
@const DISK_CONTROL_DIRECTION 1 << 0
@const DISK_CONTROL_START 1 << 1
@end

View File

@ -1,22 +1,5 @@
@const FL_EQ 0x1
@const FL_LT 0x2
@const FL_BE 0x2
@const FL_CARRY 0x2
@const KBD_STATUS 0xff00
@const KBD_SCANCODE 0xff01
@const KBD_KEYDOWN_HANDLER 0xff02
@const KBD_KEYUP_HANDLER 0xff03
@const VCD_BEGIN 0xfb00
@const DISK_STATUS 0xff10
@const DISK_CONTROL 0xff11
@const DISK_BLOCK_ID 0xff12
@const DISK_ADDR 0xff13
@const DISK_CONTROL_DIRECTION 1 << 0
@const DISK_CONTROL_START 1 << 1
@include "arch.vc6asm"
@include "macros.vc6asm"
start:
mov r1, 'V'
@ -35,8 +18,9 @@ start:
store [DISK_ADDR], r0
mov r2, DISK_CONTROL_START
store [DISK_CONTROL], r2
xor fl, fl
adc r0, 512
; xor fl, fl
; adc r0, 512
add r0, 512
xor fl, fl
adc r1, 1
.l0_cond:
@ -44,7 +28,7 @@ start:
xor fl, fl
adc r2, -15
xor fl, fl
cmp r1, 4
cmp r1, (BOOT_SIZE / 512) + 1
and fl, FL_LT
cmov ip, r2
@ -67,7 +51,5 @@ keydown_handler:
xor fl, fl
adc ip, -4
@const BOOT_SIZE $

12
boot/macros.vc6asm Normal file
View File

@ -0,0 +1,12 @@
@guard MACROS_VC6ASM
@include "arch.vc6asm"
@macro add 2
and fl, !FL_CARRY
adc %0, %1
@end
@end

750
src/asm.c
View File

@ -1,5 +1,6 @@
#include "isa.h"
#include <assert.h>
#include <libgen.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
@ -134,6 +135,12 @@ void map_destroy(Map *m)
free(m->entries);
}
void map_clear(Map *m)
{
map_destroy(m);
map_init(m, m->size_of_val);
}
void map_set(Map *m, const char *key, const void *val)
{
if (!m->entries_size) {
@ -190,14 +197,53 @@ void print_error(Loc loc, const char *format, ...)
{
va_list va;
va_start(va, format);
fprintf(stderr, "error: ");
fprintf(stderr, "\033[1;31merror\033[1;39m: ");
vfprintf(stderr, format, va);
fprintf(stderr, "\n at %s:%d:%d:\n", loc.filename, loc.line, loc.col);
fprintf(stderr,
"\n \033[0;37m--> %s:%d:%d\n\033[0m",
loc.filename,
loc.line,
loc.col);
va_end(va);
}
void print_info(Loc loc, const char *format, ...)
{
va_list va;
va_start(va, format);
fprintf(stderr, "\033[1;34minfo\033[1;39m: ");
vfprintf(stderr, format, va);
fprintf(stderr,
"\n \033[0;37m--> %s:%d:%d\n\033[0m",
loc.filename,
loc.line,
loc.col);
va_end(va);
}
#define print_error_exit(LOC, ...) (print_error((LOC), __VA_ARGS__), exit(1))
char *read_text_file(const char *filename)
{
FILE *file = fopen(filename, "r");
assert(file);
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
assert(file_size > 0);
size_t text_size = file_size + 1;
char *text = calloc(text_size, sizeof(char));
fseek(file, 0, SEEK_SET);
size_t bytes_read = fread(text, sizeof(char), file_size, file);
assert(bytes_read == (size_t)file_size);
fclose(file);
return text;
}
typedef enum {
Tok_Eof,
Tok_Eol,
@ -206,6 +252,7 @@ typedef enum {
Tok_Hex,
Tok_Bin,
Tok_Char,
Tok_Str,
Tok_LtLt,
Tok_GtGt,
Tok_R0,
@ -217,6 +264,11 @@ typedef enum {
Tok_Fl,
Tok_Ip,
Tok_Const,
Tok_Macro,
Tok_End,
Tok_Include,
Tok_Guard,
Tok_MacroArg,
Tok_Exclam = '!',
Tok_Dollar = '$',
Tok_Percent = '%',
@ -237,6 +289,57 @@ typedef enum {
Tok_Pipe = '|',
} TokTy;
const char *tokty_str(TokTy ty)
{
switch (ty) {
/* clang-format off */
case Tok_Eof: return "Eof";
case Tok_Eol: return "Eol";
case Tok_Ident: return "Ident";
case Tok_Int: return "Int";
case Tok_Hex: return "Hex";
case Tok_Bin: return "Bin";
case Tok_Char: return "Char";
case Tok_Str: return "Str";
case Tok_LtLt: return "LtLt";
case Tok_GtGt: return "GtGt";
case Tok_R0: return "R0";
case Tok_R1: return "R1";
case Tok_R2: return "R2";
case Tok_R3: return "R3";
case Tok_Sp: return "Sp";
case Tok_Bp: return "Bp";
case Tok_Fl: return "Fl";
case Tok_Ip: return "Ip";
case Tok_Const: return "Const";
case Tok_Macro: return "Macro";
case Tok_End: return "End";
case Tok_Include: return "Include";
case Tok_Guard: return "Guard";
case Tok_MacroArg: return "MacroArg";
case Tok_Exclam: return "Exclam";
case Tok_Dollar: return "Dollar";
case Tok_Percent: return "Percent";
case Tok_Ampersand: return "Ampersand";
case Tok_LParen: return "LParen";
case Tok_RParen: return "RParen";
case Tok_Asterisk: return "Asterisk";
case Tok_Plus: return "Plus";
case Tok_Comma: return "Comma";
case Tok_Minus: return "Minus";
case Tok_Dot: return "Dot";
case Tok_Slash: return "Slash";
case Tok_Colon: return "Colon";
case Tok_At: return "At";
case Tok_LBracket: return "LBracket";
case Tok_RBracket: return "RBracket";
case Tok_Caret: return "Caret";
case Tok_Pipe: return "Pipe";
/* clang-format on */
}
assert(false);
}
typedef struct {
Loc loc;
size_t len;
@ -280,6 +383,10 @@ static TokTy l_ident_ty(const char *text, size_t len)
"fl",
"ip",
"const",
"macro",
"end",
"include",
"guard",
};
TokTy tys[] = {
Tok_R0,
@ -291,6 +398,10 @@ static TokTy l_ident_ty(const char *text, size_t len)
Tok_Fl,
Tok_Ip,
Tok_Const,
Tok_Macro,
Tok_End,
Tok_Include,
Tok_Guard,
};
constexpr size_t kws_size = sizeof(kws) / sizeof(kws[0]);
@ -367,6 +478,19 @@ Tok lexer_next(Lexer *l)
l_step(l);
return l_tok(l, loc, Tok_Char);
}
if (l->ch == '"') {
l_step(l);
while (l->ch && l->ch != '"') {
if (l->ch == '\\') {
l_step(l);
assert(l->ch);
}
l_step(l);
}
assert(l->ch == '"');
l_step(l);
return l_tok(l, loc, Tok_Str);
}
if (l->ch == '<') {
l_step(l);
assert(l->ch == '<');
@ -379,7 +503,15 @@ Tok lexer_next(Lexer *l)
l_step(l);
return l_tok(l, loc, Tok_GtGt);
}
if (strchr(".,()[]+-*/%|&^@!:$", l->ch)) {
if (l->ch == '%') {
l_step(l);
if (l->ch >= '0' && l->ch <= '9') {
l_step(l);
return l_tok(l, loc, Tok_MacroArg);
}
return l_tok(l, loc, '%');
}
if (strchr(".,()[]+-*/|&^@!:$", l->ch)) {
TokTy ty = l->ch;
l_step(l);
return l_tok(l, loc, ty);
@ -392,22 +524,269 @@ char *lexer_strdup(Lexer *l, Tok tok)
return strndup(&l->text[tok.loc.idx], tok.len);
}
static size_t unescape_char(const char *text, char *ch)
{
*ch = text[0];
if (!*ch)
return 0;
if (*ch != '\\')
return 1;
*ch = text[1];
if (!*ch)
return 0;
switch (*ch) {
case '0':
*ch = '\0';
break;
case 'n':
*ch = '\n';
break;
case 'r':
*ch = '\r';
break;
case 't':
*ch = '\t';
break;
}
return 2;
}
static char *unescape_strndup(const char *text, size_t text_len)
{
size_t len = 0;
for (size_t i = 0; i < text_len;) {
char ch;
size_t res = unescape_char(&text[i], &ch);
if (!res)
return nullptr;
i += res;
len += 1;
}
char *out = calloc(len + 1, sizeof(char));
size_t out_i = 0;
for (size_t i = 0; i < len;) {
char ch;
size_t res = unescape_char(&text[i], &ch);
assert(res);
out[out_i] = ch;
out_i += 1;
i += res;
}
return out;
}
typedef struct {
Loc loc;
int64_t val;
} Sym;
typedef struct {
char *filename;
char *text;
Lexer lexer;
int prev_directive_depth;
} IncludeExpansion;
typedef struct {
Loc loc;
char *ident;
ARRAY_FIELD(Tok, toks);
char *text;
size_t text_offset;
int arg_count;
} Macro;
typedef struct {
Loc loc;
ARRAY_FIELD(Tok, toks);
} MacroExpanArg;
typedef struct {
Loc loc;
Macro *macro;
ARRAY_FIELD(MacroExpanArg, args);
} MacroExpansion;
static void macroexpansion_destroy(MacroExpansion *expan)
{
for (size_t i = 0; i < expan->args_size; ++i)
if (expan->args[i].toks)
free(expan->args[i].toks);
if (expan->args)
free(expan->args);
}
typedef struct {
Lexer *lexer;
Tok tok;
uint16_t *output;
size_t output_size;
size_t ip;
Map globals;
Map locals;
Map syms;
int labels_counter;
int current_label;
ARRAY_FIELD(IncludeExpansion, incl_expans);
ARRAY_FIELD(Macro, macros);
Map macro_map;
ARRAY_FIELD(MacroExpansion, macro_expans);
ARRAY_FIELD(Tok, tok_stack);
Map guard_set;
int directive_depth;
bool second_pass;
} Assembler;
void assembler_init(Assembler *a, Lexer *lexer)
{
size_t output_size = 65535;
*a = (Assembler) {
.lexer = lexer,
.tok = lexer_next(lexer),
.output = calloc(output_size, sizeof(uint16_t)),
.output_size = output_size,
.ip = 0,
.syms = { },
.labels_counter = 0,
.current_label = 0,
ARRAY_FIELD_INIT(incl_expans),
ARRAY_FIELD_INIT(macros),
.macro_map = { },
ARRAY_FIELD_INIT(macro_expans),
ARRAY_FIELD_INIT(tok_stack),
.guard_set = { },
.directive_depth = 0,
.second_pass = false,
};
map_init(&a->syms, sizeof(Sym));
map_init(&a->macro_map, sizeof(Macro *));
map_init(&a->guard_set, sizeof(int));
}
void assembler_destroy(Assembler *a)
{
free(a->output);
map_destroy(&a->syms);
for (size_t i = 0; i < a->incl_expans_size; ++i) {
free(a->incl_expans[i].filename);
free(a->incl_expans[i].text);
}
if (a->incl_expans)
free(a->incl_expans);
for (size_t i = 0; i < a->macros_size; ++i) {
free(a->macros[i].ident);
if (a->macros[i].toks)
free(a->macros[i].toks);
free(a->macros[i].text);
}
if (a->macros)
free(a->macros);
map_destroy(&a->macro_map);
for (size_t i = 0; i < a->macro_expans_size; ++i)
macroexpansion_destroy(&a->macro_expans[i]);
if (a->macro_expans)
free(a->macro_expans);
if (a->tok_stack)
free(a->tok_stack);
map_destroy(&a->guard_set);
}
static Lexer *a_lexer(Assembler *a)
{
if (a->incl_expans_size > 0)
return &a->incl_expans[a->incl_expans_size - 1].lexer;
return a->lexer;
}
static const char *a_tok_text(Assembler *a, Tok tok)
{
if (a->macro_expans_size > 0) {
Macro *macro = a->macro_expans[a->macro_expans_size - 1].macro;
return &macro->text[tok.loc.idx - macro->text_offset];
}
return &a_lexer(a)->text[tok.loc.idx];
}
static char *a_tok_strdup(Assembler *a, Tok tok)
{
return strndup(a_tok_text(a, tok), tok.len);
}
[[maybe_unused]]
static void a_print_tok(Assembler *a, Tok tok)
{
printf("%3d %-10s '%.*s'\n",
tok.loc.line,
tokty_str(tok.ty),
(int)tok.len,
a_tok_text(a, tok));
}
static void a_step_without_expand(Assembler *a)
{
while (a->tok_stack_size > 0) {
a->tok = a->tok_stack[a->tok_stack_size - 1];
a->tok_stack_size -= 1;
if (a->tok.ty != Tok_Eof)
return;
assert(a->macro_expans_size > 0);
macroexpansion_destroy(&a->macro_expans[a->macro_expans_size - 1]);
a->macro_expans_size -= 1;
}
assert(a->macro_expans_size == 0);
while (a->incl_expans_size > 0) {
IncludeExpansion *expan = &a->incl_expans[a->incl_expans_size - 1];
a->tok = lexer_next(&expan->lexer);
if (a->tok.ty != Tok_Eof)
return;
if (a->directive_depth != 0)
print_error_exit(a->tok.loc, "expected '@end'");
free(expan->filename);
free(expan->text);
a->directive_depth = expan->prev_directive_depth;
a->incl_expans_size -= 1;
}
a->tok = lexer_next(a->lexer);
}
static void a_step(Assembler *a)
{
a->tok = lexer_next(a->lexer);
a_step_without_expand(a);
if (a->tok.ty == Tok_MacroArg) {
if (a->macro_expans_size == 0)
print_error_exit(a->tok.loc, "not in macro");
MacroExpansion *expan = &a->macro_expans[a->macro_expans_size - 1];
size_t id = strtoull(a_tok_text(a, a->tok), nullptr, 10);
if (id >= expan->args->toks_size)
print_error_exit(a->tok.loc,
"insufficient arguments (takes %lu)",
expan->args->toks_size);
MacroExpanArg *arg = &expan->args[id];
assert(arg->toks_size > 0);
for (size_t i = 0; i < arg->toks_size; ++i) {
Tok tok = arg->toks[arg->toks_size - i - 1];
tok.loc.filename = arg->loc.filename;
tok.loc.line = arg->loc.line;
tok.loc.col = arg->loc.col;
ARRAY_FIELD_PUSH(a, tok_stack, &tok);
}
assert(a->tok_stack_size > 0);
a->tok = a->tok_stack[a->tok_stack_size - 1];
a->tok_stack_size -= 1;
}
// a_print_tok(a, a->tok);
}
static int64_t a_parse_binary(Assembler *a, int prec);
@ -500,49 +879,34 @@ static int64_t a_parse_operand(Assembler *a)
if (!a->second_pass)
return 0;
char *ident = lexer_strdup(a->lexer, ident_tok);
int64_t *v = map_get(&a->globals, ident);
char *ident = a_tok_strdup(a, ident_tok);
Sym *v = map_get(&a->syms, ident);
if (!v)
print_error_exit(ident_tok.loc, "undefined identifier '%s'", ident);
free(ident);
return *v;
return v->val;
}
if (a->tok.ty == Tok_Int) {
int64_t v = strtoll(&a->lexer->text[a->tok.loc.idx], nullptr, 10);
int64_t v = strtoll(a_tok_text(a, a->tok), nullptr, 10);
a_step(a);
return v;
}
if (a->tok.ty == Tok_Hex) {
int64_t v = strtoll(&a->lexer->text[a->tok.loc.idx + 2], nullptr, 16);
int64_t v = strtoll(a_tok_text(a, a->tok) + 2, nullptr, 16);
a_step(a);
return v;
}
if (a->tok.ty == Tok_Bin) {
int64_t v = strtoll(&a->lexer->text[a->tok.loc.idx + 2], nullptr, 2);
int64_t v = strtoll(a_tok_text(a, a->tok) + 2, nullptr, 2);
a_step(a);
return v;
}
if (a->tok.ty == Tok_Char) {
int64_t v = a->lexer->text[a->tok.loc.idx + 1];
char ch;
size_t res = unescape_char(a_tok_text(a, a->tok) + 1, &ch);
assert(res != 0);
a_step(a);
if (v == '\\') {
v = a->lexer->text[a->tok.loc.idx + 2];
switch (v) {
case '0':
v = '\0';
break;
case 'n':
v = '\n';
break;
case 'r':
v = '\r';
break;
case 't':
v = '\t';
break;
}
}
return v;
return (int64_t)ch;
}
if (a->tok.ty == '$') {
a_step(a);
@ -571,23 +935,211 @@ static void a_assert_eol(Assembler *a)
print_error_exit(a->tok.loc, "expected end-of-line");
}
static void a_sym_define(Assembler *a, const char *ident, Loc loc, int64_t val)
{
Sym *found = map_get(&a->syms, ident);
if (!a->second_pass && found) {
print_error(loc, "redefinitions of '%s'", ident);
print_info(found->loc, "'%s' defined here", ident);
exit(1);
}
Sym sym = { loc, val };
map_set(&a->syms, ident, &sym);
}
static void a_parse_directive_const(Assembler *a)
{
a_step(a);
if (a->tok.ty != Tok_Ident)
print_error_exit(a->tok.loc, "expected identifier");
Tok ident_tok = a->tok;
a_step(a);
int64_t val = a_parse_expr(a);
a_assert_eol(a);
char *ident = a_tok_strdup(a, ident_tok);
a_sym_define(a, ident, ident_tok.loc, val);
free(ident);
}
static void a_parse_directive_macro(Assembler *a)
{
a_step(a);
if (a->tok.ty != Tok_Ident)
print_error_exit(a->tok.loc, "expected identifier");
Tok ident_tok = a->tok;
a_step(a);
if (a->tok.ty != Tok_Int)
print_error_exit(a->tok.loc, "expected argument count");
int64_t arg_count = a_parse_operand(a);
a_assert_eol(a);
char *ident = a_tok_strdup(a, ident_tok);
Macro macro = {
.loc = ident_tok.loc,
.ident = ident,
ARRAY_FIELD_INIT(toks),
.text = nullptr,
.text_offset = 0,
.arg_count = arg_count,
};
size_t begin_idx = a->tok.loc.idx;
Tok last = a->tok;
a_step(a);
int depth = 1;
while (a->tok.ty != Tok_Eof) {
if (last.ty == '@' && a->tok.ty == Tok_End)
depth -= 1;
if (last.ty == '@'
&& (a->tok.ty == Tok_Macro || a->tok.ty == Tok_Guard))
depth += 1;
if (depth == 0)
break;
ARRAY_FIELD_PUSH(&macro, toks, &last);
last = a->tok;
a_step_without_expand(a);
}
if (a->tok.ty == Tok_Eof)
print_error_exit(a->tok.loc, "expected '@end'");
a_step(a);
a_assert_eol(a);
size_t end_idx = a->tok.loc.idx;
if (a->second_pass) {
free(ident);
if (macro.toks)
free(macro.toks);
return;
}
size_t text_len = end_idx - begin_idx;
char *text = calloc(text_len + 1, sizeof(char));
strncpy(text, &a_lexer(a)->text[begin_idx], text_len);
macro.text = text;
macro.text_offset = begin_idx;
ARRAY_FIELD_PUSH(a, macros, &macro);
Macro *macro_ptr = &a->macros[a->macros_size - 1];
Macro **found;
if (!a->second_pass && (found = map_get(&a->macro_map, ident))) {
print_error(ident_tok.loc, "redefinitions of macro '%s'", ident);
print_info((*found)->loc, "macro '%s' defined here", ident);
exit(1);
}
map_set(&a->macro_map, ident, &macro_ptr);
}
static void a_parse_directive_include(Assembler *a)
{
a_step(a);
if (a->tok.ty != Tok_Str)
print_error_exit(a->tok.loc, "expected string");
Tok str_tok = a->tok;
a_step(a);
a_assert_eol(a);
char *str_val
= unescape_strndup(a_tok_text(a, str_tok) + 1, str_tok.len - 2);
char *current_filename = strdup(a_lexer(a)->loc.filename);
char *current_dir = dirname(current_filename);
size_t included_filename_len = strlen(current_dir) + 1 + strlen(str_val);
char *filename = calloc(included_filename_len, sizeof(char));
snprintf(
filename, included_filename_len + 1, "%s/%s", current_dir, str_val);
free(str_val);
free(current_filename);
char *text = read_text_file(filename);
IncludeExpansion expan = {
.filename = filename,
.text = text,
.lexer = {
.text = text,
.loc = {filename, 0, 1, 1},
.ch = text[0],
},
.prev_directive_depth = a->directive_depth,
};
ARRAY_FIELD_PUSH(a, incl_expans, &expan);
a->directive_depth = 0;
// Step into included file.
a_step(a);
}
static void a_skip_until_directive_end(Assembler *a)
{
Tok last = a->tok;
a_step(a);
int start_depth = a->directive_depth;
while (a->tok.ty != Tok_Eof) {
if (last.ty == '@' && (a->tok.ty == Tok_End))
a->directive_depth -= 1;
else if (last.ty == '@'
&& (a->tok.ty == Tok_Macro || a->tok.ty == Tok_Guard))
a->directive_depth += 1;
if (a->directive_depth < start_depth)
break;
last = a->tok;
a_step(a);
}
if (a->tok.ty == Tok_Eof)
print_error_exit(a->tok.loc, "expected '@end'");
}
static void a_parse_directive_guard(Assembler *a)
{
a_step(a);
if (a->tok.ty != Tok_Ident)
print_error_exit(a->tok.loc, "expected identifier");
Tok ident_tok = a->tok;
a_step(a);
a_assert_eol(a);
a->directive_depth += 1;
char *ident = a_tok_strdup(a, ident_tok);
if (map_get(&a->guard_set, ident)) {
a_skip_until_directive_end(a);
a_step(a);
a_assert_eol(a);
} else {
map_set(&a->guard_set, ident, &(int[]) { 0 });
}
free(ident);
}
static void a_parse_directive(Assembler *a)
{
assert(a->tok.ty == '@');
a_step(a);
if (a->tok.ty == Tok_Const) {
a_parse_directive_const(a);
} else if (a->tok.ty == Tok_Macro) {
a_parse_directive_macro(a);
} else if (a->tok.ty == Tok_Include) {
a_parse_directive_include(a);
} else if (a->tok.ty == Tok_End) {
if (a->directive_depth == 0)
print_error_exit(a->tok.loc, "unexpected '@end'");
a->directive_depth -= 1;
a_step(a);
if (a->tok.ty != Tok_Ident)
print_error_exit(a->tok.loc, "expected identifier");
char *ident = lexer_strdup(a->lexer, a->tok);
a_step(a);
int64_t val = a_parse_expr(a);
a_assert_eol(a);
map_set(&a->globals, ident, &val);
free(ident);
return;
} else if (a->tok.ty == Tok_Guard) {
a_parse_directive_guard(a);
} else {
print_error_exit(a->tok.loc, "malformed directive");
}
print_error_exit(a->tok.loc, "malformed directive");
}
static uint16_t tokty_reg(TokTy ty)
@ -682,9 +1234,58 @@ static void a_parse_ins_args(Assembler *a, Op *op)
a_assert_eol(a);
}
static void a_parse_macro_expan(Assembler *a, Macro *macro, Loc loc)
{
MacroExpansion expan = {
.loc = loc,
.macro = macro,
ARRAY_FIELD_INIT(args),
};
bool first = true;
while (first ? a->tok.ty != Tok_Eol : a->tok.ty == ',') {
if (!first) {
assert(a->tok.ty == ',');
a_step(a);
}
first = false;
if (a->tok.ty == Tok_Eol || a->tok.ty == ',')
print_error_exit(a->tok.loc, "expected argument");
MacroExpanArg arg = {
.loc = a->tok.loc,
ARRAY_FIELD_INIT(toks),
};
while (
a->tok.ty != Tok_Eof && a->tok.ty != Tok_Eol && a->tok.ty != ',') {
ARRAY_FIELD_PUSH(&arg, toks, &a->tok);
a_step(a);
}
ARRAY_FIELD_PUSH(&expan, args, &arg);
}
a_assert_eol(a);
ARRAY_FIELD_PUSH(a, macro_expans, &expan);
ARRAY_FIELD_PUSH(a, tok_stack, &(Tok) { .ty = Tok_Eof });
for (size_t i = 0; i < macro->toks_size; ++i) {
Tok tok = macro->toks[macro->toks_size - i - 1];
tok.loc.filename = expan.loc.filename;
tok.loc.line = expan.loc.line;
tok.loc.col = expan.loc.col;
ARRAY_FIELD_PUSH(a, tok_stack, &tok);
}
// Step into macro expansion.
a_step(a);
}
static void a_parse_ins(Assembler *a, Tok ident_tok)
{
char *ident = lexer_strdup(a->lexer, ident_tok);
char *ident = a_tok_strdup(a, ident_tok);
Op op = { };
@ -693,6 +1294,13 @@ static void a_parse_ins(Assembler *a, Tok ident_tok)
if (strcmp(ident, opcode_strs[i]) == 0)
opcode = i;
Macro **macro = map_get(&a->macro_map, ident);
if (macro) {
free(ident);
a_parse_macro_expan(a, *macro, ident_tok.loc);
return;
}
if (opcode == Opcode_size)
print_error_exit(ident_tok.loc, "unknown operation '%s'", ident);
@ -701,8 +1309,8 @@ static void a_parse_ins(Assembler *a, Tok ident_tok)
op.ty = opcode;
a_parse_ins_args(a, &op);
// if (a->second_pass)
// op_print_line(&op, a->ip);
if (a->second_pass)
op_print_line(&op, a->ip);
a->ip += op_encode(a->output, a->output_size, a->ip, &op);
}
@ -718,8 +1326,8 @@ static void a_parse_label_or_ins(Assembler *a)
if (a->tok.ty == ':') {
a_step(a);
char *ident = lexer_strdup(a->lexer, ident_tok);
map_set(&a->globals, ident, &a->ip);
char *ident = a_tok_strdup(a, ident_tok);
a_sym_define(a, ident, ident_tok.loc, a->ip);
free(ident);
a->labels_counter += 1;
@ -745,13 +1353,13 @@ static void a_parse_local_label(Assembler *a)
print_error_exit(a->tok.loc, "expected ':'");
a_step(a);
char *ident = lexer_strdup(a->lexer, ident_tok);
char *ident = a_tok_strdup(a, ident_tok);
size_t unique_ident_size = strlen(ident) + 16;
char *unique_ident = calloc(unique_ident_size, 1);
snprintf(
unique_ident, unique_ident_size - 1, "%d.%s", a->current_label, ident);
map_set(&a->locals, ident, &a->ip);
a_sym_define(a, ident, ident_tok.loc, a->ip);
free(ident);
assembler_parse_line(a);
@ -780,7 +1388,7 @@ void assembler_parse_line(Assembler *a)
a->tok.ty,
a->tok.ty,
a->tok.len,
&a->lexer->text[a->tok.loc.idx]);
a_tok_text(a, a->tok));
}
bool assembler_done(Assembler *a)
@ -806,18 +1414,7 @@ int main(int argc, char *argv[])
assert(input_filename);
assert(output_filename);
FILE *input_file = fopen(input_filename, "r");
assert(input_file);
fseek(input_file, 0, SEEK_END);
long input_file_size = ftell(input_file);
assert(input_file_size > 0);
size_t input_text_size = input_file_size + 1;
char *input_text = calloc(input_text_size, 1);
fseek(input_file, 0, SEEK_SET);
size_t bytes_read = fread(input_text, 1, input_file_size, input_file);
assert(bytes_read == (size_t)input_file_size);
fclose(input_file);
char *input_text = read_text_file(input_filename);
Lexer lexer = {
.text = input_text,
@ -825,27 +1422,15 @@ int main(int argc, char *argv[])
.ch = input_text[0],
};
size_t output_size = 65535;
Assembler assembler = {
.lexer = &lexer,
.tok = lexer_next(&lexer),
.output = calloc(output_size, sizeof(uint16_t)),
.output_size = output_size,
.globals = { },
.locals = { },
.ip = 0,
.labels_counter = 0,
.current_label = 0,
.second_pass = false,
};
map_init(&assembler.globals, sizeof(int64_t));
map_init(&assembler.locals, sizeof(int64_t));
Assembler assembler;
assembler_init(&assembler, &lexer);
while (!assembler_done(&assembler))
assembler_parse_line(&assembler);
if (assembler.directive_depth != 0)
print_error_exit(assembler.tok.loc, "expected '@end'");
lexer.loc.idx = 0;
lexer.loc.line = 1;
lexer.loc.col = 1;
@ -857,21 +1442,20 @@ int main(int argc, char *argv[])
assembler.current_label = 0;
assembler.second_pass = true;
map_clear(&assembler.guard_set);
while (!assembler_done(&assembler))
assembler_parse_line(&assembler);
map_destroy(&assembler.globals);
map_destroy(&assembler.locals);
free(input_text);
FILE *output_file = fopen(output_filename, "w");
assert(output_file);
size_t bytes_written
size_t words_written
= fwrite(assembler.output, sizeof(uint16_t), assembler.ip, output_file);
fclose(output_file);
printf("%lu bytes written.\n", bytes_written);
printf("%lu words written.\n", words_written);
free(assembler.output);
assembler_destroy(&assembler);
}

View File

@ -23,9 +23,16 @@ syn match Operator '*'
syn match Operator '/'
syn match Operator '%'
syn match Operator '!'
syn match Operator '$'
syn match Operator '\$'
syn match PreProc '@const'
syn match PreProc '@macro'
syn match PreProc '@end'
syn match PreProc '@include'
syn match PreProc '@if'
syn match PreProc '@elif'
syn match PreProc '@else'
syn match PreProc '@guard'
syn match Number '0'
syn match Number '[1-9][0-9]*'
@ -38,8 +45,12 @@ syn region String start=+"+ skip=+\\\\\|\\"+ end=+"+
syn match Comment ";.*$"
syn match PreProc '%\d\+'
syn match PreProc '^\s\{4\}[a-zA-Z_]\w*' contains=Keyword
syn match Identifier '[a-zA-Z_]\w*'
syn match Identifier '[a-zA-Z_]\w*\ze:'
syn match Function '^[a-zA-Z_]\w*\ze:'
syn match Identifier '^\.\zs[a-zA-Z_]\w*\ze:'
let b:current_syntax = "vc6asm"