aslutils package

Submodules

aslutils.ASLLexer module

class aslutils.ASLLexer.ASLLexer(input=None)

Bases: antlr4.Lexer.Lexer

And = 27
Array = 22
Assert = 17
Assign = 73
BitPattern = 87
BitType = 47
BitVector = 86
BitsType = 46
Bool = 81
BooleanType = 45
Case = 5
Catch = 41
Colon = 63
Comma = 78
Consistent = 18
Constant = 20
Constrained_Unpredictable = 35
Div = 25
Divide = 67
Do = 13
Dot = 79
Downto = 11
Else = 4
Elsif = 3
End = 50
Enumeration = 19
Eor = 30
Equal = 52
FixedPointNum = 85
For = 9
Greater = 54
GreaterEqual = 56
Hex = 84
Identifier = 82
If = 1
Implementation_Defined = 37
In = 31
Integer = 83
IntegerType = 44
Is = 21
Land = 59
LeftBrace = 76
LeftBracket = 64
LeftParen = 74
LeftShift = 70
Less = 55
LessEqual = 57
LineComment = 90
Lor = 58
Minus = 69
Mod = 26
Mult = 66
Negation = 60
Newline = 51
Not = 28
NotEqual = 53
Of = 6
Or = 29
Otherwise = 8
Plus = 68
Pow = 72
Quot = 23
Raise = 39
RangeDown = 62
RangeUp = 61
RealType = 48
Rem = 24
Repeat = 14
Return = 16
RightBrace = 77
RightBracket = 65
RightParen = 75
RightShift = 71
See = 36
Semicolon = 80
Start = 49
String = 88
Subarchitecture_Defined = 38
Then = 2
Throw = 42
To = 10
Try = 40
Typeof = 43
Undefined = 32
Unknown = 33
Unpredictable = 34
Until = 15
When = 7
While = 12
Ws = 89
atn = <antlr4.atn.ATN.ATN object>
decisionsToDFA = [<antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>]
grammarFileName = 'ASL.g4'
literalNames = ['<INVALID>', "'if'", "'then'", "'elsif'", "'else'", "'case'", "'of'", "'when'", "'otherwise'", "'for'", "'to'", "'downto'", "'while'", "'do'", "'repeat'", "'until'", "'return'", "'assert'", "'Consistent'", "'enumeration'", "'constant'", "'is'", "'array'", "'QUOT'", "'REM'", "'DIV'", "'MOD'", "'AND'", "'NOT'", "'OR'", "'EOR'", "'IN'", "'UNDEFINED'", "'UNKNOWN'", "'UNPREDICTABLE'", "'CONSTRAINED_UNPREDICTABLE'", "'SEE'", "'IMPLEMENTATION_DEFINED'", "'SUBARCHITECTURE_DEFINED'", "'RAISE'", "'try'", "'catch'", "'throw'", "'integer'", "'boolean'", "'bits'", "'bit'", "'real'", "'START'", "'END'", "'NEWLINE'", "'=='", "'!='", "'>'", "'<'", "'>='", "'<='", "'||'", "'&&'", "'!'", "'+:'", "'-:'", "':'", "'['", "']'", "'*'", "'/'", "'+'", "'-'", "'<<'", "'>>'", "'^'", "'='", "'('", "')'", "'{'", "'}'", "','", "'.'", "';'"]
modeNames = ['DEFAULT_MODE']
ruleNames = ['If', 'Then', 'Elsif', 'Else', 'Case', 'Of', 'When', 'Otherwise', 'For', 'To', 'Downto', 'While', 'Do', 'Repeat', 'Until', 'Return', 'Assert', 'Consistent', 'Enumeration', 'Constant', 'Is', 'Array', 'Quot', 'Rem', 'Div', 'Mod', 'And', 'Not', 'Or', 'Eor', 'In', 'Undefined', 'Unknown', 'Unpredictable', 'Constrained_Unpredictable', 'See', 'Implementation_Defined', 'Subarchitecture_Defined', 'Raise', 'Try', 'Catch', 'Throw', 'Typeof', 'IntegerType', 'BooleanType', 'BitsType', 'BitType', 'RealType', 'Start', 'End', 'Newline', 'Equal', 'NotEqual', 'Greater', 'Less', 'GreaterEqual', 'LessEqual', 'Lor', 'Land', 'Negation', 'RangeUp', 'RangeDown', 'Colon', 'LeftBracket', 'RightBracket', 'Mult', 'Divide', 'Plus', 'Minus', 'LeftShift', 'RightShift', 'Pow', 'Assign', 'LeftParen', 'RightParen', 'LeftBrace', 'RightBrace', 'Comma', 'Dot', 'Semicolon', 'Bool', 'Identifier', 'Integer', 'Hex', 'FixedPointNum', 'BitVector', 'BitPattern', 'String', 'Ws', 'LineComment']
symbolicNames = ['<INVALID>', 'If', 'Then', 'Elsif', 'Else', 'Case', 'Of', 'When', 'Otherwise', 'For', 'To', 'Downto', 'While', 'Do', 'Repeat', 'Until', 'Return', 'Assert', 'Consistent', 'Enumeration', 'Constant', 'Is', 'Array', 'Quot', 'Rem', 'Div', 'Mod', 'And', 'Not', 'Or', 'Eor', 'In', 'Undefined', 'Unknown', 'Unpredictable', 'Constrained_Unpredictable', 'See', 'Implementation_Defined', 'Subarchitecture_Defined', 'Raise', 'Try', 'Catch', 'Throw', 'Typeof', 'IntegerType', 'BooleanType', 'BitsType', 'BitType', 'RealType', 'Start', 'End', 'Newline', 'Equal', 'NotEqual', 'Greater', 'Less', 'GreaterEqual', 'LessEqual', 'Lor', 'Land', 'Negation', 'RangeUp', 'RangeDown', 'Colon', 'LeftBracket', 'RightBracket', 'Mult', 'Divide', 'Plus', 'Minus', 'LeftShift', 'RightShift', 'Pow', 'Assign', 'LeftParen', 'RightParen', 'LeftBrace', 'RightBrace', 'Comma', 'Dot', 'Semicolon', 'Bool', 'Identifier', 'Integer', 'Hex', 'FixedPointNum', 'BitVector', 'BitPattern', 'String', 'Ws', 'LineComment']
aslutils.ASLLexer.serializedATN()

aslutils.ASLParser module

class aslutils.ASLParser.ASLParser(input: antlr4.BufferedTokenStream.TokenStream)

Bases: antlr4.Parser.Parser

And = 27
Array = 22
Assert = 17
Assign = 73
class AssignableExprContext(parser, parent: antlr4.ParserRuleContext.ParserRuleContext = None, invokingState: int = -1)

Bases: antlr4.ParserRuleContext.ParserRuleContext

Colon()
Comma(i: int = None)
Dot()
Identifier()
LeftBrace()
LeftBracket()
LeftParen()
Minus()
RangeDown(i: int = None)
RightBrace()
RightBracket()
RightParen()
accept(visitor: antlr4.tree.Tree.ParseTreeVisitor)
assignableExpr(i: int = None)
expression(i: int = None)
getRuleIndex()
BitPattern = 87
BitType = 47
BitVector = 86
BitsType = 46
class BlockContext(parser, parent: antlr4.ParserRuleContext.ParserRuleContext = None, invokingState: int = -1)

Bases: antlr4.ParserRuleContext.ParserRuleContext

End()
Newline()
Start()
accept(visitor: antlr4.tree.Tree.ParseTreeVisitor)
getRuleIndex()
simpleStatement(i: int = None)
statement(i: int = None)
Bool = 81
BooleanType = 45
Case = 5
Catch = 41
Colon = 63
Comma = 78
Consistent = 18
Constant = 20
Constrained_Unpredictable = 35
Div = 25
Divide = 67
Do = 13
Dot = 79
Downto = 11
EOF = -1
Else = 4
Elsif = 3
End = 50
Enumeration = 19
Eor = 30
Equal = 52
class ExpressionContext(parser, parent: antlr4.ParserRuleContext.ParserRuleContext = None, invokingState: int = -1)

Bases: antlr4.ParserRuleContext.ParserRuleContext

And()
Colon()
Comma(i: int = None)
Div()
Divide()
Dot()
Else()
Eor()
Equal()
Greater()
GreaterEqual()
Identifier()
If()
In()
Land()
LeftBrace()
LeftBracket()
LeftParen()
LeftShift()
Less()
LessEqual()
Lor()
Minus()
Mod()
Mult()
Negation()
Not()
NotEqual()
Or()
Plus()
RangeDown(i: int = None)
RightBrace()
RightBracket()
RightParen()
RightShift()
Then()
Unknown()
accept(visitor: antlr4.tree.Tree.ParseTreeVisitor)
expression(i: int = None)
getRuleIndex()
literal()
typeName()
FixedPointNum = 85
For = 9
Greater = 54
GreaterEqual = 56
Hex = 84
Identifier = 82
If = 1
Implementation_Defined = 37
In = 31
Integer = 83
IntegerType = 44
Is = 21
Land = 59
LeftBrace = 76
LeftBracket = 64
LeftParen = 74
LeftShift = 70
Less = 55
LessEqual = 57
LineComment = 90
class LiteralContext(parser, parent: antlr4.ParserRuleContext.ParserRuleContext = None, invokingState: int = -1)

Bases: antlr4.ParserRuleContext.ParserRuleContext

BitPattern()
BitVector()
Bool()
FixedPointNum()
Hex()
Integer()
String()
accept(visitor: antlr4.tree.Tree.ParseTreeVisitor)
getRuleIndex()
Lor = 58
Minus = 69
Mod = 26
Mult = 66
Negation = 60
Newline = 51
Not = 28
NotEqual = 53
Of = 6
Or = 29
Otherwise = 8
Plus = 68
Pow = 72
Quot = 23
RULE_assignableExpr = 5
RULE_block = 3
RULE_expression = 4
RULE_literal = 6
RULE_simpleStatement = 2
RULE_start = 0
RULE_statement = 1
RULE_typeName = 7
Raise = 39
RangeDown = 62
RangeUp = 61
RealType = 48
Rem = 24
Repeat = 14
Return = 16
RightBrace = 77
RightBracket = 65
RightParen = 75
RightShift = 71
See = 36
Semicolon = 80
class SimpleStatementContext(parser, parent: antlr4.ParserRuleContext.ParserRuleContext = None, invokingState: int = -1)

Bases: antlr4.ParserRuleContext.ParserRuleContext

Assert()
Assign()
Comma(i: int = None)
Constant()
Enumeration()
Identifier(i: int = None)
Implementation_Defined()
LeftBrace()
LeftParen()
RightBrace()
RightParen()
See()
Semicolon()
String()
Undefined()
Unpredictable()
accept(visitor: antlr4.tree.Tree.ParseTreeVisitor)
assignableExpr(i: int = None)
expression(i: int = None)
getRuleIndex()
typeName()
Start = 49
class StartContext(parser, parent: antlr4.ParserRuleContext.ParserRuleContext = None, invokingState: int = -1)

Bases: antlr4.ParserRuleContext.ParserRuleContext

accept(visitor: antlr4.tree.Tree.ParseTreeVisitor)
getRuleIndex()
statement(i: int = None)
class StatementContext(parser, parent: antlr4.ParserRuleContext.ParserRuleContext = None, invokingState: int = -1)

Bases: antlr4.ParserRuleContext.ParserRuleContext

Assign()
Case()
Do()
Downto()
Else()
Elsif(i: int = None)
End()
For()
Identifier(i: int = None)
If()
Newline()
Of()
Otherwise()
Repeat()
Semicolon()
Start()
Then(i: int = None)
To()
Until()
When(i: int = None)
While()
accept(visitor: antlr4.tree.Tree.ParseTreeVisitor)
assignableExpr()
block(i: int = None)
expression(i: int = None)
getRuleIndex()
literal(i: int = None)
simpleStatement()
String = 88
Subarchitecture_Defined = 38
Then = 2
Throw = 42
To = 10
Try = 40
class TypeNameContext(parser, parent: antlr4.ParserRuleContext.ParserRuleContext = None, invokingState: int = -1)

Bases: antlr4.ParserRuleContext.ParserRuleContext

BitType()
BitsType()
BooleanType()
Identifier()
IntegerType()
LeftParen()
RealType()
RightParen()
accept(visitor: antlr4.tree.Tree.ParseTreeVisitor)
expression()
getRuleIndex()
Typeof = 43
Undefined = 32
Unknown = 33
Unpredictable = 34
Until = 15
When = 7
While = 12
Ws = 89
assignableExpr(_p: int = 0)
assignableExpr_sempred(localctx: aslutils.ASLParser.ASLParser.AssignableExprContext, predIndex: int)
atn = <antlr4.atn.ATN.ATN object>
block()
decisionsToDFA = [<antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>, <antlr4.dfa.DFA.DFA object>]
expression(_p: int = 0)
expression_sempred(localctx: aslutils.ASLParser.ASLParser.ExpressionContext, predIndex: int)
grammarFileName = 'ASL.g4'
literal()
literalNames = ['<INVALID>', "'if'", "'then'", "'elsif'", "'else'", "'case'", "'of'", "'when'", "'otherwise'", "'for'", "'to'", "'downto'", "'while'", "'do'", "'repeat'", "'until'", "'return'", "'assert'", "'Consistent'", "'enumeration'", "'constant'", "'is'", "'array'", "'QUOT'", "'REM'", "'DIV'", "'MOD'", "'AND'", "'NOT'", "'OR'", "'EOR'", "'IN'", "'UNDEFINED'", "'UNKNOWN'", "'UNPREDICTABLE'", "'CONSTRAINED_UNPREDICTABLE'", "'SEE'", "'IMPLEMENTATION_DEFINED'", "'SUBARCHITECTURE_DEFINED'", "'RAISE'", "'try'", "'catch'", "'throw'", '<INVALID>', "'integer'", "'boolean'", "'bits'", "'bit'", "'real'", "'START'", "'END'", "'NEWLINE'", "'=='", "'!='", "'>'", "'<'", "'>='", "'<='", "'||'", "'&&'", "'!'", "'+:'", "'-:'", "':'", "'['", "']'", "'*'", "'/'", "'+'", "'-'", "'<<'", "'>>'", "'^'", "'='", "'('", "')'", "'{'", "'}'", "','", "'.'", "';'"]
ruleNames = ['start', 'statement', 'simpleStatement', 'block', 'expression', 'assignableExpr', 'literal', 'typeName']
sempred(localctx: <module 'antlr4.RuleContext' from '/home/alex/Documents/MastersThesis/simulator-support/aslutils/env/lib/python3.6/site-packages/antlr4/RuleContext.py'>, ruleIndex: int, predIndex: int)
sharedContextCache = <antlr4.PredictionContext.PredictionContextCache object>
simpleStatement()
start()
statement()
symbolicNames = ['<INVALID>', 'If', 'Then', 'Elsif', 'Else', 'Case', 'Of', 'When', 'Otherwise', 'For', 'To', 'Downto', 'While', 'Do', 'Repeat', 'Until', 'Return', 'Assert', 'Consistent', 'Enumeration', 'Constant', 'Is', 'Array', 'Quot', 'Rem', 'Div', 'Mod', 'And', 'Not', 'Or', 'Eor', 'In', 'Undefined', 'Unknown', 'Unpredictable', 'Constrained_Unpredictable', 'See', 'Implementation_Defined', 'Subarchitecture_Defined', 'Raise', 'Try', 'Catch', 'Throw', 'Typeof', 'IntegerType', 'BooleanType', 'BitsType', 'BitType', 'RealType', 'Start', 'End', 'Newline', 'Equal', 'NotEqual', 'Greater', 'Less', 'GreaterEqual', 'LessEqual', 'Lor', 'Land', 'Negation', 'RangeUp', 'RangeDown', 'Colon', 'LeftBracket', 'RightBracket', 'Mult', 'Divide', 'Plus', 'Minus', 'LeftShift', 'RightShift', 'Pow', 'Assign', 'LeftParen', 'RightParen', 'LeftBrace', 'RightBrace', 'Comma', 'Dot', 'Semicolon', 'Bool', 'Identifier', 'Integer', 'Hex', 'FixedPointNum', 'BitVector', 'BitPattern', 'String', 'Ws', 'LineComment']
typeName()
aslutils.ASLParser.serializedATN()

aslutils.ASLVisitor module

class aslutils.ASLVisitor.ASLVisitor

Bases: antlr4.tree.Tree.ParseTreeVisitor

visitAssignableExpr(ctx: aslutils.ASLParser.ASLParser.AssignableExprContext)
visitBlock(ctx: aslutils.ASLParser.ASLParser.BlockContext)
visitExpression(ctx: aslutils.ASLParser.ASLParser.ExpressionContext)
visitLiteral(ctx: aslutils.ASLParser.ASLParser.LiteralContext)
visitSimpleStatement(ctx: aslutils.ASLParser.ASLParser.SimpleStatementContext)
visitStart(ctx: aslutils.ASLParser.ASLParser.StartContext)
visitStatement(ctx: aslutils.ASLParser.ASLParser.StatementContext)
visitTypeName(ctx: aslutils.ASLParser.ASLParser.TypeNameContext)

aslutils.asl2 module

aslutils.asl2.asl_to_lang(string, fields, LangVisitor)

Converts the given processed ASL string into a code snippet

For instance the following ASL code:

integer bits_d = 4;
integer bits_f = 5 * UInt(op) + 3;

Will be processed to:

integer bits_d = 4; NEWLINE integer bits_f = 5 * op + 3;

Assuming op is a 5 bit field declared before, the processed code along with: [(op, 5)] will be passed into asl_to_lang(). The result of the function call would be the string (in the case of a c visitor):

int64_t bits_f = 5 * (u_int64_t)op + 3;

And the variable table:

{'op': (True, ASLType(ASLType.Kind.bits, 5), None),
 'bits_d': (False, ASLType(ASLType.Kind.int), 4),
 'bits_f': (False, ASLType(ASLType.Kind.int), None)}

Note that the code for bits_d is not generated but this information is passed out via the variable table.

Parameters:
  • string (str) – ASL snippet string. The ASL code has to contain special tokens for structure instead of indentation and newlines.
  • fields ([(str, int)]) – A list of fields, each specified by a name and a length (in bits).
  • LangVisitor (class) – The visitor class to use on the ast
Returns:

A pair containing a variable map and the generated c code. The variable map maps from variable name to if it already existed before the snippet, its type (if known) and its value (if it’s a known constant).

Return type:

({str: (bool, ASLType or None, Any)}, [str])

aslutils.asl2c module

class aslutils.asl2c.CVisitor(variables)

Bases: aslutils.ASLVisitor.ASLVisitor

(Internal) Class that generates C code from ASL Code

Externally, this should not be used directly, but via asl_to_c().

The entry method for parsing should always be visitStart(). Since the visitor keeps track of variables, for each call to visitStart() a fresh object should be used.

The methods return the generated C code as a [str]. Note that no code is generated for variables that are simply assigned to constants. So in order to generate the “full” valid code for the ASL snippet it is possible that the content of self.variables also has to be taken into account.

Parameters:variables ({str: (ASLType or None, Any or None)}) – A mapping from name to (type: ASLType or None, value) for all pre-existing variables. This is needed so that when assigning to one of those variables it is not redeclared as ASL is somewhat like python with respect to var declarations.
Variables:self.variables ({str: (bool, ASLType or None, Any or None)}) – A mapping from variable name to a tuple containing: a bool which signifies whether the variable already existed before the snippet (in this order), the type of the variable as an ASLType or None if unknown. The constant value of the variable or None if unknown.
visitAssignableExpr(ctx: aslutils.ASLParser.ASLParser.AssignableExprContext)
visitBlock(ctx: aslutils.ASLParser.ASLParser.BlockContext)
visitErrorNode(node)
visitExpression(ctx: aslutils.ASLParser.ASLParser.ExpressionContext)
visitLiteral(ctx: aslutils.ASLParser.ASLParser.LiteralContext)
visitSimpleStatement(ctx: aslutils.ASLParser.ASLParser.SimpleStatementContext)
visitStart(ctx: aslutils.ASLParser.ASLParser.StartContext)
visitStatement(ctx: aslutils.ASLParser.ASLParser.StatementContext)
aslutils.asl2c.asl_to_c(string, fields)

Converts the given processed ASL string into a c program snippet

Calls asl_to_lang(), for more information check it’s documentation.

Parameters:
  • string (str) – ASL snippet string. The ASL code has to contain special tokens for structure instead of indentation and newlines.
  • fields ([(str, int)]) – A list of fields, each specified by a name and a length (in bits).
Returns:

A pair containing a variable map and the generated c code. The variable map maps from variable name to if it already existed before the snippet, its type (if known) and its value (if it’s a known constant).

Return type:

({str: (bool, ASLType or None, Any)}, [str])

aslutils.asl2py module

class aslutils.asl2py.PythonVisitor(variables)

Bases: aslutils.ASLVisitor.ASLVisitor

(Internal) Class that generates Python code from ASL Code

Externally, this should not be used directly, but via asl_to_py().

The entry method for parsing should always be visitStart(). Since the visitor keeps track of variables, for each call to visitStart() a fresh object should be used.

The methods return the generated C code as a [str]. Note that no code is generated for variables that are simply assigned to constants. So in order to generate the “full” valid code for the ASL snippet it is possible that the content of self.variables also has to be taken into account.

Parameters:variables ({str: (ASLType or None, Any or None)}) – A mapping from name to (type: ASLType or None, value) for all pre-existing variables. This is needed so that when assigning to one of those variables it is not redeclared as ASL is somewhat like python with respect to var declarations.
Variables:self.variables ({str: (bool, ASLType or None, Any or None)}) – A mapping from variable name to a tuple containing: a bool which signifies whether the variable already existed before the snippet (in this order), the type of the variable as an ASLType or None if unknown. The constant value of the variable or None if unknown.
visitAssignableExpr(ctx: aslutils.ASLParser.ASLParser.AssignableExprContext)
visitBlock(ctx: aslutils.ASLParser.ASLParser.BlockContext)
visitErrorNode(node)
visitExpression(ctx: aslutils.ASLParser.ASLParser.ExpressionContext)
visitLiteral(ctx: aslutils.ASLParser.ASLParser.LiteralContext)
visitSimpleStatement(ctx: aslutils.ASLParser.ASLParser.SimpleStatementContext)
visitStart(ctx: aslutils.ASLParser.ASLParser.StartContext)
visitStatement(ctx: aslutils.ASLParser.ASLParser.StatementContext)
aslutils.asl2py.asl_to_py(string, fields)

Converts the given processed ASL string into a python program snippet

Calls asl_to_lang(), for more information check it’s documentation.

Parameters:
  • string (str) – ASL snippet string. The ASL code has to contain special tokens for structure instead of indentation and newlines.
  • fields ([(str, int)]) – A list of fields, each specified by a name and a length (in bits).
Returns:

A pair containing a variable map and the generated c code. The variable map maps from variable name to if it already existed before the snippet, its type (if known) and its value (if it’s a known constant).

Return type:

({str: (bool, ASLType or None, Any)}, [str])

aslutils.asl2vhd module

class aslutils.asl2vhd.VHDLVisitor(variables)

Bases: aslutils.ASLVisitor.ASLVisitor

(Internal) Class that generates VHDL code from ASL Code

Externally, this should not be used directly, but via asl_to_vhdl().

The entry method for parsing should always be visitStart(). Since the visitor keeps track of variables, for each call to visitStart() a fresh object should be used.

The methods return the generated VHDL code as a [str]. Note that no code is generated for variables that are simply assigned to constants. So in order to generate the “full” valid code for the ASL snippet it is possible that the content of self.variables also has to be taken into account.

Parameters:variables ({str: (ASLType or None, Any or None)}) – A mapping from name to (type: ASLType or None, value) for all pre-existing variables. This is needed so that when assigning to one of those variables it is not redeclared as ASL is somewhat like python with respect to var declarations.
Variables:self.variables ({str: (bool, ASLType or None, Any or None)}) – A mapping from variable name to a tuple containing: a bool which signifies whether the variable already existed before the snippet (in this order), the type of the variable as an ASLType or None if unknown. The constant value of the variable or None if unknown.
visitAssignableExpr(ctx: aslutils.ASLParser.ASLParser.AssignableExprContext)
visitBlock(ctx: aslutils.ASLParser.ASLParser.BlockContext)
visitErrorNode(node)
visitExpression(ctx: aslutils.ASLParser.ASLParser.ExpressionContext)
visitLiteral(ctx: aslutils.ASLParser.ASLParser.LiteralContext)
visitSimpleStatement(ctx: aslutils.ASLParser.ASLParser.SimpleStatementContext)
visitStart(ctx: aslutils.ASLParser.ASLParser.StartContext)
visitStatement(ctx: aslutils.ASLParser.ASLParser.StatementContext)
aslutils.asl2vhd.asl_to_vhdl(string, fields)

Converts the given processed ASL string into a vhdl process snippet

Calls asl_to_lang(), for more information check it’s documentation.

Parameters:
  • string (str) – ASL snippet string. The ASL code has to contain special tokens for structure instead of indentation and newlines.
  • fields ([(str, int)]) – A list of fields, each specified by a name and a length (in bits).
Returns:

A pair containing a variable map and the generated c code. The variable map maps from variable name to if it already existed before the snippet, its type (if known) and its value (if it’s a known constant).

Return type:

({str: (bool, ASLType or None, Any)}, [str])

aslutils.asl_type module

class aslutils.asl_type.ASLType(type, value=None)

Bases: object

Type information for an ASL expression

Currently the following types are supported (see enum ASLType.Kind):
  • bits – bitstring of length n
  • bitpattern – bitstring of length n which may contain don’t care bits (x)
  • int – mathematical integer with (in theory) infinite precision
  • bool – either true or false
  • real – mathematical real number with (in theory) infinite precision
  • other – custom types
Parameters:
  • type (ASLType.Kind) – enum value of type ASLType.Kind
  • value – for bits and bitpatterns this specifies the length if known, for other it specifies the type name (opt)
class Kind

Bases: enum.Enum

An enumeration.

bitpattern = 2
bits = 1
bool = 4
int = 3
other = 6
real = 5
c_type()

Returns type in C (as a str) that corresponds the closest to self

aslutils.asl_type_visitor module

class aslutils.asl_type_visitor.ASLTypeVisitor(parent)

Bases: aslutils.ASLVisitor.ASLVisitor

Visits expressions and returns an ASLType or None

Does minimal typechecking with asserts. If the visitor visits code containing type errors, it might return the wrong type (it is not guaranteed to complain when there are type errors). If the type can’t be inferred, it returns None.

Parameters:parent – The parent object that invokes this object. The parent object must have a field named value_visitor which is of type ASLValueVisitor and a field named type_visitor which stores this object.
visitExpression(ctx: aslutils.ASLParser.ASLParser.ExpressionContext)
visitLiteral(ctx: aslutils.ASLParser.ASLParser.LiteralContext)
visitTypeName(ctx: aslutils.ASLParser.ASLParser.TypeNameContext)

aslutils.asl_value_visitor module

class aslutils.asl_value_visitor.ASLValueVisitor(parent)

Bases: aslutils.ASLVisitor.ASLVisitor

Visits expressions and returns their value as a python type if it’s a constant otherwise None.

For bits a value of type int is returned. For bitpatterns the returned value is the pair of (value: int, mask: int). For other a string is returned. For the other types (int, bool, real) a value of the directly corresponding python type is returned (so: int, bool, float).

Parameters:parent – The parent object that invokes this object. The parent object must have a field named type_visitor which is of type ASLTypeVisitor and a field named value_visitor which stores this object.
visitExpression(ctx: aslutils.ASLParser.ASLParser.ExpressionContext)
visitLiteral(ctx: aslutils.ASLParser.ASLParser.LiteralContext)

aslutils.parse_asl_file module

class aslutils.parse_asl_file.CaseField(str_repr)

Bases: object

Represents one field of a case statement

An example case statement line with 3 fields:

case (op, 25 +: 3, id_t) of

The class represents one of the following:

  • A named field (if intialized with “op” or “id_t”).
  • An implicit range (if initialized with “25 +: 3”).

The result of initializing with invalid input is not defined.

Parameters:

str_repr (str) – The string in ASL syntax, it does not contain the comma or paren

Variables:
  • self.name (str or None) – The name of the field if named field
  • self.start (int or None) – The start of the implicit range if implicit range
  • self.run (int or None) – The run of the implicit range if implicit range
class aslutils.parse_asl_file.NopDecodeListener

Bases: object

Defines the interface of listening to decoder file traversals

Meant for subclassing and selectively overriding methods to visit decoder files.

For nodes which have children (decode, case and when) the return value of the corresponding function decides whether the children should be visited or not. Those functions also have functions (after_listen…) that get called after the children have been, or would have been visited (irrespective of what the listen function returned). By default they return True.

after_listen_case(fields)

Called after visiting case

after_listen_decode(name)

Called after visiting decode (__decode decoding_name)

after_listen_when(values)

Called after visiting case

listen_case(fields)

Called on case statements with a list of fields, has children

For instance case (field_name, 5 +: 2) of would cause it to be called with an array of two fields.

Parameters:fields ([CaseField]) – A list of fields, one for each field in the case statement
listen_decode(name)

Called on decode (__decode decoding_name), has children

listen_encoding(name)

Called on encoding (__encoding encname)

listen_field(name, start, run)

Called on field declarations (__field fname 0 +: 5)

listen_unallocated()

Called on unallocated directives (__UNALLOCATED)

listen_undocumented()

Called on undocumented directives (__UNDOCUMENTED)

listen_unused()

Called on unused directives (__UNUSED)

listen_when(values)

Called on when statements with a list of values, has children

For instance when (‘xx10’, _, _) of would cause it to be called with an array of three values.

Parameters:fields ([WhenValue]) – A list of values, one for each value in the when statement
class aslutils.parse_asl_file.NopInstrsListener

Bases: object

Defines the interface of listening to instruction file traversals

Meant for subclassing and selectively overriding methods to visit instruction files.

For nodes which have children (instruction and encoding) the return value of the corresponding function decides whether the children should be visited or not. Those functions also have functions (after_listen…) that get called after the children have been, or would have been visited (irrespective of what the listen function returned). By default they return True.

after_listen_encoding(name)

Called after encoding (__encoding enc_name)

after_listen_instruction(name)

Called after instructions (__instruction inst_name)

listen_decode(code)

Called on decode (__decode), no children

listen_encode(code)

Called on encode (__encode), no children

listen_encoding(name)

Called on encoding (__encoding enc_name) has children

listen_execute(code)

Called on execute (__execute), no children

listen_instruction(name)

Called on instructions (__instruction inst_name), has children

listen_postdecode(code)

Called on postdecode (__postdecode), no children

listen_postencode(code)

Called on postencode (__postencode), no children

class aslutils.parse_asl_file.WhenValue(str_repr)

Bases: object

Represents one value of a when statement

An example when statement line with 4 values:

when (!'000', _, '0101x00', '1101' to '1111') =>

The class represents one of the following:

  • Don’t care (if intialized with “_”).
  • A bit-pattern (if intialized with “‘0101x00’”).
  • A negated bit-pattern (if intialized with “!‘000’”).
  • A range (if initialized with “‘1101’ to ‘1111’”).

The result of initializing with invalid input is not defined. Note that the range syntax is an extension not allowed in standard ASL.

Parameters:

str_repr (str) – The value in ASL syntax

Variables:
  • self.empty (bool or None) – True if the value is a don’t care
  • self.notvalue (str or None) – The bit-pattern (without the single quotes) if negated bit pattern
  • self.value (str or None) – The bit-pattern (without the single quotes) if bit pattern
  • self.range ((int, int) or None) – The start and end (inclusive) of the range if range.
aslutils.parse_asl_file.extract_code(tree, result)

(Internal) returns a string (split into multiple parts for efficiency) of ASL processed code extracted from the given nodes

Processed code is asl code which contains START, END, and NEWLINE tokens to give it structure as opposed to indentation. This allows the code to be lexed using antlr lexers.

Parameters:
  • tree ([Node] where Node is ([Node], str)) – A list of nodes which correspond to asl code
  • result ([str]) – Partial results for recursion (in the top-level call this should be empty)
Returns:

For efficiency the processed ASL code is split into pieces

Return type:

[str]

aslutils.parse_asl_file.line_list(filename)

(Internal) Returns a list of tuples (indentation-level, line) one for every line

Empty lines, comments and extra whitespace are striped out. Indents are expected to be 4 spaces.

Parameters:filename (str) – Path of the asl file name (may be relative or absolute).
Returns:List of every content line with the indentation-level as a tuple
Return type:[(int, str)]
aslutils.parse_asl_file.mask_bits(bitstring)

Returns an int corresponding to the bitmask of the bitpattern

For example “100xx01” is translated to 0b1110011.

Returns:The integer value where only don’t cares are mapped to 0
Return type:int
aslutils.parse_asl_file.parse_asl_decoder_file(filename, listener)

Parses the given asl decoder file and invokes the listener object on each node

Parameters:
  • filename (str) – A path to the decoder file
  • listener – A listener object which implements the same interface as NopDecodeListener
aslutils.parse_asl_file.parse_asl_instructions_file(filename, listener)

Parses the given asl instruction file and invokes the listener object on each node

Parameters:
  • filename (str) – A path to the instruction file
  • listener – A listener object which implements the same interface as NopInstrsListener
aslutils.parse_asl_file.tree_from_lines(lines)

(Internal) Generates a tree from a set of line tuples (indent-level, line)

The following input:

[(0, "str"), (1, "str1"), (1, str2), (0, str3)]

would result in the tree:

[([([], str1), ([], str2)], str), ([], str3)]
Parameters:lines ([(int, str)]) – A list of lines where one line is the string and the indentation-level
Returns:A correctly nested representation of the lines
Return type:[Node] where Node is ([Node], str)
aslutils.parse_asl_file.value_bits(bitstring)

Returns an int corresponding to the value of the bitpattern

For example “100xx01” is translated to 0b1000001.

Returns:The integer value of the bit pattern interpreted without don’t care values
Return type:int
aslutils.parse_asl_file.visit_decoder_tree(tree, listener)

(Internal) Traverses the given tree of a decoder file and invokes the listener

For each line it decides what kind of line it is and calls the corresponding function of the listener.

Parameters:
  • tree – The syntax tree of the ASL decoder file where each node is tuple of ([children], line) where children is a list of zero or more child nodes and line is the line as a string stripped of all whitespace and comments.
  • listener – A listener object which implements the interface specified by NopDecodeListener.
aslutils.parse_asl_file.visit_instructions_listing(tree, listener)

(Internal) Traverses the given tree of a instructions file and invokes the listener

For each line it decides what kind of line it is and calls the corresponding function of the listener.

Parameters:
  • tree – The syntax tree of the ASL decoder file where each node is tuple of ([children], line) where children is a list of zero or more child nodes and line is the line as a string stripped of all whitespace and comments.
  • listener – A listener object which implements the interface specified by NopInstrsListener.

Module contents