aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/options/Stage.scala
blob: cefdd957932459b70ba48eb8039ff8d226c4e108 (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
// SPDX-License-Identifier: Apache-2.0

package firrtl.options

import firrtl.AnnotationSeq

import logger.Logger

/** A [[Stage]] represents one stage in the FIRRTL hardware compiler framework. A [[Stage]] is, conceptually, a
  * [[Phase]] that includes a command line interface.
  *
  * The FIRRTL compiler is a stage as well as any frontend or backend that runs before/after FIRRTL. Concretely, Chisel
  * is a [[Stage]] as is FIRRTL's Verilog emitter. Each stage performs a mathematical transformation on an
  * [[AnnotationSeq]] where some input annotations are processed to produce different annotations. Command line options
  * may be pulled in if available.
  */
abstract class Stage extends Phase {

  /** A utility that helps convert command line options to annotations */
  val shell: Shell

  /** Run this stage on some input annotations
    * @param annotations input annotations
    * @return output annotations
    */
  protected def run(annotations: AnnotationSeq): AnnotationSeq

  /** Execute this stage on some input annotations. Annotations will be read from any input annotation files.
    * @param annotations input annotations
    * @return output annotations
    * @throws firrtl.options.OptionsException if command line or annotation validation fails
    */
  final def transform(annotations: AnnotationSeq): AnnotationSeq = {
    val annotationsx =
      Seq(new phases.GetIncludes)
        .map(phases.DeletedWrapper(_))
        .foldLeft(annotations)((a, p) => p.transform(a))

    Logger.makeScope(annotationsx) {
      Seq(
        new phases.AddDefaults,
        new phases.Checks,
        new Phase { def transform(a: AnnotationSeq) = run(a) },
        new phases.WriteOutputAnnotations
      )
        .map(phases.DeletedWrapper(_))
        .foldLeft(annotationsx)((a, p) => p.transform(a))
    }
  }

  /** Run this stage on on a mix of arguments and annotations
    * @param args command line arguments
    * @param initialAnnotations annotation
    * @return output annotations
    * @throws firrtl.options.OptionsException if command line or annotation validation fails
    */
  final def execute(args: Array[String], annotations: AnnotationSeq): AnnotationSeq =
    transform(shell.parse(args, annotations))

}

/** Provides a main method for a [[Stage]]
  * @param stage the stage to run
  */
class StageMain(val stage: Stage) {

  /** The main function that serves as this stage's command line interface.
    * @param args command line arguments
    */
  final def main(args: Array[String]): Unit = try {
    stage.execute(args, Seq.empty)
  } catch {
    case a: StageError =>
      System.exit(a.code.number)
    case a: OptionsException =>
      StageUtils.dramaticUsageError(a.message)
      System.exit(1)
  }

}