123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- /*
- * 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 API;
- import common.Block;
- import common.Code;
- import common.Instruction;
- import common.IvannosysTargetArch;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.Map;
- import java.util.Set;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author EUGENIO CARVALHO
- */
- public abstract class TargetGen implements IvannosysTargetArch {
- protected HashMap<String, ExportInterface> exportMethods = new HashMap<>();
- protected Code target;
- protected Code IR;
- protected Instruction prev = null, next = null;
- public TargetGen(String id) {
- BaseGenInit(id);
- }
- public void BaseGenInit(String id) {
- target = new Code(id);
- }
- public Code getTarget() {
- return target;
- }
- public void setTarget(Code target) {
- this.target = target;
- }
- public Code getIR() {
- return IR;
- }
- public ExportInterface GetExportMethod(String id) throws Exception {
- id = id.trim();
- if (!exportMethods.containsKey(id)) {
- Set<String> set = exportMethods.keySet();
- String[] valids = new String[set.size()];
- set.toArray(valids);
- throw new Exception(String.format(
- "O método de exportação '%s' não foi definido no alvo '%s'. Métodos disponiveis: %s",
- id,
- target.Name(),
- Utils.Join(valids, ", ")));
- }
- return exportMethods.get(id);
- }
- public HashMap<String, ExportInterface> GetExportMethods() {
- return exportMethods;
- }
- public IvannosysTargetArch SetTAC(Code tac) throws Exception {
- // Verifica se o codigo gerado vai ser util caso contrario aborta o processo;
- ArrayList<String> methods = BuildParams.Get("export");
- if (methods.isEmpty()) {
- throw new Exception(String.format("Nenhum método de exportação definido para o alvo '%s'", target.Name()));
- }
- IR = tac;
- target.GlobalData().copy(tac.GlobalData());
- target.Parent = IR;
- return this;
- }
- protected Instruction Add(Instruction inst) {
- target.Block().Add(inst);
- return inst;
- }
- public TargetGen AddExportOption(String id, ExportInterface exp) {
- exportMethods.put(id.trim(), exp);
- return this;
- }
- @Override
- public IvannosysTargetArch Format() {
- try {
- String code = target.getCodeStream();
- System.out.println("Target[" + target.Name() + "]:\n" + code);
- } catch (Exception ex) {
- Logger.getLogger(TargetGen.class.getName()).log(Level.SEVERE, null, ex);
- }
- return this;
- }
- @Override
- public IvannosysTargetArch Compile() {
- try {
- // Traduz o codigo intermediario para o alvo
- Translate();
- } catch (Exception ex) {
- Logger.getLogger(TargetGen.class.getName()).log(Level.SEVERE, null, ex);
- }
- return this;
- }
- protected void Translate() throws Exception {
- String targetID = target.Name(), id;
- target.Parent = IR;
- CodeProcessor.Trigger(target, targetID, "BeforeTranslate");
- for (Map.Entry<String, Block> b : IR.getBlocks().entrySet()) {
- id = b.getKey();
- IR.Use(id);
- target.OpenBlock(id);
- CodeProcessor.Trigger(target, targetID, "BeforeTranslateBlock");
- Prolog(id);
- TranslateBlock(b.getValue());
- Epilog(id);
- target.CloseBlock();
- target.UpdatePositions();
- CodeProcessor.Trigger(target, targetID, "AfterTranslateBlock");
- }
- CodeProcessor.Trigger(target, targetID, "AfterTranslate");
- }
- @Override
- public IvannosysTargetArch Export() throws Exception {
- ArrayList<String> methods = BuildParams.Get("export");
- for (String method : methods) {
- GetExportMethod(method).Exec(IR, target);
- }
- return this;
- }
- protected void TranslateBlock(Block b) throws Exception {
- // System.out.println("Translate block:" + b.getName() + "::" + b.Instructions().size());
- LinkedList<Instruction> instructions = b.Instructions();
- Integer index = 0, limit = instructions.size(), nextIndex;
- for (Instruction inst : instructions) {
- nextIndex = index + 1;
- next = (nextIndex < limit) ? instructions.get(nextIndex) : null;
- // System.out.println("Translate: " + inst.Get("global.position") + "::read["
- // + inst.Get("reg.dst.load")
- // + "|"
- // + inst.Get("reg.p1.load")
- // + "|"
- // + inst.Get("reg.p2.load")
- // + "] >> write["
- // + inst.Get("reg.dst.store")
- // + "|"
- // + inst.Get("reg.p1.store")
- // + "|"
- // + inst.Get("reg.p2.store")
- // + "]"
- // + inst.Get("type")
- // );
- //
- switch (inst.Get("type")) {
- case "label":
- if (!inst.Get("label").equals(getTarget().Block().getName())) {
- TranslateLabel(inst);
- }
- break;
- case "assign":
- TranslateAssign(inst);
- break;
- case "unary":
- TranslateUnary(inst);
- break;
- case "copy":
- TranslateCopy(inst);
- break;
- case "jump":
- TranslateJump(inst);
- break;
- case "call":
- TranslateCall(inst);
- break;
- case "return":
- TranslateReturn(inst);
- break;
- case "push_param":
- TranslatePushParam(inst);
- break;
- case "push_return":
- TranslatePushReturn(inst);
- break;
- case "pop_return":
- TranslatePopReturn(inst);
- break;
- case "pop_param":
- TranslatePopParam(inst);
- break;
- case "branch":
- TranslateBranch(inst);
- break;
- case "indexed_assign":
- TranslateIndexedAssignment(inst);
- break;
- case "pointer_assign":
- TranslatePointerAssignment(inst);
- break;
- case "store":
- TranslateStore(inst);
- break;
- case "load":
- TranslateLoad(inst);
- break;
- default:
- throw new Exception(String.format("TAC instruction type '%s' not defined", inst.Get("type")));
- }
- prev = inst;
- index++;
- }
- }
- protected Instruction Next() {
- return next;
- }
- protected Instruction Prev() {
- return prev;
- }
- }
|