Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opcode Metadata #1515

Merged
merged 8 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 106 additions & 63 deletions DMCompiler/Bytecode/DreamProcOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,102 +8,102 @@
namespace DMCompiler.Bytecode;

public enum DreamProcOpcode : byte {
BitShiftLeft = 0x1,
PushType = 0x2,
PushString = 0x3,
[OpcodeMetadata(stackDelta: -1)] BitShiftLeft = 0x1,
[OpcodeMetadata(stackDelta: 1)] PushType = 0x2,
[OpcodeMetadata(stackDelta: 1)] PushString = 0x3,
FormatString = 0x4,
SwitchCaseRange = 0x5,
PushReferenceValue = 0x6,
[OpcodeMetadata(stackDelta: -2)]SwitchCaseRange = 0x5, //This could either shrink the stack by 2 or 3. Assume 2.
[OpcodeMetadata(stackDelta: 1)] PushReferenceValue = 0x6, // TODO: Local refs should be pure, and other refs that aren't modified
//0x7
Add = 0x8,
[OpcodeMetadata(stackDelta: -1)] Add = 0x8,
Assign = 0x9,
Call = 0xA,
MultiplyReference = 0xB,
JumpIfFalse = 0xC,
JumpIfTrue = 0xD,
[OpcodeMetadata(stackDelta: -1)] JumpIfFalse = 0xC,
[OpcodeMetadata(stackDelta: -1)] JumpIfTrue = 0xD,
Jump = 0xE,
CompareEquals = 0xF,
Return = 0x10,
PushNull = 0x11,
Subtract = 0x12,
CompareLessThan = 0x13,
CompareGreaterThan = 0x14,
BooleanAnd = 0x15,
[OpcodeMetadata(stackDelta: -1)] CompareEquals = 0xF,
[OpcodeMetadata(stackDelta: -1)] Return = 0x10,
[OpcodeMetadata(stackDelta: 1)] PushNull = 0x11,
[OpcodeMetadata(stackDelta: -1)] Subtract = 0x12,
[OpcodeMetadata(stackDelta: -1)] CompareLessThan = 0x13,
[OpcodeMetadata(stackDelta: -1)] CompareGreaterThan = 0x14,
[OpcodeMetadata(stackDelta: -1)] BooleanAnd = 0x15, //Either shrinks the stack 1 or 0. Assume 1.
BooleanNot = 0x16,
DivideReference = 0x17,
Negate = 0x18,
Modulus = 0x19,
[OpcodeMetadata(stackDelta: -1)] Modulus = 0x19,
Append = 0x1A,
CreateRangeEnumerator = 0x1B,
[OpcodeMetadata(stackDelta: -3)] CreateRangeEnumerator = 0x1B,
Input = 0x1C,
CompareLessThanOrEqual = 0x1D,
[OpcodeMetadata(stackDelta: -1)] CompareLessThanOrEqual = 0x1D,
CreateAssociativeList = 0x1E,
Remove = 0x1F,
DeleteObject = 0x20,
PushResource = 0x21,
[OpcodeMetadata(stackDelta: -1)] DeleteObject = 0x20,
[OpcodeMetadata(stackDelta: 1)] PushResource = 0x21,
CreateList = 0x22,
CallStatement = 0x23,
BitAnd = 0x24,
CompareNotEquals = 0x25,
PushProc = 0x26,
Divide = 0x27,
Multiply = 0x28,
[OpcodeMetadata(stackDelta: -1)] BitAnd = 0x24,
[OpcodeMetadata(stackDelta: -1)] CompareNotEquals = 0x25,
[OpcodeMetadata(stackDelta: 1)] PushProc = 0x26,
[OpcodeMetadata(stackDelta: -1)] Divide = 0x27,
[OpcodeMetadata(stackDelta: -1)] Multiply = 0x28,
BitXorReference = 0x29,
BitXor = 0x2A,
BitOr = 0x2B,
[OpcodeMetadata(stackDelta: -1)] BitXor = 0x2A,
[OpcodeMetadata(stackDelta: -1)] BitOr = 0x2B,
BitNot = 0x2C,
Combine = 0x2D,
CreateObject = 0x2E,
BooleanOr = 0x2F,
[OpcodeMetadata(stackDelta: -1)] BooleanOr = 0x2F, // Shrinks the stack by 1 or 0. Assume 1.
//0x30
CompareGreaterThanOrEqual = 0x31,
SwitchCase = 0x32,
[OpcodeMetadata(stackDelta: -1)] CompareGreaterThanOrEqual = 0x31,
[OpcodeMetadata(stackDelta: -1)] SwitchCase = 0x32, //This could either shrink the stack by 1 or 2. Assume 1.
Mask = 0x33,
//0x34
Error = 0x35,
IsInList = 0x36,
[OpcodeMetadata(stackDelta: -1)] IsInList = 0x36,
//0x37
PushFloat = 0x38,
[OpcodeMetadata(stackDelta: 1)] PushFloat = 0x38,
ModulusReference = 0x39,
CreateListEnumerator = 0x3A,
[OpcodeMetadata(stackDelta: -1)] CreateListEnumerator = 0x3A,
Enumerate = 0x3B,
DestroyEnumerator = 0x3C,
Browse = 0x3D,
BrowseResource = 0x3E,
OutputControl = 0x3F,
BitShiftRight = 0x40,
CreateFilteredListEnumerator = 0x41,
Power = 0x42,
[OpcodeMetadata(stackDelta: -3)] Browse = 0x3D,
[OpcodeMetadata(stackDelta: -3)] BrowseResource = 0x3E,
[OpcodeMetadata(stackDelta: -3)] OutputControl = 0x3F,
[OpcodeMetadata(stackDelta: -1)] BitShiftRight = 0x40,
[OpcodeMetadata(stackDelta: -1)] CreateFilteredListEnumerator = 0x41,
[OpcodeMetadata(stackDelta: -1)] Power = 0x42,
//0x43,
//0x44
Prompt = 0x45,
Ftp = 0x46,
Initial = 0x47,
[OpcodeMetadata(stackDelta: -3)] Prompt = 0x45,
[OpcodeMetadata(stackDelta: -3)] Ftp = 0x46,
[OpcodeMetadata(stackDelta: -1)] Initial = 0x47,
//0x48
IsType = 0x49,
LocateCoord = 0x4A,
Locate = 0x4B,
[OpcodeMetadata(stackDelta: -1)] IsType = 0x49,
[OpcodeMetadata(stackDelta: -2)] LocateCoord = 0x4A,
[OpcodeMetadata(stackDelta: -1)] Locate = 0x4B,
IsNull = 0x4C,
Spawn = 0x4D,
OutputReference = 0x4E,
Output = 0x4F,
[OpcodeMetadata(stackDelta: -1)] Spawn = 0x4D,
[OpcodeMetadata(stackDelta: -1)] OutputReference = 0x4E,
[OpcodeMetadata(stackDelta: -2)] Output = 0x4F,
JumpIfNullDereference = 0x50,
Pop = 0x51,
[OpcodeMetadata(stackDelta: -1)] Pop = 0x51,
Prob = 0x52,
IsSaved = 0x53,
[OpcodeMetadata(stackDelta: -1)] IsSaved = 0x53,
PickUnweighted = 0x54,
PickWeighted = 0x55,
Increment = 0x56,
Decrement = 0x57,
CompareEquivalent = 0x58,
CompareNotEquivalent = 0x59,
[OpcodeMetadata(stackDelta: 1)] Increment = 0x56,
[OpcodeMetadata(stackDelta: 1)] Decrement = 0x57,
[OpcodeMetadata(stackDelta: -1)] CompareEquivalent = 0x58,
[OpcodeMetadata(stackDelta: -1)] CompareNotEquivalent = 0x59,
Throw = 0x5A,
IsInRange = 0x5B,
[OpcodeMetadata(stackDelta: -2)] IsInRange = 0x5B,
MassConcatenation = 0x5C,
CreateTypeEnumerator = 0x5D,
[OpcodeMetadata(stackDelta: -1)] CreateTypeEnumerator = 0x5D,
//0x5E
PushGlobalVars = 0x5F,
ModulusModulus = 0x60,
[OpcodeMetadata(stackDelta: 1)] PushGlobalVars = 0x5F,
[OpcodeMetadata(stackDelta: -1)] ModulusModulus = 0x60,
ModulusModulusReference = 0x61,
//0x62
//0x63
Expand All @@ -112,7 +112,7 @@ public enum DreamProcOpcode : byte {
JumpIfTrueReference = 0x66,
JumpIfFalseReference = 0x67,
DereferenceField = 0x68,
DereferenceIndex = 0x69,
[OpcodeMetadata(stackDelta: -1)] DereferenceIndex = 0x69,
DereferenceCall = 0x6A,
PopReference = 0x6B,
//0x6C
Expand All @@ -124,19 +124,19 @@ public enum DreamProcOpcode : byte {
EnumerateNoAssign = 0x72,
Gradient = 0x73,
AssignInto = 0x74,
GetStep = 0x75,
[OpcodeMetadata(stackDelta: -1)] GetStep = 0x75,
Length = 0x76,
GetDir = 0x77,
[OpcodeMetadata(stackDelta: -1)] GetDir = 0x77,
DebuggerBreakpoint = 0x78,
Sin = 0x79,
Cos = 0x7A,
Tan = 0x7B,
ArcSin = 0x7C,
ArcCos = 0x7D,
ArcTan = 0x7E,
ArcTan2 = 0x7F,
[OpcodeMetadata(stackDelta: -1)] ArcTan2 = 0x7F,
Sqrt = 0x80,
Log = 0x81,
[OpcodeMetadata(stackDelta: -1)] Log = 0x81,
LogE = 0x82,
Abs = 0x83,
}
Expand Down Expand Up @@ -402,3 +402,46 @@ public static string GetOpcodesHash() {
return BitConverter.ToString(hashBytes).Replace("-", "");
}
}

/// <summary>
/// Custom attribute for declaring <see cref="OpcodeMetadata"/> metadata for individual opcodes
/// </summary>
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
internal sealed class OpcodeMetadataAttribute : Attribute {
public OpcodeMetadata Metadata;

public OpcodeMetadataAttribute(int stackDelta = 0) {
Metadata = new OpcodeMetadata(stackDelta);
}
}

/// <summary>
/// Miscellaneous metadata associated with individual <see cref="DreamProcOpcode"/> opcodes using the <see cref="OpcodeMetadataAttribute"/> attribute
/// </summary>
public struct OpcodeMetadata {
public readonly int StackDelta; // Net change in stack size caused by this opcode

public OpcodeMetadata(int stackDelta = 0) {
StackDelta = stackDelta;
}
}

/// <summary>
/// Automatically builds a cache of the <see cref="OpcodeMetadata"/> attribute for each opcode
/// </summary>
public static class OpcodeMetadataCache {
private static readonly OpcodeMetadata[] MetadataCache = new OpcodeMetadata[256];

static OpcodeMetadataCache() {
foreach (DreamProcOpcode opcode in Enum.GetValues(typeof(DreamProcOpcode))) {
var field = typeof(DreamProcOpcode).GetField(opcode.ToString());
var attribute = Attribute.GetCustomAttribute(field!, typeof(OpcodeMetadataAttribute));
var metadataAttribute = (OpcodeMetadataAttribute)attribute;
MetadataCache[(byte)opcode] = metadataAttribute?.Metadata ?? new OpcodeMetadata();
}
}

public static OpcodeMetadata GetMetadata(DreamProcOpcode opcode) {
return MetadataCache[(byte)opcode];
}
}
Loading
Loading