1bf215546Sopenharmony_ci#
2bf215546Sopenharmony_ci# Copyright (C) 2020 Collabora, Ltd.
3bf215546Sopenharmony_ci#
4bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci# copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci# to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci# and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci# Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci#
11bf215546Sopenharmony_ci# The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci# paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci# Software.
14bf215546Sopenharmony_ci#
15bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci# IN THE SOFTWARE.
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ciTEMPLATE = """#include "bi_opcodes.h"
24bf215546Sopenharmony_ci<%
25bf215546Sopenharmony_cidef hasmod(mods, name):
26bf215546Sopenharmony_ci        return 1 if name in mods else 0
27bf215546Sopenharmony_ci%>
28bf215546Sopenharmony_cistruct bi_op_props bi_opcode_props[BI_NUM_OPCODES] = {
29bf215546Sopenharmony_ci% for opcode in sorted(mnemonics):
30bf215546Sopenharmony_ci    <%
31bf215546Sopenharmony_ci        add = instructions["+" + opcode][0][1] if "+" + opcode in instructions else None
32bf215546Sopenharmony_ci        size = typesize(opcode)
33bf215546Sopenharmony_ci        message = add["message"].upper() if add else "NONE"
34bf215546Sopenharmony_ci        sr_count = add["staging_count"].upper() if add else "0"
35bf215546Sopenharmony_ci        sr_read = int(add["staging"] in ["r", "rw"] if add else False)
36bf215546Sopenharmony_ci        sr_write = int(add["staging"] in ["w", "rw"] if add else False)
37bf215546Sopenharmony_ci        last = int(bool(add["last"]) if add else False)
38bf215546Sopenharmony_ci        table = int(bool(add["table"]) if add else False)
39bf215546Sopenharmony_ci        branch = int(opcode.startswith('BRANCH'))
40bf215546Sopenharmony_ci        has_fma = int("*" + opcode in instructions)
41bf215546Sopenharmony_ci        has_add = int("+" + opcode in instructions)
42bf215546Sopenharmony_ci        mods = ops[opcode]['modifiers']
43bf215546Sopenharmony_ci        clamp = hasmod(mods, 'clamp')
44bf215546Sopenharmony_ci        not_result = hasmod(mods, 'not_result')
45bf215546Sopenharmony_ci        abs = hasmod(mods, 'abs0') | (hasmod(mods, 'abs1') << 1) | (hasmod(mods, 'abs2') << 2)
46bf215546Sopenharmony_ci        neg = hasmod(mods, 'neg0') | (hasmod(mods, 'neg1') << 1) | (hasmod(mods, 'neg2') << 2)
47bf215546Sopenharmony_ci        m_not = hasmod(mods, 'not1')
48bf215546Sopenharmony_ci    %>
49bf215546Sopenharmony_ci    [BI_OPCODE_${opcode.replace('.', '_').upper()}] = {
50bf215546Sopenharmony_ci        "${opcode}", BIFROST_MESSAGE_${message}, BI_SIZE_${size},
51bf215546Sopenharmony_ci        BI_SR_COUNT_${sr_count}, ${sr_read}, ${sr_write}, ${last}, ${branch},
52bf215546Sopenharmony_ci        ${table}, ${has_fma}, ${has_add}, ${clamp}, ${not_result}, ${abs},
53bf215546Sopenharmony_ci        ${neg}, ${m_not},
54bf215546Sopenharmony_ci    },
55bf215546Sopenharmony_ci% endfor
56bf215546Sopenharmony_ci};"""
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ciimport sys
59bf215546Sopenharmony_cifrom bifrost_isa import *
60bf215546Sopenharmony_cifrom mako.template import Template
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ciinstructions = parse_instructions(sys.argv[1], include_pseudo = True)
63bf215546Sopenharmony_ciir_instructions = partition_mnemonics(instructions)
64bf215546Sopenharmony_cimnemonics = set(x[1:] for x in instructions.keys())
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ciprint(Template(COPYRIGHT + TEMPLATE).render(ops = ir_instructions, mnemonics = mnemonics, instructions = instructions, typesize = typesize))
67