1/10/2022

EXEC and EVAL in TCL


 What is EXEC ?

  • The exec command executes a system command(binary) from linux/unix while staying within TCLSH.
  • The exec command treats its arguments as the names and arguments for a set of programs to run.
  • In VLSI EDA Automation any external EDA-Software Binary can also be executed using exec while staying within the shell of Parent EDA Tool.
Simple Use of EXEC :

First we will understand the usage of exec w.r.to to basic TCL Language usage through an example.

We create a simple Hello World TCL script which we will use in the main script through exec.

 

Now , in the main code , we will use above mentioned TCL Code as well as few basic Linux binaries through exec as :

pwd , whoami and which .

Once executed this code gives the below output :


EDA Automation : Use of EXEC 

By now you are able to understand the basic use of exec. In this section we will extend the use of exec to a situation similar to the EDA Automation use of exec.

When a TCL Script is executed in the EDA Tool Shell , it may encounter that sudden ERROR or WARNING has occurred. These two may crash the script. For better programming , your intent will always be to capture such error or warning without crashing the script. To serve this purpose we will use another TCL procedure "catch" which evaluates a TCL script snippet inside it and trap exceptional returns . Thus an actual ERROR or WARNING can be intervened in a sand-box mode and without its bare execution which can lead to a crash.

Let us exemplify the mentioned use through the below script :



Upon execution this script gives  the below output :


 

First If-Else block :

The first if-block in the above code is used to capture the failure (if any by error/warning) . So, if there is no failure while the if-condition is checked , the code execution will move over to the else block. Here we do the successful code execution through the else block. Here intentionally the execution code is set to success for the if-block.

Second If-Else block :

While in the next if-else code block , the if condition is meant to fail i.e the real occurrence of error/warning . In this case code execution is limited to the if-block only and does not go to the else block at all , as it went in the previous if-else block. 

So our purpose is served i.e error is encountered and printed on the screen without the crashing of the code !

What is EVAL ?

  • Tcl allow new command creation and execute them while running a tcl script.
  • Typically A tcl command is defined as "a list of strings where the first string is a command or procedure".
  • Any string or list which meets this criteria can be evaluated and executed by the command eval.
  • In VLSI EDA Automation, you may club a set of command which you need to repeat several times in your EDA-Tool shell for design or analysis .

Simple Use of EVAL :

To use the eval command first , we have to set or define a short-hand for "a set of tcl commands". 

This we do as : set cmd {puts "Hello World !"}  

The cmd variable works like a MACRO in C-Language and this is our shorthand . We can execute this MACRO afterwards as :  eval $cmd

Here goes the detailed code-example , with multiple such MACRO, for understanding :

Upon execution this gives the below output :


Repetitive use of EVAL :

In this eval example , we write an up-counter using TCL code and eval. The increment variable is set as : set cmdIncr {incr i}  where  i is initiated with 0

The full code of the up-counter goes like this :

Upon execution of this code the output comes as :


Summary :

  • Eval & Exec are ways to execute internal macro & external binary respectively.
  • Eval provides a way short-hand to a bunch of tcl commands clubbed together.
  • Exec provides a way to call external binaries within TCLSH shell.


The entire article is well narrated in a video.

Watch the video for better understanding.

Courtesy : www.pngegg.com