PageRenderTime 25ms CodeModel.GetById 11ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/security/nss/lib/freebl/mpi/types.pl

http://github.com/zpao/v8monkey
Perl | 162 lines | 94 code | 13 blank | 55 comment | 3 complexity | 95dd3e4fdfbb4e70ad72940af2a9527b MD5 | raw file
  1#!/usr/bin/perl
  2
  3#
  4# types.pl - find recommended type definitions for digits and words
  5#
  6# This script scans the Makefile for the C compiler and compilation
  7# flags currently in use, and using this combination, attempts to
  8# compile a simple test program that outputs the sizes of the various
  9# unsigned integer types, in bytes.  Armed with these, it finds all
 10# the "viable" type combinations for mp_digit and mp_word, where
 11# viability is defined by the requirement that mp_word be at least two
 12# times the precision of mp_digit.
 13#
 14# Of these, the one with the largest digit size is chosen, and
 15# appropriate typedef statements are written to standard output.
 16
 17# ***** BEGIN LICENSE BLOCK *****
 18# Version: MPL 1.1/GPL 2.0/LGPL 2.1
 19#
 20# The contents of this file are subject to the Mozilla Public License Version
 21# 1.1 (the "License"); you may not use this file except in compliance with
 22# the License. You may obtain a copy of the License at
 23# http://www.mozilla.org/MPL/
 24#
 25# Software distributed under the License is distributed on an "AS IS" basis,
 26# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 27# for the specific language governing rights and limitations under the
 28# License.
 29#
 30# The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
 31#
 32# The Initial Developer of the Original Code is
 33# Michael J. Fromberger <sting@linguist.dartmouth.edu>.
 34# Portions created by the Initial Developer are Copyright (C) 2000
 35# the Initial Developer. All Rights Reserved.
 36#
 37# Contributor(s):
 38#
 39# Alternatively, the contents of this file may be used under the terms of
 40# either the GNU General Public License Version 2 or later (the "GPL"), or
 41# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 42# in which case the provisions of the GPL or the LGPL are applicable instead
 43# of those above. If you wish to allow use of your version of this file only
 44# under the terms of either the GPL or the LGPL, and not to allow others to
 45# use your version of this file under the terms of the MPL, indicate your
 46# decision by deleting the provisions above and replace them with the notice
 47# and other provisions required by the GPL or the LGPL. If you do not delete
 48# the provisions above, a recipient may use your version of this file under
 49# the terms of any one of the MPL, the GPL or the LGPL.
 50#
 51# ***** END LICENSE BLOCK *****
 52
 53# $Id: types.pl,v 1.2 2005/02/02 22:28:22 gerv%gerv.net Exp $
 54#
 55
 56@_=split(/\//,$0);chomp($prog=pop(@_));
 57
 58# The array of integer types to be considered...
 59@TYPES = ( 
 60	   "unsigned char", 
 61	   "unsigned short", 
 62	   "unsigned int", 
 63	   "unsigned long"
 64);
 65
 66# Macro names for the maximum unsigned value of each type
 67%TMAX = ( 
 68	  "unsigned char"   => "UCHAR_MAX",
 69	  "unsigned short"  => "USHRT_MAX",
 70	  "unsigned int"    => "UINT_MAX",
 71	  "unsigned long"   => "ULONG_MAX"
 72);
 73
 74# Read the Makefile to find out which C compiler to use
 75open(MFP, "<Makefile") or die "$prog: Makefile: $!\n";
 76while(<MFP>) {
 77    chomp;
 78    if(/^CC=(.*)$/) {
 79	$cc = $1;
 80	last if $cflags;
 81    } elsif(/^CFLAGS=(.*)$/) {
 82	$cflags = $1;
 83	last if $cc;
 84    }
 85}
 86close(MFP);
 87
 88# If we couldn't find that, use 'cc' by default
 89$cc = "cc" unless $cc;
 90
 91printf STDERR "Using '%s' as the C compiler.\n", $cc;
 92
 93print STDERR "Determining type sizes ... \n";
 94open(OFP, ">tc$$.c") or die "$prog: tc$$.c: $!\n";
 95print OFP "#include <stdio.h>\n\nint main(void)\n{\n";
 96foreach $type (@TYPES) {
 97    printf OFP "\tprintf(\"%%d\\n\", (int)sizeof(%s));\n", $type;
 98}
 99print OFP "\n\treturn 0;\n}\n";
100close(OFP);
101
102system("$cc $cflags -o tc$$ tc$$.c");
103
104die "$prog: unable to build test program\n" unless(-x "tc$$");
105
106open(IFP, "./tc$$|") or die "$prog: can't execute test program\n";
107$ix = 0;
108while(<IFP>) {
109    chomp;
110    $size{$TYPES[$ix++]} = $_;
111}
112close(IFP);
113
114unlink("tc$$");
115unlink("tc$$.c");
116
117print STDERR "Selecting viable combinations ... \n";
118while(($type, $size) = each(%size)) {
119    push(@ts, [ $size, $type ]);
120}
121
122# Sort them ascending by size 
123@ts = sort { $a->[0] <=> $b->[0] } @ts;
124
125# Try all possible combinations, finding pairs in which the word size
126# is twice the digit size.  The number of possible pairs is too small
127# to bother doing this more efficiently than by brute force
128for($ix = 0; $ix <= $#ts; $ix++) {
129    $w = $ts[$ix];
130
131    for($jx = 0; $jx <= $#ts; $jx++) {
132	$d = $ts[$jx];
133
134	if($w->[0] == 2 * $d->[0]) {
135	    push(@valid, [ $d, $w ]);
136	}
137    }
138}
139
140# Sort descending by digit size
141@valid = sort { $b->[0]->[0] <=> $a->[0]->[0] } @valid;
142
143# Select the maximum as the recommended combination
144$rec = shift(@valid);
145
146printf("typedef %-18s mp_sign;\n", "char");
147printf("typedef %-18s mp_digit;  /* %d byte type */\n", 
148       $rec->[0]->[1], $rec->[0]->[0]);
149printf("typedef %-18s mp_word;   /* %d byte type */\n", 
150       $rec->[1]->[1], $rec->[1]->[0]);
151printf("typedef %-18s mp_size;\n", "unsigned int");
152printf("typedef %-18s mp_err;\n\n", "int");
153
154printf("#define %-18s (CHAR_BIT*sizeof(mp_digit))\n", "DIGIT_BIT");
155printf("#define %-18s %s\n", "DIGIT_MAX", $TMAX{$rec->[0]->[1]});
156printf("#define %-18s (CHAR_BIT*sizeof(mp_word))\n", "MP_WORD_BIT");
157printf("#define %-18s %s\n\n", "MP_WORD_MAX", $TMAX{$rec->[1]->[1]});
158printf("#define %-18s (DIGIT_MAX+1)\n\n", "RADIX");
159
160printf("#define %-18s \"%%0%dX\"\n", "DIGIT_FMT", (2 * $rec->[0]->[0]));
161
162exit 0;