Skip to content

Commit

Permalink
Use new array munging helpers whereever possible in the kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Jun 2, 2012
1 parent ba74410 commit 7aa4b5e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 76 deletions.
4 changes: 1 addition & 3 deletions lib/Builtins.cs
Expand Up @@ -2577,11 +2577,9 @@ class CrossSource: ItemSource {
}

public static Frame dispatch_fromtype(Frame th) {
Variable[] npos = new Variable[th.pos.Length - 2];
STable from = th.pos[1].Fetch().mo;
string name = th.pos[2].Fetch().mo.mro_raw_Str.Get(th.pos[2]);
npos[0] = th.pos[0];
Array.Copy(th.pos, 3, npos, 1, npos.Length - 1);
var npos = Utils.PrependArr(th.pos, th.pos[0], 3);

if (!npos[0].Fetch().Does(from)) {
return Kernel.Die(th, "Cannot dispatch to a method on " +
Expand Down
26 changes: 9 additions & 17 deletions lib/CodeGen.cs
Expand Up @@ -1783,10 +1783,8 @@ class CpsOp {
// TODO: it's not really necessary to force a full cps-format
// node here. But we don't have universal recursion for
// non-cps nodes.
ClrOp[] body = new ClrOp[z.stmts.Length + 1];
Array.Copy(z.stmts, 0, body, 1, body.Length - 1);
body[0] = ClrSync.Instance;
return new CpsOp(body, z.head);
return new CpsOp(Utils.PrependArr(z.stmts, ClrSync.Instance),
z.head);
}

public static CpsOp Ternary(CpsOp cond, CpsOp iftrue, CpsOp iffalse) {
Expand Down Expand Up @@ -2554,9 +2552,8 @@ class NamProcessor {
return CpsOp.Sequence(ops.ToArray());
};
handlers["_inline"] = delegate(NamProcessor th, object[] zyg) {
object[] rzyg = new object[zyg.Length - 2];
Array.Copy(zyg, 2, rzyg, 0, rzyg.Length);
return th.Scan(th.InlineCall((SubInfo)zyg[1], rzyg));
return th.Scan(th.InlineCall((SubInfo)zyg[1],
Utils.TrimArr(zyg,2,0)));
};
handlers["letscope"] = delegate(NamProcessor th, object[] zyg) {
List<ClrEhSpan> xn = new List<ClrEhSpan>();
Expand Down Expand Up @@ -3233,10 +3230,8 @@ class NamProcessor {
static Func<CpsOp[], CpsOp> RxCall(Type cps, string name) {
MethodInfo mi = Tokens.RxFrame.GetMethod(name);
return delegate(CpsOp[] cpses) {
CpsOp[] n = new CpsOp[cpses.Length + 1];
Array.Copy(cpses, 0, n, 1, cpses.Length);
n[0] = CpsOp.RxFrame();
return CpsOp.CpsCall(cps, mi, n); };
return CpsOp.CpsCall(cps, mi,
Utils.PrependArr(cpses, CpsOp.RxFrame())); };
}

static Func<CpsOp[], CpsOp> Constructy(ConstructorInfo mi) {
Expand All @@ -3262,10 +3257,9 @@ class NamProcessor {
FieldInfo f = Tokens.STable.GetField(name);
MethodInfo g = f.FieldType.GetMethod("Invoke");
return delegate(CpsOp[] cpses) {
CpsOp[] args = new CpsOp[cpses.Length - 1];
Array.Copy(cpses, 1, args, 0, args.Length);
return CpsOp.Contexty(f, g, new CpsOp[2] {
cpses[0], CpsOp.NewArray(Tokens.Variable, args) });
cpses[0], CpsOp.NewArray(Tokens.Variable,
Utils.TrimArr(cpses,1,0)) });
};
}

Expand Down Expand Up @@ -4622,11 +4616,9 @@ public class DowncallReceiver : CallReceiver {
}
public static object sub_set_extend(object[] args) {
SubInfo s = (SubInfo)Handle.Unbox(args[1]);
object[] val = new object[args.Length - 3];
Array.Copy(args, 3, val, 0, val.Length);
if (s.extend == null)
s.extend = new Dictionary<string,object[]>();
s.extend[(string)args[2]] = val;
s.extend[(string)args[2]] = Utils.TrimArr(args,3,0);
return null;
}
public static object sub_get_extend(object[] args) {
Expand Down
49 changes: 14 additions & 35 deletions lib/Kernel.cs
Expand Up @@ -1006,10 +1006,8 @@ public sealed class RuntimeUnit : IFreeze {
n.name = tb.String();
string[] srcinfo = tb.Strings();
if (Builtins.upcall_receiver != null) {
object[] args = new object[srcinfo.Length + 1];
Array.Copy(srcinfo, 0, args, 1, srcinfo.Length);
args[0] = "check_dated";
object result = Builtins.UpCall(args);
object result = Builtins.UpCall(Utils.PrependArr<object>(
srcinfo, "check_dated"));
if (result is Exception)
throw (Exception)result;
if ((string)result != "ok")
Expand Down Expand Up @@ -2421,8 +2419,7 @@ public class Frame: P6any, IFixup {
if (sub != null && poscap.Length > 0 && sub.Does(Kernel.MethodMO)) {
// Hide the self value so that using |callframe.args in a
// nextwith call will DTRT
poscap = new Variable[pos.Length - 1];
Array.Copy(pos, 1, poscap, 0, poscap.Length);
poscap = Utils.TrimArr(pos,1,0);
}
nw.SetSlot(Kernel.CaptureMO, "$!positionals", poscap);
nw.SetSlot(Kernel.CaptureMO, "$!named", named);
Expand Down Expand Up @@ -2663,8 +2660,7 @@ public class Frame: P6any, IFixup {
goto gotit;
}
P6any nw = new P6opaque(Kernel.CaptureMO);
Variable[] spos = new Variable[pos.Length - posc];
Array.Copy(pos, posc, spos, 0, spos.Length);
var spos = Utils.TrimArr(pos, posc, 0);
VarHash snamed = null;
if (named != null) {
snamed = new VarHash();
Expand Down Expand Up @@ -3056,10 +3052,8 @@ class InvokeSub : InvokeHandler {
class InvokeCallMethod : InvokeHandler {
public override Frame Invoke(P6any th, Frame caller,
Variable[] pos, VarHash named) {
Variable[] np = new Variable[pos.Length + 1];
Array.Copy(pos, 0, np, 1, pos.Length);
np[0] = th;
return th.InvokeMethod(caller, "postcircumfix:<( )>", np, named);
return th.InvokeMethod(caller, "postcircumfix:<( )>",
Utils.PrependArr(pos, th), named);
}
}

Expand All @@ -3071,10 +3065,7 @@ class PushyCallMethod : PushyHandler {
protected override void SetData(object[]o) { method = (string)o[0]; }

public override Variable Invoke(Variable obj, Variable[] args) {
Variable[] rargs = new Variable[args.Length + 1];
Array.Copy(args, 0, rargs, 1, args.Length);
rargs[0] = obj;
return Kernel.RunInferior(obj.Fetch().InvokeMethod(Kernel.GetInferiorRoot(), method, rargs, null));
return Kernel.RunInferior(obj.Fetch().InvokeMethod(Kernel.GetInferiorRoot(), method, Utils.PrependArr(args, obj), null));
}
}

Expand Down Expand Up @@ -4704,11 +4695,8 @@ public class Kernel {
}

private static Frame SubInvokeSubC(Frame th) {
Variable[] post;
post = new Variable[th.pos.Length - 1];
Array.Copy(th.pos, 1, post, 0, th.pos.Length - 1);
return CodeMO.mro_INVOKE.Invoke((P6opaque)th.pos[0].Fetch(),
th.Return(), post, th.named);
th.Return(), Utils.TrimArr(th.pos,1,0), th.named);
}

private static Frame JunctionFallbackC(Frame th) {
Expand All @@ -4720,8 +4708,7 @@ public class Kernel {
th.lex1 = new Variable[((Variable[])th.lex0).Length];
th.lex2 = jo.slots[0];
th.lex3 = th.pos[1].Fetch().mo.mro_raw_Str.Get(th.pos[1]);
th.lex4 = new Variable[th.pos.Length - 1];
Array.Copy(th.pos, 2, (Variable[])th.lex4, 1, th.pos.Length-2);
th.lex4 = Utils.PrependArr(th.pos, null, 2);
}

Variable[] src = (Variable[]) th.lex0;
Expand Down Expand Up @@ -5600,9 +5587,8 @@ internal class MMDCandidate : MultiCandidate {
PushyHandler fcv = (PushyHandler) th.info.param[1];
Variable[] fullpc = UnboxAny<Variable[]>(
((Variable)th.lex1).Fetch());
Variable[] chop = new Variable[fullpc.Length - 1];
Array.Copy(fullpc, 1, chop, 0, chop.Length);
th.caller.resultSlot = fcv.Invoke((Variable)th.lex0, chop);
th.caller.resultSlot = fcv.Invoke((Variable)th.lex0,
Utils.TrimArr(fullpc, 1, 0));
return th.Return();
}
private static void WrapPushy(STable kl, string name,
Expand Down Expand Up @@ -5631,11 +5617,8 @@ internal class MMDCandidate : MultiCandidate {

if (pos.Length > 2) {
Variable inter = ((IndexHandler)p[0]).Get(self, index);
Variable[] npos = new Variable[pos.Length - 1];
Array.Copy(pos, 2, npos, 1, pos.Length - 2);
npos[0] = inter;
return inter.Fetch().InvokeMethod(th.Return(), (string)p[4],
npos, n);
Utils.PrependArr(pos, inter, 2), n);
} else if (index == null) {
// TODO: handle adverbs on Zen slices (XXX does this make sense?)
res = self.List ? self : Kernel.NewRWListVar(self.Fetch());
Expand Down Expand Up @@ -5728,9 +5711,7 @@ internal class MMDCandidate : MultiCandidate {
static STable ToComposable(STable arg, STable cls) {
if (arg.mo.type == P6how.CURRIED_ROLE) {
STable r;
Variable[] pass = new Variable[arg.mo.curriedArgs.Length+1];
pass[0] = cls.typeObj;
Array.Copy(arg.mo.curriedArgs, 0, pass, 1, pass.Length-1);
var pass = Utils.PrependArr(arg.mo.curriedArgs, cls.typeObj);
Frame ifr = (Frame)RunInferior(arg.mo.roleFactory.
Invoke(GetInferiorRoot(), pass, null)).Fetch();

Expand Down Expand Up @@ -6497,9 +6478,7 @@ class LastFrameNode {
var tp = (Variable[]) o.slots[0];
n = o.slots[1] as VarHash;
// nextdispatch copies over the old self
p = new Variable[tp.Length + 1];
Array.Copy(tp, 0, p, 1, tp.Length);
p[0] = tf.pos[0];
p = Utils.PrependArr(tp, tf.pos[0]);
}
return de.info.SetupCall(tf.caller, de.outer, de.ip6, p, n,
false, de);
Expand Down
7 changes: 2 additions & 5 deletions lib/NieczaCLR.cs
Expand Up @@ -330,8 +330,7 @@ class OverloadCandidate : MultiCandidate {

if (pi.Length != 0 && pi[pi.Length-1].GetCustomAttributes(
typeof(ParamArrayAttribute), false).Length != 0) {
Type[] args2 = new Type[args1.Length - 1];
Array.Copy(args1, 0, args2, 0, args2.Length);
Type[] args2 = Utils.TrimArr(args1, 0, 1);
into.Add(new OverloadCandidate(what, args2, refs,
args1[args1.Length - 1].GetElementType()));
}
Expand Down Expand Up @@ -651,9 +650,7 @@ public class CLRWrapperProvider {
m.how = Kernel.BoxRaw(m, Kernel.ClassHOWMO);
STable pm = t.BaseType == null ? Kernel.AnyMO :
GetWrapper(t.BaseType);
STable[] mro = new STable[pm.mo.mro.Length + 1];
Array.Copy(pm.mo.mro, 0, mro, 1, pm.mo.mro.Length);
mro[0] = m;
var mro = Utils.PrependArr(pm.mo.mro, m);
m.FillClass(new string[] { }, new STable[] { }, new STable[] { pm }, mro);
foreach (Type ity in t.GetInterfaces())
m.mo.role_typecheck_list.Add(GetWrapper(ity));
Expand Down
19 changes: 6 additions & 13 deletions lib/ObjModel.cs
Expand Up @@ -54,12 +54,9 @@ public abstract class P6any: Variable, IFreeze {
pos, named, false, m);
}
if (mo.mro_methods.TryGetValue("FALLBACK", out m)) {
Variable[] npos = new Variable[pos.Length + 1];
Array.Copy(pos, 1, npos, 2, pos.Length - 1);
npos[0] = pos[0];
npos[1] = Kernel.BoxAnyMO(name, Kernel.StrMO);
return m.info.SetupCall(caller, m.outer, m.ip6,
npos, named, false, m);
Utils.PrependArr(pos, 1, pos[0], Builtins.MakeStr(name)),
named, false, m);
}
return Fail(caller, "Unable to resolve method " + name);
}
Expand Down Expand Up @@ -495,21 +492,17 @@ public class DispatchSet {
if (parent == null) {
FillClass(slots, slot_types, new STable[] {}, new STable[] { stable });
} else {
STable[] mro = new STable[parent.mo.mro.Length + 1];
Array.Copy(parent.mo.mro, 0, mro, 1, mro.Length-1);
mro[0] = stable;
FillClass(slots, slot_types, new STable[] { parent }, mro);
FillClass(slots, slot_types, new STable[] { parent },
Utils.PrependArr(parent.mo.mro, stable));
}
Invalidate();
}

public void FillSubset(STable super) {
stable.useAcceptsType = true;
type = SUBSET; rtype = "subset";
STable[] mro = new STable[super.mo.mro.Length + 1];
Array.Copy(super.mo.mro, 0, mro, 1, mro.Length - 1);
mro[0] = stable;
FillClass(super.all_slot, super.type_slot, new STable[] { super }, mro);
FillClass(super.all_slot, super.type_slot, new STable[] { super },
Utils.PrependArr(super.mo.mro, stable));
Revalidate();
stable.SetupVTables();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Utils.cs
Expand Up @@ -684,9 +684,9 @@ public class Utils {
return narr;
}

public static T[] PrependArr<T>(T[] arr,T addit) {
T[] narr = new T[arr.Length + 1];
Array.Copy(arr, 0, narr, 1, arr.Length);
public static T[] PrependArr<T>(T[] arr,T addit,int cut = 0) {
T[] narr = new T[arr.Length + 1 - cut];
Array.Copy(arr, cut, narr, 1, arr.Length - cut);
narr[0] = addit;
return narr;
}
Expand Down

0 comments on commit 7aa4b5e

Please sign in to comment.