Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Jun 8, 2024
1 parent 2c683a1 commit fa42c07
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/Juvix/Compiler/Casm/Data/Result.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import Juvix.Compiler.Casm.Language
data Result = Result
{ _resultLabelInfo :: LabelInfo,
_resultCode :: [Instruction],
_resultBuiltins :: [Builtin]
_resultBuiltins :: [Builtin],
_resultOutputSize :: Natural
}

makeLenses ''Result
3 changes: 2 additions & 1 deletion src/Juvix/Compiler/Casm/Translation/FromCairo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ fromCairo elems0 =
Result
{ _resultLabelInfo = mempty,
_resultCode = go 0 [] elems0,
_resultBuiltins = mempty
_resultBuiltins = mempty,
_resultOutputSize = 0
}
where
errorMsg :: Address -> Text -> a
Expand Down
12 changes: 9 additions & 3 deletions src/Juvix/Compiler/Casm/Translation/FromReg.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fromReg tab = mkResult $ run $ runLabelInfoBuilderWithNextId (Reg.getNextSymbolI
initBuiltinsInstr = mkAssignAp (Binop $ BinopValue FieldAdd (MemRef Fp (-2)) (Imm 1))
callMainInstr = mkCallRel (Lab $ LabelRef mainSym (Just mainName))
jmpEndInstr = mkJumpRel (Val $ Lab endLab)
margs = concat $ reverse $ map mkLoadInputArg mainArgs
loadInputArgsInstrs = concat $ reverse $ map mkLoadInputArg mainArgs
-- [ap] = [[ap - 2 - k] + k]; ap++
bltsRet = map (\k -> mkAssignAp (Load $ LoadValue (MemRef Ap (-2 - k)) k)) [0 .. bnum - 1]
resRetInstr = mkAssignAp (Val $ Ref $ MemRef Ap (-bnum - 1))
Expand All @@ -46,7 +46,7 @@ fromReg tab = mkResult $ run $ runLabelInfoBuilderWithNextId (Reg.getNextSymbolI
: jmpEndInstr
: Label startLab
: initBuiltinsInstr
: margs
: loadInputArgsInstrs
++ callMainInstr
: bltsRet
++ [resRetInstr, Return]
Expand All @@ -66,7 +66,13 @@ fromReg tab = mkResult $ run $ runLabelInfoBuilderWithNextId (Reg.getNextSymbolI
)
where
mkResult :: (LabelInfo, ([Builtin], Code)) -> Result
mkResult (labi, (blts, code)) = Result labi code blts
mkResult (labi, (blts, code)) =
Result
{ _resultLabelInfo = labi,
_resultCode = code,
_resultBuiltins = blts,
_resultOutputSize = 1
}

mkLoadInputArg :: Text -> [Instruction]
mkLoadInputArg arg = [Hint (HintInput arg), mkAssignAp (Val $ Ref $ MemRef Ap 0)]
Expand Down
9 changes: 5 additions & 4 deletions src/Juvix/Compiler/Core/Transformation/Check/Cairo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ checkCairo md = do
unless (checkType (ii ^. identifierType)) $
throw
CoreError
{ _coreErrorMsg = ppOutput "for this target the arguments and the result of the `main` function must be numbers or field elements",
{ _coreErrorMsg = ppOutput "for this target the arguments and the result of the `main` function cannot be functions",
_coreErrorLoc = fromMaybe defaultLoc (ii ^. identifierLocation),
_coreErrorNode = Nothing
}
Expand All @@ -29,7 +29,8 @@ checkCairo md = do
checkType :: Node -> Bool
checkType ty =
let (tyargs, tgt) = unfoldPi' ty
in all isPrimIntegerOrField (tgt : tyargs)
in all isNonFunType (tgt : tyargs)
where
isPrimIntegerOrField ty' =
isTypeInteger ty' || isTypeField ty' || isDynamic ty'
isNonFunType = \case
NPi {} -> False
_ -> True

0 comments on commit fa42c07

Please sign in to comment.