Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor <foo> LTM resolution, start stubbing in dispatch methods
  • Loading branch information
sorear committed Mar 18, 2011
1 parent a074df5 commit c2fbfdf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 62 deletions.
67 changes: 19 additions & 48 deletions lib/Cursor.cs
Expand Up @@ -819,43 +819,10 @@ public struct Edge {
public int curfate;

public STable cursor_class;
public HashSet<string> method_stack = new HashSet<string>();
public HashSet<string> name_stack = new HashSet<string>();
public HashSet<string> used_methods = new HashSet<string>();
public List<Frame> outer_stack = new List<Frame>();
public Dictionary<string,LAD> method_cache = new Dictionary<string,LAD>();
public Dictionary<string,Frame> outer_cache = new Dictionary<string,Frame>();

public LAD ResolveMethod(string name, out Frame outer) {
LAD sub = null;
if (method_cache.TryGetValue(name, out sub)) {
if (Lexer.LtmTrace)
Console.WriteLine("+ Method HIT for {0}", name);
outer = outer_cache[name];
return sub;
}
if (Lexer.LtmTrace)
Console.WriteLine("+ Method MISS for {0}", name);
P6any method = cursor_class.Can(name);

if (Lexer.LtmTrace && method != null)
Console.WriteLine("+ Found method");

if (method == null) {
outer = outer_cache[name] = null;
return method_cache[name] = new LADImp();
}

sub = ((SubInfo)(((P6opaque)method).GetSlot("info"))).ltm;
outer = ((Frame)(((P6opaque)method).GetSlot("outer")));

if (Lexer.LtmTrace)
Console.WriteLine("+ {0} to sub-automaton",
(sub != null ? "Resolved" : "Failed to resolve"));

method_cache[name] = sub;
outer_cache[name] = outer;
return sub;
}
public List<Frame> outer_stack = new List<Frame>();
public List<SubInfo> info_stack = new List<SubInfo>();

public int AddNode() {
nodes_l.Add(new Node(curfate));
Expand Down Expand Up @@ -1240,29 +1207,28 @@ public class LADMethod : LAD {
return new LADImp();
pad.used_methods.Add(name);

if (pad.method_stack.Contains(name)) {
if (pad.name_stack.Contains(name)) {
// NFAs cannot be recursive, so treat this as the end of the
// declarative prefix.
if (Lexer.LtmTrace)
Console.WriteLine("+ Pruning to avoid recursion");
return new LADImp();
}

Frame outer;
LAD sub = pad.ResolveMethod(name, out outer);
DispatchEnt de;
if (!pad.cursor_class.mro_methods.TryGetValue(name, out de)
|| de.info.ltm == null)
return new LADImp();

pad.method_stack.Add(name);
pad.outer_stack.Add(outer);
pad.name_stack.Add(name);
pad.outer_stack.Add(de.outer);
pad.info_stack.Add(de.info);

LAD ret;
if (sub == null) {
ret = new LADImp();
} else {
ret = sub.Reify(pad);
}
LAD ret = de.info.ltm.Reify(pad);

pad.method_stack.Remove(name);
pad.name_stack.Remove(name);
pad.outer_stack.RemoveAt(pad.outer_stack.Count - 1);
pad.info_stack.RemoveAt(pad.info_stack.Count - 1);

return ret;
}
Expand Down Expand Up @@ -1292,8 +1258,10 @@ public class LADProtoRegex : LAD {

for (int i = 0; i < opts.Length; i++) {
pad.outer_stack.Add((Frame)cands[i].GetSlot("outer"));
pad.info_stack.Add((SubInfo)cands[i].GetSlot("info"));
opts[i] = ((SubInfo)cands[i].GetSlot("info")).ltm.Reify(pad);
pad.outer_stack.RemoveAt(pad.outer_stack.Count - 1);
pad.info_stack.RemoveAt(pad.info_stack.Count - 1);
}

return new LADAny(opts);
Expand Down Expand Up @@ -1466,6 +1434,7 @@ public class Lexer {
pad.cursor_class = kl;
LAD[] lads_p = new LAD[lads.Length];
pad.outer_stack.Add(fromf);
pad.info_stack.Add(fromf.info);
for (int i = 0; i < lads_p.Length; i++)
lads_p[i] = lads[i].Reify(pad);

Expand Down Expand Up @@ -1629,9 +1598,11 @@ public class Lexer {
pad.cursor_class = kl;
for (int i = 0; i < candidates.Length; i++) {
pad.outer_stack.Add((Frame) candidates[i].GetSlot("outer"));
pad.info_stack.Add((SubInfo) candidates[i].GetSlot("info"));
branches[i] = (((SubInfo) candidates[i].GetSlot("info")).ltm).
Reify(pad);
pad.outer_stack.RemoveAt(pad.outer_stack.Count - 1);
pad.info_stack.RemoveAt(pad.info_stack.Count - 1);
}
return lc.protorx_nfa[name] = new Lexer(pad, name, branches);
}
Expand Down
39 changes: 25 additions & 14 deletions lib/Kernel.cs
Expand Up @@ -213,25 +213,36 @@ public sealed class BValue {

// This stores all the invariant stuff about a Sub, i.e. everything
// except the outer pointer. Now distinct from protopads
//
// Actually not quite *in*variant; some of this stuff has to be
// changed, but it's rare by construction. We don't want to be
// like Rakudo/Parrot where simple sub cloning requires copying
// 100s of bytes.
public class SubInfo {
public int[] lines;
// Essential call functions
public DynBlockDelegate code;
public int nspill;
public int[] sig_i;
public object[] sig_r;

// Standard metadata
public Dictionary<string, int> dylex;
public uint dylex_filter; // (32,1) Bloom on hash code
public int[] lines;
public STable mo;
// for inheriting hints
public SubInfo outer;
public string name;
public Dictionary<string, BValue> hints;
// maybe should be a hint
public LAD ltm;
public int nspill;
public Dictionary<string, int> dylex;
public uint dylex_filter; // (32,1) Bloom on hash code
public int[] sig_i;
public object[] sig_r;

public object vtable_boxed;
public object vtable_unboxed;
// Used for closing runtime-generated SubInfo over values used
// For vtable wrappers: 0 = unboxed, 1 = boxed
// For dispatch routines, 0 = parameter list
public object param0, param1;

// No instance fields past this point
public const int SIG_I_RECORD = 3;
public const int SIG_I_FLAGS = 0;
public const int SIG_I_SLOT = 1;
Expand Down Expand Up @@ -1331,13 +1342,13 @@ public List<STable> superclasses
private object _GetVT(string name) {
DispatchEnt de;
mro_methods.TryGetValue(name, out de);
return de == null ? null : de.info.vtable_boxed;
return de == null ? null : de.info.param1;
}

private object _GetVTU(string name) {
DispatchEnt de;
mro_methods.TryGetValue(name, out de);
return de == null ? null : de.info.vtable_unboxed;
return de == null ? null : de.info.param0;
}

private void SetMRO(STable[] arr) {
Expand Down Expand Up @@ -2074,8 +2085,8 @@ public class Kernel {
SubInfo.SIG_F_RWTRANS | SubInfo.SIG_F_POSITIONAL,
0, 0 };
si.sig_r = new object[1] { "self" };
si.vtable_boxed = cvb;
si.vtable_unboxed = cvu;
si.param1 = cvb;
si.param0 = cvu;
kl.AddMethod(0, name, MakeSub(si, null));
}

Expand All @@ -2092,7 +2103,7 @@ public class Kernel {
SubInfo.SIG_F_RWTRANS | SubInfo.SIG_F_POSITIONAL, 1, 0
};
si.sig_r = new object[2] { "self", "$key" };
si.vtable_boxed = cv;
si.param1 = cv;
kl.AddMethod(0, name, MakeSub(si, null));
}

Expand Down Expand Up @@ -2359,7 +2370,7 @@ class LastFrameNode {
new VarDeque() };

SubMO = new STable("Sub");
SubInvokeSubSI.vtable_boxed = new InvokeSub();
SubInvokeSubSI.param1 = new InvokeSub();
SubMO.FillProtoClass(new string[] { "outer", "info" });
SubMO.AddMethod(0, "INVOKE", MakeSub(SubInvokeSubSI, null));
SubMO.Invalidate();
Expand Down

0 comments on commit c2fbfdf

Please sign in to comment.