From 0da0a54b7404c2ad0a1a066309fa58f3dd37a559 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 | 28 ++++++++++++++-------------- test.fnl | 5 +++++ test/assembler/opcodes.fnl | 13 ++++++++++++- 3 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 test.fnl diff --git a/host/assembler/opcodes.fnl b/host/assembler/opcodes.fnl index c33912e..1903cf0 100644 --- a/host/assembler/opcodes.fnl +++ b/host/assembler/opcodes.fnl @@ -56,22 +56,22 @@ dest (register? dest)] (when (not dest) (error "dest must be a register")) (if source-register - (bor - (lshift cond 28) - (lshift 0xd 21) - (lshift set-flags 20) - (lshift dest 12) - source-register) + (construct-opcode + [cond 28] + [0xd 21] + [set-flags 20] + [dest 12] + [source-register 0]) (let [rotation (calculate-rotation source)] (when (= nil rotation) (error "Unencodable immediate value")) - (bor - (lshift cond 28) - (lshift 1 25) - (lshift 0xd 21) - (lshift set-flags 20) - (lshift dest 12) - (lshift (/ rotation 2) 8) - (band (util.rotate-left source rotation) 0xff)))))) + (construct-opcode + (cond 28) + (1 25) + (0xd 21) + (set-flags 20) + (dest 12) + ((/ rotation 2) 8) + ((band (util.rotate-left source rotation) 0xff) 0)))))) (fn branch [offset ?options] (let [{: cond} (parse-conditions ?options) diff --git a/test.fnl b/test.fnl new file mode 100644 index 0000000..7c33156 --- /dev/null +++ b/test.fnl @@ -0,0 +1,5 @@ +(local t (require :deps.faith)) + +(local default-modules [:test.assembler.opcodes]) + +(t.run (if (= 0 (length arg)) default-modules arg)) diff --git a/test/assembler/opcodes.fnl b/test/assembler/opcodes.fnl index 841f1ea..3a510ef 100644 --- a/test/assembler/opcodes.fnl +++ b/test/assembler/opcodes.fnl @@ -1,2 +1,13 @@ (local t (require :deps.faith)) -(local opcodes (require :host.assembler.opcodes)) +(local {: r : move} (require :host.assembler.opcodes)) +(fn hex [n] + "format a number as 32 bit hexadecimal" + (string.format :0x%08x n)) + +(fn test-move [] + (t.= 0xe1a05003 + (move (r 5) (r 3))) + (t.= 0xe3a05003 + (move (r 5) 3))) + +{: test-move}