Skip to content
Newer
Older
100644 203 lines (172 sloc) 6.64 KB
d6e29a5 @sorear First batch of changes to remove "use"-dependencies from niecza
authored Jan 14, 2012
1 our ($OpSimplePair, $OpCallSub, $OpLexical, $OpBuiltin, $OpTake,
2 $OpControl, $OpMakeJunction, $OpGeneralConst);
f5fd473 @sorear Finish removing "easy" cases of direct cross-module class referencing
authored Jan 14, 2012
3 our ($PassSimplifier);
d6e29a5 @sorear First batch of changes to remove "use"-dependencies from niecza
authored Jan 15, 2012
4
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 7, 2011
5 class NieczaPassSimplifier;
6
7 # This optimization pass handles lowering calls to well-known functions
8
9 method invoke($*unit) {
10 # XXX enter and sigs need love
11 $*unit.visit_local_subs_postorder(-> $su {
12 $su.code = run_optree($su, $su.code, 1)
13 });
14 $*unit
15 }
16
fb5e02c @sorear Compiler-side modifications to attach code to subs and run units
authored Sep 18, 2011
17 method invoke_incr($sub, $ops) {
2762955 @sorear If a constant foldable expression throws an exception, treat it as a …
authored Feb 4, 2012
18 my $*where = False;
fb5e02c @sorear Compiler-side modifications to attach code to subs and run units
authored Sep 18, 2011
19 run_optree($sub, $ops, 1);
20 }
21
d61f697 @sorear Change constant folding failure to be a warning
authored Feb 4, 2012
22 # sub sorry($msg) {
23 # die "No position info to report ($msg)" unless $*where;
24 # $*where.CURSOR.sorry($msg);
25 # }
26
27 sub worry($msg) {
2762955 @sorear If a constant foldable expression throws an exception, treat it as a …
authored Feb 4, 2012
28 die "No position info to report ($msg)" unless $*where;
d61f697 @sorear Change constant folding failure to be a warning
authored Feb 5, 2012
29 $*where.CURSOR.worry($msg);
2762955 @sorear If a constant foldable expression throws an exception, treat it as a …
authored Feb 4, 2012
30 }
31
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
32 sub no_named_params($op) {
33 if defined $op.args {
34 for @( $op.args ) -> $a {
d6e29a5 @sorear First batch of changes to remove "use"-dependencies from niecza
authored Jan 15, 2012
35 if $a.^isa($OpSimplePair) {
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
36 return Any;
d6e29a5 @sorear First batch of changes to remove "use"-dependencies from niecza
authored Jan 15, 2012
37 } elsif $a.^isa($OpCallSub) && $a.invocant.^isa($OpLexical)
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
38 && $a.invocant.name eq '&prefix:<|>' {
39 return Any;
40 }
41 }
42 }
43 ($op.args // $op.positionals);
44 }
45
46 sub capture_params($op) {
47 if !defined $op.args {
48 return ($op.positionals);
49 }
50
51 my @named;
52 my @pos;
53
54 for @( $op.args ) -> $a {
d6e29a5 @sorear First batch of changes to remove "use"-dependencies from niecza
authored Jan 15, 2012
55 if $a.^isa($OpSimplePair) {
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
56 push @named, $a.key => $a.value;
d6e29a5 @sorear First batch of changes to remove "use"-dependencies from niecza
authored Jan 15, 2012
57 } elsif $a.^isa($OpCallSub) && $a.invocant.^isa($OpLexical)
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
58 && $a.invocant.name eq '&prefix:<|>' {
59 return Nil;
60 } else {
61 push @pos, $a;
62 }
63 }
64
65 $(@pos), @named;
66 }
67
68 our %funcs = (
69 '&postcircumfix:<{ }>' => &do_atkey,
70 '&postcircumfix:<[ ]>' => &do_atpos,
4c4f12a @sorear Move most builtin definitions into the setting
authored Jun 24, 2011
71
1e98895 @sorear &return et al are inlinable primitives
authored Feb 13, 2011
72 '&last' => do_nullary_control(2),
4c4f12a @sorear Move most builtin definitions into the setting
authored Jun 24, 2011
73 '&next' => do_nullary_control(1),
74 '&proceed' => do_nullary_control(7),
9ba8284 @sorear Implement term:<proceed>, Any.{isa,can,does}, we now pass given.t
authored Jun 29, 2011
75 '&term:<proceed>' => do_nullary_control(7),
4c4f12a @sorear Move most builtin definitions into the setting
authored Jun 24, 2011
76 '&redo' => do_nullary_control(3),
77
31e91e9 @sorear Add a makejunction primitive
authored Jun 6, 2011
78 '&infix:<&>' => do_makejunction(0),
79 '&infix:<^>' => do_makejunction(2),
80 '&infix:<|>' => do_makejunction(3),
81 '&all' => do_makejunction(8),
82 '&none' => do_makejunction(9),
83 '&one' => do_makejunction(10),
84 '&any' => do_makejunction(11),
4c4f12a @sorear Move most builtin definitions into the setting
authored Jun 24, 2011
85
1e98895 @sorear &return et al are inlinable primitives
authored Feb 14, 2011
86 '&return' => &do_return_take,
4f237ff @sorear Fix return values in given/when
authored Jun 29, 2011
87 '&succeed' => &do_return_take,
1e98895 @sorear &return et al are inlinable primitives
authored Feb 14, 2011
88 '&take' => &do_return_take,
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
89 );
90
4c4f12a @sorear Move most builtin definitions into the setting
authored Jun 24, 2011
91 sub do_builtin($name, $expect) { sub ($body, $nv, $invname, $op) { #OK not used
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
92 return $op unless defined my $args = no_named_params($op);
2825023 @sorear push/pop/shift/unshift get inlined
authored May 30, 2011
93 return $op unless $args ~~ $expect;
6833fdb @sorear Add checkpoints to generated code to prevent the "warnings and some e…
authored May 26, 2012
94 return $OpBuiltin.new(pos => $op.pos, name => $name, args => $args);
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
95 } }
96
4c4f12a @sorear Move most builtin definitions into the setting
authored Jun 24, 2011
97 sub do_return_take($body, $nv, $invname, $op) { #OK not used
1e98895 @sorear &return et al are inlinable primitives
authored Feb 14, 2011
98 return $op unless defined my $args = no_named_params($op);
99 my $parcel = ($args == 1 ?? $args[0] !!
6833fdb @sorear Add checkpoints to generated code to prevent the "warnings and some e…
authored May 26, 2012
100 $args == 0 ?? $OpLexical.new(pos => $op.pos, name => 'Nil') !!
d6e29a5 @sorear First batch of changes to remove "use"-dependencies from niecza
authored Jan 15, 2012
101 $OpCallSub.new(invocant => $OpLexical.new(name => '&infix:<,>'),
6833fdb @sorear Add checkpoints to generated code to prevent the "warnings and some e…
authored May 26, 2012
102 pos => $op.pos, positionals => [@$args]));
1e98895 @sorear &return et al are inlinable primitives
authored Feb 14, 2011
103 return ($invname eq '&take' ??
6833fdb @sorear Add checkpoints to generated code to prevent the "warnings and some e…
authored May 26, 2012
104 $OpTake.new(pos => $op.pos, value => $parcel) !!
105 $OpControl.new(pos => $op.pos, payload => $parcel,
4f237ff @sorear Fix return values in given/when
authored Jun 29, 2011
106 number => $invname eq '&return' ?? 4 !! 6));
1e98895 @sorear &return et al are inlinable primitives
authored Feb 14, 2011
107 }
108
4c4f12a @sorear Move most builtin definitions into the setting
authored Jun 24, 2011
109 sub do_nullary_control($number) { sub ($body, $nv, $ , $op) { #OK not used
1e98895 @sorear &return et al are inlinable primitives
authored Feb 14, 2011
110 return $op unless defined my $args = no_named_params($op);
111 return $op unless $args == 0;
6833fdb @sorear Add checkpoints to generated code to prevent the "warnings and some e…
authored May 26, 2012
112 return $OpControl.new(:$number, pos => $op.pos, payload => $OpLexical.new(name => 'Nil'));
1e98895 @sorear &return et al are inlinable primitives
authored Feb 14, 2011
113 } }
114
4c4f12a @sorear Move most builtin definitions into the setting
authored Jun 24, 2011
115 sub do_makejunction($typecode) { sub ($body, $nv, $ , $op) { #OK not used
31e91e9 @sorear Add a makejunction primitive
authored Jun 6, 2011
116 return $op unless defined my $args = no_named_params($op);
6833fdb @sorear Add checkpoints to generated code to prevent the "warnings and some e…
authored May 26, 2012
117 return $OpMakeJunction.new(pos => $op.pos, :$typecode, zyg => @$args);
31e91e9 @sorear Add a makejunction primitive
authored Jun 6, 2011
118 } }
119
4c4f12a @sorear Move most builtin definitions into the setting
authored Jun 24, 2011
120 sub do_atkey($body, $nv, $invname, $op) { #OK not used
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
121 my ($args, %named) = capture_params($op);
122 return $op unless defined($args) && $args == 2;
123 my $delete = %named<delete>:delete;
124 my $exists = %named<exists>:delete;
125 return $op if %named;
d6e29a5 @sorear First batch of changes to remove "use"-dependencies from niecza
authored Jan 15, 2012
126 return $op if $delete && (!$delete.^isa($OpLexical) || $delete.name ne 'True');
127 return $op if $exists && (!$exists.^isa($OpLexical) || $exists.name ne 'True');
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
128 return $op if $delete && $exists;
6833fdb @sorear Add checkpoints to generated code to prevent the "warnings and some e…
authored May 26, 2012
129 return $OpBuiltin.new(pos => $op.pos, name => ($delete ?? 'delete_key' !!
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
130 $exists ?? 'exists_key' !! 'at_key'), args => $args);
131 }
132
4c4f12a @sorear Move most builtin definitions into the setting
authored Jun 24, 2011
133 sub do_atpos($body, $nv, $invname, $op) { #OK not used
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
134 return $op unless defined my $args = no_named_params($op);
135 return $op unless $args == 2;
6833fdb @sorear Add checkpoints to generated code to prevent the "warnings and some e…
authored May 26, 2012
136 return $OpBuiltin.new(pos => $op.pos, name => 'at_pos', args => $args);
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
137 }
138
eba5eb3 @sorear Take a first crack at implementing constant folding
authored Jan 8, 2012
139 # XXX should support folding of SimplePair, SimpleParcel too
0da7a8d @sorear Allow "constant" declarations to be folded as constants
authored Jan 22, 2012
140 sub check_folding($body, $sub, $op) {
eba5eb3 @sorear Take a first crack at implementing constant folding
authored Jan 9, 2012
141 my @evargs;
142 for $op.getargs -> $aop {
143 my $name;
d6e29a5 @sorear First batch of changes to remove "use"-dependencies from niecza
authored Jan 15, 2012
144 if $aop.^isa($OpSimplePair) {
eba5eb3 @sorear Take a first crack at implementing constant folding
authored Jan 9, 2012
145 $name = $aop.key;
146 $aop := $aop.value;
147 }
0da7a8d @sorear Allow "constant" declarations to be folded as constants
authored Jan 23, 2012
148 push @evargs, $name, ($aop.const_value($body) // return);
eba5eb3 @sorear Take a first crack at implementing constant folding
authored Jan 9, 2012
149 }
150
151 my $ret = $*unit.constant_fold($sub, @evargs) // return;
2762955 @sorear If a constant foldable expression throws an exception, treat it as a …
authored Feb 4, 2012
152 if $ret.^isa(Str) {
d61f697 @sorear Change constant folding failure to be a warning
authored Feb 5, 2012
153 $ret ~~ / $$ /;
154 worry "Operation cannot succeed (constant folding threw exception: $/.prematch())";
2762955 @sorear If a constant foldable expression throws an exception, treat it as a …
authored Feb 4, 2012
155 return;
156 }
6833fdb @sorear Add checkpoints to generated code to prevent the "warnings and some e…
authored May 26, 2012
157 $OpGeneralConst.new(pos => $op.pos, value => $ret);
eba5eb3 @sorear Take a first crack at implementing constant folding
authored Jan 9, 2012
158 }
159
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
160 sub run_optree($body, $op, $nv) {
586fbaa @sorear More misc bootstrap fixes, mostly undef ast stuff
authored Dec 20, 2011
161 die "WTF in $body.name()" if !defined $op;
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
162 my @kids := flat($op.ctxzyg($nv));
2762955 @sorear If a constant foldable expression throws an exception, treat it as a …
authored Feb 4, 2012
163 temp $*where; $*where = $op.pos // $*where;
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
164 my $i = 0;
165 while $i < @kids {
166 @kids[$i] = run_optree($body, @kids[$i], @kids[$i+1]);
167 $i = $i + 2;
168 }
169
d6e29a5 @sorear First batch of changes to remove "use"-dependencies from niecza
authored Jan 15, 2012
170 return $op unless $op.^isa($OpCallSub);
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
171 my $inv = $op.invocant;
d6e29a5 @sorear First batch of changes to remove "use"-dependencies from niecza
authored Jan 15, 2012
172 return $op unless $inv.^isa($OpLexical);
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
173 my $invname = $inv.name;
8a35c4f @sorear Finish basic type creation
authored Sep 28, 2011
174 my @inv_lex = $body.lookup_lex($invname);
b5d9d05 @sorear Allow defining multi-subs with inlining annotations
authored Dec 31, 2011
175 return $op unless @inv_lex;
176 @inv_lex = $body.lookup_lex($invname ~ ':(!proto)')
177 if @inv_lex[0] eq 'dispatch' &&
178 @inv_lex[4].has_lexical($invname ~ ':(!proto)');
179 return $op unless @inv_lex[0] eq 'sub';
8a35c4f @sorear Finish basic type creation
authored Sep 29, 2011
180
eba5eb3 @sorear Take a first crack at implementing constant folding
authored Jan 9, 2012
181 if @inv_lex[4].get_extend('pure') {
75abc9a @sorear When constant folding multis, call the dispatcher, not the proto. :)
authored May 26, 2012
182 # note, we have to use the real dispatcher here! don't look past it
183 # to the proto
184 my @real_lex = $body.lookup_lex($invname);
185 my $sub = @real_lex[0] eq 'sub' ?? @real_lex[4] !! @real_lex[5];
186 if check_folding($body, $sub, $op) -> $nop { return $nop }
eba5eb3 @sorear Take a first crack at implementing constant folding
authored Jan 9, 2012
187 }
188
11c6923 @sorear Reimplement sub "extend" functionality
authored Oct 5, 2011
189 if @inv_lex[4].get_extend('builtin') -> $B {
190 return $op unless defined my $args = no_named_params($op);
191 return $op unless $args >= $B[1] &&
192 (!defined($B[2]) || $args <= $B[2]);
6833fdb @sorear Add checkpoints to generated code to prevent the "warnings and some e…
authored May 26, 2012
193 return $OpBuiltin.new(pos => $op.pos, name => $B[0], args => $args);
11c6923 @sorear Reimplement sub "extend" functionality
authored Oct 5, 2011
194 }
8a35c4f @sorear Finish basic type creation
authored Sep 29, 2011
195
196 return $op unless @inv_lex[4].unit.name eq 'CORE';
319dea8 @sorear [v6] Port forward NieczaPassSimplifier
authored Jan 8, 2011
197 return $op unless my $func = %funcs{$invname};
198
199 $func($body, $nv, $invname, $op);
200 }
f5fd473 @sorear Finish removing "easy" cases of direct cross-module class referencing
authored Jan 15, 2012
201
202 INIT { $PassSimplifier = NieczaPassSimplifier; }
Something went wrong with that request. Please try again.