Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename sta::source to sta::include to allow use of tcl source #205

Closed
wants to merge 3 commits into from

Conversation

donn
Copy link

@donn donn commented Feb 4, 2025

  • create new global stack, ::_sta_file_stack, to keep track of evaluated files (include and source)
  • rename sta::source to sta::include, allowing the original tcl source (which behaves differently, including inheriting the caller's scope) to be used
  • create new thin wrapper for tcl source as sta::source - all it does is push to and pop from ::_sta_file_stack
  • update sta_error, sta_warn to inspect the stack and alter behavior based on whether the current file is sourced or included

Original PR body follows:


  • add new global variable, ::sta_scoped_source that if set to 1, causes all sourced files to be evaluated at the scope of the calling function instead of the top level (default 0). read_sdc will always be evaluated at the top-level, however.
  • ignore early returns when sourcing files - tcl return is supposed to work when sourcing files. see https://www.tcl-lang.org/man/tcl8.6/TclCmd/return.htm for more info

I have chosen to make it opt-in behavior out of an excess of caution. The ~/.sta issue could technically be resolved by upleveling sourceTclFile, but that may have unforeseen consequences. Fortunately, it's not a huge ask to ask users to set ::sta_scoped_source to 1 before package require statements.

Resolves #202

@CLAassistant
Copy link

CLAassistant commented Feb 4, 2025

CLA assistant check
All committers have signed the CLA.

@donn donn marked this pull request as draft February 4, 2025 16:25
@donn
Copy link
Author

donn commented Feb 4, 2025

sorry - found another issue with package require

EDIT: So this is annoying but early returns from a sourced file also have to be emulated for tcllib to work. I'll see what can be done about that.

@akashlevy
Copy link
Contributor

Any chance you can resolve #204 while you are at it? Would greatly appreciate it @donn

@donn
Copy link
Author

donn commented Feb 4, 2025

Hi Akash- will try, pun not intended, but I really doubt I can because this whole thing is already enough of a Tcl scoping nightmare. I can't just include the forward-compat code either as it has no license.

@akashlevy
Copy link
Contributor

If you can use catch instead of try, that would be awesome

@akashlevy
Copy link
Contributor

This is addressed in #206 so no need to address @donn, thanks anyways

@donn donn marked this pull request as ready for review February 5, 2025 10:55
@donn donn changed the title optional scoped sourcing, fix info script optional scoped sourcing, allow early returns from source Feb 6, 2025
- add new global variable, ::sta_scoped_source that if set to 1, causes all sourced files to be evaluated at the scope of the calling function instead of the top level (default 0). read_sdc will always be evaluated at the top-level, however.
- ignore early returns when sourcing files - tcl return is supposed to work when sourcing files. see https://www.tcl-lang.org/man/tcl8.6/TclCmd/return.htm for more info

Signed-off-by: Mohamed Gaber <donn@efabless.com>
@donn
Copy link
Author

donn commented Feb 6, 2025

@jjcherry56 Should be ready now.

@jjcherry56
Copy link
Collaborator

I am not a fan of this approach.
The redefinition of source dates back 20 years to match the behavior of another tool.
I think it would make more sense to remove the code that redefines source and define a new command "read_cmds" (without the unused -verbose option) based on the existing implementation.

@donn
Copy link
Author

donn commented Feb 6, 2025

That... would be my preferred approach too. I just assumed you wouldn't want to break existing code.

akashlevy added a commit to Silimate/OpenSTA that referenced this pull request Feb 10, 2025
- rename sta::source to sta::read_cmds, allowing the original tcl source (which behaves differently, including inheriting the caller's scope) to be used
- reverts `69bcdbf507e6e3d45ba471d73d7ae45865ec729c`.

Signed-off-by: Mohamed Gaber <donn@efabless.com>
@donn donn changed the title optional scoped sourcing, allow early returns from source rename sta::source to sta::read_cmds to allow use of tcl source Feb 13, 2025
@donn
Copy link
Author

donn commented Feb 13, 2025

@jjcherry56 Done.

@jjcherry56
Copy link
Collaborator

I tried this myself a few days ago. I decided "include" makes more sense than "read_cmds".
The problem is that using the builtin source (which happens the most) sta_errror/warn do not report the filename or line number, so it isn't this simple. I stopped there because I have higher priority issues to deal with.

@donn donn marked this pull request as draft February 14, 2025 15:07
@donn donn changed the title rename sta::source to sta::read_cmds to allow use of tcl source rename sta::source to sta::include to allow use of tcl source Feb 14, 2025
@donn donn marked this pull request as ready for review February 14, 2025 15:24
…atible

- sta_warn, sta_error now work with tcl source using a very thin wrapper and a source/include file stack

Signed-off-by: Mohamed Gaber <donn@efabless.com>
@donn
Copy link
Author

donn commented Feb 14, 2025

@jjcherry56 So I implemented a solution to this that works as follows:

There's now an ::_sta_file_stack. This stack is composed of tuples of:

  • either include or source
  • the normalized filename

Both sta::include and sta::source add the file currently under processing to the stack.

sta::warn and sta::error peek the top element of the stack, and if it's an include, use the previous behavior.

If it's a source, however, the frame stack is inspected to return the line number of the lowest stack frame in the sourced file. This is to emulate the behavior of how include's line numbers work as closely as possible. I added a test for this behavior.

@jjcherry56
Copy link
Collaborator

what I had in mind was NOT redefining source and making include set the info file/line properties like source does so it has the same behavior and modifying sta_error/warn to use the info file/line instead of variables or stack

@donn
Copy link
Author

donn commented Feb 14, 2025

That'd require include to be partially implemented in C so it can create a new stack frame (I'm unaware of a way to do that in Tcl) -- sadly though, I'd have to tackle that another time as that is far more involved.

@donn donn closed this Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

reimplemented source executes all commands at top-level, breaking typical tcl codebases
4 participants