Skip to content

Commit

Permalink
translation from JuvixAsm
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Oct 14, 2022
1 parent edd17bd commit 1aeba80
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 27 deletions.
5 changes: 4 additions & 1 deletion src/Juvix/Compiler/Asm/Data/InfoTable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ data FunctionInfo = FunctionInfo
-- and the result is a function.
_functionArgsNum :: Int,
_functionType :: Type,
_functionMaxValueStackHeight :: Int,
_functionMaxTempStackHeight :: Int,
_functionCode :: Code
}

Expand All @@ -48,7 +50,8 @@ data InductiveInfo = InductiveInfo
_inductiveLocation :: Maybe Location,
_inductiveSymbol :: Symbol,
_inductiveKind :: Type,
_inductiveConstructors :: [ConstructorInfo]
_inductiveConstructors :: [ConstructorInfo],
_inductiveRepresentation :: IndRep
}

makeLenses ''InfoTable
Expand Down
4 changes: 4 additions & 0 deletions src/Juvix/Compiler/Asm/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Juvix.Compiler.Asm.Error where
import Juvix.Compiler.Asm.Language
import Juvix.Data.PPOutput
import Text.Megaparsec.Pos qualified as M
import Text.Show

data AsmError = AsmError
{ _asmErrorLoc :: Maybe Location,
Expand Down Expand Up @@ -30,6 +31,9 @@ instance ToGenericError AsmError where
instance Pretty AsmError where
pretty (AsmError {..}) = pretty _asmErrorMsg

instance Show AsmError where
show (AsmError {..}) = fromText _asmErrorMsg

instance HasLoc AsmError where
getLoc (AsmError {..}) = fromMaybe defaultLoc _asmErrorLoc
where
Expand Down
4 changes: 1 addition & 3 deletions src/Juvix/Compiler/Asm/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ validateCode tab args = void . recurse sig args
}

validateFunction :: Member (Error AsmError) r => InfoTable -> FunctionInfo -> Sem r ()
validateFunction tab fi = validateCode tab args (fi ^. functionCode)
where
args = HashMap.fromList $ zip [0 ..] (typeArgs (fi ^. functionType))
validateFunction tab fi = validateCode tab (argumentsFromFunctionInfo fi) (fi ^. functionCode)

validateInfoTable :: Member (Error AsmError) r => InfoTable -> Sem r ()
validateInfoTable tab = mapM_ (validateFunction tab) (HashMap.elems (tab ^. infoFunctions))
Expand Down
11 changes: 11 additions & 0 deletions src/Juvix/Compiler/Asm/Extra/Memory.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import Safe (atMay)

type Arguments = HashMap Offset Type

argumentsFromFunctionInfo :: FunctionInfo -> Arguments
argumentsFromFunctionInfo fi =
HashMap.fromList $
zip [0 ..] (take (fi ^. functionArgsNum) (typeArgs (fi ^. functionType)))

-- | A static representation of JuvixAsm memory providing type information for
-- memory locations.
data Memory = Memory
Expand All @@ -39,12 +44,18 @@ pushValueStack ty = over memoryValueStack (Stack.push ty)
popValueStack :: Int -> Memory -> Memory
popValueStack n = iterateN n (over memoryValueStack Stack.pop)

valueStackHeight :: Memory -> Int
valueStackHeight mem = length (mem ^. memoryValueStack)

pushTempStack :: Type -> Memory -> Memory
pushTempStack ty = over memoryTempStack (Stack.push ty)

popTempStack :: Int -> Memory -> Memory
popTempStack n = iterateN n (over memoryTempStack Stack.pop)

tempStackHeight :: Memory -> Int
tempStackHeight mem = length (mem ^. memoryTempStack)

-- | Read value stack at index `n` from the top.
topValueStack :: Int -> Memory -> Maybe Type
topValueStack n mem = Stack.nthFromTop n (mem ^. memoryValueStack)
Expand Down
5 changes: 4 additions & 1 deletion src/Juvix/Compiler/Asm/Extra/Recursors.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Juvix.Compiler.Asm.Extra.Recursors
( module Juvix.Compiler.Asm.Extra.Recursors,
Arguments,
module Juvix.Compiler.Asm.Extra.Memory,
)
where

Expand All @@ -23,6 +23,9 @@ data RecursorSig r a = RecursorSig

makeLenses ''RecursorSig

recurseFun :: Member (Error AsmError) r => RecursorSig r a -> FunctionInfo -> Sem r [a]
recurseFun sig fi = recurse sig (argumentsFromFunctionInfo fi) (fi ^. functionCode)

recurse :: Member (Error AsmError) r => RecursorSig r a -> Arguments -> Code -> Sem r [a]
recurse sig args = fmap snd . recurse' sig (mkMemory args)

Expand Down
4 changes: 3 additions & 1 deletion src/Juvix/Compiler/Asm/Translation/FromCore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ genCode infoTable fi =
_functionSymbol = fi ^. Core.functionSymbol,
_functionArgsNum = fi ^. Core.functionArgsNum,
_functionType = convertType (fi ^. Core.functionArgsNum) (fi ^. Core.functionType),
_functionCode = code
_functionCode = code,
_functionMaxTempStackHeight = -1, -- computed later
_functionMaxValueStackHeight = -1
}
where
unimplemented :: forall a. a
Expand Down
10 changes: 7 additions & 3 deletions src/Juvix/Compiler/Asm/Translation/FromSource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ declareBuiltins = do
_inductiveSymbol = sym,
_inductiveLocation = Just i,
_inductiveKind = TyDynamic,
_inductiveConstructors = constrs
_inductiveConstructors = constrs,
_inductiveRepresentation = IndRepStandard
}
)
lift $ mapM_ registerConstr constrs
Expand Down Expand Up @@ -115,7 +116,9 @@ statementFunction = do
_functionLocation = Just i,
_functionCode = [],
_functionArgsNum = length argtys,
_functionType = mkTypeFun argtys (fromMaybe TyDynamic mrty)
_functionType = mkTypeFun argtys (fromMaybe TyDynamic mrty),
_functionMaxValueStackHeight = -1, -- computed later
_functionMaxTempStackHeight = -1
}
lift $ registerFunction fi0
mcode <- (kw kwSemicolon $> Nothing) <|> optional (braces parseCode)
Expand Down Expand Up @@ -153,7 +156,8 @@ statementInductive = do
_inductiveLocation = Just i,
_inductiveSymbol = sym,
_inductiveKind = TyDynamic,
_inductiveConstructors = []
_inductiveConstructors = [],
_inductiveRepresentation = IndRepStandard
}
lift $ registerInductive ii
ctrs <- braces $ P.sepEndBy (constrDecl sym) (kw kwSemicolon)
Expand Down
51 changes: 51 additions & 0 deletions src/Juvix/Compiler/Reg/Data/InfoTable.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Juvix.Compiler.Reg.Data.InfoTable where

import Juvix.Compiler.Reg.Language

data InfoTable = InfoTable
{ _infoFunctions :: HashMap Symbol FunctionInfo,
_infoConstrs :: HashMap Tag ConstructorInfo,
_infoInductives :: HashMap Symbol InductiveInfo,
_infoMainFunction :: Maybe Symbol
}

data FunctionInfo = FunctionInfo
{ _functionName :: Text,
_functionLocation :: Maybe Location,
_functionSymbol :: Symbol,
_functionArgsNum :: Int,
_functionStackVarsNum :: Int,
_functionTempVarsNum :: Int,
_functionCode :: Code
}

data ConstructorInfo = ConstructorInfo
{ _constructorName :: Text,
_constructorLocation :: Maybe Location,
_constructorTag :: Tag,
_constructorArgsNum :: Int,
_constructorInductive :: Symbol,
_constructorRepresentation :: MemRep
}

data InductiveInfo = InductiveInfo
{ _inductiveName :: Text,
_inductiveLocation :: Maybe Location,
_inductiveSymbol :: Symbol,
_inductiveConstructors :: [ConstructorInfo],
_inductiveRepresentation :: IndRep
}

makeLenses ''InfoTable
makeLenses ''FunctionInfo
makeLenses ''ConstructorInfo
makeLenses ''InductiveInfo

emptyInfoTable :: InfoTable
emptyInfoTable =
InfoTable
{ _infoFunctions = mempty,
_infoConstrs = mempty,
_infoInductives = mempty,
_infoMainFunction = Nothing
}
21 changes: 5 additions & 16 deletions src/Juvix/Compiler/Reg/Language.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ where

import Juvix.Compiler.Reg.Language.Base

data Type
= -- | Unboxed integer or pointer to boxed integer.
TyInteger
| -- | Unboxed boolean.
TyBool
| -- | Pointer to a string.
TyString
| -- | A raw word with arbitrary bit pattern.
TyWord
| -- | A pointer to a constructor.
TyPtr

data Value
= ConstInt Integer
| ConstBool Bool
Expand All @@ -44,12 +32,12 @@ data VarGroup = VarGroupArgs | VarGroupStack | VarGroupTemp

data VarRef = VarRef
{ _varRefGroup :: VarGroup,
_varRefIndex :: Index,
_varRefType :: Type
_varRefIndex :: Index
}

data Instruction
= Binop BinaryOp
= Nop -- no operation
| Binop BinaryOp
| Assign InstrAssign
| Trace InstrTrace
| Dump
Expand Down Expand Up @@ -113,6 +101,7 @@ data InstrAllocClosure = InstrAllocClosure

data InstrExtendClosure = InstrExtendClosure
{ _instrExtendClosureResult :: VarRef,
_instrExtendClosureValue :: VarRef,
_instrExtendClosureArgs :: [Value],
_instrExtendClosureLiveVars :: [VarRef]
}
Expand All @@ -132,8 +121,8 @@ data InstrCall = InstrCall

data InstrCallClosures = InstrCallClosures
{ _instrCallClosuresResult :: VarRef,
_instrCallClosuresClosure :: VarRef,
_instrCallClosuresIsTail :: Bool,
_instrCallClosuresValue :: VarRef,
_instrCallClosuresArgs :: [Value],
_instrCallClosuresLiveVars :: [VarRef]
}
Expand Down
Loading

0 comments on commit 1aeba80

Please sign in to comment.