slang-users mailing list

[2021 Date Index] [2021 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] Using the tcc module


Hi Sebastian,

On Thu, Jan 21, at 04:15 Sebastian Gniazdowski wrote:
> Hi,
> I saw some exciting posts in the archives about an embedded, jit C compiler
> – tcc. I wonder how the support is going, is the module in the main S-Lang
> distribution maybe?

I wrote that code, mostly because i wanted constant String Type (char *) objects
generated at runtime.
Sure, you can run any C code that tinycc can complile, but this is an inherently
unsafe operation by default, since an access violation will crash the interpreter.

> In general I would like to know how to run my first C code (`printf("hi");`,
> probably) from within a S-Lang script.

Assuming you've compiled the code, it is easy as that:

slsh> () = evalfile ("./Tcc.sl");
slsh> variable s = Tcc.init ();
slsh> s.execute_string  (`
#include <stdio.h>
int main (int argc, char **argv) {
  return fprintf (stdout, "%s", argv[0]);
}`, ["my message\n"]);

or if you want to add an intrinsic function:

slsh> s.sladd_intrinsic_function  ("myprint", 1, "c_myprint", Integer_Type, [String_Type], `#include <stdio.h>
        int c_myprint (char *msg) {return fprintf (stdout, "%s", msg);}`);
0
slsh> myprint ("my message\n");

Be aware that you can not reuse a state, that's a tcc limitation, so you have to
run s.delete () to free that state, and call the s.new () method to instantiate it
again.

> I'm working on a S-Lang support in Midnight Commander (mcedit, mainly), I think that
> such C support would be a nice extension/sophistication of it.

But it's been a long since i've used that code, and i do not know if current tcc
development, broke some code. This is used to compile, against the development git
tree, the so called mob branch. The last formal release is quite ancient, and i
think most people, that are using the compiler use that branch, but distributions
might package the formal release.
Because of this, i believe it will be quite hard for proper user support and guarantee
stabilization and correctness.

But, yes, it can be usefull, in cases when you want to interact with S-Lang's C API,
which was the primary intention anyway. Or to run properly tested C code at runtime
withing SLang. So yes, it can be fancy.

Feel free to fork and extend, as this was my really first C code, and as i see it now
sure needs some fixing.

> Sebastian Gniazdowski

Best regards,
 ag

p.s.,
Hi John :-). Strange, a week ago, i was thinking to write you after so much time!
Still i use the S-Lang enviroment, but I'm developing in C nowadays.

Hopefully everyone of you, will stay safe.
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.


[2021 date index] [2021 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]