r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Red] Red language group

Dockimbel
19-Sep-2011
[3414x3]
The current resolver only works for simple cases, basically, only 
when you're directly passing a function pointer as argument to a 
function.
I need to add the unresolved calling convention error routine.
When such error will be reported, users would need to provide manually 
this information, by adding 'cdecl or 'stdcall as attribute.
Kaj
19-Sep-2011
[3417]
I don't really see a connection between the function a pointer is 
passed to and the function that will eventually call it
Dockimbel
19-Sep-2011
[3418]
Well, it depends if the function taking a function pointer is invoking 
it directly or if it's passing it to another function (local or imported). 
In the former case, the compiler can set the right cconv itself, 
in the latter case, it can't and need some help from the user.
Kaj
19-Sep-2011
[3419]
Yes, so is the current inferer always guessing right?
Dockimbel
19-Sep-2011
[3420x3]
Let me have a look at the specs, I remember there was an edge case 
documented there...
No trace of any special cases in the specs anymore. So, the compiler 
is always guessing right, when it can guess. When it can't, it leaves 
the cconv in undetermined state (??).
The inferer is very simple, it just looks at the cconv of the calling 
function and propagates it to the callback. If the callback has already 
a cconv defined, it checks if both cconv matches, and raise an error 
if it's not the case.
Kaj
19-Sep-2011
[3423x2]
Not much to report yet, except this:
bash-4.0# hello-SQLite-world                                     
                                                                 
           
SQLite version: 3.6.22
SQLite version number: 3006022

SQLite source ID: 2010-01-05 15:30:36 28d0d7710761114a44a1a3a425a6883c661f06e7
Dockimbel
19-Sep-2011
[3425x2]
Good start!
Ok, these are the changes I will make today wrt cconv resolution:

- 'cdecl and 'stdcall keywords will be accepted as function attributes.

- 'callback attribute remains, it lets the compiler decide on the 
cconv to apply. If it fails to solve it, an error will be raised 
and user will have to manually set it instead (replacing 'callback 
by 'cdecl or 'stdcall).
BrianH
19-Sep-2011
[3427]
You can only have one function attribute?
Pekr
19-Sep-2011
[3428]
then we need typesets and attribute sets :-)
Dockimbel
19-Sep-2011
[3429]
BrianH: no, there are several others that can be mixed together, 
but only one from [callback stdcall cdecl].
BrianH
19-Sep-2011
[3430]
So that means that any function declared as 'cdecl or 'stdcall can 
now be used as a callback? Or can all functions?
Dockimbel
19-Sep-2011
[3431x2]
Only those having one of those above 3 attributes can be used as 
"callbacks".
I think I see your concern. It might be probably less confusing to 
leave the 'callback keyword mandatory for "callbacks" and accept 
'stdcall and 'cdecl as additional attributes, but it is a bit more 
verbose also.
Kaj
19-Sep-2011
[3433]
What's the difference between a cdecl callback and a regular cdecl 
function?
Dockimbel
19-Sep-2011
[3434]
IIRC, no differences, the callback attribute was just meant to help 
the compiler figure out that the cconv has to be solved at compile-time.
Kaj
19-Sep-2011
[3435]
I thought so, so why not drop the callback attribute and let the 
programmer decide whether it is used as a callback or not?
Dockimbel
19-Sep-2011
[3436x3]
Yes, it would be the logical thing to do.
But in such case, users woud have to always set the cconv manually 
and the compiler wouldn't be able to help catch errors.
The current 'callback attribute triggers the compiler inferer and 
cconv errors.
Kaj
19-Sep-2011
[3439]
No, I mean your original plan was alright
Dockimbel
19-Sep-2011
[3440]
Well, having just "cdecl" or "stdcall" might not hint enough the 
reader that the function is used as a callback...
Kaj
19-Sep-2011
[3441]
But that's the point: it may not be
BrianH
19-Sep-2011
[3442]
Just wondering: Why do functions need to be marked as being able 
to be callbacks? Is it to prevent them from being inlined?
Dockimbel
19-Sep-2011
[3443x5]
Answered that a few lines above: "the callback attribute was just 
meant to help the compiler figure out that the cconv has to be solved 
at compile-time."
It is only related to calling convention resolution.
If we get rid of 'callback attribute, then only 'cdecl attribute 
will be required and it will have to be set manually everywhere it 
is needed.
The stdcall wouldn't be required as it is the internal calling convention 
used by native Red/System functions.
In such case, you could use any function as callbacks (as all functions 
calling convention would be known at compile-time).
BrianH
19-Sep-2011
[3448x2]
Ah, cool, I was worried that you were only going to make explicitly 
marked functions usable as callbacks, rather than all functions.
You can omit the 'stdcall attribute for stdcall callbacks?
Dockimbel
19-Sep-2011
[3450x2]
If we drop 'callback attribute, 'stdcall wouldn't be needed, as it 
is the default convention used by Red/System.
This might be the best option currently, as it would just require 
users to change 'callback to 'cdecl in most cases and just drop the 
'callback attribute in other cases.
BrianH
19-Sep-2011
[3452]
Then we could just specify 'callback if we want the system to guess, 
'cdecl if we know it's that, and nothing if stdcall.
Dockimbel
19-Sep-2011
[3453]
No, it can't work that way. If 'callback is used, then we need the 
error system to catch the unresolved cases, so we need to set initially 
every callback function calling convention as "unknown" instead of 
stdcall. For the unresolved cases, the user must be able to set manually 
the convention, so he needs both 'cdecl and 'stdcall to be accepted.
BrianH
19-Sep-2011
[3454x2]
I guess I just don't understand well enough what is done to support 
callbacks. If you specify a 'callback attribute, is there a shim 
created that marshalls arguments from the callback calling convention 
(cdecl or stdcall) to the internal calling convention of the function 
(stdcall or cdecl)? And if you specify 'cdecl or 'stdcall, does that 
affect the internal calling convention so that no shim is required?
(Probably should just read the source)
Dockimbel
19-Sep-2011
[3456x3]
When you use 'callback attribute on a function, the compiler just 
tries to determine what is the appropriate calling convention to 
apply internally to the callback function. In order to acheive that, 
it checks the first caller function that will take the callback as 
argument and set the callback to the same calling convention. This 
part is done by the compiler's front-end, which just tries to set 
the calling convention correctly (just a word! in an internal block!), 
nothing else.


The compiler's back-end just emits the right code for whatever calling 
convention the function has (cdecl, stdcall, syscall, ...).
If you were asking if a runtime conversion was done between calling 
conventions, the answer is no.
Kaj: I think after this discussion that dropping the whole callback 
thing should be the simplest solution. I would only add a 'cdecl 
attribute for functions that needs to be called from C code. Any 
function (with or without 'cdecl attribute) could then be used as 
callback. Would you agree?
Kaj
19-Sep-2011
[3459x2]
Agreed
You may want to add the stdcall attribute already, as well, for when 
you go to fastcall for native functions
Dockimbel
19-Sep-2011
[3461]
Right.
BrianH
19-Sep-2011
[3462]
Sounds good to me too.
Dockimbel
20-Sep-2011
[3463]
BrianH: I need to correct a bit my answer to your question. Actually, 
there is a small code emitter dependency on the 'callback attribute. 
It is used to save special registers (edi, esi, ebx) to conform to 
IA-32 ABI. It is only needed when providing callbacks that are invoked 
by the OS or external libraries.


I should be able to keep this feature in the new revision while making 
it transparent to the users.