Skip to content

Commit adef2e3

Browse files
committed
improve for syntax
commenting
1 parent cd37872 commit adef2e3

File tree

4 files changed

+395
-339
lines changed

4 files changed

+395
-339
lines changed

src/main/scala/ir/dsl/DSL.scala

+3-331
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,6 @@ def ret(params: (String, Expr)*): EventuallyReturn = EventuallyReturn(params)
185185

186186
def unreachable: EventuallyUnreachable = EventuallyUnreachable()
187187

188-
case class Call(target: String, actualParams: (String, Expr)*)
189-
190-
def directCall(lhs: Iterable[(String, Variable)], rhs: Call): EventuallyCall =
191-
EventuallyCall(DelayNameResolve(rhs.target), lhs.toArray, rhs.actualParams)
192-
193188
def directCall(lhs: Iterable[(String, Variable)], tgt: String, actualParams: (String, Expr)*): EventuallyCall =
194189
EventuallyCall(DelayNameResolve(tgt), lhs.toArray, actualParams)
195190

@@ -255,6 +250,9 @@ def block(label: String, sl: (NonCallStatement | EventuallyStatement | Eventuall
255250
EventuallyBlock(label, statements, rjump)
256251
}
257252

253+
/**
254+
* Construct a block from a list of statements with a default name.
255+
*/
258256
def stmts(sl: (EventuallyCall | NonCallStatement | EventuallyStatement | EventuallyJump)*): EventuallyBlock = {
259257

260258
val stmts =
@@ -372,165 +370,6 @@ case class EventuallyProgram(
372370
def cloneable = this.copy(mainProcedure = mainProcedure.cloneable, otherProcedures = otherProcedures.map(_.cloneable))
373371
}
374372

375-
object Counter {
376-
var count = 1
377-
def next() = {
378-
count += 1
379-
count
380-
}
381-
382-
def nlabel(name: String = "id"): String = {
383-
name + "_" + next()
384-
}
385-
}
386-
387-
extension (i: EventuallyBlock)
388-
@targetName("sequenceblocklist")
389-
infix def `;`(j: List[EventuallyBlock]): List[EventuallyBlock] = sequence(List(i), j)
390-
391-
extension (i: List[EventuallyBlock])
392-
@targetName("sequenceblocklist")
393-
infix def `;`(j: List[EventuallyBlock]): List[EventuallyBlock] = sequence(i, j)
394-
395-
def sequence(first: List[EventuallyBlock], rest: List[EventuallyBlock]): List[EventuallyBlock] = {
396-
require(first.nonEmpty)
397-
require(rest.nonEmpty)
398-
require(first.last.j.isInstanceOf[EventuallyUnreachable])
399-
val last = first.last
400-
last.j = goto(rest.head.label)
401-
first ++ rest
402-
}
403-
404-
def blocks(blocks: List[EventuallyBlock]*): List[EventuallyBlock] = {
405-
blocks.toList match {
406-
case Nil => List()
407-
case h :: Nil => h
408-
case h :: tail => tail.foldLeft(h)((acc, nextb) => sequence(acc, nextb))
409-
}
410-
411-
}
412-
413-
def setSuccIfUndef(first: List[EventuallyBlock], rest: EventuallyBlock*) = {
414-
require(first.nonEmpty)
415-
require(rest.nonEmpty)
416-
417-
if (first.last.j.isInstanceOf[EventuallyUnreachable]) {
418-
val last = first.last
419-
last.j = goto(rest.head.label)
420-
}
421-
first
422-
}
423-
424-
//def While(cond: Expr, body: EventuallyBlock*): List[EventuallyBlock] = {
425-
// While(cond, body.toList)
426-
//}
427-
428-
def For(
429-
init: List[EventuallyBlock],
430-
cond: Expr,
431-
after: List[EventuallyBlock],
432-
body: List[EventuallyBlock]
433-
): List[EventuallyBlock] = {
434-
require(after.nonEmpty)
435-
require(init.nonEmpty)
436-
val internal = sequence(body, after)
437-
val loop = While(cond) Do (internal)
438-
sequence(init, loop)
439-
}
440-
441-
//def For(init: EventuallyBlock, cond: Expr, after: EventuallyBlock, body: EventuallyBlock*): List[EventuallyBlock] = {
442-
// For(List(init), cond, List(after), body.toList)
443-
//}
444-
//
445-
//def For(init: NonCallStatement, cond: Expr, after: NonCallStatement, body: EventuallyBlock*): List[EventuallyBlock] = {
446-
// require(body.nonEmpty)
447-
// For(List(stmts(init)), cond, List(stmts(after)), body.toList)
448-
//}
449-
//def For(
450-
// init: NonCallStatement,
451-
// cond: Expr,
452-
// after: NonCallStatement,
453-
// body: List[EventuallyBlock]
454-
//): List[EventuallyBlock] = {
455-
// For(List(stmts(init)), cond, List(stmts(after)), body.toList)
456-
//}
457-
458-
case class WhileDo(cond: Expr) {
459-
def Do(body: Iterable[EventuallyBlock]) = While(cond, (body.toList))
460-
@targetName("doBlocks")
461-
def Do(body: EventuallyBlock*) = While(cond, (body.toList))
462-
@targetName("doStatements")
463-
def Do(sl: (EventuallyCall | NonCallStatement | EventuallyStatement | EventuallyJump)*) =
464-
While(cond, (List(stmts(sl: _*))))
465-
}
466-
467-
def While(cond: Expr) = WhileDo(cond)
468-
469-
def While(cond: Expr, body: List[EventuallyBlock]): List[EventuallyBlock] = {
470-
val loopExit = Counter.nlabel("while_exit")
471-
val loopBackedge = Counter.nlabel("while_backedge")
472-
val loopEntry = Counter.nlabel("while_entry")
473-
val loopBody = Counter.nlabel("while_body")
474-
List(block(loopEntry, goto(loopBody, loopExit)))
475-
++
476-
sequence(sequence(List(block(loopBody, Assume(cond))), body), List(block(loopBackedge, goto(loopEntry))))
477-
++
478-
List(block(loopExit, Assume(UnaryExpr(BoolNOT, cond)), unreachable))
479-
}
480-
481-
case class ThenV(cond: Expr, body: List[EventuallyBlock]) {
482-
def Else(els: Iterable[EventuallyBlock]): List[EventuallyBlock] = If(cond, body, els.toList)
483-
def Else(els: EventuallyBlock*): List[EventuallyBlock] = If(cond, body, els.toList)
484-
@targetName("elseStatements")
485-
def Else(els: (NonCallStatement | EventuallyStatement | EventuallyJump)*): List[EventuallyBlock] =
486-
If(cond, body, List(stmts(els: _*)))
487-
}
488-
489-
case class ElseV(cond: Expr, thenBody: List[EventuallyBlock], body: List[EventuallyBlock])
490-
491-
given IfThenBlocks: Conversion[ThenV, List[EventuallyBlock]] with
492-
def apply(x: ThenV): List[EventuallyBlock] = If(x.cond, x.body, List())
493-
494-
given StmtList
495-
: Conversion[EventuallyCall | NonCallStatement | EventuallyStatement | EventuallyJump, List[EventuallyBlock]] with
496-
def apply(x: EventuallyCall | NonCallStatement | EventuallyStatement | EventuallyJump): List[EventuallyBlock] = List(
497-
stmts(x)
498-
)
499-
500-
given BlockList: Conversion[EventuallyBlock, List[EventuallyBlock]] with
501-
def apply(b: EventuallyBlock) = List(b)
502-
503-
case class IfThen(cond: Expr) {
504-
def Then(body: Iterable[EventuallyBlock]): ThenV = ThenV(cond, body.toList)
505-
def Then(body: EventuallyBlock*): ThenV = ThenV(cond, body.toList)
506-
@targetName("thenStatements")
507-
def Then(body: (NonCallStatement | EventuallyStatement | EventuallyJump)*): ThenV = ThenV(cond, List(stmts(body: _*)))
508-
509-
}
510-
511-
def If(cond: Expr) = IfThen(cond)
512-
513-
def If(cond: Expr, ifThen: List[EventuallyBlock], ifElse: List[EventuallyBlock]): List[EventuallyBlock] = {
514-
val ifEntry = Counter.nlabel("if_entry")
515-
val thenCase = Counter.nlabel("if_then")
516-
val elseCase = Counter.nlabel("if_else")
517-
val ifExit = Counter.nlabel("if_exit")
518-
519-
val thenNonempty =
520-
if (ifThen.isEmpty) then List(block(Counter.nlabel("if_then_empty"), unreachable)) else ifThen
521-
val elseNonempty =
522-
if (ifElse.isEmpty) then List(block(Counter.nlabel("if_else_empty"), unreachable)) else ifElse.toList
523-
524-
val exitBlock = block(ifExit, unreachable)
525-
val thenBlocks = setSuccIfUndef(thenNonempty, exitBlock)
526-
val elseBlocks = setSuccIfUndef(elseNonempty, exitBlock)
527-
528-
List(block(ifEntry, goto(thenCase, elseCase)))
529-
++ sequence(List(block(thenCase, Assume(cond), unreachable)), thenBlocks)
530-
++ sequence(List(block(elseCase, Assume(UnaryExpr(BoolNOT, cond)), unreachable)), elseBlocks)
531-
++ List(exitBlock)
532-
}
533-
534373
def prog(mainProc: EventuallyProcedure, procedures: EventuallyProcedure*): Program =
535374
prog(Seq(), mainProc, procedures: _*)
536375

@@ -554,170 +393,3 @@ def progUnresolved(
554393
procedures: EventuallyProcedure*
555394
): EventuallyProgram =
556395
EventuallyProgram(mainProc, procedures, initialMemory)
557-
558-
/**
559-
* Expr construction
560-
*/
561-
562-
extension (lvar: Variable)
563-
infix def :=(j: Expr) = LocalAssign(lvar, j)
564-
def :=(j: Int) = lvar.getType match {
565-
case BitVecType(sz) => LocalAssign(lvar, BitVecLiteral(j, sz))
566-
case IntType => LocalAssign(lvar, IntLiteral(j))
567-
case _ => ???
568-
}
569-
def :=(j: Boolean) = lvar.getType match {
570-
case BoolType => LocalAssign(lvar, if j then TrueLiteral else FalseLiteral)
571-
case _ => ???
572-
}
573-
574-
extension (lvar: List[(String, Variable)]) infix def :=(j: Call) = directCall(lvar, j)
575-
extension (lvar: Seq[(String, Variable)]) infix def :=(j: Call) = directCall(lvar, j)
576-
577-
extension (v: Int)
578-
@targetName("ibv64")
579-
def bv64 = BitVecLiteral(v, 64)
580-
@targetName("ibv32")
581-
def bv32 = BitVecLiteral(v, 32)
582-
@targetName("ibv16")
583-
def bv16 = BitVecLiteral(v, 16)
584-
@targetName("ibv8")
585-
def bv8 = BitVecLiteral(v, 8)
586-
@targetName("ibv1")
587-
def bv1 = BitVecLiteral(v, 1)
588-
@targetName("itobv")
589-
def bv(sz: Int) = BitVecLiteral(v, sz)
590-
591-
def bv64 = BitVecType(64)
592-
def bv32 = BitVecType(32)
593-
def bv16 = BitVecType(16)
594-
def bv8 = BitVecType(8)
595-
def bv1 = BitVecType(1)
596-
597-
extension (i: Expr)
598-
infix def ===(j: Expr): Expr = i.getType match {
599-
case IntType => BinaryExpr(IntEQ, i, j)
600-
case b: BitVecType => BinaryExpr(BVEQ, i, j)
601-
case BoolType => BinaryExpr(BoolEQ, i, j)
602-
case m: MapType => ???
603-
}
604-
infix def !==(j: Expr): Expr = i.getType match {
605-
case IntType => BinaryExpr(IntNEQ, i, j)
606-
case b: BitVecType => BinaryExpr(BVNEQ, i, j)
607-
case BoolType => BinaryExpr(BoolNEQ, i, j)
608-
case m: MapType => ???
609-
}
610-
infix def +(j: Expr): Expr = i.getType match {
611-
case IntType => BinaryExpr(IntADD, i, j)
612-
case b: BitVecType => BinaryExpr(BVADD, i, j)
613-
case BoolType => BinaryExpr(BoolOR, i, j)
614-
case m: MapType => ???
615-
}
616-
infix def -(j: Expr): Expr = i.getType match {
617-
case IntType => BinaryExpr(IntSUB, i, j)
618-
case b: BitVecType => BinaryExpr(BVSUB, i, j)
619-
case BoolType => ???
620-
case m: MapType => ???
621-
}
622-
infix def *(j: Expr): Expr = i.getType match {
623-
case IntType => BinaryExpr(IntMUL, i, j)
624-
case b: BitVecType => BinaryExpr(BVMUL, i, j)
625-
case BoolType => BinaryExpr(BoolAND, i, j)
626-
case m: MapType => ???
627-
}
628-
infix def /(j: Expr): Expr = i.getType match {
629-
case IntType => BinaryExpr(IntDIV, i, j)
630-
case b: BitVecType => BinaryExpr(BVSDIV, i, j)
631-
case BoolType => ???
632-
case m: MapType => ???
633-
}
634-
infix def &&(j: Expr): Expr = i.getType match {
635-
case IntType => ???
636-
case b: BitVecType => BinaryExpr(BVAND, i, j)
637-
case BoolType => BinaryExpr(BoolAND, i, j)
638-
case m: MapType => ???
639-
}
640-
infix def ||(j: Expr): Expr = i.getType match {
641-
case IntType => ???
642-
case b: BitVecType => BinaryExpr(BVOR, i, j)
643-
case BoolType => BinaryExpr(BoolOR, i, j)
644-
case m: MapType => ???
645-
}
646-
infix def <<(j: Expr): Expr = i.getType match {
647-
case IntType => ???
648-
case b: BitVecType => BinaryExpr(BVSHL, i, j)
649-
case BoolType => ???
650-
case m: MapType => ???
651-
}
652-
infix def >>(j: Expr): Expr = i.getType match {
653-
case IntType => ???
654-
case b: BitVecType => BinaryExpr(BVASHR, i, j)
655-
case BoolType => ???
656-
case m: MapType => ???
657-
}
658-
infix def >>>(j: Expr): Expr = i.getType match {
659-
case IntType => ???
660-
case b: BitVecType => BinaryExpr(BVLSHR, i, j)
661-
case BoolType => ???
662-
case m: MapType => ???
663-
}
664-
infix def %(j: Expr): Expr = i.getType match {
665-
case IntType => BinaryExpr(IntMOD, i, j)
666-
case b: BitVecType => BinaryExpr(BVSMOD, i, j)
667-
case BoolType => ???
668-
case m: MapType => ???
669-
}
670-
infix def <(j: Expr): Expr = i.getType match {
671-
case IntType => BinaryExpr(IntLT, i, j)
672-
case b: BitVecType => BinaryExpr(BVSLT, i, j)
673-
case BoolType => ???
674-
case m: MapType => ???
675-
}
676-
infix def >(j: Expr): Expr = i.getType match {
677-
case IntType => BinaryExpr(IntGT, i, j)
678-
case b: BitVecType => BinaryExpr(BVSGT, i, j)
679-
case BoolType => ???
680-
case m: MapType => ???
681-
}
682-
infix def <=(j: Expr): Expr = i.getType match {
683-
case IntType => BinaryExpr(IntLE, i, j)
684-
case b: BitVecType => BinaryExpr(BVSLE, i, j)
685-
case BoolType => ???
686-
case m: MapType => ???
687-
}
688-
infix def >=(j: Expr): Expr = i.getType match {
689-
case IntType => BinaryExpr(IntGE, i, j)
690-
case b: BitVecType => BinaryExpr(BVSGE, i, j)
691-
case BoolType => ???
692-
case m: MapType => ???
693-
}
694-
infix def ult(j: Expr): Expr = i.getType match {
695-
case IntType => ???
696-
case b: BitVecType => BinaryExpr(BVULT, i, j)
697-
case BoolType => ???
698-
case m: MapType => ???
699-
}
700-
infix def ugt(j: Expr): Expr = i.getType match {
701-
case IntType => ???
702-
case b: BitVecType => BinaryExpr(BVUGT, i, j)
703-
case BoolType => ???
704-
case m: MapType => ???
705-
}
706-
infix def ule(j: Expr): Expr = i.getType match {
707-
case IntType => ???
708-
case b: BitVecType => BinaryExpr(BVULE, i, j)
709-
case BoolType => ???
710-
case m: MapType => ???
711-
}
712-
infix def uge(j: Expr): Expr = i.getType match {
713-
case IntType => ???
714-
case b: BitVecType => BinaryExpr(BVUGE, i, j)
715-
case BoolType => ???
716-
case m: MapType => ???
717-
}
718-
infix def ++(j: Expr): Expr = i.getType match {
719-
case IntType => ???
720-
case b: BitVecType => BinaryExpr(BVCONCAT, i, j)
721-
case BoolType => ???
722-
case m: MapType => ???
723-
}

0 commit comments

Comments
 (0)