mirror of
https://gitlab.com/camelot/kickc.git
synced 2025-04-08 14:37:40 +00:00
Added support for \xnn as escape in strings. However only characters defined in the current encoding works. Refactored handling of escapes and encodings. Closes #383
This commit is contained in:
parent
b36a8b087c
commit
61c5914f73
@ -1,6 +1,6 @@
|
||||
package dk.camelot64.kickc.asm;
|
||||
|
||||
import dk.camelot64.kickc.model.values.ConstantString;
|
||||
import dk.camelot64.kickc.model.values.StringEncoding;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -21,9 +21,9 @@ public class AsmDataChunk {
|
||||
/** The value of the data element. */
|
||||
String value;
|
||||
/** The string encoding used in any char/string value */
|
||||
Set<ConstantString.Encoding> encoding;
|
||||
Set<StringEncoding> encoding;
|
||||
|
||||
AsmDataNumericElement(AsmDataNumeric.Type type, String value, Set<ConstantString.Encoding> encoding) {
|
||||
AsmDataNumericElement(AsmDataNumeric.Type type, String value, Set<StringEncoding> encoding) {
|
||||
this.type = type;
|
||||
this.value = value;
|
||||
this.encoding = encoding;
|
||||
@ -37,7 +37,7 @@ public class AsmDataChunk {
|
||||
return value;
|
||||
}
|
||||
|
||||
public Set<ConstantString.Encoding> getEncoding() {
|
||||
public Set<StringEncoding> getEncoding() {
|
||||
return encoding;
|
||||
}
|
||||
}
|
||||
@ -53,9 +53,9 @@ public class AsmDataChunk {
|
||||
/** The fill value. */
|
||||
String fillValue;
|
||||
/** The string encoding used in any char/string value */
|
||||
Set<ConstantString.Encoding> encoding;
|
||||
Set<StringEncoding> encoding;
|
||||
|
||||
AsmDataFilledElement(AsmDataNumeric.Type type, String totalSizeBytesAsm, int numElements, String fillValue, Set<ConstantString.Encoding> encoding) {
|
||||
AsmDataFilledElement(AsmDataNumeric.Type type, String totalSizeBytesAsm, int numElements, String fillValue, Set<StringEncoding> encoding) {
|
||||
this.type = type;
|
||||
this.totalSizeBytesAsm = totalSizeBytesAsm;
|
||||
this.numElements = numElements;
|
||||
@ -79,7 +79,7 @@ public class AsmDataChunk {
|
||||
return fillValue;
|
||||
}
|
||||
|
||||
public Set<ConstantString.Encoding> getEncoding() {
|
||||
public Set<StringEncoding> getEncoding() {
|
||||
return encoding;
|
||||
}
|
||||
}
|
||||
@ -89,9 +89,9 @@ public class AsmDataChunk {
|
||||
/** The string value. */
|
||||
String value;
|
||||
/** The string encoding used in the string value */
|
||||
Set<ConstantString.Encoding> encoding;
|
||||
Set<StringEncoding> encoding;
|
||||
|
||||
AsmDataStringElement(String value, Set<ConstantString.Encoding> encoding) {
|
||||
AsmDataStringElement(String value, Set<StringEncoding> encoding) {
|
||||
this.value = value;
|
||||
this.encoding = encoding;
|
||||
}
|
||||
@ -100,7 +100,7 @@ public class AsmDataChunk {
|
||||
return value;
|
||||
}
|
||||
|
||||
public Set<ConstantString.Encoding> getEncoding() {
|
||||
public Set<StringEncoding> getEncoding() {
|
||||
return encoding;
|
||||
}
|
||||
}
|
||||
@ -113,9 +113,9 @@ public class AsmDataChunk {
|
||||
/** The kickasm code initializing the value. */
|
||||
String kickAsmCode;
|
||||
/** The string encoding used in the string value */
|
||||
Set<ConstantString.Encoding> encoding;
|
||||
Set<StringEncoding> encoding;
|
||||
|
||||
public AsmDataKickAsmElement(int byteSize, String kickAsmCode, Set<ConstantString.Encoding> encoding) {
|
||||
public AsmDataKickAsmElement(int byteSize, String kickAsmCode, Set<StringEncoding> encoding) {
|
||||
this.byteSize = byteSize;
|
||||
this.kickAsmCode = kickAsmCode;
|
||||
this.encoding = encoding;
|
||||
@ -129,7 +129,7 @@ public class AsmDataChunk {
|
||||
return kickAsmCode;
|
||||
}
|
||||
|
||||
public Set<ConstantString.Encoding> getEncoding() {
|
||||
public Set<StringEncoding> getEncoding() {
|
||||
return encoding;
|
||||
}
|
||||
}
|
||||
@ -141,19 +141,19 @@ public class AsmDataChunk {
|
||||
this.elements = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addDataNumeric(AsmDataNumeric.Type type, String value, Set<ConstantString.Encoding> encoding) {
|
||||
public void addDataNumeric(AsmDataNumeric.Type type, String value, Set<StringEncoding> encoding) {
|
||||
elements.add(new AsmDataNumericElement(type, value, encoding));
|
||||
}
|
||||
|
||||
public void addDataFilled(AsmDataNumeric.Type type, String totalSizeBytesAsm, int numElements, String fillValue, Set<ConstantString.Encoding> encoding) {
|
||||
public void addDataFilled(AsmDataNumeric.Type type, String totalSizeBytesAsm, int numElements, String fillValue, Set<StringEncoding> encoding) {
|
||||
elements.add(new AsmDataFilledElement(type, totalSizeBytesAsm, numElements, fillValue, encoding));
|
||||
}
|
||||
|
||||
public void addDataString(String string, Set<ConstantString.Encoding> encoding) {
|
||||
public void addDataString(String string, Set<StringEncoding> encoding) {
|
||||
elements.add(new AsmDataStringElement(string, encoding));
|
||||
}
|
||||
|
||||
public void addDataKickAsm(int byteSize, String kickAsmCode, Set<ConstantString.Encoding> encoding) {
|
||||
public void addDataKickAsm(int byteSize, String kickAsmCode, Set<StringEncoding> encoding) {
|
||||
elements.add(new AsmDataKickAsmElement(byteSize, kickAsmCode, encoding));
|
||||
}
|
||||
|
||||
|
@ -34,11 +34,11 @@ public class AsmFormat {
|
||||
return getAsmBool(((ConstantBool) value).getBool());
|
||||
} else if(value instanceof ConstantChar) {
|
||||
ConstantChar constantChar = (ConstantChar) value;
|
||||
String escapedChar = ConstantChar.asciiToCharEscape(constantChar.getChar());
|
||||
String escapedChar = constantChar.getCharEscaped();
|
||||
return "'" + escapedChar + "'";
|
||||
} else if(value instanceof ConstantString) {
|
||||
String stringValue = ((ConstantString) value).getValue();
|
||||
String escapedString = ConstantString.asciiToStringEscape(stringValue);
|
||||
String escapedString = ((ConstantString) value).getStringEscaped();
|
||||
boolean hasEscape = !stringValue.equals(escapedString);
|
||||
return (hasEscape ? "@" : "") + "\"" + escapedString + "\"";
|
||||
} else if(value instanceof ConstantUnary) {
|
||||
|
@ -2,8 +2,8 @@ package dk.camelot64.kickc.asm;
|
||||
|
||||
import dk.camelot64.kickc.model.CompileError;
|
||||
import dk.camelot64.kickc.model.Program;
|
||||
import dk.camelot64.kickc.model.values.ConstantString;
|
||||
import dk.camelot64.kickc.model.values.ScopeRef;
|
||||
import dk.camelot64.kickc.model.values.StringEncoding;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@ -31,7 +31,7 @@ public class AsmProgram {
|
||||
private int nextLineIndex;
|
||||
|
||||
/** The current encoding used for printing strings. */
|
||||
private ConstantString.Encoding currentEncoding = ConstantString.Encoding.SCREENCODE_MIXED;
|
||||
private StringEncoding currentEncoding = StringEncoding.SCREENCODE_MIXED;
|
||||
|
||||
public AsmProgram() {
|
||||
this.chunks = new ArrayList<>();
|
||||
@ -64,17 +64,17 @@ public class AsmProgram {
|
||||
* Get the current encoding used for strings/chars
|
||||
* @return The encoding
|
||||
*/
|
||||
public ConstantString.Encoding getCurrentEncoding() {
|
||||
public StringEncoding getCurrentEncoding() {
|
||||
return currentEncoding;
|
||||
}
|
||||
|
||||
public void ensureEncoding(Collection<ConstantString.Encoding> encodings) {
|
||||
public void ensureEncoding(Collection<StringEncoding> encodings) {
|
||||
if(encodings == null || encodings.size() == 0) return;
|
||||
if(encodings.size() > 1) {
|
||||
throw new CompileError("Different character encodings in one ASM statement not supported!");
|
||||
}
|
||||
// Size is 1 - grab it!
|
||||
ConstantString.Encoding encoding = encodings.iterator().next();
|
||||
StringEncoding encoding = encodings.iterator().next();
|
||||
if(!getCurrentEncoding().equals(encoding)) {
|
||||
addLine(new AsmSetEncoding(encoding));
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
package dk.camelot64.kickc.asm;
|
||||
|
||||
import dk.camelot64.kickc.model.values.ConstantString;
|
||||
import dk.camelot64.kickc.model.values.StringEncoding;
|
||||
|
||||
/** Set the text encoding */
|
||||
public class AsmSetEncoding implements AsmLine {
|
||||
|
||||
private final ConstantString.Encoding encoding;
|
||||
private final StringEncoding encoding;
|
||||
private int index;
|
||||
|
||||
public AsmSetEncoding(ConstantString.Encoding encoding) {
|
||||
public AsmSetEncoding(StringEncoding encoding) {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
@ -37,7 +37,7 @@ public class AsmSetEncoding implements AsmLine {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public ConstantString.Encoding getEncoding() {
|
||||
public StringEncoding getEncoding() {
|
||||
return encoding;
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
package dk.camelot64.kickc.model.values;
|
||||
|
||||
import dk.camelot64.kickc.model.CompileError;
|
||||
import dk.camelot64.kickc.model.InternalError;
|
||||
import dk.camelot64.kickc.model.Program;
|
||||
import dk.camelot64.kickc.model.symbols.ProgramScope;
|
||||
import dk.camelot64.kickc.model.types.SymbolType;
|
||||
import kickass.nonasm.c64.CharToPetsciiConverter;
|
||||
|
||||
/**
|
||||
* SSA form constant char value (a byte)
|
||||
@ -16,9 +13,9 @@ public class ConstantChar implements ConstantEnumerable<Character> {
|
||||
private Character value;
|
||||
|
||||
/** The encoding of the character. */
|
||||
private ConstantString.Encoding encoding;
|
||||
private StringEncoding encoding;
|
||||
|
||||
public ConstantChar(Character value, ConstantString.Encoding encoding) {
|
||||
public ConstantChar(Character value, StringEncoding encoding) {
|
||||
this.value = value;
|
||||
this.encoding = encoding;
|
||||
}
|
||||
@ -43,23 +40,21 @@ public class ConstantChar implements ConstantEnumerable<Character> {
|
||||
*/
|
||||
@Override
|
||||
public Long getInteger() {
|
||||
Byte constCharIntValue = null;
|
||||
if(ConstantString.Encoding.SCREENCODE_MIXED.equals(encoding)) {
|
||||
constCharIntValue = CharToPetsciiConverter.charToScreenCode_mixed.get(value);
|
||||
} else if(ConstantString.Encoding.SCREENCODE_UPPER.equals(encoding)) {
|
||||
constCharIntValue = CharToPetsciiConverter.charToScreenCode_upper.get(value);
|
||||
} else if(ConstantString.Encoding.PETSCII_MIXED.equals(encoding)) {
|
||||
constCharIntValue = CharToPetsciiConverter.charToPetscii_mixed.get(value);
|
||||
} else if(ConstantString.Encoding.PETSCII_UPPER.equals(encoding)) {
|
||||
constCharIntValue = CharToPetsciiConverter.charToPetscii_upper.get(value);
|
||||
}
|
||||
return constCharIntValue.longValue();
|
||||
return encoding.getInteger(value);
|
||||
}
|
||||
|
||||
public ConstantString.Encoding getEncoding() {
|
||||
public StringEncoding getEncoding() {
|
||||
return encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the char where any special character has been properly escaped (eg '\n' for a newline).
|
||||
* @return The character with escapes if needed.
|
||||
*/
|
||||
public String getCharEscaped() {
|
||||
return encoding.asciiToEscape(value, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toString(null);
|
||||
@ -67,7 +62,7 @@ public class ConstantChar implements ConstantEnumerable<Character> {
|
||||
|
||||
@Override
|
||||
public String toString(Program program) {
|
||||
String enc = (encoding.equals(ConstantString.Encoding.SCREENCODE_MIXED)) ? "" : encoding.suffix;
|
||||
String enc = (encoding.equals(StringEncoding.SCREENCODE_MIXED)) ? "" : encoding.suffix;
|
||||
if(program == null) {
|
||||
return "'" + value + "'" + enc;
|
||||
} else {
|
||||
@ -75,59 +70,4 @@ public class ConstantChar implements ConstantEnumerable<Character> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a potentially escaped char
|
||||
*
|
||||
* @param charString Either just a single char - or an escaped char using \-notation
|
||||
* @return The ASCII char
|
||||
*/
|
||||
public static char charEscapeToAscii(String charString) {
|
||||
if(charString.length() == 1) {
|
||||
return charString.charAt(0);
|
||||
} else if(charString.length() == 2) {
|
||||
switch(charString.charAt(1)) {
|
||||
case 'n':
|
||||
return '\n';
|
||||
case 'r':
|
||||
return '\r';
|
||||
case 'f':
|
||||
return '\f';
|
||||
case '"':
|
||||
return '\"';
|
||||
case '\'':
|
||||
return '\'';
|
||||
case '\\':
|
||||
return '\\';
|
||||
default:
|
||||
throw new CompileError("Illegal char escape sequence \\" + charString.charAt(1));
|
||||
}
|
||||
} else {
|
||||
throw new InternalError("Illegal char '" + charString + "'");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a char to an escape sequence if needed. If not needed the char itself is returned.
|
||||
* @param aChar The char
|
||||
* @return The char itself - or the appropriate escape sequence
|
||||
*/
|
||||
public static String asciiToCharEscape(char aChar) {
|
||||
switch(aChar) {
|
||||
case '\n':
|
||||
return "\\n";
|
||||
case '\r':
|
||||
return "\\r";
|
||||
case '\f':
|
||||
return "\\f";
|
||||
case '\'':
|
||||
return "\\'";
|
||||
case '\\':
|
||||
return "\\\\";
|
||||
default:
|
||||
return Character.toString(aChar);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package dk.camelot64.kickc.model.values;
|
||||
|
||||
import dk.camelot64.kickc.model.CompileError;
|
||||
import dk.camelot64.kickc.model.Program;
|
||||
import dk.camelot64.kickc.model.symbols.ProgramScope;
|
||||
import dk.camelot64.kickc.model.types.SymbolType;
|
||||
@ -13,33 +12,16 @@ import java.util.Objects;
|
||||
*/
|
||||
public class ConstantString implements ConstantLiteral<String> {
|
||||
|
||||
/** String encoding. */
|
||||
public static enum Encoding {
|
||||
PETSCII_MIXED("petscii_mixed", "pm"),
|
||||
PETSCII_UPPER("petscii_upper", "pu"),
|
||||
SCREENCODE_MIXED("screencode_mixed", "sm"),
|
||||
SCREENCODE_UPPER("screencode_upper", "su");
|
||||
|
||||
public final String name;
|
||||
public final String suffix;
|
||||
|
||||
Encoding(String name, String suffix) {
|
||||
this.name = name;
|
||||
this.suffix = suffix;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** The string value. */
|
||||
private String value;
|
||||
|
||||
/** The encoding to use for the string. */
|
||||
private Encoding encoding;
|
||||
private StringEncoding encoding;
|
||||
|
||||
/** true if the string should be zero terminated. */
|
||||
private boolean zeroTerminated;
|
||||
|
||||
public ConstantString(String value, Encoding encoding, boolean zeroTerminated) {
|
||||
public ConstantString(String value, StringEncoding encoding, boolean zeroTerminated) {
|
||||
this.value = value;
|
||||
this.encoding = encoding;
|
||||
this.zeroTerminated = zeroTerminated;
|
||||
@ -59,20 +41,29 @@ public class ConstantString implements ConstantLiteral<String> {
|
||||
return value;
|
||||
}
|
||||
|
||||
public Encoding getEncoding() {
|
||||
public StringEncoding getEncoding() {
|
||||
return encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the string where characters have been escaped. (eg. newline as '\n')
|
||||
* @return The escaped string.
|
||||
*/
|
||||
public String getStringEscaped() {
|
||||
return encoding.asciiToEscape(value);
|
||||
}
|
||||
|
||||
public boolean isZeroTerminated() {
|
||||
return zeroTerminated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the length of the string - including zero-termination if present.
|
||||
*
|
||||
* @return The length
|
||||
*/
|
||||
public int getStringLength() {
|
||||
return value.length() + (zeroTerminated?1:0);
|
||||
return value.length() + (zeroTerminated ? 1 : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -82,7 +73,7 @@ public class ConstantString implements ConstantLiteral<String> {
|
||||
|
||||
@Override
|
||||
public String toString(Program program) {
|
||||
String suffix = (encoding.equals(Encoding.SCREENCODE_MIXED)) ? "" : encoding.suffix;
|
||||
String suffix = (encoding.equals(StringEncoding.SCREENCODE_MIXED)) ? "" : encoding.suffix;
|
||||
suffix += zeroTerminated ? "" : "z";
|
||||
if(program == null) {
|
||||
return "\"" + value + "\"" + suffix;
|
||||
@ -106,87 +97,5 @@ public class ConstantString implements ConstantLiteral<String> {
|
||||
return Objects.hash(value, encoding, zeroTerminated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find any string escape sequences and convert them to the ASCII-equivalent character
|
||||
*
|
||||
* @param stringValue The string to convert
|
||||
* @return The string where any escape sequence has been converted to ASCII
|
||||
* @throws CompileError If the string value has a syntax error (unfinished or illegal escape sequences)
|
||||
*/
|
||||
public static String stringEscapeToAscii(String stringValue) {
|
||||
StringBuilder stringResult = new StringBuilder();
|
||||
char[] stringChars = stringValue.toCharArray();
|
||||
int i = 0;
|
||||
while(i < stringChars.length) {
|
||||
// State: Normal - examine whether an escape is starting
|
||||
char stringChar = stringChars[i];
|
||||
if(stringChar == '\\') {
|
||||
// Escape started - handle it!
|
||||
i++;
|
||||
if(i >= stringChars.length) throw new CompileError("Unfinished string escape sequence at end of string");
|
||||
char escapeChar = stringChars[i];
|
||||
switch(escapeChar) {
|
||||
case 'n':
|
||||
stringChar = '\n';
|
||||
break;
|
||||
case 'r':
|
||||
stringChar = '\r';
|
||||
break;
|
||||
case 'f':
|
||||
stringChar = '\f';
|
||||
break;
|
||||
case '"':
|
||||
stringChar = '"';
|
||||
break;
|
||||
case '\'':
|
||||
stringChar = '\'';
|
||||
break;
|
||||
case '\\':
|
||||
stringChar = '\\';
|
||||
break;
|
||||
default:
|
||||
throw new CompileError("Illegal string escape sequence \\" + escapeChar);
|
||||
}
|
||||
}
|
||||
// Output the char
|
||||
stringResult.append(stringChar);
|
||||
i++;
|
||||
}
|
||||
return stringResult.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find any ASCII character that must be escaped to represent the string in source code - and convert them to the escaped string.
|
||||
*
|
||||
* @param stringValue The string to convert
|
||||
* @return The string where any character that must be escaped is converted to the escape sequence
|
||||
*/
|
||||
public static String asciiToStringEscape(String stringValue) {
|
||||
StringBuilder stringResult = new StringBuilder();
|
||||
char[] stringChars = stringValue.toCharArray();
|
||||
for(char stringChar : stringChars) {
|
||||
switch(stringChar) {
|
||||
case '\n':
|
||||
stringResult.append("\\n");
|
||||
break;
|
||||
case '\r':
|
||||
stringResult.append("\\r");
|
||||
break;
|
||||
case '\f':
|
||||
stringResult.append("\\f");
|
||||
break;
|
||||
case '"':
|
||||
stringResult.append("\\\"");
|
||||
break;
|
||||
case '\\':
|
||||
stringResult.append("\\\\");
|
||||
break;
|
||||
default:
|
||||
stringResult.append(stringChar);
|
||||
}
|
||||
}
|
||||
return stringResult.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,155 @@
|
||||
package dk.camelot64.kickc.model.values;
|
||||
|
||||
import dk.camelot64.kickc.model.CompileError;
|
||||
import kickass.nonasm.c64.CharToPetsciiConverter;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.PrimitiveIterator;
|
||||
|
||||
/** String encoding. */
|
||||
public enum StringEncoding {
|
||||
|
||||
PETSCII_MIXED("petscii_mixed", "pm", CharToPetsciiConverter.charToScreenCode_mixed),
|
||||
PETSCII_UPPER("petscii_upper", "pu", CharToPetsciiConverter.charToScreenCode_upper),
|
||||
SCREENCODE_MIXED("screencode_mixed", "sm", CharToPetsciiConverter.charToScreenCode_mixed),
|
||||
SCREENCODE_UPPER("screencode_upper", "su", CharToPetsciiConverter.charToScreenCode_upper);
|
||||
|
||||
/** The encoding name. */
|
||||
public final String name;
|
||||
|
||||
/** The string suffix usable for selecting the encoding. */
|
||||
public final String suffix;
|
||||
|
||||
/** The mapping from character value to integer value for the encoding. */
|
||||
public final Map<Character, Byte> mapping;
|
||||
|
||||
StringEncoding(String name, String suffix, Map<Character, Byte> mapping) {
|
||||
this.name = name;
|
||||
this.suffix = suffix;
|
||||
this.mapping = mapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the integer value of a character using a specific encoding
|
||||
*
|
||||
* @param aChar The character
|
||||
* @return The integer value of the character using the encoding
|
||||
*/
|
||||
public Long getInteger(Character aChar) {
|
||||
Byte constCharIntValue = mapping.get(aChar);
|
||||
return constCharIntValue.longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a character with a specific integer value using the specific encoding
|
||||
*
|
||||
* @param intValue The integer value
|
||||
* @return The character that has the integer value using the encoding
|
||||
*/
|
||||
public Character getChar(Byte intValue) {
|
||||
for(Map.Entry<Character, Byte> mapEntry : mapping.entrySet()) {
|
||||
if(mapEntry.getValue() == intValue.byteValue())
|
||||
return mapEntry.getKey();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find any string escape sequences and convert them to the ASCII-equivalent character
|
||||
*
|
||||
* @param stringValue The string to convert
|
||||
* @return The string where any escape sequence has been converted to ASCII
|
||||
* @throws CompileError If the string value has a syntax error (unfinished or illegal escape sequences)
|
||||
*/
|
||||
public String escapeToAscii(String stringValue) {
|
||||
StringBuilder stringResult = new StringBuilder();
|
||||
final PrimitiveIterator.OfInt escapedIterator = stringValue.chars().iterator();
|
||||
while(escapedIterator.hasNext()) {
|
||||
stringResult.append(escapeToAsciiFirst(escapedIterator));
|
||||
}
|
||||
return stringResult.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Grabs the first (potentially escaped) character from an iterator.
|
||||
* Converts any escapes such as '\n', '\xnn' etc. to the right ASCII character.
|
||||
* Moves the iterator forward.
|
||||
*
|
||||
* @param escapedString The characters of the string to parse one char from. The iterator is moved beyond any handled chars.
|
||||
* @return The first ASCII character of the list.
|
||||
*/
|
||||
public char escapeToAsciiFirst(PrimitiveIterator.OfInt escapedCharsIterator) {
|
||||
char stringChar = (char)escapedCharsIterator.nextInt();
|
||||
if(stringChar != '\\')
|
||||
return stringChar;
|
||||
// Escape started - handle it!
|
||||
if(!escapedCharsIterator.hasNext()) throw new CompileError("Unfinished string escape sequence at end of string");
|
||||
char escapeChar = (char)escapedCharsIterator.nextInt();
|
||||
switch(escapeChar) {
|
||||
case 'n':
|
||||
return '\n';
|
||||
case 'r':
|
||||
return '\r';
|
||||
case 'f':
|
||||
return '\f';
|
||||
case '"':
|
||||
return '"';
|
||||
case '\'':
|
||||
return '\'';
|
||||
case '\\':
|
||||
return '\\';
|
||||
case 'x':
|
||||
String hexNum = "";
|
||||
hexNum += (char)escapedCharsIterator.nextInt();
|
||||
hexNum += (char)escapedCharsIterator.nextInt();
|
||||
final int hexChar = Integer.parseInt(hexNum, 16);
|
||||
final Character aChar = getChar((byte) hexChar);
|
||||
if(aChar == null)
|
||||
throw new CompileError("No character 0x" + hexNum + " in encoding " + name);
|
||||
return aChar;
|
||||
default:
|
||||
throw new CompileError("Illegal string escape sequence \\" + escapeChar);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a char to an escape sequence if needed. If not needed the char itself is returned.
|
||||
* @param aChar The char
|
||||
* @param escapeSingleQuotes Should single quotes ' be escaped. (true when encoding chars, false when encoding chars)
|
||||
* @return The char itself - or the appropriate escape sequence
|
||||
*/
|
||||
public String asciiToEscape(char aChar, boolean escapeSingleQuotes) {
|
||||
switch(aChar) {
|
||||
case '\n':
|
||||
return "\\n";
|
||||
case '\r':
|
||||
return "\\r";
|
||||
case '\f':
|
||||
return "\\f";
|
||||
case '\"':
|
||||
return "\\\"";
|
||||
case '\'':
|
||||
if(escapeSingleQuotes)
|
||||
return "\\'";
|
||||
else
|
||||
return Character.toString(aChar);
|
||||
case '\\':
|
||||
return "\\\\";
|
||||
default:
|
||||
return Character.toString(aChar);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a char to an escape sequence if needed. If not needed the char itself is returned.
|
||||
* @param aChar The char
|
||||
* @return The char itself - or the appropriate escape sequence
|
||||
*/
|
||||
public String asciiToEscape(String string) {
|
||||
StringBuilder escaped = new StringBuilder();
|
||||
string.chars().forEach(value -> escaped.append(asciiToEscape((char) value, false)));
|
||||
return escaped.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -150,7 +150,7 @@ fragment NAME_CHAR : [a-zA-Z0-9_];
|
||||
|
||||
// Strings and chars
|
||||
STRING : '"' ('\\"' | ~'"')* '"' [z]?([ps][mu]?)?[z]? ;
|
||||
CHAR : '\'' ('\\'['"rfn] | ~'\'' ) '\'';
|
||||
CHAR : '\'' ('\\'(['"rfn]|'x'[0-9a-f][0-9a-f]) | ~'\'' ) '\'';
|
||||
|
||||
// White space on hidden channel 1
|
||||
WS : [ \t\r\n\u00a0]+ -> channel(1);
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@ -153,7 +153,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
|
||||
@Override
|
||||
public Object visitGlobalDirectiveEncoding(KickCParser.GlobalDirectiveEncodingContext ctx) {
|
||||
try {
|
||||
this.currentEncoding = ConstantString.Encoding.valueOf(ctx.NAME().getText().toUpperCase(Locale.ENGLISH));
|
||||
this.currentEncoding = StringEncoding.valueOf(ctx.NAME().getText().toUpperCase(Locale.ENGLISH));
|
||||
} catch(IllegalArgumentException e) {
|
||||
throw new CompileError("Unknown string encoding " + ctx.NAME().getText(), new StatementSource(ctx));
|
||||
}
|
||||
@ -2196,18 +2196,18 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
|
||||
}
|
||||
|
||||
/** The current string encoding used if no explicit encoding is specified. */
|
||||
private ConstantString.Encoding currentEncoding = ConstantString.Encoding.SCREENCODE_MIXED;
|
||||
private StringEncoding currentEncoding = StringEncoding.SCREENCODE_MIXED;
|
||||
|
||||
@Override
|
||||
public RValue visitExprString(KickCParser.ExprStringContext ctx) {
|
||||
StringBuilder stringValue = new StringBuilder();
|
||||
String subText;
|
||||
String lastSuffix = "";
|
||||
ConstantString.Encoding encoding = null;
|
||||
StringEncoding encoding = null;
|
||||
for(TerminalNode stringNode : ctx.STRING()) {
|
||||
subText = stringNode.getText();
|
||||
String suffix = subText.substring(subText.lastIndexOf('"') + 1);
|
||||
ConstantString.Encoding suffixEncoding = getEncodingFromSuffix(suffix);
|
||||
StringEncoding suffixEncoding = getEncodingFromSuffix(suffix);
|
||||
if(suffixEncoding != null) {
|
||||
if(encoding != null && !encoding.equals(suffixEncoding)) {
|
||||
throw new CompileError("Cannot mix encodings in concatenated strings " + ctx.getText(), new StatementSource(ctx));
|
||||
@ -2219,7 +2219,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
|
||||
}
|
||||
boolean zeroTerminated = !lastSuffix.contains("z");
|
||||
try {
|
||||
return new ConstantString(ConstantString.stringEscapeToAscii(stringValue.toString()), encoding, zeroTerminated);
|
||||
return new ConstantString(encoding.escapeToAscii(stringValue.toString()), encoding, zeroTerminated);
|
||||
} catch(CompileError e) {
|
||||
// Rethrow - adding statement context!
|
||||
throw new CompileError(e.getMessage(), new StatementSource(ctx));
|
||||
@ -2232,19 +2232,19 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
|
||||
* @param suffix The string suffix
|
||||
* @return The encoding specified by the suffix. If not the current source encoding is returned.
|
||||
*/
|
||||
private ConstantString.Encoding getEncodingFromSuffix(String suffix) {
|
||||
private StringEncoding getEncodingFromSuffix(String suffix) {
|
||||
if(suffix.contains("pm")) {
|
||||
return ConstantString.Encoding.PETSCII_MIXED;
|
||||
return StringEncoding.PETSCII_MIXED;
|
||||
} else if(suffix.contains("pu")) {
|
||||
return ConstantString.Encoding.PETSCII_UPPER;
|
||||
return StringEncoding.PETSCII_UPPER;
|
||||
} else if(suffix.contains("p")) {
|
||||
return ConstantString.Encoding.PETSCII_MIXED;
|
||||
return StringEncoding.PETSCII_MIXED;
|
||||
} else if(suffix.contains("sm")) {
|
||||
return ConstantString.Encoding.SCREENCODE_MIXED;
|
||||
return StringEncoding.SCREENCODE_MIXED;
|
||||
} else if(suffix.contains("su")) {
|
||||
return ConstantString.Encoding.SCREENCODE_UPPER;
|
||||
return StringEncoding.SCREENCODE_UPPER;
|
||||
} else if(suffix.contains("s")) {
|
||||
return ConstantString.Encoding.SCREENCODE_MIXED;
|
||||
return StringEncoding.SCREENCODE_MIXED;
|
||||
} else {
|
||||
return currentEncoding;
|
||||
}
|
||||
@ -2261,7 +2261,7 @@ public class Pass0GenerateStatementSequence extends KickCParserBaseVisitor<Objec
|
||||
try {
|
||||
String charText = ctx.getText();
|
||||
charText = charText.substring(1, charText.length() - 1);
|
||||
char constChar = ConstantChar.charEscapeToAscii(charText);
|
||||
char constChar = currentEncoding.escapeToAscii(charText).charAt(0);
|
||||
return new ConstantChar(constChar, currentEncoding);
|
||||
} catch(CompileError e) {
|
||||
// Rethrow adding source location
|
||||
|
@ -1161,8 +1161,8 @@ public class Pass4CodeGeneration {
|
||||
* @param value The constant to examine
|
||||
* @return Any encoding found inside the constant
|
||||
*/
|
||||
private static Set<ConstantString.Encoding> getEncoding(Value value) {
|
||||
LinkedHashSet<ConstantString.Encoding> encodings = new LinkedHashSet<>();
|
||||
private static Set<StringEncoding> getEncoding(Value value) {
|
||||
LinkedHashSet<StringEncoding> encodings = new LinkedHashSet<>();
|
||||
ProgramValue programValue = new ProgramValue.GenericValue(value);
|
||||
ProgramValueHandler handler = (ProgramValue pVal, Statement currentStmt, ListIterator<Statement> stmtIt, ControlFlowBlock currentBlock) -> {
|
||||
Value val = pVal.get();
|
||||
@ -1183,8 +1183,8 @@ public class Pass4CodeGeneration {
|
||||
* @param asmFragmentInstance The asm fragment instance to examine
|
||||
* @return Any encoding found inside the constant
|
||||
*/
|
||||
private static Set<ConstantString.Encoding> getEncoding(AsmFragmentInstanceSpecFactory asmFragmentInstance) {
|
||||
LinkedHashSet<ConstantString.Encoding> encodings = new LinkedHashSet<>();
|
||||
private static Set<StringEncoding> getEncoding(AsmFragmentInstanceSpecFactory asmFragmentInstance) {
|
||||
LinkedHashSet<StringEncoding> encodings = new LinkedHashSet<>();
|
||||
Map<String, Value> bindings = asmFragmentInstance.getBindings();
|
||||
for(Value boundValue : bindings.values()) {
|
||||
encodings.addAll(getEncoding(boundValue));
|
||||
|
@ -641,6 +641,11 @@ public class TestPrograms {
|
||||
compileAndCompare("code-after-return");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringEscapesErr2() throws IOException, URISyntaxException {
|
||||
assertError("string-escapes-err-2", "No character 0xff in encoding petscii_mixed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringEscapesErr1() throws IOException, URISyntaxException {
|
||||
assertError("string-escapes-err-1", "Illegal string escape sequence");
|
||||
@ -651,6 +656,11 @@ public class TestPrograms {
|
||||
assertError("string-escapes-err-0", "Unfinished string escape sequence at end of string");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringEscapes4() throws IOException, URISyntaxException {
|
||||
compileAndCompare("string-escapes-4");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringEscapes3() throws IOException, URISyntaxException {
|
||||
compileAndCompare("string-escapes-3");
|
||||
|
29
src/test/kc/string-escapes-4.kc
Normal file
29
src/test/kc/string-escapes-4.kc
Normal file
@ -0,0 +1,29 @@
|
||||
// Test using some simple supported string escape
|
||||
// Uses \xnn to add chars by hex-code
|
||||
|
||||
#pragma encoding(petscii_mixed)
|
||||
char MSG1[] = "c\x41m\x45lot";
|
||||
|
||||
#pragma encoding(screencode_upper)
|
||||
char MSG2[] = "C\x01M\x05LOT";
|
||||
|
||||
char CH = '\x10';
|
||||
|
||||
char* SCREEN1 = 0x0400;
|
||||
char* SCREEN2 = 0x0428;
|
||||
char* SCREEN3 = 0x0428;
|
||||
|
||||
void main() {
|
||||
// Show mixed chars on screen
|
||||
*((char*)0xd018) = 0x17;
|
||||
|
||||
char i=0;
|
||||
while(MSG1[i]) {
|
||||
SCREEN1[i] = MSG1[i];
|
||||
SCREEN2[i] = MSG2[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
SCREEN3[0] = CH;
|
||||
|
||||
}
|
8
src/test/kc/string-escapes-err-2.kc
Normal file
8
src/test/kc/string-escapes-err-2.kc
Normal file
@ -0,0 +1,8 @@
|
||||
// Test errors using string escape sequences
|
||||
// Unsupported hex character sequence
|
||||
|
||||
#pragma encoding(petscii_mixed)
|
||||
char MESSAGE[] = "qwe\xff";
|
||||
|
||||
void main() {
|
||||
}
|
43
src/test/ref/string-escapes-4.asm
Normal file
43
src/test/ref/string-escapes-4.asm
Normal file
@ -0,0 +1,43 @@
|
||||
// Test using some simple supported string escape
|
||||
// Uses \xnn to add chars by hex-code
|
||||
.pc = $801 "Basic"
|
||||
:BasicUpstart(main)
|
||||
.pc = $80d "Program"
|
||||
.encoding "screencode_upper"
|
||||
.const CH = 'P'
|
||||
.label SCREEN1 = $400
|
||||
.label SCREEN2 = $428
|
||||
.label SCREEN3 = $428
|
||||
main: {
|
||||
// *((char*)0xd018) = 0x17
|
||||
// Show mixed chars on screen
|
||||
lda #$17
|
||||
sta $d018
|
||||
ldx #0
|
||||
__b1:
|
||||
// while(MSG1[i])
|
||||
lda MSG1,x
|
||||
cmp #0
|
||||
bne __b2
|
||||
// SCREEN3[0] = CH
|
||||
lda #CH
|
||||
sta SCREEN3
|
||||
// }
|
||||
rts
|
||||
__b2:
|
||||
// SCREEN1[i] = MSG1[i]
|
||||
lda MSG1,x
|
||||
sta SCREEN1,x
|
||||
// SCREEN2[i] = MSG2[i]
|
||||
lda MSG2,x
|
||||
sta SCREEN2,x
|
||||
// i++;
|
||||
inx
|
||||
jmp __b1
|
||||
}
|
||||
.encoding "petscii_mixed"
|
||||
MSG1: .text "cAmElot"
|
||||
.byte 0
|
||||
.encoding "screencode_upper"
|
||||
MSG2: .text "CAMELOT"
|
||||
.byte 0
|
29
src/test/ref/string-escapes-4.cfg
Normal file
29
src/test/ref/string-escapes-4.cfg
Normal file
@ -0,0 +1,29 @@
|
||||
@begin: scope:[] from
|
||||
[0] phi()
|
||||
to:@1
|
||||
@1: scope:[] from @begin
|
||||
[1] phi()
|
||||
[2] call main
|
||||
to:@end
|
||||
@end: scope:[] from @1
|
||||
[3] phi()
|
||||
|
||||
(void()) main()
|
||||
main: scope:[main] from @1
|
||||
[4] *((byte*) 53272) ← (byte) $17
|
||||
to:main::@1
|
||||
main::@1: scope:[main] from main main::@2
|
||||
[5] (byte) main::i#2 ← phi( main/(byte) 0 main::@2/(byte) main::i#1 )
|
||||
[6] if((byte) 0!=*((const byte*) MSG1 + (byte) main::i#2)) goto main::@2
|
||||
to:main::@3
|
||||
main::@3: scope:[main] from main::@1
|
||||
[7] *((const byte*) SCREEN3) ← (const byte) CH
|
||||
to:main::@return
|
||||
main::@return: scope:[main] from main::@3
|
||||
[8] return
|
||||
to:@return
|
||||
main::@2: scope:[main] from main::@1
|
||||
[9] *((const byte*) SCREEN1 + (byte) main::i#2) ← *((const byte*) MSG1 + (byte) main::i#2)
|
||||
[10] *((const byte*) SCREEN2 + (byte) main::i#2) ← *((const byte*) MSG2 + (byte) main::i#2)
|
||||
[11] (byte) main::i#1 ← ++ (byte) main::i#2
|
||||
to:main::@1
|
456
src/test/ref/string-escapes-4.log
Normal file
456
src/test/ref/string-escapes-4.log
Normal file
@ -0,0 +1,456 @@
|
||||
Warning! Adding boolean cast to non-boolean condition *((const byte*) MSG1 + (byte) main::i)
|
||||
Identified constant variable (byte) CH
|
||||
Identified constant variable (byte*) SCREEN1
|
||||
Identified constant variable (byte*) SCREEN2
|
||||
Identified constant variable (byte*) SCREEN3
|
||||
Culled Empty Block (label) main::@4
|
||||
Culled Empty Block (label) main::@5
|
||||
Culled Empty Block (label) main::@6
|
||||
|
||||
CONTROL FLOW GRAPH SSA
|
||||
@begin: scope:[] from
|
||||
to:@1
|
||||
|
||||
(void()) main()
|
||||
main: scope:[main] from @1
|
||||
*((byte*)(number) $d018) ← (number) $17
|
||||
(byte) main::i#0 ← (byte) 0
|
||||
to:main::@1
|
||||
main::@1: scope:[main] from main main::@2
|
||||
(byte) main::i#2 ← phi( main/(byte) main::i#0 main::@2/(byte) main::i#1 )
|
||||
(bool~) main::$0 ← (number) 0 != *((const byte*) MSG1 + (byte) main::i#2)
|
||||
if((bool~) main::$0) goto main::@2
|
||||
to:main::@3
|
||||
main::@2: scope:[main] from main::@1
|
||||
(byte) main::i#3 ← phi( main::@1/(byte) main::i#2 )
|
||||
*((const byte*) SCREEN1 + (byte) main::i#3) ← *((const byte*) MSG1 + (byte) main::i#3)
|
||||
*((const byte*) SCREEN2 + (byte) main::i#3) ← *((const byte*) MSG2 + (byte) main::i#3)
|
||||
(byte) main::i#1 ← ++ (byte) main::i#3
|
||||
to:main::@1
|
||||
main::@3: scope:[main] from main::@1
|
||||
*((const byte*) SCREEN3 + (number) 0) ← (const byte) CH
|
||||
to:main::@return
|
||||
main::@return: scope:[main] from main::@3
|
||||
return
|
||||
to:@return
|
||||
@1: scope:[] from @begin
|
||||
call main
|
||||
to:@2
|
||||
@2: scope:[] from @1
|
||||
to:@end
|
||||
@end: scope:[] from @2
|
||||
|
||||
SYMBOL TABLE SSA
|
||||
(label) @1
|
||||
(label) @2
|
||||
(label) @begin
|
||||
(label) @end
|
||||
(const byte) CH = (byte) 'P'su
|
||||
(const byte*) MSG1[] = (byte*) "cAmElot"pm
|
||||
(const byte*) MSG2[] = (byte*) "CAMELOT"su
|
||||
(const byte*) SCREEN1 = (byte*)(number) $400
|
||||
(const byte*) SCREEN2 = (byte*)(number) $428
|
||||
(const byte*) SCREEN3 = (byte*)(number) $428
|
||||
(void()) main()
|
||||
(bool~) main::$0
|
||||
(label) main::@1
|
||||
(label) main::@2
|
||||
(label) main::@3
|
||||
(label) main::@return
|
||||
(byte) main::i
|
||||
(byte) main::i#0
|
||||
(byte) main::i#1
|
||||
(byte) main::i#2
|
||||
(byte) main::i#3
|
||||
|
||||
Adding number conversion cast (unumber) $17 in *((byte*)(number) $d018) ← (number) $17
|
||||
Adding number conversion cast (unumber) 0 in (bool~) main::$0 ← (number) 0 != *((const byte*) MSG1 + (byte) main::i#2)
|
||||
Adding number conversion cast (unumber) 0 in *((const byte*) SCREEN3 + (number) 0) ← (const byte) CH
|
||||
Successful SSA optimization PassNAddNumberTypeConversions
|
||||
Inlining cast *((byte*)(number) $d018) ← (unumber)(number) $17
|
||||
Successful SSA optimization Pass2InlineCast
|
||||
Simplifying constant pointer cast (byte*) 1024
|
||||
Simplifying constant pointer cast (byte*) 1064
|
||||
Simplifying constant pointer cast (byte*) 1064
|
||||
Simplifying constant integer cast $17
|
||||
Simplifying constant pointer cast (byte*) 53272
|
||||
Simplifying constant integer cast 0
|
||||
Simplifying constant integer cast 0
|
||||
Successful SSA optimization PassNCastSimplification
|
||||
Finalized unsigned number type (byte) $17
|
||||
Finalized unsigned number type (byte) 0
|
||||
Finalized unsigned number type (byte) 0
|
||||
Successful SSA optimization PassNFinalizeNumberTypeConversions
|
||||
Alias main::i#2 = main::i#3
|
||||
Successful SSA optimization Pass2AliasElimination
|
||||
Simple Condition (bool~) main::$0 [4] if((byte) 0!=*((const byte*) MSG1 + (byte) main::i#2)) goto main::@2
|
||||
Successful SSA optimization Pass2ConditionalJumpSimplification
|
||||
Constant (const byte) main::i#0 = 0
|
||||
Successful SSA optimization Pass2ConstantIdentification
|
||||
Simplifying expression containing zero SCREEN3 in [8] *((const byte*) SCREEN3 + (byte) 0) ← (const byte) CH
|
||||
Successful SSA optimization PassNSimplifyExpressionWithZero
|
||||
Inlining constant with var siblings (const byte) main::i#0
|
||||
Constant inlined main::i#0 = (byte) 0
|
||||
Successful SSA optimization Pass2ConstantInlining
|
||||
Adding NOP phi() at start of @begin
|
||||
Adding NOP phi() at start of @1
|
||||
Adding NOP phi() at start of @2
|
||||
Adding NOP phi() at start of @end
|
||||
CALL GRAPH
|
||||
Calls in [] to main:2
|
||||
|
||||
Created 1 initial phi equivalence classes
|
||||
Coalesced [13] main::i#4 ← main::i#1
|
||||
Coalesced down to 1 phi equivalence classes
|
||||
Culled Empty Block (label) @2
|
||||
Adding NOP phi() at start of @begin
|
||||
Adding NOP phi() at start of @1
|
||||
Adding NOP phi() at start of @end
|
||||
|
||||
FINAL CONTROL FLOW GRAPH
|
||||
@begin: scope:[] from
|
||||
[0] phi()
|
||||
to:@1
|
||||
@1: scope:[] from @begin
|
||||
[1] phi()
|
||||
[2] call main
|
||||
to:@end
|
||||
@end: scope:[] from @1
|
||||
[3] phi()
|
||||
|
||||
(void()) main()
|
||||
main: scope:[main] from @1
|
||||
[4] *((byte*) 53272) ← (byte) $17
|
||||
to:main::@1
|
||||
main::@1: scope:[main] from main main::@2
|
||||
[5] (byte) main::i#2 ← phi( main/(byte) 0 main::@2/(byte) main::i#1 )
|
||||
[6] if((byte) 0!=*((const byte*) MSG1 + (byte) main::i#2)) goto main::@2
|
||||
to:main::@3
|
||||
main::@3: scope:[main] from main::@1
|
||||
[7] *((const byte*) SCREEN3) ← (const byte) CH
|
||||
to:main::@return
|
||||
main::@return: scope:[main] from main::@3
|
||||
[8] return
|
||||
to:@return
|
||||
main::@2: scope:[main] from main::@1
|
||||
[9] *((const byte*) SCREEN1 + (byte) main::i#2) ← *((const byte*) MSG1 + (byte) main::i#2)
|
||||
[10] *((const byte*) SCREEN2 + (byte) main::i#2) ← *((const byte*) MSG2 + (byte) main::i#2)
|
||||
[11] (byte) main::i#1 ← ++ (byte) main::i#2
|
||||
to:main::@1
|
||||
|
||||
|
||||
VARIABLE REGISTER WEIGHTS
|
||||
(void()) main()
|
||||
(byte) main::i
|
||||
(byte) main::i#1 202.0
|
||||
(byte) main::i#2 176.75
|
||||
|
||||
Initial phi equivalence classes
|
||||
[ main::i#2 main::i#1 ]
|
||||
Complete equivalence classes
|
||||
[ main::i#2 main::i#1 ]
|
||||
Allocated zp[1]:2 [ main::i#2 main::i#1 ]
|
||||
|
||||
INITIAL ASM
|
||||
Target platform is c64basic / MOS6502X
|
||||
// File Comments
|
||||
// Test using some simple supported string escape
|
||||
// Uses \xnn to add chars by hex-code
|
||||
// Upstart
|
||||
.pc = $801 "Basic"
|
||||
:BasicUpstart(__bbegin)
|
||||
.pc = $80d "Program"
|
||||
// Global Constants & labels
|
||||
.encoding "screencode_upper"
|
||||
.const CH = 'P'
|
||||
.label SCREEN1 = $400
|
||||
.label SCREEN2 = $428
|
||||
.label SCREEN3 = $428
|
||||
// @begin
|
||||
__bbegin:
|
||||
// [1] phi from @begin to @1 [phi:@begin->@1]
|
||||
__b1_from___bbegin:
|
||||
jmp __b1
|
||||
// @1
|
||||
__b1:
|
||||
// [2] call main
|
||||
jsr main
|
||||
// [3] phi from @1 to @end [phi:@1->@end]
|
||||
__bend_from___b1:
|
||||
jmp __bend
|
||||
// @end
|
||||
__bend:
|
||||
// main
|
||||
main: {
|
||||
.label i = 2
|
||||
// [4] *((byte*) 53272) ← (byte) $17 -- _deref_pbuc1=vbuc2
|
||||
// Show mixed chars on screen
|
||||
lda #$17
|
||||
sta $d018
|
||||
// [5] phi from main to main::@1 [phi:main->main::@1]
|
||||
__b1_from_main:
|
||||
// [5] phi (byte) main::i#2 = (byte) 0 [phi:main->main::@1#0] -- vbuz1=vbuc1
|
||||
lda #0
|
||||
sta.z i
|
||||
jmp __b1
|
||||
// main::@1
|
||||
__b1:
|
||||
// [6] if((byte) 0!=*((const byte*) MSG1 + (byte) main::i#2)) goto main::@2 -- vbuc1_neq_pbuc2_derefidx_vbuz1_then_la1
|
||||
lda #0
|
||||
ldy.z i
|
||||
cmp MSG1,y
|
||||
bne __b2
|
||||
jmp __b3
|
||||
// main::@3
|
||||
__b3:
|
||||
// [7] *((const byte*) SCREEN3) ← (const byte) CH -- _deref_pbuc1=vbuc2
|
||||
lda #CH
|
||||
sta SCREEN3
|
||||
jmp __breturn
|
||||
// main::@return
|
||||
__breturn:
|
||||
// [8] return
|
||||
rts
|
||||
// main::@2
|
||||
__b2:
|
||||
// [9] *((const byte*) SCREEN1 + (byte) main::i#2) ← *((const byte*) MSG1 + (byte) main::i#2) -- pbuc1_derefidx_vbuz1=pbuc2_derefidx_vbuz1
|
||||
ldy.z i
|
||||
lda MSG1,y
|
||||
sta SCREEN1,y
|
||||
// [10] *((const byte*) SCREEN2 + (byte) main::i#2) ← *((const byte*) MSG2 + (byte) main::i#2) -- pbuc1_derefidx_vbuz1=pbuc2_derefidx_vbuz1
|
||||
ldy.z i
|
||||
lda MSG2,y
|
||||
sta SCREEN2,y
|
||||
// [11] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuz1=_inc_vbuz1
|
||||
inc.z i
|
||||
// [5] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
|
||||
__b1_from___b2:
|
||||
// [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@2->main::@1#0] -- register_copy
|
||||
jmp __b1
|
||||
}
|
||||
// File Data
|
||||
.encoding "petscii_mixed"
|
||||
MSG1: .text "cAmElot"
|
||||
.byte 0
|
||||
.encoding "screencode_upper"
|
||||
MSG2: .text "CAMELOT"
|
||||
.byte 0
|
||||
|
||||
REGISTER UPLIFT POTENTIAL REGISTERS
|
||||
Statement [4] *((byte*) 53272) ← (byte) $17 [ ] ( main:2 [ ] { } ) always clobbers reg byte a
|
||||
Statement [6] if((byte) 0!=*((const byte*) MSG1 + (byte) main::i#2)) goto main::@2 [ main::i#2 ] ( main:2 [ main::i#2 ] { } ) always clobbers reg byte a
|
||||
Removing always clobbered register reg byte a as potential for zp[1]:2 [ main::i#2 main::i#1 ]
|
||||
Statement [7] *((const byte*) SCREEN3) ← (const byte) CH [ ] ( main:2 [ ] { } ) always clobbers reg byte a
|
||||
Statement [9] *((const byte*) SCREEN1 + (byte) main::i#2) ← *((const byte*) MSG1 + (byte) main::i#2) [ main::i#2 ] ( main:2 [ main::i#2 ] { } ) always clobbers reg byte a
|
||||
Statement [10] *((const byte*) SCREEN2 + (byte) main::i#2) ← *((const byte*) MSG2 + (byte) main::i#2) [ main::i#2 ] ( main:2 [ main::i#2 ] { } ) always clobbers reg byte a
|
||||
Statement [4] *((byte*) 53272) ← (byte) $17 [ ] ( main:2 [ ] { } ) always clobbers reg byte a
|
||||
Statement [6] if((byte) 0!=*((const byte*) MSG1 + (byte) main::i#2)) goto main::@2 [ main::i#2 ] ( main:2 [ main::i#2 ] { } ) always clobbers reg byte a
|
||||
Statement [7] *((const byte*) SCREEN3) ← (const byte) CH [ ] ( main:2 [ ] { } ) always clobbers reg byte a
|
||||
Statement [9] *((const byte*) SCREEN1 + (byte) main::i#2) ← *((const byte*) MSG1 + (byte) main::i#2) [ main::i#2 ] ( main:2 [ main::i#2 ] { } ) always clobbers reg byte a
|
||||
Statement [10] *((const byte*) SCREEN2 + (byte) main::i#2) ← *((const byte*) MSG2 + (byte) main::i#2) [ main::i#2 ] ( main:2 [ main::i#2 ] { } ) always clobbers reg byte a
|
||||
Potential registers zp[1]:2 [ main::i#2 main::i#1 ] : zp[1]:2 , reg byte x , reg byte y ,
|
||||
|
||||
REGISTER UPLIFT SCOPES
|
||||
Uplift Scope [main] 378.75: zp[1]:2 [ main::i#2 main::i#1 ]
|
||||
Uplift Scope []
|
||||
|
||||
Uplifting [main] best 443 combination reg byte x [ main::i#2 main::i#1 ]
|
||||
Uplifting [] best 443 combination
|
||||
|
||||
ASSEMBLER BEFORE OPTIMIZATION
|
||||
// File Comments
|
||||
// Test using some simple supported string escape
|
||||
// Uses \xnn to add chars by hex-code
|
||||
// Upstart
|
||||
.pc = $801 "Basic"
|
||||
:BasicUpstart(__bbegin)
|
||||
.pc = $80d "Program"
|
||||
// Global Constants & labels
|
||||
.encoding "screencode_upper"
|
||||
.const CH = 'P'
|
||||
.label SCREEN1 = $400
|
||||
.label SCREEN2 = $428
|
||||
.label SCREEN3 = $428
|
||||
// @begin
|
||||
__bbegin:
|
||||
// [1] phi from @begin to @1 [phi:@begin->@1]
|
||||
__b1_from___bbegin:
|
||||
jmp __b1
|
||||
// @1
|
||||
__b1:
|
||||
// [2] call main
|
||||
jsr main
|
||||
// [3] phi from @1 to @end [phi:@1->@end]
|
||||
__bend_from___b1:
|
||||
jmp __bend
|
||||
// @end
|
||||
__bend:
|
||||
// main
|
||||
main: {
|
||||
// [4] *((byte*) 53272) ← (byte) $17 -- _deref_pbuc1=vbuc2
|
||||
// Show mixed chars on screen
|
||||
lda #$17
|
||||
sta $d018
|
||||
// [5] phi from main to main::@1 [phi:main->main::@1]
|
||||
__b1_from_main:
|
||||
// [5] phi (byte) main::i#2 = (byte) 0 [phi:main->main::@1#0] -- vbuxx=vbuc1
|
||||
ldx #0
|
||||
jmp __b1
|
||||
// main::@1
|
||||
__b1:
|
||||
// [6] if((byte) 0!=*((const byte*) MSG1 + (byte) main::i#2)) goto main::@2 -- vbuc1_neq_pbuc2_derefidx_vbuxx_then_la1
|
||||
lda MSG1,x
|
||||
cmp #0
|
||||
bne __b2
|
||||
jmp __b3
|
||||
// main::@3
|
||||
__b3:
|
||||
// [7] *((const byte*) SCREEN3) ← (const byte) CH -- _deref_pbuc1=vbuc2
|
||||
lda #CH
|
||||
sta SCREEN3
|
||||
jmp __breturn
|
||||
// main::@return
|
||||
__breturn:
|
||||
// [8] return
|
||||
rts
|
||||
// main::@2
|
||||
__b2:
|
||||
// [9] *((const byte*) SCREEN1 + (byte) main::i#2) ← *((const byte*) MSG1 + (byte) main::i#2) -- pbuc1_derefidx_vbuxx=pbuc2_derefidx_vbuxx
|
||||
lda MSG1,x
|
||||
sta SCREEN1,x
|
||||
// [10] *((const byte*) SCREEN2 + (byte) main::i#2) ← *((const byte*) MSG2 + (byte) main::i#2) -- pbuc1_derefidx_vbuxx=pbuc2_derefidx_vbuxx
|
||||
lda MSG2,x
|
||||
sta SCREEN2,x
|
||||
// [11] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuxx=_inc_vbuxx
|
||||
inx
|
||||
// [5] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
|
||||
__b1_from___b2:
|
||||
// [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@2->main::@1#0] -- register_copy
|
||||
jmp __b1
|
||||
}
|
||||
// File Data
|
||||
.encoding "petscii_mixed"
|
||||
MSG1: .text "cAmElot"
|
||||
.byte 0
|
||||
.encoding "screencode_upper"
|
||||
MSG2: .text "CAMELOT"
|
||||
.byte 0
|
||||
|
||||
ASSEMBLER OPTIMIZATIONS
|
||||
Removing instruction jmp __b1
|
||||
Removing instruction jmp __bend
|
||||
Removing instruction jmp __b1
|
||||
Removing instruction jmp __b3
|
||||
Removing instruction jmp __breturn
|
||||
Succesful ASM optimization Pass5NextJumpElimination
|
||||
Removing instruction __b1_from___bbegin:
|
||||
Removing instruction __b1:
|
||||
Removing instruction __bend_from___b1:
|
||||
Succesful ASM optimization Pass5RedundantLabelElimination
|
||||
Removing instruction __bend:
|
||||
Removing instruction __b1_from_main:
|
||||
Removing instruction __b3:
|
||||
Removing instruction __breturn:
|
||||
Removing instruction __b1_from___b2:
|
||||
Succesful ASM optimization Pass5UnusedLabelElimination
|
||||
Updating BasicUpstart to call main directly
|
||||
Removing instruction jsr main
|
||||
Succesful ASM optimization Pass5SkipBegin
|
||||
Removing instruction __bbegin:
|
||||
Succesful ASM optimization Pass5UnusedLabelElimination
|
||||
|
||||
FINAL SYMBOL TABLE
|
||||
(label) @1
|
||||
(label) @begin
|
||||
(label) @end
|
||||
(const byte) CH = (byte) 'P'su
|
||||
(const byte*) MSG1[] = (byte*) "cAmElot"pm
|
||||
(const byte*) MSG2[] = (byte*) "CAMELOT"su
|
||||
(const byte*) SCREEN1 = (byte*) 1024
|
||||
(const byte*) SCREEN2 = (byte*) 1064
|
||||
(const byte*) SCREEN3 = (byte*) 1064
|
||||
(void()) main()
|
||||
(label) main::@1
|
||||
(label) main::@2
|
||||
(label) main::@3
|
||||
(label) main::@return
|
||||
(byte) main::i
|
||||
(byte) main::i#1 reg byte x 202.0
|
||||
(byte) main::i#2 reg byte x 176.75
|
||||
|
||||
reg byte x [ main::i#2 main::i#1 ]
|
||||
|
||||
|
||||
FINAL ASSEMBLER
|
||||
Score: 368
|
||||
|
||||
// File Comments
|
||||
// Test using some simple supported string escape
|
||||
// Uses \xnn to add chars by hex-code
|
||||
// Upstart
|
||||
.pc = $801 "Basic"
|
||||
:BasicUpstart(main)
|
||||
.pc = $80d "Program"
|
||||
// Global Constants & labels
|
||||
.encoding "screencode_upper"
|
||||
.const CH = 'P'
|
||||
.label SCREEN1 = $400
|
||||
.label SCREEN2 = $428
|
||||
.label SCREEN3 = $428
|
||||
// @begin
|
||||
// [1] phi from @begin to @1 [phi:@begin->@1]
|
||||
// @1
|
||||
// [2] call main
|
||||
// [3] phi from @1 to @end [phi:@1->@end]
|
||||
// @end
|
||||
// main
|
||||
main: {
|
||||
// *((char*)0xd018) = 0x17
|
||||
// [4] *((byte*) 53272) ← (byte) $17 -- _deref_pbuc1=vbuc2
|
||||
// Show mixed chars on screen
|
||||
lda #$17
|
||||
sta $d018
|
||||
// [5] phi from main to main::@1 [phi:main->main::@1]
|
||||
// [5] phi (byte) main::i#2 = (byte) 0 [phi:main->main::@1#0] -- vbuxx=vbuc1
|
||||
ldx #0
|
||||
// main::@1
|
||||
__b1:
|
||||
// while(MSG1[i])
|
||||
// [6] if((byte) 0!=*((const byte*) MSG1 + (byte) main::i#2)) goto main::@2 -- vbuc1_neq_pbuc2_derefidx_vbuxx_then_la1
|
||||
lda MSG1,x
|
||||
cmp #0
|
||||
bne __b2
|
||||
// main::@3
|
||||
// SCREEN3[0] = CH
|
||||
// [7] *((const byte*) SCREEN3) ← (const byte) CH -- _deref_pbuc1=vbuc2
|
||||
lda #CH
|
||||
sta SCREEN3
|
||||
// main::@return
|
||||
// }
|
||||
// [8] return
|
||||
rts
|
||||
// main::@2
|
||||
__b2:
|
||||
// SCREEN1[i] = MSG1[i]
|
||||
// [9] *((const byte*) SCREEN1 + (byte) main::i#2) ← *((const byte*) MSG1 + (byte) main::i#2) -- pbuc1_derefidx_vbuxx=pbuc2_derefidx_vbuxx
|
||||
lda MSG1,x
|
||||
sta SCREEN1,x
|
||||
// SCREEN2[i] = MSG2[i]
|
||||
// [10] *((const byte*) SCREEN2 + (byte) main::i#2) ← *((const byte*) MSG2 + (byte) main::i#2) -- pbuc1_derefidx_vbuxx=pbuc2_derefidx_vbuxx
|
||||
lda MSG2,x
|
||||
sta SCREEN2,x
|
||||
// i++;
|
||||
// [11] (byte) main::i#1 ← ++ (byte) main::i#2 -- vbuxx=_inc_vbuxx
|
||||
inx
|
||||
// [5] phi from main::@2 to main::@1 [phi:main::@2->main::@1]
|
||||
// [5] phi (byte) main::i#2 = (byte) main::i#1 [phi:main::@2->main::@1#0] -- register_copy
|
||||
jmp __b1
|
||||
}
|
||||
// File Data
|
||||
.encoding "petscii_mixed"
|
||||
MSG1: .text "cAmElot"
|
||||
.byte 0
|
||||
.encoding "screencode_upper"
|
||||
MSG2: .text "CAMELOT"
|
||||
.byte 0
|
||||
|
19
src/test/ref/string-escapes-4.sym
Normal file
19
src/test/ref/string-escapes-4.sym
Normal file
@ -0,0 +1,19 @@
|
||||
(label) @1
|
||||
(label) @begin
|
||||
(label) @end
|
||||
(const byte) CH = (byte) 'P'su
|
||||
(const byte*) MSG1[] = (byte*) "cAmElot"pm
|
||||
(const byte*) MSG2[] = (byte*) "CAMELOT"su
|
||||
(const byte*) SCREEN1 = (byte*) 1024
|
||||
(const byte*) SCREEN2 = (byte*) 1064
|
||||
(const byte*) SCREEN3 = (byte*) 1064
|
||||
(void()) main()
|
||||
(label) main::@1
|
||||
(label) main::@2
|
||||
(label) main::@3
|
||||
(label) main::@return
|
||||
(byte) main::i
|
||||
(byte) main::i#1 reg byte x 202.0
|
||||
(byte) main::i#2 reg byte x 176.75
|
||||
|
||||
reg byte x [ main::i#2 main::i#1 ]
|
Loading…
x
Reference in New Issue
Block a user