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-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 {
|
|
|
|
|
/// <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,
|
|
|
|
|
User, // user-defined label
|
|
|
|
|
Project, // from project configuration file
|
|
|
|
|
Platform, // from platform definition file
|
2019-08-26 23:58:53 +00:00
|
|
|
|
Auto, // auto-generated label
|
|
|
|
|
Variable // local variable
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Local internal label, global internal label, or reference to an
|
|
|
|
|
/// external address? Constants get a separate type in case we need to
|
|
|
|
|
/// distinguish them from addresses.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public enum Type {
|
|
|
|
|
Unknown = 0,
|
|
|
|
|
LocalOrGlobalAddr, // local symbol, may be promoted to global
|
|
|
|
|
GlobalAddr, // user wants this to be a global symbol
|
|
|
|
|
GlobalAddrExport, // global symbol that is exported to linkers
|
|
|
|
|
ExternalAddr, // reference to address outside program (e.g. platform sym file)
|
|
|
|
|
Constant // constant value
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// True if the symbol's type is an internal label (auto or user). Will be false
|
2019-10-04 23:53:31 +00:00
|
|
|
|
/// for external addresses (including variables) and constants.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsInternalLabel {
|
2019-10-23 04:27:49 +00:00
|
|
|
|
get { return SymbolSource == Source.User || SymbolSource == Source.Auto; }
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// True if the symbol is a local variable.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsVariable {
|
2019-10-23 04:27:49 +00:00
|
|
|
|
get { return SymbolSource == Source.Variable; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsConstant {
|
|
|
|
|
get { return SymbolType == Type.Constant; }
|
2019-08-31 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Label sent to assembler.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Label { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Symbol's numeric value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Two-character string representation of Source and Type, for display in the UI.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string SourceTypeString { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// No nullary constructor.
|
|
|
|
|
private Symbol() { }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs immutable object.
|
|
|
|
|
/// </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>
|
|
|
|
|
public Symbol(string label, int value, Source source, Type type) {
|
|
|
|
|
Debug.Assert(!string.IsNullOrEmpty(label));
|
|
|
|
|
Label = label;
|
|
|
|
|
Value = value;
|
|
|
|
|
SymbolType = type;
|
|
|
|
|
SymbolSource = source;
|
|
|
|
|
|
|
|
|
|
// Generate SourceTypeString.
|
|
|
|
|
string sts;
|
|
|
|
|
switch (SymbolSource) {
|
|
|
|
|
case Source.Auto: sts = "A"; break;
|
|
|
|
|
case Source.User: sts = "U"; break;
|
|
|
|
|
case Source.Platform: sts = "P"; break;
|
2019-08-26 23:58:53 +00:00
|
|
|
|
case Source.Project: sts = "R"; break;
|
|
|
|
|
case Source.Variable: sts = "V"; break;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
default: sts = "?"; break;
|
|
|
|
|
}
|
|
|
|
|
switch (SymbolType) {
|
|
|
|
|
case Type.LocalOrGlobalAddr: sts += "L"; break;
|
|
|
|
|
case Type.GlobalAddr: sts += "G"; break;
|
|
|
|
|
case Type.GlobalAddrExport: sts += "X"; break;
|
|
|
|
|
case Type.ExternalAddr: sts += "E"; break;
|
|
|
|
|
case Type.Constant: sts += "C"; break;
|
|
|
|
|
default: sts += "?"; break;
|
|
|
|
|
}
|
|
|
|
|
SourceTypeString = sts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return Label + "{" + SymbolSource + "," + SymbolType +
|
|
|
|
|
",val=$" + Value.ToString("x4") + "}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
// All fields must be equal. Ignore SourceTypeString, since it's generated
|
|
|
|
|
// from Source and Type.
|
|
|
|
|
return Asm65.Label.LABEL_COMPARER.Equals(a.Label, b.Label) && a.Value == b.Value &&
|
|
|
|
|
a.SymbolSource == b.SymbolSource && a.SymbolType == b.SymbolType;
|
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
|
// Convert the label to upper case before computing the hash code, so that
|
|
|
|
|
// symbols with "foo" and "FOO" (which are equal) have the same hash code.
|
|
|
|
|
return Asm65.Label.ToNormal(Label).GetHashCode() ^
|
|
|
|
|
Value ^ (int)SymbolType ^ (int)SymbolSource;
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|