aboutsummaryrefslogtreecommitdiff
path: root/src/main/stanza/firrtl-test-main.stanza
blob: 4fcc9deeb87ae3cf6f70cdb783285ca132817170 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
;Copyright (c) 2014 - 2016 The Regents of the University of
;California (Regents). All Rights Reserved.  Redistribution and use in
;source and binary forms, with or without modification, are permitted
;provided that the following conditions are met:
;   * Redistributions of source code must retain the above
;     copyright notice, this list of conditions and the following
;     two paragraphs of disclaimer.
;   * Redistributions in binary form must reproduce the above
;     copyright notice, this list of conditions and the following
;     two paragraphs of disclaimer in the documentation and/or other materials
;     provided with the distribution.
;   * Neither the name of the Regents nor the names of its contributors
;     may be used to endorse or promote products derived from this
;     software without specific prior written permission.
;IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
;SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
;ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
;REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
;LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
;ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION
;TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
;MODIFICATIONS.
#include<"core/stringeater.stanza">
#include<"core/macro-utils.stanza">
#include<"compiler/stz-algorithms.stanza">
#include<"compiler/stz-parser.stanza">
#include<"compiler/stz-lexer.stanza">
#include("bigint2.stanza")
#include("firrtl-lexer.stanza")
#include("firrtl-ir.stanza")
#include("ir-utils.stanza")
#include("ir-parser.stanza")
#include("passes.stanza")
#include("chirrtl.stanza")
#include("primop.stanza")
#include("errors.stanza")
#include("compilers.stanza")
#include("firrtl.stanza")
;#include("flo.stanza")
;#include("verilog.stanza")
;#include("symbolic-value.stanza")

;Custom Packages
;#include("custom-passes.stanza")
;#include("custom-compiler.stanza")

defpackage firrtl-main :
   import core
   import verse
   import firrtl/parser
   import firrtl/passes
   import firrtl/ir2
   import firrtl/lexer
   import stz/parser
   import firrtl/ir-utils
   import firrtl/compiler
   import firrtl/chirrtl
   import firrtl/firrtl
   ;Custom Packages
   ;import firrtl/custom-passes
   ;import firrtl/custom-compiler

defn set-printvars! (p:List<Char>) :
   if contains(p,'t') : PRINT-TYPES = true
   if contains(p,'k') : PRINT-KINDS = true
   if contains(p,'w') : PRINT-WIDTHS = true
   if contains(p,'T') : PRINT-TWIDTHS = true
   if contains(p,'g') : PRINT-GENDERS = true
   if contains(p,'c') : PRINT-CIRCUITS = true
   if contains(p,'d') : PRINT-DEBUG = true
   if contains(p,'i') : PRINT-INFO = true

defn get-passes (pass-names:List<String>) -> List<Pass> :
   for n in pass-names map :
      val p = for p in append(standard-passes,chirrtl-passes) find :
         n == short-name(p)
      if p == false :
         error(to-string $ ["Unrecognized pass flag: " n])
      p as Pass
         
defn main () :
   val args = commandline-arguments()
   var input = false
   var output = false
   var firms = Vector<String>()
   var compiler = false
   val pass-names = Vector<String>()
   val pass-args = Vector<String>()
   var printvars = ""
   var last-s = ""
   var backend = false

   val prev-out = CURRENT-OUTPUT-STREAM
   CURRENT-OUTPUT-STREAM = STANDARD-ERROR


   for (s in args, i in 0 to false) do :
      if s == "-i" : last-s = s
      else if s == "-o" : last-s = s
      else if s == "-x" : last-s = s
      else if s == "-X" : last-s = s
      else if s == "-p" : last-s = s
      else if s == "-s" : last-s = s
      else if s == "-m" : last-s = s
      else if s == "-b" : last-s = s
      else :
         if last-s == "-i" : input = args[i]
         if last-s == "-o" : output = args[i]
         if last-s == "-x" : add(pass-names,args[i])
         if last-s == "-X" : compiler = args[i]
         if last-s == "-p" : printvars = to-string([printvars args[i]])
         if last-s == "-s" : add(pass-args,args[i])
         if last-s == "-m" : add(firms,args[i])
         if last-s == "-b" : backend = args[i]

   var with-output = 
      fn (f:()->False) : 
         val prev-stream = CURRENT-OUTPUT-STREAM
         CURRENT-OUTPUT-STREAM = STANDARD-OUTPUT
         f()
         CURRENT-OUTPUT-STREAM = prev-stream

   if input == false : 
      error("No input file provided. Use -i flag.")
   if output != false and output != "-": 
      with-output = 
         fn (f:()->False) : 
            val prev-stream = CURRENT-OUTPUT-STREAM
            val out-stream = FileOutputStream(output as String)
            CURRENT-OUTPUT-STREAM = out-stream
            f()
            CURRENT-OUTPUT-STREAM = prev-stream
            close(out-stream)

   if compiler == false and backend == false and length(pass-names) == 0 :
      error("Must specify a compiler or a backend. Use -X flag or -b flag.")

   val lexed = lex-file(input as String)
   val circuit = parse-firrtl(lexed)

   val modules* = Vector<Module>()
   for m in modules(circuit) do :
      add(modules*,m)

   val included-c = 
      for m in firms map :
         val lexed = lex-file(m as String)
         parse-firrtl(lexed)

   for c in included-c do :
      for m in modules(c) do :
         add(modules*,m)

   val circuit* = Circuit(info(circuit),to-list(modules*),main(circuit))

   set-printvars!(to-list(printvars))
   
   if compiler == false :
      var c*:Circuit = run-passes(circuit*,get-passes(to-list(pass-names)))
      switch {_ == backend} :
        "verilog" : run-backend(c*,LoToVerilog(with-output))
        "firrtl" : run-backend(c*,FIRRTL(with-output))
        else : error("Invalid backend flag!")
   else :
      switch {_ == compiler} :
         ;"flo" : error("Flo backend not currently supported.")
            ; run-passes(circuit*,StandardFlo(with-output))
         "verilog" : run-passes(circuit*,StandardVerilog(with-output))
         "firrtl" : run-passes(circuit*,StandardFIRRTL(with-output))
         "lofirrtl" : run-passes(circuit*,StandardLoFIRRTL(with-output))
         ;"verilute" : run-passes(circuit*,InstrumentedVerilog(with-output,to-list $ pass-args))
         else : error("Invalid compiler flag")

   CURRENT-OUTPUT-STREAM = prev-out
main()