optimize.scm 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. ;;; Test script to show high-level optimization of Scheme expressions
  2. ;;; Copyright (C) 2023, 2024 Igalia, S.L.
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. (use-modules (language tree-il)
  16. (hoot compile)
  17. (system base compile)
  18. (system base language)
  19. (ice-9 match)
  20. (ice-9 pretty-print)
  21. (ice-9 textual-ports))
  22. (define (read1 str)
  23. (call-with-input-string
  24. str
  25. (lambda (port)
  26. (let ((expr (read port)))
  27. (when (eof-object? expr)
  28. (error "No expression to evaluate"))
  29. (let ((tail (read port)))
  30. (unless (eof-object? tail)
  31. (error "Unexpected trailing expression" tail)))
  32. expr))))
  33. (define* (optimize expr #:key (optimization-level (default-optimization-level))
  34. (opts '()) import-abi?)
  35. (define lower-tree-il
  36. ((language-lowerer (lookup-language 'tree-il)) optimization-level opts))
  37. (let* ((env #f)
  38. (tree-il (scheme->sealed-tree-il expr #:import-abi? import-abi?))
  39. (optimized (lower-tree-il tree-il env)))
  40. (pretty-print (tree-il->scheme optimized env))))
  41. (when (batch-mode?)
  42. (match (program-arguments)
  43. ((arg0 . args)
  44. (let lp ((args args) (import-abi? #f))
  45. (match args
  46. (("--import-abi" . args)
  47. (lp args #t))
  48. ((expr)
  49. (optimize (read1 expr) #:import-abi? import-abi?))
  50. (_
  51. (format (current-error-port) "usage: ~a [--import-abi] EXPR\n" arg0)
  52. (exit 1)))))))