From f37895e9ef53f3bcd7c3cce211cfd516489ef95c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Harboe?= Date: Tue, 1 Dec 2026 10:04:46 +0100 Subject: [PATCH 1/3] tcl: compare strings with eq/ne, not ==/!= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tcl's == tries a numeric comparison first, so "1e3" == "1000" and "0x10" == "16" are true. Every ==/!= whose operand is a string (a literal such as "NULL", "POWER", "BLOCK", "WC", or a variable holding a name, type or extension) becomes eq/ne; a list is tested with llength. Numeric comparisons are left alone. Adds the rule to TclContract.md. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Øyvind Harboe --- docs/contrib/TclContract.md | 4 ++++ flow/platforms/gf180/setRC.tcl | 14 +++++++------- flow/scripts/floorplan.tcl | 2 +- flow/scripts/load.tcl | 6 +++--- flow/scripts/macro_place_util.tcl | 2 +- flow/scripts/pdn.tcl | 2 +- flow/scripts/report_metrics.tcl | 6 +++--- flow/scripts/run_step.tcl | 2 +- flow/scripts/save_images.tcl | 2 +- flow/scripts/synth_preamble.tcl | 6 +++--- flow/scripts/synth_syn.tcl | 2 +- flow/scripts/util.tcl | 10 +++++----- flow/util/cell-veneer/lefdef.tcl | 18 +++++++++--------- flow/util/cell-veneer/wrap.tcl | 2 +- flow/util/cell-veneer/wrap_stdcells.tcl | 16 ++++++++-------- 15 files changed, 49 insertions(+), 45 deletions(-) diff --git a/docs/contrib/TclContract.md b/docs/contrib/TclContract.md index e1b5d98686..5ac9795b17 100644 --- a/docs/contrib/TclContract.md +++ b/docs/contrib/TclContract.md @@ -24,6 +24,10 @@ to follow. ## Idiom +- **Strings compare with `eq` and `ne`; `==` is for numbers.** `==` tries a + numeric comparison first, so `"1e3" == "1000"` and `"0x10" == "16"` are + true. A list is tested with `[llength $x]`, never against `""` or `{}`. + *Breaks:* a name or type that looks like a number compares equal to another. - **A script never writes its own version of an idiom `util.tcl` already has.** An optional flag is added with `append_env_var`, a command that does real work runs through `log_cmd`, a result is written with `orfs_write_db`, diff --git a/flow/platforms/gf180/setRC.tcl b/flow/platforms/gf180/setRC.tcl index eaa1b32ca6..241777f72d 100644 --- a/flow/platforms/gf180/setRC.tcl +++ b/flow/platforms/gf180/setRC.tcl @@ -5,13 +5,13 @@ set_layer_rc -layer Metal5 -resistance 5.85545E-02 -capacitance 1.55595E-04 regexp {(\d+)} $::env(METAL_OPTION) metal -if { $metal == "6" } { +if { $metal eq "6" } { set_wire_rc -signal -layer Metal2 set_wire_rc -clock -layer Metal5 -} elseif { $metal == "5" } { +} elseif { $metal eq "5" } { # TC matches LEF. These are the temperature adjusted values. # The other stacks are likely similar but I haven't checked yet. - if { $::env(CORNER) == "WC" } { + if { $::env(CORNER) eq "WC" } { set_layer_rc -via Via1 -resistance 16.845 set_layer_rc -via Via2 -resistance 16.845 set_layer_rc -via Via3 -resistance 16.845 @@ -23,7 +23,7 @@ if { $metal == "6" } { $via setResistance 16.845 } } - } elseif { $::env(CORNER) == "BC" } { + } elseif { $::env(CORNER) eq "BC" } { set_layer_rc -via Via1 -resistance 4.23 set_layer_rc -via Via2 -resistance 4.23 set_layer_rc -via Via3 -resistance 4.23 @@ -39,13 +39,13 @@ if { $metal == "6" } { set_wire_rc -signal -layer Metal2 set_wire_rc -clock -layer Metal4 -} elseif { $metal == "4" } { +} elseif { $metal eq "4" } { set_wire_rc -signal -layer Metal2 set_wire_rc -clock -layer Metal3 -} elseif { $metal == "3" } { +} elseif { $metal eq "3" } { set_wire_rc -signal -layer Metal2 set_wire_rc -clock -layer Metal2 -} elseif { $metal == "2" } { +} elseif { $metal eq "2" } { set_wire_rc -signal -layer Metal2 set_wire_rc -clock -layer Metal2 } diff --git a/flow/scripts/floorplan.tcl b/flow/scripts/floorplan.tcl index 2488218f07..5a32402947 100644 --- a/flow/scripts/floorplan.tcl +++ b/flow/scripts/floorplan.tcl @@ -20,7 +20,7 @@ proc report_unused_masters { } { foreach lib $libs { foreach master [$lib getMasters] { # filter out non-block masters, or you can remove this conditional to detect any unused master - if { [$master getType] == "BLOCK" } { + if { [$master getType] eq "BLOCK" } { lappend masters $master } } diff --git a/flow/scripts/load.tcl b/flow/scripts/load.tcl index a5574939ff..9af9dc3383 100644 --- a/flow/scripts/load.tcl +++ b/flow/scripts/load.tcl @@ -34,7 +34,7 @@ proc load_stage_input { design_file } { proc load_design_from { dir design_file } { # Do not reload if design is already loaded set db [ord::get_db] - if { [$db getChip] != "NULL" && [[$db getChip] getBlock] != "NULL" } { + if { [$db getChip] ne "NULL" && [[$db getChip] getBlock] ne "NULL" } { return } @@ -44,7 +44,7 @@ proc load_design_from { dir design_file } { # Read design files set ext [file extension $design_file] - if { $ext == ".v" } { + if { $ext eq ".v" } { read_lef $::env(TECH_LEF) read_lef $::env(SC_LEF) if { [env_var_exists_and_non_empty ADDITIONAL_LEFS] } { @@ -58,7 +58,7 @@ proc load_design_from { dir design_file } { } read_verilog $dir/$design_file log_cmd link_design {*}[hier_options] $::env(DESIGN_NAME) - } elseif { $ext == ".odb" } { + } elseif { $ext eq ".odb" } { log_cmd read_db {*}[hier_options] $dir/$design_file } else { error "Unrecognized input file $design_file" diff --git a/flow/scripts/macro_place_util.tcl b/flow/scripts/macro_place_util.tcl index b613835974..0015d4215e 100644 --- a/flow/scripts/macro_place_util.tcl +++ b/flow/scripts/macro_place_util.tcl @@ -1,4 +1,4 @@ -if { [find_macros] != "" } { +if { [llength [find_macros]] != 0 } { if { ![env_var_exists_and_non_empty RTLMP_RPT_DIR] } { set ::env(RTLMP_RPT_DIR) "$::env(OBJECTS_DIR)/rtlmp" } diff --git a/flow/scripts/pdn.tcl b/flow/scripts/pdn.tcl index 8c39fb1d2b..0a4a98af94 100644 --- a/flow/scripts/pdn.tcl +++ b/flow/scripts/pdn.tcl @@ -22,7 +22,7 @@ set_placement_padding -global -left 0 -right 0 set block [ord::get_db_block] foreach net [$block getNets] { set type [$net getSigType] - if { $type == "POWER" || $type == "GROUND" } { + if { $type eq "POWER" || $type eq "GROUND" } { # Temporarily disable due to CI issues # puts "Check supply: [$net getName]" # check_power_grid -net [$net getName] diff --git a/flow/scripts/report_metrics.tcl b/flow/scripts/report_metrics.tcl index 749a2cb340..702ed7f135 100644 --- a/flow/scripts/report_metrics.tcl +++ b/flow/scripts/report_metrics.tcl @@ -175,7 +175,7 @@ proc report_metrics { stage when { include_erc true } { include_clock_skew true report_puts "hold violation count [sta::endpoint_violation_count min]" set critical_path [lindex [find_timing_paths -sort_by_slack] 0] - if { $critical_path != "" } { + if { $critical_path ne "" } { set path_delay [sta::format_time [[$critical_path path] arrival] 4] set path_slack [sta::format_time [[$critical_path path] slack] 4] } else { @@ -197,7 +197,7 @@ proc report_metrics { stage when { include_erc true } { include_clock_skew true set inp_to_reg_critical_path \ [lindex [find_timing_paths -path_delay max -from [all_inputs] -to [all_registers]] 0] - if { $inp_to_reg_critical_path != "" } { + if { $inp_to_reg_critical_path ne "" } { set target_clock_latency_max \ [sta::format_time [$inp_to_reg_critical_path target_clk_delay] 4] } else { @@ -207,7 +207,7 @@ proc report_metrics { stage when { include_erc true } { include_clock_skew true set inp_to_reg_critical_path [lindex \ [find_timing_paths -path_delay min -from [all_inputs] -to [all_registers]] 0] - if { $inp_to_reg_critical_path != "" } { + if { $inp_to_reg_critical_path ne "" } { set target_clock_latency_min \ [sta::format_time [$inp_to_reg_critical_path target_clk_delay] 4] set source_clock_latency [sta::format_time [$inp_to_reg_critical_path source_clk_latency] 4] diff --git a/flow/scripts/run_step.tcl b/flow/scripts/run_step.tcl index f4cae94d80..79f994083e 100644 --- a/flow/scripts/run_step.tcl +++ b/flow/scripts/run_step.tcl @@ -9,7 +9,7 @@ proc write_failure_checkpoint { } { return } set chip [[ord::get_db] getChip] - if { $chip == "NULL" || [$chip getBlock] == "NULL" } { + if { $chip eq "NULL" || [$chip getBlock] eq "NULL" } { puts "No design loaded, no failure checkpoint written" return } diff --git a/flow/scripts/save_images.tcl b/flow/scripts/save_images.tcl index 2f6c06d17b..92cde054af 100644 --- a/flow/scripts/save_images.tcl +++ b/flow/scripts/save_images.tcl @@ -7,7 +7,7 @@ set height [ord::dbu_to_microns $height] set resolution [expr $height / 1000] set markerdb [[ord::get_db_block] findMarkerCategory DRC] -if { $markerdb != "NULL" && [$markerdb getMarkerCount] > 0 } { +if { $markerdb ne "NULL" && [$markerdb getMarkerCount] > 0 } { gui::select_marker_category $markerdb } diff --git a/flow/scripts/synth_preamble.tcl b/flow/scripts/synth_preamble.tcl index bd113e85e7..1b1c2f73c0 100644 --- a/flow/scripts/synth_preamble.tcl +++ b/flow/scripts/synth_preamble.tcl @@ -36,7 +36,7 @@ if { [env_var_exists_and_non_empty SYNTH_NETLIST_FILES] } { proc read_checkpoint { file } { # We are reading a Yosys checkpoint - if { [file extension $file] == ".json" } { + if { [file extension $file] eq ".json" } { read_json $file } else { read_rtlil $file @@ -268,12 +268,12 @@ proc convert_liberty_areas { } { continue } set area [rtlil::get_attr -string -mod $cell area] - if { $found_cell == "" || $area < $found_cell_area } { + if { $found_cell eq "" || $area < $found_cell_area } { set found_cell $cell set found_cell_area $area } } - if { $found_cell == "" } { + if { $found_cell eq "" } { error "reference nand2 cell not found" } diff --git a/flow/scripts/synth_syn.tcl b/flow/scripts/synth_syn.tcl index 1c380a65b2..ae1f15e407 100644 --- a/flow/scripts/synth_syn.tcl +++ b/flow/scripts/synth_syn.tcl @@ -64,7 +64,7 @@ lappend elaborate_args {*}$::env(SYNTH_SLANG_ARGS) # If the sources are solely .v files, enable Verilog compatibility set has_non_v_files false foreach fn $verilog_files { - if { [file extension [string trim $fn]] != ".v" } { + if { [file extension [string trim $fn]] ne ".v" } { set has_non_v_files true } } diff --git a/flow/scripts/util.tcl b/flow/scripts/util.tcl index 4c023bed5a..8b9d5b7f5b 100644 --- a/flow/scripts/util.tcl +++ b/flow/scripts/util.tcl @@ -90,7 +90,7 @@ proc extract_stage { input_file } { exit 1 } lappend number_groups $num1 - if { $num2 != "" } { + if { $num2 ne "" } { lappend number_groups $num2 } else { lappend number_groups "0" @@ -324,7 +324,7 @@ proc is_physical_only_master { master } { proc has_signal_pins { master } { foreach mterm [$master getMTerms] { set sig_type [$mterm getSigType] - if { $sig_type != "POWER" && $sig_type != "GROUND" } { + if { $sig_type ne "POWER" && $sig_type ne "GROUND" } { return 1 } } @@ -335,7 +335,7 @@ proc has_signal_pins { master } { proc has_liberty_cell { master } { set master_name [$master getName] set lib_cells [get_lib_cells -quiet */$master_name] - if { $lib_cells == {} } { + if { [llength $lib_cells] == 0 } { return 0 } return 1 @@ -399,7 +399,7 @@ proc use_ccs_delay_calculator { } { # design is placed, what OpenROAD estimates (global routes if there are # any, else placement), else none. proc orfs_load_parasitics { step } { - if { $step == "6_final" && [final_extracts_parasitics] } { + if { $step eq "6_final" && [final_extracts_parasitics] } { log_cmd read_spef $::env(RESULTS_DIR)/6_final.spef } elseif { [grt::design_is_placed] } { log_cmd estimate_parasitics @@ -466,7 +466,7 @@ proc copy_endpoint_slack { input_file output_file } { } set step [file rootname [file tail $output_file]] set chip [[ord::get_db] getChip] - if { $chip != "NULL" && [$chip getBlock] != "NULL" } { + if { $chip ne "NULL" && [$chip getBlock] ne "NULL" } { write_endpoint_slack $step return } diff --git a/flow/util/cell-veneer/lefdef.tcl b/flow/util/cell-veneer/lefdef.tcl index 901fd13706..c90a7c978c 100644 --- a/flow/util/cell-veneer/lefdef.tcl +++ b/flow/util/cell-veneer/lefdef.tcl @@ -36,7 +36,7 @@ proc open { file_name } { # tclint-disable-next-line redefined-builtin proc close { } { variable lefOut - if { $lefOut != "stdout" } { + if { $lefOut ne "stdout" } { ::close $lefOut } set lefOut stdout @@ -105,7 +105,7 @@ proc read_macros { file_name } { dict set cells $cell_name site $site } elseif { [regexp {PIN\s*([^\s]*)} $line - pin_name] } { set pin_pattern [regsub -all {([\[\]])} $pin_name {\\\1}] - if { [info vars antennamodel] != "" } { + if { [info vars antennamodel] ne "" } { unset antennamodel } while { ![eof $ch] } { @@ -119,7 +119,7 @@ proc read_macros { file_name } { } elseif { [regexp {ANTENNAMODEL\s+([^\s]*)} $line - antennamodel] } { continue } elseif { [regexp {ANTENNAGATEAREA\s+([^\s]*)\s+LAYER\s+([^\s]*)} $line - gate_area layer] } { - if { [info vars antennamodel] == "" } { + if { [info vars antennamodel] eq "" } { set antennamodel "default" } if { [dict exists $cells $cell_name pins $pin_name antenna_model $antennamodel] } { @@ -130,7 +130,7 @@ proc read_macros { file_name } { lappend model [list gate_area $gate_area layer $layer] dict set cells $cell_name pins $pin_name antenna_model $antennamodel $model } elseif { [regexp {ANTENNAGATEAREA\s+([^\s]*)} $line - gate_area] } { - if { [info vars antennamodel] == "" } { + if { [info vars antennamodel] eq "" } { set antennamodel "default" } if { [dict exists $cells $cell_name pins $pin_name antenna_model $antennamodel] } { @@ -214,7 +214,7 @@ proc read_macros { file_name } { if { [regexp {^\s*$} $line] } { continue } elseif { [regexp {LAYER\s+([^\s]*)(\s+DESIGNRULEWIDTH\s+([0-9.]+))?} $line - layer - drw] } { - if { $drw != "" } { + if { $drw ne "" } { dict set cells $cell_name layers $layer drw $drw } continue @@ -473,7 +473,7 @@ proc open { file_name } { # tclint-disable-next-line redefined-builtin proc close { } { variable defOut - if { $defOut != "stdout" } { + if { $defOut ne "stdout" } { ::close $defOut } set defOut stdout @@ -676,7 +676,7 @@ proc get_line_direction { points } { proc get_line_width { layer_name points } { set direction [get_line_direction $points] - if { [get_layer_direction $layer_name] == $direction } { + if { [get_layer_direction $layer_name] eq $direction } { return [get_layer_width $layer_name] } else { return [get_layer_non_preferred_width $layer_name] @@ -690,13 +690,13 @@ proc get_extended_line { layer_name points } { set direction [get_line_direction $points] - if { $direction == [get_layer_direction $layer_name] } { + if { $direction eq [get_layer_direction $layer_name] } { set extension [expr [get_layer_non_preferred_width $layer_name] / 2] } else { set extension [expr [get_layer_width $layer_name] / 2] } - if { $direction == "VERTICAL" } { + if { $direction eq "VERTICAL" } { set x_min [lindex $points 0 0] set x_max [lindex $points 0 0] set y_min [expr min([lindex $points 0 1], [lindex $points 1 1]) - $extension] diff --git a/flow/util/cell-veneer/wrap.tcl b/flow/util/cell-veneer/wrap.tcl index 8fa52113ab..e91a45a7f7 100755 --- a/flow/util/cell-veneer/wrap.tcl +++ b/flow/util/cell-veneer/wrap.tcl @@ -15,7 +15,7 @@ if { [set idx [lsearch -exact $argv {-cfg}]] > -1 } { wrapper critical 2 "no configuration data loaded" } -if { [lindex $argv 0] == "-macro" } { +if { [lindex $argv 0] eq "-macro" } { set lef_files [lrange $argv 1 end] set cells {} foreach file_name $lef_files { diff --git a/flow/util/cell-veneer/wrap_stdcells.tcl b/flow/util/cell-veneer/wrap_stdcells.tcl index f6ad587cc6..1a77b73482 100644 --- a/flow/util/cell-veneer/wrap_stdcells.tcl +++ b/flow/util/cell-veneer/wrap_stdcells.tcl @@ -70,7 +70,7 @@ proc find_cells_with_m2_pins { } { if { [dict exists $data $cell_name] } { dict for {layer_name obstructions} [dict get $cell obstructions] { - if { $layer_name == "M2" } { + if { $layer_name eq "M2" } { foreach obs $obstructions { set rect [dict get $obs rect] set x1 [lindex $rect 0] @@ -151,7 +151,7 @@ proc create_def_wrapper { cell_name new_cell_name } { dict for {pin_name pin} [dict get $orig_cell pins] { dict set design pins $pin_name net_name $pin_name if { [dict exists $pin use] } { - if { [dict get $pin use] == "POWER" || [dict get $pin use] == "GROUND" } { + if { [dict get $pin use] eq "POWER" || [dict get $pin use] eq "GROUND" } { dict set design special_nets $pin_name connections [list "PIN $pin_name" "* $pin_name"] dict set design special_nets $pin_name use [dict get $pin use] } else { @@ -228,17 +228,17 @@ proc move_m2_pins_to_edge { cell_name cell_data } { } } - if { $direction == "blocked" } { + if { $direction eq "blocked" } { break } set y [expr [dict get $physical_pin track] * 128] - if { $direction == "right" } { + if { $direction eq "right" } { set x1 [dict get $physical_pin to] set x2 [expr $cell_width + ($padding_cell_width * ($right_padding + 1))] set x3 [expr $cell_width + ($padding_cell_width * ($right_padding + 1)) + $via_overlap] incr right_padding - } elseif { $direction == "left" } { + } elseif { $direction eq "left" } { set x1 [dict get $physical_pin from] set x2 [expr 0 - ($padding_cell_width * ($left_padding + 1))] set x3 [expr 0 - ($padding_cell_width * ($left_padding + 1)) - $via_overlap] @@ -271,7 +271,7 @@ proc move_m2_pins_to_edge { cell_name cell_data } { break } - if { $direction == "blocked" } { + if { $direction eq "blocked" } { break } @@ -471,7 +471,7 @@ proc wrap_macro { cell_name } { set grid_pins {} dict for {pin_name pin} [dict get $cell pins] { - if { [dict get $pin use] != "SIGNAL" } { continue } + if { [dict get $pin use] ne "SIGNAL" } { continue } # CHEAT: Assume that there is only one port for each pin and one rectangle per layer set port [lindex [dict get $pin ports] 0] @@ -597,7 +597,7 @@ proc wrap_macro { cell_name } { "$macro_x [dict get $net macro_pin_y]" \ "$target_grid_point [dict get $net macro_pin_y]" \ "$target_grid_point $y_position"]] - if { [dict get $net pin_layer] != "C4" } { + if { [dict get $net pin_layer] ne "C4" } { lappend segments [list \ layer [dict get $net pin_layer] \ points [list \ From 8b016e8b3917944f6a593618531176093b8bf24e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Harboe?= Date: Tue, 1 Dec 2026 10:09:02 +0100 Subject: [PATCH 2/3] tcl: find_macros returns a string, test it with ne MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Signed-off-by: Øyvind Harboe --- flow/scripts/macro_place_util.tcl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flow/scripts/macro_place_util.tcl b/flow/scripts/macro_place_util.tcl index 0015d4215e..57a4cc1af6 100644 --- a/flow/scripts/macro_place_util.tcl +++ b/flow/scripts/macro_place_util.tcl @@ -1,4 +1,4 @@ -if { [llength [find_macros]] != 0 } { +if { [find_macros] ne "" } { if { ![env_var_exists_and_non_empty RTLMP_RPT_DIR] } { set ::env(RTLMP_RPT_DIR) "$::env(OBJECTS_DIR)/rtlmp" } From 4996c8073b2af9b72233ade1c1bd2345962035c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Harboe?= Date: Tue, 1 Dec 2026 11:28:31 +0100 Subject: [PATCH 3/3] TclContract: llength for a list a command returns, ne "" for env MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An environment variable is a string even when it holds a list, so the sibling PRs that read optional lists from the environment test them with ne ""; the rule now says which test goes with which value. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Øyvind Harboe --- docs/contrib/TclContract.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/contrib/TclContract.md b/docs/contrib/TclContract.md index 5ac9795b17..356b30f380 100644 --- a/docs/contrib/TclContract.md +++ b/docs/contrib/TclContract.md @@ -26,7 +26,8 @@ to follow. - **Strings compare with `eq` and `ne`; `==` is for numbers.** `==` tries a numeric comparison first, so `"1e3" == "1000"` and `"0x10" == "16"` are - true. A list is tested with `[llength $x]`, never against `""` or `{}`. + true. A list a command returns is tested with `[llength $x]`; a + variable from the environment is a string, tested with `ne ""`. *Breaks:* a name or type that looks like a number compares equal to another. - **A script never writes its own version of an idiom `util.tcl` already has.** An optional flag is added with `append_env_var`, a command that does