Path: blob/21.2-virgl/src/freedreno/isa/decode.py
4564 views
#1# Copyright © 2020 Google, Inc.2#3# Permission is hereby granted, free of charge, to any person obtaining a4# copy of this software and associated documentation files (the "Software"),5# to deal in the Software without restriction, including without limitation6# the rights to use, copy, modify, merge, publish, distribute, sublicense,7# and/or sell copies of the Software, and to permit persons to whom the8# Software is furnished to do so, subject to the following conditions:9#10# The above copyright notice and this permission notice (including the next11# paragraph) shall be included in all copies or substantial portions of the12# Software.13#14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20# IN THE SOFTWARE.2122from mako.template import Template23from isa import ISA24import sys2526template = """\27/* Copyright (C) 2020 Google, Inc.28*29* Permission is hereby granted, free of charge, to any person obtaining a30* copy of this software and associated documentation files (the "Software"),31* to deal in the Software without restriction, including without limitation32* the rights to use, copy, modify, merge, publish, distribute, sublicense,33* and/or sell copies of the Software, and to permit persons to whom the34* Software is furnished to do so, subject to the following conditions:35*36* The above copyright notice and this permission notice (including the next37* paragraph) shall be included in all copies or substantial portions of the38* Software.39*40* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR41* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,42* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL43* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER44* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING45* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS46* IN THE SOFTWARE.47*/4849#include "decode.h"5051/*52* enum tables, these don't have any link back to other tables so just53* dump them up front before the bitset tables54*/5556%for name, enum in isa.enums.items():57static const struct isa_enum ${enum.get_c_name()} = {58.num_values = ${len(enum.values)},59.values = {60% for val, display in enum.values.items():61{ .val = ${val}, .display = "${display}" },62% endfor63},64};65%endfor6667/*68* generated expression functions, can be linked from bitset tables, so69* also dump them up front70*/7172%for name, expr in isa.expressions.items():73static uint64_t74${expr.get_c_name()}(struct decode_scope *scope)75{76% for fieldname in expr.fieldnames:77int64_t ${fieldname} = isa_decode_field(scope, "${fieldname}");78% endfor79return ${expr.expr};80}81%endfor8283/*84* Forward-declarations (so we don't have to figure out which order to85* emit various tables when they have pointers to each other)86*/8788%for name, bitset in isa.bitsets.items():89static const struct isa_bitset bitset_${bitset.get_c_name()};90%endfor9192%for root_name, root in isa.roots.items():93const struct isa_bitset *${root.get_c_name()}[];94%endfor9596/*97* bitset tables:98*/99100%for name, bitset in isa.bitsets.items():101% for case in bitset.cases:102% for field_name, field in case.fields.items():103% if field.get_c_typename() == 'TYPE_BITSET':104% if len(field.params) > 0:105static const struct isa_field_params ${case.get_c_name()}_${field.get_c_name()} = {106.num_params = ${len(field.params)},107.params = {108% for param in field.params:109{ .name= "${param[0]}", .as = "${param[1]}" },110% endfor111112},113};114% endif115% endif116% endfor117static const struct isa_case ${case.get_c_name()} = {118% if case.expr is not None:119.expr = &${isa.expressions[case.expr].get_c_name()},120% endif121% if case.display is not None:122.display = "${case.display}",123% endif124.num_fields = ${len(case.fields)},125.fields = {126% for field_name, field in case.fields.items():127{ .name = "${field_name}", .low = ${field.low}, .high = ${field.high},128% if field.expr is not None:129.expr = &${isa.expressions[field.expr].get_c_name()},130% endif131% if field.display is not None:132.display = "${field.display}",133% endif134.type = ${field.get_c_typename()},135% if field.get_c_typename() == 'TYPE_BITSET':136.bitsets = ${isa.roots[field.type].get_c_name()},137% if len(field.params) > 0:138.params = &${case.get_c_name()}_${field.get_c_name()},139% endif140% endif141% if field.get_c_typename() == 'TYPE_ENUM':142.enums = &${isa.enums[field.type].get_c_name()},143% endif144% if field.get_c_typename() == 'TYPE_ASSERT':145.val = ${field.val},146% endif147},148% endfor149},150};151% endfor152static const struct isa_bitset bitset_${bitset.get_c_name()} = {153<% pattern = bitset.get_pattern() %>154% if bitset.extends is not None:155.parent = &bitset_${isa.bitsets[bitset.extends].get_c_name()},156% endif157.name = "${name}",158.gen = {159.min = ${bitset.gen_min},160.max = ${bitset.gen_max},161},162.match = ${hex(pattern.match)},163.dontcare = ${hex(pattern.dontcare)},164.mask = ${hex(pattern.mask)},165.num_cases = ${len(bitset.cases)},166.cases = {167% for case in bitset.cases:168&${case.get_c_name()},169% endfor170},171};172%endfor173174/*175* bitset hierarchy root tables (where decoding starts from):176*/177178%for root_name, root in isa.roots.items():179const struct isa_bitset *${root.get_c_name()}[] = {180% for leaf_name, leaf in isa.leafs.items():181% if leaf.get_root() == root:182&bitset_${leaf.get_c_name()},183% endif184% endfor185(void *)0186};187%endfor188189"""190191xml = sys.argv[1]192dst = sys.argv[2]193194isa = ISA(xml)195196with open(dst, 'wb') as f:197f.write(Template(template, output_encoding='utf-8').render(isa=isa))198199200