Hello all,

Paul Fox wrote:
>  > is there a way in sdcc to see the memory requirements of individual
>  > object files?  i'm thinking of the moral equivalent of "size *.o",
> 
> there was no response to the above, so i've put together a script that
> attempts to extract this information from the .rel files for our 8051
> project.

Slightly old thread now but I found it useful (many thanks again, Paul) and 
I've made some modifications. The modified script, tested on unbuntu 10.10 and 
MAC OS X 10.6.6, is inline after the change list:

- The script now takes the project's map file as the 1st argument in the 
command line. It parses it and extracts the paths/names of modules which are 
being linked in (instead of brute force parsing obj/*.rel). This is important 
in cases when we have files which are being compiled but not linked.

- It now supports named code segments when building with banking support. The 
total code size reported is the sum of code found in any of the segments listed 
in the code_segs variable, rather than only in CSEG. To add/rename code 
segments, just change the values inside $code_segs

- Similar for XRAM segments, it sums up variables assigned to XISEG in addition 
to XSEG (listed in $mem_segs)

- If we have built sdcc from sources, we have standard library rel files in the 
build dir (one directory with rel files for each memory model / stack-auto / 
xstack combo). The script will optionally parse those and include them in the 
calculation. In order to turn this on, simply set $sdcc_src to the location of 
your SDCC sources. Then change $libdir to correspond to the memory model used 
for your builds

- Slightly better portability (mainly related to different versions of sed and 
awk - see inline)

========
#!/bin/bash

# Copyright 2010 One Laptop per Child
# License information below.

# produce a listing of the code and initialized constant sizes
# for the all the obj/*.rel files produced by sdcc.
# requires xargs, cut, sed, awk

me=${0##*/}

tmpfile="/tmp/$me.$$"
code_segs=( CSEG HOME BANK1 BANK2 BANK3 XINIT )
mem_segs=( XSEG XISEG )

# gawk refuses to sum up hex values unless we pass --posix
# Try 'awk' first. If that doesn't work, experiment.
AWK=awk
#AWK="gawk --posix"
#AWK=mawk

# If we want to parse SDCC's library rel files, make sure to set SDCC_SRC below
# correctly. Also, we need to change huge-stack-auto to correspond to the memory
# model used for our particular build.
# If we don't care about libraries, just ignore both steps above
sdcc_src=$HOME/svn-working-copies/sdcc
libdir=${sdcc_src}/device/lib/huge-stack-auto

trap "rm -f $tmpfile" 0

main()
{
  # make sure sort puts files starting with _ at the start
  export LC_COLLATE=C
  pwd
  date
  echo $tmpfile
  
  build_dir=$(dirname $1)
  files=( $( grep .rel $1 | $AWK '{print $(NF-1)}' | sort ) )

  printf " %50s %7s %7s %7s %7s\n" CODE XRAM CONST DATA BITS
  listsizes | tee $tmpfile

  echo
  echo "-- Totals (see .mem for more accurate info) --"

  echo -en " CODE:\t"
  sumcol 2 < $tmpfile

  echo -en " XRAM:\t"
  sumcol 3 < $tmpfile
  
  echo -en "CONST:\t"
  sumcol 4 < $tmpfile
  
  echo -en " DATA:\t"
  sumcol 5 < $tmpfile

  echo -en " BITS:\t"
  sumcol 6 < $tmpfile
  echo " (Each file with an ISR increments the reported BITS value by 1)"
}

listsizes()
{
  for rel_file in ${files[@]}
  do
    x="$build_dir/$rel_file"
    if [ ! -f $x ] ; then x="$libdir/$rel_file"; fi
    if [ -f $x ]
    then
      (
        # Output our filename
        echo ${x##*/}:
        
        # Sum up all the CODE segments
        reg=""
        for seg in ${code_segs[@]}
          do reg="$reg|$seg"
        done 
        egrep -w ${reg:1} $x |
          sed -E -e 's/A //' \
                 -e 's/size //' \
                 -e 's/ flags.*//' \
                 -e "s/${reg:1}//" \
                 -e 's/[0-9A-F]+/0x&/' |
          $AWK '{ SUM += $1 } END { print SUM }'

        # Do the same for mem segments
        reg=""
        for seg in ${mem_segs[@]}
          do reg="$reg|$seg"
        done 
        egrep -w ${reg:1} $x |
          sed -E -e 's/A //' \
                 -e 's/size //' \
                 -e 's/ flags.*//' \
                 -e "s/${reg:1}//" \
                 -e 's/[0-9A-F]+/0x&/' |
          $AWK '{ SUM += $1 } END { print SUM }'

        # CONST, DATA, BITS
        (
          egrep -w 'CONST' $x
          egrep -w 'DSEG' $x
          egrep -w 'BIT_BANK' $x
        ) |  
        sed -E -e 's/A //' \
               -e 's/size //' \
               -e 's/ flags.*//' \
               -e "s/CONST|DSEG|BIT_BANK//" \
               -e 's/[0-9A-F]+/0x&/'
      ) |
      xargs -n 6 printf "%-43s %7d %7d %7d %7d %7d\n"
    fi 
  done
}

# tally the Nth field of the input
sumcol()
{
  $AWK '
    BEGIN   { total = 0; }
            { total += $'$1'}
    END     { print total; }
  '
}

if [ $# != 1 ] ; then echo "Please supply a .map filename as argument."; exit ; 
fi

main $1

exit

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   In other words, you are welcome to use, share and improve this program.
   You are forbidden to forbid anyone else to use, share and improve
   what you give them.   Help stamp out software-hoarding!

   As a special exception, you may use this file as part of a free software
   library for the XO of the One Laptop per Child project without restriction.
   Specifically, if other files instantiate
   templates or use macros or inline functions from this file, or you compile
   this file and link it with other files to produce an executable, this
   file does not by itself cause the resulting executable to be covered by
   the GNU General Public License.  This exception does not however
   invalidate any other reasons why the executable file might be covered by
   the GNU General Public License.



------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to