aboutsummaryrefslogtreecommitdiff
path: root/src/main/antlr4
diff options
context:
space:
mode:
authorazidar2015-10-06 14:28:24 -0700
committerazidar2015-10-06 14:28:24 -0700
commit2485d20374166b27c06c475a4aef365761a818f7 (patch)
tree627b3c180ba41d7619b1acc8be03e2195dd208aa /src/main/antlr4
parent62e922b0e7ea5f90c14a918ab09ce04a28f082d4 (diff)
parent0a9dfbe9f58338fc8af11015f6e9227e0cb46ea4 (diff)
Merge branch 'master' of github.com:ucb-bar/firrtl
Conflicts: README.md
Diffstat (limited to 'src/main/antlr4')
-rw-r--r--src/main/antlr4/FIRRTL.g4207
1 files changed, 207 insertions, 0 deletions
diff --git a/src/main/antlr4/FIRRTL.g4 b/src/main/antlr4/FIRRTL.g4
new file mode 100644
index 00000000..08697bb1
--- /dev/null
+++ b/src/main/antlr4/FIRRTL.g4
@@ -0,0 +1,207 @@
+// Jack Koenig
+// UC Berkeley ASPIRE Lab
+// July 9, 2015
+
+grammar FIRRTL;
+
+/*------------------------------------------------------------------
+ * PARSER RULES
+ *------------------------------------------------------------------*/
+
+// TODO add [info] support (all over the place)
+// TODO Fix connect
+// TODO Add partial connect
+// TODO Should FIRRTL keywords be legal IDs?
+
+// Does there have to be at least one module?
+circuit
+ : 'circuit' id ':' '{' module* '}'
+ ;
+
+// TODO Add support for extmodule
+module
+ : 'module' id ':' '{' port* blockStmt '}'
+ ;
+
+port
+ : portKind id ':' type
+ ;
+
+portKind
+ : 'input'
+ | 'output'
+ ;
+
+type
+ : 'UInt' '<' width '>'
+ | 'SInt' '<' width '>'
+ | 'Clock'
+ | '{' field* '}' // Bundle
+ | type '[' IntLit ']' // Vector
+ ;
+
+field
+ : orientation id ':' type
+ ;
+
+// FIXME This is what the spec says it should be
+//orientation
+// : 'default'
+// | 'reverse'
+// ;
+orientation
+ : 'flip'
+ | // Nothing
+ ;
+
+width
+ : IntLit
+ | '?'
+ ;
+
+// Much faster than replacing blockStmt with stmt+
+blockStmt
+ : (stmt)*
+ ;
+
+stmt
+ : 'wire' id ':' type
+ | 'reg' id ':' type exp exp
+ | 'smem' id ':' type exp
+ | 'cmem' id ':' type exp
+ | 'inst' id (':' | 'of') id // FIXME which should it be? ':' or 'of'
+ | 'node' id '=' exp
+ | 'poison' id ':' type // Poison, FIXME
+ | dir 'accessor' id '=' exp '[' exp ']' exp? // FIXME what is this extra exp?
+ | exp ':=' exp // Connect
+ | 'onreset' exp ':=' exp
+ | exp '<>' exp // Bulk Connect
+ | exp '[' IntLit 'through' IntLit ']' ':=' exp // SubWordConnect
+ | 'when' exp ':' '{' blockStmt '}' ( 'else' ':' '{' blockStmt '}' )?
+ | 'assert' exp
+ | 'skip'
+ ;
+
+// Accessor Direction
+dir
+ : 'infer'
+ | 'read'
+ | 'write'
+ | 'rdwr'
+ ;
+
+// TODO implement
+// What is exp?
+exp
+ : 'UInt' '<' width '>' '(' (IntLit) ')' // FIXME what does "ints" mean?
+ | 'SInt' '<' width '>' '(' (IntLit) ')' // FIXME same
+ | id // Ref
+ | exp '.' id // FIXME Does this work for no space?
+ | exp '[' IntLit ']'
+ | primop '(' exp* IntLit* ')' // FIXME Need a big check here
+ ;
+
+id
+ : Id
+ | keyword
+ ;
+
+// FIXME need to make sure this is exhaustive including all FIRRTL keywords that are legal IDs
+keyword
+ : primop
+ | dir
+ | 'inst'
+ ;
+
+primop
+ : 'add'
+ | 'sub'
+ | 'addw'
+ | 'subw'
+ | 'mul'
+ | 'div'
+ | 'mod'
+ | 'quo'
+ | 'rem'
+ | 'lt'
+ | 'leq'
+ | 'gt'
+ | 'geq'
+ | 'eq'
+ | 'neq'
+ | 'mux'
+ | 'pad'
+ | 'asUInt'
+ | 'asSInt'
+ | 'shl'
+ | 'shr'
+ | 'dshl'
+ | 'dshr'
+ | 'cvt'
+ | 'neg'
+ | 'not'
+ | 'and'
+ | 'or'
+ | 'xor'
+ | 'andr'
+ | 'orr'
+ | 'xorr'
+ | 'cat'
+ | 'bit'
+ | 'bits'
+ ;
+
+/*------------------------------------------------------------------
+ * LEXER RULES
+ *------------------------------------------------------------------*/
+
+Id
+ : IdNondigit
+ ( IdNondigit
+ | Digit
+ )*
+ ;
+
+fragment
+IdNondigit
+ : Nondigit
+ | [~!@#$%^*-+=?/]
+ ;
+
+// Should enforcing signed, non-neg, and positive ints be done in parser?
+// => YES
+IntLit
+ : '0'
+ | ( '+' | '-' )? [1-9] ( Digit )*
+ | '"' 'h' ( HexDigit )+ '"'
+ ;
+
+fragment
+Nondigit
+ : [a-zA-Z_]
+ ;
+
+fragment
+Digit
+ : [0-9]
+ ;
+
+fragment
+HexDigit
+ : [a-zA-Z0-9]
+ ;
+
+Comment
+ : ';' ~[\r\n]*
+ -> skip
+ ;
+
+Whitespace
+ : [ \t,]+
+ -> skip
+ ;
+
+Newline
+ : ( '\r'? '\n' )+
+ -> skip
+ ;