From cf72ee2b6c8fe87e43f3e6553fcb13588fb560a3 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 19 Nov 2021 10:14:54 -0400 Subject: Added tree-shaking to the compiler. --- lux-jvm/source/luxc/lang/directive/jvm.lux | 48 +++- .../luxc/lang/translation/jvm/extension/host.lux | 287 +++++++++++---------- .../source/luxc/lang/translation/jvm/function.lux | 6 +- .../source/luxc/lang/translation/jvm/runtime.lux | 10 +- 4 files changed, 207 insertions(+), 144 deletions(-) (limited to 'lux-jvm') diff --git a/lux-jvm/source/luxc/lang/directive/jvm.lux b/lux-jvm/source/luxc/lang/directive/jvm.lux index 60032010d..3c384b475 100644 --- a/lux-jvm/source/luxc/lang/directive/jvm.lux +++ b/lux-jvm/source/luxc/lang/directive/jvm.lux @@ -20,7 +20,8 @@ [array {"+" Array}] ["[0]" list ("[1]#[0]" mix functor monoid)] ["[0]" dictionary {"+" Dictionary}] - ["[0]" sequence {"+" Sequence} ("[1]#[0]" functor mix)]]] + ["[0]" sequence {"+" Sequence} ("[1]#[0]" functor mix)] + ["[0]" set {"+" Set}]]] [math [number ["[0]" nat]]] @@ -28,7 +29,7 @@ ["/" jvm [encoding ["[0]" name {"+" External}]] - ["[1][0]" type {"+" Type Constraint} + ["[1][0]" type {"+" Type Typed Constraint} [category {"+" Void Value Return Primitive Object Class Var Parameter}] ["[0]" parser] ["[0]T" lux] @@ -54,7 +55,9 @@ [directive ["[0]/" lux]]]]]] [meta - [archive {"+" Archive}]]]]]] + [archive {"+" Archive} + ["[0]" artifact] + ["[0]" dependency]]]]]]] [/// [host ["[0]" jvm {"+" Inst} @@ -692,7 +695,7 @@ (type: (Constructor a) [Privacy Bit (List Annotation) (List (Type Var)) (List (Type Class)) - Text (List Argument) (List [(Type Value) a]) + Text (List Argument) (List (Typed a)) a]) (type: (Override a) @@ -722,6 +725,35 @@ {#Static (Static a)} {#Abstract Abstract})) +(def: (method_dependencies archive method) + (-> Archive (Method Synthesis) + (generation.Operation jvm.Anchor jvm.Inst jvm.Definition + (Set artifact.Dependency))) + (case method + {#Constructor [privacy strict_floating_point? annotations variables exceptions + self arguments constructor_arguments + body]} + (do [! phase.monad] + [all_super_ctor_dependencies (monad.each ! (|>> product.right (dependency.dependencies archive)) + constructor_arguments) + body_dependencies (dependency.dependencies archive body)] + (in (dependency.all (list& body_dependencies all_super_ctor_dependencies)))) + + + (^or {#Override [[parent_name parent_variables] name strict_floating_point? annotations variables + self arguments return exceptions + body]} + {#Virtual [name privacy final? strict_floating_point? annotations variables + self arguments return exceptions + body]} + {#Static [name privacy strict_floating_point? annotations variables + arguments return exceptions + body]}) + (dependency.dependencies archive body) + + {#Abstract _} + (# phase.monad in artifact.no_dependencies))) + (def: constructor (Parser (Constructor Code)) (let [constructor_argument (: (Parser [(Type Value) Code]) @@ -1434,6 +1466,10 @@ methodsA (monad.each ! (method_analysis archive declaration supers) methodsC) methodsS (monad.each ! (method_synthesis archive) methodsA) methodsG (monad.each ! (method_generation archive super_class) methodsS) + all_dependencies (|> methodsS + (monad.each ! (method_dependencies archive)) + (# ! each dependency.all) + directive.lifted_generation) .let [directive [class_name (def.class {jvm.#V1_6} {jvm.#Public} jvm.noneC class_name (list#each ..constraint type_variables) @@ -1443,7 +1479,7 @@ methodsG)))]]] (directive.lifted_generation (do ! - [artifact_id (generation.learn_custom class_name) + [artifact_id (generation.learn_custom class_name all_dependencies) _ (generation.execute! directive) _ (generation.save! artifact_id {.#Some class_name} directive) _ (generation.log! (format "JVM Class " (%.text class_name)))] @@ -1470,7 +1506,7 @@ def.fuse))]]] (directive.lifted_generation (do ! - [artifact_id (generation.learn_custom class_name) + [artifact_id (generation.learn_custom class_name artifact.no_dependencies) _ (generation.execute! directive) _ (generation.save! artifact_id {.#Some class_name} directive) _ (generation.log! (format "JVM Interface " (%.text class_name)))] diff --git a/lux-jvm/source/luxc/lang/translation/jvm/extension/host.lux b/lux-jvm/source/luxc/lang/translation/jvm/extension/host.lux index bbc8920f6..ee4096fe1 100644 --- a/lux-jvm/source/luxc/lang/translation/jvm/extension/host.lux +++ b/lux-jvm/source/luxc/lang/translation/jvm/extension/host.lux @@ -1,71 +1,73 @@ (.using - [library - [lux {"-" Type Label Primitive int char type} - [ffi {"+" import:}] - [abstract - ["[0]" monad {"+" do}]] - [control - ["[0]" maybe ("[1]#[0]" functor)] - ["[0]" exception {"+" exception:}] - ["[0]" function] - ["<>" parser ("[1]#[0]" monad) - ["<[0]>" text] - ["<[0]>" synthesis {"+" Parser}]]] - [data - ["[0]" product] - ["[0]" text ("[1]#[0]" equivalence) - ["%" format {"+" format}]] - [collection - ["[0]" list ("[1]#[0]" monad mix)] - ["[0]" dictionary {"+" Dictionary}] - ["[0]" set]]] - [macro - ["[0]" template]] - [math - [number - ["n" nat]]] - [target - [jvm - ["[0]" type {"+" Type Typed Argument} - ["[0]" category {"+" Void Value Return Primitive Object Class Array Var Parameter Method}] - ["[0]" box] - ["[0]" reflection] - ["[0]" signature] - ["[0]" descriptor] - ["[0]" parser]]]] - [tool - [compiler - ["[0]" phase ("[1]#[0]" monad)] - [reference {"+" } - ["[0]" variable {"+" Variable Register}]] - [meta - [archive {"+" Archive}]] - [language - [lux - [analysis {"+" Environment}] - ["[0]" synthesis {"+" Synthesis Path %synthesis}] - ["[0]" generation] - [phase - [generation - [extension {"+" Nullary Unary Binary - nullary unary binary}]] + [library + [lux {"-" Type Label Primitive int char type} + [ffi {"+" import:}] + [abstract + ["[0]" monad {"+" do}]] + [control + ["[0]" maybe ("[1]#[0]" functor)] + ["[0]" exception {"+" exception:}] + ["[0]" function] + ["<>" parser ("[1]#[0]" monad) + ["<[0]>" text] + ["<[0]>" synthesis {"+" Parser}]]] + [data + ["[0]" product] + ["[0]" text ("[1]#[0]" equivalence) + ["%" format {"+" format}]] + [collection + ["[0]" list ("[1]#[0]" monoid mix monad)] + ["[0]" dictionary {"+" Dictionary}] + ["[0]" set {"+" Set}]]] + [macro + ["[0]" template]] + [math + [number + ["n" nat]]] + [target + [jvm + ["[0]" type {"+" Type Typed Argument} + ["[0]" category {"+" Void Value Return Primitive Object Class Array Var Parameter Method}] + ["[0]" box] + ["[0]" reflection] + ["[0]" signature] + ["[0]" descriptor] + ["[0]" parser]]]] + [tool + [compiler + ["[0]" phase ("[1]#[0]" monad)] + [reference {"+" } + ["[0]" variable {"+" Variable Register}]] + [meta + [archive {"+" Archive} + ["[0]" artifact] + ["[0]" dependency]]] + [language + [lux + [analysis {"+" Environment}] + ["[0]" synthesis {"+" Synthesis Path %synthesis}] + ["[0]" generation] + [phase + [generation + [extension {"+" Nullary Unary Binary + nullary unary binary}]] + [analysis + ["[0]A" reference]] + ["[0]" extension + ["[0]" bundle] [analysis - ["[0]A" reference]] - ["[0]" extension - ["[0]" bundle] - [analysis - ["/" jvm]]]]]]]]]] - [luxc - [lang - [host - ["$" jvm {"+" Label Inst Def Handler Bundle Operation Phase} - ["_" inst] - ["_[0]" def]]]]] - ["[0]" // "_" - [common {"+" custom}] - ["/[1]" // - ["[1][0]" reference] - ["[1][0]" function]]]) + ["/" jvm]]]]]]]]]] + [luxc + [lang + [host + ["$" jvm {"+" Label Inst Def Handler Bundle Operation Phase} + ["_" inst] + ["_[0]" def]]]]] + ["[0]" // "_" + [common {"+" custom}] + ["/[1]" // + ["[1][0]" reference] + ["[1][0]" function]]]) (template [ ] [(def: .public @@ -1073,6 +1075,13 @@ ... (# type.equivalence = type.double returnT) (unwrap_primitive _.DRETURN type.double))))))) +(def: (method_dependencies archive method) + (-> Archive (/.Overriden_Method Synthesis) (Operation (Set artifact.Dependency))) + (let [[_super _name _strict_fp? _annotations + _t_vars _this _arguments _return _exceptions + bodyS] method] + (dependency.dependencies archive bodyS))) + (def: class::anonymous Handler (..custom @@ -1086,72 +1095,86 @@ inputsTS overriden_methods]) (do [! phase.monad] - [[context _] (generation.with_new_context archive (in [])) - .let [[module_id artifact_id] context - anonymous_class_name (///.class_name context) - class (type.class anonymous_class_name (list)) - total_environment (|> overriden_methods - ... Get all the environments. - (list#each product.left) - ... Combine them. - list#conjoint - ... Remove duplicates. - (set.of_list synthesis.hash) - set.list) - global_mapping (|> total_environment - ... Give them names as "foreign" variables. - list.enumeration - (list#each (function (_ [id capture]) - [capture {variable.#Foreign id}])) - (dictionary.of_list synthesis.hash)) - normalized_methods (list#each (function (_ [environment - [ownerT name - strict_fp? annotations vars - self_name arguments returnT exceptionsT - body]]) - (let [local_mapping (|> environment - list.enumeration - (list#each (function (_ [foreign_id capture]) - [(synthesis.variable/foreign foreign_id) - (|> global_mapping - (dictionary.value capture) - maybe.trusted)])) - (dictionary.of_list synthesis.hash))] - [ownerT name - strict_fp? annotations vars - self_name arguments returnT exceptionsT - (normalize_method_body local_mapping body)])) - overriden_methods)] - inputsTI (monad.each ! (generate_input generate archive) inputsTS) - method_definitions (|> normalized_methods - (monad.each ! (function (_ [ownerT name - strict_fp? annotations varsT - self_name arguments returnT exceptionsT - bodyS]) - (do ! - [bodyG (generation.with_context artifact_id - (generate archive bodyS)) - .let [argumentsT (list#each product.right arguments)]] - (in (_def.method {$.#Public} - (if strict_fp? - ($_ $.++M $.finalM $.strictM) - $.finalM) - name - (type.method [varsT argumentsT returnT exceptionsT]) - (|>> (prepare_arguments 1 argumentsT) - bodyG - (returnI returnT))))))) - (# ! each _def.fuse)) - .let [directive [anonymous_class_name - (_def.class {$.#V1_6} {$.#Public} $.finalC - anonymous_class_name (list) - super_class super_interfaces - (|>> (///function.with_environment total_environment) - (..with_anonymous_init class total_environment super_class inputsTI) - method_definitions))]] - _ (generation.execute! directive) - _ (generation.save! artifact_id {.#None} directive)] - (..anonymous_instance generate archive class total_environment)))])) + [all_input_dependencies (monad.each ! (|>> product.right (dependency.dependencies archive)) inputsTS) + all_closure_dependencies (|> overriden_methods + (list#each product.left) + list.together + (monad.each ! (dependency.dependencies archive))) + all_method_dependencies (monad.each ! (|>> product.right (method_dependencies archive)) overriden_methods) + .let [all_dependencies (dependency.all ($_ list#composite + all_input_dependencies + all_closure_dependencies + all_method_dependencies))]] + (do [! phase.monad] + [[context _] (generation.with_new_context + archive + all_dependencies + (in [])) + .let [[module_id artifact_id] context + anonymous_class_name (///.class_name context) + class (type.class anonymous_class_name (list)) + total_environment (|> overriden_methods + ... Get all the environments. + (list#each product.left) + ... Combine them. + list#conjoint + ... Remove duplicates. + (set.of_list synthesis.hash) + set.list) + global_mapping (|> total_environment + ... Give them names as "foreign" variables. + list.enumeration + (list#each (function (_ [id capture]) + [capture {variable.#Foreign id}])) + (dictionary.of_list synthesis.hash)) + normalized_methods (list#each (function (_ [environment + [ownerT name + strict_fp? annotations vars + self_name arguments returnT exceptionsT + body]]) + (let [local_mapping (|> environment + list.enumeration + (list#each (function (_ [foreign_id capture]) + [(synthesis.variable/foreign foreign_id) + (|> global_mapping + (dictionary.value capture) + maybe.trusted)])) + (dictionary.of_list synthesis.hash))] + [ownerT name + strict_fp? annotations vars + self_name arguments returnT exceptionsT + (normalize_method_body local_mapping body)])) + overriden_methods)] + inputsTI (monad.each ! (generate_input generate archive) inputsTS) + method_definitions (|> normalized_methods + (monad.each ! (function (_ [ownerT name + strict_fp? annotations varsT + self_name arguments returnT exceptionsT + bodyS]) + (do ! + [bodyG (generation.with_context artifact_id + (generate archive bodyS)) + .let [argumentsT (list#each product.right arguments)]] + (in (_def.method {$.#Public} + (if strict_fp? + ($_ $.++M $.finalM $.strictM) + $.finalM) + name + (type.method [varsT argumentsT returnT exceptionsT]) + (|>> (prepare_arguments 1 argumentsT) + bodyG + (returnI returnT))))))) + (# ! each _def.fuse)) + .let [directive [anonymous_class_name + (_def.class {$.#V1_6} {$.#Public} $.finalC + anonymous_class_name (list) + super_class super_interfaces + (|>> (///function.with_environment total_environment) + (..with_anonymous_init class total_environment super_class inputsTI) + method_definitions))]] + _ (generation.execute! directive) + _ (generation.save! artifact_id {.#None} directive)] + (..anonymous_instance generate archive class total_environment))))])) (def: class_bundle Bundle diff --git a/lux-jvm/source/luxc/lang/translation/jvm/function.lux b/lux-jvm/source/luxc/lang/translation/jvm/function.lux index 6d2d3e8f9..7932360a3 100644 --- a/lux-jvm/source/luxc/lang/translation/jvm/function.lux +++ b/lux-jvm/source/luxc/lang/translation/jvm/function.lux @@ -33,7 +33,8 @@ [synthesis {"+" Synthesis Abstraction Apply}] ["[0]" generation {"+" Context}]]] [meta - [archive {"+" Archive}]]]]]] + [archive {"+" Archive} + ["[0]" dependency]]]]]]] [luxc [lang [host @@ -307,6 +308,7 @@ (-> (Maybe Context) (Generator Abstraction)) (do [! phase.monad] [@begin _.make_label + dependencies (dependency.dependencies archive bodyS) [function_context bodyI] (case forced_context {.#Some function_context} (do ! @@ -316,7 +318,7 @@ without_context])) {.#None} - (generation.with_new_context archive + (generation.with_new_context archive dependencies (generation.with_anchor [@begin 1] (generate archive bodyS)))) .let [function_class (//.class_name function_context)] diff --git a/lux-jvm/source/luxc/lang/translation/jvm/runtime.lux b/lux-jvm/source/luxc/lang/translation/jvm/runtime.lux index dc2cb5053..33cf199ea 100644 --- a/lux-jvm/source/luxc/lang/translation/jvm/runtime.lux +++ b/lux-jvm/source/luxc/lang/translation/jvm/runtime.lux @@ -11,7 +11,8 @@ ["%" format {"+" format}]] [collection ["[0]" list ("[1]@[0]" functor)] - ["[0]" sequence]]] + ["[0]" sequence] + ["[0]" set]]] ["[0]" math [number ["n" nat]]] @@ -411,10 +412,11 @@ ... function_payload ..translate_function ] (in [(|> artifact.empty - artifact.resource + (artifact.resource true artifact.no_dependencies) product.right - artifact.resource - product.right) + ... (artifact.resource true artifact.no_dependencies) + ... product.right + ) (sequence.sequence runtime_payload ... function_payload )]))) -- cgit v1.2.3