Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direct translation from normalized JuvixCore to VampIR #2086

Merged
merged 14 commits into from
May 19, 2023
1 change: 1 addition & 0 deletions app/Commands/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ runCommand opts@CompileOptions {..} = do
TargetNative64 -> Compile.runCPipeline arg
TargetWasm32Wasi -> Compile.runCPipeline arg
TargetGeb -> Compile.runGebPipeline arg
TargetVampIR -> Compile.runVampIRPipeline arg
TargetCore -> writeCoreFile arg
TargetAsm -> Compile.runAsmPipeline arg

Expand Down
1 change: 1 addition & 0 deletions app/Commands/Dev/Asm/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ runCommand opts = do
TargetWasm32Wasi -> return Backend.TargetCWasm32Wasi
TargetNative64 -> return Backend.TargetCNative64
TargetGeb -> exitMsg (ExitFailure 1) "error: GEB target not supported for JuvixAsm"
TargetVampIR -> exitMsg (ExitFailure 1) "error: VampIR target not supported for JuvixAsm"
TargetCore -> exitMsg (ExitFailure 1) "error: JuvixCore target not supported for JuvixAsm"
TargetAsm -> exitMsg (ExitFailure 1) "error: JuvixAsm target not supported for JuvixAsm"

Expand Down
1 change: 1 addition & 0 deletions app/Commands/Dev/Core/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ runCommand opts = do
TargetWasm32Wasi -> runCPipeline arg
TargetNative64 -> runCPipeline arg
TargetGeb -> runGebPipeline arg
TargetVampIR -> runVampIRPipeline arg
TargetCore -> return ()
TargetAsm -> runAsmPipeline arg
where
Expand Down
13 changes: 13 additions & 0 deletions app/Commands/Dev/Core/Compile/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Juvix.Compiler.Asm.Pretty qualified as Asm
import Juvix.Compiler.Backend qualified as Backend
import Juvix.Compiler.Backend.C qualified as C
import Juvix.Compiler.Backend.Geb qualified as Geb
import Juvix.Compiler.Backend.VampIR.Translation qualified as VampIR
import Juvix.Compiler.Core.Data.InfoTable qualified as Core
import System.FilePath (takeBaseName)

Expand All @@ -33,6 +34,7 @@ getEntry PipelineArg {..} = do
TargetWasm32Wasi -> Backend.TargetCWasm32Wasi
TargetNative64 -> Backend.TargetCNative64
TargetGeb -> Backend.TargetGeb
TargetVampIR -> Backend.TargetVampIR
TargetCore -> Backend.TargetCore
TargetAsm -> Backend.TargetAsm

Expand Down Expand Up @@ -79,6 +81,17 @@ runGebPipeline pa@PipelineArg {..} = do
Geb.Result {..} <- getRight (run (runReader entryPoint (runError (coreToGeb spec _pipelineArgInfoTable :: Sem '[Error JuvixError, Reader EntryPoint] Geb.Result))))
embed $ TIO.writeFile (toFilePath gebFile) _resultCode

runVampIRPipeline ::
forall r.
(Members '[Embed IO, App] r) =>
PipelineArg ->
Sem r ()
runVampIRPipeline pa@PipelineArg {..} = do
entryPoint <- getEntry pa
vampirFile <- Compile.outputFile _pipelineArgOptions _pipelineArgFile
VampIR.Result {..} <- getRight (run (runReader entryPoint (runError (coreToVampIR _pipelineArgInfoTable :: Sem '[Error JuvixError, Reader EntryPoint] VampIR.Result))))
embed $ TIO.writeFile (toFilePath vampirFile) _resultCode

runAsmPipeline :: (Members '[Embed IO, App] r) => PipelineArg -> Sem r ()
runAsmPipeline pa@PipelineArg {..} = do
entryPoint <- getEntry pa
Expand Down
1 change: 1 addition & 0 deletions app/Commands/Dev/Core/Compile/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ coreSupportedTargets =
[ TargetWasm32Wasi,
TargetNative64,
TargetGeb,
TargetVampIR,
TargetAsm
]

Expand Down
2 changes: 1 addition & 1 deletion app/Commands/Dev/Core/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ parseCoreCommand =
compileInfo =
info
(CoreCompile <$> parseCoreCompileOptions)
(progDesc "Compile a JuvixCore file to native code, WebAssembly or GEB")
(progDesc "Compile a JuvixCore file to native code, WebAssembly, GEB or VampIR")
4 changes: 4 additions & 0 deletions app/Commands/Extra/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ runCompile inputFile o = do
TargetWasm32Wasi -> runError (clangWasmWasiCompile inputFile o)
TargetNative64 -> runError (clangNativeCompile inputFile o)
TargetGeb -> return $ Right ()
TargetVampIR -> return $ Right ()
TargetCore -> return $ Right ()
TargetAsm -> return $ Right ()

Expand All @@ -42,6 +43,7 @@ prepareRuntime buildDir o = do
TargetNative64 | o ^. compileDebug -> writeRuntime nativeDebugRuntime
TargetNative64 -> writeRuntime nativeReleaseRuntime
TargetGeb -> return ()
TargetVampIR -> return ()
TargetCore -> return ()
TargetAsm -> return ()
where
Expand Down Expand Up @@ -98,6 +100,8 @@ outputFile opts inputFile =
if
| opts ^. compileTerm -> replaceExtension' ".geb" inputFile
| otherwise -> replaceExtension' ".lisp" baseOutputFile
TargetVampIR ->
replaceExtension' ".pir" baseOutputFile
TargetCore ->
replaceExtension' ".jvc" baseOutputFile
TargetAsm ->
Expand Down
2 changes: 2 additions & 0 deletions app/Commands/Extra/Compile/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ data CompileTarget
= TargetWasm32Wasi
| TargetNative64
| TargetGeb
| TargetVampIR
| TargetCore
| TargetAsm
deriving stock (Eq, Data, Bounded, Enum)
Expand All @@ -17,6 +18,7 @@ instance Show CompileTarget where
TargetWasm32Wasi -> "wasm32-wasi"
TargetNative64 -> "native"
TargetGeb -> "geb"
TargetVampIR -> "vampir"
TargetCore -> "core"
TargetAsm -> "asm"

Expand Down
44 changes: 26 additions & 18 deletions cntlines.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
#!/bin/bash

RUNTIME=`find runtime/src/ -name '*.c' -or -name '*.h' | xargs wc -l | tail -1 | tr -d ' toal'`
function count() {
cloc $1 | grep 'SUM:' | awk '{print $5}'
}

BACKENDC=`find src/Juvix/Compiler/Backend/C/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
GEB=`find src/Juvix/Compiler/Backend/Geb/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
REG=`find src/Juvix/Compiler/Reg/ -name '*.hs' -print | xargs wc -l | tail -1 | tr -d ' toal'`
ASM=`find src/Juvix/Compiler/Asm/ -name '*.hs' -print | xargs wc -l | tail -1 | tr -d ' toal'`
CORE=`find src/Juvix/Compiler/Core/ -name '*.hs' -print | xargs wc -l | tail -1 | tr -d ' toal'`
RUNTIME=$(count runtime/src)

CONCRETE=`find src/Juvix/Compiler/Concrete/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
ABSTRACT=`find src/Juvix/Compiler/Abstract/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
INTERNAL=`find src/Juvix/Compiler/Internal/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
BUILTINS=`find src/Juvix/Compiler/Builtins/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
PIPELINE=`find src/Juvix/Compiler/Pipeline/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
BACKENDC=$(count src/Juvix/Compiler/Backend/C/)
GEB=$(count src/Juvix/Compiler/Backend/Geb/)
VAMPIR=$(count src/Juvix/Compiler/Backend/VampIR/)
REG=$(count src/Juvix/Compiler/Reg/)
ASM=$(count src/Juvix/Compiler/Asm/)
CORE=$(count src/Juvix/Compiler/Core/)

APP=`find app/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
HTML=`find src/Juvix/Compiler/Backend/Html/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
EXTRA=`find src/Juvix/Extra/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
DATA=`find src/Juvix/Data/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
PRELUDE=`find src/Juvix/Prelude/ -name '*.hs' | xargs wc -l | tail -1 | tr -d ' toal'`
CONCRETE=$(count src/Juvix/Compiler/Concrete/)
ABSTRACT=$(count src/Juvix/Compiler/Abstract/)
INTERNAL=$(count src/Juvix/Compiler/Internal/)
BUILTINS=$(count src/Juvix/Compiler/Builtins/)
PIPELINE=$(count src/Juvix/Compiler/Pipeline/)

APP=$(count app/)
HTML=$(count src/Juvix/Compiler/Backend/Html/)
EXTRA=$(count src/Juvix/Extra/)
DATA=$(count src/Juvix/Data/)
PRELUDE=$(count src/Juvix/Prelude/)

FRONT=$((CONCRETE + ABSTRACT + INTERNAL + BUILTINS + PIPELINE))
BACK=$((BACKENDC + GEB + REG + ASM + CORE))
BACK=$((BACKENDC + GEB + VAMPIR + REG + ASM + CORE))
OTHER=$((APP + HTML + EXTRA + DATA + PRELUDE))
TESTS=$(count test/)

echo "Front end: $FRONT LOC"
echo " Concrete: $CONCRETE LOC"
Expand All @@ -31,8 +37,9 @@ echo " Internal: $INTERNAL LOC"
echo " Builtins: $BUILTINS LOC"
echo " Pipeline: $PIPELINE LOC"
echo "Middle and back end: $BACK LOC"
echo " C backend: $BACKENDC LOC"
echo " VampIR backend: $VAMPIR LOC"
echo " GEB backend: $GEB LOC"
echo " C backend: $BACKENDC LOC"
echo " JuvixReg: $REG LOC"
echo " JuvixAsm: $ASM LOC"
echo " JuvixCore: $CORE LOC"
Expand All @@ -43,3 +50,4 @@ echo " Html: $HTML LOC"
echo " Extra: $EXTRA LOC"
echo " Data: $DATA LOC"
echo " Prelude: $PRELUDE LOC"
echo "Tests: $TESTS LOC"
3 changes: 3 additions & 0 deletions src/Juvix/Compiler/Backend.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ data Target
= TargetCWasm32Wasi
| TargetCNative64
| TargetGeb
| TargetVampIR
| TargetCore
| TargetAsm
deriving stock (Data, Eq, Show)
Expand Down Expand Up @@ -62,6 +63,8 @@ getLimits tgt debug = case tgt of
}
TargetGeb ->
defaultLimits
TargetVampIR ->
defaultLimits
TargetCore ->
defaultLimits
TargetAsm ->
Expand Down
28 changes: 0 additions & 28 deletions src/Juvix/Compiler/Backend/Geb/Pretty/Keywords.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,6 @@ import Juvix.Compiler.Backend.Geb.Language
import Juvix.Data.CodeAnn
import Juvix.Extra.Strings qualified as Str

keywords :: [Doc Ann]
keywords =
[ kwInitial,
kwTerminal,
kwProd,
kwCoprod,
kwHom,
kwInteger,
kwEq,
kwLt,
kwAbsurd,
kwUnit,
kwLeft,
kwRight,
kwFst,
kwSnd,
kwPair,
kwLamb,
kwApp,
kwVar,
kwAdd,
kwSub,
kwMul,
kwDiv,
kwMod,
kwFail
]

kwAbsurd :: Doc Ann
kwAbsurd = keyword Str.gebAbsurd

Expand Down
81 changes: 81 additions & 0 deletions src/Juvix/Compiler/Backend/VampIR/Language.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module Juvix.Compiler.Backend.VampIR.Language
( module Juvix.Compiler.Backend.VampIR.Language,
module Juvix.Prelude,
)
where

import Juvix.Prelude

newtype Var = Var
{ _varName :: Text
}

data OpCode
= OpAdd
| OpSub
| OpMul
| OpDiv
| OpMod
| OpEq
| OpLt
| OpLe

data Binop = Binop
{ _binopOp :: OpCode,
_binopLeft :: Expression,
_binopRight :: Expression
}

data IfThenElse = IfThenElse
{ _ifThenElseCondition :: Expression,
_ifThenElseBranchTrue :: Expression,
_ifThenElseBranchFalse :: Expression
}

data Expression
= ExpressionVar Var
| ExpressionConstant Integer
| ExpressionBinop Binop
| ExpressionIfThenElse IfThenElse
| ExpressionFail

data LocalDef = LocalDef
{ _localDefName :: Text,
_localDefValue :: Expression
}

data Function = Function
{ _functionName :: Text,
_functionArguments :: [Text],
_functionLocalDefs :: [LocalDef],
_functionExpression :: Expression
}

newtype Program = Program
{ _programFunctions :: [Function]
}

makeLenses ''Var
makeLenses ''Binop
makeLenses ''IfThenElse
makeLenses ''Expression
makeLenses ''LocalDef
makeLenses ''Function
makeLenses ''Program

instance HasAtomicity Var where
atomicity _ = Atom

instance HasAtomicity Binop where
atomicity _ = Aggregate appFixity

instance HasAtomicity IfThenElse where
atomicity _ = Aggregate appFixity

instance HasAtomicity Expression where
atomicity = \case
ExpressionVar x -> atomicity x
ExpressionConstant {} -> Atom
ExpressionBinop x -> atomicity x
ExpressionIfThenElse x -> atomicity x
ExpressionFail -> Atom
28 changes: 28 additions & 0 deletions src/Juvix/Compiler/Backend/VampIR/Pretty.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Juvix.Compiler.Backend.VampIR.Pretty
( module Juvix.Compiler.Backend.VampIR.Pretty,
module Juvix.Compiler.Backend.VampIR.Pretty.Base,
module Juvix.Compiler.Backend.VampIR.Pretty.Options,
module Juvix.Data.PPOutput,
)
where

import Juvix.Compiler.Backend.VampIR.Language
import Juvix.Compiler.Backend.VampIR.Pretty.Base
import Juvix.Compiler.Backend.VampIR.Pretty.Options
import Juvix.Data.PPOutput
import Prettyprinter.Render.Terminal qualified as Ansi

ppOutDefault :: PrettyCode c => c -> AnsiText
ppOutDefault = AnsiText . PPOutput . doc defaultOptions

ppOut :: (CanonicalProjection a Options, PrettyCode c) => a -> c -> AnsiText
ppOut o = AnsiText . PPOutput . doc (project o)

ppTrace' :: (CanonicalProjection a Options, PrettyCode c) => a -> c -> Text
ppTrace' opts = Ansi.renderStrict . reAnnotateS stylize . layoutPretty defaultLayoutOptions . doc (project opts)

ppTrace :: PrettyCode c => c -> Text
ppTrace = ppTrace' traceOptions

ppPrint :: PrettyCode c => c -> Text
ppPrint = show . ppOutDefault
Loading