-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the dynamic dispatch loop in JuvixAsm (#2556)
- Loading branch information
Showing
23 changed files
with
732 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
|
||
function juvix_apply_1(*, *) : * { | ||
push arg[0]; | ||
argsnum; | ||
push 1; | ||
eq; | ||
br { | ||
true: { -- argsnum = 1 | ||
push arg[1]; | ||
push arg[0]; | ||
tcall $ 1; | ||
} | ||
false: { -- argsnum > 1 | ||
push arg[1]; | ||
push arg[0]; | ||
cextend 1; | ||
ret; | ||
} | ||
}; | ||
} | ||
|
||
function juvix_apply_2(*, *, *) : * { | ||
push arg[0]; | ||
argsnum; | ||
tsave n { | ||
push n; | ||
push 2; | ||
eq; | ||
br { | ||
true: { -- argsnum = 2 | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
tcall $ 2; | ||
} | ||
false: { | ||
push n; | ||
push 1; | ||
eq; | ||
br { | ||
true: { -- argsnum = 1 | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
call $ 1; | ||
tcall juvix_apply_1; | ||
} | ||
false: { -- argsnum > 2 | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
cextend 2; | ||
ret; | ||
} | ||
}; | ||
} | ||
}; | ||
}; | ||
} | ||
|
||
function juvix_apply_3(*, *, *, *) : * { | ||
push arg[0]; | ||
argsnum; | ||
tsave n { | ||
push n; | ||
push 3; | ||
eq; | ||
br { | ||
true: { -- argsnum = 3 | ||
push arg[3]; | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
tcall $ 3; | ||
} | ||
false: { | ||
push n; | ||
push 3; | ||
lt; | ||
br { | ||
true: { -- argsnum > 3 | ||
push arg[3]; | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
cextend 3; | ||
ret; | ||
} | ||
false: { -- argsnum <= 2 | ||
push n; | ||
push 2; | ||
eq; | ||
br { | ||
true: { -- argsnum = 2 | ||
push arg[3]; | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
call $ 2; | ||
tcall juvix_apply_1; | ||
} | ||
false: { -- argsnum = 1 | ||
push arg[3]; | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
call $ 1; | ||
tcall juvix_apply_2; | ||
} | ||
}; | ||
} | ||
}; | ||
} | ||
}; | ||
}; | ||
} | ||
|
||
function juvix_apply_4(*, *, *, *, *) : * { | ||
push arg[0]; | ||
argsnum; | ||
tsave n { | ||
push n; | ||
push 4; | ||
eq; | ||
br { | ||
true: { -- argsnum = 4 | ||
push arg[4]; | ||
push arg[3]; | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
tcall $ 4; | ||
} | ||
false: { | ||
push n; | ||
push 4; | ||
lt; | ||
br { | ||
true: { -- argsnum > 4 | ||
push arg[4]; | ||
push arg[3]; | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
cextend 4; | ||
ret; | ||
} | ||
false: { -- argsnum <= 3 | ||
push n; | ||
push 3; | ||
eq; | ||
br { | ||
true: { -- argsnum = 3 | ||
push arg[4]; | ||
push arg[3]; | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
call $ 3; | ||
tcall juvix_apply_1; | ||
} | ||
false: { | ||
push n; | ||
push 2; | ||
eq; | ||
br { | ||
true: { -- argsnum = 2 | ||
push arg[4]; | ||
push arg[3]; | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
call $ 2; | ||
tcall juvix_apply_2; | ||
} | ||
false: { -- argsnum = 1 | ||
push arg[4]; | ||
push arg[3]; | ||
push arg[2]; | ||
push arg[1]; | ||
push arg[0]; | ||
call $ 1; | ||
tcall juvix_apply_3; | ||
} | ||
}; | ||
} | ||
}; | ||
} | ||
}; | ||
} | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module Juvix.Compiler.Asm.Extra.Apply where | ||
|
||
import Data.FileEmbed qualified as FE | ||
import Data.HashMap.Strict qualified as HashMap | ||
import Data.Text.Encoding | ||
import Juvix.Compiler.Asm.Data.InfoTable | ||
import Juvix.Compiler.Asm.Data.InfoTableBuilder | ||
import Juvix.Compiler.Asm.Language | ||
import Juvix.Compiler.Asm.Translation.FromSource | ||
|
||
data ApplyBuiltins = ApplyBuiltins | ||
{ -- | The number of `juvix_apply_n` functions. | ||
_applyBuiltinsNum :: Int, | ||
-- | Maps `n` to the function `juvix_apply_n`. | ||
_applyBuiltinsMap :: HashMap Int Symbol | ||
} | ||
|
||
makeLenses ''ApplyBuiltins | ||
|
||
addApplyBuiltins :: InfoTable -> (ApplyBuiltins, InfoTable) | ||
addApplyBuiltins tab = (blts, bs' ^. stateInfoTable) | ||
where | ||
nextSymbol = maximum (0 : HashMap.keys (tab ^. infoFunctions) ++ HashMap.keys (tab ^. infoInductives)) + 1 | ||
nextUserId = maximum (0 : mapMaybe getUserTag (HashMap.keys (tab ^. infoConstrs))) + 1 | ||
|
||
bs :: BuilderState | ||
bs = | ||
BuilderState | ||
{ _stateNextSymbol = nextSymbol, | ||
_stateNextUserTag = nextUserId, | ||
_stateInfoTable = tab, | ||
_stateIdents = mempty | ||
} | ||
|
||
bs' :: BuilderState | ||
bs' = | ||
fromRight impossible $ | ||
parseText' bs $ | ||
decodeUtf8 $(FE.makeRelativeToProject "runtime/src/asm/apply.jva" >>= FE.embedFile) | ||
|
||
blts :: ApplyBuiltins | ||
blts = | ||
ApplyBuiltins | ||
{ _applyBuiltinsNum = 4, | ||
_applyBuiltinsMap = | ||
HashMap.fromList $ map mkApply [1 .. 4] | ||
} | ||
|
||
mkApply :: Int -> (Int, Symbol) | ||
mkApply x = (x, f) | ||
where | ||
idt = "juvix_apply_" <> show x | ||
f = case fromJust $ HashMap.lookup idt (bs' ^. stateIdents) of | ||
IdentFun s -> s | ||
_ -> impossible | ||
|
||
getUserTag :: Tag -> Maybe Word | ||
getUserTag = \case | ||
BuiltinTag {} -> Nothing | ||
UserTag x -> Just x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.