idlc 0.1.1 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/idlc/cli.rb CHANGED
@@ -12,6 +12,15 @@ require "optparse"
12
12
  require "yaml"
13
13
 
14
14
  module Idl
15
+ # Minimal register-file descriptor used by the CLI when no architecture is configured.
16
+ # Provides the interface that SymbolTable.new(register_files:) expects.
17
+ DefaultXRegisterFile = Struct.new(:name, :register_length, :registers) do
18
+ def self.build(xlen = 64)
19
+ registers = Array.new(32) { |i| Struct.new(:name).new("x#{i}") }
20
+ new("X", "return #{xlen};", registers)
21
+ end
22
+ end
23
+
15
24
  class Cli
16
25
  extend Forwardable
17
26
  def_delegators :@runner,
@@ -48,7 +57,7 @@ module Idl
48
57
  end
49
58
 
50
59
  compiler = Compiler.new
51
- symtab = SymbolTable.new
60
+ symtab = SymbolTable.new(register_files: [DefaultXRegisterFile.build])
52
61
 
53
62
  add_defines(compiler, symtab)
54
63
  expr_ast = compiler.compile_expression(args[0], symtab)
@@ -104,7 +113,7 @@ module Idl
104
113
 
105
114
  def do_tc_inst(args, options, vars)
106
115
  compiler = Compiler.new
107
- symtab = SymbolTable.new
116
+ symtab = SymbolTable.new(register_files: [DefaultXRegisterFile.build])
108
117
 
109
118
  add_defines(compiler, symtab)
110
119
  symtab.push(nil)
data/lib/idlc/idl.treetop CHANGED
@@ -66,32 +66,36 @@ grammar Idl
66
66
  end
67
67
 
68
68
  rule int
69
- # verilog style: explicit bit width
70
- (([0-9]+)/'MXLEN')? "'" 'b' [0-1xX] [0-1_xX]* <Idl::IntLiteralSyntaxNode> /
71
- (([0-9]+)/'MXLEN')? "'" 'o' [0-7xX] [0-7_xX]* <Idl::IntLiteralSyntaxNode> /
72
- (([0-9]+)/'MXLEN')? "'" 'd'? [0-9] [0-9_]* <Idl::IntLiteralSyntaxNode> /
73
- (([0-9]+)/'MXLEN')? "'" 'h' [0-9a-fA-FxX] [0-9a-fA-F_xX]* <Idl::IntLiteralSyntaxNode> /
69
+ # plain decimal (most common) — tried first; disjoint from all other alternatives
70
+ # (other alternatives start with '0', "'", or require a trailing 's' suffix)
71
+ # Negative lookahead !"'" ensures we don't consume the width prefix of a Verilog literal (e.g. 32'b...)
72
+ [1-9] [0-9]* !"'" 's' <Idl::IntLiteralSyntaxNode> /
73
+ [1-9] [0-9]* !"'" <Idl::IntLiteralSyntaxNode> /
74
74
 
75
- # verilog style: explicit bit width, signed
76
- (([0-9]+)/'MXLEN')? "'" 'sb' [0-1xX] [0-1_xX]* <Idl::IntLiteralSyntaxNode> /
77
- (([0-9]+)/'MXLEN')? "'" 'so' [0-7xX] [0-7_xX]* <Idl::IntLiteralSyntaxNode> /
78
- (([0-9]+)/'MXLEN')? "'" 's' 'd'? [0-9] [0-9_]* <Idl::IntLiteralSyntaxNode> /
79
- (([0-9]+)/'MXLEN')? "'" 'sh' [0-9a-fA-FxX] [0-9a-fA-F_xX]* <Idl::IntLiteralSyntaxNode> /
80
-
81
- # c++ style: signed
75
+ # c++ style: signed (must come before unsigned to avoid consuming the trailing 's')
82
76
  '0b' [0-1] [0-1_]* 's' <Idl::IntLiteralSyntaxNode> /
83
77
  '0' [0-7] [0-7_]* 's' <Idl::IntLiteralSyntaxNode> /
84
- [1-9] [0-9]* 's' <Idl::IntLiteralSyntaxNode> /
85
78
  '0x' [0-9a-fA-F] [0-9a-fA-F_]* 's' <Idl::IntLiteralSyntaxNode> /
86
79
 
87
80
  # c++ style: unsigned
88
81
  '0b' [0-1] [0-1_]* <Idl::IntLiteralSyntaxNode> /
89
82
  '0' [0-7] [0-7_]* <Idl::IntLiteralSyntaxNode> /
90
- [1-9] [0-9]* <Idl::IntLiteralSyntaxNode> /
91
83
  '0x' [0-9a-fA-F] [0-9a-fA-F_]* <Idl::IntLiteralSyntaxNode> /
92
84
 
93
- # special case: just a single 0
94
- '0' 's'? <Idl::IntLiteralSyntaxNode>
85
+ # special case: just a single 0 (not followed by a verilog-style quote)
86
+ '0' !"'" 's'? <Idl::IntLiteralSyntaxNode> /
87
+
88
+ # verilog style: explicit bit width
89
+ (([0-9]+)/'MXLEN')? "'" 'b' [0-1xX] [0-1_xX]* <Idl::IntLiteralSyntaxNode> /
90
+ (([0-9]+)/'MXLEN')? "'" 'o' [0-7xX] [0-7_xX]* <Idl::IntLiteralSyntaxNode> /
91
+ (([0-9]+)/'MXLEN')? "'" 'd'? [0-9] [0-9_]* <Idl::IntLiteralSyntaxNode> /
92
+ (([0-9]+)/'MXLEN')? "'" 'h' [0-9a-fA-FxX] [0-9a-fA-F_xX]* <Idl::IntLiteralSyntaxNode> /
93
+
94
+ # verilog style: explicit bit width, signed
95
+ (([0-9]+)/'MXLEN')? "'" 'sb' [0-1xX] [0-1_xX]* <Idl::IntLiteralSyntaxNode> /
96
+ (([0-9]+)/'MXLEN')? "'" 'so' [0-7xX] [0-7_xX]* <Idl::IntLiteralSyntaxNode> /
97
+ (([0-9]+)/'MXLEN')? "'" 's' 'd'? [0-9] [0-9_]* <Idl::IntLiteralSyntaxNode> /
98
+ (([0-9]+)/'MXLEN')? "'" 'sh' [0-9a-fA-FxX] [0-9a-fA-F_xX]* <Idl::IntLiteralSyntaxNode>
95
99
  end
96
100
 
97
101
  rule p0_binary_operator
@@ -299,7 +303,7 @@ grammar Idl
299
303
 
300
304
  # expression that can accept an array index operator (expr[])
301
305
  rule ary_eligible_expression
302
- enum_to_a
306
+ dollar_function_call
303
307
  /
304
308
  paren_expression
305
309
  /
@@ -313,7 +317,7 @@ grammar Idl
313
317
  /
314
318
  csr_field_access_expression
315
319
  /
316
- bits_cast
320
+ csr_register_access_expression
317
321
  /
318
322
  rval # must come last!
319
323
  end
@@ -330,20 +334,16 @@ grammar Idl
330
334
  rval space* '++' <Idl::PostIncrementExpressionSyntaxNode>
331
335
  end
332
336
 
333
- rule bits_cast
334
- '$bits' space* '(' space*
335
- expr:(
336
- csr_register_access_expression
337
- /
338
- enum_ref
339
- /
340
- expression
341
- )
342
- space* ')' <Idl::BitsCastSyntaxNode>
337
+ rule dollar_function_call
338
+ '$' name:([a-zA-Z_] [a-zA-Z0-9_?]*) space* '(' space* args:dollar_arg_list space* ')' <Idl::DollarFunctionCallSyntaxNode>
339
+ end
340
+
341
+ rule dollar_variable
342
+ '$' name:([a-zA-Z_] [a-zA-Z0-9_]*) <Idl::DollarVariableSyntaxNode>
343
343
  end
344
344
 
345
- rule enum_to_a
346
- '$enum_to_a' space* '(' space* type_name ')' <Idl::EnumArrayCastSyntaxNode>
345
+ rule dollar_arg_list
346
+ first:expression? rest:(space* ',' space* expression)*
347
347
  end
348
348
 
349
349
  rule unary_expression
@@ -355,24 +355,7 @@ grammar Idl
355
355
  /
356
356
  ary_access
357
357
  /
358
- # get the width (N) of a Bits type. Mostly just useful for writing tests and checking types
359
- '$width' space* '(' space* expression space* ')' <Idl::WidthRevealSyntaxNode>
360
- /
361
- '$signed' space* '(' space* expression space* ')' <Idl::SignCastSyntaxNode>
362
- /
363
- bits_cast
364
- /
365
- '$enum_size' space* '(' space* type_name ')' <Idl::EnumSizeSyntaxNode>
366
- /
367
- '$enum_element_size' space* '(' space* type_name ')' <Idl::EnumElementSizeSyntaxNode>
368
- /
369
- enum_to_a
370
- /
371
- '$enum' space* '(' space* type_name space* ',' space* expression space* ')' <Idl::EnumCastSyntaxNode>
372
- /
373
- '$array_size' space* '(' space* expression ')' <Idl::ArraySizeSyntaxNode>
374
- /
375
- '$array_includes?' space* '(' space* ary:ary_eligible_expression space* ',' space* value:expression space* ')' <Idl::ArrayIncludesSyntaxNode>
358
+ dollar_function_call
376
359
  /
377
360
  paren_expression
378
361
  /
@@ -392,6 +375,8 @@ grammar Idl
392
375
  /
393
376
  csr_field_access_expression
394
377
  /
378
+ csr_register_access_expression
379
+ /
395
380
  enum_ref
396
381
  /
397
382
  rval # must come last
@@ -443,11 +428,12 @@ grammar Idl
443
428
  end
444
429
 
445
430
  rule function_call
431
+ # plain function call is most common; CSR calls require literal "CSR[" prefix so these are disjoint
432
+ function_name space* '(' space* function_arg_list space* ')' <Idl::FunctionCallExpressionSyntaxNode>
433
+ /
446
434
  csr:csr_register_access_expression space* '.' space* 'sw_write' space* '(' space* expression space* ')' <Idl::CsrSoftwareWriteSyntaxNode>
447
435
  /
448
436
  csr:csr_register_access_expression space* '.' space* function_name space* '(' space* function_arg_list space* ')' <Idl::CsrFunctionCallSyntaxNode>
449
- /
450
- function_name space* '(' space* function_arg_list space* ')' <Idl::FunctionCallExpressionSyntaxNode>
451
437
  end
452
438
 
453
439
  rule function_name
@@ -505,19 +491,18 @@ grammar Idl
505
491
  end
506
492
 
507
493
  rule rval
508
- (int / builtin_read_only_var / builtin_read_write_var / string / id)
494
+ (int / dollar_variable / string / id)
509
495
  end
510
496
 
511
497
  rule assignment
512
498
  '(' first:(id / dontcare_lvalue) space* rest:(',' space* var:(id / dontcare_lvalue) space*)+ ')' space* '=' space* function_call <Idl::MultiVariableAssignmentSyntaxNode> /
513
499
  single_declaration_with_initialization /
514
- '$pc' space* '=' space* rval:expression <Idl::PcAssignmentSyntaxNode> /
500
+ dollar_variable space* '=' space* rval:expression <Idl::DollarVariableAssignmentSyntaxNode> /
515
501
  var:id space* '=' space* rval:expression <Idl::VariableAssignmentSyntaxNode> /
516
502
  # note: there is no full CSR assignment (must assign fields)
517
503
  csr_field_access_expression space* '=' space* rval:expression <Idl::CsrFieldAssignmentSyntaxNode> /
518
504
  id space* '.' space* field_name space* '=' space* rval:expression <Idl::FieldAssignmentSyntaxNode> /
519
- var:ary_eligible_expression space* '[' space* msb:expression space* ':' space* lsb:expression space* ']' space* '=' space* rval:expression <Idl::AryRangeAssignmentSyntaxNode> /
520
- var:ary_eligible_expression space* '[' space* idx:expression space* ']' space* '=' space* rval:expression <Idl::AryElementAssignmentSyntaxNode>
505
+ var:ary_eligible_expression space* brackets:('[' space* msb:(expression space* ':' space*)? lsb:expression space* ']' space*)+ '=' space* rval:expression <Idl::AryRangeAssignmentSyntaxNode>
521
506
  end
522
507
 
523
508
  rule ary_size_decl
@@ -639,14 +624,6 @@ grammar Idl
639
624
  [A-Za-z] [A-Za-z_0-9]* <Idl::IdSyntaxNode>
640
625
  end
641
626
 
642
- rule builtin_read_only_var
643
- "$encoding" <Idl::BuiltinVariableSyntaxNode>
644
- end
645
-
646
- rule builtin_read_write_var
647
- "$pc" <Idl::BuiltinVariableSyntaxNode>
648
- end
649
-
650
627
  rule csr_name
651
628
  [a-z] [a-z0-9_.]*
652
629
  end