fix macros and labels

This commit is contained in:
sfja 2026-11-06 05:36:17 +01:00
parent dd8f47eb76
commit 425adbe5f6
6 changed files with 251 additions and 91 deletions

View File

@ -1,9 +1,9 @@
@guard ARCH_VC6ASM
@guard ARCH
@const FL_EQ 0x1
@const FL_LT 0x2
@const FL_BE 0x2
@const FL_CARRY 0x2
@const FL_EQ 1 << 0
@const FL_LT 1 << 1
@const FL_BE 1 << 2
@const FL_CARRY 1 << 3
@const KBD_STATUS 0xff00
@const KBD_SCANCODE 0xff01

View File

@ -9,47 +9,111 @@ start:
mov r1, '6'
store [VCD_BEGIN + 2], r1
mov bp, 0x1000
mov sp, bp
mov r1, text
mov r2, VCD_BEGIN + 80
mov r0, ip
add r0, .ret0
push r0
mov ip, println
.ret0:
; mov r1, prompt
; mov r2, VCD_BEGIN + 80
; mov r0, ip
; add r0, .ret1
; push r0
; mov ip, println
; .ret1:
mov r0, 512
mov r1, 1
xor fl, fl
adc ip, 14
add ip, .l0_cond - $ - 2
.l0_body:
store [DISK_BLOCK_ID], r1
store [DISK_ADDR], r0
mov r2, DISK_CONTROL_START
store [DISK_CONTROL], r2
; xor fl, fl
; adc r0, 512
add r0, 512
xor fl, fl
adc r1, 1
add r1, 1
.l0_cond:
mov r2, ip
xor fl, fl
adc r2, -15
xor fl, fl
cmp r1, (BOOT_SIZE / 512) + 1
add r2, .l0_body - $ + 2
clf
cmp r1, BOOT_SIZE / 512 + 2
and fl, FL_LT
cmov ip, r2
mov r0, keydown_handler
store [KBD_KEYDOWN_HANDLER], r0
mov r1, 0
.spin:
hlt
add ip, .spin - $ - 2
println:
push bp
mov bp, sp
sub sp, 2
store [sp], r1
mov r0, sp
add r0, 1
store [r0], r2
mov r1, 0
add ip, .l0_cond - $ - 2
.l0_body:
mov r0, sp
add r0, 1
load r3, [r0]
add r3, r1
load r2, [sp]
add r2, r1
load r2, [r2]
store [r3], r2
add r1, 1
.l0_cond:
load r2, [sp]
add r2, r1
load r2, [r2]
clf
cmp r2, '\0'
not fl
and fl, FL_EQ
mov r3, ip
add r3, .l0_body - $ + 2
clc
cmov ip, r3
.leave:
mov sp, bp
pop bp
pop r0
mov ip, r0
text:
dw "jeg fik println til at virke >\0"
prompt:
dw "> \0"
keydown_handler:
load r0, [KBD_SCANCODE]
xor fl, fl
adc r0, 93
mov r2, VCD_BEGIN + 40
xor fl, fl
adc r2, r1
add r0, 93
mov r2, VCD_BEGIN + 40 * 3 + 2
add r2, r1
store [r2], r0
xor fl, fl
adc r1, 1
add r1, 1
.spin:
hlt
xor fl, fl
adc ip, -4
add ip, .spin - $ - 2
@const BOOT_SIZE $

View File

@ -1,12 +1,34 @@
@guard MACROS_VC6ASM
@guard MACROS
@include "arch.vc6asm"
@macro add 2
@macro clf 0
xor fl, fl
@end
@macro clc 0
and fl, !FL_CARRY
@end
@macro add 2
clc
adc %0, %1
@end
@macro sub 2
clc
sbb %0, %1
@end
@macro pop 1
load %0, [sp]
add sp, 1
@end
;
@macro push 1
sub sp, 1
store [sp], %0
@end
@end

View File

@ -8,7 +8,6 @@ target_compile_options(project_options INTERFACE
$<$<COMPILE_LANG_AND_ID:C,GNU,Clang,AppleClang>:-Wall;-Wextra;-Wpedantic;-Werror=switch>
)
add_library(isa STATIC isa.c)
target_include_directories(isa PUBLIC .)
@ -18,3 +17,6 @@ target_link_libraries(emu PRIVATE project_options isa SDL3::SDL3)
add_executable(asm asm.c)
target_link_libraries(asm PRIVATE project_options isa)
target_compile_options(asm PRIVATE -fsanitize=address)
target_link_options(asm PRIVATE -fsanitize=address)

184
src/asm.c
View File

@ -344,6 +344,7 @@ typedef struct {
Loc loc;
size_t len;
TokTy ty;
const char *text;
} Tok;
typedef struct {
@ -354,7 +355,7 @@ typedef struct {
static Tok l_tok(Lexer *l, Loc loc, TokTy ty)
{
return (Tok) { loc, l->loc.idx - loc.idx, ty };
return (Tok) { loc, l->loc.idx - loc.idx, ty, &l->text[loc.idx] };
}
static void l_step(Lexer *l)
@ -596,7 +597,6 @@ typedef struct {
char *ident;
ARRAY_FIELD(Tok, toks);
char *text;
size_t text_offset;
int arg_count;
} Macro;
@ -607,7 +607,7 @@ typedef struct {
typedef struct {
Loc loc;
Macro *macro;
size_t macro_idx;
ARRAY_FIELD(MacroExpanArg, args);
} MacroExpansion;
@ -663,7 +663,7 @@ void assembler_init(Assembler *a, Lexer *lexer)
};
map_init(&a->syms, sizeof(Sym));
map_init(&a->macro_map, sizeof(Macro *));
map_init(&a->macro_map, sizeof(size_t));
map_init(&a->guard_set, sizeof(int));
}
@ -702,20 +702,6 @@ static Lexer *a_lexer(Assembler *a)
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)
{
@ -723,18 +709,20 @@ static void a_print_tok(Assembler *a, Tok tok)
tok.loc.line,
tokty_str(tok.ty),
(int)tok.len,
a_tok_text(a, tok));
tok.text);
}
static void a_step_without_expand(Assembler *a)
{
while (a->tok_stack_size > 0) {
assert(a->macro_expans_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;
}
@ -742,7 +730,9 @@ static void a_step_without_expand(Assembler *a)
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;
@ -761,17 +751,18 @@ static void a_step_without_expand(Assembler *a)
static void a_step(Assembler *a)
{
a_step_without_expand(a);
// a_print_tok(a, a->tok);
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)
size_t id = strtoull(a->tok.text + 1, nullptr, 10);
if (id >= expan->args_size)
print_error_exit(a->tok.loc,
"insufficient arguments (takes %lu)",
expan->args->toks_size);
expan->args_size);
MacroExpanArg *arg = &expan->args[id];
assert(arg->toks_size > 0);
@ -783,9 +774,11 @@ static void a_step(Assembler *a)
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;
}
// printf(" - ");
// a_print_tok(a, a->tok);
}
@ -850,7 +843,7 @@ static int64_t a_parse_binary(Assembler *a, int prec)
TokTy ty = op->ty;
a_step(a);
int64_t r = a_parse_binary(a, prec + 1);
int64_t r = a_parse_binary(a, op->prec + 1);
l = tokty_op(ty, l, r);
}
return l;
@ -879,31 +872,57 @@ static int64_t a_parse_operand(Assembler *a)
if (!a->second_pass)
return 0;
char *ident = a_tok_strdup(a, ident_tok);
char *ident = strndup(ident_tok.text, ident_tok.len);
Sym *v = map_get(&a->syms, ident);
if (!v)
print_error_exit(ident_tok.loc, "undefined identifier '%s'", ident);
free(ident);
return v->val;
}
if (a->tok.ty == '.') {
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->second_pass)
return 0;
char *ident = strndup(ident_tok.text, ident_tok.len);
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);
Sym *v = map_get(&a->syms, unique_ident);
if (!v)
print_error_exit(ident_tok.loc, "undefined identifier '%s'", ident);
free(ident);
free(unique_ident);
return v->val;
}
if (a->tok.ty == Tok_Int) {
int64_t v = strtoll(a_tok_text(a, a->tok), nullptr, 10);
int64_t v = strtoll(a->tok.text, nullptr, 10);
a_step(a);
return v;
}
if (a->tok.ty == Tok_Hex) {
int64_t v = strtoll(a_tok_text(a, a->tok) + 2, nullptr, 16);
int64_t v = strtoll(a->tok.text + 2, nullptr, 16);
a_step(a);
return v;
}
if (a->tok.ty == Tok_Bin) {
int64_t v = strtoll(a_tok_text(a, a->tok) + 2, nullptr, 2);
int64_t v = strtoll(a->tok.text + 2, nullptr, 2);
a_step(a);
return v;
}
if (a->tok.ty == Tok_Char) {
char ch;
size_t res = unescape_char(a_tok_text(a, a->tok) + 1, &ch);
size_t res = unescape_char(a->tok.text + 1, &ch);
assert(res != 0);
a_step(a);
return (int64_t)ch;
@ -920,7 +939,10 @@ static int64_t a_parse_operand(Assembler *a)
a_step(a);
return v;
}
print_error_exit(a->tok.loc, "expected expression");
print_error_exit(a->tok.loc,
"expected expression, got '%.*s'",
(int)a->tok.len,
a->tok.text);
}
static void a_skip_eol(Assembler *a)
@ -958,7 +980,7 @@ static void a_parse_directive_const(Assembler *a)
int64_t val = a_parse_expr(a);
a_assert_eol(a);
char *ident = a_tok_strdup(a, ident_tok);
char *ident = strndup(ident_tok.text, ident_tok.len);
a_sym_define(a, ident, ident_tok.loc, val);
free(ident);
}
@ -975,18 +997,18 @@ static void a_parse_directive_macro(Assembler *a)
int64_t arg_count = a_parse_operand(a);
a_assert_eol(a);
char *ident = a_tok_strdup(a, ident_tok);
char *ident = strndup(ident_tok.text, ident_tok.len);
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 begin_tok = a->tok;
size_t begin_idx = begin_tok.loc.idx;
Tok last = a->tok;
a_step(a);
int depth = 1;
@ -1017,20 +1039,22 @@ static void a_parse_directive_macro(Assembler *a)
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);
strncpy(text, begin_tok.text, text_len);
macro.text = text;
macro.text_offset = begin_idx;
for (size_t i = 0; i < macro.toks_size; ++i) {
Tok *tok = &macro.toks[i];
tok->text = &text[tok->loc.idx - begin_idx];
}
ARRAY_FIELD_PUSH(a, macros, &macro);
Macro *macro_ptr = &a->macros[a->macros_size - 1];
Macro **found;
size_t *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);
print_info(a->macros[*found].loc, "macro '%s' defined here", ident);
exit(1);
}
map_set(&a->macro_map, ident, &macro_ptr);
map_set(&a->macro_map, ident, (size_t[]) { a->macros_size - 1 });
}
static void a_parse_directive_include(Assembler *a)
@ -1042,14 +1066,13 @@ static void a_parse_directive_include(Assembler *a)
a_step(a);
a_assert_eol(a);
char *str_val
= unescape_strndup(a_tok_text(a, str_tok) + 1, str_tok.len - 2);
char *str_val = unescape_strndup(str_tok.text + 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));
char *filename = calloc(included_filename_len + 1, sizeof(char));
snprintf(
filename, included_filename_len + 1, "%s/%s", current_dir, str_val);
@ -1108,13 +1131,13 @@ static void a_parse_directive_guard(Assembler *a)
a->directive_depth += 1;
char *ident = a_tok_strdup(a, ident_tok);
char *ident = strndup(ident_tok.text, ident_tok.len);
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 });
map_set(&a->guard_set, ident, (int[]) { 0 });
}
free(ident);
}
@ -1202,13 +1225,15 @@ static void a_parse_ins_args(Assembler *a, Op *op)
case Op_Nop:
case Op_Hlt:
break;
case Op_Not:
a_parse_op1_reg(a, op);
break;
case Op_Mov:
case Op_Cmp:
case Op_CMov:
case Op_Or:
case Op_Xor:
case Op_And:
case Op_Not:
case Op_Shr:
case Op_Shl:
case Op_RShr:
@ -1234,14 +1259,52 @@ 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)
static void a_parse_dw(Assembler *a, Loc loc)
{
(void)loc;
size_t ip = a->ip;
bool first = true;
while (a->tok.ty != Tok_Eof && a->tok.ty != Tok_Eol) {
if (!first) {
if (a->tok.ty != ',')
print_error_exit(a->tok.loc, "expected ','");
a_step(a);
}
first = false;
if (a->tok.ty == Tok_Str) {
Tok tok = a->tok;
a_step(a);
for (size_t i = 1; i < tok.len;) {
char ch;
size_t res = unescape_char(&tok.text[i], &ch);
assert(res > 0);
i += res;
assert(ip < a->output_size);
a->output[ip] = (uint16_t)ch;
ip += 1;
}
} else {
int64_t val = a_parse_expr(a);
assert(ip < a->output_size);
a->output[ip] = (uint16_t)val;
ip += 1;
}
}
a->ip = ip;
}
static void a_parse_macro_expan(Assembler *a, size_t macro_idx, Loc loc)
{
MacroExpansion expan = {
.loc = loc,
.macro = macro,
.macro_idx = macro_idx,
ARRAY_FIELD_INIT(args),
};
Macro *macro = &a->macros[macro_idx];
bool first = true;
while (first ? a->tok.ty != Tok_Eol : a->tok.ty == ',') {
if (!first) {
@ -1285,7 +1348,7 @@ static void a_parse_macro_expan(Assembler *a, Macro *macro, Loc loc)
static void a_parse_ins(Assembler *a, Tok ident_tok)
{
char *ident = a_tok_strdup(a, ident_tok);
char *ident = strndup(ident_tok.text, ident_tok.len);
Op op = { };
@ -1294,10 +1357,16 @@ 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) {
if (strcmp(ident, "dw") == 0) {
free(ident);
a_parse_macro_expan(a, *macro, ident_tok.loc);
a_parse_dw(a, ident_tok.loc);
return;
}
size_t *macro_idx = map_get(&a->macro_map, ident);
if (macro_idx) {
free(ident);
a_parse_macro_expan(a, *macro_idx, ident_tok.loc);
return;
}
@ -1326,7 +1395,7 @@ static void a_parse_label_or_ins(Assembler *a)
if (a->tok.ty == ':') {
a_step(a);
char *ident = a_tok_strdup(a, ident_tok);
char *ident = strndup(ident_tok.text, ident_tok.len);
a_sym_define(a, ident, ident_tok.loc, a->ip);
free(ident);
@ -1353,14 +1422,15 @@ static void a_parse_local_label(Assembler *a)
print_error_exit(a->tok.loc, "expected ':'");
a_step(a);
char *ident = a_tok_strdup(a, ident_tok);
char *ident = strndup(ident_tok.text, ident_tok.len);
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);
a_sym_define(a, ident, ident_tok.loc, a->ip);
a_sym_define(a, unique_ident, ident_tok.loc, a->ip);
free(ident);
free(unique_ident);
assembler_parse_line(a);
}
@ -1388,7 +1458,7 @@ void assembler_parse_line(Assembler *a)
a->tok.ty,
a->tok.ty,
a->tok.len,
a_tok_text(a, a->tok));
a->tok.text);
}
bool assembler_done(Assembler *a)

View File

@ -75,6 +75,7 @@ typedef struct {
Disk *disk;
bool is_off;
bool vcd_updated;
bool dump_regs;
} VM;
static uint16_t op2_reg_or_imm(VMState *s, const Op *op)
@ -257,7 +258,8 @@ void vm_run(VM *vm)
if (ok)
ok = vm_run_op(vm, &s, &op) == 0;
// vmstate_dump_regs(&s);
if (vm->dump_regs)
vmstate_dump_regs(&s);
if (!ok) {
printf("error occured at:\n");
@ -266,12 +268,6 @@ void vm_run(VM *vm)
break;
}
if (*ip > 100) {
printf("stopped\n");
vm->is_off = true;
break;
}
if (op.ty == Op_Hlt) {
wait_next_iter = true;
break;
@ -475,6 +471,12 @@ int main(int argc, char *argv[])
vm.state.memory = calloc(vm.state.memory_size, sizeof(uint16_t));
vm.disk = (Disk *)&disk;
for (int i = 2; i < argc; ++i) {
if (strcmp(argv[i], "--dump-regs") == 0) {
vm.dump_regs = true;
}
}
thrd_t vm_thread;
thrd_create(&vm_thread, vm_thread_start, &vm);
#ifndef DISABLE_SDL