-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiger.rkt
executable file
·45 lines (35 loc) · 1.62 KB
/
tiger.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env racket
#lang racket/base
(require (for-syntax racket/base))
(require "driver.rkt" "ir-anf-printable-ast.rkt")
(require racket/cmdline racket/pretty)
(require (planet endobson/llvm/simple))
(define-values (source-path destination-path output-mode)
(let ((source-path #f) (destination-path 'default) (output-mode 'binary))
(command-line
#:once-each
("-o" output-file "Set the output file" (set! destination-path output-file))
#:once-any
("--binary" "outputs the executable" (set! output-mode 'binary))
("--llvm" "outputs llvm bitcode" (set! output-mode 'llvm))
("--llvm-opt" "outputs optimized llvm bitcode" (set! output-mode 'llvm-opt))
("--ir" "outputs ir representation" (set! output-mode 'ir))
("--lifted" "outputs lifted representation" (set! output-mode 'lifted))
#:args (source-file)
(set! source-path source-file)
(values source-path destination-path output-mode))))
(define-syntax (set-output-file stx)
(syntax-case stx ()
((_ exprs ...)
#'((lambda (thunk)
(if (equal? destination-path 'default) (thunk)
(with-output-to-file destination-path (thunk)))) (lambda () exprs ...)))))
(define program
(let ((mode (if (equal? 'binary output-mode) 'llvm-opt output-mode)))
(call-with-input-file source-path (lambda (port) (full-compile port mode)))))
(if (equal? output-mode 'binary) (void (compile-llvm program (if (equal? destination-path 'default) "a.out" destination-path)))
(set-output-file
(case output-mode
((llvm llvm-opt) (display (llvm-module-description program)))
((ir) (pretty-write (anf->printable program)))
((lifted) (pretty-write program)))))