I installed tclsh on WSL ubuntu as follows:
$ sudo apt-get install tcl
$ sudo apt-get install tk
$ sudo apt-get install tcllib
then I created a simple TCL program:
# myscript.tcl
puts "hello world!"
next I tried compile myscript.tcl into binary bytecodes file:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
$ tclsh
% puts $tcl_version
"8.6"
% tcl::unsupported::compile myscript.tcl
invalid command name "tcl::unsupported::compile"
As you can see, it gives me an error that it can’t find tcl::unsupported::compile.
Does anybody know what happened to tcl::unsupported::compile? did they remove this or is it moved somewhere else? Am i doing something wrong?
This is what i found on topic of “tcl::unsupported::compile”
It is possible to compile Tcl code into bytecode using the built-in “tclcompiler” package that comes with Tcl.
To compile a Tcl script into bytecode, you can use the “tcl::unsupported::compile” command. For example, suppose you have a Tcl script named “myscript.tcl”, you can compile it into bytecode using the following command:
tcl::unsupported::compile myscript.tcl
This command will create a file named “myscript.tbc” in the same directory as the original script. The “.tbc” file contains the compiled bytecode for the script.
To run the compiled bytecode, you can use the “tcl::unsupported::disassemble” command to generate the disassembled bytecode, and then use the “tcl::eval” command to evaluate the bytecode. For example:
set bytecode [tcl::unsupported::disassemble myscript.tbc]
tcl::eval $bytecode
Note that the “tclcompiler” package is considered unsupported and subject to change in future Tcl versions. Additionally, not all Tcl extensions may be compatible with the compiled bytecode, so it’s important to test thoroughly before relying on bytecode compilation in production code.
2
Answers
You are looking for ::tcl::unsupported::assemble, aren’t you?
There never was a
tcl::unsupported::compile
; Tcl compiles your code when it sees fit to do so, usually when it needs the bytecode for some reason, either because it is about to execute the code or because you asked for it withdisassemble
orgetbytecode
(both intcl::unsupported
because the bytecode internals are explicitly not a stable API). If you are starting out with Tcl code, that is totally the way to do it. (The compiler is both really fast and cached aggressively.)You can use
tcl::unsupported::assemble
to convert a subset of bytecode encodes into something you can run (put it inside a procedure for best effect) but that really is just a subset. In particular, it omits some opcodes that are difficult to express the safety requirements of (such as how to keep the stack balanced). In some other cases, it uses virtual opcodes that hide which of a family of them is really used (because you shouldn’t have to care and, as an author of command compilers for things likeswitch
andtry
I wish I’d never had to care either!) It is mostly just an interesting exercise in what can be done, except if you want to do something like build some kinds of control structure; it is the easiest way to do things that would otherwise requiregoto
in Tcl for example.There isn’t a good way to turn Tcl into the assembly, and it would only work in some cases at best. You don’t tend to need it.
There is a Tcl-to-native code compiler experiment that I wrote part of (tclquadcode). It isn’t finished. It isn’t part of Tcl itself. It isn’t likely to become part of Tcl either; it is very slow right now. (Assuming it builds and that the parts of the LLVM API it hooks into haven’t changed too much.)
It can produce very fast code, especially for anything with a lot of math in it, but doesn’t accelerate string code much and we never finished the next stage which was getting lists and dicts to be more efficient.
Most requirements for targeted speed can be addressed with Critcl, which lets you embed C code snippets directly in your Tcl code.
The compiler and bytecode loader in the TclDevKit are more for code obfuscation, not performance.