Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VCLLVM] LLVM to COL rewriter for LLVM specific nodes #990

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion col/src/main/java/vct/col/ast/Node.scala
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,9 @@ final case class BipInternal[G]()(implicit val o: Origin = DiagnosticOrigin) ext
final case class BipPortSynchronization[G](ports: Seq[Ref[G, BipPort[G]]], wires: Seq[BipGlueDataWire[G]])(val blame: Blame[BipSynchronizationFailure])(implicit val o: Origin) extends GlobalDeclaration[G] with BipPortSynchronizationImpl[G]
final case class BipTransitionSynchronization[G](transitions: Seq[Ref[G, BipTransition[G]]], wires: Seq[BipGlueDataWire[G]])(val blame: Blame[BipSynchronizationFailure])(implicit val o: Origin) extends GlobalDeclaration[G] with BipTransitionSynchronizationImpl[G]

final class LlvmFunctionContract[G](val value:String, val references:Seq[(String, Ref[G, Declaration[G]])])(implicit val o: Origin) extends NodeFamily[G] with LLVMFunctionContractImpl[G]
final class LlvmFunctionContract[G](val value:String, val references:Seq[(String, Ref[G, Declaration[G]])])
(val blame: Blame[NontrivialUnsatisfiable])
(implicit val o: Origin) extends NodeFamily[G] with LLVMFunctionContractImpl[G]

final class LlvmFunctionDefinition[G](val returnType: Type[G],
val args: Seq[Variable[G]],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package vct.col.ast.lang

import vct.col.ast.LlvmFunctionDefinition

trait LLVMFunctionDefinitionImpl[G] { this: LlvmFunctionDefinition[G] =>
import vct.col.ast.{Declaration, LlvmFunctionDefinition}
import vct.col.ast.util.Declarator

trait LLVMFunctionDefinitionImpl[G] extends Declarator[G] { this: LlvmFunctionDefinition[G] =>
override def declarations: Seq[Declaration[G]] = args
}
3 changes: 2 additions & 1 deletion col/src/main/java/vct/col/resolve/ctx/Referrable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ case object Referrable {
case decl: VeyMontSeqProg[G] => RefSeqProg(decl)
case decl: VeyMontThread[G] => RefVeyMontThread(decl)
case decl: JavaBipGlueContainer[G] => RefJavaBipGlueContainer()
case decl: LlvmFunctionDefinition[G] => RefLlvmFunctionDefinition()
})

def originName(decl: Declaration[_]): String = decl.o match {
Expand Down Expand Up @@ -221,7 +222,7 @@ case class RefPVLConstructor[G](decl: PVLConstructor[G]) extends Referrable[G] w
case class RefJavaBipStatePredicate[G](state: String, decl: JavaAnnotation[G]) extends Referrable[G] with JavaBipStatePredicateTarget[G]
case class RefJavaBipGuard[G](decl: JavaMethod[G]) extends Referrable[G] with JavaNameTarget[G]
case class RefJavaBipGlueContainer[G]() extends Referrable[G] // Bip glue jobs are not actually referrable

case class RefLlvmFunctionDefinition[G]() extends Referrable[G]
case class RefSeqProg[G](decl: VeyMontSeqProg[G]) extends Referrable[G]
case class RefVeyMontThread[G](decl: VeyMontThread[G]) extends Referrable[G] with PVLNameTarget[G]

Expand Down
4 changes: 2 additions & 2 deletions parsers/src/main/java/vct/parsers/ColLLVMParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ case class ColLLVMParser(override val originProvider: OriginProvider, override v
}

override def parse[G](stream: CharStream): ParseResult[G] = {
throw Unreachable("LLVM IR files should be parsed from an not ANTLR CharStream, use VCLLVM instead!")
throw Unreachable("LLVM IR files shouldn't be parsed from an ANTLR CharStream, use VCLLVM instead!")
}

override def parse[G](readable: Readable): ParseResult[G] = {
val command = Seq("VCLLVM", "--sample-col") //TODO convert --sample-col to actual file at some point
val command = Seq("VCLLVM", readable.fileName)
val process = new ProcessBuilder(command:_*).start()
process.waitFor()
if (process.exitValue() != 0) {
Expand Down
40 changes: 40 additions & 0 deletions rewrite/src/main/java/vct/col/rewrite/lang/LangLLVMToCol.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package vct.col.rewrite.lang

import com.typesafe.scalalogging.LazyLogging
import vct.col.ast._
import vct.col.origin.Origin
import vct.col.rewrite.{Generation, Rewritten}

case class LangLLVMToCol[Pre <: Generation](rw: LangSpecificToCol[Pre]) extends LazyLogging {
type Post = Rewritten[Pre]
implicit val implicitRewriter: AbstractRewriter[Pre, Post] = rw


def rewriteFunctionDef(func: LlvmFunctionDefinition[Pre]): Unit = {
implicit val o: Origin = func.contract.o
// TODO replace stub contract
val stubContract = new ApplicableContract[Post](
requires = new UnitAccountedPredicate[Post](BooleanValue(value = true)),
ensures = new UnitAccountedPredicate[Post](BooleanValue(value = true)),
contextEverywhere = BooleanValue(value = true),
signals = Seq.empty,
givenArgs = Seq.empty,
yieldsArgs = Seq.empty,
decreases = None)(func.contract.blame)(func.contract.o)


rw.globalDeclarations.declare(
new Procedure[Post](
returnType = rw.dispatch(func.returnType),
args = rw.variables.collect {
func.args.foreach(rw.dispatch)
}._1,
outArgs = Nil,
typeArgs = Nil,
body = Some(rw.dispatch(func.body)),
contract = stubContract
)(func.blame)(func.o)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ case class LangSpecificToCol[Pre <: Generation]() extends Rewriter[Pre] with Laz
val c: LangCToCol[Pre] = LangCToCol(this)
val pvl: LangPVLToCol[Pre] = LangPVLToCol(this)
val silver: LangSilverToCol[Pre] = LangSilverToCol(this)
val llvm: LangLLVMToCol[Pre] = LangLLVMToCol(this)

val currentThis: ScopedStack[Expr[Post]] = ScopedStack()
val currentClass: ScopedStack[Class[Pre]] = ScopedStack()
Expand Down Expand Up @@ -66,6 +67,7 @@ case class LangSpecificToCol[Pre <: Generation]() extends Rewriter[Pre] with Laz
case func: CFunctionDefinition[Pre] => c.rewriteFunctionDef(func)
case decl: CGlobalDeclaration[Pre] => c.rewriteGlobalDecl(decl)
case decl: CLocalDeclaration[Pre] => ???
case func: LlvmFunctionDefinition[Pre] => llvm.rewriteFunctionDef(func)

case cls: Class[Pre] =>
currentClass.having(cls) {
Expand Down