|
1460 | 1460 | (if (length= e 3) |
1461 | 1461 | `(const ,(cadr e) ,(expand-forms (caddr e))) |
1462 | 1462 | (let ((arg (cadr e))) |
1463 | | - (case (car arg) |
1464 | | - ((global) (let ((asgn (cadr arg))) |
1465 | | - (check-assignment asgn) |
1466 | | - `(block |
1467 | | - ,.(map (lambda (v) `(global ,v)) |
1468 | | - (lhs-bound-names (cadr asgn))) |
1469 | | - ,(expand-assignment asgn #t)))) |
1470 | | - ((=) (check-assignment arg) |
1471 | | - (expand-assignment arg #t)) |
1472 | | - (else (error "expected assignment after \"const\"")))))) |
| 1463 | + (cond |
| 1464 | + ((symbol? arg) |
| 1465 | + ;; Undefined constant: Expr(:const, :a) (not available in surface syntax) |
| 1466 | + `(block ,e (latestworld))) |
| 1467 | + ((eq? (car arg) 'global) |
| 1468 | + (let ((asgn (cadr arg))) |
| 1469 | + (check-assignment asgn) |
| 1470 | + `(block |
| 1471 | + ,.(map (lambda (v) `(global ,v)) |
| 1472 | + (lhs-bound-names (cadr asgn))) |
| 1473 | + ,(expand-assignment asgn #t)))) |
| 1474 | + ((eq? (car arg) '=) |
| 1475 | + (check-assignment arg) |
| 1476 | + (expand-assignment arg #t)) |
| 1477 | + (else |
| 1478 | + (error "expected assignment after \"const\"")))))) |
1473 | 1479 |
|
1474 | 1480 | (define (expand-atomic-decl e) |
1475 | 1481 | (error "unimplemented or unsupported atomic declaration")) |
|
3103 | 3109 | (set! vars (cons (cadr e) vars))) |
3104 | 3110 | ((= const) |
3105 | 3111 | (let ((v (decl-var (cadr e)))) |
3106 | | - (find-assigned-vars- (caddr e)) |
| 3112 | + (unless (and (eq? (car e) 'const) (null? (cddr e))) |
| 3113 | + (find-assigned-vars- (caddr e))) |
3107 | 3114 | (if (or (ssavalue? v) (globalref? v) (underscore-symbol? v)) |
3108 | 3115 | '() |
3109 | 3116 | (set! vars (cons v vars))))) |
|
3525 | 3532 | (vinfo:set-sa! vi #f) |
3526 | 3533 | (vinfo:set-sa! vi #t)) |
3527 | 3534 | (vinfo:set-asgn! vi #t)))) |
3528 | | - (analyze-vars (caddr e) env captvars sp tab)) |
| 3535 | + (unless (null? (cddr e)) |
| 3536 | + (analyze-vars (caddr e) env captvars sp tab))) |
3529 | 3537 | ((call) |
3530 | 3538 | (let ((vi (get tab (cadr e) #f))) |
3531 | 3539 | (if vi |
@@ -4148,8 +4156,6 @@ f(x) = yt(x) |
4148 | 4156 | `(call (core declare_global) (thismodule) (inert ,(cadr e)) (false))) |
4149 | 4157 | (latestworld))))) |
4150 | 4158 | ((const) |
4151 | | - ;; Check we've expanded surface `const` (1 argument form) |
4152 | | - (assert (and (length= e 3))) |
4153 | 4159 | (when (globalref? (cadr e)) |
4154 | 4160 | (put! globals (cadr e) #f)) |
4155 | 4161 | e) |
@@ -4719,10 +4725,15 @@ f(x) = yt(x) |
4719 | 4725 | (list cnd)))))) |
4720 | 4726 | tests)) |
4721 | 4727 | (define (emit-assignment-or-setglobal lhs rhs (op '=)) |
4722 | | - ;; (const (globalref _ _) _) does not use setglobal! |
4723 | | - (if (and (globalref? lhs) (eq? op '=)) |
4724 | | - (emit `(call (top setglobal!) ,(cadr lhs) (inert ,(caddr lhs)) ,rhs)) |
4725 | | - (emit `(,op ,lhs ,rhs)))) |
| 4728 | + ;; (= (globalref _ _) _) => setglobal! |
| 4729 | + ;; (const (globalref _ _) _) => declare_const |
| 4730 | + (cond ((and (globalref? lhs) (eq? op '=)) |
| 4731 | + (emit `(call (core setglobal!) ,(cadr lhs) (inert ,(caddr lhs)) ,rhs))) |
| 4732 | + ((and (globalref? lhs) (eq? op 'const)) |
| 4733 | + (emit `(call (core declare_const) ,(cadr lhs) (inert ,(caddr lhs)) ,rhs))) |
| 4734 | + (else |
| 4735 | + (assert (eq? op '=)) |
| 4736 | + (emit `(= ,lhs ,rhs))))) |
4726 | 4737 | (define (emit-assignment lhs rhs (op '=)) |
4727 | 4738 | (if rhs |
4728 | 4739 | (if (valid-ir-rvalue? lhs rhs) |
@@ -4803,21 +4814,26 @@ f(x) = yt(x) |
4803 | 4814 | (when (pair? (cadr lam)) |
4804 | 4815 | (error (string "`global const` declaration not allowed inside function" (format-loc current-loc))))) |
4805 | 4816 | (let ((lhs (cadr e))) |
4806 | | - (if (and (symbol? lhs) (underscore-symbol? lhs)) |
4807 | | - (compile (caddr e) break-labels value tail) |
4808 | | - (let* ((rhs (compile (caddr e) break-labels #t #f)) |
4809 | | - (lhs (if (and arg-map (symbol? lhs)) |
4810 | | - (get arg-map lhs lhs) |
4811 | | - lhs))) |
4812 | | - (if (and value rhs) |
4813 | | - (let ((rr (if (or (atom? rhs) (ssavalue? rhs) (eq? (car rhs) 'null)) |
4814 | | - rhs (make-ssavalue)))) |
4815 | | - (if (not (eq? rr rhs)) |
4816 | | - (emit `(= ,rr ,rhs))) |
4817 | | - (emit-assignment-or-setglobal lhs rr (car e)) |
4818 | | - (if tail (emit-return tail rr)) |
4819 | | - rr) |
4820 | | - (emit-assignment lhs rhs (car e))))))) |
| 4817 | + (cond ((and (symbol? lhs) (underscore-symbol? lhs)) |
| 4818 | + (compile (caddr e) break-labels value tail)) |
| 4819 | + ((and (eq? (car e) 'const) (null? (cddr e)) (globalref? (cadr e))) |
| 4820 | + ;; No RHS - make undefined constant |
| 4821 | + (let ((lhs (cadr e))) |
| 4822 | + (emit `(call (core declare_const) ,(cadr lhs) (inert ,(caddr lhs)))))) |
| 4823 | + (else |
| 4824 | + (let* ((rhs (compile (caddr e) break-labels #t #f)) |
| 4825 | + (lhs (if (and arg-map (symbol? lhs)) |
| 4826 | + (get arg-map lhs lhs) |
| 4827 | + lhs))) |
| 4828 | + (if (and value rhs) |
| 4829 | + (let ((rr (if (or (atom? rhs) (ssavalue? rhs) (eq? (car rhs) 'null)) |
| 4830 | + rhs (make-ssavalue)))) |
| 4831 | + (if (not (eq? rr rhs)) |
| 4832 | + (emit `(= ,rr ,rhs))) |
| 4833 | + (emit-assignment-or-setglobal lhs rr (car e)) |
| 4834 | + (if tail (emit-return tail rr)) |
| 4835 | + rr) |
| 4836 | + (emit-assignment lhs rhs (car e)))))))) |
4821 | 4837 | ((block) |
4822 | 4838 | (let* ((last-fname filename) |
4823 | 4839 | (fnm (first-non-meta e)) |
|
0 commit comments