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

Fix memory count for string operations #1924

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions runtime/src/juvix/object/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ size_t print_to_buf(char *buf, size_t n, word_t x) {
return k;
}

// The returned pointer should be freed with `free_strbuf`
char *print(word_t x) {
// TODO: replace this with malloc when we have it for all APIs
char *buf = palloc(1);
Expand Down
16 changes: 16 additions & 0 deletions src/Juvix/Compiler/Asm/Transformation/Prealloc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ computeCodePrealloc tab code = prealloc <$> foldS sig code (0, [])
TailCall {} -> return (0, cmd : prealloc acc)
CallClosures {} -> return (0, cmd : prealloc acc)
TailCallClosures {} -> return (0, cmd : prealloc acc)
Binop StrConcat -> do
opts <- ask
let size = opts ^. optLimits . limitsMaxStringSize
return (k + size, cmd : c)
ValShow -> do
opts <- ask
let size = opts ^. optLimits . limitsMaxStringSize
return (k + size, cmd : c)
_ -> return (k, cmd : c)
where
cmd = Instr instr
Expand Down Expand Up @@ -108,6 +116,14 @@ checkCodePrealloc tab code = do
opts <- ask
let size = opts ^. optLimits . limitsMaxClosureSize
return $ \k -> cont (k - size)
Binop StrConcat -> do
opts <- ask
let size = opts ^. optLimits . limitsMaxStringSize
return $ \k -> cont (k - size)
ValShow -> do
opts <- ask
let size = opts ^. optLimits . limitsMaxStringSize
return $ \k -> cont (k - size)
_ -> return id

goBranch :: CmdBranch -> (Int -> Int) -> (Int -> Int) -> (Int -> Int) -> Sem r (Int -> Int)
Expand Down
3 changes: 3 additions & 0 deletions src/Juvix/Compiler/Backend.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data Limits = Limits
_limitsMaxLocalVars :: Int,
_limitsMaxClosureSize :: Int,
_limitsClosureHeadSize :: Int,
_limitsMaxStringSize :: Int,
_limitsMaxStackDelta :: Int,
_limitsMaxFunctionAlloc :: Int,
_limitsDispatchStackSize :: Int,
Expand All @@ -32,6 +33,7 @@ getLimits tgt debug = case tgt of
_limitsMaxLocalVars = 2048,
_limitsMaxClosureSize = 253 + 3,
_limitsClosureHeadSize = if debug then 3 else 2,
_limitsMaxStringSize = 255 + 1,
_limitsMaxStackDelta = 16368,
_limitsMaxFunctionAlloc = 16368,
_limitsDispatchStackSize = 4,
Expand All @@ -45,6 +47,7 @@ getLimits tgt debug = case tgt of
_limitsMaxLocalVars = 1024,
_limitsMaxClosureSize = 253 + 3,
_limitsClosureHeadSize = if debug then 3 else 2,
_limitsMaxStringSize = 255 + 1,
_limitsMaxStackDelta = 8184,
_limitsMaxFunctionAlloc = 8184,
_limitsDispatchStackSize = 4,
Expand Down
4 changes: 4 additions & 0 deletions test/Asm/Compile/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ asmCompileAssertion' tab mainFile expectedFile stdinText step = do
Runtime.clangAssertion cFile expectedFile stdinText step
)
where
-- TODO: In the future, the target supplied here might need to correspond to
-- the actual target, and C code will then need to be re-generated for each
-- target separately. Now this works only because those limits that
-- Prealloc.hs uses are the same for the Native64 and Wasm32 targets.
asmOpts :: Options
asmOpts = makeOptions Backend.TargetCNative64 True

Expand Down