Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6197877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:53:58+00:00 2026-05-24T03:53:58+00:00

I have a recursive function emit : Map<string,LocalBuilder> -> exp -> unit where il

  • 0

I have a recursive function emit : Map<string,LocalBuilder> -> exp -> unit where il : ILGenerator is global to the function and exp is a discriminant union representing a type-checked parsed language with case InstanceCall of exp * MethodInfo * exp list * Type and Type is a property on exp representing the Type of the expression.

In the following fragment, I am trying to emit IL opcodes for an instance call where instance.Type may or may not be a ValueType. So I understand I can use OpCodes.Constrained to flexibly and efficiently make virtual calls on reference, value, and enum types. I’m new to Reflection.Emit and machine languages in general so understanding the linked documentation for OpCodes.Constrained is not strong for me.

Here is my attempt, but it results in a VerificationException, “Operation could destabilize the runtime.”:

let rec emit lenv ast =
    match ast with
    ...
    | InstanceCall(instance,methodInfo,args,_) ->
        instance::args |> List.iter (emit lenv)
        il.Emit(OpCodes.Constrained, instance.Type)
        il.Emit(OpCodes.Callvirt, methodInfo)
    ...

Looking at the docs, I think the key may be “A managed pointer, ptr, is pushed onto the stack. The type of ptr must be a managed pointer (&) to thisType. Note that this is different from the case of an unprefixed callvirt instruction, which expects a reference of thisType.”

Update

Thank you @Tomas and @desco, I now understand when to use OpCodes.Constrained (instance.Type is a ValueType, but methodInfo.DeclaringType is a reference type).

But it turns out I don’t need to consider that case just yet, and my real problem was the instance argument on the stack: it only took me 6 hours to learn it needs an address instead of the value (looking at the DLR source code gave me clues, and then using ilasm.exe on a simple C# program made it clear).

Here is my final working version:

let rec emit lenv ast =
    match ast with
    | Int32(x,_) -> 
        il.Emit(OpCodes.Ldc_I4, x)
    ...
    | InstanceCall(instance,methodInfo,args,_) ->
        emit lenv instance
        //if value type, pop, put in field, then load the field address
        if instance.Type.IsValueType then
            let loc = il.DeclareLocal(instance.Type)
            il.Emit(OpCodes.Stloc, loc)
            il.Emit(OpCodes.Ldloca, loc)

        for arg in args do emit lenv arg

        if instance.Type.IsValueType then
            il.Emit(OpCodes.Call, methodInfo)
        else
            il.Emit(OpCodes.Callvirt, methodInfo)
        ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:53:59+00:00Added an answer on May 24, 2026 at 3:53 am

    I think that the bit of documentation that you quoted at the end of the question is the source of the problem. I’m not quite sure what the OpCodes.Constrained prefix is for (I don’t understand the documentation better than you), but I tried looking how it is used by Microsoft :-).

    Here is a snippet from source code of Dynamic Language Runtime that emits a method call:

    // Emit arguments
    List<WriteBack> wb = EmitArguments(mi, args);
    
    // Emit the actual call
    OpCode callOp = UseVirtual(mi) ? OpCodes.Callvirt : OpCodes.Call;
    if (callOp == OpCodes.Callvirt && objectType.IsValueType) {
        // This automatically boxes value types if necessary.
        _ilg.Emit(OpCodes.Constrained, objectType);
    }
    // The method call can be a tail call if [...]
    if ((flags & CompilationFlags.EmitAsTailCallMask) == CompilationFlags.EmitAsTail && 
        !MethodHasByRefParameter(mi)) {
        _ilg.Emit(OpCodes.Tailcall);
    }
    if (mi.CallingConvention == CallingConventions.VarArgs) {
        _ilg.EmitCall(callOp, mi, args.Map(a => a.Type));
    } else {
        _ilg.Emit(callOp, mi);
    }
    
    // Emit writebacks for properties passed as "ref" arguments
    EmitWriteBack(wb);
    

    I think you’ll probably want to follow their behavior – it seems that the constrained prefix is only used for virtual calls on value types. My interpretation is that for value types, you know what is the actual type, so you don’t need actual (unconstrained) virtual call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recursive function which walk nested dict and return needed key's value: def
I have a simple recursive array function that looks like this: function recursive_array($results) {
I have the following example of a recursive function, and what I don't understand
I have seen the other Questions on SO about the Recursive function and I
I have defined a type and a function: type element = ... let merge
I am having problems running the below recursive function on our development web server.
I have one question about sys.setrecursionlimit() From the python docs this function: Set the
It seems every time I go to write a recursive function I end up
I have these Django models: class Group(models.Model): name = models.CharField(max_length=100) parent_group = models.ManyToManyField("self", blank=True)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.