1*35b53f8cSChandrakanth patil#!/usr/bin/env perl 2*35b53f8cSChandrakanth patil 3*35b53f8cSChandrakanth patil# This script cleans up the "official" Broadcom hsi_struct_defs.h file as distributed 4*35b53f8cSChandrakanth patil# to something somewhat more programmer friendly. 5*35b53f8cSChandrakanth patil# 6*35b53f8cSChandrakanth patil 7*35b53f8cSChandrakanth patilmy $do_decode = 0; 8*35b53f8cSChandrakanth patil 9*35b53f8cSChandrakanth patilif (! -f $ARGV[0]) { 10*35b53f8cSChandrakanth patil print "Input file not specified (should be path to hsi_struct_defs.h)\n"; 11*35b53f8cSChandrakanth patil exit 1; 12*35b53f8cSChandrakanth patil} 13*35b53f8cSChandrakanth patil 14*35b53f8cSChandrakanth patilif (!open(IN, "<", $ARGV[0])) { 15*35b53f8cSChandrakanth patil print "Failure to open input file\n"; 16*35b53f8cSChandrakanth patil exit 1; 17*35b53f8cSChandrakanth patil} 18*35b53f8cSChandrakanth patil 19*35b53f8cSChandrakanth patilif (!open(OUT, ">", "hsi_struct_def.h")) { 20*35b53f8cSChandrakanth patil print "Failure to open output file\n"; 21*35b53f8cSChandrakanth patil exit 1; 22*35b53f8cSChandrakanth patil} 23*35b53f8cSChandrakanth patil 24*35b53f8cSChandrakanth patil$/=undef; 25*35b53f8cSChandrakanth patilmy $header = <IN>; 26*35b53f8cSChandrakanth patilclose IN; 27*35b53f8cSChandrakanth patil 28*35b53f8cSChandrakanth patilprint OUT <<END_OF_NOTICE; 29*35b53f8cSChandrakanth patil/*- 30*35b53f8cSChandrakanth patil * BSD LICENSE 31*35b53f8cSChandrakanth patil * 32*35b53f8cSChandrakanth patil * Copyright (c) 2016 Broadcom, All Rights Reserved. 33*35b53f8cSChandrakanth patil * The term Broadcom refers to Broadcom Limited and/or its subsidiaries 34*35b53f8cSChandrakanth patil * 35*35b53f8cSChandrakanth patil * Redistribution and use in source and binary forms, with or without 36*35b53f8cSChandrakanth patil * modification, are permitted provided that the following conditions 37*35b53f8cSChandrakanth patil * are met: 38*35b53f8cSChandrakanth patil * * Redistributions of source code must retain the above copyright 39*35b53f8cSChandrakanth patil * notice, this list of conditions and the following disclaimer. 40*35b53f8cSChandrakanth patil * * Redistributions in binary form must reproduce the above copyright 41*35b53f8cSChandrakanth patil * notice, this list of conditions and the following disclaimer in 42*35b53f8cSChandrakanth patil * the documentation and/or other materials provided with the 43*35b53f8cSChandrakanth patil * distribution. 44*35b53f8cSChandrakanth patil * 45*35b53f8cSChandrakanth patil * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 46*35b53f8cSChandrakanth patil * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 47*35b53f8cSChandrakanth patil * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 48*35b53f8cSChandrakanth patil * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 49*35b53f8cSChandrakanth patil * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 50*35b53f8cSChandrakanth patil * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 51*35b53f8cSChandrakanth patil * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 52*35b53f8cSChandrakanth patil * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 53*35b53f8cSChandrakanth patil * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 54*35b53f8cSChandrakanth patil * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 55*35b53f8cSChandrakanth patil * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56*35b53f8cSChandrakanth patil */ 57*35b53f8cSChandrakanth patil 58*35b53f8cSChandrakanth patilEND_OF_NOTICE 59*35b53f8cSChandrakanth patil 60*35b53f8cSChandrakanth patil# Convert line endings 61*35b53f8cSChandrakanth patil$header =~ s/\r\n/\n/gs; 62*35b53f8cSChandrakanth patil 63*35b53f8cSChandrakanth patil# Convert arrays of two u32_t to a single uint64_t 64*35b53f8cSChandrakanth patil$header =~ s/\bu32_t(\s+[a-zA-Z0-9_]+)\[2\]/uint64_t$1/gs; 65*35b53f8cSChandrakanth patil 66*35b53f8cSChandrakanth patil# Convert uint32_t *_lo/uint32_t *_hi to a single uint64_t 67*35b53f8cSChandrakanth patil$header =~ s/\bu32_t(\s+[a-zA-Z0-9_]+)_lo;\s*\r?\n\s*u32_t(\s+[a-zA-Z0-9_]+)_hi/uint64_t$1/gs; 68*35b53f8cSChandrakanth patil 69*35b53f8cSChandrakanth patil# Convert types 70*35b53f8cSChandrakanth patil$header =~ s/\bu([0-9]+)_t\b/uint$1_t/gs; 71*35b53f8cSChandrakanth patil 72*35b53f8cSChandrakanth patil# Convert literals 73*35b53f8cSChandrakanth patil$header =~ s/\b((?:0x)?[0-9a-f]+)UL/UINT32_C($1)/gs; 74*35b53f8cSChandrakanth patil 75*35b53f8cSChandrakanth patil# Strip comments 76*35b53f8cSChandrakanth patil#$header =~ s/^(\s*[^\/\s][^\/]+?)\s*\/\*.*?\*\/\s*?$/$1/gm; 77*35b53f8cSChandrakanth patil#$header =~ s/[ \t]*\/\*.*?\*\/\s*?\n?//gs; 78*35b53f8cSChandrakanth patil 79*35b53f8cSChandrakanth patil# Pack structs 80*35b53f8cSChandrakanth patil#$header =~ s/}(\s+)([^\s]+_t[,;])/} __attribute__((packed))$1$2/gs; 81*35b53f8cSChandrakanth patil 82*35b53f8cSChandrakanth patil# Normalize indent 83*35b53f8cSChandrakanth patil$header =~ s/( ) +(#define)/$1$2/gs; 84*35b53f8cSChandrakanth patil$header =~ s/^(}[^\n]*;)\n([^\n])/$1\n\n$2/gsm; 85*35b53f8cSChandrakanth patil$header =~ s/([^\n])\n(typedef)/$1\n\n$2/gs; 86*35b53f8cSChandrakanth patil$header =~ s/ /\t/g; 87*35b53f8cSChandrakanth patil$header =~ s/ /\t/g; 88*35b53f8cSChandrakanth patil$header =~ s/([^\s]\t+) +/$1/g; 89*35b53f8cSChandrakanth patil 90*35b53f8cSChandrakanth patil# Remove typedefs and pack structs 91*35b53f8cSChandrakanth patil$header =~ s/^typedef struct (.*?)\n{\n(.*?)}[^\n]*;/struct $1 {\n$2} __attribute__((packed));/gsm; 92*35b53f8cSChandrakanth patil 93*35b53f8cSChandrakanth patilprint OUT $header; 94*35b53f8cSChandrakanth patilclose OUT; 95*35b53f8cSChandrakanth patil 96*35b53f8cSChandrakanth patilif ($do_decode) { 97*35b53f8cSChandrakanth patil if(!open(OUT, ">", "hsi_struct_decode.c")) { 98*35b53f8cSChandrakanth patil print "Failure to open decoder output file\n"; 99*35b53f8cSChandrakanth patil exit 1; 100*35b53f8cSChandrakanth patil } 101*35b53f8cSChandrakanth patil 102*35b53f8cSChandrakanth patil print OUT <<END_OF_NOTICE; 103*35b53f8cSChandrakanth patil /*- 104*35b53f8cSChandrakanth patil * BSD LICENSE 105*35b53f8cSChandrakanth patil * 106*35b53f8cSChandrakanth patil * Copyright (c) 2016 Broadcom, All Rights Reserved. 107*35b53f8cSChandrakanth patil * The term Broadcom refers to Broadcom Limited and/or its subsidiaries 108*35b53f8cSChandrakanth patil * 109*35b53f8cSChandrakanth patil * Redistribution and use in source and binary forms, with or without 110*35b53f8cSChandrakanth patil * modification, are permitted provided that the following conditions 111*35b53f8cSChandrakanth patil * are met: 112*35b53f8cSChandrakanth patil * * Redistributions of source code must retain the above copyright 113*35b53f8cSChandrakanth patil * notice, this list of conditions and the following disclaimer. 114*35b53f8cSChandrakanth patil * * Redistributions in binary form must reproduce the above copyright 115*35b53f8cSChandrakanth patil * notice, this list of conditions and the following disclaimer in 116*35b53f8cSChandrakanth patil * the documentation and/or other materials provided with the 117*35b53f8cSChandrakanth patil * distribution. 118*35b53f8cSChandrakanth patil * 119*35b53f8cSChandrakanth patil * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 120*35b53f8cSChandrakanth patil * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 121*35b53f8cSChandrakanth patil * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 122*35b53f8cSChandrakanth patil * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 123*35b53f8cSChandrakanth patil * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 124*35b53f8cSChandrakanth patil * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 125*35b53f8cSChandrakanth patil * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 126*35b53f8cSChandrakanth patil * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 127*35b53f8cSChandrakanth patil * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 128*35b53f8cSChandrakanth patil * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 129*35b53f8cSChandrakanth patil * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 130*35b53f8cSChandrakanth patil */ 131*35b53f8cSChandrakanth patil 132*35b53f8cSChandrakanth patilEND_OF_NOTICE 133*35b53f8cSChandrakanth patil 134*35b53f8cSChandrakanth patil if(!open(HDR, ">", "hsi_struct_decode.h")) { 135*35b53f8cSChandrakanth patil print "Failure to open decoder output header file\n"; 136*35b53f8cSChandrakanth patil exit 1; 137*35b53f8cSChandrakanth patil } 138*35b53f8cSChandrakanth patil 139*35b53f8cSChandrakanth patil print HDR <<END_OF_NOTICE; 140*35b53f8cSChandrakanth patil /*- 141*35b53f8cSChandrakanth patil * BSD LICENSE 142*35b53f8cSChandrakanth patil * 143*35b53f8cSChandrakanth patil * Copyright(c) 2014-2015 Broadcom Corporation. 144*35b53f8cSChandrakanth patil * All rights reserved. 145*35b53f8cSChandrakanth patil * 146*35b53f8cSChandrakanth patil * Redistribution and use in source and binary forms, with or without 147*35b53f8cSChandrakanth patil * modification, are permitted provided that the following conditions 148*35b53f8cSChandrakanth patil * are met: 149*35b53f8cSChandrakanth patil * 150*35b53f8cSChandrakanth patil * * Redistributions of source code must retain the above copyright 151*35b53f8cSChandrakanth patil * notice, this list of conditions and the following disclaimer. 152*35b53f8cSChandrakanth patil * * Redistributions in binary form must reproduce the above copyright 153*35b53f8cSChandrakanth patil * notice, this list of conditions and the following disclaimer in 154*35b53f8cSChandrakanth patil * the documentation and/or other materials provided with the 155*35b53f8cSChandrakanth patil * distribution. 156*35b53f8cSChandrakanth patil * * Neither the name of Broadcom Corporation nor the names of its 157*35b53f8cSChandrakanth patil * contributors may be used to endorse or promote products derived 158*35b53f8cSChandrakanth patil * from this software without specific prior written permission. 159*35b53f8cSChandrakanth patil * 160*35b53f8cSChandrakanth patil * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 161*35b53f8cSChandrakanth patil * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 162*35b53f8cSChandrakanth patil * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 163*35b53f8cSChandrakanth patil * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 164*35b53f8cSChandrakanth patil * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 165*35b53f8cSChandrakanth patil * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 166*35b53f8cSChandrakanth patil * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 167*35b53f8cSChandrakanth patil * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 168*35b53f8cSChandrakanth patil * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 169*35b53f8cSChandrakanth patil * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 170*35b53f8cSChandrakanth patil * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 171*35b53f8cSChandrakanth patil */ 172*35b53f8cSChandrakanth patil 173*35b53f8cSChandrakanth patilEND_OF_NOTICE 174*35b53f8cSChandrakanth patil 175*35b53f8cSChandrakanth patil print OUT "#ifdef HSI_DEBUG\n#include <inttypes.h>\n#include <rte_common.h>\n#include <rte_log.h>\n#include \"hsi_struct_def_dpdk.h\"\n#include \"hsi_struct_decode.h\"\n#include \"hsi_struct_decode.h\"\n\n"; 176*35b53f8cSChandrakanth patil print HDR "#ifdef HSI_DEBUG\n#include \"hsi_struct_def_dpdk.h\"\n\n"; 177*35b53f8cSChandrakanth patil 178*35b53f8cSChandrakanth patil my $hdr_defs = ''; 179*35b53f8cSChandrakanth patil 180*35b53f8cSChandrakanth patil sub print_single_val 181*35b53f8cSChandrakanth patil { 182*35b53f8cSChandrakanth patil my $field=shift; 183*35b53f8cSChandrakanth patil my $type=shift; 184*35b53f8cSChandrakanth patil my $max_field_len=shift; 185*35b53f8cSChandrakanth patil my $name = shift; 186*35b53f8cSChandrakanth patil my $macroshash = shift; 187*35b53f8cSChandrakanth patil my %macros = %$macroshash; 188*35b53f8cSChandrakanth patil $macrosref = shift; 189*35b53f8cSChandrakanth patil my @macros = @$macrosref; 190*35b53f8cSChandrakanth patil $macrosref = shift; 191*35b53f8cSChandrakanth patil my @fields = @$macrosref; 192*35b53f8cSChandrakanth patil 193*35b53f8cSChandrakanth patil if ($type eq 'uint32_t') { 194*35b53f8cSChandrakanth patil printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%08\"PRIX32\"\\n\", data->$field);\n",$max_field_len,$field; 195*35b53f8cSChandrakanth patil } 196*35b53f8cSChandrakanth patil elsif ($type eq 'uint16_t') { 197*35b53f8cSChandrakanth patil printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%04\"PRIX16\"\\n\", data->$field);\n",$max_field_len,$field; 198*35b53f8cSChandrakanth patil } 199*35b53f8cSChandrakanth patil elsif ($type eq 'uint8_t') { 200*35b53f8cSChandrakanth patil printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%02\"PRIX8\"\\n\", data->$field);\n",$max_field_len,$field; 201*35b53f8cSChandrakanth patil } 202*35b53f8cSChandrakanth patil elsif ($type eq 'uint64_t') { 203*35b53f8cSChandrakanth patil printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%016\"PRIX64\"\\n\", data->$field);\n",$max_field_len,$field; 204*35b53f8cSChandrakanth patil } 205*35b53f8cSChandrakanth patil elsif ($type eq 'char') { 206*35b53f8cSChandrakanth patil if ($field =~ s/\[([0-9]+)\]//) { 207*35b53f8cSChandrakanth patil printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = \\\"%%.$1s\\\"\\n\", data->$field);\n",$max_field_len,$field; 208*35b53f8cSChandrakanth patil } 209*35b53f8cSChandrakanth patil else { 210*35b53f8cSChandrakanth patil printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%02\"PRIX8\"\\n\", data->$field);\n",$max_field_len,$field; 211*35b53f8cSChandrakanth patil } 212*35b53f8cSChandrakanth patil } 213*35b53f8cSChandrakanth patil else { 214*35b53f8cSChandrakanth patil print "Unhandled type: '$type'\n"; 215*35b53f8cSChandrakanth patil } 216*35b53f8cSChandrakanth patil 217*35b53f8cSChandrakanth patil my $macro_prefix = uc($name).'_'.uc($field).'_'; 218*35b53f8cSChandrakanth patil # Special handling for the common flags_type field 219*35b53f8cSChandrakanth patil $macro_prefix =~ s/FLAGS_TYPE_$/FLAGS_/ if ($field eq 'flags_type'); 220*35b53f8cSChandrakanth patil # Special handling for _hi types 221*35b53f8cSChandrakanth patil $macro_prefix =~ s/_HI_/_/ if ($name =~ /_hi$/); 222*35b53f8cSChandrakanth patil 223*35b53f8cSChandrakanth patil $macro_prefix =~ s/\[[0-9]+\]//; 224*35b53f8cSChandrakanth patil my %vmacros; 225*35b53f8cSChandrakanth patil my $vmacros_have_mask = 0; 226*35b53f8cSChandrakanth patil my @vmacros; 227*35b53f8cSChandrakanth patil my %subfields; 228*35b53f8cSChandrakanth patil my $all_single_bits=1; 229*35b53f8cSChandrakanth patil MACRO: 230*35b53f8cSChandrakanth patil foreach my $macro (@macros) { 231*35b53f8cSChandrakanth patil if ($macro =~ /^$macro_prefix(.*)_MASK$/) { 232*35b53f8cSChandrakanth patil my $macro = $&; 233*35b53f8cSChandrakanth patil my $maskdef = $macros{$macro}; 234*35b53f8cSChandrakanth patil my $subfield = $1; 235*35b53f8cSChandrakanth patil my $subfield_value = "(data->$field & $macro)"; 236*35b53f8cSChandrakanth patil if (defined $macros{"$macro_prefix$subfield\_SFT"}) { 237*35b53f8cSChandrakanth patil $subfield_value = "($subfield_value >> $macro_prefix$subfield\_SFT)"; 238*35b53f8cSChandrakanth patil } 239*35b53f8cSChandrakanth patil $maskdef =~ s/[x0 ]//g; 240*35b53f8cSChandrakanth patil if ($type eq 'uint64_t') { 241*35b53f8cSChandrakanth patil printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s $subfield = %%0*\" PRIX64 \"\\n\", %u, $subfield_value);\n", $max_field_len, '', length($maskdef); 242*35b53f8cSChandrakanth patil } 243*35b53f8cSChandrakanth patil else { 244*35b53f8cSChandrakanth patil printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s $subfield = %%0*X\\n\", %u, $subfield_value);\n", $max_field_len, '', length($maskdef); 245*35b53f8cSChandrakanth patil } 246*35b53f8cSChandrakanth patil delete $$macroshash{$macro}; 247*35b53f8cSChandrakanth patil } 248*35b53f8cSChandrakanth patil elsif ($macro =~ /^$macro_prefix(.*)_SFT$/) { 249*35b53f8cSChandrakanth patil delete $$macroshash{$macro}; 250*35b53f8cSChandrakanth patil } 251*35b53f8cSChandrakanth patil elsif ($macro =~ /^$macro_prefix\MASK$/) { 252*35b53f8cSChandrakanth patil $vmacros_have_mask = 1; 253*35b53f8cSChandrakanth patil delete $$macroshash{$macro}; 254*35b53f8cSChandrakanth patil } 255*35b53f8cSChandrakanth patil elsif ($macro =~ /^$macro_prefix(.*)$/) { 256*35b53f8cSChandrakanth patil my $macro = $&; 257*35b53f8cSChandrakanth patil my $subfield = $1; 258*35b53f8cSChandrakanth patil 259*35b53f8cSChandrakanth patil # Check for longer fields with the same base... ie: link and link_speed 260*35b53f8cSChandrakanth patil foreach my $extra_field (@fields) { 261*35b53f8cSChandrakanth patil next if ($extra_field eq $field); 262*35b53f8cSChandrakanth patil if ($extra_field =~ /^$field/) { 263*35b53f8cSChandrakanth patil my $extra_prefix = uc($name).'_'.uc($extra_field).'_'; 264*35b53f8cSChandrakanth patil next MACRO if ($macro =~ /^$extra_prefix/); 265*35b53f8cSChandrakanth patil } 266*35b53f8cSChandrakanth patil } 267*35b53f8cSChandrakanth patil 268*35b53f8cSChandrakanth patil push @vmacros, $macro; 269*35b53f8cSChandrakanth patil my $macroeval = $macros{$macro}; 270*35b53f8cSChandrakanth patil $macroeval =~ s/UINT32_C\((.*?)\)/$1/g; 271*35b53f8cSChandrakanth patil $vmacros{$macro} = eval("$macroeval"); 272*35b53f8cSChandrakanth patil $subfields{$macro} = $subfield; 273*35b53f8cSChandrakanth patil 274*35b53f8cSChandrakanth patil $all_single_bits = 0 if ($vmacros{$macro} & ($vmacros{$macro}-1)); 275*35b53f8cSChandrakanth patil $all_single_bits = 0 if ($vmacros{$macro} == 0); 276*35b53f8cSChandrakanth patil } 277*35b53f8cSChandrakanth patil } 278*35b53f8cSChandrakanth patil if ($all_single_bits) { 279*35b53f8cSChandrakanth patil foreach my $macro (@vmacros) { 280*35b53f8cSChandrakanth patil my $subfield_value = "(data->$field & $macro)"; 281*35b53f8cSChandrakanth patil printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s $subfields{$macro} : %%s\\n\", $subfield_value?\"ON\":\"OFF\");\n", $max_field_len, ''; 282*35b53f8cSChandrakanth patil delete $$macroshash{$macro}; 283*35b53f8cSChandrakanth patil } 284*35b53f8cSChandrakanth patil } 285*35b53f8cSChandrakanth patil else { 286*35b53f8cSChandrakanth patil printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s Value : %%s\\n\",\n", $max_field_len, ''; 287*35b53f8cSChandrakanth patil foreach my $macro (@vmacros) { 288*35b53f8cSChandrakanth patil my $subfield_value = "data->$field"; 289*35b53f8cSChandrakanth patil $subfield_value = "($subfield_value & $macro_prefix\MASK)" if $vmacros_have_mask; 290*35b53f8cSChandrakanth patil print OUT "\t\t$subfield_value == $macro ? \"$subfields{$macro}\" :\n"; 291*35b53f8cSChandrakanth patil delete $$macroshash{$macro}; 292*35b53f8cSChandrakanth patil } 293*35b53f8cSChandrakanth patil print OUT "\t\t\"Unknown\");\n"; 294*35b53f8cSChandrakanth patil } 295*35b53f8cSChandrakanth patil } 296*35b53f8cSChandrakanth patil 297*35b53f8cSChandrakanth patil while ($header =~ /^typedef\s+struct\s+(.*?)\s+{(.*?)^}/msg) { 298*35b53f8cSChandrakanth patil my ($name,$def) = ($1, $2); 299*35b53f8cSChandrakanth patil my @fields=(); 300*35b53f8cSChandrakanth patil my %type=(); 301*35b53f8cSChandrakanth patil my @macros=(); 302*35b53f8cSChandrakanth patil my %macros=(); 303*35b53f8cSChandrakanth patil my $max_field_len=0; 304*35b53f8cSChandrakanth patil 305*35b53f8cSChandrakanth patil # First, pull out all the fields in order... 306*35b53f8cSChandrakanth patil while($def =~ /^\s*([^\s#\/]+?)\s+([^;\/\s]+?)\s*;/mg) { 307*35b53f8cSChandrakanth patil my ($type, $name) = ($1, $2); 308*35b53f8cSChandrakanth patil push @fields, $name; 309*35b53f8cSChandrakanth patil $type{$name}=$type; 310*35b53f8cSChandrakanth patil $max_field_len = length($name) if length($name) > $max_field_len; 311*35b53f8cSChandrakanth patil } 312*35b53f8cSChandrakanth patil # Now, pull out the macros... 313*35b53f8cSChandrakanth patil while($def =~ /^\s*\#define\s+([^\s]+?)\s+(.*?)\s*$/mg) { 314*35b53f8cSChandrakanth patil push @macros, $1; 315*35b53f8cSChandrakanth patil $macros{$1}=$2; 316*35b53f8cSChandrakanth patil } 317*35b53f8cSChandrakanth patil 318*35b53f8cSChandrakanth patil # Now, generate code to print the struct... 319*35b53f8cSChandrakanth patil print OUT "void decode_$name(const char *string __rte_unused, struct $name *data) {\n\tRTE_LOG(DEBUG, PMD, \"$name\\n\");\n"; 320*35b53f8cSChandrakanth patil print HDR "void decode_$name(const char *string __rte_unused, struct $name *data);\n"; 321*35b53f8cSChandrakanth patil $hdr_defs .= "#define decode_$name(x, y) {}\n"; 322*35b53f8cSChandrakanth patil foreach my $field (@fields) { 323*35b53f8cSChandrakanth patil if ($field =~ /\[([0-9]+)\]/) { 324*35b53f8cSChandrakanth patil if ($type{$field} eq 'char') { 325*35b53f8cSChandrakanth patil print_single_val($field, $type{$field}, $max_field_len, $name, \%macros, \@macros, \@fields); 326*35b53f8cSChandrakanth patil } 327*35b53f8cSChandrakanth patil else { 328*35b53f8cSChandrakanth patil foreach my $idx (0..$1-1) { 329*35b53f8cSChandrakanth patil my $item = $field; 330*35b53f8cSChandrakanth patil $item =~ s/\[[0-9]+\]/[$idx]/; 331*35b53f8cSChandrakanth patil print_single_val($item, $type{$field}, $max_field_len, $name, \%macros, \@macros, \@fields); 332*35b53f8cSChandrakanth patil } 333*35b53f8cSChandrakanth patil } 334*35b53f8cSChandrakanth patil } 335*35b53f8cSChandrakanth patil else { 336*35b53f8cSChandrakanth patil print_single_val($field, $type{$field}, $max_field_len, $name, \%macros, \@macros, \@fields); 337*35b53f8cSChandrakanth patil } 338*35b53f8cSChandrakanth patil } 339*35b53f8cSChandrakanth patil # print "Unhandled macros:\n",join("\n", keys %macros),"\n" if (keys %macros > 0); 340*35b53f8cSChandrakanth patil print OUT "}\n\n"; 341*35b53f8cSChandrakanth patil } 342*35b53f8cSChandrakanth patil print OUT "#endif\n"; 343*35b53f8cSChandrakanth patil 344*35b53f8cSChandrakanth patil print HDR "#else\n"; 345*35b53f8cSChandrakanth patil print HDR $hdr_defs; 346*35b53f8cSChandrakanth patil print HDR "#endif\n"; 347*35b53f8cSChandrakanth patil close OUT; 348*35b53f8cSChandrakanth patil close HDR; 349*35b53f8cSChandrakanth patil} 350