Add assembler macros

This commit is contained in:
Shadowfacts 2019-12-07 22:47:48 -05:00
parent d2af80fa26
commit d4e68f6062
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 134 additions and 63 deletions

View File

@ -1,4 +1,5 @@
# Used by "mix format" # Used by "mix format"
[ [
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
locals_without_parens: [assemble_macro: 2]
] ]

View File

@ -1,5 +1,17 @@
defmodule Assembler.Helpers do
defmacro assemble_macro(match, asm) do
quote do
def assemble_insn(unquote(match), memory) do
{macro_mem, _} = do_assemble(unquote(asm), length(memory))
macro_mem ++ memory
end
end
end
end
defmodule Assembler do defmodule Assembler do
import NimbleParsec import NimbleParsec
import Assembler.Helpers
label = label =
ascii_string([?a..?z, ?A..?Z, ?_], min: 1) ascii_string([?a..?z, ?A..?Z, ?_], min: 1)
@ -21,7 +33,7 @@ defmodule Assembler do
|> ascii_string([{:not, ?,}], min: 1) |> ascii_string([{:not, ?,}], min: 1)
instruction = instruction =
ignore(ascii_string([?\s], 2)) ignore(ascii_string([?\s, ?\t], min: 0))
|> ascii_string([?a..?z], min: 1) |> ascii_string([?a..?z], min: 1)
|> repeat(param |> ignore(ascii_char([?,]))) |> repeat(param |> ignore(ascii_char([?,])))
|> optional(param) |> optional(param)
@ -37,45 +49,14 @@ defmodule Assembler do
]) ])
) )
@asm """
in res
clt $res, _self + 1, cmpRes
jnz $cmpRes, lessThan
ceq $res, 8, cmpRes
jnz $cmpRes, equal
out 1001
hlt
lessThan:
out 999
hlt
equal:
out 1000
hlt
res: 0
cmpRes: 0
"""
def test do def test do
assemble(@asm) File.read!("test/intcode/cmp8.asm")
|> assemble()
|> Day5.run_cli()
end end
def assemble(asm) do def assemble(asm, offset \\ 0) do
{memory, labels} = {memory, labels} = do_assemble(asm, offset)
asm
|> String.trim_trailing("\n")
|> String.split("\n")
|> Enum.map(fn line ->
case parse_line(line) do
{:ok, res, _, _, _, _} ->
res
err ->
raise "Unable to parse line: '#{line}':\n#{inspect(err)}"
end
end)
|> Enum.reduce({[], %{}}, &assemble_line/2)
memory memory
|> Enum.reverse() |> Enum.reverse()
@ -92,23 +73,47 @@ defmodule Assembler do
end) end)
end end
def assemble_line([], acc), do: acc def do_assemble(asm, offset) do
asm
|> String.trim_trailing("\n")
|> String.split("\n")
|> Enum.map(fn line ->
case parse_line(line) do
{:ok, res, _, _, _, _} ->
res
def assemble_line([label: [name]], {memory, labels}) do err ->
raise "Unable to parse line: '#{line}':\n#{inspect(err)}"
end
end)
|> Enum.reduce({[], %{}}, fn line, acc ->
assemble_line(line, acc, offset)
end)
end
def assemble_line([], acc, _), do: acc
def assemble_line([label: [name]], {memory, labels}, offset) do
{ {
memory, memory,
Map.put(labels, name, length(memory)) Map.put(labels, name, length(memory) + offset)
} }
end end
def assemble_line([data_label: [name, value]], {memory, labels}) do def assemble_line([data_label: [name, value]], {memory, labels}, _) do
value =
case value do
{:neg, [val]} -> -val
val when is_integer(val) -> val
end
{ {
[value | memory], [value | memory],
Map.put(labels, name, length(memory)) Map.put(labels, name, length(memory))
} }
end end
def assemble_line([insn: insn], {memory, labels}) do def assemble_line([insn: insn], {memory, labels}, _) do
{ {
assemble_insn(insn, memory), assemble_insn(insn, memory),
labels labels
@ -159,6 +164,46 @@ defmodule Assembler do
[99 | memory] [99 | memory]
end end
# macros
assemble_macro ["mov", src, dest],
"""
add 0, #{src}, #{dest}
"""
assemble_macro ["jmp", dest],
"""
jnz _self, #{dest}
"""
assemble_macro ["sub", a, b, dest],
"""
mul #{b}, -1, #{dest}
add #{a}, $#{dest}, #{dest}
"""
assemble_macro ["cle", a, b, dest],
"""
clt #{a}, #{b}, #{dest}
jnz $#{dest}, end
ceq #{a}, #{b}, #{dest}
end:
"""
assemble_macro ["cgt", a, b, dest],
"""
clt #{a}, #{b}, #{dest}
jnz $#{dest}, false
ceq #{a}, #{b}, #{dest}
jnz $#{dest}, false
mov 1, #{dest}
jmp end
false:
mov 0, #{dest}
end:
"""
@doc """ @doc """
Raises the base to to the given power. Raises the base to to the given power.

View File

@ -24,6 +24,12 @@ defmodule AssemblerTest do
assert assemble("hlt") == [99] assert assemble("hlt") == [99]
end end
test "assembles macros" do
assert assemble("mov 1, 2") == [1101, 0, 1, 2]
assert assemble("jmp 2") == [1105, 1, 2]
assert assemble("sub 5, 3, res\nres: 0") == [1102, 3, -1, 8, 101, 5, 8, 8, 0]
end
test "assembles simple program" do test "assembles simple program" do
program = """ program = """
add 1, 2, 10 add 1, 2, 10

19
test/intcode/cmp8.asm Normal file
View File

@ -0,0 +1,19 @@
in res
clt $res, 8, cmpRes
jnz $(res + 1), lessThan
ceq $res, 8, cmpRes
jnz $cmpRes, equal
out 1001
hlt
lessThan:
out 999
hlt
equal:
out 1000
hlt
res: 0
cmpRes: 0