From 0791b20f94f2c02595a6f39ca67eb40787a7521c Mon Sep 17 00:00:00 2001 From: Fey Naomi Schrewe Date: Fri, 17 Oct 2025 09:13:11 +0200 Subject: [PATCH] chore: add macro to simplify constructing opcodes --- host/assembler/opcodes.fnl | 16 ++++++++-------- test/assembler/opcodes.fnl | 13 ++++++++++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/host/assembler/opcodes.fnl b/host/assembler/opcodes.fnl index 1903cf0..d5fe095 100644 --- a/host/assembler/opcodes.fnl +++ b/host/assembler/opcodes.fnl @@ -77,19 +77,19 @@ (let [{: cond} (parse-conditions ?options) max-immediate 0xffffff offset (band offset util.word-max)] - (bor - (lshift cond 28) - (lshift 10 24) - (band (rshift offset 2) 0xffffff)))) + (construct-opcode + (cond 28) + (10 24) + ((band (rshift offset 2) 0xffffff) 0)))) (fn branch-link [offset ?options] (let [{: cond} (parse-conditions ?options) max-immediate 0xffffff offset (band offset util.word-max)] - (bor - (lshift cond 28) - (lshift 11 24) - (band (rshift offset 2) 0xffffff)))) + (construct-opcode + (cond 28) + (11 24) + ((band (rshift offset 2) 0xffffff) 0)))) {: r : move diff --git a/test/assembler/opcodes.fnl b/test/assembler/opcodes.fnl index 13ba747..069d5e2 100644 --- a/test/assembler/opcodes.fnl +++ b/test/assembler/opcodes.fnl @@ -1,6 +1,5 @@ (local t (require :deps.faith)) -(local {: r : move} (require :host.assembler.opcodes)) - +(local {: r : move : branch : branch-link} (require :host.assembler.opcodes)) (fn hex [n] "format a number as 32 bit hexadecimal" (string.format :0x%08x n)) @@ -11,4 +10,12 @@ (t.= 0xe3a05003 (move (r 5) 3))) -{: test-move} +(fn test-branch [] + (t.= 0xea000021 + (branch 132))) + +(fn test-branch-link [] + (t.= 0xeb000021 + (branch-link 132))) + +{: test-move : test-branch : test-branch-link}