Compile-time calculation: Difference between revisions
Content added Content deleted
(→{{header|Visual Basic .NET}}: Moved Compiler note to correct location) |
(→{{header|C sharp}}: Added C#) |
||
Line 251: | Line 251: | ||
leave |
leave |
||
ret</lang> |
ret</lang> |
||
⚫ | |||
'''Compiler:''' Roslyn C#, language version 7.3 |
|||
The Roslyn compiler performs constant folding at compile-time and emits IL that contains the result. |
|||
<lang csharp>using System; |
|||
public static class Program |
|||
{ |
|||
public const int FACTORIAL_10 = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1; |
|||
static void Main() |
|||
{ |
|||
Console.WriteLine(FACTORIAL_10); |
|||
} |
|||
}</lang> |
|||
{{out|Emitted IL|note=disassembled with ILSpy}} |
|||
<!--CIL assembly has a similar overall syntax to C#, so colorize it as such.--> |
|||
<lang csharp>.class public auto ansi abstract sealed beforefieldinit Program |
|||
extends [System.Runtime]System.Object |
|||
{ |
|||
// Fields |
|||
.field public static literal int32 FACTORIAL_10 = int32(3628800) |
|||
// Methods |
|||
.method private hidebysig static |
|||
void Main () cil managed |
|||
{ |
|||
// Method begins at RVA 0x2050 |
|||
// Code size 11 (0xb) |
|||
.maxstack 8 |
|||
.entrypoint |
|||
IL_0000: ldc.i4 3628800 |
|||
IL_0005: call void [System.Console]System.Console::WriteLine(int32) |
|||
IL_000a: ret |
|||
} // end of method Program::Main |
|||
} // end of class Program</lang> |
|||
Note that the constant field is generated only when both it and the containing class are visible outside of the assembly. |
|||
Constant expressions that appear outside of constant declarations are also folded, so |
|||
<lang csharp>using System; |
|||
static class Program |
|||
{ |
|||
static void Main() |
|||
{ |
|||
Console.WriteLine(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1); |
|||
} |
|||
}</lang> |
|||
and |
|||
<lang csharp>using System; |
|||
static class Program |
|||
{ |
|||
static void Main() |
|||
{ |
|||
int factorial; |
|||
factorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1; |
|||
Console.WriteLine(factorial); |
|||
} |
|||
}</lang> |
|||
produce the same IL, except without the field. |
|||
{{out|Emitted IL|note=disassembled with ILSpy}} |
|||
<lang csharp>.class private auto ansi abstract sealed beforefieldinit Program |
|||
extends [System.Runtime]System.Object |
|||
{ |
|||
// Methods |
|||
.method private hidebysig static |
|||
void Main () cil managed |
|||
{ |
|||
// Method begins at RVA 0x2050 |
|||
// Code size 11 (0xb) |
|||
.maxstack 8 |
|||
.entrypoint |
|||
IL_0000: ldc.i4 3628800 |
|||
IL_0005: call void [System.Console]System.Console::WriteLine(int32) |
|||
IL_000a: ret |
|||
} // end of method Program::Main |
|||
} // end of class Program</lang> |
|||
{{out}} |
|||
<pre>3628800</pre> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 1,138: | Line 1,234: | ||
<!--CIL assembly has a similar overall syntax to C#, so colorize it as such.--> |
<!--CIL assembly has a similar overall syntax to C#, so colorize it as such.--> |
||
<lang |
<lang csharp>.class private auto ansi sealed Program |
||
extends [System.Runtime]System.Object |
extends [System.Runtime]System.Object |
||
{ |
{ |
||
Line 1,187: | Line 1,283: | ||
{{out|Emitted IL|note=disassembled with ILSpy}} |
{{out|Emitted IL|note=disassembled with ILSpy}} |
||
<lang |
<lang csharp>.class private auto ansi sealed Program |
||
extends [System.Runtime]System.Object |
extends [System.Runtime]System.Object |
||
{ |
{ |
||
Line 1,297: | Line 1,393: | ||
{{omit from|Bc}} |
{{omit from|Bc}} |
||
{{omit from|Brlcad}} |
{{omit from|Brlcad}} |
||
⚫ | |||
{{omit from|E}} |
{{omit from|E}} |
||
{{omit from|Gambas}} |
{{omit from|Gambas}} |