From ffaf9068b5b6b06866bee46a83abb9e3c00b1b85 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Sat, 30 Oct 2004 21:01:37 +0000 Subject: [PATCH] Completed support for generating C code. --- .../RepairCompiler/MCC/CRuntime/buildruntime | 3 +- .../MCC/IR/BooleanLiteralExpr.java | 3 +- Repair/RepairCompiler/MCC/IR/CastExpr.java | 3 +- Repair/RepairCompiler/MCC/IR/CodeWriter.java | 4 + Repair/RepairCompiler/MCC/IR/DotExpr.java | 19 +- .../RepairCompiler/MCC/IR/ElementOfExpr.java | 3 +- .../RepairCompiler/MCC/IR/ForQuantifier.java | 7 +- .../RepairCompiler/MCC/IR/ImageSetExpr.java | 39 ++-- .../MCC/IR/IntegerLiteralExpr.java | 3 +- .../RepairCompiler/MCC/IR/LogicStatement.java | 8 +- Repair/RepairCompiler/MCC/IR/OpExpr.java | 30 ++- .../RepairCompiler/MCC/IR/PrintWrapper.java | 17 +- .../RepairCompiler/MCC/IR/RelationExpr.java | 5 +- .../MCC/IR/RelationFunctionExpr.java | 8 +- .../MCC/IR/RelationQuantifier.java | 26 ++- .../MCC/IR/RepairGenerator.java | 213 +++++++++++------- Repair/RepairCompiler/MCC/IR/SetExpr.java | 6 +- .../RepairCompiler/MCC/IR/SetQuantifier.java | 15 +- .../RepairCompiler/MCC/IR/SizeofFunction.java | 8 +- Repair/RepairCompiler/MCC/IR/Sources.java | 3 +- .../MCC/IR/StandardCodeWriter.java | 47 ++-- .../MCC/IR/StructureGenerator.java | 8 +- Repair/RepairCompiler/MCC/IR/SumExpr.java | 11 +- .../MCC/IR/TokenLiteralExpr.java | 3 +- Repair/RepairCompiler/MCC/IR/TupleOfExpr.java | 4 +- Repair/RepairCompiler/MCC/IR/UpdateNode.java | 29 ++- Repair/RepairCompiler/MCC/IR/VarExpr.java | 18 +- 27 files changed, 343 insertions(+), 200 deletions(-) diff --git a/Repair/RepairCompiler/MCC/CRuntime/buildruntime b/Repair/RepairCompiler/MCC/CRuntime/buildruntime index c056fb6..3f66d45 100755 --- a/Repair/RepairCompiler/MCC/CRuntime/buildruntime +++ b/Repair/RepairCompiler/MCC/CRuntime/buildruntime @@ -5,4 +5,5 @@ gcc $FLAG -c tmap.c gcc $FLAG -c instrument.c gcc $FLAG -c libredblack/redblack.c gcc $FLAG -c stack.c - +gcc $FLAG -c size.c +gcc $FLAG -c cache_aux.c diff --git a/Repair/RepairCompiler/MCC/IR/BooleanLiteralExpr.java b/Repair/RepairCompiler/MCC/IR/BooleanLiteralExpr.java index 7273818..da76e0e 100755 --- a/Repair/RepairCompiler/MCC/IR/BooleanLiteralExpr.java +++ b/Repair/RepairCompiler/MCC/IR/BooleanLiteralExpr.java @@ -35,7 +35,8 @@ public class BooleanLiteralExpr extends LiteralExpr { } public void generate(CodeWriter writer, VarDescriptor dest) { - writer.outputline("int " + dest.getSafeSymbol() + " = " + (value ? "1" : "0") + ";"); + writer.addDeclaration("int", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = " + (value ? "1" : "0") + ";"); } public void prettyPrint(PrettyPrinter pp) { diff --git a/Repair/RepairCompiler/MCC/IR/CastExpr.java b/Repair/RepairCompiler/MCC/IR/CastExpr.java index 263460f..c72590f 100755 --- a/Repair/RepairCompiler/MCC/IR/CastExpr.java +++ b/Repair/RepairCompiler/MCC/IR/CastExpr.java @@ -73,7 +73,8 @@ public class CastExpr extends Expr { public void generate(CodeWriter writer, VarDescriptor dest) { VarDescriptor vd = VarDescriptor.makeNew("expr"); expr.generate(writer, vd); - writer.outputline("int " + dest.getSafeSymbol() + " = (int) " + vd.getSafeSymbol() + ";"); + writer.addDeclaration("int", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = (int) " + vd.getSafeSymbol() + ";"); } public void prettyPrint(PrettyPrinter pp) { diff --git a/Repair/RepairCompiler/MCC/IR/CodeWriter.java b/Repair/RepairCompiler/MCC/IR/CodeWriter.java index 9118096..1722980 100755 --- a/Repair/RepairCompiler/MCC/IR/CodeWriter.java +++ b/Repair/RepairCompiler/MCC/IR/CodeWriter.java @@ -8,6 +8,10 @@ public interface CodeWriter extends PrettyPrinter{ public void startblock(); public void endblock(); + public void startBuffer(); + public void emptyBuffer(); + public void addDeclaration(String type, String varname); + public void addDeclaration(String function); public void pushSymbolTable(SymbolTable st); public SymbolTable popSymbolTable(); public SymbolTable getSymbolTable(); diff --git a/Repair/RepairCompiler/MCC/IR/DotExpr.java b/Repair/RepairCompiler/MCC/IR/DotExpr.java index be43415..4f550a4 100755 --- a/Repair/RepairCompiler/MCC/IR/DotExpr.java +++ b/Repair/RepairCompiler/MCC/IR/DotExpr.java @@ -167,8 +167,8 @@ public class DotExpr extends Expr { if (writer.getInvariantValue()!=null&& writer.getInvariantValue().isInvariant(this)) { - writer.outputline(getType().getGenerateType().getSafeSymbol()+ - " "+dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";"); + writer.addDeclaration(getType().getGenerateType().getSafeSymbol().toString(), dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";"); writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";"); return; } @@ -200,7 +200,8 @@ public class DotExpr extends Expr { boolean doboundscheck=true; boolean performedboundscheck=false; - writer.outputline(getType().getGenerateType() + " " + dest.getSafeSymbol()+"=0;"); + writer.addDeclaration(getType().getGenerateType().toString(),dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol()+"=0;"); if (intindex != null) { if (intindex instanceof IntegerLiteralExpr && ((IntegerLiteralExpr) intindex).getValue() == 0) { @@ -261,11 +262,13 @@ public class DotExpr extends Expr { /* derive offset in bytes */ VarDescriptor offset = VarDescriptor.makeNew("offset"); - writer.outputline("int " + offset.getSafeSymbol() + " = " + ob.getSafeSymbol() + " >> 3;"); + writer.addDeclaration("int", offset.getSafeSymbol()); + writer.outputline(offset.getSafeSymbol() + " = " + ob.getSafeSymbol() + " >> 3;"); if (fd.getType() instanceof ReservedTypeDescriptor && !fd.getPtr()) { VarDescriptor shift = VarDescriptor.makeNew("shift"); - writer.outputline("int " + shift.getSafeSymbol() + " = " + ob.getSafeSymbol() + + writer.addDeclaration("int", shift.getSafeSymbol()); + writer.outputline(shift.getSafeSymbol() + " = " + ob.getSafeSymbol() + " - (" + offset.getSafeSymbol() + " << 3);"); int mask = bitmask(((IntegerLiteralExpr)fd.getType().getSizeExpr()).getValue()); @@ -287,10 +290,12 @@ public class DotExpr extends Expr { writer.startblock(); VarDescriptor typevar=VarDescriptor.makeNew("typechecks"); if (DOMEMCHECKS&&(!DOTYPECHECKS)) { - writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");"); + writer.addDeclaration("bool", typevar.getSafeSymbol()); + writer.outputline(typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");"); dotypecheck = true; } else if (DOTYPECHECKS) { - writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");"); + writer.addDeclaration("bool", typevar.getSafeSymbol()); + writer.outputline(typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + this.td.getId() + ");"); } if (DOTYPECHECKS||DOMEMCHECKS) { diff --git a/Repair/RepairCompiler/MCC/IR/ElementOfExpr.java b/Repair/RepairCompiler/MCC/IR/ElementOfExpr.java index 6de61f4..3799435 100755 --- a/Repair/RepairCompiler/MCC/IR/ElementOfExpr.java +++ b/Repair/RepairCompiler/MCC/IR/ElementOfExpr.java @@ -55,7 +55,8 @@ public class ElementOfExpr extends Expr { public void generate(CodeWriter writer, VarDescriptor dest) { VarDescriptor ed = VarDescriptor.makeNew("element"); element.generate(writer, ed); - writer.outputline("int " + dest.getSafeSymbol() + " = SimpleHashcontainskey("+set.getSafeSymbol() +"_hash ,"+ ed.getSafeSymbol() + ");"); + writer.addDeclaration("int", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = SimpleHashcontainskey("+set.getSafeSymbol() +"_hash ,"+ ed.getSafeSymbol() + ");"); } public void prettyPrint(PrettyPrinter pp) { diff --git a/Repair/RepairCompiler/MCC/IR/ForQuantifier.java b/Repair/RepairCompiler/MCC/IR/ForQuantifier.java index 317d12c..20a0356 100755 --- a/Repair/RepairCompiler/MCC/IR/ForQuantifier.java +++ b/Repair/RepairCompiler/MCC/IR/ForQuantifier.java @@ -39,8 +39,8 @@ public class ForQuantifier extends Quantifier { VarDescriptor ud = VarDescriptor.makeNew(); lower.generate(writer, ld); upper.generate(writer, ud); - - writer.outputline("for (int " + var.getSafeSymbol() + " = " + ld.getSafeSymbol() + "; " + var.getSafeSymbol() + " <= " + ud.getSafeSymbol() + "; " + var.getSafeSymbol() + "++)"); + writer.addDeclaration("int",var.getSafeSymbol()); + writer.outputline("for (" + var.getSafeSymbol() + " = " + ld.getSafeSymbol() + "; " + var.getSafeSymbol() + " <= " + ud.getSafeSymbol() + "; " + var.getSafeSymbol() + "++)"); writer.startblock(); } @@ -51,7 +51,8 @@ left,String right) { public int generate_worklistload(CodeWriter writer, int offset) { String varname = var.getSafeSymbol(); - writer.outputline("int " + varname + " = wi->word" + offset + ";"); + writer.addDeclaration("int",varname); + writer.outputline(varname + " = wi->word" + offset + ";"); return offset + 1; } diff --git a/Repair/RepairCompiler/MCC/IR/ImageSetExpr.java b/Repair/RepairCompiler/MCC/IR/ImageSetExpr.java index 7c5dc06..a6d2f1a 100755 --- a/Repair/RepairCompiler/MCC/IR/ImageSetExpr.java +++ b/Repair/RepairCompiler/MCC/IR/ImageSetExpr.java @@ -124,12 +124,14 @@ public class ImageSetExpr extends SetExpr { public void generate_inclusion(CodeWriter writer, VarDescriptor dest, VarDescriptor element) { String hash = inverse ? "_hashinv, " : "_hash, " ; if (!isimageset) { - writer.outputline("int " + dest.getSafeSymbol() + " = SimpleHashcontainskeydata(" + rd.getSafeSymbol() + hash + vd.getSafeSymbol() + ", " + element.getSafeSymbol() + ");"); + writer.addDeclaration("int", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = SimpleHashcontainskeydata(" + rd.getSafeSymbol() + hash + vd.getSafeSymbol() + ", " + element.getSafeSymbol() + ");"); } else { VarDescriptor newset=VarDescriptor.makeNew("newset"); generate_set(writer,newset); - writer.outputline("int "+dest.getSafeSymbol()+"=SimpleHashcontainskey("+newset.getSafeSymbol()+","+element.getSafeSymbol()+");"); - writer.outputline("delete "+newset.getSafeSymbol()+";"); + writer.addDeclaration("int", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol()+"=SimpleHashcontainskey("+newset.getSafeSymbol()+","+element.getSafeSymbol()+");"); + writer.outputline("freeSimpleHash("+newset.getSafeSymbol()+");"); } } @@ -138,47 +140,54 @@ public class ImageSetExpr extends SetExpr { assert rd != null; if (!isimageset) { String hash = inverse ? "_hashinv, " : "_hash, " ; - writer.outputline("int " + dest.getSafeSymbol() + " = SimpleHashcount(" + rd.getSafeSymbol() + hash + vd.getSafeSymbol() + ");"); + writer.addDeclaration("int", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = SimpleHashcount(" + rd.getSafeSymbol() + hash + vd.getSafeSymbol() + ");"); } else { VarDescriptor newset=VarDescriptor.makeNew("newset"); generate_set(writer,newset); - writer.outputline("int "+dest.getSafeSymbol()+"=SimpleHashcountset("+newset.getSafeSymbol()+");"); - writer.outputline("delete "+newset.getSafeSymbol()+";"); + writer.addDeclaration("int", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol()+"=SimpleHashcountset("+newset.getSafeSymbol()+");"); + writer.outputline("freeSimpleHash("+newset.getSafeSymbol()+");"); } } public void generate_leftside(CodeWriter writer, VarDescriptor dest) { if (!isimageset) { - writer.outputline(vd.getType().getGenerateType()+" "+dest.getSafeSymbol()+" = "+vd.getSafeSymbol()+";"); + writer.addDeclaration(vd.getType().getGenerateType().toString(), dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol()+" = "+vd.getSafeSymbol()+";"); } else { VarDescriptor iseset=VarDescriptor.makeNew("set"); ise.generate_set(writer,iseset); - writer.outputline("int "+dest.getSafeSymbol()+" = SimpleHashfirstkey("+iseset.getSafeSymbol()+");"); - writer.outputline("delete "+iseset.getSafeSymbol()+";"); + writer.addDeclaration("int",dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol()+" = SimpleHashfirstkey("+iseset.getSafeSymbol()+");"); + writer.outputline("freeSimpleHash("+iseset.getSafeSymbol()+");"); } } public void generate_set(CodeWriter writer, VarDescriptor dest) { if (!isimageset) { String hash = inverse ? "_hashinv, " : "_hash, " ; - writer.outputline("struct SimpleHash * "+dest.getSafeSymbol()+"=SimpleHashimageSet("+rd.getSafeSymbol()+hash+vd.getSafeSymbol()+");"); + writer.addDeclaration("struct SimpleHash *",dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol()+"=SimpleHashimageSet("+rd.getSafeSymbol()+hash+vd.getSafeSymbol()+");"); } else { VarDescriptor iseset=VarDescriptor.makeNew("set"); ise.generate_set(writer,iseset); VarDescriptor itvd=VarDescriptor.makeNew("iterator"); - writer.outputline("struct SimpleIterator "+itvd.getSafeSymbol()+";"); + writer.addDeclaration("struct SimpleIterator",itvd.getSafeSymbol()); writer.outputline("SimpleHashiterator("+iseset.getSafeSymbol()+",&"+itvd.getSafeSymbol()+");"); - - writer.outputline("struct SimpleHash *"+dest.getSafeSymbol()+"=allocateSimpleHash(10);"); + writer.addDeclaration("struct SimpleHash *", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol()+"=allocateSimpleHash(10);"); writer.outputline("while (hasNext(&"+itvd.getSafeSymbol()+")) {"); VarDescriptor keyvd=VarDescriptor.makeNew("key"); - writer.outputline("int "+keyvd.getSafeSymbol()+"=next(&"+itvd.getSafeSymbol()+");"); + writer.addDeclaration("int",keyvd.getSafeSymbol()); + writer.outputline(keyvd.getSafeSymbol()+"=next(&"+itvd.getSafeSymbol()+");"); String hash = inverse ? "_hashinv, " : "_hash, " ; VarDescriptor newset=VarDescriptor.makeNew("newset"); - writer.outputline("SimpleHash * "+newset.getSafeSymbol()+"=SimpleHashimageSet("+rd.getSafeSymbol()+hash+keyvd.getSafeSymbol()+");"); + writer.addDeclaration("struct SimpleHash *", newset.getSafeSymbol()); + writer.outputline(newset.getSafeSymbol()+"=SimpleHashimageSet("+rd.getSafeSymbol()+hash+keyvd.getSafeSymbol()+");"); writer.outputline("SimpleHashaddAll("+dest.getSafeSymbol()+", "+ newset.getSafeSymbol()+");"); writer.outputline("freeSimpleHash("+newset.getSafeSymbol()+");"); writer.outputline("}"); diff --git a/Repair/RepairCompiler/MCC/IR/IntegerLiteralExpr.java b/Repair/RepairCompiler/MCC/IR/IntegerLiteralExpr.java index be4db26..e1c91c2 100755 --- a/Repair/RepairCompiler/MCC/IR/IntegerLiteralExpr.java +++ b/Repair/RepairCompiler/MCC/IR/IntegerLiteralExpr.java @@ -35,7 +35,8 @@ public class IntegerLiteralExpr extends LiteralExpr { } public void generate(CodeWriter writer, VarDescriptor dest) { - writer.outputline("int " + dest.getSafeSymbol() + " = " + value + ";"); + writer.addDeclaration("int", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = " + value + ";"); } public void prettyPrint(PrettyPrinter pp) { diff --git a/Repair/RepairCompiler/MCC/IR/LogicStatement.java b/Repair/RepairCompiler/MCC/IR/LogicStatement.java index 7d520cd..b750ffd 100755 --- a/Repair/RepairCompiler/MCC/IR/LogicStatement.java +++ b/Repair/RepairCompiler/MCC/IR/LogicStatement.java @@ -103,7 +103,7 @@ public class LogicStatement { public void generate(CodeWriter writer, VarDescriptor dest) { - writer.outputline("int " + dest.getSafeSymbol() + ";"); + writer.addDeclaration("int", dest.getSafeSymbol()); if (op == NOT) { @@ -121,13 +121,15 @@ public class LogicStatement { VarDescriptor leftd = VarDescriptor.makeNew("leftboolean"); String lm = (VarDescriptor.makeNew("leftmaybe")).getSafeSymbol(); left.generate(writer, leftd); - writer.outputline("int " + lm + " = maybe;"); + writer.addDeclaration("int", lm); + writer.outputline(lm + " = maybe;"); writer.outputline("maybe=0;"); VarDescriptor rightd = VarDescriptor.makeNew("rightboolean"); String rm = (VarDescriptor.makeNew("rightmaybe")).getSafeSymbol(); assert right != null; right.generate(writer, rightd); - writer.outputline("int " + rm + " = maybe;"); + writer.addDeclaration("int", rm); + writer.outputline(rm + " = maybe;"); String l = leftd.getSafeSymbol(); String r = rightd.getSafeSymbol(); diff --git a/Repair/RepairCompiler/MCC/IR/OpExpr.java b/Repair/RepairCompiler/MCC/IR/OpExpr.java index 0c5082b..c5f6b27 100755 --- a/Repair/RepairCompiler/MCC/IR/OpExpr.java +++ b/Repair/RepairCompiler/MCC/IR/OpExpr.java @@ -351,7 +351,8 @@ public class OpExpr extends Expr { /* Check for loop invariant hoisting. */ if (writer.getInvariantValue()!=null&& writer.getInvariantValue().isInvariant(this)) { - writer.outputline("int "+dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";"); + writer.addDeclaration("int",dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";"); writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";"); return; } @@ -364,7 +365,8 @@ public class OpExpr extends Expr { if (right != null) { if ((opcode==Opcode.OR)|| (opcode==Opcode.AND)) { - writer.outputline("int "+lm.getSafeSymbol()+"=maybe;"); + writer.addDeclaration("int",lm.getSafeSymbol()); + writer.outputline(lm.getSafeSymbol()+"=maybe;"); writer.outputline("maybe=0;"); } @@ -374,27 +376,35 @@ public class OpExpr extends Expr { String code; if (opcode == Opcode.RND) { - writer.outputline("int " +dest.getSafeSymbol() + " = (" + + writer.addDeclaration("int",dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = (" + ld.getSafeSymbol() + ">>3)<<3; "); writer.outputline("if ("+ld.getSafeSymbol()+" % 8) "+dest.getSafeSymbol()+"+=8;"); } else if (opcode == Opcode.NOP) { - writer.outputline("int " +dest.getSafeSymbol() + " = " + + writer.addDeclaration("int", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() +"; "); } else if (opcode == Opcode.AND) { - writer.outputline("int "+rm.getSafeSymbol()+"=maybe;"); + writer.addDeclaration("int",rm.getSafeSymbol()); + writer.outputline(rm.getSafeSymbol()+"=maybe;"); writer.outputline("maybe = (" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (" + rd.getSafeSymbol() + " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");"); - writer.outputline("int "+dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " && " + rd.getSafeSymbol() + ";"); + writer.addDeclaration("int",dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " && " + rd.getSafeSymbol() + ";"); } else if (opcode == Opcode.OR) { - writer.outputline("int "+rm.getSafeSymbol()+"=maybe;"); + writer.addDeclaration("int",rm.getSafeSymbol()); + writer.outputline(rm.getSafeSymbol()+"=maybe;"); writer.outputline("maybe = (!" + ld.getSafeSymbol() + " && " + rm.getSafeSymbol() + ") || (!" + rd.getSafeSymbol() + " && " + lm.getSafeSymbol() + ") || (" + lm.getSafeSymbol() + " && " + rm.getSafeSymbol() + ");"); - writer.outputline("int "+dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() + ";"); + writer.addDeclaration("int",dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " || " + rd.getSafeSymbol() + ";"); } else if (opcode != Opcode.NOT) { /* two operands */ assert rd != null; - writer.outputline("int " + dest.getSafeSymbol() + " = " + + writer.addDeclaration("int", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = " + ld.getSafeSymbol() + " " + opcode.toString() + " " + rd.getSafeSymbol() + ";"); } else if (opcode == Opcode.NOT) { - writer.outputline("int " + dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";"); + writer.addDeclaration("int", dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = !" + ld.getSafeSymbol() + ";"); } } diff --git a/Repair/RepairCompiler/MCC/IR/PrintWrapper.java b/Repair/RepairCompiler/MCC/IR/PrintWrapper.java index 8a649b5..a855aaf 100755 --- a/Repair/RepairCompiler/MCC/IR/PrintWrapper.java +++ b/Repair/RepairCompiler/MCC/IR/PrintWrapper.java @@ -3,16 +3,17 @@ import java.util.*; public class PrintWrapper { java.io.PrintWriter output; - String buffered=""; + StringBuffer buffered=new StringBuffer(""); boolean buffer=false; Hashtable vartable=new Hashtable(); + public int indent=0; public PrintWrapper(java.io.PrintWriter output) { this.output=output; } void print(String s) { if (buffer) - buffered+=s; + buffered.append(s); else output.print(s); } @@ -22,7 +23,7 @@ public class PrintWrapper { } void println(String s) { if (buffer) - buffered+=s+"\n"; + buffered.append(s+"\n"); else output.println(s); } @@ -35,8 +36,8 @@ public class PrintWrapper { String var=(String)it.next(); output.println(((String)vartable.get(var))+" "+var+";"); } - output.print(buffered); - buffered=""; + output.print(buffered.toString()); + buffered=new StringBuffer(""); vartable=new Hashtable(); buffer=false; } @@ -53,4 +54,10 @@ public class PrintWrapper { } else output.println(type+" "+varname+";"); } + void addDeclaration(String f) { + if (buffer) { + buffered.insert(0,f+"\n"); + } else + output.println(f); + } } diff --git a/Repair/RepairCompiler/MCC/IR/RelationExpr.java b/Repair/RepairCompiler/MCC/IR/RelationExpr.java index 4eefc70..4026b31 100755 --- a/Repair/RepairCompiler/MCC/IR/RelationExpr.java +++ b/Repair/RepairCompiler/MCC/IR/RelationExpr.java @@ -106,8 +106,9 @@ public class RelationExpr extends Expr { String strinverse = inverse ? "inv" : ""; String found = (VarDescriptor.makeNew("found")).getSafeSymbol(); expr.generate(writer, domain); - writer.outputline(relation.getRange().getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() + ";"); - writer.outputline("int "+found+" = SimpleHashget(" +relation.getSafeSymbol()+"_hash"+strinverse+", "+ domain.getSafeSymbol() + ", & " + dest.getSafeSymbol() + ");"); + writer.addDeclaration(relation.getRange().getType().getGenerateType().getSafeSymbol(), dest.getSafeSymbol()); + writer.addDeclaration("int",found); + writer.outputline(found+" = SimpleHashget(" +relation.getSafeSymbol()+"_hash"+strinverse+", "+ domain.getSafeSymbol() + ", & " + dest.getSafeSymbol() + ");"); writer.outputline("if (!" + found + ") { maybe = 1; }"); } diff --git a/Repair/RepairCompiler/MCC/IR/RelationFunctionExpr.java b/Repair/RepairCompiler/MCC/IR/RelationFunctionExpr.java index 2ae18f9..d5b3cf0 100755 --- a/Repair/RepairCompiler/MCC/IR/RelationFunctionExpr.java +++ b/Repair/RepairCompiler/MCC/IR/RelationFunctionExpr.java @@ -49,7 +49,7 @@ public class RelationFunctionExpr extends Expr { public void generate(CodeWriter cr, VarDescriptor dest) { String destname = dest.getSafeSymbol(); - cr.outputline("int " + destname + ";"); + cr.addDeclaration("int", destname); // ok... destination is declared... we gotta expand this rule inplace... and instead of the inclusion we // set the destination in the guard ... otherwise maybe! @@ -66,8 +66,10 @@ public class RelationFunctionExpr extends Expr { String tempvar = (VarDescriptor.makeNew("tempvar")).getSafeSymbol(); // this is to be safe about name overlap because int t = t; sets t to 0! - cr.outputline("int " + tempvar + " = " + domain.getSafeSymbol() + ";"); - cr.outputline("int " + rulebinding.getSafeSymbol() + " = " + tempvar + ";"); + cr.addDeclaration("int", tempvar); + cr.outputline(tempvar + " = " + domain.getSafeSymbol() + ";"); + cr.addDeclaration("int", rulebinding.getSafeSymbol()); + cr.outputline(rulebinding.getSafeSymbol() + " = " + tempvar + ";"); /* pretty print! */ cr.outputline("/* about to inbed relational function*/"); diff --git a/Repair/RepairCompiler/MCC/IR/RelationQuantifier.java b/Repair/RepairCompiler/MCC/IR/RelationQuantifier.java index e759aa7..858f338 100755 --- a/Repair/RepairCompiler/MCC/IR/RelationQuantifier.java +++ b/Repair/RepairCompiler/MCC/IR/RelationQuantifier.java @@ -33,25 +33,29 @@ public class RelationQuantifier extends Quantifier { } public void generate_open(CodeWriter writer) { - writer.outputline("struct SimpleIterator "+x.getSafeSymbol()+"_iterator;"); - writer.outputline("for (SimpleHashiterator("+relation.getSafeSymbol()+"_hash, "+x.getSafeSymbol()+"_iterator); hasNext("+x.getSafeSymbol()+"_iterator); )"); + writer.addDeclaration("struct SimpleIterator",x.getSafeSymbol()+"_iterator"); + writer.outputline("for (SimpleHashiterator("+relation.getSafeSymbol()+"_hash, &"+x.getSafeSymbol()+"_iterator); hasNext(&"+x.getSafeSymbol()+"_iterator); )"); writer.startblock(); - writer.outputline(y.getType().getGenerateType() + " " + y.getSafeSymbol() + " = (" + y.getType().getGenerateType() + ") next("+x.getSafeSymbol()+"_iterator);"); + writer.addDeclaration(y.getType().getGenerateType().toString(), y.getSafeSymbol()); + writer.outputline(y.getSafeSymbol() + " = (" + y.getType().getGenerateType() + ") next(&"+x.getSafeSymbol()+"_iterator);"); // #ATTN#: key is called second because next() forwards ptr and key does not! - writer.outputline(x.getType().getGenerateType() + " " + x.getSafeSymbol() + " = (" + x.getType().getGenerateType() + ") key("+x.getSafeSymbol()+"_iterator);"); + writer.addDeclaration(x.getType().getGenerateType().toString(), x.getSafeSymbol()); + writer.outputline(x.getSafeSymbol() + " = (" + x.getType().getGenerateType() + ") key(&"+x.getSafeSymbol()+"_iterator);"); } public void generate_open(CodeWriter writer, String type,int number, String left,String right) { VarDescriptor tmp=VarDescriptor.makeNew("flag"); - writer.outputline("struct SimpleIterator * " + x.getSafeSymbol() + "_iterator = SimpleHashcreateiterator("+relation.getSafeSymbol()+"_hash);"); - writer.outputline("int "+tmp.getSafeSymbol()+"=0;"); + writer.addDeclaration("struct SimpleIterator", x.getSafeSymbol() + "_iterator"); + writer.outputline("SimpleHashiterator("+relation.getSafeSymbol()+"_hash,& "+x.getSafeSymbol()+"_iterator);"); + writer.addDeclaration("int",tmp.getSafeSymbol()); + writer.outputline(tmp.getSafeSymbol()+"=0;"); writer.outputline("if ("+type+"=="+number+")"); writer.outputline(tmp.getSafeSymbol()+"=1;"); - writer.outputline("while("+tmp.getSafeSymbol()+"||(("+type+"!="+number+")&& hasNext("+x.getSafeSymbol()+"_iterator)))"); + writer.outputline("while("+tmp.getSafeSymbol()+"||(("+type+"!="+number+")&& hasNext(&"+x.getSafeSymbol()+"_iterator)))"); writer.startblock(); - writer.outputline(x.getType().getGenerateType() + " " + x.getSafeSymbol() + ";"); - writer.outputline(y.getType().getGenerateType() + " " + y.getSafeSymbol() + ";"); + writer.addDeclaration(x.getType().getGenerateType().toString(), x.getSafeSymbol()); + writer.addDeclaration(y.getType().getGenerateType().toString(), y.getSafeSymbol()); writer.outputline("if ("+type+"=="+number+")"); writer.startblock(); writer.outputline(tmp.getSafeSymbol()+"=0;"); @@ -60,8 +64,8 @@ public class RelationQuantifier extends Quantifier { writer.endblock(); writer.outputline("else"); writer.startblock(); - writer.outputline(y.getSafeSymbol() + " = (" + y.getType().getGenerateType() + ") next("+x.getSafeSymbol()+"_iterator);"); - writer.outputline(x.getSafeSymbol() + " = (" + x.getType().getGenerateType() + ") key("+x.getSafeSymbol()+"_iterator);"); + writer.outputline(y.getSafeSymbol() + " = (" + y.getType().getGenerateType() + ") next(&"+x.getSafeSymbol()+"_iterator);"); + writer.outputline(x.getSafeSymbol() + " = (" + x.getType().getGenerateType() + ") key(&"+x.getSafeSymbol()+"_iterator);"); writer.endblock(); } diff --git a/Repair/RepairCompiler/MCC/IR/RepairGenerator.java b/Repair/RepairCompiler/MCC/IR/RepairGenerator.java index 332e58c..b0bd167 100755 --- a/Repair/RepairCompiler/MCC/IR/RepairGenerator.java +++ b/Repair/RepairCompiler/MCC/IR/RepairGenerator.java @@ -7,9 +7,9 @@ import MCC.Compiler; public class RepairGenerator { State state; - java.io.PrintWriter outputrepair = null; - java.io.PrintWriter outputaux = null; - java.io.PrintWriter outputhead = null; + PrintWrapper outputrepair = null; + PrintWrapper outputaux = null; + PrintWrapper outputhead = null; String name="foo"; String headername; static VarDescriptor oldmodel=null; @@ -74,9 +74,9 @@ public class RepairGenerator { } public void generate(OutputStream outputrepair, OutputStream outputaux,OutputStream outputhead, String st) { - this.outputrepair = new java.io.PrintWriter(outputrepair, true); - this.outputaux = new java.io.PrintWriter(outputaux, true); - this.outputhead = new java.io.PrintWriter(outputhead, true); + this.outputrepair = new PrintWrapper(new java.io.PrintWriter(outputrepair, true)); + this.outputaux = new PrintWrapper(new java.io.PrintWriter(outputaux, true)); + this.outputhead = new PrintWrapper(new java.io.PrintWriter(outputhead, true)); headername=st; name_updates(); @@ -107,7 +107,8 @@ public class RepairGenerator { generate_teardown(); CodeWriter crhead = new StandardCodeWriter(this.outputhead); craux = new StandardCodeWriter(this.outputaux); - craux.outputline("}"); + craux.emptyBuffer(); + craux.endblock(); if (Compiler.GENERATEDEBUGHOOKS) { crhead.outputline("void debughook();"); @@ -166,7 +167,9 @@ public class RepairGenerator { craux.outputline("void "+methodname+"(struct "+name+"_state * "+ststate+", struct "+name+" * "+stmodel+", struct RepairHash * "+strepairtable+", int "+stleft+")"); } craux.startblock(); - craux.outputline("int maybe=0;"); + craux.startBuffer(); + craux.addDeclaration("int","maybe"); + craux.outputline("maybe=0;"); if (Compiler.GENERATEINSTRUMENT) craux.outputline("updatecount++;"); @@ -176,6 +179,7 @@ public class RepairGenerator { }; un.generate(cr, false, false, stleft,stright, null,this); craux.outputline("if (maybe) printf(\"REALLY BAD\");"); + craux.emptyBuffer(); craux.endblock(); break; case MultUpdateNode.REMOVE: { @@ -200,7 +204,9 @@ public class RepairGenerator { crhead.outputline(methodcall+";"); craux.outputline(methodcall); craux.startblock(); - craux.outputline("int maybe=0;"); + craux.startBuffer(); + craux.addDeclaration("int","maybe"); + craux.outputline("maybe=0;"); if (Compiler.GENERATEINSTRUMENT) craux.outputline("updatecount++;"); final SymbolTable st2 = un.getRule().getSymbolTable(); @@ -209,6 +215,7 @@ public class RepairGenerator { }; un.generate(cr2, true, false, null,null, null,this); craux.outputline("if (maybe) printf(\"REALLY BAD\");"); + craux.emptyBuffer(); craux.endblock(); } break; @@ -235,6 +242,7 @@ public class RepairGenerator { crhead.outputline(methodcall+";"); craux.outputline(methodcall); craux.startblock(); + craux.startBuffer(); craux.outputline("int maybe=0;"); if (Compiler.GENERATEINSTRUMENT) craux.outputline("updatecount++;"); @@ -244,6 +252,7 @@ public class RepairGenerator { }; un.generate(cr2, false, true, stleft, stright, stnew, this); craux.outputline("if (maybe) printf(\"REALLY BAD\");"); + craux.emptyBuffer(); craux.endblock(); } break; @@ -258,7 +267,8 @@ public class RepairGenerator { private void generate_call() { CodeWriter cr = new StandardCodeWriter(outputrepair); VarDescriptor vdstate=VarDescriptor.makeNew("repairstate"); - cr.outputline("struct "+ name+"_state * "+vdstate.getSafeSymbol()+"=allocate"+name+"_state();"); + cr.addDeclaration("struct "+ name+"_state *", vdstate.getSafeSymbol()); + cr.outputline(vdstate.getSafeSymbol()+"=allocate"+name+"_state();"); Iterator globals=state.stGlobals.descriptors(); while (globals.hasNext()) { VarDescriptor vd=(VarDescriptor) globals.next(); @@ -316,8 +326,11 @@ public class RepairGenerator { public SymbolTable getSymbolTable() { return st; } }; - cr.outputline("void "+name+"_statecomputesizes(struct "+name+"_state * thisvar,int *sizearray,int **numele) {"); - cr.outputline("int maybe=0;"); + cr.outputline("void "+name+"_statecomputesizes(struct "+name+"_state * thisvar,int *sizearray,int **numele)"); + cr.startblock(); + cr.startBuffer(); + cr.addDeclaration("int","maybe"); + cr.outputline("maybe=0;"); for(int i=0;i1) { - cr.outputline("int "+mincostindex.getSafeSymbol()+";"); + cr.addDeclaration("int",mincostindex.getSafeSymbol()); boolean first=true; for(int j=0;jword" + offset + ";"); + writer.addDeclaration("int", varname); + writer.outputline(varname + " = wi->word" + offset + ";"); return offset + 1; } diff --git a/Repair/RepairCompiler/MCC/IR/SizeofFunction.java b/Repair/RepairCompiler/MCC/IR/SizeofFunction.java index 3e05368..71b1f22 100755 --- a/Repair/RepairCompiler/MCC/IR/SizeofFunction.java +++ b/Repair/RepairCompiler/MCC/IR/SizeofFunction.java @@ -37,7 +37,7 @@ public class SizeofFunction extends Expr { // is expand the guard of the rule and if its true then its 1 otherwise 0 String destname = dest.getSafeSymbol(); - cr.outputline("int " + destname + ";"); + cr.addDeclaration("int", destname); // ok... destination is declared... we gotta expand this rule inplace... and instead of the inclusion we // set the destination in the guard ... otherwise maybe! @@ -53,8 +53,10 @@ public class SizeofFunction extends Expr { String tempvar = (VarDescriptor.makeNew("tempvar")).getSafeSymbol(); // this is to be safe about name overlap because int t = t; sets t to 0! - cr.outputline("int " + tempvar + " = " + domain.getSafeSymbol() + ";"); - cr.outputline("int " + rulebinding.getSafeSymbol() + " = " + tempvar + ";"); + cr.addDeclaration("int", tempvar); + cr.outputline(tempvar + " = " + domain.getSafeSymbol() + ";"); + cr.addDeclaration("int", rulebinding.getSafeSymbol()); + cr.outputline(rulebinding.getSafeSymbol() + " = " + tempvar + ";"); /* pretty print! */ cr.outputline("/* about to inbed relational function*/"); diff --git a/Repair/RepairCompiler/MCC/IR/Sources.java b/Repair/RepairCompiler/MCC/IR/Sources.java index 797644a..7072ba8 100755 --- a/Repair/RepairCompiler/MCC/IR/Sources.java +++ b/Repair/RepairCompiler/MCC/IR/Sources.java @@ -44,7 +44,8 @@ public class Sources { cr.pushSymbolTable(state.stGlobals); e.generate(cr, size); cr.popSymbolTable(); - cr.outputline(td.getGenerateType().getSafeSymbol()+" "+vd.getSafeSymbol()+"=("+td.getGenerateType().getSafeSymbol()+") calloc(1,"+size.getSafeSymbol()+");"); + cr.addDeclaration(td.getGenerateType().getSafeSymbol(), vd.getSafeSymbol()); + cr.outputline(vd.getSafeSymbol()+"=("+td.getGenerateType().getSafeSymbol()+") calloc(1,"+size.getSafeSymbol()+");"); cr.outputline("alloc((void *) "+vd.getSafeSymbol()+","+size.getSafeSymbol()+");"); if (Compiler.ALLOCATECPLUSPLUS) { if (td instanceof StructureTypeDescriptor) { diff --git a/Repair/RepairCompiler/MCC/IR/StandardCodeWriter.java b/Repair/RepairCompiler/MCC/IR/StandardCodeWriter.java index ebe552e..123da04 100755 --- a/Repair/RepairCompiler/MCC/IR/StandardCodeWriter.java +++ b/Repair/RepairCompiler/MCC/IR/StandardCodeWriter.java @@ -2,15 +2,22 @@ package MCC.IR; import java.util.*; -public class StandardCodeWriter implements CodeWriter { +public class StandardCodeWriter implements CodeWriter { boolean linestarted = false; - int indent = 0; - java.io.PrintWriter output; + PrintWrapper output; Stack symboltables = new Stack(); InvariantValue ivalue; - public StandardCodeWriter(java.io.PrintWriter output) { this.output = output; } + public StandardCodeWriter(PrintWrapper output) { this.output = output; } + public StandardCodeWriter(java.io.PrintWriter output) { this.output = new PrintWrapper(output);} + + public void startBuffer() { + output.startBuffer(); + } + public void emptyBuffer() { + output.emptyBuffer(); + } public void startblock() { indent(); @@ -21,22 +28,28 @@ public class StandardCodeWriter implements CodeWriter { outputline("}"); unindent(); } - - public void indent() { - indent++; + public void addDeclaration(String type, String varname) { + output.addDeclaration(type,varname); } - - public void unindent() { - indent--; + public void addDeclaration(String f) { + output.addDeclaration(f); } + public void indent() { + output.indent++; + } + + public void unindent() { + output.indent--; + } + private void doindent() { - for (int i = 0; i < indent; i++) { + for (int i = 0; i < output.indent; i++) { output.print(" "); } linestarted = true; } - + public void outputline(String s) { if (!linestarted) { doindent(); @@ -44,14 +57,14 @@ public class StandardCodeWriter implements CodeWriter { output.println(s); linestarted = false; output.flush(); - } - + } + public void output(String s) { if (!linestarted) { doindent(); } output.print(s); - output.flush(); + output.flush(); } public void pushSymbolTable(SymbolTable st) { @@ -62,9 +75,9 @@ public class StandardCodeWriter implements CodeWriter { return (SymbolTable) symboltables.pop(); } - public SymbolTable getSymbolTable() { + public SymbolTable getSymbolTable() { if (symboltables.empty()) { - throw new IRException(); + throw new IRException(); } return (SymbolTable) symboltables.peek(); } diff --git a/Repair/RepairCompiler/MCC/IR/StructureGenerator.java b/Repair/RepairCompiler/MCC/IR/StructureGenerator.java index 284302b..b3a6664 100755 --- a/Repair/RepairCompiler/MCC/IR/StructureGenerator.java +++ b/Repair/RepairCompiler/MCC/IR/StructureGenerator.java @@ -57,7 +57,7 @@ public class StructureGenerator { str+="};"; cr.outputline(str); } else - cr.outputline("int arnumelement"+ttd.getId()+"[0];"); + cr.outputline("int arnumelement"+ttd.getId()+"[1];"); // c doesn't like 0 length arrays } String str="int* arnumelements["+String.valueOf(max)+"]={"; for(int i=0;i1 doesn't exist."); diff --git a/Repair/RepairCompiler/MCC/IR/VarExpr.java b/Repair/RepairCompiler/MCC/IR/VarExpr.java index 8ba05d9..3720a4c 100755 --- a/Repair/RepairCompiler/MCC/IR/VarExpr.java +++ b/Repair/RepairCompiler/MCC/IR/VarExpr.java @@ -115,22 +115,26 @@ public class VarExpr extends Expr { if (writer.getInvariantValue()!=null&& writer.getInvariantValue().isInvariant(this)) { - writer.outputline(vd.getType().getGenerateType().getSafeSymbol()+ - " "+dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";"); + writer.addDeclaration(vd.getType().getGenerateType().getSafeSymbol(),dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol()+"="+writer.getInvariantValue().getValue(this).getSafeSymbol()+";"); writer.outputline("maybe="+writer.getInvariantValue().getMaybe(this).getSafeSymbol()+";"); return; } - writer.outputline(vd.getType().getGenerateType().getSafeSymbol() + " " + dest.getSafeSymbol() + + writer.addDeclaration(vd.getType().getGenerateType().getSafeSymbol(),dest.getSafeSymbol()); + writer.outputline(dest.getSafeSymbol() + " = (" + vd.getType().getGenerateType().getSafeSymbol() + ") " + vd.getSafeSymbol() + "; /*varexpr*/"); if (vd.isGlobal() && (DOTYPECHECKS||DOMEMCHECKS) && (td instanceof StructureTypeDescriptor)) { VarDescriptor typevar=VarDescriptor.makeNew("typechecks"); writer.outputline("if ("+dest.getSafeSymbol()+")"); writer.startblock(); - if (DOTYPECHECKS) - writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); - else - writer.outputline("bool "+typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); + if (DOTYPECHECKS) { + writer.addDeclaration("bool",typevar.getSafeSymbol()); + writer.outputline(typevar.getSafeSymbol()+"=assertvalidtype(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); + } else { + writer.addDeclaration("bool",typevar.getSafeSymbol()); + writer.outputline(typevar.getSafeSymbol()+"=assertvalidmemory(" + dest.getSafeSymbol() + ", " + td.getId() + ");"); + } writer.outputline("if (!"+typevar.getSafeSymbol()+")"); writer.startblock(); writer.outputline(dest.getSafeSymbol()+"=0;"); -- 2.34.1