package org.graalvm.compiler.code;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.graalvm.compiler.code.CompilationResult.CodeAnnotation;
import org.graalvm.compiler.code.CompilationResult.CodeComment;
import org.graalvm.compiler.code.CompilationResult.JumpTable;
import jdk.vm.ci.code.CodeUtil;
public class HexCodeFile {
public static final String NEW_LINE = CodeUtil.NEW_LINE;
public static final String SECTION_DELIM = " <||@";
public static final String COLUMN_END = " <|@";
public static final Pattern SECTION = Pattern.compile("(\\S+)\\s+(.*)", Pattern.DOTALL);
public static final Pattern COMMENT = Pattern.compile("(\\d+)\\s+(.*)", Pattern.DOTALL);
public static final Pattern OPERAND_COMMENT = COMMENT;
public static final Pattern JUMP_TABLE = Pattern.compile("(\\d+)\\s+(\\d+)\\s+(-{0,1}\\d+)\\s+(-{0,1}\\d+)\\s*");
public static final Pattern LOOKUP_TABLE = Pattern.compile("(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s*");
public static final Pattern HEX_CODE = Pattern.compile("(\\p{XDigit}+)(?:\\s+(\\p{XDigit}*))?");
public static final Pattern PLATFORM = Pattern.compile("(\\S+)\\s+(\\S+)", Pattern.DOTALL);
public static final String EMBEDDED_HCF_OPEN = "<<<HexCodeFile";
public static final String EMBEDDED_HCF_CLOSE = "HexCodeFile>>>";
public final Map<Integer, List<String>> comments = new TreeMap<>();
public final Map<Integer, List<String>> operandComments = new TreeMap<>();
public final byte[] code;
public final ArrayList<JumpTable> jumpTables = new ArrayList<>();
public final String isa;
public final int wordWidth;
public final long startAddress;
public HexCodeFile(byte[] code, long startAddress, String isa, int wordWidth) {
this.code = code;
this.startAddress = startAddress;
this.isa = isa;
this.wordWidth = wordWidth;
}
public static HexCodeFile parse(String input, int sourceOffset, String source, String sourceName) {
return new Parser(input, sourceOffset, source, sourceName).hcf;
}
@Override
public String toString() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
writeTo(baos);
return baos.toString();
}
public String toEmbeddedString() {
return EMBEDDED_HCF_OPEN + NEW_LINE + toString() + EMBEDDED_HCF_CLOSE;
}
public void writeTo(OutputStream out) {
PrintStream ps = out instanceof PrintStream ? (PrintStream) out : new PrintStream(out);
ps.printf("Platform %s %d %s%n", isa, wordWidth, SECTION_DELIM);
ps.printf("HexCode %x %s %s%n", startAddress, HexCodeFile.hexCodeString(code), SECTION_DELIM);
for (JumpTable table : jumpTables) {
ps.printf("JumpTable %d %d %d %d %s%n", table.position, table.entrySize, table.low, table.high, SECTION_DELIM);
}
for (Map.Entry<Integer, List<String>> e : comments.entrySet()) {
int pos = e.getKey();
for (String comment : e.getValue()) {
ps.printf("Comment %d %s %s%n", pos, comment, SECTION_DELIM);
}
}
for (Map.Entry<Integer, List<String>> e : operandComments.entrySet()) {
for (String c : e.getValue()) {
ps.printf("OperandComment %d %s %s%n", e.getKey(), c, SECTION_DELIM);
}
}
ps.flush();
}
public static String hexCodeString(byte[] code) {
if (code == null) {
return "";
} else {
StringBuilder sb = new StringBuilder(code.length * 2);
for (int b : code) {
String hex = Integer.toHexString(b & 0xff);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
}
public void addComment(int pos, String comment) {
List<String> list = comments.get(pos);
if (list == null) {
list = new ArrayList<>();
comments.put(pos, list);
}
list.add(encodeString(comment));
}
public void addOperandComment(int pos, String comment) {
List<String> list = comments.get(pos);
if (list == null) {
list = new ArrayList<>(1);
comments.put(pos, list);
}
list.add(encodeString(comment));
}
public static void addAnnotations(HexCodeFile hcf, List<CodeAnnotation> annotations) {
if (annotations == null || annotations.isEmpty()) {
return;
}
for (CodeAnnotation a : annotations) {
if (a instanceof JumpTable) {
JumpTable table = (JumpTable) a;
hcf.jumpTables.add(table);
} else if (a instanceof CodeComment) {
CodeComment comment = (CodeComment) a;
hcf.addComment(comment.position, comment.value);
}
}
}
public static String encodeString(String input) {
int index;
String s = input;
while ((index = s.indexOf(SECTION_DELIM)) != -1) {
s = s.substring(0, index) + " < |@" + s.substring(index + SECTION_DELIM.length());
}
while ((index = s.indexOf(COLUMN_END)) != -1) {
s = s.substring(0, index) + " < @" + s.substring(index + COLUMN_END.length());
}
return s;
}
static class Parser {
final String input;
final String inputSource;
String isa;
int wordWidth;
byte[] code;
long startAddress;
HexCodeFile hcf;
Parser(String input, int sourceOffset, String source, String sourceName) {
this.input = input;
this.inputSource = sourceName;
parseSections(sourceOffset, source);
}
void makeHCF() {
if (hcf == null) {
if (isa != null && wordWidth != 0 && code != null) {
hcf = new HexCodeFile(code, startAddress, isa, wordWidth);
}
}
}
void checkHCF(String section, int offset) {
check(hcf != null, offset, section + " section must be after Platform and HexCode section");
}
void check(boolean condition, int offset, String message) {
if (!condition) {
error(offset, message);
}
}
Error error(int offset, String message) {
throw new Error(errorMessage(offset, message));
}
void warning(int offset, String message) {
PrintStream err = System.err;
err.println("Warning: " + errorMessage(offset, message));
}
String errorMessage(int offset, String message) {
assert offset < input.length();
InputPos inputPos = filePos(offset);
int lineEnd = input.indexOf(HexCodeFile.NEW_LINE, offset);
int lineStart = offset - inputPos.col;
String line = lineEnd == -1 ? input.substring(lineStart) : input.substring(lineStart, lineEnd);
return String.format("%s:%d: %s%n%s%n%" + (inputPos.col + 1) + "s", inputSource, inputPos.line, message, line, "^");
}
static class InputPos {
final int line;
final int col;
InputPos(int line, int col) {
this.line = line;
this.col = col;
}
}
InputPos filePos(int index) {
assert input != null;
int lineStart = input.lastIndexOf(HexCodeFile.NEW_LINE, index) + 1;
String l = input.substring(lineStart, lineStart + 10);
PrintStream out = System.out;
out.println("YYY" + input.substring(index, index + 10) + "...");
out.println("XXX" + l + "...");
int pos = input.indexOf(HexCodeFile.NEW_LINE, 0);
int line = 1;
while (pos > 0 && pos < index) {
line++;
pos = input.indexOf(HexCodeFile.NEW_LINE, pos + 1);
}
return new InputPos(line, index - lineStart);
}
void parseSections(int offset, String source) {
assert input.startsWith(source, offset);
int index = 0;
int endIndex = source.indexOf(SECTION_DELIM);
while (endIndex != -1) {
while (source.charAt(index) <= ' ') {
index++;
}
String section = source.substring(index, endIndex).trim();
parseSection(offset + index, section);
index = endIndex + SECTION_DELIM.length();
endIndex = source.indexOf(SECTION_DELIM, index);
}
}
int parseInt(int offset, String value) {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
throw error(offset, "Not a valid integer: " + value);
}
}
void parseSection(int offset, String section) {
if (section.isEmpty()) {
return;
}
assert input.startsWith(section, offset);
Matcher m = HexCodeFile.SECTION.matcher(section);
check(m.matches(), offset, "Section does not match pattern " + HexCodeFile.SECTION);
String header = m.group(1);
String body = m.group(2);
int headerOffset = offset + m.start(1);
int bodyOffset = offset + m.start(2);
if (header.equals("Platform")) {
check(isa == null, bodyOffset, "Duplicate Platform section found");
m = HexCodeFile.PLATFORM.matcher(body);
check(m.matches(), bodyOffset, "Platform does not match pattern " + HexCodeFile.PLATFORM);
isa = m.group(1);
wordWidth = parseInt(bodyOffset + m.start(2), m.group(2));
makeHCF();
} else if (header.equals("HexCode")) {
check(code == null, bodyOffset, "Duplicate Code section found");
m = HexCodeFile.HEX_CODE.matcher(body);
check(m.matches(), bodyOffset, "Code does not match pattern " + HexCodeFile.HEX_CODE);
String hexAddress = m.group(1);
startAddress = Long.valueOf(hexAddress, 16);
String hexCode = m.group(2);
if (hexCode == null) {
code = new byte[0];
} else {
check((hexCode.length() % 2) == 0, bodyOffset, "Hex code length must be even");
code = new byte[hexCode.length() / 2];
for (int i = 0; i < code.length; i++) {
String hexByte = hexCode.substring(i * 2, (i + 1) * 2);
code[i] = (byte) Integer.parseInt(hexByte, 16);
}