Вы находитесь на странице: 1из 29

Perl and Tcl Training

June 28, 2011


Mark Clark

Goals of this class


Provide a starting point for your endeavors in becoming proficient
with perl and tcl.
Further

study and hands-on experience will be required

Provide a context of how we use perl and tcl on GT projects


Vendor
Many

tool APIs are largely in tcl

of the tools and flows you will use or contribute to are written in

perl

Quick

fixes to immediate problems can often be solved by writing a short


perl script

Provide some basic software concepts that will stay with you long
after perl and tcl are historical footnotes.
Next

year may be writing all python code

Microprocessor and Graphics Development - Intel Confidential

References
Larry Wall, Tom Christiansen & Jon Orwant, Programming
Perl, OReilly

Jarkko Hietaniemi, John Macdonald, Jon Orwant, Mastering


Algorithms with Perl, OReilly

Tom Christiansen & Nathan Torkington, Perl Cookbook, OReilly


Nancy Walsh, Perl/Tk, OReilly
Steve McConnell, Code Complete, A practical Handbook of Software
Construction, Microsoft

Brent B. Welch, Practical Programming in Tcl and Tk, Prentice Hall


solvenet.synopsys.com: Training tab>Tool and Methodology Training
> Tcl Training > :The Power of Tcl 1, Becoming a Proficient Tcl User

Microprocessor and Graphics Development - Intel Confidential

Perl personality
Perl has a laissez-faire attitude it pretty much lets you be
No

need to declare variables before you use them, but you can require
yourself to do so:
use strict;
It

tries to behave like a natural language ascertaining what you are trying
to do based on context
scalar (dealing with singular things)
$n = @net_names
list (dealing with plural things)
sort @net_names

Microprocessor and Graphics Development - Intel Confidential

Simple data structures

Type

Character

Example

Description

Scalar

$net_name

An individual number or
string

Array

@nets

A list of values with a


numeric index

Hash

%wire_length

A group of values keyed


by string

Subroutine

&

&find_net

A callable chunk of code

Typeglob

*.def

Everything ending in .def

Special characters are used to indicate the type of a variable.


$error = Net $net is floating\n; # String with interpolation
$error = Array @nets is empty; #String without interpolation
$cwd = `pwd`; #String set from an external command

Microprocessor and Graphics Development - Intel Confidential

Scalars can hold references to other data


structures
$nets = \@nets; # reference to a named array
$pins_of_net = \%wire_length; # reference to a named hash
$find_length = \&find_length; # reference to a named subroutine
$nets = [crclk, rst_a, rst_b]; # reference to an unnamed array
$wire_length= {crclk => 10000, rst_a => 5000, rst_b => 6000}; #
reference to an unnamed hash

$find_length = sub {return $wire_length->{$net}}; # reference to an


unnamed subroutine

$macro = new macro(a12xyz); # reference to an object

Microprocessor and Graphics Development - Intel Confidential

Multi-Valued variables

# hash giving macros of components


my %macro;
my $macro_ref = \%macro;

my @COMPONENTS; # array of components


Index

Value

Key

Value

ckfub1_fiducial_bl

ckfub1_fiducial_bl

R63C4FID

ckfub1_fiducial_tr

ckfub1_fiducial_tr

R63C4FID

:
$k

$components[$k]
crst2d1/tvfunit1/cache0

crsthsiopxp1/dt_scan_buf

$macro{$comp}

$comp
:

:
$n-1

ram64x256
$macro_ref->{$comp}

crsthsiopxp1/dt_scan_buf

R63BF00HX05

# You can assign a hash as follows:


%macro = (
ckfub1_fiducial_bl => R63C4FIC,
ckfub1_fiducial_tr => R63C4FIC,
crsthsiopxp1/dt_scan_buf => R63BF00HX05
);
#You can get a list of keys as follows:
@comp = keys %macro;

Microprocessor and Graphics Development - Intel Confidential

Compound data structures


(x2, y2)

rectangle
# array of hashes
my @rects; # array of rectangles
my $rects_ref = \@rects
Index

(x1, y1)

Value (scalar reference to a hash)

{x1 => 0, y1 => 0, x2 => 100, y2 => 100}

{x1 => 10, y1 => 20, x2 => 1000, y2 => 1000}

:
$k

$rects[$k]{y2}
{x1 => 1000, y1 => 1000, x2 => 2000, y2 => 2000}

:
$n-1

$rects_ref->[$k]{y2}
{x1 => 2000, y1 => 1000, x2 => 2100, y2 => 2000}

Microprocessor and Graphics Development - Intel Confidential

Linked list using hashes

first

data => {}
n ext

data => {}
n ext

null

Operations: (Which are harder with arrays, which easier?)


add
delete
insert
access
find
Similarly a binary tree can be
implemented for rapid
insertion and access to
ordered data.

Microprocessor and Graphics Development - Intel Confidential

Running perl
On the command line:
perl -e print Hello world\n;
Create a file, make it executable and run:
xemacs hello_world.pl &
#!/usr/intel/bin/perl
print Hello world\n;
chmod +x hello_world.pl

10

Microprocessor and Graphics Development - Intel Confidential

Program structure
Create a template for a main program

Create a template for modules


package module_template;
require 5.003;
require Exporter;
@ISA=qw(Exporter);

#!/usr/intel/bin/perl
require 5;
<header>
<modules>
use module_template;

use strict;

<user inputs>
<log set up>
<banner>
my $object_ref = new module_template($opt_file, $log);
<clean up>
############ end of main ################
sub usage {
:
}
####### end of main_template.pl ##########

################################
#
# Author:
Mark Clark
# Date:
June 27, 2011
# Description: Perl module template.
#
##################################
##################################
#Data Members:
#
# 'file': dummy input
# 'log':
log object
#
##############################
####### public routines #########
sub new {
my $class = shift;
my $file = shift;
my $log = shift;

scripts
bin

my $self = {
'file' => $file,
'log' => $log
};
bless($self, $class);
return $self;

lib
}

######## private routines #########


######## static routines ###########
1;

11

Microprocessor and Graphics Development - Intel Confidential

Program structure
Parts of typical template
#!/usr/intel/bin/perl
require 5;
<header>
<modules>
use module_template;
<user inputs>
<log set up>
<banner>

<header>
##########################################################
#
# Program:
main_template.tcl
# Author:
Mark Clark
# Date:
June 27, 2011
# Description: Template for a main program.
##########################################################
#
use strict;

<modules>

my $object_ref = new module_template($opt_file, $log);


<clean up>
############ end of main ################

se FindBin qw($Bin);
use lib "$Bin/../lib";
use Getopt::Long;
use File::Path;
use File::Copy;
use LogData;
use std;

sub usage {
:
}
####### end of main_template.pl ##########

<user inputs>

<banner>

#Get user inputs


my ($opt_h, $opt_file);
my $n_args = $#ARGV+1;
usage() if ( !GetOptions("h" => \$opt_h,
"file:s" => \$opt_file
));
usage() if ($opt_h);
usage() if (!$opt_file);

#Output program Header


my $script=$0;
$script=~s/.*?(\w+\.pl)/$1/;
$log->info_msg(
$log->info_msg(
$log->info_msg(
$log->info_msg(
$log->info_msg(

12

"------------------------------------\n");
"
$script executing\n");
"
Author: Mark Clark, Intel
\n");
"------------------------------------\n");
"
$n_args arg(s) processed\n\n");

<log set up>


if (! -e "logs") {
mkpath(["logs"], 0, 0777);
}
my $log = new LogData();
$log->global_start_msg();

Microprocessor and Graphics Development - Intel Confidential

Program structure (Cont)


Parts of typical template

<clean up>

#!/usr/intel/bin/perl

$log->global_end_msg();
$log->info_msg ("All Done\n");

require 5;

$log->move_log("logs");

<header>
<modules>
use module_template;

my $log_file = $log->{'log_file'};
my $log_copied = copy($log_file, "main_template.log");

<user inputs>
<log set up>
<banner>
my $object_ref = new module_template($opt_file, $log);
<clean up>
############ end of main ################

<usage>
sub usage {
print "
Usage:
$0 -file <file> [-h]
Where:
-file

Replace this option with your actual

-h

Help message and exit

sub usage {
:
}

options

####### end of main_template.pl ##########

Description:

Template for a main program.

";
exit (0);
}

13

Microprocessor and Graphics Development - Intel Confidential

Lab 1: 15 minutes
Set up:
1. Create a work area: e.g. mkdir ${USER}_perl_tcl_lab
2. cd ${USER}_perl_tcl_lab
3. Copy lab collateral: cp /nfs/fm/disks/fm_cse_n71105/quantum/perl_tcl_lab/* .
4. Follow instructions in perl_tcl_lab.txt

14

Microprocessor and Graphics Development - Intel Confidential

Filehandles
Another of perls data types is the filehandle which is the name you
give to a file, device, socket or pipe

Use open to create filehandles


open(DEF,

gt.def) # Read from an existing file

open(DEF,

<gt.def) #Explicitly read from an existing file

open(DEF,

>gt_merged.def) # Create file and write to it

open(DEF,

>>gt_merged.def) # Append to an existing file

You read from the handle using the reading operator, <>
You can set the record delimiter:
my
$/

15

$save_delim = $/;

= "\n";

$/ = $save_delim;

# restore delimiter

Microprocessor and Graphics Development - Intel Confidential

Parsing

Static function from std package for


opening a possibly zipped file for read

Typical parsing code


my $fh = std::open_gz_in($filename);
$log->info_msg("Parsing $filename\n");
my $save_delim = $/;
$/ = "\n";
while ($line = <$fh>) {
chomp $line;
if($line =~ /^\s*MACRO\s+(\w+)\s*$/i) {
my $macro = $1;
$log->info_msg(Macro = $macro\n);
}
}
close $fh;
$/ = $save_delim; # restore delimiter

Chop off record


delimiter

package std;
:
sub open_gz_in {
my $file = shift;
my $gzfile = $file . ".gz";
local *FH;

Regular
expression

if ($file =~ /\.gz$/) {
open(*FH,"gzcat $file |") || read_death("$file");
} elsif(-e $file) {
open(*FH, "$file") || read_death("$file");
} elsif(-e $gzfile) {
open(*FH, "gzcat $gzfile |") || read_death("$file");
} else {
read_death("$file")
}

$log->info_msg("Done parsing $filename\n");

Calling info_msg public method from


$log object of class LogData

return *FH;
}

package LogData;
require 5.003;
require Exporter;
@ISA=qw(Exporter);
:
### Print an informational message to the local log and to the screen
###
sub info_msg {
my $self = shift;
my $msg = shift;
my $fh = $self->{'fh'};
if(defined $fh) {
print $fh "-I- INFO:
print $fh "$msg";
}
print "-I- INFO: ";
print "$msg";

";

}
16

Microprocessor and Graphics Development - Intel Confidential

Perl control structures


The if statement
if($line =~ /^\s*MACRO\s+(\w+)\s*$/i) {
my $macro = $1;
$log->info_msg(Macro = $macro\n);
} elsif(($line =~ /^\s*LAYER\s+(\w+)\s*$/i) {
my $layer = $1;
$log->info_msg(layer = $layer\n);
} else {
$log->error_msg(Bad line = $line\n);
}

The for statement


my $n = @macros;
for(my $i = 0; $i < $n; $i++) {
my $macro = $macros[$i];
print Macro = $macro\n;
}

17

The while statement


while ($line = <$fh>) {
chomp $line;
if($line =~ /^\s+$/) {
next;
}
if($line =~ /^\s*END\s*$/) {
last;
}
}

The foreach statement


foreach my $macro (@macros) {
print Macro = $macro\n;
}

Microprocessor and Graphics Development - Intel Confidential

Regular expressions
Regular expressions are used in pattern matching:
if($line =~ /MACRO/) {print Found a macro\n}
if($line !~ /MACRO/) {print Macro not found\n}
Substitution
$line =~ s/macro/MACRO/;
Splitting strings
@fields = split(/,/, $line);
Character class shortcuts
Name

Definition

Match

Dont
match

Whitespace

[ \t\n\r\f]

\s

\S

Word char.

[a-zA-Z_0-9]

\w

\W

[0-9]

\d

\D

Any char.

NA

Digit
Anything

18

Microprocessor and Graphics Development - Intel Confidential

Regular expressions (continued)


Quantifier example for matching 7 to 11 digits: \d{7,11}
Quantifier shortcuts
Name

Definition

Code

Example

Zero or more

{0,}

\w*

Zero or one

(0,1}

a?

One or more

{1,}

\d+

Anchors:
Match at word boundary: \b -- /\b\w+\b/
Match at beginning: ^ -- /^\w+/
Match at end: $ -- /\w+$/
Back references: Parentheses around a part of a pattern will cause matches to
that part to be stored in special variables, $1, $2,
Swap first two words of a string: s/(\S+)\s+(\S+)/$2 $1/

19

Microprocessor and Graphics Development - Intel Confidential

Perl objects
Object Oriented Programming (OOP)
Encapsulation: Combining a data structure with the functions dedicated to
manipulating the data. (class)
Inheritance: Building derived classes that inherit the data and functions from
previously defined base classes.
Polymorphism: Giving an action one name or symbol that is shared up and
down a class hierarchy. (e.g. computing area for rectangles, circles, etc.)
Perls Object System
An object is a scalar reference
A class is a package
A method is a subroutine

20

Microprocessor and Graphics Development - Intel Confidential

Creating a module for your class


package macros;
require 5.003;
require Exporter;
@ISA=qw(Exporter);

package used to define a class

use strict;

Inheritance list

################################
#
# Author:
Mark Clark
# Date:
June 27, 2011
# Description: macro class
#
##################################

Creating an object of the class:


my $cells = new macros($log)

##################################
#Data Members:
#
# 'file': dummy input
# 'log':
log object
#
##############################
####### public routines #########
sub new {
my $class = shift;
my $log = shift;
my $self = {
names' => {}, # macro names
'log' => $log
};
bless($self, $class);
return $self;
}
sub get_names {
return $self->{names};
}
######## private routines #########

Anonymous hash used


for objects data

Bless hash into


class.
Invoking a method:
my $names = $cells->get_names();

######## static routines ###########


1;
21

Microprocessor and Graphics Development - Intel Confidential

Comprehensive Perl Archive Network


(CPAN)
Search CPAN before creating your own module
www.cpan.org
man

22

perlmodinstall

Microprocessor and Graphics Development - Intel Confidential

Getting help
Use man or perldoc
Go to CPAN
Look at prior code for examples
e.g.

23

/p/cse/asic/quantum/6.0.a/bin (cp, dont modify)

Microprocessor and Graphics Development - Intel Confidential

Break: 5 minutes
Lab 2: 15 minutes

24

Microprocessor and Graphics Development - Intel Confidential

Your tcl code will mostly be run by a vendor


tool such as ICC
icc_shell -64bit -f scripts/flat_dbcreate_icc.tcl
icc_shell prompt> source

/p/cse/asic/quantum/6.0.a/tcl/stcl/addFillToLeftAndRightOfEbbs.tcl
braces for grouping
proc addFillToLeftAndRightOfEbbs { cell celledge power ground fill_area fill_hieght}
{
#--------------------------------------------------------------------------# Get all the embedded blocks.
#--------------------------------------------------------------------------set ebbs [filter_collection [get_fp_cells *] "is_hard_macro==true"]
set ebbs [filter_collection $ebbs "ref_name!=euunit"]
:
foreach_in_collection ebb $ebbs {
#----------------------------------------------------------------------# Get the ebb's information.
#----------------------------------------------------------------------set ebb_name [get_object_name $ebb]
:
}

square brackets perform


command substitution

dollar sign performs


variable substitution

Basic syntax:
command arg1 arg2 arg3
Shebang for stand-alone tcl program:
#!/usr/intel/bin/tclsh
25

Microprocessor and Graphics Development - Intel Confidential

Software development cycle


Rigor applied depends on size, complexity and importance
of the program
Configuration
Management

Requirements
Definition

Design

Code & debug

Maintenance
Maintenance

Maintain a suite of regression tests


Control changes
Provide documentation
26

rcs for files only you modify


cvs for a group situation

QA

Code reviews
Analyze test coverage and
create test cases
xemacs or vi
perl -d <full path>/<program>
-w option

Microprocessor and Graphics Development - Intel Confidential

Apply software standards

27

See Code Complete reference


Comment your code
Agree on styles -- indentation, white space, brace locations
Agree on naming conventions: <program>.pl,
<ModuleName>.pm
Modularize your code -- maximizes reuse and
maintainability
Dont cut and paste where subroutines can be employed
Use properly descriptive variable and subroutine names
Use strict
Avoid globals

Microprocessor and Graphics Development - Intel Confidential

Lab 3: 15 minutes

28

Microprocessor and Graphics Development - Intel Confidential

Q&A

Вам также может понравиться