5

I know .NET and Mono are binary compatible but given a set of source code, will csc and mcs produce the exact same 100% identical binary CLI executable? Would one be able to tell whether an executable was compiled with csc or mcs?

Jake Petroules
  • 23,472
  • 35
  • 144
  • 225

2 Answers2

5

A lot of things are not fully defined in the spec, or are implementation-specific extensions.

Examples of not-fully-specified:

  • the synchronisation semantics of field-like events; this is explicitly open to implementations in the ecma spec; it is stricty defined in the MS spec, but uses a different version in c# 4.0 which does not yet appear in the formal spec, IIRC
  • Expression construction (from lambdas); is simply "defined elsewhere" (actually, it isn't)

Examples of implementation extensions:

  • P/Invoke
  • COM interface handling (i.e. how you can call new on an interface)

So no: it is not guarantees to have the same IL, either between csc or [g]mcs - but even between different versions of csc.

Even more: depending on debug settings, optimizations being enabled or not, and some compilation constants being defined (such as DEBUG or TRACE), the same compiler will generate different code.

skolima
  • 31,963
  • 27
  • 115
  • 151
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

I'm sure they don't produce the same IL from the same sourcecode. Even different versions of the MS C# compiler don't.

The optimizer will work differently and create slightly different code.

I expect larger differences in the implementation of complex features such as iterators, local variable captured for lamdas,...

Then there are arbitrary compiler generated names, for example for anonymous types. No reason why they should use the same naming scheme for them.

I wouldn't be surprised if there was some assembly metadata containing the name and version of your compiler too.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • The optimizer lives in the jitter, not the compiler. – Hans Passant May 01 '11 at 17:54
  • 1
    @Hans: the compiler does some optimizations though: `string x = "a" + s0 + s1;` => `string x = string.Concat("a", s0, s1);` springs to mind. I doubt they differ in this one example though. – R. Martinho Fernandes May 01 '11 at 17:56
  • AFAIK both the jitter and the compiler have some optimizers. But you're right that the jit optimizer does most micro optimizations. – CodesInChaos May 01 '11 at 17:56
  • Meh, no, these are not 'micro' optimizations. More about it here: http://stackoverflow.com/questions/4043821/performance-differences-between-debug-and-release-builds/4045073#4045073 The same kind as optimizations normally performed by a C compiler. – Hans Passant May 01 '11 at 18:23