]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/instructions/syntax/syntax.factor
Switch to https urls
[factor.git] / basis / compiler / cfg / instructions / syntax / syntax.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors classes.parser classes.tuple combinators
4 effects kernel lexer make namespaces parser sequences
5 splitting words ;
6 IN: compiler.cfg.instructions.syntax
7
8 SYMBOLS: def use temp literal ;
9
10 SYMBOL: scalar-rep
11
12 TUPLE: insn-slot-spec type name rep ;
13
14 : parse-rep ( str/f -- rep )
15     {
16         { [ dup not ] [ ] }
17         { [ dup "scalar-rep" = ] [ drop scalar-rep ] }
18         [ "cpu.architecture" lookup-word ]
19     } cond ;
20
21 : parse-insn-slot-spec ( type string -- spec )
22     over [ "Missing type" throw ] unless
23     "/" split1 parse-rep
24     insn-slot-spec boa ;
25
26 : parse-insn-slot-specs ( seq -- specs )
27     [
28         f [
29             {
30                 { "def:" [ drop def ] }
31                 { "use:" [ drop use ] }
32                 { "temp:" [ drop temp ] }
33                 { "literal:" [ drop literal ] }
34                 [ dupd parse-insn-slot-spec , ]
35             } case
36         ] reduce drop
37     ] { } make ;
38
39 : insn-def-slots ( class -- slot/f )
40     "insn-slots" word-prop [ type>> def eq? ] filter ;
41
42 : insn-use-slots ( class -- slots )
43     "insn-slots" word-prop [ type>> use eq? ] filter ;
44
45 : insn-temp-slots ( class -- slots )
46     "insn-slots" word-prop [ type>> temp eq? ] filter ;
47
48 ! We cannot reference words in compiler.cfg.instructions directly
49 ! since that would create circularity.
50 : insn-classes-word ( -- word )
51     "insn-classes" "compiler.cfg.instructions" lookup-word ;
52
53 : insn-word ( -- word )
54     "insn" "compiler.cfg.instructions" lookup-word ;
55
56 : vreg-insn-word ( -- word )
57     "vreg-insn" "compiler.cfg.instructions" lookup-word ;
58
59 : flushable-insn-word ( -- word )
60     "flushable-insn" "compiler.cfg.instructions" lookup-word ;
61
62 : foldable-insn-word ( -- word )
63     "foldable-insn" "compiler.cfg.instructions" lookup-word ;
64
65 : insn-effect ( word -- effect )
66     boa-effect in>> but-last { } <effect> ;
67
68 : uses-vregs? ( specs -- ? )
69     [ type>> { def use temp } member-eq? ] any? ;
70
71 : define-insn-tuple ( class superclass specs -- )
72     [ name>> ] map "insn#" suffix define-tuple-class ;
73
74 : insn-ctor-name ( word -- name )
75     name>> "," append ;
76
77 : define-insn-ctor ( class specs -- )
78     [ [ insn-ctor-name create-word-in ] [ '[ _ ] [ f ] [ boa , ] surround ] bi ] dip
79     [ name>> ] map { } <effect> define-declared ;
80
81 : define-insn ( class superclass specs -- )
82     parse-insn-slot-specs
83     {
84         [ nip "insn-slots" set-word-prop ]
85         [ 2drop insn-classes-word get push ]
86         [ define-insn-tuple ]
87         [ 2drop save-location ]
88         [ nip define-insn-ctor ]
89     } 3cleave ;
90
91 SYNTAX: INSN:
92     scan-new-class insn-word ";" parse-tokens define-insn ;
93
94 SYNTAX: VREG-INSN:
95     scan-new-class vreg-insn-word ";" parse-tokens define-insn ;
96
97 SYNTAX: FLUSHABLE-INSN:
98     scan-new-class flushable-insn-word ";" parse-tokens define-insn ;
99
100 SYNTAX: FOLDABLE-INSN:
101     scan-new-class foldable-insn-word ";" parse-tokens define-insn ;