Browse Source

if_branch

eugeniucarvalho 6 years ago
parent
commit
8dbde8aa8a

BIN
build/classes/IntermediaryCode/BaseTacGen.class


+ 80 - 1
build/classes/samples/program.go

@@ -11,7 +11,10 @@ var (
 )
 
 func main() {
-    var aux = 0
+    var (
+        aux = 0
+        i = 1
+    )
 /**
 FOR_OR_AND_AND_1:
     for i := 0; (((i < LIN || 1 == 1) && (i < LIN && 1 == 1)) && 1 == 1 ); i++ {
@@ -28,7 +31,83 @@ FOR_OR_AND_AND_OR:
 
 */
 
+//if (i < LIN || 1 == 1) && (i < LIN && 1 == 1) {
+
+IF_01:
+if (i < LIN) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_02:
+if (i < LIN || 1 == 1) {
+    aux = 10
+}else {
+    aux = 20
+}
+IF_03:
+if (i < LIN && 1 == 1) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_04:
+if ((i < LIN || 1 == 1) || (i < LIN || 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_05:
+if ((i < LIN && 1 == 1) && (i < LIN && 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_06:
+if ((i < LIN && 1 == 1) || (i < LIN && 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_07:
+if ((i < LIN || 1 == 1) && (i < LIN || 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_08:
+if ((i < LIN && 1 == 1) && (i < LIN || 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_09:
+if ((i < LIN || 1 == 1) && (i < LIN && 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
 
+IF_10:
+if ((i < LIN && 1 == 1) || (i < LIN || 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_11:
+if ((i < LIN || 1 == 1) || (i < LIN && 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
 
 
 /**

BIN
build/classes/target/mips/UpdateAddressProcessor.class


+ 104 - 8
build/classes/tools/mips/memory/mi.memory

@@ -1,10 +1,106 @@
-0666763164
+0666763252
 0001962017
 0605028352
-0605093888
-0134217736
-0638189578
-0000688161
-0640745473
-2412576776
-0036507683
+0605093889
+2412380168
+0036311075
+0071303171
+0605028362
+0134217768
+0605028372
+0036311075
+0071303172
+0604831745
+0604897281
+0363724803
+0605028362
+0134217800
+0605028372
+0036311075
+0071303174
+0604504065
+0604569601
+0352911363
+0605028362
+0134217832
+0605028372
+0036311075
+0071303177
+0604897281
+0604831745
+0298647558
+0036311075
+0071303172
+0604962817
+0605552641
+0368574467
+0605028362
+0134217884
+0605028372
+0036311075
+0071303179
+0604700673
+0604766209
+0359399432
+0036311075
+0071303174
+0604831745
+0604897281
+0363724803
+0605028362
+0134217936
+0605028372
+0036311075
+0071303172
+0604569601
+0604504065
+0287834118
+0036311075
+0071303174
+0604766209
+0604700673
+0361431043
+0605028362
+0134217988
+0605028372
+0036311075
+0071303172
+0605552641
+0604962817
+0386859016
+0036311075
+0071303172
+0604504065
+0604569601
+0352911363
+0605028362
+0134218040
+0605028372
+0036311075
+0071303179
+0604897281
+0604831745
+0365756424
+0036311075
+0071303172
+0604962817
+0605552641
+0368574467
+0605028362
+0134218092
+0605028372
+0036311075
+0071303172
+0604700673
+0604766209
+0359399432
+0036311075
+0071303174
+0604831745
+0604897281
+0363724803
+0605028362
+0134218148
+0604962836
+2949578752
+4294967295

+ 164 - 109
src/IntermediaryCode/BaseTacGen.java

@@ -109,7 +109,7 @@ public final class BaseTacGen implements TacGenInterface {
     public void Ast(AbstractSyntaxTree ast) {
         this.ast = ast;
         code = new Code("TAC", ast);
-        code.AfterClose("clear.1", new CodeClearProcessor());
+//        code.AfterClose("clear.1", new CodeClearProcessor());
 //        code.AfterClose("oti.1", new CodeOtimizadorProcessor(enableOtimization));
 //        code.AfterClose("oti.1", new CodeOtimizadorProcessor(false));
         compileParams = ast.BuildParams();
@@ -738,11 +738,8 @@ public final class BaseTacGen implements TacGenInterface {
         String end = gerarLabel(), next;
         boolean ifChain = n.childrens().size() > 1;
 
-        if (ifChain) { //(If)+ (else)?
-            next = gerarLabel();
-        } else {
-            next = end;
-        }
+        //(If)+ (else)?
+        next = ifChain ? gerarLabel() : end;
 
         n.S("next", next).S("end", end);
 
@@ -779,8 +776,9 @@ public final class BaseTacGen implements TacGenInterface {
         Node test = _if.first().first();
         Node stmts = _if.find("stmts");
 
+        // Se o teste falha deve saltar o bloco 
         n.S("start", gerarLabel());
-        test.copy("start,next,end", n);
+        test.S("test.type", "jb").copy("next,start,end", n);
 
         Branch(test, 0);
         // marca com um label o inicio do bloco do if
@@ -840,7 +838,7 @@ public final class BaseTacGen implements TacGenInterface {
         Node op1 = x.get(0), op2 = x.get(1);
         String type = n.G("test.type"),
                 step = gerarLabel(),
-                attrs = "next,start,end,inc,test.type,skip.test";
+                attrs = "next,end,inc,test.type,skip.test";
 
         n.S("skip.test", step);
 
@@ -865,6 +863,18 @@ public final class BaseTacGen implements TacGenInterface {
                     } else {
                         instruction.S("label", n.G("end"));
                     }
+                    break;
+                case "jb":
+                    if (n.parent.eq("value", "||")) {
+                        if (index <= 1) {
+                            instruction.S("label", n.parent.G("skip.test"));
+                        } else if (index == 2) {
+//                            instruction.S("op", Complement(instruction.G("op")));
+                            instruction.S("label", n.G("next"));
+                        }
+                    } else {
+
+                    }
             }
         }
 //        System.out.println("instruction Branch 1:" + instruction + "\n" + op1);
@@ -882,7 +892,7 @@ public final class BaseTacGen implements TacGenInterface {
                             instruction.S("op", Complement(instruction.G("op")));
                             instruction.S("label", n.G("end"));
                         } else if (index == 2) {  // teste 2 do parametro 2
-                            instruction.S("label", n.G("start"));
+                            instruction.S("label", n.G("next"));
                         }
 //                    } else if (n.parent.eq("value", "&&")) {
 //                        if (index == 1) { // teste 1 do parametro 2 com pai && 
@@ -900,6 +910,24 @@ public final class BaseTacGen implements TacGenInterface {
 //                            throw new Exception("teste 2 do parametro 2 geral");
                         }
                     }
+                    break;
+                case "jb":
+
+                    System.out.println("I(jb:and:2)[" + index + "]\n" + instruction + "\n" + n);
+
+                    if (n.parent.eq("value", "||")) {
+                        if (index <= 1) {
+//                            instruction.S("label", n.parent.G("skip.test"));
+                            instruction.S("op", Complement(instruction.G("op")));
+                            instruction.S("label", n.G("start"));
+
+                        } else if (index == 2) {
+//                            instruction.S("op", Complement(instruction.G("op")));
+                            instruction.S("label", n.G("next"));
+                        }
+                    } else {
+
+                    }
             }
         }
     }
@@ -919,7 +947,7 @@ public final class BaseTacGen implements TacGenInterface {
         op1.copy(attrs, n);
         op2.copy(attrs, n);
 
-        System.out.println("BranchOr:" + n.parent);
+//        System.out.println("BranchOr:" + n.parent);
         // Executa o lado esquerdo do ||
         instruction = Branch(op1, 1);
 
@@ -931,6 +959,31 @@ public final class BaseTacGen implements TacGenInterface {
                             instruction.S("label", n.parent.G("skip.test"));
                         }
                     }
+                    break;
+                case "jb":
+//                    System.out.println("I(jb:1)[" + index + "]\n" + instruction + "\n" + n);
+
+                    if (n.parent.eq("value", "||")) {
+//                        if (index == 2) {
+                        instruction.S("op", Complement(instruction.G("op")));
+                        instruction.S("label", n.G("start"));
+//                        }
+                    } else if (n.parent.eq("value", "&&")) {
+                        if (index <= 1) {
+                            instruction.S("op", Complement(instruction.G("op")));
+                            instruction.S("label", n.parent.G("skip.test"));
+                        } else if (index == 2) {
+                            instruction.S("op", Complement(instruction.G("op")));
+                            instruction.S("label", n.parent.G("start"));
+                        }
+                    } else {
+                        instruction.S("op", Complement(instruction.G("op")));
+                        if (index <= 1) {
+                            instruction.S("label", n.G("start"));
+                        } else if (index == 2) {
+
+                        }
+                    }
             }
         }
 //        System.out.println("instruction Branch 1:" + instruction + "\n" + op1);
@@ -948,6 +1001,31 @@ public final class BaseTacGen implements TacGenInterface {
                             instruction.S("label", n.parent.G("end"));
                         }
                     }
+                    break;
+                case "jb":
+                    if (n.parent.eq("value", "||")) {
+                        if (index <= 1) {
+
+                            instruction.S("op", Complement(instruction.G("op")));
+                            instruction.S("label", n.G("start"));
+
+                        } else if (index == 2) {
+//
+//                            instruction.S("op", Complement(instruction.G("op")));
+                            instruction.S("label", n.G("next"));
+                        }
+                    } else if (n.parent.eq("value", "&&")) {
+                        if (index == 1) {
+//                            instruction.S("op", Complement(instruction.G("op")));
+//                            instruction.S("label", n.parent.G("start"));
+                        } else if (index == 2) {
+//                            instruction.S("op", Complement(instruction.G("op")));
+//                            instruction.S("label", n.parent.G("start"));
+                        }
+                    } else {
+//                        instruction.S("op", Complement(instruction.G("op")));
+//                        instruction.S("label", n.G("next"));
+                    }
             }
         }
     }
@@ -956,15 +1034,13 @@ public final class BaseTacGen implements TacGenInterface {
         ArrayList<Node> x = n.childrens();
         String type = n.G("test.type");
         boolean invert = false;
-        String op, p0, p1, next = "";
+        String op, p0, p1;
 
         switch (type) {
             case "jb":
                 invert = true;
-                next = n.G("end");
                 break;
             case "jtb":
-                next = n.G("start");
                 break;
             default:
                 throw new Exception("test.type '".concat(type).concat("' not suported. Values {jtb,jb}"));
@@ -987,44 +1063,17 @@ public final class BaseTacGen implements TacGenInterface {
         if (invert) {
             op = Complement(op);
         }
-
-        return Branch(op, p0, p1, next);
-
-//        switch (n.parent.G("value")) {
-//            case "||":
-//                if (index == 1) {
-//                    invert = false;
-//                    next = n.G("start");
-//                }
-//                break;
-//            case "&&":
-//                break;
-//            default:
-//            // Sem combinação ( || / && )
-//        }
-//        System.out.println("BranchTest:[" + index + ":" + op + ":" + invert
-//                + ":" + n.eq("invert.test", "false") + "]" + n);
-//        Instruction instruction = Branch(op, p0, p1, label);
-//                instruction. ;
-//                if (n.Has("false")) {
-        //                    _goto(n.G("false"));
-        //                }
-        //                if (empty) {
-        //                    branckInvert.pop();
-        //                }
-    }
-
-    protected Instruction Branch(String op, final String p1, final String p2, String label) throws Exception {
-
+//        return Branch(op, p0, p1, next);
         Instruction r1 = new Instruction();
+
         r1.S("format", "branch")
                 .S("type", "branch")
                 .S("op", op)
-                .S("p1", p1)
-                .S("p2", p2)
-                .S("p1value", "" + Api.IsValue(p1))
-                .S("p2value", "" + Api.IsValue(p2))
-                .S("label", label);
+                .S("p1", p0)
+                .S("p2", p1)
+                .S("p1value", "" + Api.IsValue(p0))
+                .S("p2value", "" + Api.IsValue(p1))
+                .S("label", n.G("next"));
 
 //        System.out.println("Add branch:" + branckInvert.peek() + branckInvert + ">>" + (!branckInvert.isEmpty() && branckInvert.peek()) + "\n" + r1);
         code.Add(r1);
@@ -1032,7 +1081,6 @@ public final class BaseTacGen implements TacGenInterface {
     }
 
     protected Instruction Branch(Node n, int index) throws Exception {
-//        ArrayList<Node> x = n.childrens();
         Instruction instruction = null;
 
         switch (n.getText()) {
@@ -1045,62 +1093,13 @@ public final class BaseTacGen implements TacGenInterface {
             default:
                 instruction = BranchTest(n, index);
         }
-
-        if (index == 0 && instruction != null) {
-
-        }
-
         return instruction;
-//        if (and) {
-//            n.remove("or");
-//        }
     }
 
     protected String Complement(String op) {
         return Complements.get(op);
     }
 
-//    /**
-//     * ProceSSa um teSte logico AND
-//     *
-//     * @param n
-//     * @throws Exception
-//     */
-//    protected void _and(Node n) throws Exception {
-//        String attrs = "next,start,end,test,skip.test";
-//        Node b1 = n.getFilho(0).copy(attrs, n);
-//        Node b2 = n.getFilho(1).copy(attrs, n);
-//
-////        boolean isFor = n.eq("test.type", "for");
-////        System.out.println("_AND{" + isFor + "}:" + n.G(isFor ? "and" : "next"));
-////        String l = n.G(n.eq("test.type", "for") ? "and" : "next");
-//        b1.S("true", n.G("next"));
-////        b1.S("false", b.G("false"));
-////        System.out.println("_AND:" + label + "-s" + b1);
-//        b2.S("true", n.G("next"));
-////        b2.S("false", b.G("false"));
-////        b1.S("true", gerarLabel());
-////        b1.S("false", b.G("false"));
-////
-////        b2.S("true", b.G("true"));
-////        b2.S("false", b.G("false"));
-//    }
-//    /**
-//     * ProceSSa um teSte logico OR
-//     *
-//     * @param n
-//     * @throws Exception
-//     */
-//    protected void _or(Node n) throws Exception {
-//
-//        String attrs = "next,start,end,test,skip.test";
-//        Node b1 = n.getFilho(0).copy(attrs, n);
-//        Node b2 = n.getFilho(1).copy(attrs, n);
-//
-//        b1.S("true", n.G("start"));
-//        b2.S("true", n.G("start"));
-//
-//    }
     /**
      * Cria uma linha de Salto para PuShLabel eSpecificado no parametro.
      *
@@ -1147,14 +1146,6 @@ public final class BaseTacGen implements TacGenInterface {
                 .set("locker", getIdMaster()));
 
     }
-//
-//    protected String updateLabel(Node n) {
-//        String PushLabel = n.G("_return");
-////        if (!PushLabel.substring(0, 1).equals("_R")) {
-////        }
-////        n.S("_return", PushLabel);
-//        return PushLabel;
-//    }
 
     /**
      * Gera todoS oS SaltoS de um loop for
@@ -1163,18 +1154,18 @@ public final class BaseTacGen implements TacGenInterface {
      * @throws Exception
      */
     protected void genFor(Node n) throws Exception {
-        String attrs = "next,start,end,inc,test";
+        String attrs = "next,end,inc,test";
         Node test = n.find("test").first();
 
         setMaster(n);
 
-        String start = gerarLabel(),
+        String next = gerarLabel(),
                 //                next = gerarLabel(),
                 end = gerarLabel(),
                 inc = gerarLabel(),
                 testLabel = gerarLabel();
 
-        n.S("start", start)
+        n.S("next", next)
                 .S("end", end)
                 .S("inc", inc)
                 .S("test", testLabel);
@@ -1191,7 +1182,7 @@ public final class BaseTacGen implements TacGenInterface {
         _goto(testLabel);
 
         /*Marca o inicio do bloco do laco*/
-        PushLabel(start, "branch");
+        PushLabel(next, "branch");
 
         /*Executa o bloco do for*/
         visit(stmt);
@@ -2221,3 +2212,67 @@ public final class BaseTacGen implements TacGenInterface {
 //        }
 //        return this;
 //    }
+//    protected Instruction Branch(String op, final String p1, final String p2, String label) throws Exception {
+//
+//        Instruction r1 = new Instruction();
+//        r1.S("format", "branch")
+//                .S("type", "branch")
+//                .S("op", op)
+//                .S("p1", p1)
+//                .S("p2", p2)
+//                .S("p1value", "" + Api.IsValue(p1))
+//                .S("p2value", "" + Api.IsValue(p2))
+//                .S("label", label);
+//
+////        System.out.println("Add branch:" + branckInvert.peek() + branckInvert + ">>" + (!branckInvert.isEmpty() && branckInvert.peek()) + "\n" + r1);
+//        code.Add(r1);
+//        return r1;
+//    }
+//    /**
+//     * ProceSSa um teSte logico AND
+//     *
+//     * @param n
+//     * @throws Exception
+//     */
+//    protected void _and(Node n) throws Exception {
+//        String attrs = "next,start,end,test,skip.test";
+//        Node b1 = n.getFilho(0).copy(attrs, n);
+//        Node b2 = n.getFilho(1).copy(attrs, n);
+//
+////        boolean isFor = n.eq("test.type", "for");
+////        System.out.println("_AND{" + isFor + "}:" + n.G(isFor ? "and" : "next"));
+////        String l = n.G(n.eq("test.type", "for") ? "and" : "next");
+//        b1.S("true", n.G("next"));
+////        b1.S("false", b.G("false"));
+////        System.out.println("_AND:" + label + "-s" + b1);
+//        b2.S("true", n.G("next"));
+////        b2.S("false", b.G("false"));
+////        b1.S("true", gerarLabel());
+////        b1.S("false", b.G("false"));
+////
+////        b2.S("true", b.G("true"));
+////        b2.S("false", b.G("false"));
+//    }
+//    /**
+//     * ProceSSa um teSte logico OR
+//     *
+//     * @param n
+//     * @throws Exception
+//     */
+//    protected void _or(Node n) throws Exception {
+//
+//        String attrs = "next,start,end,test,skip.test";
+//        Node b1 = n.getFilho(0).copy(attrs, n);
+//        Node b2 = n.getFilho(1).copy(attrs, n);
+//
+//        b1.S("true", n.G("start"));
+//        b2.S("true", n.G("start"));
+//
+//    }//
+//    protected String updateLabel(Node n) {
+//        String PushLabel = n.G("_return");
+////        if (!PushLabel.substring(0, 1).equals("_R")) {
+////        }
+////        n.S("_return", PushLabel);
+//        return PushLabel;
+//    }

+ 80 - 1
src/samples/program.go

@@ -11,7 +11,10 @@ var (
 )
 
 func main() {
-    var aux = 0
+    var (
+        aux = 0
+        i = 1
+    )
 /**
 FOR_OR_AND_AND_1:
     for i := 0; (((i < LIN || 1 == 1) && (i < LIN && 1 == 1)) && 1 == 1 ); i++ {
@@ -28,7 +31,83 @@ FOR_OR_AND_AND_OR:
 
 */
 
+//if (i < LIN || 1 == 1) && (i < LIN && 1 == 1) {
+
+IF_01:
+if (i < LIN) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_02:
+if (i < LIN || 1 == 1) {
+    aux = 10
+}else {
+    aux = 20
+}
+IF_03:
+if (i < LIN && 1 == 1) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_04:
+if ((i < LIN || 1 == 1) || (i < LIN || 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_05:
+if ((i < LIN && 1 == 1) && (i < LIN && 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_06:
+if ((i < LIN && 1 == 1) || (i < LIN && 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_07:
+if ((i < LIN || 1 == 1) && (i < LIN || 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_08:
+if ((i < LIN && 1 == 1) && (i < LIN || 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_09:
+if ((i < LIN || 1 == 1) && (i < LIN && 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
 
+IF_10:
+if ((i < LIN && 1 == 1) || (i < LIN || 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
+
+IF_11:
+if ((i < LIN || 1 == 1) || (i < LIN && 1 == 1)) {
+    aux = 10
+}else {
+    aux = 20
+}
 
 
 /**

+ 1 - 1
src/target/mips/UpdateAddressProcessor.java

@@ -59,7 +59,7 @@ class UpdateAddressProcessor implements CodeProcessing {
                 }
                 // Atualziza o offset das instruições de branch
                 if (updateOffsetShift.containsKey(y.G("inst"))) {
-                    System.out.println("UPDATE ADDRESS:" + y + c.labels);
+//                    System.out.println("UPDATE ADDRESS:" + y + c.labels);
                     offset = Integer.parseInt(c.labels.get(y.G("label")).get(0)) * 4;
                     offset = (offset - y.getInt("global.position")) / 4;
 //                    System.out.println("Offset:" + offset);

+ 130 - 8
src/tools/mips/memory/mi.memory

@@ -1,10 +1,132 @@
-0666763164
+0666763252
 0001962017
 0605028352
-0605093888
-0134217736
-0638189578
-0000688161
-0640745473
-2412576776
-0036507683
+0605093889
+2412380168
+0036311075
+0071303171
+0605028362
+0134217768
+0605028372
+0036311075
+0071303172
+0604831745
+0604897281
+0363724803
+0605028362
+0134217800
+0605028372
+0036311075
+0071303174
+0604504065
+0604569601
+0352911363
+0605028362
+0134217832
+0605028372
+0036311075
+0071303177
+0604897281
+0604831745
+0298647558
+0036311075
+0071303172
+0604962817
+0605552641
+0368574467
+0605028362
+0134217884
+0605028372
+0036311075
+0071303179
+0604700673
+0604766209
+0359399432
+0036311075
+0071303174
+0604831745
+0604897281
+0363724803
+0605028362
+0134217936
+0605028372
+0036311075
+0071303172
+0604569601
+0604504065
+0287834118
+0036311075
+0071303174
+0604766209
+0604700673
+0361431043
+0605028362
+0134217988
+0605028372
+0036311075
+0071303172
+0605552641
+0604962817
+0386859016
+0036311075
+0071303172
+0604504065
+0604569601
+0352911363
+0605028362
+0134218040
+0605028372
+0036311075
+0071303179
+0604897281
+0604831745
+0365756424
+0036311075
+0071303172
+0604962817
+0605552641
+0368574467
+0605028362
+0134218092
+0605028372
+0036311075
+0071303172
+0604700673
+0604766209
+0359399432
+0036311075
+0071303174
+0604831745
+0604897281
+0363724803
+0605028362
+0134218144
+0605028372
+0036311075
+0071303172
+0604569601
+0604504065
+0287834118
+0036311075
+0071303172
+0604766209
+0604700673
+0361431043
+0605028362
+0134218196
+0605028372
+0036311075
+0071303177
+0605552641
+0604962817
+0319750150
+0036311075
+0071303174
+0604504065
+0604569601
+0352911363
+0605028362
+0134218252
+0604766228
+2949382144
+4294967295