tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] Two compilers at once


From: Steven G. Messervey
Subject: Re: [Tinycc-devel] Two compilers at once
Date: Fri, 21 Jun 2019 11:19:04 +0000 (UTC)



On Friday, May 24, 2019, 1:48:24 PM EDT, Karl Yerkes <address@hidden> wrote:


I would like to have two compilers active at the same time so i can recompile and hot-swap a function. ;On my journey, I noticed that making a new, second compiler before the first one compiles leads to strange behaviour. I am running macOS 10.14.5 with tcc 0.9.27.

// compile and run like this:
//
//   c++ -std=c++11 -ltcc tinycc-devel-example.cpp ; and ./a.out
//
// the `tcc_compile_string` step will fail with this error:
//
//   <string>: error: missing #endif
//
#include <cassert>
#include <cstdio>
#include "libtcc.h"

int main() {
  TCCState* a = nullptr;
  char (*A)(int) = nullptr;
  assert((a = tcc_new()) != nullptr);
  TCCState* b = tcc_new();  // remove this line to make the program work
  assert(tcc_set_output_type(a, TCC_OUTPUT_MEMORY) == 0);
  assert(tcc_compile_string(a, "char foo(int t) { return (char)t; }") != -1);
  assert(tcc_relocate(a, TCC_RELOCATE_AUTO) >= 0);
  assert((A = (char (*)(int))tcc_get_symbol(a, "foo")) != nullptr);
  printf("%d = A(0)\n", A(0));
}


As long as I make the second compiler after the first one compiles something, things work. is this a bug? Is using two compilers unsupported or prohibited?

-- karl


_______________________________________________
Tinycc-devel mailing list
address@hidden
Tinycc-devel Info Page





Perhaps we've been coming at this from the wrong direction;
I'm not quite sure one would need two compiler instances to recompile a single function.

TCC allows you to allocate memory for an object/function/program yourself- you call tcc_relocate with NULL for the 'ptr' parameter, and it returns
the number of bytes needed to hold the object.
This means that that memory is no longer bound to or managed by TCC.

I know when I've adapted TCC to cache function handlers, I just update the pointer to the handler function and then
free() the old pointer; but this was single-threaded use, and I was disposing of TCC in between (re)compilations.

For multi-threaded use, I believe you would just need a mutex to synchronize access the pointer.

Have you tried this? Or is there some other stuff your code is doing that needs two compiler instances?

-Steve

reply via email to

[Prev in Thread] Current Thread [Next in Thread]