Mercurial > repos > rhope
comparison nworker_c.rhope @ 167:690ed78ead8a
Some type inference fixes and make method calls into regular calls when first arg type is known at compile time
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 04 Apr 2011 01:41:19 -0400 |
parents | d1e206ff75f9 |
children | d2b941f82d74 |
comparison
equal
deleted
inserted
replaced
166:1bfc19076f1b | 167:690ed78ead8a |
---|---|
221 ] | 221 ] |
222 } | 222 } |
223 | 223 |
224 _Has Input Types@NWorker Node[node,input num:does,does not] | 224 _Has Input Types@NWorker Node[node,input num:does,does not] |
225 { | 225 { |
226 does <- If[[input num] > [[node]Inputs >>]] {} | 226 does <- If[[input num] >= [[node]Inputs >>]] {} |
227 { | 227 { |
228 ,does not <- [[node]Input Types >>]Index[input num] | 228 ,does not <- [[node]Input Types >>]Index[input num] |
229 { | 229 { |
230 Print[[[[[["Input: "]Append[String[input num]]]Append[", count: "]]Append[String[count]]]Append[", wires: "]]Append[String[[[[node]Wires To >>]Index[ [input num]+[1] ]]Length]]] | |
230 count <- [~]Index[1] | 231 count <- [~]Index[1] |
231 ,does not <- If[[count] = [[[[node]Wires To >>]Index[input num]]Length]] | 232 ,does not <- If[[count] = [[[[node]Wires To >>]Index[[input num]+[1]]]Length]] |
232 { | 233 { |
233 does,does not <- [node]_Has Input Types[[input num]+[1]] | 234 does,does not <- [node]_Has Input Types[[input num]+[1]] |
234 } | 235 } |
235 } | 236 } |
236 } | 237 } |
239 Has Input Types?@NWorker Node[node:does,does not] | 240 Has Input Types?@NWorker Node[node:does,does not] |
240 { | 241 { |
241 If[[[node]Inputs >>] > [0]] | 242 If[[[node]Inputs >>] > [0]] |
242 { | 243 { |
243 does,does not <- _Has Input Types[node,0] | 244 does,does not <- _Has Input Types[node,0] |
245 { Print["does have input types"] } | |
246 { Print["does not have input types"] } | |
244 }{ | 247 }{ |
245 does <- Yes | 248 does <- Yes |
246 } | 249 } |
247 } | 250 } |
248 | 251 |
321 node index <- [[worker]Nodes >>]Length | 324 node index <- [[worker]Nodes >>]Length |
322 } | 325 } |
323 | 326 |
324 Propagate Type[nodelist,dest,prog,worker,type:out] | 327 Propagate Type[nodelist,dest,prog,worker,type:out] |
325 { | 328 { |
326 node <- [nodelist]Index[[dest]Index >>] | 329 If[[[dest]IO Num >>] >= [0]] |
330 { | |
331 Print[[[["Propagate Type, Index: "]Append[String[[dest]Index >>]]]Append[" io num: "]]Append[String[[dest]IO Num >>]]] | |
332 node <- [nodelist]Index[[dest]Index >>] | |
327 | 333 |
328 [[node]Input Types >>]Index[[dest]IO Num >>] | 334 [[node]Input Types >>]Index[[dest]IO Num >>] |
329 { | 335 { |
330 existing type <- [~]Index[0] | 336 Print[["Existing type: "]Append[[existing type]Name >>]] |
331 new count <- [[~]Index[1]]+[1] | 337 existing type <- [~]Index[0] |
332 If[[[existing type]Name >>] = [[type]Name >>]] | 338 new count <- [[~]Index[1]]+[1] |
333 { | 339 If[[[existing type]Name >>] = [[type]Name >>]] |
334 If[[[existing type]Variant >>] = [[type]Variant >>]] | 340 { |
335 { | 341 If[[[existing type]Variant >>] = [[type]Variant >>]] |
336 If[[[existing type]Params >>] = [[type]Params >>]] | |
337 { | 342 { |
338 new type <- Val[existing type] | 343 If[[[existing type]Params >>] = [[type]Params >>]] |
344 { | |
345 new type <- Val[existing type] | |
346 }{ | |
347 new variant <- [existing type]Variant >> | |
348 new params <- () | |
349 } | |
339 }{ | 350 }{ |
340 new variant <- [existing type]Variant >> | 351 new variant <- "Boxed" |
341 new params <- () | 352 If[[[existing type]Params >>] = [[type]Params >>]] |
353 { | |
354 new params <- [existing type]Params >> | |
355 }{ | |
356 new params <- () | |
357 } | |
342 } | 358 } |
359 new type <- [[existing type]Set Variant[new variant]]Params <<[new params] | |
343 }{ | 360 }{ |
344 new variant <- "Boxed" | 361 new type <- Type Instance["Any Type"] |
345 If[[[existing type]Params >>] = [[type]Params >>]] | 362 } |
346 { | 363 }{ |
347 new params <- [existing type]Params >> | 364 Print["No existing type info for this input"] |
348 }{ | 365 new type <- Val[type] |
349 new params <- () | 366 new count <- 1 |
350 } | 367 } |
351 } | 368 Print[[[["new type: "]Append[[new type]Name >>]]Append[", new count: "]]Append[String[new count]]] |
352 new type <- [[existing type]Set Variant[new variant]]Params <<[new params] | 369 new node <- [node]Input Types <<[ |
353 }{ | 370 [ [node]Input Types >> ]Set[ [dest]IO Num >>, [[()]Append[new type]]Append[new count] ] |
354 new type <- Type Instance["Any Type"] | 371 ] |
355 } | 372 { Print["got new node"] } |
356 }{ | 373 out <- Infer Types Node[[nodelist]Set[[dest]Index >>, new node], new node, [dest]Index >>, prog, worker] |
357 new type <- Val[type] | 374 }{ |
358 new count <- 1 | 375 out <- nodelist |
359 } | 376 } |
360 new node <- [node]Input Types <<[ | |
361 [ [node]Input Types >> ]Set[ [dest]IO Num >>, [[()]Append[new type]]Append[new count] ] | |
362 ] | |
363 out <- Infer Types Node[[nodelist]Set[[dest]Index >>, new node], new node, [dest]Index >>, prog, worker] | |
364 } | 377 } |
365 | 378 |
366 Propagate Types[nodelist,dests,output num,prog,worker,source node:out] | 379 Propagate Types[nodelist,dests,output num,prog,worker,source node:out] |
367 { | 380 { |
368 out <- Fold[Propagate Type[?, ?, prog, worker, [[source node]Output Types >>]Index[output num]], nodelist, dests] | 381 [[source node]Output Types >>]Index[output num] |
382 { | |
383 out <- Fold[Propagate Type[?, ?, prog, worker, ~], nodelist, dests] | |
384 }{ | |
385 Print[["Output type is missing for output #"]Append[String[output num]]] | |
386 out <- nodelist | |
387 } | |
369 } | 388 } |
370 | 389 |
371 Infer Types Node[nodelist,node,index,prog,worker:out] | 390 Infer Types Node[nodelist,node,index,prog,worker:out] |
372 { | 391 { |
392 If[[[node]Type >>]=["call"]] | |
393 { | |
394 extra <- [", worker: "]Append[[[node]Data >>]Name >>] | |
395 }{ | |
396 extra <- "" | |
397 } | |
398 Print[[[[["Infer Types Node: "]Append[String[index]]]Append[", type: "]]Append[[node]Type >>]]Append[extra]] | |
373 If[[[node]Type >>] = ["const"]] | 399 If[[[node]Type >>] = ["const"]] |
374 { | 400 { |
375 const type <- Blueprint Of[[node]Data >>] | 401 const type <- Blueprint Of[[node]Data >>] |
376 [(Int8(),UInt8(),Int16(),UInt16(),Int32(),UInt32(),Int64(),UInt64(), | 402 [(Int8(),UInt8(),Int16(),UInt16(),Int32(),UInt32(),Int64(),UInt64(), |
377 Type Instance(),Worker Literal(),List(),List Leaf(),String(),String Slice(),String Cat())]Find[=[const type, ?]] | 403 Type Instance(),Worker Literal(),List(),List Leaf(),String(),String Slice(),String Cat())]Find[=[const type, ?]] |
382 outtype <- "Any Type" | 408 outtype <- "Any Type" |
383 } | 409 } |
384 nextnode <- [node]Output Types <<[ [()]Append[Type Instance[outtype]] ] | 410 nextnode <- [node]Output Types <<[ [()]Append[Type Instance[outtype]] ] |
385 | 411 |
386 }{ | 412 }{ |
387 If[[[node]Type >>] = ["input"]] | 413 If[[[node]Type >>] = ["setglobal"]] |
388 { | 414 { |
389 nextnode <- [node]Output Types <<[ [()]Append[ [[worker]Input Types >>]Index[[node]Data >>] ] ] | 415 out <- nodelist |
390 }{ | 416 }{ |
391 If[[[node]Type >>] = ["output"]] | 417 If[[[node]Type >>] = ["input"]] |
392 { | 418 { |
393 out <- nodelist | 419 nextnode <- [node]Output Types <<[ [()]Append[ [[worker]Input Types >>]Index[[node]Data >>] ] ] |
394 | |
395 }{ | 420 }{ |
396 If[[[node]Type >>] = ["getglobal"]] | 421 If[[[node]Type >>] = ["output"]] |
397 { | 422 { |
398 nextnode <- [node]Output Types <<[ [()]Append[Type Instance["Any Type"]] ] | 423 out <- nodelist |
424 | |
399 }{ | 425 }{ |
400 [node]Has Input Types? | 426 If[[[node]Type >>] = ["getglobal"]] |
401 { | 427 { |
402 If[[[node]Type >>] = ["setfield"]] | 428 nextnode <- [node]Output Types <<[ [()]Append[Type Instance["Any Type"]] ] |
429 }{ | |
430 [node]Has Input Types? | |
403 { | 431 { |
404 nextnode <- [node]Output Types <<[ [()]Append[ [[[node]Input Types >>]Index[0]]Index[0] ] ] | 432 If[[[node]Type >>] = ["setfield"]] |
405 }{ | |
406 If[[[node]Type >>] = ["getfield"]] | |
407 { | 433 { |
408 type <- [[[node]Input Types >>]Index[0]]Index[0] | 434 nextnode <- [node]Output Types <<[ [()]Append[ [[[node]Input Types >>]Index[0]]Index[0] ] ] |
409 If[[[type]Name >>] = ["Any Type"]] | 435 }{ |
436 If[[[node]Type >>] = ["getfield"]] | |
410 { | 437 { |
411 outtype <- Val[type] | 438 type <- [[[node]Input Types >>]Index[0]]Index[0] |
412 }{ | 439 If[[[type]Name >>] = ["Any Type"]] |
413 outtype <- [prog]Find Field[[node]Data >>, type] {} | |
414 { | 440 { |
415 //TODO: Return errors rather than printing them | 441 outtype <- Val[type] |
416 Print[ | |
417 [[[[["Type " | |
418 ]Append[[type]Name >>] | |
419 ]Append[" does not have a field named "] | |
420 ]Append[[node]Data >>] | |
421 ]Append[" in worker "] | |
422 ]Append[worker name]] | |
423 } | |
424 } | |
425 nextnode <- [node]Output Types <<[ [()]Append[outtype] ] | |
426 }{ | |
427 | |
428 worker name <- [[node]Data >>]Name >> | |
429 [prog]Is Method?[worker name] | |
430 { | |
431 first arg type <- [[[node]Input Types >>]Index[0]]Index[0] | |
432 If[[[first arg type]Name >>] = ["Any Type"]] | |
433 { | |
434 outtypes <- Fold[Append[?, Type Instance["Any Type"]], (), Range[0, [node]Inputs >>]] | |
435 }{ | 442 }{ |
436 worker def <- [prog]Find Method[worker name, first arg type] {} | 443 outtype <- [prog]Find Field[[node]Data >>, type] {} |
437 { | 444 { |
438 //TODO: Return errors instead of printing them | 445 //TODO: Return errors rather than printing them |
439 Print[ | 446 Print[ |
440 [[[[["Type " | 447 [[[[["Type " |
441 ]Append[[first arg type]Name >>] | 448 ]Append[[type]Name >>] |
442 ]Append[" does not support method "] | 449 ]Append[" does not have a field named "] |
443 ]Append[worker name] | 450 ]Append[[node]Data >>] |
444 ]Append[" in worker "] | 451 ]Append[" in worker "] |
445 ]Append[ [worker]Name >> ]] | 452 ]Append[worker name]] |
446 } | 453 } |
447 } | 454 } |
455 nextnode <- [node]Output Types <<[ [()]Append[outtype] ] | |
448 }{ | 456 }{ |
449 worker def <- [prog]Find Worker Def[worker name] | 457 worker name <- [[node]Data >>]Name >> |
458 [prog]Is Method?[worker name] | |
459 { | |
460 Print[[["Infer Types: "]Append[worker name]]Append[" is a method"]] | |
461 first arg type <- [[[node]Input Types >>]Index[0]]Index[0] | |
462 If[[[first arg type]Name >>] = ["Any Type"]] | |
463 { | |
464 Print["No type info for first arg"] | |
465 outtypes <- Fold[Append[?, Type Instance["Any Type"]], (), Range[0, [node]Inputs >>]] | |
466 Print[["outtypes length: "]Append[String[Length[outtypes]]]] | |
467 nextnode <- [node]Output Types <<[ outtypes ] | |
468 }{ | |
469 worker def <- [prog]Find Method[worker name, first arg type] | |
470 { | |
471 new worker name <- [[worker name]Append["@"]]Append[[first arg type]Name >>] | |
472 new ref <- [prog]Find Worker[new worker name] {} { | |
473 Print[["Could not find worker ref for "]Append[new worker name]] | |
474 } | |
475 nextnode <- [[node]Output Types <<[outtypes] | |
476 ]Data <<[new ref] | |
477 Print[["New worker name:"]Append[new worker name]] | |
478 }{ | |
479 //TODO: Return errors instead of printing them | |
480 Print[ | |
481 [[[[["Type " | |
482 ]Append[[first arg type]Name >>] | |
483 ]Append[" does not support method "] | |
484 ]Append[worker name] | |
485 ]Append[" in worker "] | |
486 ]Append[ [worker]Name >> ]] | |
487 } | |
488 } | |
489 }{ | |
490 Print[[["Infer Types: "]Append[worker name]]Append[" is not a method"]] | |
491 worker def <- [prog]Find Worker Def[worker name] {} | |
492 { Print["Error, could not find worker def"] } | |
493 nextnode <- [node]Output Types <<[ outtypes ] | |
494 } | |
495 outtypes <- [worker def]Output Types >> | |
450 } | 496 } |
451 outtypes <- [worker def]Output Types >> | |
452 nextnode <- [node]Output Types <<[ outtypes ] | |
453 } | 497 } |
498 }{ | |
499 Print["Skipping node because input type info not present"] | |
500 out <- nodelist | |
454 } | 501 } |
455 }{ | |
456 out <- nodelist | |
457 } | 502 } |
458 } | 503 } |
459 } | 504 } |
460 } | 505 } |
461 } | 506 } |
462 | 507 |
463 Val[nextnode] | 508 Val[nextnode] |
464 { | 509 { |
465 nextlist <- [nodelist]Set[index, nextnode] | 510 nextlist <- [nodelist]Set[index, nextnode] |
466 out <- Fold[Propagate Types[?, ?, ?, prog, worker, nextnode], nodelist, [nextnode]Wires From >>] | 511 { Print["Calling Propagate types on descdendants of this node"] } |
512 out <- Fold[Propagate Types[?, ?, ?, prog, worker, nextnode], nextlist, [nextnode]Wires From >>] | |
467 } | 513 } |
468 } | 514 } |
469 | 515 |
470 Infer Types@NWorker[worker,prog:out] | 516 Infer Types@NWorker[worker,prog:out] |
471 { | 517 { |
1316 } | 1362 } |
1317 | 1363 |
1318 Add Method@NBlueprint[bp,name:out] | 1364 Add Method@NBlueprint[bp,name:out] |
1319 { | 1365 { |
1320 out <- [bp]Methods <<[ [[bp]Methods >>]Set[name, Yes] ] | 1366 out <- [bp]Methods <<[ [[bp]Methods >>]Set[name, Yes] ] |
1367 | |
1321 } | 1368 } |
1322 | 1369 |
1323 Understands Method@NBlueprint[bp,name:out] | 1370 Understands Method@NBlueprint[bp,name:out] |
1324 { | 1371 { |
1325 out <- [[bp]Methods >>]Index[name] {} | 1372 out <- [[bp]Methods >>]Index[name] {} |
1326 { out <- No } | 1373 { out <- No } |
1327 } | 1374 } |
1328 | 1375 |
1376 Eq Field Name[field,name:out] | |
1377 { | |
1378 out <- [[field]Index[0]]=[name] | |
1379 } | |
1380 | |
1329 Get Field Type@NBlueprint[bp,name:out,notfound] | 1381 Get Field Type@NBlueprint[bp,name:out,notfound] |
1330 { | 1382 { |
1331 ,notfound <- [[bp]Fields >>]Index[name] | 1383 ,not found <- [[bp]Fields >>]Find[Eq Field Name[?, name]] |
1332 { out <- [~]Index[1] } | 1384 { out <- [[[bp]Fields >>]Index[~]]Index[1] } |
1333 } | 1385 } |
1334 | 1386 |
1335 _Compile Blueprint Fields[type,field:out] | 1387 _Compile Blueprint Fields[type,field:out] |
1336 { | 1388 { |
1337 name <- [field]Index[0] | 1389 name <- [field]Index[0] |
1559 | 1611 |
1560 Register Method@NProgram[prog, name, convention, inputs, outputs: out] | 1612 Register Method@NProgram[prog, name, convention, inputs, outputs: out] |
1561 { | 1613 { |
1562 [[prog]Worker Refs >>]Index[name] | 1614 [[prog]Worker Refs >>]Index[name] |
1563 { | 1615 { |
1564 ref <- [[[[~]Inputs <<[ Max[[~]Inputs >>, inputs] ] | 1616 ref <- [[[[[~]Inputs <<[ Max[[~]Inputs >>, inputs] ] |
1565 ]Min Inputs <<[ Min[[~]Min Inputs >>, inputs] ] | 1617 ]Min Inputs <<[ Min[[~]Min Inputs >>, inputs] ] |
1566 ]Outputs <<[ Max[[~]Outputs >>, outputs] ] | 1618 ]Outputs <<[ Max[[~]Outputs >>, outputs] ] |
1567 ]Min Outputs <<[ Min[[~]Min Outputs >>, outputs] ] | 1619 ]Min Outputs <<[ Min[[~]Min Outputs >>, outputs] ] |
1620 ]Is Method? <<[Yes] | |
1568 }{ | 1621 }{ |
1569 ref <- Worker Ref[name, convention, inputs, outputs, Yes] | 1622 ref <- Worker Ref[name, convention, inputs, outputs, Yes] |
1570 } | 1623 } |
1571 out <- [prog]Worker Refs <<[ [[prog]Worker Refs >>]Set[name, ref]] | 1624 out <- [prog]Worker Refs <<[ [[prog]Worker Refs >>]Set[name, ref]] |
1572 } | 1625 } |