Moved call-site behavior to base class

This commit is contained in:
arookas 2016-02-01 12:33:04 -05:00
parent 436718ecf0
commit c3e358adba

View file

@ -48,6 +48,7 @@ namespace arookas {
abstract class sunSymbol { abstract class sunSymbol {
string mName; string mName;
sunSymbolModifiers mModifiers; sunSymbolModifiers mModifiers;
List<sunRelocation> mRelocations;
public string Name { public string Name {
get { return mName; } get { return mName; }
@ -57,16 +58,33 @@ namespace arookas {
set { mModifiers = value; } set { mModifiers = value; }
} }
public bool HasRelocations {
get { return mRelocations.Count > 0; }
}
// symbol table // symbol table
public abstract sunSymbolType Type { get; } public abstract sunSymbolType Type { get; }
public abstract uint Data { get; } public abstract uint Data { get; }
protected sunSymbol(string name) { protected sunSymbol(string name) {
mName = name; mName = name;
mRelocations = new List<sunRelocation>(10);
} }
public abstract void Compile(sunCompiler compiler); public abstract void Compile(sunCompiler compiler);
public void OpenRelocation(sunRelocation relocation) {
if (relocation == null) {
throw new ArgumentNullException("relocation");
}
mRelocations.Add(relocation);
}
public void CloseRelocations(sunCompiler compiler) {
foreach (var relocation in mRelocations) {
relocation.Relocate(compiler);
}
}
public static sunSymbolModifiers GetModifiers(sunNode modifierlist) { public static sunSymbolModifiers GetModifiers(sunNode modifierlist) {
if (modifierlist == null) { if (modifierlist == null) {
return sunSymbolModifiers.None; return sunSymbolModifiers.None;
@ -89,20 +107,14 @@ namespace arookas {
get { return mParameters; } get { return mParameters; }
} }
public abstract bool HasCallSites { get; }
protected sunCallableSymbol(string name, sunParameterInfo parameterInfo) protected sunCallableSymbol(string name, sunParameterInfo parameterInfo)
: base(name) { : base(name) {
mParameters = parameterInfo; mParameters = parameterInfo;
} }
public abstract void OpenCallSite(sunCompiler compiler, int argumentCount);
public abstract void CloseCallSites(sunCompiler compiler);
} }
class sunBuiltinSymbol : sunCallableSymbol { class sunBuiltinSymbol : sunCallableSymbol {
int mIndex; int mIndex;
List<sunBuiltinCallSite> mCallSites;
public int Index { public int Index {
get { return mIndex; } get { return mIndex; }
@ -122,57 +134,21 @@ namespace arookas {
public sunBuiltinSymbol(string name, sunParameterInfo parameters, int index) public sunBuiltinSymbol(string name, sunParameterInfo parameters, int index)
: base(name, parameters) { : base(name, parameters) {
mIndex = index; mIndex = index;
mCallSites = new List<sunBuiltinCallSite>(10);
} }
public override void Compile(sunCompiler compiler) { public override void Compile(sunCompiler compiler) {
// don't compile builtins // don't compile builtins
} }
public override void OpenCallSite(sunCompiler compiler, int argumentCount) {
var callSite = new sunBuiltinCallSite(compiler.Binary.OpenPoint(), argumentCount);
mCallSites.Add(callSite);
compiler.Binary.WriteFUNC(0, 0); // dummy
}
public override void CloseCallSites(sunCompiler compiler) {
compiler.Binary.Keep();
foreach (var callSite in mCallSites) {
compiler.Binary.Goto(callSite.Point);
compiler.Binary.WriteFUNC(mIndex, callSite.ArgCount);
}
compiler.Binary.Back();
}
struct sunBuiltinCallSite {
sunPoint mPoint;
int mArgCount;
public sunPoint Point {
get { return mPoint; }
}
public int ArgCount {
get { return mArgCount; }
}
public sunBuiltinCallSite(sunPoint point, int argCount) {
mPoint = point;
mArgCount = argCount;
}
}
} }
class sunFunctionSymbol : sunCallableSymbol { class sunFunctionSymbol : sunCallableSymbol {
uint mOffset; uint mOffset;
sunNode mBody; sunNode mBody;
List<sunPoint> mCallSites;
public uint Offset { public uint Offset {
get { return mOffset; } get { return mOffset; }
} }
public override bool HasCallSites {
get { return mCallSites.Count > 0; }
}
// symbol table // symbol table
public override sunSymbolType Type { public override sunSymbolType Type {
get { return sunSymbolType.Function; } get { return sunSymbolType.Function; }
@ -186,7 +162,6 @@ namespace arookas {
if (body == null) { if (body == null) {
throw new ArgumentNullException("body"); throw new ArgumentNullException("body");
} }
mCallSites = new List<sunPoint>(5);
mBody = body; mBody = body;
} }
@ -203,15 +178,6 @@ namespace arookas {
compiler.Binary.WriteRET0(); compiler.Binary.WriteRET0();
compiler.Context.Scopes.Pop(); compiler.Context.Scopes.Pop();
} }
public override void OpenCallSite(sunCompiler compiler, int argumentCount) {
var point = compiler.Binary.WriteCALL(argumentCount);
mCallSites.Add(point);
}
public override void CloseCallSites(sunCompiler compiler) {
foreach (var callSite in mCallSites) {
compiler.Binary.ClosePoint(callSite, Offset);
}
}
} }
class sunParameterInfo : IEnumerable<string> { class sunParameterInfo : IEnumerable<string> {