2019-05-28 01:46:09 +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;
|
2019-08-09 23:42:30 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-05-28 01:46:09 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
2019-08-09 23:42:30 +00:00
|
|
|
|
namespace Asm65 {
|
2019-05-28 01:46:09 +00:00
|
|
|
|
/// <summary>
|
2019-08-09 23:42:30 +00:00
|
|
|
|
/// String pseudo-op formatter. Handles character encoding conversion and quoting of
|
|
|
|
|
/// delimiters and non-printable characters.
|
2019-05-28 01:46:09 +00:00
|
|
|
|
/// </summary>
|
2019-08-09 23:42:30 +00:00
|
|
|
|
public class StringOpFormatter {
|
2019-08-14 00:22:21 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Text direction. If text is stored in reverse order, we want to un-reverse it to
|
|
|
|
|
/// make it readable. This gets tricky for a multi-line item. For the assembler we
|
|
|
|
|
/// want to break it into lines and then reverse each chunk, but on screen we want to
|
|
|
|
|
/// reverse the entire thing as a single block.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public enum ReverseMode { Forward, LineReverse, FullReverse };
|
2019-08-09 23:42:30 +00:00
|
|
|
|
|
2019-08-14 00:22:21 +00:00
|
|
|
|
public CharEncoding.Convert CharConv { get; set; }
|
2019-08-09 23:42:30 +00:00
|
|
|
|
|
|
|
|
|
// Output format for raw (non-printable) characters. Most assemblers use comma-separated
|
|
|
|
|
// hex values, some allow dense hex strings.
|
|
|
|
|
public enum RawOutputStyle { DenseHex, CommaSep };
|
2019-05-28 01:46:09 +00:00
|
|
|
|
|
|
|
|
|
// Outputs.
|
2019-08-09 23:42:30 +00:00
|
|
|
|
public bool HasEscapedText { get; private set; }
|
|
|
|
|
public List<string> Lines { get; private set; }
|
2019-05-28 01:46:09 +00:00
|
|
|
|
|
2019-08-14 22:25:09 +00:00
|
|
|
|
private Formatter.DelimiterDef mDelimiterDef;
|
2019-08-14 00:22:21 +00:00
|
|
|
|
private RawOutputStyle mRawStyle;
|
|
|
|
|
private int mMaxOperandLen;
|
|
|
|
|
|
2019-08-09 23:42:30 +00:00
|
|
|
|
// Reference to array with 16 hex digits. (May be upper or lower case.)
|
2019-05-28 01:46:09 +00:00
|
|
|
|
private char[] mHexChars;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Character collection buffer. The delimiters are written into the buffer
|
|
|
|
|
/// because they're mixed with bytes, particularly when we have to escape the
|
|
|
|
|
/// delimiter character. Strings might start or end with escaped delimiters,
|
|
|
|
|
/// so we don't add them until we have to.
|
2019-08-09 23:42:30 +00:00
|
|
|
|
/// </summary>
|
2019-05-28 01:46:09 +00:00
|
|
|
|
private char[] mBuffer;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Next available character position.
|
|
|
|
|
/// </summary>
|
2019-08-09 23:42:30 +00:00
|
|
|
|
private int mIndex;
|
2019-05-28 01:46:09 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// State of the buffer, based on the last thing we added.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private enum State {
|
|
|
|
|
Unknown = 0,
|
|
|
|
|
StartOfLine,
|
|
|
|
|
InQuote,
|
2019-08-09 23:42:30 +00:00
|
|
|
|
OutQuote,
|
|
|
|
|
Finished
|
2019-05-28 01:46:09 +00:00
|
|
|
|
}
|
2019-08-09 23:42:30 +00:00
|
|
|
|
private State mState;
|
2019-05-28 01:46:09 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
2019-08-09 23:42:30 +00:00
|
|
|
|
/// <param name="formatter">Reference to text formatter.</param>
|
2019-08-14 22:25:09 +00:00
|
|
|
|
/// <param name="delimiterDef">String delimiter values.</param>
|
2019-08-09 23:42:30 +00:00
|
|
|
|
/// <param name="byteStyle">How to format raw byte data.</param>
|
|
|
|
|
/// <param name="charConv">Character conversion delegate.</param>
|
2019-08-14 22:25:09 +00:00
|
|
|
|
public StringOpFormatter(Formatter formatter, Formatter.DelimiterDef delimiterDef,
|
2020-07-20 01:39:27 +00:00
|
|
|
|
RawOutputStyle byteStyle, CharEncoding.Convert charConv) {
|
2019-08-14 00:22:21 +00:00
|
|
|
|
mRawStyle = byteStyle;
|
2020-07-20 01:39:27 +00:00
|
|
|
|
mMaxOperandLen = formatter.OperandWrapLen;
|
2019-08-09 23:42:30 +00:00
|
|
|
|
CharConv = charConv;
|
2019-05-28 01:46:09 +00:00
|
|
|
|
|
2019-08-14 22:25:09 +00:00
|
|
|
|
mDelimiterDef = delimiterDef;
|
2019-08-14 00:22:21 +00:00
|
|
|
|
mBuffer = new char[mMaxOperandLen];
|
2019-08-09 23:42:30 +00:00
|
|
|
|
mHexChars = formatter.HexDigits;
|
|
|
|
|
Lines = new List<string>();
|
|
|
|
|
|
2019-08-14 00:22:21 +00:00
|
|
|
|
// suffix not used, so we don't expect it to be set to something
|
2019-08-14 22:25:09 +00:00
|
|
|
|
Debug.Assert(string.IsNullOrEmpty(mDelimiterDef.Suffix));
|
2019-08-14 00:22:21 +00:00
|
|
|
|
|
2019-08-09 23:42:30 +00:00
|
|
|
|
Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reset() {
|
|
|
|
|
mState = State.StartOfLine;
|
|
|
|
|
mIndex = 0;
|
|
|
|
|
Lines.Clear();
|
2019-08-14 00:22:21 +00:00
|
|
|
|
|
|
|
|
|
// Copy the prefix string into the buffer for the first line.
|
2019-08-14 22:25:09 +00:00
|
|
|
|
for (int i = 0; i < mDelimiterDef.Prefix.Length; i++) {
|
|
|
|
|
mBuffer[mIndex++] = mDelimiterDef.Prefix[i];
|
2019-08-14 00:22:21 +00:00
|
|
|
|
}
|
2019-05-28 01:46:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-08-09 23:42:30 +00:00
|
|
|
|
/// Write a character into the buffer. If the character matches the delimiter, or
|
|
|
|
|
/// isn't printable, the raw character value will be written as a byte instead.
|
2019-05-28 01:46:09 +00:00
|
|
|
|
/// </summary>
|
2019-08-09 23:42:30 +00:00
|
|
|
|
/// <param name="rawCh">Raw character value.</param>
|
|
|
|
|
public void WriteChar(byte rawCh) {
|
|
|
|
|
Debug.Assert(mState != State.Finished);
|
|
|
|
|
|
|
|
|
|
char ch = CharConv(rawCh);
|
2019-08-14 22:25:09 +00:00
|
|
|
|
if (ch == mDelimiterDef.OpenDelim || ch == mDelimiterDef.CloseDelim ||
|
2019-08-14 00:22:21 +00:00
|
|
|
|
ch == CharEncoding.UNPRINTABLE_CHAR) {
|
2019-05-28 01:46:09 +00:00
|
|
|
|
// Must write it as a byte.
|
2019-08-09 23:42:30 +00:00
|
|
|
|
WriteByte(rawCh);
|
2019-05-28 01:46:09 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we're at the start of a line, add delimiter, then new char.
|
|
|
|
|
// If we're inside quotes, just add the character. We must have space for
|
|
|
|
|
// two chars (new char, close quote).
|
|
|
|
|
// If we're outside quotes, add a comma and delimiter, then the character.
|
|
|
|
|
// We must have 4 chars remaining (comma, open quote, new char, close quote).
|
|
|
|
|
switch (mState) {
|
|
|
|
|
case State.StartOfLine:
|
2019-08-14 22:25:09 +00:00
|
|
|
|
mBuffer[mIndex++] = mDelimiterDef.OpenDelim;
|
2019-05-28 01:46:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case State.InQuote:
|
2019-08-14 00:22:21 +00:00
|
|
|
|
if (mIndex + 2 > mMaxOperandLen) {
|
2019-05-28 01:46:09 +00:00
|
|
|
|
Flush();
|
2019-08-14 22:25:09 +00:00
|
|
|
|
mBuffer[mIndex++] = mDelimiterDef.OpenDelim;
|
2019-05-28 01:46:09 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case State.OutQuote:
|
2019-08-14 00:22:21 +00:00
|
|
|
|
if (mIndex + 4 > mMaxOperandLen) {
|
2019-05-28 01:46:09 +00:00
|
|
|
|
Flush();
|
2019-08-14 22:25:09 +00:00
|
|
|
|
mBuffer[mIndex++] = mDelimiterDef.OpenDelim;
|
2019-05-28 01:46:09 +00:00
|
|
|
|
} else {
|
|
|
|
|
mBuffer[mIndex++] = ',';
|
2019-08-14 22:25:09 +00:00
|
|
|
|
mBuffer[mIndex++] = mDelimiterDef.OpenDelim;
|
2019-05-28 01:46:09 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Debug.Assert(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
mBuffer[mIndex++] = ch;
|
|
|
|
|
mState = State.InQuote;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write a hex value into the buffer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="val">Value to add.</param>
|
|
|
|
|
public void WriteByte(byte val) {
|
2019-08-09 23:42:30 +00:00
|
|
|
|
Debug.Assert(mState != State.Finished);
|
|
|
|
|
|
|
|
|
|
HasEscapedText = true;
|
|
|
|
|
|
2019-05-28 01:46:09 +00:00
|
|
|
|
// If we're at the start of a line, just output the byte.
|
|
|
|
|
// If we're inside quotes, emit a delimiter, comma, and the byte. We must
|
|
|
|
|
// have space for four (DenseHex) or five (CommaSep) chars.
|
|
|
|
|
// If we're outside quotes, add the byte. We must have two (DenseHex) or
|
|
|
|
|
// four (CommaSep) chars remaining.
|
|
|
|
|
switch (mState) {
|
|
|
|
|
case State.StartOfLine:
|
|
|
|
|
break;
|
|
|
|
|
case State.InQuote:
|
2019-08-14 00:22:21 +00:00
|
|
|
|
int minWidth = (mRawStyle == RawOutputStyle.CommaSep) ? 5 : 4;
|
|
|
|
|
if (mIndex + minWidth > mMaxOperandLen) {
|
2019-05-28 01:46:09 +00:00
|
|
|
|
Flush();
|
|
|
|
|
} else {
|
2019-08-14 22:25:09 +00:00
|
|
|
|
mBuffer[mIndex++] = mDelimiterDef.CloseDelim;
|
2019-05-28 01:46:09 +00:00
|
|
|
|
mBuffer[mIndex++] = ',';
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case State.OutQuote:
|
2019-08-14 00:22:21 +00:00
|
|
|
|
minWidth = (mRawStyle == RawOutputStyle.CommaSep) ? 4 : 2;
|
|
|
|
|
if (mIndex + minWidth > mMaxOperandLen) {
|
2019-05-28 01:46:09 +00:00
|
|
|
|
Flush();
|
|
|
|
|
} else {
|
2019-08-14 00:22:21 +00:00
|
|
|
|
if (mRawStyle == RawOutputStyle.CommaSep) {
|
2019-05-28 01:46:09 +00:00
|
|
|
|
mBuffer[mIndex++] = ',';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Debug.Assert(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 00:22:21 +00:00
|
|
|
|
if (mRawStyle == RawOutputStyle.CommaSep) {
|
2019-05-28 01:46:09 +00:00
|
|
|
|
mBuffer[mIndex++] = '$';
|
|
|
|
|
}
|
|
|
|
|
mBuffer[mIndex++] = mHexChars[val >> 4];
|
|
|
|
|
mBuffer[mIndex++] = mHexChars[val & 0x0f];
|
|
|
|
|
mState = State.OutQuote;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tells the object to flush any pending data to the output.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Finish() {
|
|
|
|
|
Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Outputs the buffer of pending data. A closing delimiter will be added if needed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void Flush() {
|
|
|
|
|
switch (mState) {
|
|
|
|
|
case State.StartOfLine:
|
|
|
|
|
// empty string; put out a pair of delimiters
|
2019-08-14 22:25:09 +00:00
|
|
|
|
mBuffer[mIndex++] = mDelimiterDef.OpenDelim;
|
|
|
|
|
mBuffer[mIndex++] = mDelimiterDef.CloseDelim;
|
2019-05-28 01:46:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case State.InQuote:
|
|
|
|
|
// add delimiter and finish
|
2019-08-14 22:25:09 +00:00
|
|
|
|
mBuffer[mIndex++] = mDelimiterDef.CloseDelim;
|
2019-05-28 01:46:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case State.OutQuote:
|
|
|
|
|
// just output it
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-08-09 23:42:30 +00:00
|
|
|
|
|
|
|
|
|
string newStr = new string(mBuffer, 0, mIndex);
|
2019-08-14 00:22:21 +00:00
|
|
|
|
Debug.Assert(newStr.Length <= mMaxOperandLen);
|
2019-08-09 23:42:30 +00:00
|
|
|
|
Lines.Add(newStr);
|
|
|
|
|
|
|
|
|
|
mState = State.Finished;
|
|
|
|
|
|
2019-05-28 01:46:09 +00:00
|
|
|
|
mIndex = 0;
|
2019-08-09 23:42:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Feeds the bytes into the StringGather.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void FeedBytes(byte[] data, int offset, int length, int leadingBytes,
|
2019-08-14 00:22:21 +00:00
|
|
|
|
ReverseMode revMode) {
|
2019-08-09 23:42:30 +00:00
|
|
|
|
int startOffset = offset;
|
|
|
|
|
int strEndOffset = offset + length;
|
|
|
|
|
|
|
|
|
|
// Write leading bytes. This is used for the 8- or 16-bit length (when no
|
|
|
|
|
// appropriate pseudo-op is available), because we want to output that as hex
|
|
|
|
|
// even if it maps to a printable character.
|
|
|
|
|
while (leadingBytes-- > 0) {
|
|
|
|
|
WriteByte(data[offset++]);
|
|
|
|
|
}
|
2019-08-14 00:22:21 +00:00
|
|
|
|
if (revMode == ReverseMode.LineReverse) {
|
2019-08-09 23:42:30 +00:00
|
|
|
|
// Max per line is line length minus the two delimiters. We don't allow
|
|
|
|
|
// any hex quoting in reversed text, so this always works. (If somebody
|
|
|
|
|
// does try to reverse text with delimiters or unprintable chars, we'll
|
|
|
|
|
// blow out the line limit, but for a cross-assembler that should be purely
|
|
|
|
|
// cosmetic.)
|
2019-08-14 00:22:21 +00:00
|
|
|
|
int maxPerLine = mMaxOperandLen - 2;
|
2019-08-09 23:42:30 +00:00
|
|
|
|
int numBlockLines = (length + maxPerLine - 1) / maxPerLine;
|
|
|
|
|
|
|
|
|
|
for (int chunk = 0; chunk < numBlockLines; chunk++) {
|
|
|
|
|
int chunkOffset = startOffset + chunk * maxPerLine;
|
|
|
|
|
int endOffset = chunkOffset + maxPerLine;
|
|
|
|
|
if (endOffset > strEndOffset) {
|
|
|
|
|
endOffset = strEndOffset;
|
|
|
|
|
}
|
|
|
|
|
for (int off = endOffset - 1; off >= chunkOffset; off--) {
|
|
|
|
|
WriteChar(data[off]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-14 00:22:21 +00:00
|
|
|
|
} else if (revMode == ReverseMode.FullReverse) {
|
|
|
|
|
for (; offset < strEndOffset; offset++) {
|
|
|
|
|
int posn = startOffset + (strEndOffset - offset) - 1;
|
|
|
|
|
WriteChar(data[posn]);
|
|
|
|
|
}
|
2019-08-09 23:42:30 +00:00
|
|
|
|
} else {
|
2019-08-14 00:22:21 +00:00
|
|
|
|
Debug.Assert(revMode == ReverseMode.Forward);
|
2019-08-09 23:42:30 +00:00
|
|
|
|
for (; offset < strEndOffset; offset++) {
|
|
|
|
|
WriteChar(data[offset]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-28 01:46:09 +00:00
|
|
|
|
|
2019-08-09 23:42:30 +00:00
|
|
|
|
Finish();
|
2019-05-28 01:46:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|