Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implement serialization for SubInfo, LAD, LexInfo

  • Loading branch information...
commit b1088dc24d6b36955eb44e8063d2874aac946f04 1 parent c6ea422
@sorear authored
Showing with 217 additions and 22 deletions.
  1. +63 −2 lib/Cursor.cs
  2. +127 −20 lib/Kernel.cs
  3. +27 −0 lib/Serialize.cs
View
65 lib/Cursor.cs
@@ -1,4 +1,5 @@
using Niecza;
+using Niecza.Serialization;
using System;
using System.Collections.Generic;
using System.Text;
@@ -533,7 +534,7 @@ public class Cursor : P6any {
public CapInfo captures;
public STable save_klass;
- public override void Freeze(Niecza.Serialization.FreezeBuffer fb) { throw new NotImplementedException(); }
+ public override void Freeze(FreezeBuffer fb) { throw new NotImplementedException(); }
public string GetBacking() { return global.orig_s; }
public Cursor(P6any proto, string text, P6any actions)
@@ -935,9 +936,10 @@ public struct Edge {
}
// ltm automaton descriptors
-public abstract class LAD {
+public abstract class LAD : IFreeze {
public abstract void ToNFA(NFA pad, int from, int to);
public abstract void Dump(int indent);
+ public abstract void Freeze(FreezeBuffer fb);
public virtual void QueryLiteral(NFA pad, out int len, out bool cont) {
len = 0; cont = false;
}
@@ -970,6 +972,10 @@ public class LADStr : LAD {
public override void Dump(int indent) {
Console.WriteLine(new string(' ', indent) + "str: " + text);
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADStr);
+ fb.String(text);
+ }
}
public class LADStrNoCase : LAD {
@@ -997,6 +1003,10 @@ public class LADStrNoCase : LAD {
public override void Dump(int indent) {
Console.WriteLine(new string(' ', indent) + "strnocase: " + text);
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADStrNoCase);
+ fb.String(text);
+ }
}
public class LADCC : LAD {
@@ -1011,6 +1021,10 @@ public class LADCC : LAD {
public override void Dump(int indent) {
Console.WriteLine(new string(' ', indent) + "cc: " + cc.ToString());
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADCC);
+ fb.Ints(cc.vec);
+ }
}
public class LADImp : LAD {
@@ -1024,6 +1038,9 @@ public class LADImp : LAD {
public override void Dump(int indent) {
Console.WriteLine(new string(' ', indent) + "imp");
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADImp);
+ }
}
public class LADNull : LAD {
@@ -1039,6 +1056,9 @@ public class LADNull : LAD {
public override void QueryLiteral(NFA pad, out int len, out bool cont) {
len = 0; cont = true;
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADNull);
+ }
}
public class LADNone : LAD {
@@ -1049,6 +1069,9 @@ public class LADNone : LAD {
public override void Dump(int indent) {
Console.WriteLine(new string(' ', indent) + "none");
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADNone);
+ }
}
public class LADDot : LAD {
@@ -1060,6 +1083,9 @@ public class LADDot : LAD {
public override void Dump(int indent) {
Console.WriteLine(new string(' ', indent) + "dot");
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADDot);
+ }
}
public class LADStar : LAD {
@@ -1078,6 +1104,10 @@ public class LADStar : LAD {
Console.WriteLine(new string(' ', indent) + "star:");
child.Dump(indent + 4);
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADStar);
+ fb.ObjRef(child);
+ }
}
public class LADOpt : LAD {
@@ -1094,6 +1124,10 @@ public class LADOpt : LAD {
Console.WriteLine(new string(' ', indent) + "opt:");
child.Dump(indent + 4);
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADOpt);
+ fb.ObjRef(child);
+ }
}
public class LADPlus : LAD {
@@ -1118,6 +1152,10 @@ public class LADPlus : LAD {
Console.WriteLine(new string(' ', indent) + "plus:");
child.Dump(indent + 4);
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADPlus);
+ fb.ObjRef(child);
+ }
}
public class LADSequence : LAD {
@@ -1155,6 +1193,12 @@ public class LADSequence : LAD {
foreach (LAD l in args)
l.Dump(indent + 4);
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADSequence);
+ fb.Int(args.Length);
+ foreach (LAD l in args)
+ fb.ObjRef(l);
+ }
}
public class LADAny : LAD {
@@ -1177,6 +1221,12 @@ public class LADAny : LAD {
foreach (LAD k in zyg)
k.Dump(indent + 4);
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADAny);
+ fb.Int(zyg.Length);
+ foreach (LAD l in zyg)
+ fb.ObjRef(l);
+ }
}
public class LADParam : LAD {
@@ -1240,6 +1290,10 @@ public class LADParam : LAD {
public override void Dump(int indent) {
Console.WriteLine(new string(' ', indent) + "param: " + name);
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADParam);
+ fb.String(name);
+ }
}
public class LADMethod : LAD {
@@ -1291,6 +1345,10 @@ public class LADMethod : LAD {
public override void Dump(int indent) {
Console.WriteLine(new string(' ', indent) + "methodcall " + name);
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADMethod);
+ fb.String(name);
+ }
}
// Only really makes sense if used in the static scope of a proto
@@ -1332,6 +1390,9 @@ public class LADDispatcher : LAD {
public override void Dump(int indent) {
Console.WriteLine(new string(' ', indent) + "dispatcher");
}
+ public override void Freeze(FreezeBuffer fb) {
+ fb.Byte((byte) SerializationCode.LADDispatcher);
+ }
}
// These objects get put in hash tables, so don't change nstates[] after
View
147 lib/Kernel.cs
@@ -650,6 +650,11 @@ public abstract class LexInfo {
}
public virtual void BindFields() {}
public virtual int SigIndex() { return -1; }
+
+ internal abstract void DoFreeze(FreezeBuffer fb);
+ internal enum LexSerCode {
+ Simple, Sub, Label, Dispatch, Common, Hint, Package, Alias
+ }
}
public abstract class LIVarish : LexInfo {
@@ -725,6 +730,11 @@ public class LICommon : LexInfo {
return new ClrMethodCall(false, Tokens.Kernel_BindGlobal,
new ClrStringLiteral(hkey), to);
}
+
+ internal override void DoFreeze(FreezeBuffer fb) {
+ fb.Byte((byte)LexSerCode.Common);
+ fb.String(hkey);
+ }
}
public class LIHint : LexInfo {
@@ -747,6 +757,10 @@ public class LIHint : LexInfo {
return new ClrSetField(Tokens.BValue_v,
Backend.currentUnit.RefConstant(name, var, null).head, to);
}
+
+ internal override void DoFreeze(FreezeBuffer fb) {
+ fb.Byte((byte)LexSerCode.Hint);
+ }
}
public class LISub : LIVarish {
@@ -755,6 +769,11 @@ public class LISub : LIVarish {
public override void Init(Frame f) {
Set(f, Kernel.NewROScalar(def.protosub));
}
+ internal override void DoFreeze(FreezeBuffer fb) {
+ fb.Byte((byte)LexSerCode.Sub);
+ fb.Int(index);
+ fb.ObjRef(def);
+ }
}
public class LISimple : LIVarish {
@@ -770,6 +789,12 @@ public class LISimple : LIVarish {
this.flags = flags;
this.type = type;
}
+ internal override void DoFreeze(FreezeBuffer fb) {
+ fb.Byte((byte)LexSerCode.Simple);
+ fb.Int(index);
+ fb.Byte((byte)flags);
+ fb.ObjRef(type);
+ }
public override void Init(Frame f) {
if ((flags & NOINIT) != 0)
return;
@@ -790,12 +815,20 @@ public class LILabel : LIVarish {
public override void Init(Frame f) {
Set(f, Kernel.NewLabelVar(f, name));
}
+ internal override void DoFreeze(FreezeBuffer fb) {
+ fb.Byte((byte)LexSerCode.Label);
+ fb.Int(index);
+ }
}
public class LIDispatch : LIVarish {
public override void Init(Frame f) {
throw new Exception("MMD NYI");
}
+ internal override void DoFreeze(FreezeBuffer fb) {
+ fb.Byte((byte)LexSerCode.Dispatch);
+ fb.Int(index);
+ }
}
public class LIAlias : LexInfo {
@@ -833,6 +866,10 @@ public class LIAlias : LexInfo {
}
return real.SetCode(up, bind);
}
+ internal override void DoFreeze(FreezeBuffer fb) {
+ fb.Byte((byte)LexSerCode.Alias);
+ fb.String(to);
+ }
}
public class LIPackage : LexInfo {
@@ -844,6 +881,10 @@ public class LIPackage : LexInfo {
return Backend.currentUnit.RefConstant(pkg.name + "V",
pkg.typeVar, typeof(Variable)).head;
}
+ internal override void DoFreeze(FreezeBuffer fb) {
+ fb.Byte((byte)LexSerCode.Package);
+ fb.ObjRef(pkg);
+ }
}
// This stores all the invariant stuff about a Sub, i.e. everything
@@ -860,39 +901,32 @@ public class SubInfo : IFreeze {
public int[] sig_i;
public object[] sig_r;
- // Basic metadata
+ // Local metadata
public int[] lines;
public Dictionary<string, LexInfo> dylex;
public uint dylex_filter; // (32,1) Bloom on hash code
+ public string name;
+ // maybe should be in extend or a hint?
+ public LAD ltm;
+ public int special;
+ public int phaser;
+ public string outervar;
// References to related objects
+ public RuntimeUnit unit;
public SubInfo outer;
public P6any protosub;
public Frame protopad;
- public RuntimeUnit unit;
-
+ public STable cur_pkg, methodof, body_of, in_class;
public STable mo;
- // Standard metadata
- public string name;
- // maybe should be a hint
- public LAD ltm;
-
- public int special;
- public int phaser;
- public STable cur_pkg, methodof, body_of, in_class;
- public string outervar;
+ // caches for fast $OUTER::_, invocants, exn handling
public int outer_topic_rank;
public int outer_topic_key;
public int self_key;
public SubInfo catch_, control;
- public class UsedInScopeInfo {
- public string file;
- public int line;
- public int levels;
- public string orig_file;
- public int orig_line;
- }
+
+ // this is used only at compile time
public Dictionary<string,UsedInScopeInfo> used_in_scope;
public int num_lex_slots;
@@ -908,6 +942,13 @@ public class UsedInScopeInfo {
public Dictionary<string,object[]> extend;
// No instance fields past this point
+ public class UsedInScopeInfo {
+ public string file;
+ public int line;
+ public int levels;
+ public string orig_file;
+ public int orig_line;
+ }
public const int RUN_ONCE = 1;
public const int MAKE_PROTOPAD = 2;
@@ -1451,7 +1492,73 @@ public class UsedInScopeInfo {
void IFreeze.Freeze(FreezeBuffer fb) {
fb.Byte((byte)SerializationCode.SubInfo);
- throw new NotImplementedException();
+ // TODO: make serialization work with the code
+ fb.Int(nspill);
+ fb.Ints(sig_i);
+ if (sig_i != null) {
+ fb.Int(sig_r.Length);
+ foreach (object o in sig_r) {
+ if (o is string) {
+ fb.ObjRef(null);
+ fb.String((string)o);
+ } else {
+ fb.ObjRef((IFreeze)o);
+ }
+ }
+ }
+
+ fb.Ints(lines);
+ fb.Int(dylex.Count);
+ foreach (KeyValuePair<string, LexInfo> kv in dylex) {
+ fb.String(kv.Key);
+ kv.Value.DoFreeze(fb);
+ }
+ // not saving dylex_filter as it is a cache
+ fb.String(name);
+ fb.ObjRef(ltm);
+ fb.Int(special);
+ fb.Int(phaser);
+ fb.String(outervar);
+ fb.ObjRef(unit);
+ fb.ObjRef(outer);
+ fb.ObjRef(protosub);
+ fb.ObjRef(protopad);
+ fb.ObjRef(cur_pkg);
+ fb.ObjRef(methodof);
+ fb.ObjRef(body_of);
+ fb.ObjRef(in_class);
+ fb.ObjRef(mo);
+ // not storing caches here
+ // also not storing used_in_scope, it's compiler only data
+ // TODO: we want to store the compiled code not this
+ fb.String(nam_str);
+ if (nam_str != null) {
+ fb.Int(nam_refs.Length);
+ foreach(object o in nam_refs)
+ fb.ObjRef((IFreeze)o);
+ }
+ // can't save param0/param1. Or can we?
+ fb.Int(children.Count);
+ foreach(SubInfo si in children)
+ fb.ObjRef(si);
+ if (extend == null)
+ fb.Int(-1);
+ else {
+ fb.Int(extend.Count);
+ foreach(KeyValuePair<string,object[]> kv in extend) {
+ fb.String(kv.Key);
+ fb.Int(kv.Value.Length);
+ foreach (object o in kv.Value) {
+ if (o is int) {
+ fb.Byte(0);
+ fb.Int((int)o);
+ } else {
+ fb.Byte(1);
+ fb.String((string)o);
+ }
+ }
+ }
+ }
}
}
View
27 lib/Serialize.cs
@@ -201,6 +201,23 @@ enum SerializationCode : byte {
NewArrayViviHook,
HashViviHook,
NewHashViviHook,
+
+ // Longest-token automaton descriptors
+ LADNone, // no-args
+ LADNull,
+ LADDot,
+ LADDispatcher,
+ LADImp,
+ LADStr, // string
+ LADStrNoCase,
+ LADMethod,
+ LADParam,
+ LADOpt, // LAD
+ LADPlus,
+ LADStar,
+ LADSequence, // LAD[]
+ LADAny,
+ LADCC, // CC
}
// An instance of this class is used to serialize serialization units
@@ -273,6 +290,16 @@ public class FreezeBuffer {
}
}
+ public void Ints(int[] s) {
+ if (s == null) {
+ Int(-1);
+ } else {
+ Int(s.Length);
+ foreach (int ch in s)
+ Int(ch);
+ }
+ }
+
// This is the main routine you should call from your Freeze
// callbacks to freeze an object
public void ObjRef(IFreeze o) {
Please sign in to comment.
Something went wrong with that request. Please try again.