Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add Sink handling w/ DCE
  • Loading branch information
sorear committed Dec 21, 2010
1 parent 7b1a299 commit 4c5e093
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions lib/CLRBackend.cs
Expand Up @@ -733,16 +733,44 @@ abstract class ClrOp {
public abstract void CodeGen(CgContext cx);
public virtual void ListCases(CgContext cx) { }

public virtual ClrOp Sink() {
throw (Returns == Tokens.Void)
? (Exception)new ArgumentException()
: new NotImplementedException();
}

protected static void TypeCheck(Type sub, Type super, string msg) {
if (!super.IsAssignableFrom(sub))
throw new Exception(msg + " " + sub + " not subtype of " + super);
}
}

// NOT FOR GENERAL USE: only in implementing Sink for Clr*** with
// irreducable operations
class ClrSink : ClrOp {
public readonly ClrOp zyg;
public override void ListCases(CgContext cx) {
zyg.ListCases(cx);
}
public override void CodeGen(CgContext cx) {
zyg.CodeGen(cx);
cx.il.Emit(OpCodes.Pop);
}
public ClrSink(ClrOp zyg) {
if (zyg.Returns == Tokens.Void)
throw new ArgumentException();
this.zyg = zyg;
Returns = Tokens.Void;
}
}

class ClrMethodCall : ClrOp {
public readonly MethodInfo Method;
public readonly ClrOp[] Zyg;

public override ClrOp Sink() {
return new ClrSink(this);
}
public override void CodeGen(CgContext cx) {
if (HasCases) {
cx.il.Emit(OpCodes.Ldarg_0);
Expand Down Expand Up @@ -807,6 +835,11 @@ class ClrContexty : ClrOp {
public readonly MethodInfo inv;
public readonly FieldInfo thing;

// This could be avoided in some cases, but probably +$customobj;
// shouldn't be optimized out
public override ClrOp Sink() {
return new ClrSink(this);
}
public override void CodeGen(CgContext cx) {
zyg[0].CodeGen(cx);
cx.make_ospill();
Expand All @@ -833,6 +866,13 @@ class ClrOperator : ClrOp {
public readonly OpCode op;
public readonly ClrOp[] zyg;

public override ClrOp Sink() {
ClrOp[] szyg = new ClrOp[zyg.Length];
for (int i = 0; i < szyg.Length; i++)
szyg[i] = zyg[i].Sink();
return new ClrSeq(szyg);
}

public override void CodeGen(CgContext cx) {
foreach (ClrOp c in zyg)
c.CodeGen(cx);
Expand All @@ -850,6 +890,11 @@ class ClrGetField : ClrOp {
public readonly FieldInfo f;
public readonly ClrOp zyg;

// Not strictly right, but Perl 6 code never sees CLR nulls, and
// this is a major win for some cases
public override ClrOp Sink() {
return zyg.Sink();
}
public override void CodeGen(CgContext cx) {
zyg.CodeGen(cx);
cx.il.Emit(OpCodes.Ldfld, f);
Expand Down Expand Up @@ -884,6 +929,7 @@ class ClrSetField : ClrOp {
class ClrGetSField : ClrOp {
public readonly FieldInfo f;

public override ClrOp Sink() { return ClrNoop.Instance; }
public override void CodeGen(CgContext cx) {
cx.il.Emit(OpCodes.Ldsfld, f);
}
Expand Down Expand Up @@ -914,6 +960,7 @@ class ClrPadGet : ClrOp {
public readonly int up;
public readonly int index;

public override ClrOp Sink() { return ClrNoop.Instance; }
public override void CodeGen(CgContext cx) {
cx.il.Emit(OpCodes.Ldarg_0);
for (int i = 0; i < up; i++)
Expand Down Expand Up @@ -959,6 +1006,20 @@ class ClrNoop : ClrOp {
public static ClrNoop Instance = new ClrNoop();
}

// only used in ClrOperator.Sink, and assumes it in the HasCases=false
class ClrSeq : ClrOp {
readonly ClrOp[] zyg;
public ClrSeq(ClrOp[] zyg) {
Returns = Tokens.Void;
HasCases = false;
this.zyg = zyg;
}
public override void CodeGen(CgContext cx) {
foreach(ClrOp z in zyg)
z.CodeGen(cx);
}
}

class ClrSubyCall : ClrOp {
public readonly string mname;
public readonly string sig;
Expand Down Expand Up @@ -1078,6 +1139,7 @@ class ClrPokeLet : ClrOp {

class ClrPeekLet : ClrOp {
string Name;
public override ClrOp Sink() { return ClrNoop.Instance; }
public ClrPeekLet(string name, Type letType) {
Name = name;
Returns = letType;
Expand All @@ -1099,6 +1161,7 @@ class ClrResult : ClrOp {
public ClrResult(Type letType) {
Returns = letType;
}
public override ClrOp Sink() { return ClrNoop.Instance; }
public override void CodeGen(CgContext cx) {
if (Returns == Tokens.Void)
return;
Expand All @@ -1111,6 +1174,9 @@ class ClrResult : ClrOp {
class ClrDropLet : ClrOp {
string Name;
ClrOp Inner;
public override ClrOp Sink() {
return new ClrDropLet(Name, Inner.Sink());
}
public ClrDropLet(string name, ClrOp inner) {
Name = name;
Inner = inner;
Expand Down Expand Up @@ -1200,6 +1266,7 @@ class ClrCpsReturn : ClrOp {

class ClrStringLiteral : ClrOp {
string data;
public override ClrOp Sink() { return ClrNoop.Instance; }
public ClrStringLiteral(string data) {
this.data = data;
Returns = Tokens.String;
Expand All @@ -1215,6 +1282,7 @@ class ClrStringLiteral : ClrOp {
// will also handle int8, int16, and unsigned versions thereof.
class ClrIntLiteral : ClrOp {
int data;
public override ClrOp Sink() { return ClrNoop.Instance; }
public ClrIntLiteral(Type ty, int data) {
this.data = data;
Returns = ty;
Expand All @@ -1227,6 +1295,7 @@ class ClrIntLiteral : ClrOp {

class ClrNumLiteral : ClrOp {
double data;
public override ClrOp Sink() { return ClrNoop.Instance; }
public ClrNumLiteral(double data) {
this.data = data;
Returns = Tokens.Double;
Expand Down Expand Up @@ -1408,6 +1477,10 @@ class CpsOp {
return new CpsOp(new ClrGetField(fi, heads[0]));
});
}

public static CpsOp Sink(CpsOp zyg) {
return new CpsOp(zyg.stmts, zyg.head.Sink());
}
}

class NamProcessor {
Expand Down Expand Up @@ -1528,12 +1601,15 @@ class NamProcessor {
return bit;
};

thandlers["sink"] = delegate(CpsOp[] z) {
return CpsOp.Sink(z[0]); };
thandlers["prog"] = CpsOp.Sequence;
thandlers["bif_defined"] = Contexty("mro_defined");
thandlers["bif_bool"] = Contexty("mro_Bool");
thandlers["bif_num"] = Contexty("mro_Numeric");
thandlers["bif_str"] = Contexty("mro_Str");
thandlers["fetch"] = Methody(null, Tokens.Variable_Fetch);
thandlers["obj_typename"] = Methody(null, Tokens.IP6.GetMethod("GetTypeName"));

foreach (KeyValuePair<string, Func<CpsOp[], CpsOp>> kv
in thandlers) {
Expand Down Expand Up @@ -1618,6 +1694,7 @@ public class CLRBackend {
});

unit.VisitSubsPostorder(delegate(int ix, StaticSub obj) {
Console.WriteLine(obj.name);
NamProcessor np = new NamProcessor(obj);
MethodInfo mi = DefineCpsMethod(
Unit.SharedName('C', ix, obj.name), false,
Expand Down

0 comments on commit 4c5e093

Please sign in to comment.