Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve IL for Condition Expressions
Browse files Browse the repository at this point in the history
Rather than relying on dup and storing the value on the statck while we
perform the `#f` check we can instead store the temporary value in a
local. This simplifies the codepath for the `true` case.
iwillspeak committed Jun 22, 2024
1 parent 7635818 commit 0a758ad
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/Feersum.CompilerServices/Compile/Compiler.fs
Original file line number Diff line number Diff line change
@@ -388,26 +388,29 @@ module private Utils =
| BoundExpr.Load storage -> readFrom ctx storage
| BoundExpr.If(cond, ifTrue, maybeIfFalse) ->
let lblTrue = ctx.IL.Create(OpCodes.Nop)
let lblNotBool = ctx.IL.Create(OpCodes.Nop)
let lblEnd = ctx.IL.Create(OpCodes.Nop)
let condTemp = makeTemp ctx ctx.Assm.MainModule.TypeSystem.Object

// ILAAA : <expression for cond>
// ILBBB : dup
// ILAAA : stloc condTemp
// ILBBB : ldloc condTemp
// ILBBB : isinst bool
// ILBBB : brfalse ILlblNotBool
// ILBBB : brtrue ILlblTrue
// ILCCC : <false expression>
// ILBBB : brfalse ILlblTrue
// ILCCC : ldloc condTemp
// ILCCC : brtrue ILlblTrue
// ILDDD : <false expression>
// ILXXX : br ILlblEnd ; or just `ret` for tail calls
// ILlblNotBool: pop
// ILlblTrue : <true expression>
// ILlblEnd : nop ; only if not tail call

emitExpression ctx false cond

ctx.IL.Emit(OpCodes.Dup)
ctx.IL.Emit(OpCodes.Stloc, condTemp)
ctx.IL.Emit(OpCodes.Ldloc, condTemp)
ctx.IL.Emit(OpCodes.Isinst, ctx.Assm.MainModule.TypeSystem.Boolean)
ctx.IL.Emit(OpCodes.Brfalse, lblNotBool)
ctx.IL.Emit(OpCodes.Brfalse, lblTrue)

ctx.IL.Emit(OpCodes.Ldloc, condTemp)
ctx.IL.Emit(OpCodes.Unbox_Any, ctx.Assm.MainModule.TypeSystem.Boolean)
ctx.IL.Emit(OpCodes.Brtrue, lblTrue)

@@ -429,8 +432,6 @@ module private Utils =
else
ctx.IL.Emit(OpCodes.Br, lblEnd)

ctx.IL.Append(lblNotBool)
ctx.IL.Emit(OpCodes.Pop)
ctx.IL.Append(lblTrue)
recurse ifTrue

0 comments on commit 0a758ad

Please sign in to comment.