Remove the terrible and problematic shell script with cleaner Lua scripts for generating the em_table stuff.

This commit is contained in:
David Given 2022-07-16 13:12:52 +02:00
parent 6443647c8c
commit 1ceaf6ea0f
9 changed files with 178 additions and 99 deletions

View file

@ -1,25 +1,32 @@
local generated = normalrule {
name = "generated",
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 = {
"./new_table",
"util/cmisc+ed",
"./make_"..f:gsub("%.", "_")..".lua",
"./em_table_lib.lua",
"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",
"em_"..f
},
deps = {
"h+emheaders"
},
commands = {
"%{ins[1]} %{ins[2]} %{ins[3]} %{dir} %{dir}",
"$LUA %{ins[1]} < %{ins[3]} > %{outs}"
}
}
}
end
clibrary {
name = "lib",
@ -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"
}
}

View file

@ -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

View file

@ -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 <em_flag.h>")
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("};")

View file

@ -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("};")

View file

@ -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

View file

@ -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("};")

View file

@ -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

View file

@ -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

View file

@ -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 <Y`
echo 'lpseu' `expr $i + $p - 1` >>X
i=`wc -l <Z`
echo 'lmnem' `expr $i + $m - 1` >>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 <em_flag.h>
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