Skip to main content

Access Tcl Global Variables from a Sourced File

When you read an SDC file from the command line, it works fine. When you read it using a Tcl procedure, it won’t work, because the global variables cannot be accessed. Here’s how to fix this annoying problem.

Tcl source Works on the Command Line

Suppose you reference the global variable CLK_SKEW in SDC file clk.sdc:

# File clk.sdc, which is a Tcl script
set_clock_uncertainty [get_clocks clk] -setup $CLK_SKEW

You will have no problem when you read the SDC file on the command line using Tcl source or read_sdc:

set CLK_SKEW 15
source clk.sdc
# or alternatively,
read_sdc clk.sdc

This works because on the command line, you are in the global context.

Tcl source Fails in a Tcl Procedure

Now, suppose that you define a Tcl proc to do something fancy, like source multiple files:

proc read_multiple_sdc {sdc_file_list} {
    foreach sdc_file $sdc_file_list {
        source $sdc_file
    }
}

upon execution, this fails:

set CLK_SKEW 15
read_multiple_sdc {"clk.sdc" "design.sdc"}
Error: can't read "CLK_SKEW": no such variable

This is because global variable CLK_SKEW cannot be referenced from within the context of the procedure read_multiple_sdc.

Solutions

There are two solutions.

Declare Global Variables using Tcl global

The best solution is to declare each global variable you will reference as global:

# File clk.sdc 
global CLK_SKEW
set_clock_uncertainty [get_clocks clk] -setup $CLK_SKEW

This approach forces the author of the SDC file to think carefully about each global variable being accessed, while the other global variables remain safely hidden.

Use Tcl uplevel

Another approach is for your procedure to execute read_sdc or source using Tcl uplevel:

proc read_multiple_sdc {sdc_file_list} {
    foreach sdc_file $sdc_file_list {
        uplevel #0 source $sdc_file
    }
}

uplevel #0 means to execute the specified script in context zero, the global context. This gives the Tcl script in the SDC file access to all global variables, but it increases the risk that the SDC file might inadvertently modify a global variable. The advantage is that the SDC file requires no modification.

Background

These techniques should work with any Tcl script. The above discussion is oriented toward reading Standard Design Constraint (SDC) files, which are Tcl scripts used to describe design intent in digital IC design. SDC files are used with logic synthesis or place and route tools like Synopsys Design Compiler, Primetime and IC Compiler, Cadence RTL Compiler and SoC Encounter.

Comments

Popular posts from this blog

The Reef Lounge

When I was about four, we vacationed at the Handerly Hotel & Resort in San Diego (formerly the Stardust Motor Hotel).  That place was heaven on earth as far as I was concerned. I loved the pool. Next to the pool there was a bar, and behind the bar was an underwater theater. It was here that I saw one of the most outlandish scenes I have ever witnessed.

Install East Asian Languages with no Windows XP Installation Disk

When you try to install files for East Asian languages, you are asked for a Windows XP installation disk. New computers have shipped with Windows pre-installed for a long time now, so few people have a physical Windows XP disk.  Here’s how to install without the DVD/CD-ROM.