From 2fd83903cf17571900d7afd8cd57795d23c3fe61 Mon Sep 17 00:00:00 2001 From: Alexis Engelke Date: Sun, 20 Feb 2022 17:15:21 +0100 Subject: [PATCH] meson,parseinstrs: Make decode and encode optional --- decode.c | 6 ++--- encode.c | 2 +- fadec-enc.h | 2 +- fadec.h | 2 +- format.c | 6 ++--- meson.build | 56 ++++++++++++++++++++++++++++++----------------- meson_options.txt | 2 ++ parseinstrs.py | 28 +++++++++++++----------- 8 files changed, 62 insertions(+), 42 deletions(-) diff --git a/decode.c b/decode.c index 80b1857..c0cbef0 100644 --- a/decode.c +++ b/decode.c @@ -11,7 +11,7 @@ // Defines FD_TABLE_OFFSET_32 and FD_TABLE_OFFSET_64, if available #define FD_DECODE_TABLE_DEFINES -#include +#include #undef FD_DECODE_TABLE_DEFINES enum DecodeMode { @@ -35,7 +35,7 @@ static unsigned table_walk(unsigned cur_idx, unsigned entry_idx, unsigned* out_kind) { static __attribute__((aligned(16))) const uint16_t _decode_table[] = { #define FD_DECODE_TABLE_DATA -#include +#include #undef FD_DECODE_TABLE_DATA }; unsigned entry = _decode_table[cur_idx + entry_idx]; @@ -273,7 +273,7 @@ prefix_end: static __attribute__((aligned(16))) const struct InstrDesc descs[] = { #define FD_DECODE_TABLE_DESCS -#include +#include #undef FD_DECODE_TABLE_DESCS }; const struct InstrDesc* desc = &descs[table_idx >> 2]; diff --git a/encode.c b/encode.c index bebbead..ebaac3a 100644 --- a/encode.c +++ b/encode.c @@ -311,7 +311,7 @@ struct EncodeDesc { }; static const struct EncodeDesc descs[] = { -#include +#include }; int diff --git a/fadec-enc.h b/fadec-enc.h index e6dc637..cc04a08 100644 --- a/fadec-enc.h +++ b/fadec-enc.h @@ -46,7 +46,7 @@ typedef int64_t FeOp; enum { #define FE_MNEMONIC(name,value) name = value, -#include +#include #undef FE_MNEMONIC FE_MNEM_MAX }; diff --git a/fadec.h b/fadec.h index ab8fa0d..b544eb0 100644 --- a/fadec.h +++ b/fadec.h @@ -30,7 +30,7 @@ typedef enum { typedef enum { #define FD_MNEMONIC(name,value) FDI_ ## name = value, -#include +#include #undef FD_MNEMONIC } FdInstrType; diff --git a/format.c b/format.c index 171f983..1169723 100644 --- a/format.c +++ b/format.c @@ -135,19 +135,19 @@ static char* fd_mnemonic(char buf[restrict static 48], const FdInstr* instr) { #define FD_DECODE_TABLE_STRTAB1 static const char* mnemonic_str = -#include +#include "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; // 15 NULL Bytes to prevent overflow #undef FD_DECODE_TABLE_STRTAB1 #define FD_DECODE_TABLE_STRTAB2 static const uint16_t mnemonic_offs[] = { -#include +#include }; #undef FD_DECODE_TABLE_STRTAB2 #define FD_DECODE_TABLE_STRTAB3 static const uint8_t mnemonic_lens[] = { -#include +#include }; #undef FD_DECODE_TABLE_STRTAB3 diff --git a/meson.build b/meson.build index 865b888..c0d2a1a 100644 --- a/meson.build +++ b/meson.build @@ -31,6 +31,21 @@ if get_option('warning_level').to_int() >= 3 endforeach endif +sources = [] +headers = [] +components = [] + +if get_option('with_decode') + components += 'decode' + headers += files('fadec.h') + sources += files('decode.c', 'format.c') +endif +if get_option('with_encode') + components += 'encode' + headers += files('fadec-enc.h') + sources += files('encode.c') +endif + generate_args = [] if get_option('archmode') != 'only64' generate_args += ['--32'] @@ -42,33 +57,34 @@ if get_option('with_undoc') generate_args += ['--with-undoc'] endif -instr_data = custom_target('tables', - command: [python3, '@INPUT0@', '@INPUT1@', '@OUTPUT@'] + generate_args, - input: files('parseinstrs.py', 'instrs.txt'), - output: [ - 'fadec-mnems.inc', 'fadec-table.inc', - 'fadec-enc-mnems.inc', 'fadec-enc-cases.inc', - ], - install: true, - install_dir: [ - get_option('includedir'), false, - get_option('includedir'), false, - ]) +tables = [] +foreach component : components + tables += custom_target('@0@_table'.format(component), + command: [python3, '@INPUT0@', component, + '@INPUT1@', '@OUTPUT@'] + generate_args, + input: files('parseinstrs.py', 'instrs.txt'), + output: ['fadec-@0@-public.inc'.format(component), + 'fadec-@0@-private.inc'.format(component)], + install: true, + install_dir: [get_option('includedir'), false]) +endforeach -libfadec = static_library('fadec', 'decode.c', 'encode.c', 'format.c', instr_data, - install: true) +libfadec = static_library('fadec', sources, tables, install: true) fadec = declare_dependency(link_with: libfadec, include_directories: include_directories('.'), - sources: instr_data) + sources: tables) +install_headers(headers) + +foreach component : components + test(component, executable('@0@-test'.format(component), + '@0@-test.c'.format(component), + dependencies: fadec)) +endforeach + if meson.version().version_compare('>=0.54.0') meson.override_dependency('fadec', fadec) endif -test('decode', executable('decode-test', 'decode-test.c', dependencies: fadec)) -test('encode', executable('encode-test', 'encode-test.c', dependencies: fadec)) - -install_headers('fadec.h', 'fadec-enc.h') - pkg = import('pkgconfig') pkg.generate(libraries: libfadec, version: '0.1', diff --git a/meson_options.txt b/meson_options.txt index d80fd3d..7383a74 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,2 +1,4 @@ option('archmode', type: 'combo', choices: ['both', 'only32', 'only64']) option('with_undoc', type: 'boolean', value: false) +option('with_decode', type: 'boolean', value: true) +option('with_encode', type: 'boolean', value: true) diff --git a/parseinstrs.py b/parseinstrs.py index 6a99bbb..2ecfaf9 100644 --- a/parseinstrs.py +++ b/parseinstrs.py @@ -499,7 +499,9 @@ def superstring(strs): realstrs.remove(s) return merged -def decode_table(entries, modes): +def decode_table(entries, args): + modes = args.modes + trie = Trie(root_count=len(modes)) mnems, descs, desc_map = set(), [], {} for weak, opcode, desc in entries: @@ -568,7 +570,7 @@ def decode_table(entries, modes): #endif """ -def encode_table(entries): +def encode_table(entries, args): mnemonics = defaultdict(list) mnemonics["FE_NOP"].append(("NP", 0, 0, "0x90")) for weak, opcode, desc in entries: @@ -686,15 +688,19 @@ def encode_table(entries): return mnem_tab, descs if __name__ == "__main__": + generators = { + "decode": decode_table, + "encode": encode_table, + } + parser = argparse.ArgumentParser() parser.add_argument("--32", dest="modes", action="append_const", const=32) parser.add_argument("--64", dest="modes", action="append_const", const=64) parser.add_argument("--with-undoc", action="store_true") + parser.add_argument("mode", choices=generators.keys()) parser.add_argument("table", type=argparse.FileType('r')) - parser.add_argument("decode_mnems", type=argparse.FileType('w')) - parser.add_argument("decode_table", type=argparse.FileType('w')) - parser.add_argument("encode_mnems", type=argparse.FileType('w')) - parser.add_argument("encode_table", type=argparse.FileType('w')) + parser.add_argument("out_public", type=argparse.FileType('w')) + parser.add_argument("out_private", type=argparse.FileType('w')) args = parser.parse_args() entries = [] @@ -706,10 +712,6 @@ if __name__ == "__main__": if "UNDOC" not in desc.flags or args.with_undoc: entries.append((weak, opcode, desc)) - fd_mnem_list, fd_table = decode_table(entries, args.modes) - args.decode_mnems.write(fd_mnem_list) - args.decode_table.write(fd_table) - - fe_mnem_list, fe_code = encode_table(entries) - args.encode_mnems.write(fe_mnem_list) - args.encode_table.write(fe_code) + res_public, res_private = generators[args.mode](entries, args) + args.out_public.write(res_public) + args.out_private.write(res_private)