Skip to content

Commit

Permalink
sloppy commit, but when starting state chart temporals I decided to c…
Browse files Browse the repository at this point in the history
…hange the approach to handling rounds
  • Loading branch information
mbellotti committed Jan 24, 2023
1 parent 5aff0d9 commit 5fc318a
Show file tree
Hide file tree
Showing 19 changed files with 1,388 additions and 1,130 deletions.
2 changes: 0 additions & 2 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,10 +921,8 @@ func (pe *PrefixExpression) Position() []int { return pe.Token.GetPosition(
func (pe *PrefixExpression) String() string {
var out bytes.Buffer

out.WriteString("(")
out.WriteString(pe.Operator)
out.WriteString(pe.Right.String())
out.WriteString(")")

return out.String()
}
Expand Down
4 changes: 2 additions & 2 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestString(t *testing.T) {
want = "Mean: 2.000000Sigma: 0.400000;"
case *PrefixExpression:
got = t.String()
want = "(!3)"
want = "!3"
case *InfixExpression:
got = t.String()
want = "(3 > 3)"
Expand All @@ -181,7 +181,7 @@ func TestString(t *testing.T) {
want = "test fuzz = 24;"
case *ExpressionStatement:
got = t.String()
want = "(!3)"
want = "!3"
case *ParallelFunctions:
got = t.String()
want = "testtest"
Expand Down
1 change: 1 addition & 0 deletions grammar/FaultLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ RUN: 'run';
SPEC: 'spec';
STOCK: 'stock';
THEN: 'then';
WHEN: 'when';
THIS: 'this';
EVENTUALLY: 'eventually';
EVENTUALLYALWAYS: 'eventually-always';
Expand Down
4 changes: 2 additions & 2 deletions grammar/FaultParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ assumption
temporal
: ('eventually' | 'always' | 'eventually-always' )
| ('nmt' | 'nft') integer
| 'then' expression
;

invariant
: expression
: expression # invar
| 'when' expression 'then' expression # stageInvariant
;

assignment
Expand Down
38 changes: 22 additions & 16 deletions listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -1239,24 +1239,31 @@ func (l *FaultListener) ExitForStmt(c *parser.ForStmtContext) {
}
}

func (l *FaultListener) ExitStageInvariant(c *parser.StageInvariantContext) {
right := l.pop()
left := l.pop()
token := util.GenerateToken("ASSERT", "assert", c.GetStart(), c.GetStop())
l.push(&ast.InvariantClause{
Token: token,
Left: left.(ast.Expression),
Operator: "then",
Right: right.(ast.Expression),
})
}

func (l *FaultListener) ExitAssertion(c *parser.AssertionContext) {
token := util.GenerateToken("ASSERT", "assert", c.GetStart(), c.GetStop())

var temporal string
var temporalFilter string
var temporalN int
var err error
if c.Temporal() != nil {
if c.Temporal().GetChildCount() == 2 {
l.pop() // Discard the int
i := l.pop()
temporalRaw := c.Temporal().GetText()
temporal = ""
temporalFilter = temporalRaw[0 : len(temporalRaw)-1]
temporalN, err = strconv.Atoi(string(temporalRaw[len(temporalRaw)-1]))
if err != nil {
pos := token.Position
panic(fmt.Sprintf("temporal logic not value, filter should be an int: line %d col %d", pos[0], pos[1]))
}
temporalFilter = temporalRaw[0 : len(temporalRaw)-len(i.(*ast.IntegerLiteral).String())]
temporalN = int(i.(*ast.IntegerLiteral).Value)
} else {
temporal = c.Temporal().GetText()
}
Expand All @@ -1277,6 +1284,8 @@ func (l *FaultListener) ExitAssertion(c *parser.AssertionContext) {
Operator: e.Operator,
Right: e.Right,
}
case *ast.InvariantClause:
con = e
}

l.push(&ast.AssertionStatement{
Expand All @@ -1293,18 +1302,13 @@ func (l *FaultListener) ExitAssumption(c *parser.AssumptionContext) {
var temporal string
var temporalFilter string
var temporalN int
var err error
if c.Temporal() != nil {
if c.Temporal().GetChildCount() == 2 {
l.pop() // Discard the int
i := l.pop()
temporalRaw := c.Temporal().GetText()
temporal = ""
temporalFilter = temporalRaw[0 : len(temporalRaw)-1]
temporalN, err = strconv.Atoi(string(temporalRaw[len(temporalRaw)-1]))
if err != nil {
pos := token.Position
panic(fmt.Sprintf("temporal logic not value, filter should be an int: line %d col %d", pos[0], pos[1]))
}
temporalFilter = temporalRaw[0 : len(temporalRaw)-len(i.(*ast.IntegerLiteral).String())]
temporalN = int(i.(*ast.IntegerLiteral).Value)
} else {
temporal = c.Temporal().GetText()
}
Expand All @@ -1323,6 +1327,8 @@ func (l *FaultListener) ExitAssumption(c *parser.AssumptionContext) {
Operator: e.Operator,
Right: e.Right,
}
case *ast.InvariantClause:
con = e
}

l.push(&ast.AssumptionStatement{
Expand Down
31 changes: 20 additions & 11 deletions llvm/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@ var DoubleP = &irtypes.PointerType{ElemType: irtypes.Double}
var I1P = &irtypes.PointerType{ElemType: irtypes.I1}

var OP_NEGATE = map[string]string{
"==": "!=",
">=": "<",
">": "<=",
"<=": ">",
"!=": "==",
"<": ">=",
"&&": "||",
"||": "&&",
"==": "!=",
">=": "<",
">": "<=",
"<=": ">",
"!=": "==",
"<": ">=",
"&&": "||",
"||": "&&",
"then": "then",
//"=": "!=",
}

type Compiler struct {
module *ir.Module
module *ir.Module
markers []*ir.Global // 0-index -> Round
// 1-index -> Parallel Group

hasRunBlock bool
runRound int16
Expand Down Expand Up @@ -94,7 +97,7 @@ func NewCompiler() *Compiler {
Uncertains: make(map[string][]float64),
Components: make(map[string]*StateFunc),
}
c.addGlobal()
c.setup()
return c
}

Expand Down Expand Up @@ -211,6 +214,7 @@ func (c *Compiler) compile(node ast.Node) {
case *ast.ForStatement:
c.contextFuncName = "__run"
for i := int64(0); i < v.Rounds.Value; i++ {
c.contextBlock.NewStore(constant.NewInt(irtypes.I16, int64(c.runRound)), c.markers[0])
c.compileBlock(v.Body)
c.runRound = c.runRound + 1
c.stateCheck()
Expand Down Expand Up @@ -1567,7 +1571,12 @@ func (c *Compiler) GetIR() string {
return c.module.String()
}

func (c *Compiler) addGlobal() {
func (c *Compiler) setup() {
//Initialize Markers
c.markers = []*ir.Global{
c.module.NewGlobalDef("__rounds", constant.NewInt(irtypes.I16, 0)),
c.module.NewGlobalDef("__parallelGroup", constant.NewCharArrayFromString("start")),
}
// run block
c.contextFunc = c.module.NewFunc("__run", irtypes.Void)
mainBlock := c.contextFunc.NewBlock(name.Block())
Expand Down

0 comments on commit 5fc318a

Please sign in to comment.