2019-05-02 22:45:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 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.Diagnostics;
|
2019-11-13 01:24:41 +00:00
|
|
|
|
using System.Text;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-07-20 20:28:10 +00:00
|
|
|
|
namespace SourceGen {
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Symbolic representation of a value. Instances are immutable.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Symbol {
|
2019-11-09 04:44:45 +00:00
|
|
|
|
public const char UNCERTAIN_CHAR = '?';
|
2019-11-13 01:24:41 +00:00
|
|
|
|
private const char NO_ANNO_CHAR = '\ufffd'; // REPLACEMENT CHARACTER '<27>'
|
|
|
|
|
private const char UNIQUE_TAG_CHAR = '\u00a7'; // SECTION SIGN
|
|
|
|
|
private const int NON_UNIQUE_LEN = 7; // NON_UNIQUE_CHAR + 6 hex digits
|
2019-11-09 04:44:45 +00:00
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <summary>
|
2019-08-26 23:58:53 +00:00
|
|
|
|
/// How was the symbol defined?
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public enum Source {
|
|
|
|
|
// These are in order of highest to lowest precedence. This matters when
|
2019-08-26 23:58:53 +00:00
|
|
|
|
// looking up a symbol by value from the symbol table, because multiple symbols
|
|
|
|
|
// can have the same value.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
Unknown = 0,
|
2019-11-09 04:44:45 +00:00
|
|
|
|
User, // user-defined; only used for internal address labels
|
|
|
|
|
Project, // external address or const, from project configuration file
|
|
|
|
|
Platform, // external address or const, from platform definition file
|
|
|
|
|
Auto, // auto-generated internal address label
|
|
|
|
|
Variable // external address or const, from local variable table
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// Unique or non-unique address label? Is it required to be global or exported?
|
|
|
|
|
/// Constants get a separate type.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// There's really just three types: unique address symbol, non-unique address symbol,
|
|
|
|
|
/// and constant. There's also a set of boolean flags indicating whether the symbol
|
|
|
|
|
/// should be forced to be global, whether it should be included in the export table,
|
|
|
|
|
/// and whether it's internal or external.
|
|
|
|
|
///
|
|
|
|
|
/// It turns out that many combinations of type and flag don't actually make sense,
|
|
|
|
|
/// e.g. I don't know what a non-unique exported external constant is, so we just
|
|
|
|
|
/// enumerate the combinations that make sense.
|
|
|
|
|
/// </remarks>
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public enum Type {
|
|
|
|
|
Unknown = 0,
|
2019-11-09 04:44:45 +00:00
|
|
|
|
|
|
|
|
|
NonUniqueLocalAddr, // non-unique local symbol, may be promoted to global
|
|
|
|
|
LocalOrGlobalAddr, // unique local symbol, may be promoted to global
|
|
|
|
|
GlobalAddr, // unique global symbol
|
|
|
|
|
GlobalAddrExport, // unique global symbol; included in linker export table
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
ExternalAddr, // reference to address outside program (e.g. platform sym file)
|
|
|
|
|
Constant // constant value
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 01:33:05 +00:00
|
|
|
|
/// <summary>
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// User-specified commentary on the label.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
2019-11-09 04:44:45 +00:00
|
|
|
|
public enum LabelAnnotation {
|
|
|
|
|
None = 0,
|
|
|
|
|
Uncertain, // user isn't sure if this is correct
|
|
|
|
|
Generated // label was generated, e.g. address table formatter
|
2019-08-31 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <summary>
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// Unique label.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Non-unique labels have extra stuff at the end to make them unique. That is
|
|
|
|
|
/// included here, so that the Label field is still viable as a unique identifier.
|
|
|
|
|
/// </remarks>
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public string Label { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// Symbol's 32-bit numeric value.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// For address types, the value should be constained to [0,2^24). For constants,
|
|
|
|
|
/// all values are valid.
|
|
|
|
|
/// </remarks>
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public int Value { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
Allow explicit widths in project/platform symbols, part 3
Implement multi-byte project/platform symbols by filling out a table
of addresses. Each symbol is "painted" into the table, replacing
an existing entry if the new entry has higher priority. This allows
us to handle overlapping entries, giving boosted priority to platform
symbols that are defined in .sym65 files loaded later.
The bounds on project/platform symbols are now rigidly defined. If
the "nearby" feature is enabled, references to SYM-1 will be picked
up, but we won't go hunting for SYM+1 unless the symbol is at least
two bytes wide.
The cost of adding a symbol to the symbol table is about the same,
but we don't have a quick way to remove a symbol.
Previously, if two platform symbols had the same value, the symbol
with the alphabetically lowest label would win. Now, the symbol
defined in the most-recently-loaded file wins. (If you define two
symbols with the same value in the same file, it's still resolved
alphabetically.) This allows the user to pick the winner by
arranging the load order of the platform symbol files.
Platform symbols now keep a reference to the file ident of the
symbol file that defined them, so we can show the symbols's source
in the Info panel.
These changes altered the behavior of test 2008-address-changes,
which includes some tests on external addresses that are close to
labeled internal addresses. The previous behavior essentially
treated user labels as being 3 bytes wide and extending outside the
file bounds, which was mildly convenient on occasion but felt a
little skanky. (We could do with a way to define external symbols
relative to internal symbols, for things like the source address of
code that gets relocated.)
Also, re-enabled some unit tests.
Also, added a bit of identifying stuff to CrashLog.txt.
2019-10-02 23:26:05 +00:00
|
|
|
|
/// Symbol origin, e.g. auto-generated or entered by user. Enum values are in
|
|
|
|
|
/// priority order.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public Source SymbolSource { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Type of symbol, e.g. local or global.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Type SymbolType { get; private set; }
|
|
|
|
|
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Notes on the label.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public LabelAnnotation LabelAnno { get; private set; }
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Two-character string representation of Source and Type, for display in the UI.
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// Generated from SymbolSource and SymbolType.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public string SourceTypeString { get; private set; }
|
|
|
|
|
|
2019-11-09 04:44:45 +00:00
|
|
|
|
|
2019-11-16 04:40:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Label without the non-unique tag.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string LabelForSerialization {
|
|
|
|
|
get {
|
|
|
|
|
if (SymbolType != Type.NonUniqueLocalAddr) {
|
|
|
|
|
return Label;
|
|
|
|
|
} else {
|
|
|
|
|
return Label.Substring(0, Label.Length - NON_UNIQUE_LEN);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// True if the symbol's type is an internal label (auto or user). Will be false
|
|
|
|
|
/// for external addresses (including variables) and constants.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsInternalLabel {
|
|
|
|
|
get { return SymbolSource == Source.User || SymbolSource == Source.Auto; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// True if the symbol is a local variable.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsVariable {
|
|
|
|
|
get { return SymbolSource == Source.Variable; }
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// True if the symbol is a non-unique local.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsNonUnique {
|
|
|
|
|
get { return SymbolType == Type.NonUniqueLocalAddr; }
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// True if the symbol represents a constant value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsConstant {
|
|
|
|
|
get { return SymbolType == Type.Constant; }
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
// No nullary constructor.
|
|
|
|
|
private Symbol() { }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// Basic constructor.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="label">Label string. Syntax assumed valid.</param>
|
Allow explicit widths in project/platform symbols, part 1
The ability to give explicit widths to local variables worked out
pretty well, so we're going to try adding the same thing to project
and platform symbols.
The first step is to allow widths to be specified in platform files,
and set with the project symbol editor. The DefSymbol editor is
also used for local variables, so a bit of dancing is required.
For platform/project symbols the width is optional, and is totally
ignored for constants. (For variables, constants are used for the
StackRel args, so the width is meaningful and required.)
We also now show the symbol's type (address or constant) and width
in the listing. This gets really distracting when overused, so we
only show it when the width is explicitly set. The default width
is 1, which most things will be, so users can make an aesthetic
choice there. (The place where widths make very little sense is when
the symbol represents a code entry point, rather than a data item.)
The maximum width of a local variable is now 256, but it's not
allowed to overlap with other variables or run of the end of the
direct page. The maximum width of a platform/project symbol is
65536, with bank-wrap behavior TBD.
The local variable table editor now refers to stack-relative
constants as such, rather than simply "constant", to make it clear
that it's not just defining an 8-bit constant.
Widths have been added to a handful of Apple II platform defs.
2019-10-01 21:58:24 +00:00
|
|
|
|
/// <param name="value">Symbol value.</param>
|
|
|
|
|
/// <param name="source">User-defined, auto-generated, ?</param>
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <param name="type">Type of symbol this is.</param>
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// <param name="labelAnno">Optional annotation.</param>
|
2019-11-09 04:44:45 +00:00
|
|
|
|
public Symbol(string label, int value, Source source, Type type,
|
|
|
|
|
LabelAnnotation labelAnno) {
|
|
|
|
|
Debug.Assert(Asm65.Label.ValidateLabel(label));
|
2019-11-13 01:24:41 +00:00
|
|
|
|
Debug.Assert(type != Type.NonUniqueLocalAddr || value == 0xdead); // use other ctor
|
2019-05-02 22:45:40 +00:00
|
|
|
|
Label = label;
|
|
|
|
|
Value = value;
|
|
|
|
|
SymbolType = type;
|
|
|
|
|
SymbolSource = source;
|
2019-11-09 04:44:45 +00:00
|
|
|
|
LabelAnno = labelAnno;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
// Generate SourceTypeString.
|
2019-11-09 04:44:45 +00:00
|
|
|
|
char sc, tc;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
switch (SymbolSource) {
|
2019-11-09 04:44:45 +00:00
|
|
|
|
case Source.Auto: sc = 'A'; break;
|
|
|
|
|
case Source.User: sc = 'U'; break;
|
|
|
|
|
case Source.Platform: sc = 'P'; break;
|
|
|
|
|
case Source.Project: sc = 'J'; break;
|
|
|
|
|
case Source.Variable: sc = 'V'; break;
|
|
|
|
|
default: sc = '?'; break;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
switch (SymbolType) {
|
2019-11-09 04:44:45 +00:00
|
|
|
|
case Type.NonUniqueLocalAddr: tc = 'N'; break;
|
|
|
|
|
case Type.LocalOrGlobalAddr: tc = 'L'; break;
|
|
|
|
|
case Type.GlobalAddr: tc = 'G'; break;
|
|
|
|
|
case Type.GlobalAddrExport: tc = 'X'; break;
|
|
|
|
|
case Type.ExternalAddr: tc = 'E'; break;
|
|
|
|
|
case Type.Constant: tc = 'C'; break;
|
|
|
|
|
default: tc = '?'; break;
|
|
|
|
|
}
|
|
|
|
|
SourceTypeString = "" + sc + tc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor for non-unique labels.
|
|
|
|
|
/// </summary>
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// <param name="label">Label string. Syntax assumed valid.</param>
|
|
|
|
|
/// <param name="value">Symbol value.</param>
|
|
|
|
|
/// <param name="labelAnno">Optional annotation.</param>
|
|
|
|
|
/// <param name="uniqueTag">Tag that makes a non-unique label unique, e.g. the
|
|
|
|
|
/// offset for which a user label has been created.</param>
|
|
|
|
|
public Symbol(string label, int value, LabelAnnotation labelAnno, int uniqueTag)
|
|
|
|
|
: this(label, 0xdead, Source.User, Type.NonUniqueLocalAddr, labelAnno) {
|
|
|
|
|
Debug.Assert(uniqueTag >= 0 && uniqueTag < 0x01000000); // fit in 6 hex digits
|
|
|
|
|
Debug.Assert(label.IndexOf(UNIQUE_TAG_CHAR) < 0); // already extended?
|
|
|
|
|
|
|
|
|
|
Value = value; // passed a bogus value earlier for assert
|
|
|
|
|
|
|
|
|
|
// Add tag to label to make it unique.
|
|
|
|
|
Label = label + UNIQUE_TAG_CHAR + uniqueTag.ToString("x6");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generates a displayable form of the label. This will have the non-unique label
|
|
|
|
|
/// prefix and annotation suffix, and will have the non-unique tag removed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="formatter">Formatter object.</param>
|
|
|
|
|
/// <returns>Label suitable for display.</returns>
|
|
|
|
|
public string GenerateDisplayLabel(Asm65.Formatter formatter) {
|
|
|
|
|
return ConvertLabelForDisplay(Label, LabelAnno, IsNonUnique, formatter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the annotation suffix character, or NO_ANNO_CHAR if nothing appropriate.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static char GetLabelAnnoChar(LabelAnnotation anno) {
|
|
|
|
|
char ch = NO_ANNO_CHAR;
|
|
|
|
|
if (anno == LabelAnnotation.Uncertain) {
|
|
|
|
|
ch = UNCERTAIN_CHAR;
|
|
|
|
|
} else if (anno == LabelAnnotation.Generated) {
|
|
|
|
|
//ch = '\u00a4'; // CURRENCY SIGN '¤'
|
|
|
|
|
}
|
|
|
|
|
return ch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-11-16 04:40:14 +00:00
|
|
|
|
/// Converts a label to displayable form by stripping the uniquification tag (if any),
|
|
|
|
|
/// inserting the non-unique label prefix if appropriate, and appending the optional
|
|
|
|
|
/// annotation character.
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// </summary>
|
2019-11-16 04:40:14 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// There's generally two ways to display a label:
|
|
|
|
|
/// (1) When displaying labels on-screen, we get a label with the uniquification tag,
|
|
|
|
|
/// and we want to show the non-unique label prefix ('@' or ':') and annotation.
|
|
|
|
|
/// (2) When generating assembly source, we get a remapped label with no uniquification
|
|
|
|
|
/// tag, and we don't want to show the prefix or annotation.
|
|
|
|
|
/// For case #2, there's no reason to call here. (We're currently doing so because
|
|
|
|
|
/// remapping isn't happening yet, but that should change soon. When that happens, we
|
|
|
|
|
/// should be able to eliminate the showNonUnique arg.)
|
|
|
|
|
/// </remarks>
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// <param name="label">Base label string. Has the uniquification tag, but no
|
|
|
|
|
/// annotation char or non-unique prefix.</param>
|
|
|
|
|
/// <param name="anno">Annotation; may be None.</param>
|
2019-11-16 04:40:14 +00:00
|
|
|
|
/// <param name="showNonUnique">Set true if the returned label should show the
|
|
|
|
|
/// non-unique label prefix.</param>
|
|
|
|
|
/// <param name="formatter">Format object that holds the non-unique label prefix
|
|
|
|
|
/// string.</param>
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// <returns>Formatted label.</returns>
|
|
|
|
|
public static string ConvertLabelForDisplay(string label, LabelAnnotation anno,
|
2019-11-16 04:40:14 +00:00
|
|
|
|
bool showNonUnique, Asm65.Formatter formatter) {
|
2019-11-13 01:24:41 +00:00
|
|
|
|
StringBuilder sb = new StringBuilder(label.Length + 2);
|
|
|
|
|
|
2019-11-16 04:40:14 +00:00
|
|
|
|
if (showNonUnique) {
|
2019-11-13 01:24:41 +00:00
|
|
|
|
sb.Append(formatter.NonUniqueLabelPrefix);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 04:40:14 +00:00
|
|
|
|
if (label.Length > NON_UNIQUE_LEN &&
|
|
|
|
|
label[label.Length - NON_UNIQUE_LEN] == UNIQUE_TAG_CHAR) {
|
|
|
|
|
// showNonUnique may be false if generating assembly code (but by this
|
|
|
|
|
// point the unique tag should be remapped away)
|
|
|
|
|
sb.Append(label.Substring(0, label.Length - NON_UNIQUE_LEN));
|
2019-11-13 01:24:41 +00:00
|
|
|
|
} else {
|
|
|
|
|
sb.Append(label);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char annoChar = GetLabelAnnoChar(anno);
|
|
|
|
|
if (annoChar != NO_ANNO_CHAR) {
|
|
|
|
|
sb.Append(annoChar);
|
|
|
|
|
}
|
|
|
|
|
return sb.ToString();
|
2019-11-09 04:44:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs a detailed validation of a symbol label, breaking out different failure
|
|
|
|
|
/// causes for the benefit of code that reports errors to the user. The label may
|
|
|
|
|
/// have additional characters, such as annotations, which are trimmed away. The
|
|
|
|
|
/// trimmed version of the string is returned.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="label">Label to examine.</param>
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// <param name="nonUniquePrefix">For address symbols, the prefix string for
|
2019-11-16 04:40:14 +00:00
|
|
|
|
/// non-unique labels (e.g. '@' or ':'). May be null if not validating a user
|
|
|
|
|
/// label.</param>
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// <param name="isValid">True if the entire label is valid.</param>
|
|
|
|
|
/// <param name="isLenValid">True if the label has a valid length.</param>
|
|
|
|
|
/// <param name="isFirstCharValid">True if the first character is valid.</param>
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// <param name="hasNonUniquePrefix">True if the first character indicates that this is
|
|
|
|
|
/// a non-unique label.</param>
|
2019-11-09 04:44:45 +00:00
|
|
|
|
/// <param name="anno">Annotation found, or None if none found.</param>
|
|
|
|
|
/// <returns>Trimmed version of the string.</returns>
|
2019-11-13 01:24:41 +00:00
|
|
|
|
public static string TrimAndValidateLabel(string label, string nonUniquePrefix,
|
|
|
|
|
out bool isValid, out bool isLenValid, out bool isFirstCharValid,
|
|
|
|
|
out bool hasNonUniquePrefix, out LabelAnnotation anno) {
|
2019-11-09 04:44:45 +00:00
|
|
|
|
anno = LabelAnnotation.None;
|
2019-11-13 01:24:41 +00:00
|
|
|
|
hasNonUniquePrefix = false;
|
2019-11-09 04:44:45 +00:00
|
|
|
|
|
|
|
|
|
// Do we have at least one char?
|
|
|
|
|
if (string.IsNullOrEmpty(label)) {
|
|
|
|
|
isValid = isLenValid = isFirstCharValid = false;
|
|
|
|
|
return label;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string trimLabel = label;
|
|
|
|
|
// Check for an annotation char, remove it if found.
|
|
|
|
|
if (trimLabel[trimLabel.Length - 1] == UNCERTAIN_CHAR) {
|
|
|
|
|
anno = LabelAnnotation.Uncertain;
|
|
|
|
|
trimLabel = trimLabel.Substring(0, trimLabel.Length - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
// Check for leading non-unique ident char.
|
|
|
|
|
if (trimLabel.Length > 0 && !string.IsNullOrEmpty(nonUniquePrefix)) {
|
|
|
|
|
if (trimLabel[0] == nonUniquePrefix[0]) {
|
|
|
|
|
hasNonUniquePrefix = true;
|
|
|
|
|
trimLabel = trimLabel.Substring(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 04:44:45 +00:00
|
|
|
|
// Now that we're down to the base string, do the full validation test. If it
|
|
|
|
|
// passes, we don't need to dig any deeper.
|
|
|
|
|
isValid = Asm65.Label.ValidateLabelDetail(trimLabel, out isLenValid,
|
|
|
|
|
out isFirstCharValid);
|
|
|
|
|
|
|
|
|
|
return trimLabel;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return Label + "{" + SymbolSource + "," + SymbolType +
|
2019-11-09 04:44:45 +00:00
|
|
|
|
",val=$" + Value.ToString("x4") + "," + LabelAnno + "}";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator ==(Symbol a, Symbol b) {
|
|
|
|
|
if (ReferenceEquals(a, b)) {
|
|
|
|
|
return true; // same object, or both null
|
|
|
|
|
}
|
|
|
|
|
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) {
|
|
|
|
|
return false; // one is null
|
|
|
|
|
}
|
2019-11-09 04:44:45 +00:00
|
|
|
|
// All fields must be equal. Ignore SourceTypeString and AnnotatedLabel, since
|
|
|
|
|
// they're generated from other fields.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
return Asm65.Label.LABEL_COMPARER.Equals(a.Label, b.Label) && a.Value == b.Value &&
|
2019-11-09 04:44:45 +00:00
|
|
|
|
a.SymbolSource == b.SymbolSource && a.SymbolType == b.SymbolType &&
|
|
|
|
|
a.LabelAnno == b.LabelAnno;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
public static bool operator !=(Symbol a, Symbol b) {
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
|
|
|
|
public override bool Equals(object obj) {
|
|
|
|
|
return obj is Symbol && this == (Symbol)obj;
|
|
|
|
|
}
|
|
|
|
|
public override int GetHashCode() {
|
2019-11-09 04:44:45 +00:00
|
|
|
|
// Convert label to "normal form" if we're doing case-insensitive. (We're not
|
|
|
|
|
// anymore, so it's a no-op now.)
|
2019-05-02 22:45:40 +00:00
|
|
|
|
return Asm65.Label.ToNormal(Label).GetHashCode() ^
|
2019-11-09 04:44:45 +00:00
|
|
|
|
Value ^ (int)SymbolType ^ (int)SymbolSource ^ (int)LabelAnno;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-06-17 19:44:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Comparison function, used when sorting the symbol table.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
public enum SymbolSortField { CombinedType, Value, Name };
|
|
|
|
|
|
|
|
|
|
public static int Compare(SymbolSortField sortField, bool isAscending,
|
|
|
|
|
Symbol a, Symbol b) {
|
|
|
|
|
// In the symbol table, only the label field is guaranteed to be unique. We
|
|
|
|
|
// use it as a secondary sort key when comparing the other fields.
|
|
|
|
|
switch (sortField) {
|
|
|
|
|
case SymbolSortField.CombinedType:
|
|
|
|
|
if (isAscending) {
|
|
|
|
|
int cmp = string.Compare(a.SourceTypeString, b.SourceTypeString);
|
2019-10-15 23:37:14 +00:00
|
|
|
|
if (cmp == 0) {
|
|
|
|
|
int aDir = 0;
|
|
|
|
|
int bDir = 0;
|
|
|
|
|
if (a is DefSymbol) {
|
|
|
|
|
aDir = (int)((DefSymbol)a).Direction;
|
|
|
|
|
}
|
|
|
|
|
if (b is DefSymbol) {
|
|
|
|
|
bDir = (int)((DefSymbol)b).Direction;
|
|
|
|
|
}
|
|
|
|
|
cmp = aDir - bDir;
|
|
|
|
|
}
|
2019-06-17 19:44:02 +00:00
|
|
|
|
if (cmp == 0) {
|
|
|
|
|
cmp = string.Compare(a.Label, b.Label);
|
|
|
|
|
}
|
|
|
|
|
return cmp;
|
|
|
|
|
} else {
|
|
|
|
|
int cmp = string.Compare(a.SourceTypeString, b.SourceTypeString);
|
|
|
|
|
if (cmp == 0) {
|
|
|
|
|
// secondary sort is always ascending, so negate
|
|
|
|
|
cmp = -string.Compare(a.Label, b.Label);
|
|
|
|
|
}
|
|
|
|
|
return -cmp;
|
|
|
|
|
}
|
|
|
|
|
case SymbolSortField.Value:
|
|
|
|
|
if (isAscending) {
|
|
|
|
|
int cmp;
|
|
|
|
|
if (a.Value < b.Value) {
|
|
|
|
|
cmp = -1;
|
|
|
|
|
} else if (a.Value > b.Value) {
|
|
|
|
|
cmp = 1;
|
|
|
|
|
} else {
|
|
|
|
|
cmp = string.Compare(a.Label, b.Label);
|
|
|
|
|
}
|
|
|
|
|
return cmp;
|
|
|
|
|
} else {
|
|
|
|
|
int cmp;
|
|
|
|
|
if (a.Value < b.Value) {
|
|
|
|
|
cmp = -1;
|
|
|
|
|
} else if (a.Value > b.Value) {
|
|
|
|
|
cmp = 1;
|
|
|
|
|
} else {
|
|
|
|
|
cmp = -string.Compare(a.Label, b.Label);
|
|
|
|
|
}
|
|
|
|
|
return -cmp;
|
|
|
|
|
}
|
|
|
|
|
case SymbolSortField.Name:
|
|
|
|
|
if (isAscending) {
|
|
|
|
|
return string.Compare(a.Label, b.Label);
|
|
|
|
|
} else {
|
|
|
|
|
return -string.Compare(a.Label, b.Label);
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
Debug.Assert(false);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|