Does PowerShell compile scripts?

PowerShell V2 and earlier never compiled a script, it was always interpreted via virtual “eval” methods in the parse tree. PowerShell V3 and later compile the parse tree (the AST) to a LINQ expression tree, which is then compiled to a bytecode, which is then interpreted. If the script is executed 16 times, the script … Read more

Generate tail call opcode

C# compiler does not give you any guarantees about tail-call optimizations because C# programs usually use loops and so they do not rely on the tail-call optimizations. So, in C#, this is simply a JIT optimization that may or may not happen (and you cannot rely on it). F# compiler is designed to handle functional … Read more

Call and Callvirt

When the runtime executes a call instruction it’s making a call to an exact piece of code (method). There’s no question about where it exists. Once the IL has been JITted, the resulting machine code at the call site is an unconditional jmp instruction. By contrast, the callvirt instruction is used to call virtual methods … Read more

What does beforefieldinit flag do?

See my article on this very issue. Basically, beforefieldinit means “the type can be initialized at any point before any static fields are referenced.” In theory that means it can be very lazily initialized – if you call a static method which doesn’t touch any fields, the JIT doesn’t need to initialize the type. In … Read more

General purpose FromEvent method

Here you go: internal class TaskCompletionSourceHolder { private readonly TaskCompletionSource<object[]> m_tcs; internal object Target { get; set; } internal EventInfo EventInfo { get; set; } internal Delegate Delegate { get; set; } internal TaskCompletionSourceHolder(TaskCompletionSource<object[]> tsc) { m_tcs = tsc; } private void SetResult(params object[] args) { // this method will be called from emitted IL … Read more