From 4907f4ed2f471f2ca23dd7ab30f602c1baed84c6 Mon Sep 17 00:00:00 2001 From: bd-912 Date: Mon, 15 Apr 2024 00:54:47 -0600 Subject: Add skeleton of CFG visitor --- V2VM.java | 51 +++ vaporize/library/ControlFlowGraph.java | 42 ++ vaporize/library/VaporizeSimp.java | 678 --------------------------------- 3 files changed, 93 insertions(+), 678 deletions(-) create mode 100644 V2VM.java create mode 100644 vaporize/library/ControlFlowGraph.java delete mode 100644 vaporize/library/VaporizeSimp.java diff --git a/V2VM.java b/V2VM.java new file mode 100644 index 0000000..0f745f0 --- /dev/null +++ b/V2VM.java @@ -0,0 +1,51 @@ +import java.io.*; +import cs132.util.ProblemException; +import cs132.vapor.parser.VaporParser; +import cs132.vapor.ast.VaporProgram; +import cs132.vapor.ast.VBuiltIn.Op; + +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.PrintStream; + +import st.*; +import misc.*; +import vaporize.library.*; + +public class V2VM { + + public static void main(String[] args) { + try { + VaporProgram pgrm = parseVapor(System.in, System.out); + System.out.println(pgrm); + + ControlFlowGraph vp = new ControlFlowGraph(); + + } catch (IOException e) { + System.out.println(e.toString()); + System.exit(1); + } + } + + public static VaporProgram parseVapor(InputStream in, PrintStream err) throws IOException { + Op[] ops = { + Op.Add, Op.Sub, Op.MulS, Op.Eq, Op.Lt, Op.LtS, + Op.PrintIntS, Op.HeapAllocZ, Op.Error, + }; + boolean allowLocals = true; + String[] registers = null; + boolean allowStack = false; + + VaporProgram program; + try { + program = VaporParser.run(new InputStreamReader(in), 1, 1, + java.util.Arrays.asList(ops), + allowLocals, registers, allowStack); + } catch (ProblemException ex) { + err.println(ex.getMessage()); + return null; + } + + return program; + } +} diff --git a/vaporize/library/ControlFlowGraph.java b/vaporize/library/ControlFlowGraph.java new file mode 100644 index 0000000..0153b38 --- /dev/null +++ b/vaporize/library/ControlFlowGraph.java @@ -0,0 +1,42 @@ +package vaporize.library; + +import cs132.vapor.ast.*; +import st.*; +import misc.*; +import java.util.*; + +public class ControlFlowGraph extends VInstr.VisitorPR { + + public R visit(P p, VMemRead r) throws RuntimeException { + return null; + } + + public R visit(P p, VMemWrite w) throws RuntimeException { + return null; + } + + public R visit(P p, VAssign a) throws RuntimeException { + return null; + } + + public R visit(P p, VBranch b) throws RuntimeException { + return null; + } + + public R visit(P p, VGoto g) throws RuntimeException { + return null; + } + + public R visit(P p, VCall c) throws RuntimeException { + return null; + } + + public R visit(P p, VBuiltIn c) throws RuntimeException { + return null; + } + + public R visit(P p, VReturn r) throws RuntimeException { + return null; + } + +} diff --git a/vaporize/library/VaporizeSimp.java b/vaporize/library/VaporizeSimp.java deleted file mode 100644 index 6c69ea2..0000000 --- a/vaporize/library/VaporizeSimp.java +++ /dev/null @@ -1,678 +0,0 @@ -package vaporize.library; - -import syntaxtree.*; -package visitor; -import st.*; -import misc.*; -import java.util.*; - -public class VaporizeSimp implements GJVisitor { - - // - // Auto class visitors--probably don't need to be overridden. - // - public R visit(NodeList n, A argu) { - R _ret=null; - int _count=0; - for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { - e.nextElement().accept(this,argu); - _count++; - } - return _ret; - } - - public R visit(NodeListOptional n, A argu) { - if ( n.present() ) { - R _ret=null; - int _count=0; - for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { - e.nextElement().accept(this,argu); - _count++; - } - return _ret; - } - else - return null; - } - - public R visit(NodeOptional n, A argu) { - if ( n.present() ) - return n.node.accept(this,argu); - else - return null; - } - - public R visit(NodeSequence n, A argu) { - R _ret=null; - int _count=0; - for ( Enumeration e = n.elements(); e.hasMoreElements(); ) { - e.nextElement().accept(this,argu); - _count++; - } - return _ret; - } - - public R visit(NodeToken n, A argu) { return null; } - - // - // User-generated visitor methods below - // - - /** - * f0 -> MainClass() - * f1 -> ( TypeDeclaration() )* - * f2 -> - */ - public R visit(Goal n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; - } - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "{" - * f3 -> "public" - * f4 -> "static" - * f5 -> "void" - * f6 -> "main" - * f7 -> "(" - * f8 -> "String" - * f9 -> "[" - * f10 -> "]" - * f11 -> Identifier() - * f12 -> ")" - * f13 -> "{" - * f14 -> ( VarDeclaration() )* - * f15 -> ( Statement() )* - * f16 -> "}" - * f17 -> "}" - */ - public R visit(MainClass n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - n.f7.accept(this, argu); - n.f8.accept(this, argu); - n.f9.accept(this, argu); - n.f10.accept(this, argu); - n.f11.accept(this, argu); - n.f12.accept(this, argu); - n.f13.accept(this, argu); - n.f14.accept(this, argu); - n.f15.accept(this, argu); - n.f16.accept(this, argu); - n.f17.accept(this, argu); - return _ret; - } - - /** - * f0 -> ClassDeclaration() - * | ClassExtendsDeclaration() - */ - public R visit(TypeDeclaration n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "{" - * f3 -> ( VarDeclaration() )* - * f4 -> ( MethodDeclaration() )* - * f5 -> "}" - */ - public R visit(ClassDeclaration n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - return _ret; - } - - /** - * f0 -> "class" - * f1 -> Identifier() - * f2 -> "extends" - * f3 -> Identifier() - * f4 -> "{" - * f5 -> ( VarDeclaration() )* - * f6 -> ( MethodDeclaration() )* - * f7 -> "}" - */ - public R visit(ClassExtendsDeclaration n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - n.f7.accept(this, argu); - return _ret; - } - - /** - * f0 -> Type() - * f1 -> Identifier() - * f2 -> ";" - */ - public R visit(VarDeclaration n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; - } - - /** - * f0 -> "public" - * f1 -> Type() - * f2 -> Identifier() - * f3 -> "(" - * f4 -> ( FormalParameterList() )? - * f5 -> ")" - * f6 -> "{" - * f7 -> ( VarDeclaration() )* - * f8 -> ( Statement() )* - * f9 -> "return" - * f10 -> Expression() - * f11 -> ";" - * f12 -> "}" - */ - public R visit(MethodDeclaration n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - n.f7.accept(this, argu); - n.f8.accept(this, argu); - n.f9.accept(this, argu); - n.f10.accept(this, argu); - n.f11.accept(this, argu); - n.f12.accept(this, argu); - return _ret; - } - - /** - * f0 -> FormalParameter() - * f1 -> ( FormalParameterRest() )* - */ - public R visit(FormalParameterList n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - return _ret; - } - - /** - * f0 -> Type() - * f1 -> Identifier() - */ - public R visit(FormalParameter n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - return _ret; - } - - /** - * f0 -> "," - * f1 -> FormalParameter() - */ - public R visit(FormalParameterRest n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - return _ret; - } - - /** - * f0 -> ArrayType() - * | BooleanType() - * | IntegerType() - * | Identifier() - */ - public R visit(Type n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> "int" - * f1 -> "[" - * f2 -> "]" - */ - public R visit(ArrayType n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; - } - - /** - * f0 -> "boolean" - */ - public R visit(BooleanType n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> "int" - */ - public R visit(IntegerType n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> Block() - * | AssignmentStatement() - * | ArrayAssignmentStatement() - * | IfStatement() - * | WhileStatement() - * | PrintStatement() - */ - public R visit(Statement n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> "{" - * f1 -> ( Statement() )* - * f2 -> "}" - */ - public R visit(Block n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; - } - - /** - * f0 -> Identifier() - * f1 -> "=" - * f2 -> Expression() - * f3 -> ";" - */ - public R visit(AssignmentStatement n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - return _ret; - } - - /** - * f0 -> Identifier() - * f1 -> "[" - * f2 -> Expression() - * f3 -> "]" - * f4 -> "=" - * f5 -> Expression() - * f6 -> ";" - */ - public R visit(ArrayAssignmentStatement n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - return _ret; - } - - /** - * f0 -> "if" - * f1 -> "(" - * f2 -> Expression() - * f3 -> ")" - * f4 -> Statement() - * f5 -> "else" - * f6 -> Statement() - */ - public R visit(IfStatement n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - return _ret; - } - - /** - * f0 -> "while" - * f1 -> "(" - * f2 -> Expression() - * f3 -> ")" - * f4 -> Statement() - */ - public R visit(WhileStatement n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - return _ret; - } - - /** - * f0 -> "System.out.println" - * f1 -> "(" - * f2 -> Expression() - * f3 -> ")" - * f4 -> ";" - */ - public R visit(PrintStatement n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - return _ret; - } - - /** - * f0 -> AndExpression() - * | CompareExpression() - * | PlusExpression() - * | MinusExpression() - * | TimesExpression() - * | ArrayLookup() - * | ArrayLength() - * | MessageSend() - * | PrimaryExpression() - */ - public R visit(Expression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "&&" - * f2 -> PrimaryExpression() - */ - public R visit(AndExpression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "<" - * f2 -> PrimaryExpression() - */ - public R visit(CompareExpression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "+" - * f2 -> PrimaryExpression() - */ - public R visit(PlusExpression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "-" - * f2 -> PrimaryExpression() - */ - public R visit(MinusExpression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "*" - * f2 -> PrimaryExpression() - */ - public R visit(TimesExpression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "[" - * f2 -> PrimaryExpression() - * f3 -> "]" - */ - public R visit(ArrayLookup n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "." - * f2 -> "length" - */ - public R visit(ArrayLength n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; - } - - /** - * f0 -> PrimaryExpression() - * f1 -> "." - * f2 -> Identifier() - * f3 -> "(" - * f4 -> ( ExpressionList() )? - * f5 -> ")" - */ - public R visit(MessageSend n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - return _ret; - } - - /** - * f0 -> Expression() - * f1 -> ( ExpressionRest() )* - */ - public R visit(ExpressionList n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - return _ret; - } - - /** - * f0 -> "," - * f1 -> Expression() - */ - public R visit(ExpressionRest n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - return _ret; - } - - /** - * f0 -> IntegerLiteral() - * | TrueLiteral() - * | FalseLiteral() - * | Identifier() - * | ThisExpression() - * | ArrayAllocationExpression() - * | AllocationExpression() - * | NotExpression() - * | BracketExpression() - */ - public R visit(PrimaryExpression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> - */ - public R visit(IntegerLiteral n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> "true" - */ - public R visit(TrueLiteral n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> "false" - */ - public R visit(FalseLiteral n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> - */ - public R visit(Identifier n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> "this" - */ - public R visit(ThisExpression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - return _ret; - } - - /** - * f0 -> "new" - * f1 -> "int" - * f2 -> "[" - * f3 -> Expression() - * f4 -> "]" - */ - public R visit(ArrayAllocationExpression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - return _ret; - } - - /** - * f0 -> "new" - * f1 -> Identifier() - * f2 -> "(" - * f3 -> ")" - */ - public R visit(AllocationExpression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - return _ret; - } - - /** - * f0 -> "!" - * f1 -> Expression() - */ - public R visit(NotExpression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - return _ret; - } - - /** - * f0 -> "(" - * f1 -> Expression() - * f2 -> ")" - */ - public R visit(BracketExpression n, A argu) { - R _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; - } - -} -- cgit v1.2.3