>From 8f97fe3f3509d5dcbbf0c5cca4f2afeadb094258 Mon Sep 17 00:00:00 2001 From: Evan Hanson Date: Mon, 15 Feb 2016 22:05:42 +1300 Subject: [PATCH 1/4] Track source nodes for better scrutiny output Adds a map from specialized nodes back to their original source nodes so that the scrutinizer can (a) print line numbers for parts of the program that started out as ##core#call nodes but ended up being specialized to something without line number information, and (b) print program fragments as they appeared in the user's source, pre-specialization. --- scrutinizer.scm | 60 ++++++++++++++++++++++++++++++++++++++++++--------------- support.scm | 7 ++++++- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/scrutinizer.scm b/scrutinizer.scm index b4ed8e1..4d4cf5b 100644 --- a/scrutinizer.scm +++ b/scrutinizer.scm @@ -127,16 +127,6 @@ (define (walked-result n) (first (node-parameters n))) ; assumes ##core#the/result node -(define (node-line-number n) - (case (node-class n) - ((##core#call) - (let ((params (node-parameters n))) - (and (pair? (cdr params)) - (pair? (cadr params)) ; debug-info has line-number information? - (source-info->line (cadr params))))) - ((##core#typecase) - (car (node-parameters n))) - (else #f))) (define (scrutinize node db complain specialize strict block-compilation) (let ((blist '()) ; (((VAR . FLOW) TYPE) ...) @@ -272,7 +262,7 @@ (define (node-source-prefix n) (let ((line (node-line-number n))) - (if (not line) "" (sprintf "(~a) " line)))) + (if (not line) "" (sprintf "(~a) " line)))) (define (location-name loc) (define (lname loc1) @@ -291,7 +281,7 @@ (define add-loc cons) (define (fragment x) - (let ((x (build-expression-tree x))) + (let ((x (build-expression-tree (source-node-tree x)))) (let walk ((x x) (d 0)) (cond ((atom? x) (strip-syntax x)) ((>= d +fragment-max-depth+) '...) @@ -842,7 +832,7 @@ (append (type-typeenv (car types)) typeenv) #t) ;; drops exp - (copy-node! (car subs) n) + (mutate-node! n (car subs)) (walk n e loc dest tail flow ctags)) (else (trail-restore trail0 typeenv) @@ -1844,6 +1834,47 @@ db) (print "; END OF FILE")))) +;; +;; Source node tracking +;; +;; Nodes are mutated in place during specialization, which may lose line +;; number information if, for example, a node is changed from a +;; ##core#call to a class without debug info. To preserve line numbers +;; and allow us to print fragments of the original source, we maintain a +;; side table of mappings from mutated nodes to copies of the originals. +;; + +(define node-mutations '()) + +(define (mutate-node! node expr) + (set! node-mutations (alist-update! node (copy-node node) node-mutations)) + (copy-node! (build-node-graph expr) node)) + +(define (source-node n #!optional (k values)) + (let ((orig (alist-ref n node-mutations eq?))) + (if (not orig) (k n) (source-node orig k)))) + +(define (source-node-tree n) + (source-node + n + (lambda (n*) + (make-node (node-class n*) + (node-parameters n*) + (map source-node (node-subexpressions n*)))))) + +(define (node-line-number n) + (node-debug-info (source-node n))) + +(define (node-debug-info n) + (case (node-class n) + ((##core#call) + (let ((params (node-parameters n))) + (and (pair? (cdr params)) + (pair? (cadr params)) ; debug-info has line-number information? + (source-info->line (cadr params))))) + ((##core#typecase) + (car (node-parameters n))) + (else #f))) ;; Mutate node for specialization @@ -1871,8 +1902,7 @@ ((not (pair? x)) x) ((eq? 'quote (car x)) x) ; to handle numeric constants (else (cons (subst (car x)) (subst (cdr x)))))) - (let ((spec (subst template))) - (copy-node! (build-node-graph spec) node)))) + (mutate-node! node (subst template)))) ;;; Type-validation and -normalization diff --git a/support.scm b/support.scm index 63c7fc7..51e6a29 100644 --- a/support.scm +++ b/support.scm @@ -45,7 +45,7 @@ node-parameters node-parameters-set! node-subexpressions node-subexpressions-set! varnode qnode build-node-graph build-expression-tree fold-boolean inline-lambda-bindings - tree-copy copy-node! emit-global-inline-file load-inline-file + tree-copy copy-node! copy-node emit-global-inline-file load-inline-file match-node expression-has-side-effects? simple-lambda-node? dump-undefined-globals dump-defined-globals dump-global-refs make-foreign-callback-stub foreign-callback-stub? @@ -732,6 +732,11 @@ (cons (rec (car t)) (rec (cdr t))) t) ) ) +(define (copy-node n) + (make-node (node-class n) + (node-parameters n) + (node-subexpressions n))) + (define (copy-node! from to) (node-class-set! to (node-class from)) (node-parameters-set! to (node-parameters from)) -- 2.7.0.rc3