[Mono-list] ldtoken with ByRef, question

DraconisBG draconisbg at yahoo.com
Fri Nov 21 16:21:52 EST 2008


I'll tell my problem in very brief, perhaps the experts will get it
immediately as I might be missing something obvious:

ILGenerator.Emit(OpCodes.ldtoken, Type t) seems to emit "ldtoken int32"
instead of "ldtoken int32&" for me, when t is an out parameter of type int32
(i.e. an int32&). So it does not work for ByRef types emulated by
Reflection. If this is the case, how can I emit ldtoken int32& ?

The slightly longer explanation (any help would be appreciated):

I have a task where I have to modify gmcs, so it builds certain things into
the target application it compiles. In brief, the target application creates
dynamic methods for certain purposes, and later calls them.  The code in
these dynamic methods call those certain methods in runtime, with their
arguments; anyway, it would be complicated to explain it in brief. To sum
up, the DynamicMethod created by the target application (i.e. by the IL code
I emit to it) has the same signature as the method it will call.
For example, if we have Method(int a, int b), then Dynamic method created
on-the-fly will have (int a, int b) signature too. The dynamic method's code
simply calls the normal method, basically just forwarding the arguments
(ldarg a, ldarg b etc. -- I mean pseudocode here, of course).

For performance reasons, a delegate is created for the dynamicmethod too,
and that is what I use to call the dynamicmethod.

The problem is:

The solution includes copying the parameter type list of certain methods,
adding them to arrays accessible by the *emitted* IL code. This is required
because the target application instantiates the DynamicMethod, so it needs
to know the arguments. So I use GMCS to emit IL code to the target
application that does this; i.e. creates an array of System.Type references,
and sets the references to the particular method's parameter types (as those
are known in gmcs compile time).

So I have an array, argtypes[i] in gmcs. To make a runtime array from it, I
declare an array, and fill it in a loop:
 ilgen.Emit(OpCodes.Ldtoken, argtypes[j]);
 ilgen.Emit(OpCodes.Call, getTypeFromHandle);
So when the application is run, it will initialize the parameter array for
the DynamicMethod with this IL code. It works PERFECTLY for everything (the
application works like a charm), except for ByRef parameters. So the

E.g. the parameter is an "out int" parameter, i.e. int32&. I checked it,
argtypes[i] is correct for it, IsByRef returns true etc. The DELEGATE type
is also created properly (the corresponding parameter type is int32&), but
not the DYNAMICMETHOD instantiation code: in the dissambled IL, it contains
"ldtoken int32" instead of "ldtoken int32&"!

In other words, ilgenerator.Emit(ldtoken, int32&) seems to emit "ldtoken
int32" !

I checked if there is a related gmcs bug reported or if I am missing
something (I used google too), no results. So I thought I would ask it here,
many many thanks in advance for any help!
View this message in context: http://www.nabble.com/ldtoken-with-ByRef%2C-question-tp20629727p20629727.html
Sent from the Mono - General mailing list archive at Nabble.com.

More information about the Mono-list mailing list