Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add signature encoder
  • Loading branch information
sorear committed Dec 23, 2010
1 parent 5370305 commit 95bc91e
Showing 1 changed file with 55 additions and 8 deletions.
63 changes: 55 additions & 8 deletions lib/CLRBackend.cs
Expand Up @@ -44,6 +44,8 @@ sealed class JScalar {
r[i] = (int)((JScalar)arr[i+cut]).num;
return r;
}
public static int I(object x) { return (int)((JScalar)x).num; }
public static string S(object x) { return x == null ? null : ((JScalar)x).str; }
public override string ToString() { return text; }
}

Expand Down Expand Up @@ -859,6 +861,10 @@ sealed class Tokens {
BValue.GetField("v");
public static readonly FieldInfo SubInfo_mo =
SubInfo.GetField("mo");
public static readonly FieldInfo SubInfo_sig_i =
SubInfo.GetField("sig_i");
public static readonly FieldInfo SubInfo_sig_r =
SubInfo.GetField("sig_r");
public static readonly FieldInfo DynObject_slots =
DynObject.GetField("slots");
public static readonly FieldInfo DMO_typeObject =
Expand Down Expand Up @@ -1227,7 +1233,7 @@ class ClrPadGet : ClrOp {
cx.il.Emit(OpCodes.Ldarg_0);
for (int i = 0; i < up; i++)
cx.il.Emit(OpCodes.Ldfld, Tokens.Frame_outer);
cx.EmitGetlex(index, Tokens.Variable);
cx.EmitGetlex(index + Tokens.NumInt32, Tokens.Variable);
}

public ClrPadGet(int up, int index) {
Expand All @@ -1246,9 +1252,9 @@ class ClrPadSet : ClrOp {
cx.il.Emit(OpCodes.Ldarg_0);
for (int i = 0; i < up; i++)
cx.il.Emit(OpCodes.Ldfld, Tokens.Frame_outer);
cx.EmitPreSetlex(index);
cx.EmitPreSetlex(index + Tokens.NumInt32);
zyg.CodeGen(cx);
cx.EmitSetlex(index, Tokens.Variable);
cx.EmitSetlex(index + Tokens.NumInt32, Tokens.Variable);
}

public ClrPadSet(int up, int index, ClrOp zyg) {
Expand Down Expand Up @@ -2259,7 +2265,7 @@ class NamProcessor {
FieldBuilder fb =
cpb.tb.DefineField("K" + cpb.module.constants++,
val.head.Returns, FieldAttributes.Static);
cpb.module.constantInit.Add(CpsOp.SetSField(fb, val));
cpb.module.thaw.Add(CpsOp.SetSField(fb, val));
return CpsOp.GetSField(fb);
}

Expand Down Expand Up @@ -2972,7 +2978,7 @@ public class CLRBackend {
Unit unit;

internal int constants;
internal List<CpsOp> constantInit = new List<CpsOp>();
internal List<CpsOp> thaw = new List<CpsOp>();

CLRBackend(string dir, string mobname, string filename) {
AssemblyName an = new AssemblyName(mobname);
Expand All @@ -2985,6 +2991,49 @@ public class CLRBackend {
TypeAttributes.Class | TypeAttributes.BeforeFieldInit);
}

void EncodeSignature(List<CpsOp> thaw, StaticSub obj) {
if (obj.sig == null) return;
List<int> sig_i = new List<int>();
List<CpsOp> sig_r = new List<CpsOp>();
object[] rsig = (object[]) obj.sig;
foreach (object p in rsig) {
object[] param = (object[]) p;
string name = JScalar.S(param[0]);
int flags = JScalar.I(param[1]);
string slot = JScalar.S(param[2]);
string[] names = JScalar.SA(0, param[3]);
Xref deflt = Xref.from(param[4] as object[]);

sig_r.Add(CpsOp.StringLiteral(name));
foreach (string n in names)
sig_r.Add(CpsOp.StringLiteral(n));
int ufl = 0;
if ((flags & 4) != 0) ufl |= SubInfo.SIG_F_RWTRANS;
else if ((flags & 64) == 0) ufl |= SubInfo.SIG_F_READWRITE;

if ((flags & 384) != 0) ufl |= SubInfo.SIG_F_BINDLIST;
if (deflt != null) {
ufl |= SubInfo.SIG_F_HASDEFAULT;
sig_r.Add(CpsOp.GetSField(((StaticSub)deflt.Resolve()).subinfo));
}
if ((flags & 16) != 0) ufl |= SubInfo.SIG_F_OPTIONAL;
if ((flags & 32) != 0) ufl |= SubInfo.SIG_F_POSITIONAL;
if ((flags & 1) != 0 && (flags & 256) != 0)
ufl |= SubInfo.SIG_F_SLURPY_NAM;
if ((flags & 1) != 0 && (flags & 256) == 0)
ufl |= SubInfo.SIG_F_SLURPY_POS;
if ((flags & 2) != 0) ufl |= SubInfo.SIG_F_SLURPY_CAP;
if ((flags & 8) != 0) ufl |= SubInfo.SIG_F_SLURPY_PCL;
sig_i.Add(ufl);
sig_i.Add(slot == null ? -1 : ((LexSimple)obj.l_lexicals[slot]).index);
sig_i.Add(names.Length);
}
thaw.Add(CpsOp.SetField(Tokens.SubInfo_sig_i,
CpsOp.GetSField(obj.subinfo), CpsOp.NewIntArray(sig_i.ToArray())));
thaw.Add(CpsOp.SetField(Tokens.SubInfo_sig_r,
CpsOp.GetSField(obj.subinfo), CpsOp.NewArray(typeof(object), sig_r.ToArray())));
}

void Process(Unit unit) {
this.unit = unit;

Expand All @@ -3001,8 +3050,6 @@ public class CLRBackend {
np.MakeBody();
});

List<CpsOp> thaw = new List<CpsOp>(constantInit);

foreach (object le in unit.log) {
object[] lea = (object[]) le;
string t = ((JScalar)lea[0]).str;
Expand Down Expand Up @@ -3149,7 +3196,7 @@ public class CLRBackend {
});

unit.VisitSubsPostorder(delegate(int ix, StaticSub obj) {
// TODO append chunks to Thaw here for sub3 stuff
EncodeSignature(thaw, obj);
});

CpsBuilder boot = new CpsBuilder(this, "BOOT", true);
Expand Down

0 comments on commit 95bc91e

Please sign in to comment.