| 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | ## |
| 4 | ## Copyright(c) 2019-2024 rev.ng Labs Srl. All Rights Reserved. |
| 5 | ## |
| 6 | ## This program is free software; you can redistribute it and/or modify |
| 7 | ## it under the terms of the GNU General Public License as published by |
| 8 | ## the Free Software Foundation; either version 2 of the License, or |
| 9 | ## (at your option) any later version. |
| 10 | ## |
| 11 | ## This program is distributed in the hope that it will be useful, |
| 12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | ## GNU General Public License for more details. |
| 15 | ## |
| 16 | ## You should have received a copy of the GNU General Public License |
| 17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 | ## |
| 19 | |
| 20 | import sys |
| 21 | import re |
| 22 | import string |
| 23 | import argparse |
| 24 | from io import StringIO |
| 25 | |
| 26 | import hex_common |
| 27 | |
| 28 | |
| 29 | ## |
| 30 | ## Generate code to be fed to the idef_parser |
| 31 | ## |
| 32 | ## Consider A2_add: |
| 33 | ## |
| 34 | ## Rd32=add(Rs32,Rt32), { RdV=RsV+RtV;} |
| 35 | ## |
| 36 | ## We produce: |
| 37 | ## |
| 38 | ## A2_add(RdV, in RsV, in RtV) { |
| 39 | ## { RdV=RsV+RtV;} |
| 40 | ## } |
| 41 | ## |
| 42 | ## A2_add represents the instruction tag. Then we have a list of TCGv |
| 43 | ## that the code generated by the parser can expect in input. Some of |
| 44 | ## them are inputs ("in" prefix), while some others are outputs. |
| 45 | ## |
| 46 | def main(): |
| 47 | parser = argparse.ArgumentParser( |
| 48 | "Emit instruction implementations that can be fed to idef-parser" |
| 49 | ) |
| 50 | parser.add_argument("semantics", help="semantics file") |
| 51 | parser.add_argument("out", help="output file") |
| 52 | args = parser.parse_args() |
| 53 | hex_common.read_semantics_file(args.semantics) |
| 54 | hex_common.calculate_attribs() |
| 55 | hex_common.init_registers() |
| 56 | tagregs = hex_common.get_tagregs() |
| 57 | tagimms = hex_common.get_tagimms() |
| 58 | |
| 59 | with open(args.out, "w") as f: |
| 60 | f.write('#include "macros.h.inc"\n\n') |
| 61 | |
| 62 | for tag in hex_common.tags: |
| 63 | if hex_common.tag_ignore(tag): |
| 64 | continue |
| 65 | ## Skip the priv instructions |
| 66 | if "A_PRIV" in hex_common.attribdict[tag]: |
| 67 | continue |
| 68 | ## Skip the guest instructions |
| 69 | if "A_GUEST" in hex_common.attribdict[tag]: |
| 70 | continue |
| 71 | ## Skip instructions that saturate in a ternary expression |
| 72 | if tag in {"S2_asr_r_r_sat", "S2_asl_r_r_sat"}: |
| 73 | continue |
| 74 | ## Skip instructions using switch |
| 75 | if tag in {"S4_vrcrotate_acc", "S4_vrcrotate"}: |
| 76 | continue |
| 77 | ## Skip trap instructions |
| 78 | if tag in {"J2_trap0", "J2_trap1"}: |
| 79 | continue |
| 80 | ## Skip 128-bit instructions |
| 81 | if tag in {"A7_croundd_ri", "A7_croundd_rr"}: |
| 82 | continue |
| 83 | if tag in { |
| 84 | "M7_wcmpyrw", |
| 85 | "M7_wcmpyrwc", |
| 86 | "M7_wcmpyiw", |
| 87 | "M7_wcmpyiwc", |
| 88 | "M7_wcmpyrw_rnd", |
| 89 | "M7_wcmpyrwc_rnd", |
| 90 | "M7_wcmpyiw_rnd", |
| 91 | "M7_wcmpyiwc_rnd", |
| 92 | }: |
| 93 | continue |
| 94 | ## Skip interleave/deinterleave instructions |
| 95 | if tag in {"S2_interleave", "S2_deinterleave"}: |
| 96 | continue |
| 97 | ## Skip instructions using bit reverse |
| 98 | if tag in { |
| 99 | "S2_brev", |
| 100 | "S2_brevp", |
| 101 | "S2_ct0", |
| 102 | "S2_ct1", |
| 103 | "S2_ct0p", |
| 104 | "S2_ct1p", |
| 105 | "A4_tlbmatch", |
| 106 | }: |
| 107 | continue |
| 108 | ## Skip other unsupported instructions |
| 109 | if tag == "S2_cabacdecbin" or tag == "A5_ACS": |
| 110 | continue |
| 111 | if tag.startswith("Y"): |
| 112 | continue |
| 113 | if tag.startswith("V6_"): |
| 114 | continue |
| 115 | if ( tag.startswith("F") and |
| 116 | tag not in { |
| 117 | "F2_sfimm_p", |
| 118 | "F2_sfimm_n", |
| 119 | "F2_dfimm_p", |
| 120 | "F2_dfimm_n", |
| 121 | "F2_dfmpyll", |
| 122 | "F2_dfmpylh" |
| 123 | }): |
| 124 | continue |
| 125 | if tag.endswith("_locked"): |
| 126 | continue |
| 127 | if "A_COF" in hex_common.attribdict[tag]: |
| 128 | continue |
| 129 | if ( tag.startswith('R6_release_') ): |
| 130 | continue |
| 131 | ## Skip instructions that are incompatible with short-circuit |
| 132 | ## packet register writes |
| 133 | if ( tag == 'S2_insert' or |
| 134 | tag == 'S2_insert_rp' or |
| 135 | tag == 'S2_asr_r_svw_trun' or |
| 136 | tag == 'A2_swiz' ): |
| 137 | continue |
| 138 | |
| 139 | regs = tagregs[tag] |
| 140 | imms = tagimms[tag] |
| 141 | |
| 142 | arguments = [] |
| 143 | for regtype, regid in regs: |
| 144 | reg = hex_common.get_register(tag, regtype, regid) |
| 145 | prefix = "in " if reg.is_read() else "" |
| 146 | arguments.append(f"{prefix}{reg.reg_tcg()}") |
| 147 | |
| 148 | for immlett, bits, immshift in imms: |
| 149 | arguments.append(hex_common.imm_name(immlett)) |
| 150 | |
| 151 | f.write(f"{tag}({', '.join(arguments)}) {{\n") |
| 152 | f.write(" ") |
| 153 | if hex_common.need_ea(tag): |
| 154 | f.write("size4u_t EA; ") |
| 155 | f.write(f"{hex_common.semdict[tag]}\n") |
| 156 | f.write("}\n\n") |
| 157 | |
| 158 | |
| 159 | if __name__ == "__main__": |
| 160 | main() |