2019-07-07 00:24:42 +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.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
using Asm65;
|
|
|
|
|
|
2019-07-20 20:28:10 +00:00
|
|
|
|
namespace SourceGen.WpfGui {
|
2019-07-07 00:24:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Edit a label.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class EditLabel : Window, INotifyPropertyChanged {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Symbol object. When the dialog completes successfully,
|
|
|
|
|
/// this will have the new symbol, or null if the user deleted the label.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Symbol LabelSym { get; private set; }
|
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Unique tag, for non-unique label creation. (Currently using offset.)
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int mUniqueTag;
|
|
|
|
|
|
2019-07-07 00:24:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Address we are editing the label for.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int mAddress;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reference to DisasmProject's SymbolTable.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private SymbolTable mSymbolTable;
|
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Label formatter.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private Formatter mFormatter;
|
|
|
|
|
|
2019-12-30 01:59:35 +00:00
|
|
|
|
private Brush mDefaultLabelColor = SystemColors.WindowTextBrush;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Recursion guard.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool mInUpdateControls;
|
|
|
|
|
|
|
|
|
|
public string NonUniqueButtonLabel { get; private set; }
|
|
|
|
|
|
2019-07-07 00:24:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set to true when input is valid. Controls whether the OK button is enabled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsValid {
|
|
|
|
|
get { return mIsValid; }
|
2019-11-13 01:24:41 +00:00
|
|
|
|
set { mIsValid = value; OnPropertyChanged(); }
|
2019-07-07 00:24:42 +00:00
|
|
|
|
}
|
|
|
|
|
private bool mIsValid;
|
|
|
|
|
|
2019-07-07 20:28:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Property backing the text in the text entry box.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string LabelText {
|
|
|
|
|
get { return mLabelText; }
|
2019-11-13 01:24:41 +00:00
|
|
|
|
set { mLabelText = value; OnPropertyChanged(); UpdateControls(); }
|
2019-07-07 20:28:45 +00:00
|
|
|
|
}
|
|
|
|
|
string mLabelText;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
// Radio buttons.
|
|
|
|
|
public bool mIsNonUniqueChecked, mIsNonUniqueEnabled;
|
|
|
|
|
public bool IsNonUniqueChecked {
|
|
|
|
|
get { return mIsNonUniqueChecked; }
|
|
|
|
|
set { mIsNonUniqueChecked = value; OnPropertyChanged(); UpdateControls(); }
|
|
|
|
|
}
|
|
|
|
|
public bool IsNonUniqueEnabled {
|
|
|
|
|
get { return mIsNonUniqueEnabled; }
|
|
|
|
|
set { mIsNonUniqueEnabled = value; OnPropertyChanged(); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool mIsLocalChecked, mIsLocalEnabled;
|
|
|
|
|
public bool IsLocalChecked {
|
|
|
|
|
get { return mIsLocalChecked; }
|
|
|
|
|
set { mIsLocalChecked = value; OnPropertyChanged(); UpdateControls(); }
|
|
|
|
|
}
|
|
|
|
|
public bool IsLocalEnabled {
|
|
|
|
|
get { return mIsLocalEnabled; }
|
|
|
|
|
set { mIsLocalEnabled = value; OnPropertyChanged(); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool mIsGlobalChecked, mIsGlobalEnabled;
|
|
|
|
|
public bool IsGlobalChecked {
|
|
|
|
|
get { return mIsGlobalChecked; }
|
|
|
|
|
set { mIsGlobalChecked = value; OnPropertyChanged(); UpdateControls(); }
|
|
|
|
|
}
|
|
|
|
|
public bool IsGlobalEnabled {
|
|
|
|
|
get { return mIsGlobalEnabled; }
|
|
|
|
|
set { mIsGlobalEnabled = value; OnPropertyChanged(); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool mIsExportedChecked, mIsExportedEnabled;
|
|
|
|
|
public bool IsExportedChecked {
|
|
|
|
|
get { return mIsExportedChecked; }
|
|
|
|
|
set { mIsExportedChecked = value; OnPropertyChanged(); UpdateControls(); }
|
|
|
|
|
}
|
|
|
|
|
public bool IsExportedEnabled {
|
|
|
|
|
get { return mIsExportedEnabled; }
|
|
|
|
|
set { mIsExportedEnabled = value; OnPropertyChanged(); }
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-07 00:24:42 +00:00
|
|
|
|
// INotifyPropertyChanged implementation
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
private void OnPropertyChanged([CallerMemberName] string propertyName = "") {
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
public EditLabel(Window owner, Symbol origSym, int address, int uniqueTag,
|
|
|
|
|
SymbolTable symbolTable, Formatter formatter) {
|
2019-07-07 00:24:42 +00:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
Owner = owner;
|
|
|
|
|
DataContext = this;
|
|
|
|
|
|
|
|
|
|
LabelSym = origSym;
|
|
|
|
|
mAddress = address;
|
2019-11-13 01:24:41 +00:00
|
|
|
|
mUniqueTag = uniqueTag;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
mSymbolTable = symbolTable;
|
2019-11-13 01:24:41 +00:00
|
|
|
|
mFormatter = formatter;
|
|
|
|
|
|
|
|
|
|
string fmt = (string)FindResource("str_NonUniqueLocalFmt");
|
|
|
|
|
NonUniqueButtonLabel = string.Format(fmt, mFormatter.NonUniqueLabelPrefix);
|
2019-07-07 00:24:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Window_Loaded(object sender, RoutedEventArgs e) {
|
2019-11-13 01:24:41 +00:00
|
|
|
|
IsNonUniqueEnabled = IsLocalEnabled = IsGlobalEnabled = IsExportedEnabled = true;
|
|
|
|
|
|
2019-07-07 00:24:42 +00:00
|
|
|
|
if (LabelSym == null) {
|
2019-07-07 20:28:45 +00:00
|
|
|
|
LabelText = string.Empty;
|
2019-11-13 01:24:41 +00:00
|
|
|
|
IsGlobalChecked = true;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
} else {
|
2019-11-13 01:24:41 +00:00
|
|
|
|
LabelText = LabelSym.GenerateDisplayLabel(mFormatter);
|
2019-07-07 00:24:42 +00:00
|
|
|
|
switch (LabelSym.SymbolType) {
|
2019-11-09 04:44:45 +00:00
|
|
|
|
case Symbol.Type.NonUniqueLocalAddr:
|
2019-11-13 01:24:41 +00:00
|
|
|
|
IsNonUniqueChecked = true;
|
2019-11-09 04:44:45 +00:00
|
|
|
|
break;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
case Symbol.Type.LocalOrGlobalAddr:
|
Label rework, part 6
Correct handling of local variables. We now correctly uniquify them
with regard to non-unique labels. Because local vars can effectively
have global scope we mostly want to treat them as global, but they're
uniquified relative to other globals very late in the process, so we
can't just throw them in the symbol table and be done. Fortunately
local variables exist in a separate namespace, so we just need to
uniquify the variables relative to the post-localization symbol table.
In other words, we take the symbol table, apply the label map, and
rename any variable that clashes.
This also fixes an older problem where we weren't masking the
leading '_' on variable labels when generating 64tass output.
The code list now makes non-unique labels obvious, but you can't tell
the difference between unique global and unique local. What's more,
the default type value in Edit Label is now adjusted to Global for
unique locals that were auto-generated. To make it a bit easier to
figure out what's what, the Info panel now has a "label type" line
that reports the type.
The 2023-non-unique-labels test had some additional tests added to
exercise conflicts with local variables. The 2019-local-variables
test output changed slightly because the de-duplicated variable
naming convention was simplified.
2019-11-18 21:26:03 +00:00
|
|
|
|
if (LabelSym.SymbolSource == Symbol.Source.Auto ||
|
|
|
|
|
LabelSym.LabelAnno == Symbol.LabelAnnotation.Generated) {
|
|
|
|
|
// Default to global, otherwise you get different behavior when
|
|
|
|
|
// adding a new label vs. replacing an auto or generated label.
|
|
|
|
|
IsGlobalChecked = true;
|
|
|
|
|
} else {
|
|
|
|
|
IsLocalChecked = true;
|
|
|
|
|
}
|
2019-07-07 00:24:42 +00:00
|
|
|
|
break;
|
|
|
|
|
case Symbol.Type.GlobalAddr:
|
2019-11-13 01:24:41 +00:00
|
|
|
|
IsGlobalChecked = true;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
break;
|
|
|
|
|
case Symbol.Type.GlobalAddrExport:
|
2019-11-13 01:24:41 +00:00
|
|
|
|
IsExportedChecked = true;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Debug.Assert(false); // WTF
|
2019-11-13 01:24:41 +00:00
|
|
|
|
IsGlobalChecked = true;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-13 01:24:41 +00:00
|
|
|
|
|
|
|
|
|
UpdateControls();
|
2019-07-07 20:28:45 +00:00
|
|
|
|
}
|
2019-07-07 00:24:42 +00:00
|
|
|
|
|
2019-07-07 20:28:45 +00:00
|
|
|
|
private void Window_ContentRendered(object sender, EventArgs e) {
|
2019-07-07 00:24:42 +00:00
|
|
|
|
labelTextBox.SelectAll();
|
|
|
|
|
labelTextBox.Focus();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
private void UpdateControls() {
|
|
|
|
|
if (mInUpdateControls) {
|
|
|
|
|
return;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
}
|
2019-11-13 01:24:41 +00:00
|
|
|
|
mInUpdateControls = true;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
LabelTextChanged();
|
2019-07-07 00:24:42 +00:00
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
mInUpdateControls = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool mHadNonUniquePrefix = false;
|
|
|
|
|
|
|
|
|
|
private void LabelTextChanged() {
|
2019-11-09 04:44:45 +00:00
|
|
|
|
bool isBlank = (LabelText.Length == 0);
|
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
// Strip leading non-unique prefix and the trailing annotation.
|
|
|
|
|
string trimLabel = Symbol.TrimAndValidateLabel(LabelText,
|
|
|
|
|
mFormatter.NonUniqueLabelPrefix, out bool isValid, out bool isLenValid,
|
|
|
|
|
out bool isFirstCharValid, out bool hasNonUniquePrefix,
|
|
|
|
|
out Symbol.LabelAnnotation anno);
|
|
|
|
|
|
|
|
|
|
// If they type '@'/':'/'.' at the start of the label, switch the radio button.
|
|
|
|
|
// Alternatively, if they choose a different radio button, remove the prefix.
|
|
|
|
|
// We only want to do this on the first event so we don't wedge the control.
|
|
|
|
|
if (hasNonUniquePrefix && !mHadNonUniquePrefix && !IsNonUniqueChecked) {
|
|
|
|
|
IsNonUniqueChecked = true;
|
|
|
|
|
} else if (hasNonUniquePrefix && mHadNonUniquePrefix && !IsNonUniqueChecked) {
|
|
|
|
|
LabelText = LabelText.Substring(1);
|
|
|
|
|
hasNonUniquePrefix = false;
|
|
|
|
|
}
|
|
|
|
|
mHadNonUniquePrefix = hasNonUniquePrefix;
|
|
|
|
|
|
2019-11-09 04:44:45 +00:00
|
|
|
|
if (isBlank || isLenValid) {
|
|
|
|
|
maxLengthLabel.Foreground = mDefaultLabelColor;
|
|
|
|
|
} else {
|
|
|
|
|
maxLengthLabel.Foreground = Brushes.Red;
|
|
|
|
|
}
|
|
|
|
|
if (isBlank || isFirstCharValid) {
|
|
|
|
|
firstLetterLabel.Foreground = mDefaultLabelColor;
|
|
|
|
|
} else {
|
|
|
|
|
firstLetterLabel.Foreground = Brushes.Red;
|
|
|
|
|
}
|
|
|
|
|
if (isBlank || isValid) {
|
|
|
|
|
// TODO(maybe): if the problem is that the label starts with a number, we
|
|
|
|
|
// shouldn't light up this (which is the "valid chars are" label) as well.
|
|
|
|
|
validCharsLabel.Foreground = mDefaultLabelColor;
|
|
|
|
|
} else {
|
|
|
|
|
validCharsLabel.Foreground = Brushes.Red;
|
|
|
|
|
}
|
2019-07-07 00:24:42 +00:00
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
#if false
|
|
|
|
|
if (hasNonUniqueTag) {
|
|
|
|
|
IsNonUniqueChecked = true;
|
|
|
|
|
IsLocalEnabled = IsGlobalEnabled = IsExportedEnabled = false;
|
|
|
|
|
} else {
|
|
|
|
|
IsNonUniqueEnabled = IsLocalEnabled = IsGlobalEnabled = IsExportedEnabled = true;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Refuse to continue if the label already exists and this isn't a non-unique label.
|
|
|
|
|
// The only exception is if it's the same symbol, and it's user-defined. (If
|
|
|
|
|
// they're trying to edit an auto label, we want to force them to change the name.)
|
2019-07-07 00:24:42 +00:00
|
|
|
|
//
|
|
|
|
|
// NOTE: if label matching is case-insensitive, we want to allow a situation
|
|
|
|
|
// where a label is being renamed from "FOO" to "Foo". We should be able to
|
|
|
|
|
// test for object equality on the Symbol to determine if we're renaming a
|
|
|
|
|
// symbol to itself.
|
2019-11-13 01:24:41 +00:00
|
|
|
|
if (!IsNonUniqueChecked && isValid &&
|
|
|
|
|
mSymbolTable.TryGetValue(trimLabel, out Symbol sym) &&
|
2019-07-07 00:24:42 +00:00
|
|
|
|
(sym != LabelSym || LabelSym.SymbolSource != Symbol.Source.User)) {
|
2019-11-09 04:44:45 +00:00
|
|
|
|
isValid = false;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
notDuplicateLabel.Foreground = Brushes.Red;
|
2019-11-13 01:24:41 +00:00
|
|
|
|
} else if (IsNonUniqueChecked) {
|
|
|
|
|
notDuplicateLabel.Foreground = Brushes.Gray;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
} else {
|
|
|
|
|
notDuplicateLabel.Foreground = mDefaultLabelColor;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 04:44:45 +00:00
|
|
|
|
IsValid = isBlank || isValid;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OkButton_Click(object sender, RoutedEventArgs e) {
|
2019-07-07 20:28:45 +00:00
|
|
|
|
if (string.IsNullOrEmpty(LabelText)) {
|
2019-07-07 00:24:42 +00:00
|
|
|
|
LabelSym = null;
|
|
|
|
|
} else {
|
|
|
|
|
Symbol.Type symbolType;
|
2019-11-13 01:24:41 +00:00
|
|
|
|
if (IsNonUniqueChecked) {
|
|
|
|
|
symbolType = Symbol.Type.NonUniqueLocalAddr;
|
|
|
|
|
} else if (IsLocalChecked == true) {
|
2019-07-07 00:24:42 +00:00
|
|
|
|
symbolType = Symbol.Type.LocalOrGlobalAddr;
|
2019-11-13 01:24:41 +00:00
|
|
|
|
} else if (IsGlobalChecked == true) {
|
2019-07-07 00:24:42 +00:00
|
|
|
|
symbolType = Symbol.Type.GlobalAddr;
|
2019-11-13 01:24:41 +00:00
|
|
|
|
} else if (IsExportedChecked == true) {
|
2019-07-07 00:24:42 +00:00
|
|
|
|
symbolType = Symbol.Type.GlobalAddrExport;
|
|
|
|
|
} else {
|
|
|
|
|
Debug.Assert(false); // WTF
|
2019-11-13 01:24:41 +00:00
|
|
|
|
symbolType = Symbol.Type.GlobalAddr;
|
2019-07-07 00:24:42 +00:00
|
|
|
|
}
|
2019-11-09 04:44:45 +00:00
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
// Parse and strip the annotation and optional non-unique tag.
|
|
|
|
|
string trimLabel = Symbol.TrimAndValidateLabel(LabelText,
|
|
|
|
|
mFormatter.NonUniqueLabelPrefix, out bool unused1, out bool unused2,
|
|
|
|
|
out bool unused3, out bool hasNonUniquePrefix,
|
|
|
|
|
out Symbol.LabelAnnotation anno);
|
2019-11-09 04:44:45 +00:00
|
|
|
|
|
2019-11-13 01:24:41 +00:00
|
|
|
|
if (IsNonUniqueChecked) {
|
|
|
|
|
LabelSym = new Symbol(trimLabel, mAddress, anno, mUniqueTag);
|
|
|
|
|
} else {
|
|
|
|
|
Debug.Assert(!hasNonUniquePrefix);
|
|
|
|
|
LabelSym = new Symbol(trimLabel, mAddress, Symbol.Source.User, symbolType,
|
|
|
|
|
anno);
|
|
|
|
|
}
|
2019-07-07 00:24:42 +00:00
|
|
|
|
}
|
|
|
|
|
DialogResult = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|