public void insertAfter(DNode at, DNode node) { node.next = at; node.prev = at.prev; at.prev.next = node; at.prev = node; }
public void remove(DNode node) { node.prev.next = node.next; node.next.prev = node.prev; node.next = null; node.prev = null; }
public void replace(DNode at, DNode with) { with.prev = at.prev; with.next = at.next; at.prev.next = with; at.next.prev = with; at.prev = null; at.next = null; }
public AbstractStack(AbstractStack other) { stack_ = new List<StackEntry>(); for (int i = 0; i < other.stack_.Count; i++) stack_.Add(new StackEntry(other.stack_[i].declaration, other.stack_[i].assignment)); args_ = new StackEntry[other.args_.Length]; for (int i = 0; i < args_.Length; i++) args_[i] = new StackEntry(other.args_[i].declaration, other.args_[i].assignment); pri_ = other.pri_; alt_ = other.alt_; }
public StackEntry(DDeclareLocal decl, DNode assn) { declaration = decl; assignment = assn; }
public void add(DNode node) { insertBefore(head_, node); }
public void replace(NodeList.iterator_base where, DNode with) { with.setBlock(this); nodes_.replace(where, with); }
public iterator(DNode node) : base(node) { }
public reverse_iterator(DNode node) : base(node) { }
public Node(DNode expression) { expression_ = expression; }
private string buildExpression(DNode node) { switch (node.type) { case NodeType.Constant: return(buildConstant((DConstant)node)); case NodeType.Boolean: return(buildBoolean((DBoolean)node)); case NodeType.Float: return(buildFloat((DFloat)node)); case NodeType.Character: return(buildCharacter((DCharacter)node)); case NodeType.Function: return(buildFunction((DFunction)node)); case NodeType.Load: return(buildLoad((DLoad)node)); case NodeType.String: return(buildString(((DString)node).value)); case NodeType.LocalRef: return(buildLocalRef((DLocalRef)node)); case NodeType.ArrayRef: return(buildArrayRef((DArrayRef)node)); case NodeType.Unary: return(buildUnary((DUnary)node)); case NodeType.Binary: return(buildBinary((DBinary)node)); case NodeType.SysReq: return(buildSysReq((DSysReq)node)); case NodeType.Call: return(buildCall((DCall)node)); case NodeType.DeclareLocal: { var local = (DDeclareLocal)node; return(local.var.name); } case NodeType.TempName: { var name = (DTempName)node; return(name.name); } case NodeType.Global: { var global = (DGlobal)node; return(global.var.name); } case NodeType.InlineArray: { return(buildInlineArray((DInlineArray)node)); } default: throw new Exception("waT"); } }
public void replace(iterator_base where, DNode with) { replace(where.node, with); where.node = with; }
public iterator_base(DNode node) { node_ = node; }
public NodeList() { head_ = new DSentinel(); head_.prev = head_; head_.next = head_; }
public void replace(DNode where, DNode with) { with.setBlock(this); nodes_.replace(where, with); }
public void prepend(DNode node) { node.setBlock(this); nodes_.insertBefore(nodes_.last, node); }
public DReturn(DNode value) : base(value) { }
private string buildLoadStoreRef(DNode node) { switch (node.type) { case NodeType.TempName: { DTempName temp = (DTempName)node; return temp.name; } case NodeType.DeclareLocal: { DDeclareLocal local = (DDeclareLocal)node; return local.var.name; } case NodeType.ArrayRef: return buildArrayRef((DArrayRef)node); case NodeType.LocalRef: { DLocalRef lref = (DLocalRef)node; DDeclareLocal local = lref.local; if (local.var.type == VariableType.ArrayReference || local.var.type == VariableType.Array) return local.var.name + "[0]"; if (local.var.type == VariableType.Reference) return local.var.name; throw new Exception("unknown local ref"); } case NodeType.Global: { DGlobal global = (DGlobal)node; if (global.var == null) return "__unk"; return global.var.name; } case NodeType.Load: { DLoad load = (DLoad)node; Debug.Assert(load.from.type == NodeType.DeclareLocal || load.from.type == NodeType.ArrayRef || load.from.type == NodeType.Global); return buildLoadStoreRef(load.from); } default: throw new Exception("unknown load"); } }
private void propagateInputs(DNode lhs, DNode rhs) { lhs.typeSet.addTypes(rhs.typeSet); rhs.typeSet.addTypes(lhs.typeSet); }
private string buildExpression(DNode node) { switch (node.type) { case NodeType.Constant: return buildConstant((DConstant)node); case NodeType.Boolean: return buildBoolean((DBoolean)node); case NodeType.Float: return buildFloat((DFloat)node); case NodeType.Character: return buildCharacter((DCharacter)node); case NodeType.Function: return buildFunction((DFunction)node); case NodeType.Load: return buildLoad((DLoad)node); case NodeType.String: return buildString(((DString)node).value); case NodeType.LocalRef: return buildLocalRef((DLocalRef)node); case NodeType.ArrayRef: return buildArrayRef((DArrayRef)node); case NodeType.Unary: return buildUnary((DUnary)node); case NodeType.Binary: return buildBinary((DBinary)node); case NodeType.SysReq: return buildSysReq((DSysReq)node); case NodeType.Call: return buildCall((DCall)node); case NodeType.DeclareLocal: { DDeclareLocal local = (DDeclareLocal)node; return local.var.name; } case NodeType.TempName: { DTempName name = (DTempName)node; return name.name; } case NodeType.Global: { DGlobal global = (DGlobal)node; return global.var.name; } case NodeType.InlineArray: { return buildInlineArray((DInlineArray)node); } default: throw new Exception("waT"); } }
private static DNode GuessArrayBase(DNode op1, DNode op2) { if (op1.usedAsArrayIndex) return op2; if (op2.usedAsArrayIndex) return op1; if (op1.type == NodeType.ArrayRef || op1.type == NodeType.LocalRef || IsArray(op1.typeSet)) { return op1; } if (op2.type == NodeType.ArrayRef || op2.type == NodeType.LocalRef || IsArray(op2.typeSet)) { return op2; } return null; }
public void add(DNode node) { node.setBlock(this); nodes_.add(node); }
public void set(int offset, DNode value) { entry(offset).assignment = value; }
private void writeStatement(DNode node) { switch (node.type) { case NodeType.DeclareLocal: writeLocal((DDeclareLocal)node); break; case NodeType.DeclareStatic: writeStatic((DDeclareStatic)node); break; case NodeType.Jump: case NodeType.JumpCondition: case NodeType.Return: case NodeType.Switch: break; case NodeType.SysReq: writeSysReq((DSysReq)node); break; case NodeType.Call: { writeCall((DCall)node); break; } case NodeType.Store: writeStore((DStore)node); break; case NodeType.BoundsCheck: break; case NodeType.TempName: writeTempName((DTempName)node); break; case NodeType.IncDec: writeIncDec((DIncDec)node); break; default: throw new Exception(String.Format("unknown op ({0})", node.type.ToString())); } }
public void set(Register reg, DNode node) { if (reg == Register.Pri) pri_ = node; else alt_ = node; }
public override void visit(DConstant node) { DNode replacement = null; if (node.typeSet.numTypes == 1) { var tu = node.typeSet[0]; switch (tu.kind) { case TypeUnit.Kind.Cell: { switch (tu.type.type) { case CellType.Bool: replacement = new DBoolean(node.value != 0); break; case CellType.Character: replacement = new DCharacter(Convert.ToChar(node.value)); break; case CellType.Float: { //Debug.Assert(BitConverter.IsLittleEndian); var bits = BitConverter.GetBytes(node.value); var v = BitConverter.ToSingle(bits, 0); replacement = new DFloat(v); break; } case CellType.Function: { var p = graph_.file.publics[node.value >> 1]; var function = graph_.file.lookupFunction(p.address); replacement = new DFunction(p.address, function); break; } default: return; } break; } case TypeUnit.Kind.Array: { replacement = ConstantToReference(node, tu); break; } default: return; } } if (replacement == null && node.usedAsReference) { replacement = ConstantToReference(node, null); } if (replacement != null) { block_.nodes.insertAfter(node, replacement); node.replaceAllUsesWith(replacement); } }
private void joinRegs(Register reg, DNode value) { if (value == null || stack_.reg(reg) == value) return; if (stack_.reg(reg) == null) { stack_.set(reg, value); return; } DPhi phi; DNode node = stack_.reg(reg); if (node.type != NodeType.Phi || node.block != this) { phi = new DPhi(node); stack_.set(reg, phi); add(phi); } else { phi = (DPhi)node; } phi.addInput(value); }
public void append(DNode expression) { nodes_.Add(new Node(expression)); }
private static bool IsReallyLikelyArrayCompute(DNode node, DNode abase) { if (abase.type == NodeType.ArrayRef) return true; if (IsArray(abase.typeSet)) return true; foreach (DUse use in node.uses) { if (use.node.type == NodeType.Store || use.node.type == NodeType.Load) return true; } return false; }
private static bool IsArrayOpCandidate(DNode node) { if (node.type == NodeType.Load || node.type == NodeType.Store) return true; if (node.type == NodeType.Binary) { DBinary bin = (DBinary)node; return bin.spop == SPOpcode.add; } return false; }
private static Signature SignatureOf(DNode node) { if (node.type == NodeType.Call) return ((DCall)node).function; return ((DSysReq)node).native; }
private void visitSignature(DNode call, Signature signature) { for (int i = 0; i < call.numOperands && i < signature.args.Length; i++) { DNode node = call.getOperand(i); Argument arg = i < signature.args.Length ? signature.args[i] : signature.args[signature.args.Length - 1]; TypeUnit tu = TypeUnit.FromArgument(arg); if (tu != null) node.addType(tu); } // Peek ahead for constants. if (signature.args.Length > 0 && signature.args[signature.args.Length - 1].type == VariableType.Variadic) { for (int i = signature.args.Length - 1; i < call.numOperands; i++) { DNode node = call.getOperand(i); if (node.type != NodeType.Constant) continue; DConstant constNode = (DConstant)node; Variable global = graph_.file.lookupGlobal(constNode.value); if (global != null) { call.replaceOperand(i, new DGlobal(global)); continue; } // Guess a string... call.replaceOperand(i, new DString(graph_.file.stringFromData(constNode.value))); } } }
private string buildLoadStoreRef(DNode node) { switch (node.type) { case NodeType.TempName: { var temp = (DTempName)node; return(temp.name); } case NodeType.DeclareLocal: { var local = (DDeclareLocal)node; return(local.var.name); } case NodeType.ArrayRef: return(buildArrayRef((DArrayRef)node)); case NodeType.LocalRef: { var lref = (DLocalRef)node; var local = lref.local; if (local.var.type == VariableType.ArrayReference || local.var.type == VariableType.Array) { return(local.var.name + "[0]"); } if (local.var.type == VariableType.Reference) { return(local.var.name); } throw new Exception("unknown local ref"); } case NodeType.Global: { var global = (DGlobal)node; if (global.var == null) { return("__unk"); } return(global.var.name); } case NodeType.Load: { var load = (DLoad)node; Debug.Assert(load.from.type == NodeType.DeclareLocal || load.from.type == NodeType.ArrayRef || load.from.type == NodeType.Global); return(buildLoadStoreRef(load.from)); } default: throw new Exception("unknown load"); } }