Compile-time calculation: Difference between revisions
Content added Content deleted
(→{{header|Visual Basic .NET}}: Added VB.NET) |
|||
Line 1,122: | Line 1,122: | ||
</pre> |
</pre> |
||
=={{header|Visual Basic .NET}}== |
|||
The Roslyn compiler performs constant folding at compile-time and emits IL that contains the result. |
|||
<lang vbnet>Module Program |
|||
Const FACTORIAL_10 = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 |
|||
Sub Main() |
|||
Console.WriteLine(FACTORIAL_10) |
|||
End Sub |
|||
End Module</lang> |
|||
{{out|Emitted IL|note=disassembled with ILSpy}} |
|||
'''Compiler:''' Roslyn Visual Basic, language version 15.8 |
|||
<!--CIL assembly has a similar overall syntax to C#, so colorize it as such.--> |
|||
<lang Csharp>.class private auto ansi sealed Program |
|||
extends [System.Runtime]System.Object |
|||
{ |
|||
.custom instance void Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute::.ctor() = ( |
|||
01 00 00 00 |
|||
) |
|||
// Fields |
|||
.field private static literal int32 FACTORIAL_10 = int32(3628800) |
|||
// Methods |
|||
.method public static |
|||
void Main () cil managed |
|||
{ |
|||
.custom instance void [System.Runtime]System.STAThreadAttribute::.ctor() = ( |
|||
01 00 00 00 |
|||
) |
|||
// Method begins at RVA 0x2060 |
|||
// 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> |
|||
Constant expressions that appear outside of constant declarations are also folded, so |
|||
<lang vbnet>Module Program |
|||
Sub Main() |
|||
Console.WriteLine(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1) |
|||
End Sub |
|||
End Module</lang> |
|||
and |
|||
<lang vbnet>Module Program |
|||
Sub Main() |
|||
Dim factorial As Integer |
|||
factorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 |
|||
Console.WriteLine(factorial) |
|||
End Sub |
|||
End Module</lang> |
|||
produce the same IL, albeit without the constant field that other assemblies can reference. |
|||
{{out|Emitted IL|note=disassembled with ILSpy}} |
|||
<lang Csharp>.class private auto ansi sealed Program |
|||
extends [System.Runtime]System.Object |
|||
{ |
|||
.custom instance void Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute::.ctor() = ( |
|||
01 00 00 00 |
|||
) |
|||
// Methods |
|||
.method public static |
|||
void Main () cil managed |
|||
{ |
|||
.custom instance void [System.Runtime]System.STAThreadAttribute::.ctor() = ( |
|||
01 00 00 00 |
|||
) |
|||
// Method begins at RVA 0x2060 |
|||
// 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|XLISP}}== |
=={{header|XLISP}}== |