aboutsummaryrefslogtreecommitdiff
path: root/src/main/stanza/firrtl-test-main.stanza
blob: 315033c734d3a06b0d01c7c6961d8fa524022f12 (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
#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("bigint.stanza")
#include("firrtl-lexer.stanza")
#include("firrtl-ir.stanza")
#include("ir-utils.stanza")
#include("ir-parser.stanza")
#include("passes.stanza")
#include("primop.stanza")
#include("errors.stanza")
#include("compilers.stanza")
#include("flo.stanza")
#include("verilog.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
   ;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 standard-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 = ""
   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 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 = args[i]
         if last-s == "-s" : add(pass-args,args[i])
         if last-s == "-m" : add(firms,args[i])

   if input == false : 
      error("No input file provided. Use -i flag.")
   if output == false : 
      error("No output file provided. Use -o flag.")
   if compiler == false and length(pass-names) == 0 :
      error("Must specify a compiler. Use -X 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 :
      run-passes(circuit*,get-passes(to-list(pass-names)))
   else :
      switch {_ == compiler} :
         "flo" : run-passes(circuit*,StandardFlo(output as String))
         "verilog" : run-passes(circuit*,StandardVerilog(output as String))
         "verilute" : run-passes(circuit*,InstrumentedVerilog(output as String,to-list $ pass-args))
         else : error("Invalid compiler flag")

main()