Recent

Author Topic: cppdecl examples  (Read 4160 times)

maniac

  • Newbie
  • Posts: 4
cppdecl examples
« on: December 13, 2019, 01:24:08 pm »
Hello!

I've accidentally came across the cppdecl call convention while browsing some old forum posts. I have understood that it's something very very limited and should only be used with caution, but I was not able to find any documentation or any examples with it.

Does someone have any experience with it? How is it different from cdecl? Any kind of example would be greatly appreciated, as I would to avoid inspecting the generated asm on godbolt. Thanks!

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: cppdecl examples
« Reply #1 on: December 14, 2019, 10:55:14 am »
It's mainly used together with cppclass with the intention to directly link against C++ classes. However this feature is still in its infancy and currently only supports static methods and the GCC name mangling. Also the mapping between Pascal types and C++ types is not yet as finegrained as it needs to be (e.g. long and int on32-bit platforms have the same size, but are mangled differently; in FPC that currently can't be achieved).
I have plans to further improve this (including inheritance, virtual methods and MSVC support), but this is fairly low priority.

What exactly are you trying to achieve? Maybe we can help you there.

maniac

  • Newbie
  • Posts: 4
Re: cppdecl examples
« Reply #2 on: December 14, 2019, 03:42:52 pm »
Thanks you so much for the details.

I was trying to find an alternative way to use some C++ business logic that I am sharing across different platforms. Ideally, I would use some code generation tools (like Swig) to generate a pure C api, but there doesn't seem to be any decent option (prototype old branches in Swig doesn't count). Do you happen to know any? If it makes any difference, I only need Windows compatibility.

If I can't get any automatic way of generating the c wrappers, I'll probably have to write and maintain them manually.

Thaddy

  • Hero Member
  • *****
  • Posts: 14172
  • Probably until I exterminate Putin.
Re: cppdecl examples
« Reply #3 on: December 14, 2019, 04:44:58 pm »
Pure C wrappers are usually not a problem. But you asked C++
Specialize a type, not a var.

maniac

  • Newbie
  • Posts: 4
Re: cppdecl examples
« Reply #4 on: December 14, 2019, 05:03:17 pm »
They are not, indeed. I was just hoping I would not have to write and maintain them by hand is all.

guest64953

  • Guest
Re: cppdecl examples
« Reply #5 on: December 14, 2019, 05:47:54 pm »
It's mainly used together with cppclass with the intention to directly link against C++ classes. However this feature is still in its infancy and currently only supports static methods and the GCC name mangling. Also the mapping between Pascal types and C++ types is not yet as finegrained as it needs to be (e.g. long and int on32-bit platforms have the same size, but are mangled differently; in FPC that currently can't be achieved).
I have plans to further improve this (including inheritance, virtual methods and MSVC support), but this is fairly low priority.

What exactly are you trying to achieve? Maybe we can help you there.

This is more than enough. GCC is very popular, even on Windows, many people are using MinGW. It's only a bit less popular since clang's popularization. MS is about to move to a clang based compiler. Embarcadero already did it with their new BCC32C/BCC32X C++ 11 compiler. Check it here: https://www.embarcadero.com/free-tools/ccompiler

So clang is a potential target to support. Don't care about MSVC, it will be the past soon  ;D

guest64953

  • Guest
Re: cppdecl examples
« Reply #6 on: December 14, 2019, 05:52:39 pm »
They are not, indeed. I was just hoping I would not have to write and maintain them by hand is all.

I fear that you have to write it yourself.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11356
  • FPC developer.
Re: cppdecl examples
« Reply #7 on: December 15, 2019, 12:20:32 am »
So clang is a potential target to support. Don't care about MSVC, it will be the past soon  ;D

The fact that a new MSVC might be clang based, does not mean the result will be compatible with the OSS clang. They probably will have compatible MSVC name mangling in that Clang based derivate, so that it changes the compiler inner workings really doesn't mean compatibility

guest64953

  • Guest
Re: cppdecl examples
« Reply #8 on: December 15, 2019, 01:29:19 pm »
So clang is a potential target to support. Don't care about MSVC, it will be the past soon  ;D

The fact that a new MSVC might be clang based, does not mean the result will be compatible with the OSS clang. They probably will have compatible MSVC name mangling in that Clang based derivate, so that it changes the compiler inner workings really doesn't mean compatibility

So the near future goal is just GCC  ;)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11356
  • FPC developer.
Re: cppdecl examples
« Reply #9 on: December 15, 2019, 02:19:23 pm »
So the near future goal is just GCC  ;)

GCC is heavenly reduced as default compiler in targets. The BSDs and Macs are gone.

I think in the end you need to support multiple compilers, at least on windows.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: cppdecl examples
« Reply #10 on: December 15, 2019, 02:31:55 pm »
GCC is heavenly reduced as default compiler in targets.
The angel choirs must be having a seasonal effect.
I think you mean "heavily" reduced...  :)

guest64953

  • Guest
Re: cppdecl examples
« Reply #11 on: December 15, 2019, 02:44:55 pm »
So the near future goal is just GCC  ;)

GCC is heavenly reduced as default compiler in targets. The BSDs and Macs are gone.

I think in the end you need to support multiple compilers, at least on windows.

I'm a BSD user but I've to admit they are insignificiant. Mac is always it own problem. On Linux, GCC still is the default compiler. So nothing to worry. Support GCC first, then Clang, then others.

It's also weird. The fact both of you (GCC and FPC) are GPL-ed but why I found you hate each other.

Only LLVM Pascal could have the right to said GCC is the past and the future is Clang. But indeed there is no LLVM Pascal. This also proved Pascal is a insignificiant language, because nearly all of other languages, if having GCC frontend, also have LLVM frontend. Where is my LLVM Pascal? Oh, I think if it exists it should be called LLVM PLang or something like that  ;)

guest64953

  • Guest
Re: cppdecl examples
« Reply #12 on: December 15, 2019, 02:45:49 pm »
GCC is heavenly reduced as default compiler in targets.
The angel choirs must be having a seasonal effect.
I think you mean "heavily" reduced...  :)

I also think he means "heavily"  ;)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11356
  • FPC developer.
Re: cppdecl examples
« Reply #13 on: December 15, 2019, 06:04:16 pm »
GCC is heavenly reduced as default compiler in targets.
The angel choirs must be having a seasonal effect.
I think you mean "heavily" reduced...  :)

Hehe, yes. The exception where both words are acceptable to spelling control. I do have to admit I rather rely on it when writing replies quickly.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: cppdecl examples
« Reply #14 on: December 15, 2019, 06:18:39 pm »
So clang is a potential target to support. Don't care about MSVC, it will be the past soon  ;D

The fact that a new MSVC might be clang based, does not mean the result will be compatible with the OSS clang. They probably will have compatible MSVC name mangling in that Clang based derivate, so that it changes the compiler inner workings really doesn't mean compatibility

So the near future goal is just GCC  ;)
It's not about the compiler, that I used "GCC" and "MSVC" is a bit misleading in fact. What is important is the ABI. And there we have two big ones that need to be supported: Sys V ABI (which is used by GCC and clang (on non-Windows)) and the MS ABI (which is used by MSVC as well as clang on Windows). And yes, support for the MS ABI is important as well, cause this feature is about supporting third party C++ libraries which are often provided in a MS ABI flavor for Windows (e.g. Qt).

Also there is no "near future goal". This is a long term, low priority feature.

I was trying to find an alternative way to use some C++ business logic that I am sharing across different platforms. Ideally, I would use some code generation tools (like Swig) to generate a pure C api, but there doesn't seem to be any decent option (prototype old branches in Swig doesn't count). Do you happen to know any? If it makes any difference, I only need Windows compatibility.
It might be best to extend Swig to finally generate Delphi/FPC wrappers. I had given it a try some years ago as well, but then I stumpled upon some problems and lost interest again...

 

TinyPortal © 2005-2018