From a7137f32fffeb34fb7f8bd5d1732467741c7d5f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= Date: Fri, 12 Dec 2014 15:03:09 +0100 Subject: [PATCH] compiler.cfg.save-contexts: refactoring of the vocab, using the simple-optimization combinator and union classes you can shorten it a lot --- .../save-contexts/save-contexts-docs.factor | 28 +++++++----- .../save-contexts/save-contexts-tests.factor | 15 +++++-- .../cfg/save-contexts/save-contexts.factor | 45 ++++++------------- 3 files changed, 43 insertions(+), 45 deletions(-) diff --git a/basis/compiler/cfg/save-contexts/save-contexts-docs.factor b/basis/compiler/cfg/save-contexts/save-contexts-docs.factor index abe2dd1fe1..b86b9bd9e4 100644 --- a/basis/compiler/cfg/save-contexts/save-contexts-docs.factor +++ b/basis/compiler/cfg/save-contexts/save-contexts-docs.factor @@ -1,17 +1,25 @@ -USING: compiler.cfg compiler.cfg.instructions help.markup help.syntax ; +USING: compiler.cfg compiler.cfg.instructions help.markup help.syntax +math sequences ; IN: compiler.cfg.save-contexts HELP: insert-save-contexts { $values { "cfg" cfg } } { $description "Inserts " { $link ##save-context } " instructions in each " { $link basic-block } " in the cfg that needs them. Save contexts are needed after instructions that modify the context, or instructions that read parameter registers." } -{ $see-also needs-save-context? } ; +{ $see-also context-save-needed } ; -HELP: bb-needs-save-context? -{ $values { "bb" basic-block } { "?" "a boolean" } } -{ $description "Whether to insert a " { $link ##save-context } " instruction in the block or not." } -{ $see-also needs-save-context? } ; +HELP: insns-needs-save-context? +{ $values { "insns" sequence } { "?" "a boolean" } } +{ $description "Whether to insert a " { $link ##save-context } " instruction in the given instruction sequence or not." } +{ $see-also context-save-needed } ; -HELP: needs-save-context? -{ $values { "insn" "an instruction" } { "?" "a boolean" } } -{ $description "Whether the given instruction needs to be preceeded by a " { $link ##save-context } " instruction or not. Only instructions that can allocate memory mandates save contexts." } -{ $see-also gc-map-insn } ; +HELP: context-save-needed +{ $class-description "Union class of all instructions that needs to be preceeded by a " { $link ##save-context } " instruction. Only instructions that can allocate memory mandates save contexts." } ; + +HELP: save-context-offset +{ $values { "insns" sequence } { "n" integer } } +{ $description { $link "##save-context" } " must be placed after instructions that modify the context, or instructions that read parameter registers." } ; + +ARTICLE: "compiler.cfg.save-contexts" "Insert context saves" +"Inserts " { $link ##save-context } " in blocks that need them." ; + +ABOUT: "compiler.cfg.save-contexts" diff --git a/basis/compiler/cfg/save-contexts/save-contexts-tests.factor b/basis/compiler/cfg/save-contexts/save-contexts-tests.factor index 0b8c5e7873..d6f1c4d927 100644 --- a/basis/compiler/cfg/save-contexts/save-contexts-tests.factor +++ b/basis/compiler/cfg/save-contexts/save-contexts-tests.factor @@ -11,7 +11,7 @@ V{ T{ ##branch } } 0 test-bb -0 get insert-save-context +0 get [ insert-save-context ] change-instructions drop [ V{ @@ -31,7 +31,7 @@ V{ } } 0 test-bb -0 get insert-save-context +0 get [ insert-save-context ] change-instructions drop [ V{ @@ -50,7 +50,7 @@ V{ T{ ##box } } 0 test-bb -0 get insert-save-context +0 get [ insert-save-context ] change-instructions drop [ V{ @@ -61,3 +61,12 @@ V{ ] [ 0 get instructions>> ] unit-test + +{ 3 } [ + V{ + T{ ##phi } + T{ ##phi } + T{ ##phi } + T{ insn } + } save-context-offset +] unit-test diff --git a/basis/compiler/cfg/save-contexts/save-contexts.factor b/basis/compiler/cfg/save-contexts/save-contexts.factor index 571056e2c8..e1326c2f07 100644 --- a/basis/compiler/cfg/save-contexts/save-contexts.factor +++ b/basis/compiler/cfg/save-contexts/save-contexts.factor @@ -1,47 +1,28 @@ ! Copyright (C) 2009, 2010 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors combinators.short-circuit -compiler.cfg.instructions compiler.cfg.registers -compiler.cfg.rpo cpu.architecture kernel sequences ; +USING: compiler.cfg.instructions compiler.cfg.registers compiler.cfg.rpo +cpu.architecture kernel sequences ; IN: compiler.cfg.save-contexts -! Insert context saves. +UNION: context-modifier ##phi ##inc-d ##inc-r ##callback-inputs ; +UNION: context-save-needed gc-map-insn ; -GENERIC: needs-save-context? ( insn -- ? ) +: save-context-offset ( insns -- n ) + [ context-modifier? not ] find drop ; -M: gc-map-insn needs-save-context? drop t ; -M: insn needs-save-context? drop f ; +: insns-needs-save-context? ( insns -- ? ) + [ context-save-needed? ] any? ; -: bb-needs-save-context? ( bb -- ? ) - { - [ kill-block?>> not ] - [ instructions>> [ needs-save-context? ] any? ] - } 1&& ; - -GENERIC: modifies-context? ( insn -- ? ) - -M: ##phi modifies-context? drop t ; -M: ##inc-d modifies-context? drop t ; -M: ##inc-r modifies-context? drop t ; -M: ##callback-inputs modifies-context? drop t ; -M: insn modifies-context? drop f ; - -: save-context-offset ( bb -- n ) - ! ##save-context must be placed after instructions that - ! modify the context, or instructions that read parameter - ! registers. - instructions>> [ modifies-context? not ] find drop ; - -: insert-save-context ( bb -- ) - dup bb-needs-save-context? [ +: insert-save-context ( insns -- insns' ) + dup insns-needs-save-context? [ [ int-rep next-vreg-rep int-rep next-vreg-rep ##save-context new-insn ] dip [ save-context-offset ] keep - [ insert-nth ] change-instructions drop - ] [ drop ] if ; + insert-nth + ] when ; : insert-save-contexts ( cfg -- ) - [ insert-save-context ] each-basic-block ; + [ insert-save-context ] simple-optimization ;