Class: Idl::Cli

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/idlc/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Cli

Returns a new instance of Cli.



36
37
38
39
# File 'lib/idlc/cli.rb', line 36

def initialize(args)
  @defines = {}
  @runner = Commander::Runner.new(args)
end

Instance Method Details

#add_defines(compiler, symtab) ⇒ Object



41
42
43
44
45
46
# File 'lib/idlc/cli.rb', line 41

def add_defines(compiler, symtab)
  @defines.each do |name, value_str|
    expr_ast = compiler.compile_expression(value_str, symtab)
    symtab.add!(name, Var.new(name, expr_ast.type(symtab), expr_ast.value(symtab)))
  end
end

#do_compile(args, options) ⇒ Object



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
# File 'lib/idlc/cli.rb', line 74

def do_compile(args, options)
  if args.size != 1
    if args.empty?
      warn "Missing file to compile"
    else
      warn "Unexpected arguments: #{args[1..]}"
    end
    @runner.commands["help"].run
    exit 1
  end

  compiler = Compiler.new

  io =
    if options.output == "-"
      $stdout
    else
      File.open(options.output, "w")
    end

  compiler.parser.set_input_file(args[0], 0)
  m = compiler.parser.parse(File.read(args[0]), root: options.root)
  if m.nil?
    raise SyntaxError, <<~MSG
      While parsing #{args[0]}:#{compiler.parser.failure_line}

      #{compiler.parser.failure_reason}
    MSG
  end

  ast = m.to_ast
  ast.set_input_file(args[0], 0)

  if options.format == "yaml"
    io.puts YAML.dump(ast.to_h)
  else
    raise "Unknown format: #{options.format}"
  end
end

#do_eval(args, options) ⇒ Object



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
# File 'lib/idlc/cli.rb', line 48

def do_eval(args, options)
  if args.size != 1
    if args.empty?
      warn "Missing expression to evaluate"
    else
      warn "Unexpected arguments: #{args[1..]}"
    end
    @runner.commands["help"].run
    exit 1
  end

  compiler = Compiler.new
  symtab = SymbolTable.new(register_files: [DefaultXRegisterFile.build])

  add_defines(compiler, symtab)
  expr_ast = compiler.compile_expression(args[0], symtab)

  case options.output
  when "-"
    $stdout.puts expr_ast.value(symtab)
  else
    f = File.open(options.output, "w")
    f.puts expr_ast.value(symtab)
  end
end

#do_tc_inst(args, options, vars) ⇒ Object



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
# File 'lib/idlc/cli.rb', line 114

def do_tc_inst(args, options, vars)
  compiler = Compiler.new
  symtab = SymbolTable.new(register_files: [DefaultXRegisterFile.build])

  add_defines(compiler, symtab)
  symtab.push(nil)

  vars.each do |name, width|
    symtab.add!(name, Var.new(name, Type.new(:bits, width: width.to_i), decode_var: true))
  end

  io =
    if args[0] == "-"
      $stdin
    else
      File.open(args[0], "r")
    end

  idl =
    if !options.key.nil?
      yaml_contents = YAML.safe_load(io.read, permitted_classes: [String, Array, Hash], permitted_symbols: [])
      raise "#{args[0]} has no key named '#{options.key}'" unless yaml_contents.key?(options.key)

      yaml_contents[options.key]
    else
      io.read
    end

  ast = compiler.compile_inst_scope(idl, symtab:, input_file: args[0])
  ast.type_check(symtab, strict: options.strict)
end

#runObject



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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/idlc/cli.rb', line 146

def run
  default_command :help

  program :name, "IDL Compiler"
  program :version, Idl::Compiler.version
  program :description, "Command line for the IDL reference compiler"

  add_define_option = lambda do |c|
    c.option "-D,--define PARM_NAME=PARAM_VALUE", (<<~DESC
      Define a parameter (e.g., -DMXLEN=64).
      PARAM_VALUE can be any IDL expression with a knowable value
    DESC
    ) do |varandval|
      raise ArgumentError, "Define (#{varandval}) must be in the format VAR=VAL" unless varandval =~ /.+=.+/

      var, val = varandval.split("=")
      @defines[var] = val
    end
  end

  command :compile do |c|
    c.syntax = "idlc compile [options] PATH"
    c.summary = "Compile idl, and return the result"
    c.example "Convert to AST", "idlc compiler -f yaml PATH"
    c.example "Convert operation() to AST", "idlc compiler -f yaml -r instruction_operation PATH"

    c.option "--format FORMAT", String, "Output format"
    c.option "--root ROOT_RULE", String, "Root rule to begin parsing"
    c.option "--output OUTPUT", String, "Output file (- for STDOUT)"

    c.action do |args, options|
      options.default format: "yaml"
      options.default root: "isa"
      options.default output: "-"
      do_compile(args, options)
    end
  end

  command :eval do |c|
    c.syntax = "idlc eval [options] EXPRESSION"
    c.summary = "Evaluate an IDL expression"
    c.example "Print '15'", "idlc eval -DA=5 -DB=10 A+B"

    c.option "-o,--output FILE", String, "Output file (- for STDOUT)"
    add_define_option.call(c)

    c.action do |args, options|
      options.default output: "-"
      do_eval(args, options)
    end
  end

  command "tc inst" do |c|
    vars = {}
    c.syntax = "idlc tc inst [options] FILE"
    c.summary = "Type check an instruction 'operation()' block. Exits 0 if type checking succeeds, 1 otherwise."
    c.example "Exit 0", "idlc tc inst -k 'operation()' -v xs1=5 -v xs2=5 -v xd=5 add.yaml"
    c.example "Exit 1 (variables not defined)", "idlc tc inst -k 'operation()' add.yaml"
    c.example "Exit 0", "echo 'X[2] = 15;' | idlc tc inst -"

    add_define_option.call(c)
    c.option "-k", "--key KEY", String, "When FILE is a YAML file, type check just the contents of KEY"
    c.option "-s", "--strict", "Run strict type checking (only consider reachable code, and fail if using a variable/field is not known to be defined)"
    c.option "-d", "--var NAME=WIDTH", (<<~DESC
      Define decode variable, e.g., xs2=5
      NAME is the name of the variable, and must be a valid IDL identifier
      WIDTH is the bit width of the variable, and must be an integer
    DESC
    ) do |nameandwidth|
      unless nameandwidth =~ /.+=.+/
        raise ArgumentError, "Define (#{nameandwidth}) must be in the format NAME=WIDTH"
      end

      name, width = nameandwidth.split("=")
      vars[name] = width.to_i
    end

    c.action do |args, options|
      if args.size != 1
        if args.empty?
          warn "Missing file to type check"
        else
          warn "Unexpected arguments: #{args[1..]}"
        end
        @runner.commands["help"].run
        exit 1
      end

      do_tc_inst(args, options, vars)
    end
  end

  run!
end