Browse Source

after change

eugeniucarvalho 6 years ago
parent
commit
4135790f12

+ 1 - 1
build/built-jar.properties

@@ -1,4 +1,4 @@
-#Fri, 13 Oct 2017 16:44:36 -0300
+#Fri, 13 Apr 2018 16:50:36 -0300
 
 
 C\:\\Users\\EUGENIO\ CARVALHO\\Desktop\\projects\\Ivannosys=

BIN
build/classes/API/Gen.class


BIN
build/classes/IntermediaryCode/BaseTacGen.class


BIN
build/classes/IntermediaryCode/DataFrame.class


+ 2 - 4
build/classes/samples/program.go

@@ -12,7 +12,7 @@ var (
     matrizR [LIN][COL]int
 )
 
-func preenche() int {
+func preenche(){
     var k = 1
     for i := 0; i < LIN; i++ {
         for j := 0; j < COL; j++ {
@@ -21,10 +21,9 @@ func preenche() int {
             k++
         }
     }
-    return 0
 }
 
-func multiplica() int {
+func multiplica() {
     var aux = 0
     for i := 0; i < LIN; i++ {
         for j := 0; j < COL; j++ {
@@ -35,7 +34,6 @@ func multiplica() int {
             aux = 0
         }
     }
-    return 0
 }
 func main() {
     preenche()

BIN
build/classes/target/mips/Gen$2.class


BIN
build/classes/target/mips/Gen$3.class


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


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


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


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


BIN
build/classes/template/TListener.class


+ 34 - 3
src/API/Gen.java

@@ -23,7 +23,9 @@ public abstract class Gen implements IvannosysTargetArch {
     protected Code target;
     protected Code tac;
     protected LinkedHashMap<String, CodeProcessing> beforeTranslate = new LinkedHashMap<>();
+    protected LinkedHashMap<String, CodeProcessing> beforeTranslateBlock = new LinkedHashMap<>();
     protected LinkedHashMap<String, CodeProcessing> afterTranslate = new LinkedHashMap<>();
+    protected LinkedHashMap<String, CodeProcessing> afterTranslateBlock = new LinkedHashMap<>();
 
     public Gen(String id) {
         target = new Code(id);
@@ -81,13 +83,15 @@ public abstract class Gen implements IvannosysTargetArch {
 
     protected void Translate() throws Exception {
         String id;
-
+        
+        BeforeTranslate();
+        
         for (Map.Entry<String, Block> b : tac.getBlocks().entrySet()) {
             id = b.getKey();
 
             tac.Use(id);
 
-            BeforeTranslate();
+            BeforeTranslateBlock();
 //            AllocatorRegister.Alloc(tac);
             target.OpenBlock(id);
 
@@ -99,8 +103,10 @@ public abstract class Gen implements IvannosysTargetArch {
             target.CloseBlock();
 
             target.UpdatePositions();
-            AfterTranslate();
+            AfterTranslateBlock();
         }
+        
+        AfterTranslate();
     }
 
     public Gen BeforeTranslate(String id, CodeProcessing processor) {
@@ -108,11 +114,21 @@ public abstract class Gen implements IvannosysTargetArch {
         return this;
     }
 
+    public Gen BeforeTranslateBlock(String id, CodeProcessing processor) {
+        beforeTranslateBlock.put(id, processor);
+        return this;
+    }
+
     public Gen AfterTranslate(String id, CodeProcessing processor) {
         afterTranslate.put(id, processor);
         return this;
     }
 
+    public Gen AfterTranslateBlock(String id, CodeProcessing processor) {
+        afterTranslateBlock.put(id, processor);
+        return this;
+    }
+
     protected Gen BeforeTranslate() throws Exception {
         for (Map.Entry<String, CodeProcessing> codeProcessing : beforeTranslate.entrySet()) {
             codeProcessing.getValue().Exec(tac, beforeTranslate);
@@ -120,13 +136,28 @@ public abstract class Gen implements IvannosysTargetArch {
         return this;
     }
 
+    protected Gen BeforeTranslateBlock() throws Exception {
+        for (Map.Entry<String, CodeProcessing> codeProcessing : beforeTranslateBlock.entrySet()) {
+            codeProcessing.getValue().Exec(tac, beforeTranslateBlock);
+        }
+        return this;
+    }
+
     protected Gen AfterTranslate() throws Exception {
+//        System.out.println("AfterTranslate:" + afterTranslate.entrySet().size());
         for (Map.Entry<String, CodeProcessing> codeProcessing : afterTranslate.entrySet()) {
             codeProcessing.getValue().Exec(target, afterTranslate);
         }
         return this;
     }
 
+    protected Gen AfterTranslateBlock() throws Exception {
+        for (Map.Entry<String, CodeProcessing> codeProcessing : afterTranslateBlock.entrySet()) {
+            codeProcessing.getValue().Exec(target, afterTranslateBlock);
+        }
+        return this;
+    }
+
     protected void TranslateBlock(Block b) throws Exception {
 //        System.out.println("Translate block:" + b.getName() + "::" + b.Instructions().size());
 

+ 1 - 1
src/IntermediaryCode/BaseTacGen.java

@@ -1548,7 +1548,7 @@ public final class BaseTacGen implements TacGenInterface {
         } else {
             varname = var.getText();
         }
-        System.out.printf("GerarVariavelSet{%s\n,%s\n,%s\n}\n", id, varname, var);
+//        System.out.printf("GerarVariavelSet{%s\n,%s\n,%s\n}\n", id, varname, var);
 //        if (var.eq("constant", "true")) {
 //            System.out.printf("GerarVariavelSet{%s\n,%s\n,%s\n}\n", id, varname, var);
 //        }

+ 1 - 1
src/IntermediaryCode/Block.java

@@ -99,7 +99,7 @@ public class Block {
 
     public void Update() throws Exception {
         CurrentAddress = 0;
-//        System.out.println("Update:" + instructions.size());S
+//        System.out.println("Update:" + instructions.size());
 
         for (Instruction n : instructions) {
 //            System.out.println("Update->:" + CurrentAddress + ":" + code.Position());

+ 44 - 29
src/IntermediaryCode/DataFrame.java

@@ -10,13 +10,15 @@ import ast.Node;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  *
  * @author Eugenio
  */
 public class DataFrame {
-    
+
     protected String name;
     protected LinkedHashMap<String, Node> values = new LinkedHashMap<>();//Map< String, String> copy = new TreeMap<>();
 //    protected LinkedList<Node> values = new LinkedList<>();//Map< String, String> copy = new TreeMap<>();
@@ -25,39 +27,39 @@ public class DataFrame {
 //    protected HashMap<String, Integer> _offset_ = new LinkedHashMap<>();
     protected boolean initialized = false;
     protected static AbstractSyntaxTree ast;
-    
+
     public static int TYPE_ARGUMENT = 0;
     public static int TYPE_VAR = 1;
     public static int TYPE_RETURN = 2;
     public static int TYPE_STACK = 3;
-    
+
     public int offsetctrl = 0;
     protected DataFrame data;
-    
+
     public DataFrame(String name, DataFrame data) {
         this.name = name;
         this.data = data;
-        
+
     }
-    
+
     public void setInicializar(boolean init) {
         initialized = init;
     }
-    
+
     public DataFrame Add(String alias, Node var) {
-        
+
         int size = Integer.parseInt(var.G("size"));
-        
+
         for (int i = 0; i < size; i++) {
             values.put(alias + "." + i, var);
         }
         return this;
     }
-    
+
     public LinkedHashMap<String, Node> values() {
         return values;
     }
-    
+
     public boolean has(String id) {
         for (Map.Entry<String, Node> x : values.entrySet()) {
             if (x.getKey().equals(id)) {
@@ -66,23 +68,31 @@ public class DataFrame {
         }
         return false;
     }
-    
+
     public static void setAbstractSyntaxTree(AbstractSyntaxTree ast) {
         DataFrame.ast = ast;
     }
-    
+
     public int Size() {
-        return values.size();
+        int val = 0;
+        for (Map.Entry<String, Node> v : values.entrySet()) {
+            try {
+                val += v.getValue().getInt("size");
+            } catch (Exception ex) {
+                Logger.getLogger(DataFrame.class.getName()).log(Level.SEVERE, null, ex);
+            }
+        }
+        return val;
     }
-    
+
     @Override
     public String toString() {
         StringBuilder s = new StringBuilder("Data Frame <" + name + ">:" + Size() + "\n");
-        
+
         for (Map.Entry<String, Node> x : values.entrySet()) {
             s.append(x.getKey()).append(": ").append(x.getValue().G("name")).append("\n");
         }
-        
+
         return s.toString();
     }
 
@@ -102,40 +112,45 @@ public class DataFrame {
         }
         return r;
     }
-    
+
     public int Offset(String id) {
         if (id.contains("_G") && data != null) {
             return data.Offset(id);
         }
-        
         int offset = 0;
-        id = normalize(id);
-        for (Map.Entry<String, Node> x : values.entrySet()) {
-            if (!x.getKey().equals(id)) {
-                offset++;
-                continue;
+        try {
+
+            id = normalize(id);
+
+            for (Map.Entry<String, Node> x : values.entrySet()) {
+                if (!x.getKey().equals(id)) {
+                    offset += x.getValue().getInt("size");
+                    continue;
+                }
+                break;
             }
-            break;
+        } catch (Exception ex) {
+            Logger.getLogger(DataFrame.class.getName()).log(Level.SEVERE, null, ex);
         }
         return offset;
     }
-    
+
     protected String normalize(String id) {
         String shift = "0", or = id;
-        
+
         if (id.contains("[")) {
             String[] parts = id.split("\\[");
             id = parts[0];
             shift = parts[1].replace("]", "");
         }
-        
+
         if (!id.contains(".")) {
             id += "." + shift;
         }
 //        System.out.println("Normalize:" + id + "::" + or);
         return id;
     }
-    
+
     public DataFrame copy(DataFrame data) {
         for (Map.Entry<String, Node> x : data.values().entrySet()) {
             this.values.put(x.getKey(), x.getValue());

+ 2 - 4
src/samples/program.go

@@ -12,7 +12,7 @@ var (
     matrizR [LIN][COL]int
 )
 
-func preenche() int {
+func preenche(){
     var k = 1
     for i := 0; i < LIN; i++ {
         for j := 0; j < COL; j++ {
@@ -21,10 +21,9 @@ func preenche() int {
             k++
         }
     }
-    return 0
 }
 
-func multiplica() int {
+func multiplica() {
     var aux = 0
     for i := 0; i < LIN; i++ {
         for j := 0; j < COL; j++ {
@@ -35,7 +34,6 @@ func multiplica() int {
             aux = 0
         }
     }
-    return 0
 }
 func main() {
     preenche()

+ 31 - 9
src/target/mips/Gen.java

@@ -38,6 +38,8 @@ public class Gen extends API.Gen {
         }
     };
     private ArrayList<String> returnRegisters;
+    private HashMap<String, Boolean> ctrlDataUpdate = new HashMap<>();
+    private int WORD_INC = 4;
 
     public Gen() {
         super("MIPS");
@@ -45,9 +47,12 @@ public class Gen extends API.Gen {
     }
 
     public void Init() {
-        BeforeTranslate("live.var", new OcorrenceFinderProcessor());
-        BeforeTranslate("register.alloc", allocation);
-        AfterTranslate("otimization.1", new OtimizationMips());
+        BeforeTranslateBlock("live.var", new OcorrenceFinderProcessor());
+        BeforeTranslateBlock("register.alloc", allocation);
+
+        AfterTranslateBlock("otimization.1", new OtimizationMips());
+
+        AfterTranslate("update.address", new UpdateAddressProcessor());
         getTarget()
                 .Template(new TemplateMips())
                 .Formats(new HashMap<String, String>() {
@@ -339,12 +344,24 @@ public class Gen extends API.Gen {
     public void Prolog(String id) throws Exception {
         // Copia os dados para block target
         DataFrame data = getTarget().Block().Data(getTac().Block().Data());
-
+        //Updata size in 4 bytes
+        Node v;
+        for (Map.Entry<String, Node> x : data.values().entrySet()) {
+            v = x.getValue();
+            if (!ctrlDataUpdate.containsKey(v.G("id"))) {
+                ctrlDataUpdate.put(v.G("id"), Boolean.TRUE);
+                v.S("size", v.getInt("size") * WORD_INC);
+            }
+        }
+//        for (Map.Entry<String, Node> x : data.values().entrySet()) {
+//            System.out.println("Copy:" + x.getKey() + x.getValue());
+//        }
         // Adiciona o label
         Add(new Instruction()
                 .S("type", "label")
                 .S("format", "label")
-                .S("label", id));
+                .S("label", id)
+        );
 
         // Aloca o espaco da pilha
         Instruction alloc = Copy(Registers.R_SP, Registers.R_SP, "-" + data.Size())
@@ -357,7 +374,7 @@ public class Gen extends API.Gen {
             int newvars = 0;
             for (String reg : new String[]{Registers.R_FP, Registers.R_RA}) {
                 if (call) {
-                    data.Add(reg, new Node().S("type", "int").set("size", 1));
+                    data.Add(reg, new Node().S("type", "int").set("size", 1 * WORD_INC));
                     newvars--;
                     StoreWord(reg, Registers.R_SP, data.Offset(reg))
                             .S("comment", "p| backup " + reg);
@@ -386,7 +403,8 @@ public class Gen extends API.Gen {
             boolean call = getTac().Block().HasCall();
             for (String reg : new String[]{Registers.R_FP, Registers.R_RA}) {
                 if (call) {
-                    LoadWord(reg, Registers.R_SP, data.Offset(reg)).S("comment", "e| restore " + reg);
+                    LoadWord(reg, Registers.R_SP, data.Offset(reg))
+                            .S("comment", "e| restore " + reg);
                 }
             }
 
@@ -435,7 +453,8 @@ public class Gen extends API.Gen {
             }
 
             // Carrega o conteudo enderecado em rt para rt
-            LoadWord(rt, rs, offset).S("tac.position", inst.G("global.position"));
+            LoadWord(rt, rs, offset)
+                    .S("tac.position", inst.G("global.position"));
 
         } else {
             // Se não é indexada carrega o parametro normalmente
@@ -520,6 +539,7 @@ public class Gen extends API.Gen {
             }
             Instruction Ninst = new Instruction();
             pname = inst.G(param);
+
             if (inst.eq(param + "value", "true")) {
                 Ninst.S("inst", "addiu") // R[$rt] ← {(imm)[15:0], 0 × 16}
                         .S("rs", Registers.R_ZERO)
@@ -529,7 +549,8 @@ public class Gen extends API.Gen {
                 Ninst.S("inst", "lw") // R[$rt] ← Mem4B(R[$rs] + SignExt16b(imm))
                         .S("rt", inst.G("reg." + param))
                         .S("rs", GlobalOrFrameRegister(pname))
-                        .set("offset", getTac().Block().Data().Offset(pname));
+                        .S("comment", "load address stored in " + pname)
+                        .set("offset", getTarget().Block().Data().Offset(pname));
             }
 
             Add(Ninst.S("tac.position", inst.G("global.position")));
@@ -576,6 +597,7 @@ public class Gen extends API.Gen {
     }
 
     protected Instruction LoadWord(String rt, String rs, int offset) {
+//        System.out.println("LoadWord:" + rt + ":" + rs + ":" + "off:" + offset);
         return LoadWord(rt, rs, "" + offset);
     }
 

+ 10 - 5
src/target/mips/TListenerMips.java

@@ -20,6 +20,7 @@ import template.TemplateParser;
 class TListenerMips extends template.TListener {
 
     public HashMap<String, TPLInterface> templates;
+//    public int AddressGap = 1;
 
     public TListenerMips(TemplateCtx ctx, TemplateCtx gctx) {
         super(ctx, gctx);
@@ -58,10 +59,10 @@ class TListenerMips extends template.TListener {
 
     @Override
     protected String L(ArrayList<String> arguments) {
-        
+
 //        System.out.println("TemplateListener::" + arguments);
         String label = arguments.get(0).trim();
-          
+
         if (label.equals("")) {
             return label;
         }
@@ -69,9 +70,8 @@ class TListenerMips extends template.TListener {
         String format,
                 base = gctx.G("label." + label).split("\\+")[0],
                 laddress = gctx.G(label);
-        
-//        System.out.printf("TemplateListener:{%s\n %s\n %s\n}\n", base, label, laddress);
 
+//        System.out.printf("TemplateListener:{%s\n %s\n %s\n}\n", base, label, laddress);
         int value = Integer.parseInt(laddress);
 
         if (arguments.size() > 1) {
@@ -80,7 +80,6 @@ class TListenerMips extends template.TListener {
             format = "dec";
         }
 
-//        System.out.println("============LaBEL:" + label + ":" + format + ":" + value + ":" + base);
         int address = value;
 
         switch (ctx.G("type")) {
@@ -99,6 +98,10 @@ class TListenerMips extends template.TListener {
             default:
                 System.out.println(String.format("Label type '%s' not defined", ctx.G("type")));
         }
+//        address = address * this.AddressGap;
+//        value = value * this.AddressGap;
+//        System.out.println("============LaBEL:" + label + ":" + format + ":" + value + ":" + base);
+//        System.out.println("============LaBEL:" + value + ":" + address);
 
         return laddress.trim().equals("")
                 ? ""
@@ -149,6 +152,8 @@ class TListenerMips extends template.TListener {
         if (type.equals("label")) {
             return "";
         }
+        
+        //
         switch (type) {
             case "S": // (j | jal) Salta para o endereco de destino
 //                System.out.println("instbin:" + ctx);

+ 1 - 0
src/target/mips/TemplateMips.java

@@ -36,6 +36,7 @@ public class TemplateMips extends template.Template {
         }
 
         TListenerMips listener = new TListenerMips(ctx, gctx);
+//        listener.AddressGap = 4;
         listener.templates = templates;
 
         walker.walk(listener, tree);

+ 61 - 0
src/target/mips/UpdateAddressProcessor.java

@@ -0,0 +1,61 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package target.mips;
+
+import API.Instruction;
+import IntermediaryCode.Block;
+import IntermediaryCode.Code;
+import IntermediaryCode.CodeProcessing;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ *
+ * @author EUGENIO CARVALHO
+ */
+class UpdateAddressProcessor implements CodeProcessing {
+
+    public UpdateAddressProcessor() {
+    }
+
+    /*
+     (parseInt("2c",10) * 4).toString(16)
+     */
+    @Override
+    public void Exec(Code c, LinkedHashMap<String, CodeProcessing> cp) throws Exception {
+
+        for (Map.Entry<String, Block> x : c.getBlocks().entrySet()) {
+            for (Instruction y : x.getValue().Instructions()) {
+                // atualiza o indice da instrução de pc + 4
+                if (y.isNumber("global.position")) {
+                    y.S("block.position", y.getInt("block.position") * 4);
+                    y.S("global.position", y.getInt("global.position") * 4);
+                } else if (y.eq("type", "label")) {
+                    // atualiza o endereco dos labels
+                    y.S("reference.position", y.getInt("reference.position") * 4);
+                    y.S("global.reference.position", y.getInt("global.reference.position") * 4);
+                }
+            }
+        }
+        String[] parts;
+        int index;
+        // Update address of labels
+        for (Map.Entry<String, ArrayList<String>> entry : c.labels.entrySet()) {
+            ArrayList<String> labeldata = entry.getValue();
+            labeldata.set(0, (Integer.parseInt(labeldata.get(0)) * 4) + "");
+            try {
+                parts = labeldata.get(1).split("(\\+)");
+                if (parts.length > 1) {
+                    index = Integer.parseInt(parts[1]);
+                    labeldata.set(1, parts[0] + "+" + (index * 4));
+                }
+            } catch (Exception e) {
+            }
+        }
+    }
+
+}

+ 7 - 1
src/template/TListener.java

@@ -5,7 +5,6 @@
  */
 package template;
 
-import API.Instruction;
 import API.Utils;
 import java.util.ArrayList;
 import java.util.LinkedList;
@@ -94,6 +93,9 @@ public class TListener extends TemplateBaseListener {
             case "L": // Lavel function
                 this.returnCall = L(arguments);
                 break;
+//            case "A": // Lavel function
+//                this.returnCall = A(arguments);
+//                break;
             case "PAD": // Lavel function
                 this.returnCall = PAD(arguments);
                 break;
@@ -187,6 +189,10 @@ public class TListener extends TemplateBaseListener {
         return String.format("%" + i + "s", value).replace(" ", replace);
     }
     
+//    protected String A(ArrayList<String> params) {
+//        return String.format("%" + i + "s", value).replace(" ", replace);
+//    }
+    
     protected String Get(String value) {
         if (Utils.isNumber(value)) {
             return value;