Skip to content

Commit

Permalink
JuvixTree "apply" transformation (#2595)
Browse files Browse the repository at this point in the history
* Moves the "apply" transformation from JuivxAsm to JuvixTree. This
transformation removes the `CallClosures` nodes.
* Makes Nockma compilation tests use JuvixTree instead of JuvixAsm
files.
* Depends on #2594 
* Depends on #2590
* Depends on #2589 
* Depends on #2587
  • Loading branch information
lukaszcz authored Jan 30, 2024
1 parent 4c5050c commit 1153f6b
Show file tree
Hide file tree
Showing 42 changed files with 305 additions and 455 deletions.
1 change: 1 addition & 0 deletions app/Commands/Dev/Nockma/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Commands.Dev.Nockma.Eval where

import Commands.Base hiding (Atom)
import Commands.Dev.Nockma.Eval.Options
import Juvix.Compiler.Nockma.EvalCompiled
import Juvix.Compiler.Nockma.Evaluator.Options
import Juvix.Compiler.Nockma.Pretty
import Juvix.Compiler.Nockma.Translation.FromAsm
Expand Down
8 changes: 4 additions & 4 deletions cntlines.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ function count_ext () {

RUNTIME_C=$(count runtime/src/juvix)
RUNTIME_VAMPIR=$(count_ext '*.pir' runtime/src/vampir)
RUNTIME_JVA=$(count_ext '*.jva' runtime/src/asm)
RUNTIME_JVT=$(count_ext '*.jvt' runtime/src/tree)

RUNTIME=$((RUNTIME_C+RUNTIME_VAMPIR+RUNTIME_JVA))
RUNTIME=$((RUNTIME_C+RUNTIME_VAMPIR+RUNTIME_JVT))

BACKENDC=$(count src/Juvix/Compiler/Backend/C/)
CAIRO=$(count src/Juvix/Compiler/Backend/Cairo/)
Expand Down Expand Up @@ -61,7 +61,7 @@ echo " JuvixTree: $TREE LOC"
echo " JuvixCore: $CORE LOC"
echo "Runtime: $RUNTIME LOC"
echo " C runtime: $RUNTIME_C LOC"
echo " JuvixAsm runtime: $RUNTIME_JVA LOC"
echo " JuvixTree runtime: $RUNTIME_JVT LOC"
echo " VampIR runtime: $RUNTIME_VAMPIR LOC"
echo "Other: $OTHER LOC"
echo " Application: $APP LOC"
Expand All @@ -72,4 +72,4 @@ echo " Data: $DATA LOC"
echo " Prelude: $PRELUDE LOC"
echo "Tests: $TESTS LOC"
echo ""
echo "Total: $TOTAL Haskell LOC + $RUNTIME_C C LOC + $RUNTIME_JVA JuvixAsm LOC + $RUNTIME_VAMPIR VampIR LOC"
echo "Total: $TOTAL Haskell LOC + $RUNTIME_C C LOC + $RUNTIME_JVT JuvixTree LOC + $RUNTIME_VAMPIR VampIR LOC"
193 changes: 0 additions & 193 deletions runtime/src/asm/apply.jva

This file was deleted.

57 changes: 57 additions & 0 deletions runtime/src/tree/apply.jvt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

function juvix_apply_1(*, *) : *;
function juvix_apply_2(*, *, *) : *;
function juvix_apply_3(*, *, *, *) : *;
function juvix_apply_4(*, *, *, *, *) : *;

function juvix_apply_1(*, *) : * {
br(eq(1, argsnum(arg[0]))) {
true: call(arg[0], arg[1])
false: cextend(arg[0], arg[1])
}
}

function juvix_apply_2(*, *, *) : * {
save[n](argsnum(arg[0])) {
br(eq(2, n)) {
true: call(arg[0], arg[1], arg[2])
false: br(eq(1, n)) {
true: call[juvix_apply_1](call(arg[0], arg[1]), arg[2])
false: cextend(arg[0], arg[1], arg[2])
}
}
}
}

function juvix_apply_3(*, *, *, *) : * {
save[n](argsnum(arg[0])) {
br(eq(3, n)) {
true: call(arg[0], arg[1], arg[2], arg[3])
false: br(lt(3, n)) {
true: cextend(arg[0], arg[1], arg[2], arg[3])
false: br(eq(2, n)) {
true: call[juvix_apply_1](call(arg[0], arg[1], arg[2]), arg[3])
false: call[juvix_apply_2](call(arg[0], arg[1]), arg[2], arg[3])
}
}
}
}
}

function juvix_apply_4(*, *, *, *, *) : * {
save[n](argsnum(arg[0])) {
br(eq(4, n)) {
true: call(arg[0], arg[1], arg[2], arg[3], arg[4])
false: br(lt(4, n)) {
true: cextend(arg[0], arg[1], arg[2], arg[3], arg[4])
false: br(eq(3, n)) {
true: call[juvix_apply_1](call(arg[0], arg[1], arg[2], arg[3]), arg[4])
false: br(eq(2, n)) {
true: call[juvix_apply_2](call(arg[0], arg[1], arg[2]), arg[3], arg[4])
false: call[juvix_apply_3](call(arg[0], arg[1]), arg[2], arg[3], arg[4])
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/Juvix/Compiler/Asm/Extra/Recursors.hs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ recurse' sig = go True

fixMemCallClosures :: Memory -> InstrCallClosures -> Sem r Memory
fixMemCallClosures mem InstrCallClosures {..} = do
when (_callClosuresArgsNum < 1) $
throw $
AsmError loc "invalid closure call: expected at least one supplied argument"
when (null (mem ^. memoryValueStack)) $
throw $
AsmError loc "invalid closure call: value stack is empty"
Expand Down
6 changes: 3 additions & 3 deletions src/Juvix/Compiler/Asm/Language.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ newtype InstrExtendClosure = InstrExtendClosure

data InstrCall = InstrCall
{ _callType :: CallType,
-- | The number of arguments supplied to the call.
-- | The number of arguments supplied to the call. Can be 0.
_callArgsNum :: Int
}

newtype InstrCallClosures = InstrCallClosures
{ -- | The number of arguments supplied to the call. This does not include the
-- called closure on top of the stack.
{ -- | The number of arguments supplied to the call. Should be greater than 0.
-- This does not include the called closure on top of the stack.
_callClosuresArgsNum :: Int
}

Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Asm/Pipeline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ toReg' = validate >=> filterUnreachable >=> computeStackUsage >=> computePreallo
-- | Perform transformations on JuvixAsm necessary before the translation to
-- Nockma
toNockma' :: (Members '[Error AsmError, Reader Options] r) => InfoTable -> Sem r InfoTable
toNockma' = validate >=> computeApply >=> filterUnreachable >=> computeTempHeight
toNockma' = validate >=> filterUnreachable >=> computeTempHeight

toReg :: (Members '[Error JuvixError, Reader EntryPoint] r) => InfoTable -> Sem r InfoTable
toReg = mapReader fromEntryPoint . mapError (JuvixError @AsmError) . toReg'
Expand Down
2 changes: 0 additions & 2 deletions src/Juvix/Compiler/Asm/Transformation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ module Juvix.Compiler.Asm.Transformation
( module Juvix.Compiler.Asm.Transformation.StackUsage,
module Juvix.Compiler.Asm.Transformation.Prealloc,
module Juvix.Compiler.Asm.Transformation.Validate,
module Juvix.Compiler.Asm.Transformation.Apply,
module Juvix.Compiler.Asm.Transformation.FilterUnreachable,
module Juvix.Compiler.Asm.Transformation.TempHeight,
)
where

import Juvix.Compiler.Asm.Transformation.Apply
import Juvix.Compiler.Asm.Transformation.FilterUnreachable
import Juvix.Compiler.Asm.Transformation.Prealloc
import Juvix.Compiler.Asm.Transformation.StackUsage
Expand Down
Loading

0 comments on commit 1153f6b

Please sign in to comment.