From 1ceaf6ea0f9766147eb4c689911b3cbfbdc46282 Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 16 Jul 2022 13:12:52 +0200 Subject: [PATCH] Remove the terrible and problematic shell script with cleaner Lua scripts for generating the em_table stuff. --- modules/src/em_data/build.lua | 53 +++++++++-------- modules/src/em_data/em_table_lib.lua | 88 ++++++++++++++++++++++++++++ modules/src/em_data/make_flag_c.lua | 21 +++++++ modules/src/em_data/make_mnem_c.lua | 9 +++ modules/src/em_data/make_mnem_h.lua | 7 +++ modules/src/em_data/make_pseu_c.lua | 9 +++ modules/src/em_data/make_pseu_h.lua | 7 +++ modules/src/em_data/make_spec_h.lua | 7 +++ modules/src/em_data/new_table | 76 ------------------------ 9 files changed, 178 insertions(+), 99 deletions(-) create mode 100644 modules/src/em_data/em_table_lib.lua create mode 100644 modules/src/em_data/make_flag_c.lua create mode 100644 modules/src/em_data/make_mnem_c.lua create mode 100644 modules/src/em_data/make_mnem_h.lua create mode 100644 modules/src/em_data/make_pseu_c.lua create mode 100644 modules/src/em_data/make_pseu_h.lua create mode 100644 modules/src/em_data/make_spec_h.lua delete mode 100755 modules/src/em_data/new_table diff --git a/modules/src/em_data/build.lua b/modules/src/em_data/build.lua index aa4e8adcd..2425655ae 100644 --- a/modules/src/em_data/build.lua +++ b/modules/src/em_data/build.lua @@ -1,26 +1,33 @@ -local generated = normalrule { - name = "generated", - ins = { - "./new_table", - "util/cmisc+ed", - "h/em_table", -- relative to root, which is a bit evil - }, - outleaves = { - "em_flag.c", - "em_pseu.c", - "em_mnem.c", - "em_spec.h", - "em_pseu.h", - "em_mnem.h", - }, - deps = { - "h+emheaders" - }, - commands = { - "%{ins[1]} %{ins[2]} %{ins[3]} %{dir} %{dir}", - } +local GENFILES = { + "flag.c", + "mnem.c", + "mnem.h", + "pseu.c", + "pseu.h", + "spec.h" } +local generated = {} +for _, f in ipairs(GENFILES) do + generated[#generated+1] = normalrule { + name = "em_"..f:gsub("%.", "_"), + ins = { + "./make_"..f:gsub("%.", "_")..".lua", + "./em_table_lib.lua", + "h/em_table", -- relative to root, which is a bit evil + }, + outleaves = { + "em_"..f + }, + deps = { + "h+emheaders" + }, + commands = { + "$LUA %{ins[1]} < %{ins[3]} > %{outs}" + } + } +end + clibrary { name = "lib", srcs = concat( @@ -28,10 +35,10 @@ clibrary { matching(filenamesof(generated), "%.c$") ), hdrs = { - "+generated" -- so we export the H files + generated, -- so we export the H files }, deps = { - "+generated", -- so we can see the H files + generated, -- so we can see the H files "h+emheaders" } } diff --git a/modules/src/em_data/em_table_lib.lua b/modules/src/em_data/em_table_lib.lua new file mode 100644 index 000000000..e43a9dee4 --- /dev/null +++ b/modules/src/em_data/em_table_lib.lua @@ -0,0 +1,88 @@ +local function strip(s) + return s:match("^%s*(.-)%s*$") +end + +function load_table() + local specs = {} + for line in io.lines() do + line = strip(line) + if line == "" then + break + end + + local _, _, name, num = line:find("^(%w+)%s+(%d+)$") + if not name then + error("malformed spec line in em_table") + end + + specs[#specs+1] = { + name = name, + num = tonumber(num) + } + end + + for _, v in ipairs(specs) do + specs[v.name] = v + end + + local pseudos = {} + local pseudo_count = 0 + for line in io.lines() do + line = strip(line) + if line == "" then + break + end + + local _, _, name, num, flags = line:find("^(%w+)%s+(%d+)%s+(%g+)$") + if not name then + error("malformed pseudo line in em_table") + end + + pseudos[#pseudos+1] = { + name = name, + num = tonumber(num) + specs.fpseu.num, + flags = flags + } + pseudo_count = pseudo_count + 1 + end + + local mnems = {} + local mnem_count = 0 + for line in io.lines() do + line = strip(line) + if line == "" then + break + end + + local _, _, name, flags1, flags2 = line:find("^(%w+)%s+(%g+)%s+(%g+)$") + if not name then + error("malformed mnemonic line in em_table") + end + + mnem_count = mnem_count + 1 + mnems[#mnems+1] = { + name = name, + num = mnem_count, + flags1 = flags1, + flags2 = flags2 + } + end + + local lpseu = { + name = "lpseu", + num = specs.fpseu.num + pseudo_count - 1 + } + local lmnem = { + name = "lmnem", + num = specs.fmnem.num + mnem_count - 1 + } + + specs[#specs+1] = lpseu + specs.lpseu = lpseu + + specs[#specs+1] = lmnem + specs.lmnem = lmnem + + return specs, pseudos, mnems +end + diff --git a/modules/src/em_data/make_flag_c.lua b/modules/src/em_data/make_flag_c.lua new file mode 100644 index 000000000..56614c9a3 --- /dev/null +++ b/modules/src/em_data/make_flag_c.lua @@ -0,0 +1,21 @@ +require "modules.src.em_data.em_table_lib" + +local specs, pseudos, mnems = load_table() + +local function flag(c) + if c == "-" then + return "NO" + else + return c:upper() + end +end + +print("#include ") +print("char em_flag[] = {") +for _, mnem in ipairs(mnems) do + print(string.format("\tPAR_%s | FLO_%s,", + flag(mnem.flags1:sub(1, 1)), + flag(mnem.flags1:sub(2, 2)))) +end +print("};") + diff --git a/modules/src/em_data/make_mnem_c.lua b/modules/src/em_data/make_mnem_c.lua new file mode 100644 index 000000000..6ff02fa75 --- /dev/null +++ b/modules/src/em_data/make_mnem_c.lua @@ -0,0 +1,9 @@ +require "modules.src.em_data.em_table_lib" + +local specs, pseudos, mnems = load_table() + +print("char em_mnem[][4] = {") +for _, mnem in ipairs(mnems) do + print(string.format('\t"%s",', mnem.name)) +end +print("};") diff --git a/modules/src/em_data/make_mnem_h.lua b/modules/src/em_data/make_mnem_h.lua new file mode 100644 index 000000000..22c1dc235 --- /dev/null +++ b/modules/src/em_data/make_mnem_h.lua @@ -0,0 +1,7 @@ +require "modules.src.em_data.em_table_lib" + +local specs, pseudos, mnems = load_table() + +for _, mnem in ipairs(mnems) do + print(string.format("#define op_%s %d", mnem.name, mnem.num)) +end diff --git a/modules/src/em_data/make_pseu_c.lua b/modules/src/em_data/make_pseu_c.lua new file mode 100644 index 000000000..ca2fc7dd6 --- /dev/null +++ b/modules/src/em_data/make_pseu_c.lua @@ -0,0 +1,9 @@ +require "modules.src.em_data.em_table_lib" + +local specs, pseudos, mnems = load_table() + +print("char em_pseu[][4] = {") +for _, pseu in ipairs(pseudos) do + print(string.format('\t"%s",', pseu.name)) +end +print("};") diff --git a/modules/src/em_data/make_pseu_h.lua b/modules/src/em_data/make_pseu_h.lua new file mode 100644 index 000000000..8e3b10c5f --- /dev/null +++ b/modules/src/em_data/make_pseu_h.lua @@ -0,0 +1,7 @@ +require "modules.src.em_data.em_table_lib" + +local specs, pseudos, mnems = load_table() + +for _, pseu in ipairs(pseudos) do + print(string.format("#define ps_%s %d", pseu.name, pseu.num)) +end diff --git a/modules/src/em_data/make_spec_h.lua b/modules/src/em_data/make_spec_h.lua new file mode 100644 index 000000000..32472b68d --- /dev/null +++ b/modules/src/em_data/make_spec_h.lua @@ -0,0 +1,7 @@ +require "modules.src.em_data.em_table_lib" + +local specs, pseudos, mnems = load_table() + +for _, spec in ipairs(specs) do + print(string.format("#define sp_%s %d", spec.name, spec.num)) +end diff --git a/modules/src/em_data/new_table b/modules/src/em_data/new_table deleted file mode 100755 index bdb7956a6..000000000 --- a/modules/src/em_data/new_table +++ /dev/null @@ -1,76 +0,0 @@ -#!/bin/sh -set -e - -ed=$1 -em_table=$2 -h=${3-.} -d=${4-.} - -p=$(grep fpseu $em_table | cut -f2) -m=$(grep fmnem $em_table | cut -f2) - -$ed - $em_table <<'A' > X -1,/^$/g/ /s// /gp -A - -$ed - $em_table <<'A' | awk '{$2=$2+'$p'; print}' > Y -1,/^$/d -1,/^$/g/ /s// /gp -A - -$ed - $em_table <<'A' | awk '{print $0,'$m'+i++}' > Z -1,/^$/d -1,/^$/d -1,/^$/g/ /s// /gp -A -$ed - $em_table | awk '{print $0,'$m'+i++}' - -i=`wc -l >X -i=`wc -l >X - -$ed - X <<'A' > $h/em_spec.h -g/^/s//#define sp_/p -A - -$ed - Y <<'A' > $h/em_pseu.h -g/ \(.*\) .*/s// \1/ -g/\(.*\) \(.*\)/s//#define ps_\1 \2/p -A - -$ed - Z <<'A' > $h/em_mnem.h -g/ .* /s// / -g/\(.*\) \(.*\)/s//#define op_\1 \2/p -A - -( -echo 'char em_pseu[][4] = {' -$ed - Y <<'A' -g/\(...\).*/s// "\1",/p -A -echo '};' -) > $d/em_pseu.c - -( -echo 'char em_mnem[][4] = {' -$ed - Z <<'A' -g/\(...\).*/s// "\1",/p -A -echo '};' -) > $d/em_mnem.c - -( -echo '#include -char em_flag[] = {' -$ed - Z <<'A' | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ -g/^... /s/// -g/ .*/s/// -g/\(.\)\(.\)/s//PAR_\1 | FLO_\2/ -g/-/s//NO/g -g/.*/s// &,/p -A -echo '};' -) > $d/em_flag.c - -rm X Y Z