/*
* Copyright 2018 faddenSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace PluginCommon {
///
/// Plugin-accessible symbol, for use in extension scripts.
///
/// Instances are immutable.
///
[Serializable]
public class PlSymbol {
///
/// Subset of Symbol.Source. Does not include auto-generated labels or variables.
///
public enum Source {
Unknown = 0,
User,
AddrPreLabel,
Project,
Platform
}
///
/// Subset of Symbol.Type. Does not specify local vs. global or export.
///
public enum Type {
Unknown = 0,
Address,
Constant
}
///
/// Label sent to assembler.
///
public string Label { get; private set; }
///
/// Symbol's numeric value.
///
public int Value { get; private set; }
///
/// Symbol origin.
///
private Source SymbolSource { get; set; }
public bool IsUserSymbol { get { return SymbolSource == Source.User; } }
public bool IsProjectSymbol { get { return SymbolSource == Source.Project; } }
public bool IsPlatformSymbol { get { return SymbolSource == Source.Platform; } }
///
/// Symbol type.
///
private Type SymbolType { get; set; }
public bool IsAddress { get { return SymbolType == Type.Address; } }
public bool IsConstant { get { return SymbolType == Type.Constant; } }
///
/// Platform/project symbols only: width, in bytes, of data at symbol. Will be -1
/// for user labels.
///
public int Width { get; private set; }
///
/// Platform symbols only: tag used to organize symbols into groups..
///
public string Tag { get; private set; }
///
/// Symbol offset, for user labels and pre-labels; -1 otherwise.
///
public int Offset { get; private set; }
///
/// Nullary constructor, for deserialization.
///
private PlSymbol() { }
///
/// Constructor.
///
/// Symbol label.
/// Symbol value.
/// Width, for platform/project symbols.
/// Symbol source.
/// Symbol type.
/// Symbol group tag.
/// Offset, for user labels and pre-labels; -1 otherwise.
public PlSymbol(string label, int value, int width, Source source, Type type, string tag,
int offset) {
Label = label;
Value = value;
Width = width;
SymbolSource = source;
SymbolType = type;
Tag = tag;
Offset = offset;
}
///
/// Generates a dictionary of platform symbols, keyed by value. Only symbols with
/// a matching tag are included. If more than one symbol has the same value, only
/// one will be included; which one it will be is undefined.
///
/// List of platform symbols to select from.
/// Tag to match, or null to collect all symbols.
/// Application reference, for debug log output.
/// Dictionary of matching platform symbols.
public static Dictionary GeneratePlatformValueList(List ppuSyms,
string tag, IApplication appRef) {
Dictionary dict = new Dictionary();
foreach (PlSymbol ps in ppuSyms) {
if (ps.SymbolSource != Source.Platform) {
continue;
}
if (tag == null || tag == ps.Tag) {
try {
dict.Add(ps.Value, ps);
} catch (ArgumentException) {
appRef.DebugLog("WARNING: GenerateValueList: multiple entries with " +
"value " + ps.Value.ToString("x4") + ": " + dict[ps.Value].Label +
" and " + ps.Label);
}
}
}
if (dict.Count == 0) {
appRef.DebugLog("PlSymbol: no symbols found for tag=" + tag);
}
return dict;
}
public override string ToString() {
return Label + "=" + Value.ToString("x4") + " [" + Tag + "]";
}
}
}