| 1 | use v5.10; | ||
| 2 | use Mildew::Emit::Yeast; | ||
| 3 | use MooseX::Declare; | ||
| 4 | class Mildew::TypeInfo { | ||
| 5 | has type=>(is=>'rw',builder=>'infer_type',lazy=>1); | ||
| 6 | has mold=>(is=>'rw',isa=>'Mildew::AST::Block::SSA'); | ||
| 7 | has usage=>(is=>'ro',isa=>'ArrayRef[Mildew::AST::Base]',default=>sub {[]}); | ||
| 8 | method add_usage($stmt) { | ||
| 9 | push(@{$self->usage},$stmt); | ||
| 10 | } | ||
| 11 | method infer_type { | ||
| 12 | Mildew::Type::Unknown->new(); | ||
| 13 | } | ||
| 14 | } | ||
| 15 | |||
| 16 | class Mildew::TypeInfo::FromAssignment extends Mildew::TypeInfo { | ||
| 17 | has orgin=>(is=>'ro',isa=>'Mildew::AST::Base',required=>1); | ||
| 18 | method infer_type { | ||
| 19 | $self->type(Mildew::Type::SelfRecursive->new()); | ||
| 20 | my $rvalue = $self->orgin->rvalue; | ||
| 21 | if ($rvalue->isa('Mildew::AST::Call')) { | ||
| 22 | my $type = $rvalue->capture->invocant->type_info->type->method_call($self->orgin); | ||
| 23 | $self->type($type); | ||
| 24 | for my $usage (@{$self->usage}) { | ||
| 25 | $type->add_usage($self->orgin->lvalue,$usage); | ||
| 26 | } | ||
| 27 | $type; | ||
| 28 | } else { | ||
| 29 | Mildew::Type::Unknown->new(); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | class Mildew::TypeInfo::Phi extends Mildew::TypeInfo { | ||
| 34 | } | ||
| 35 | class Mildew::TypeInfo::IntegerConstant extends Mildew::TypeInfo { | ||
| 36 | method infer_type { | ||
| 37 | Mildew::Type::IntegerConstant->new(); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | class Mildew::TypeInfo::StringConstant extends Mildew::TypeInfo { | ||
| 41 | } | ||
| 42 | class Mildew::TypeInfo::External extends Mildew::TypeInfo { | ||
| 43 | } | ||
| 44 | class Mildew::Type { | ||
| 45 | sub str { | ||
| 46 | my ($value,$str) = @_; | ||
| 47 | is_str($value) && $value->value; | ||
| 48 | } | ||
| 49 | sub is_str { | ||
| 50 | my ($value) = @_; | ||
| 51 | defined $value && $value->isa('Mildew::AST::StringConstant'); | ||
| 52 | } | ||
| 53 | method debug { | ||
| 54 | } | ||
| 55 | method method_call($call) { | ||
| 56 | Mildew::Type::Unknown->new(); | ||
| 57 | } | ||
| 58 | method emit_call($i,$stmt,$value) { | ||
| 59 | my $list = sub { | ||
| 60 | "(SMOP__Object*[]) {" . join(',',(map {"SMOP_REFERENCE(interpreter,".$value->($_).")"} @_),"NULL") . "}" | ||
| 61 | }; | ||
| 62 | my $capture = $stmt->rvalue->capture; | ||
| 63 | |||
| 64 | Mildew::Emit::Yeast::measure($stmt->id,"frame->pc = " . ($i+1) . ";\n" | ||
| 65 | . "frame->ret = &" . $value->($stmt->lvalue) . ";\n" | ||
| 66 | . Mildew::Emit::Yeast::assign($value->($stmt->lvalue), | ||
| 67 | "SMOP_DISPATCH(\n" . "interpreter,\nSMOP_RI(" . $value->($capture->invocant) . "),\n" | ||
| 68 | . $value->($stmt->rvalue->identifier) | ||
| 69 | . ",\nSMOP__NATIVE__capture_create(interpreter," | ||
| 70 | . $list->($capture->invocant,@{$capture->positional}) | ||
| 71 | . "," | ||
| 72 | . $list->(@{$capture->named}) | ||
| 73 | . ")\n" . ")") | ||
| 74 | . "break;\n"); | ||
| 75 | } | ||
| 76 | method emit_perlesque_call($stmt,$value) { | ||
| 77 | my $list = sub { | ||
| 78 | "[" . join(',',(map {$value->($_)} @_)) . "]" | ||
| 79 | }; | ||
| 80 | my $capture = $stmt->rvalue->capture; | ||
| 81 | my $perlesque_capture = Mildew::AST::unique_id(); | ||
| 82 | |||
| 83 | my $add_named = ''; | ||
| 84 | my @named = @{$capture->named}; | ||
| 85 | use Devel::PartialDump qw(warn); | ||
| 86 | while (@named) { | ||
| 87 | warn("named:",\@named); | ||
| 88 | $add_named .= "$perlesque_capture.add_named(" . $value->(shift @named) . "," .$value->(shift @named) . ")"; | ||
| 89 | } | ||
| 90 | "my P6capture $perlesque_capture = P6capture.new();\n" | ||
| 91 | . $add_named | ||
| 92 | . join("",(map {"$perlesque_capture.add_positional(" . $value->($_) . ");\n"} ($capture->invocant,@{$capture->positional}))) | ||
| 93 | . $value->($stmt->lvalue) . " = " | ||
| 94 | . $value->($capture->invocant) | ||
| 95 | . ".DISPATCH(" . $value->($stmt->rvalue->identifier) | ||
| 96 | . ",$perlesque_capture" | ||
| 97 | #. ",p6capture(" | ||
| 98 | #. $list->($capture->invocant,@{$capture->positional}) | ||
| 99 | #. ',' | ||
| 100 | #. $list->(@{$capture->named}) | ||
| 101 | . ");" | ||
| 102 | } | ||
| 103 | method add_usage($reg,$stmt) { | ||
| 104 | } | ||
| 105 | method pretty() { | ||
| 106 | ref $self; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | role Mildew::Type::FETCH { | ||
| 110 | method method_call($stmt) { | ||
| 111 | my $call = $stmt->rvalue; | ||
| 112 | my $id = Mildew::Type::str($call->identifier); | ||
| 113 | if ($id eq 'FETCH') { | ||
| 114 | $self; | ||
| 115 | } else { | ||
| 116 | Mildew::Type::Unknown->new(); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | class Mildew::Type::IntegerConstant extends Mildew::Type with Mildew::Type::FETCH { | ||
| 121 | } | ||
| 122 | class Mildew::Type::Prototype extends Mildew::Type { | ||
| 123 | has type=>(is=>'ro'); | ||
| 124 | method method_call($stmt) { | ||
| 125 | my $call = $stmt->rvalue; | ||
| 126 | my $id = Mildew::Type::str($call->identifier); | ||
| 127 | if ($id eq 'new') { | ||
| 128 | $self->type->(); | ||
| 129 | } elsif ($id eq 'FETCH') { | ||
| 130 | $self; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | class Mildew::Type::Scope extends Mildew::Type { | ||
| 135 | has content=>(is=>'rw'); | ||
| 136 | has reg=>(is=>'rw',isa=>'Mildew::AST::Reg'); | ||
| 137 | has outer=>(is=>'ro',isa=>'Mildew::Type'); | ||
| 138 | use Scalar::Util qw(refaddr); | ||
| 139 | use Term::ANSIColor qw(:constants); | ||
| 140 | |||
| 141 | # XXX do more genericly with add_usage | ||
| 142 | method infer_lexicals { | ||
| 143 | |||
| 144 | $self->debug("infering lexicals"); | ||
| 145 | #return undef unless $self->reg; | ||
| 146 | |||
| 147 | $self->content({}); | ||
| 148 | |||
| 149 | for my $stmt (@{$self->reg->type_info->usage}) { | ||
| 150 | if ($stmt->rvalue->isa('Mildew::AST::Call')) { | ||
| 151 | my $call = $stmt->rvalue; | ||
| 152 | if (refaddr($call->capture->invocant) == refaddr($self->reg)) { | ||
| 153 | if ( | ||
| 154 | @{$call->capture->positional} == 1 | ||
| 155 | && Mildew::Type::is_str($call->capture->positional->[0]) | ||
| 156 | ) { | ||
| 157 | my $name = Mildew::Type::str($call->capture->positional->[0]); | ||
| 158 | if (Mildew::Type::str($call->identifier) eq 'postcircumfix:{ }') { | ||
| 159 | $self->debug("variable defined in scope: ",GREEN,$name,RESET); | ||
| 160 | $self->content->{$name} = Mildew::Type::Lexical->new(); | ||
| 161 | $stmt->lvalue->type_info->type(); | ||
| 162 | } elsif (Mildew::Type::str($call->identifier) eq 'lookup') { | ||
| 163 | $self->debug("variable used in scope:",GREEN,$name,RESET); | ||
| 164 | $stmt->lvalue->type_info->type(); | ||
| 165 | } else { | ||
| 166 | $self->debug("unknown usage of scope:",RED,$stmt->pretty,RESET); | ||
| 167 | } | ||
| 168 | } else { | ||
| 169 | $self->debug("unknown usage of scope:",$stmt->pretty); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | } | ||
| 174 | $self->debug("infered lexicals"); | ||
| 175 | } | ||
| 176 | method lookup($varname) { | ||
| 177 | $self->debug("looking up $varname"); | ||
| 178 | if (!defined $self->content) { | ||
| 179 | return Mildew::Type::Unknown->new(); | ||
| 180 | } | ||
| 181 | if (my $type = $self->content->{$varname}) { | ||
| 182 | $type; | ||
| 183 | } elsif ($self->outer) { | ||
| 184 | $self->outer->lookup($varname); | ||
| 185 | } else { | ||
| 186 | Mildew::Type::Unknown->new(); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | method method_call($stmt) { | ||
| 190 | my $call = $stmt->rvalue; | ||
| 191 | $self->reg($call->capture->invocant); | ||
| 192 | if (!defined $self->content) { | ||
| 193 | $self->infer_lexicals; | ||
| 194 | } | ||
| 195 | my $id = Mildew::Type::str($call->identifier); | ||
| 196 | if (($id eq 'lookup' || $id eq 'postcircumfix:{ }') && Mildew::Type::is_str($call->capture->positional->[0])) { | ||
| 197 | $self->debug("handling method:",$id); | ||
| 198 | $self->lookup(Mildew::Type::str($call->capture->positional->[0])); | ||
| 199 | } else { | ||
| 200 | $self->debug("not handling method:",$id); | ||
| 201 | Mildew::Type::Unknown->new(); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | class Mildew::Type::Lexical extends Mildew::Type { | ||
| 206 | use Term::ANSIColor qw(:constants); | ||
| 207 | use Scalar::Util qw(refaddr); | ||
| 208 | use Carp qw(cluck); | ||
| 209 | has content=>(is=>'rw',isa=>'Mildew::Type',lazy_build=>1); | ||
| 210 | has binds=>(is=>'ro',isa=>'ArrayRef[Type]',default=>sub {[]}); | ||
| 211 | has stores=>(is=>'ro',isa=>'ArrayRef[Type]',default=>sub {[]}); | ||
| 212 | method _build_content { | ||
| 213 | $self->debug("infering content of lexical"); | ||
| 214 | my $container; | ||
| 215 | if (@{$self->binds} == 1) { | ||
| 216 | $container = $self->binds->[0]; | ||
| 217 | $self->debug("1 BIND: ",$self->binds->[0]); | ||
| 218 | } else { | ||
| 219 | $self->debug("many BINDs"); | ||
| 220 | $container = Mildew::Type::Scalar->new(); | ||
| 221 | } | ||
| 222 | |||
| 223 | use Data::Dumper; | ||
| 224 | $self->debug('stores = ',Dumper($self->stores)); | ||
| 225 | if ($container->can('add_store')) { | ||
| 226 | $container->add_store($_) for @{$self->stores}; | ||
| 227 | } | ||
| 228 | $self->debug("infered content of lexical"); | ||
| 229 | $container; | ||
| 230 | } | ||
| 231 | method method_call($call) { | ||
| 232 | if (Mildew::Type::str($call->rvalue->identifier) eq 'FETCH') { | ||
| 233 | $self->debug(RED,"called FETCH on lexical",RESET); | ||
| 234 | $self->content->method_call($call); | ||
| 235 | } elsif (Mildew::Type::str($call->rvalue->identifier) eq 'BIND') { | ||
| 236 | $self; | ||
| 237 | } else { | ||
| 238 | Mildew::Type::Unknown->new(); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | method add_usage($reg,$usage) { | ||
| 242 | if ($usage->isa('Mildew::AST::Assign')) { | ||
| 243 | my $call = $usage->rvalue; | ||
| 244 | if ($call->isa('Mildew::AST::Call') && (refaddr $call->capture->invocant == refaddr $reg)) { | ||
| 245 | my $id = Mildew::Type::str($call->identifier); | ||
| 246 | if ($id eq 'BIND') { | ||
| 247 | push (@{$self->binds},$call->capture->positional->[0]->type_info->type); | ||
| 248 | $self->debug("propagating {"); | ||
| 249 | $usage->lvalue->type_info->type(); | ||
| 250 | $self->debug("}"); | ||
| 251 | return; | ||
| 252 | } elsif ($id eq 'STORE') { | ||
| 253 | $self->debug("STORE on lexicals {"); | ||
| 254 | push (@{$self->stores},$call->capture->positional->[0]->type_info->type); | ||
| 255 | $self->debug("}"); | ||
| 256 | return; | ||
| 257 | } elsif ($id eq 'FETCH') { | ||
| 258 | return; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | } | ||
| 262 | $self->debug(RED,"unknow usage of lexical ",$reg->pretty,": ",$usage->pretty,RESET); | ||
| 263 | } | ||
| 264 | method pretty { | ||
| 265 | (ref $self) . " of " . $self->content->pretty; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | class Mildew::Type::Scalar extends Mildew::Type { | ||
| 269 | use Carp qw(cluck); | ||
| 270 | use Scalar::Util qw(refaddr); | ||
| 271 | has stores=>(is=>'ro',isa=>'ArrayRef[Type]',default=>sub {[]}); | ||
| 272 | has content=>(is=>'rw',builder=>'infer_content',lazy=>1,predicate=>'has_content'); | ||
| 273 | method add_store($content) { | ||
| 274 | $self->debug("adding store to ",(refaddr $self)); | ||
| 275 | push(@{$self->stores},$content); | ||
| 276 | } | ||
| 277 | method infer_content { | ||
| 278 | if (@{$self->stores} == 1) { | ||
| 279 | $self->debug("just enough stores"); | ||
| 280 | $self->stores->[0]; | ||
| 281 | } else { | ||
| 282 | use Data::Dumper; | ||
| 283 | #cluck "wrong number of stores: ",(refaddr $self),Dumper($self->stores); | ||
| 284 | Mildew::Type::Unknown->new(); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | method method_call($call) { | ||
| 288 | if (Mildew::Type::str($call->rvalue->identifier) eq 'FETCH') { | ||
| 289 | $self->content; | ||
| 290 | } else { | ||
| 291 | Mildew::Type::Unknown->new(); | ||
| 292 | } | ||
| 293 | } | ||
| 294 | method pretty { | ||
| 295 | 'Mildew::Type::Scalar of ' . ($self->has_content ? $self->content->pretty : '(not calculated yet value)'); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | class Mildew::Type::Unknown extends Mildew::Type { | ||
| 299 | } | ||
| 300 | |||
| 301 | class Mildew::Type::SelfRecursive extends Mildew::Type::Unknown { | ||
| 302 | method method_call($call) { | ||
| 303 | $self->debug("method call on self recursive\n"); | ||
| 304 | Mildew::Type::SelfRecursive->new(); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | class Mildew::Type::MildewSOLoader extends Mildew::Type { | ||
| 309 | } | ||
| 310 | $Mildew::LexicalPreludeType = Mildew::Type::Scope->new( | ||
| 311 | content => { | ||
| 312 | MildewSOLoader => Mildew::Type::Lexical->new(content=>Mildew::Type::MildewSOLoader->new()), | ||
| 313 | Scalar => Mildew::Type::Prototype->new(type=>sub {Mildew::Type::Scalar->new()}), | ||
| 314 | } | ||
| 315 | ); |