Skip to content

Commit

Permalink
Improvements to Intel asm support
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswhocodes committed Sep 16, 2016
1 parent ebd5ab3 commit 1171c92
Show file tree
Hide file tree
Showing 8 changed files with 417 additions and 268 deletions.
Expand Up @@ -177,8 +177,6 @@ private JITWatchConstants()
public static final String S_CLASS_PREFIX_STREAM_COLLECTORS = "java.util.stream.Collectors$";
public static final String S_CLASS_PREFIX_SUN_REFLECT_GENERATED = "sun.reflect.Generated";

public static final String HEXA_POSTFIX = "h";

private static final Set<String> SET_AUTOGENERATED_PREFIXES = new HashSet<>();

static
Expand Down Expand Up @@ -207,8 +205,8 @@ public static final Set<String> getAutoGeneratedClassPrefixes()
public static final String S_ESCAPED_CLOSE_PARENTHESES = "\\)";
public static final String S_OPEN_ANGLE = "<";
public static final String S_CLOSE_ANGLE = ">";
public static final String S_OPEN_SQUARE = "[";
public static final String S_CLOSE_SQUARE = "]";
public static final String S_OPEN_SQUARE_BRACKET = "[";
public static final String S_CLOSE_SQUARE_BRACKET = "]";
public static final String S_ARRAY_BRACKET_PAIR = "[]";
public static final String S_ESCAPED_OPEN_SQUARE = "\\[";
public static final String S_ESCAPED_CLOSE_SQUARE = "\\]";
Expand Down Expand Up @@ -294,7 +292,8 @@ public static final Set<String> getAutoGeneratedClassPrefixes()
public static final char C_HAT = '^';
public static final char C_DOLLAR = '$';

public static final String S_ASSEMBLY_ADDRESS = "0x";
public static final String S_HEX_PREFIX = "0x";
public static final String S_HEX_POSTFIX = "h";

public static final String S_BYTECODE_MINOR_VERSION = "minor version:";
public static final String S_BYTECODE_MAJOR_VERSION = "major version:";
Expand Down
Expand Up @@ -20,15 +20,15 @@
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.REGEX_UNICODE_PACKAGE_NAME;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.REGEX_UNICODE_PARAM_NAME;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.REGEX_ZERO_OR_MORE_SPACES;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_CLOSE_SQUARE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_CLOSE_SQUARE_BRACKET;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_COMMA;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_DOT;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_EMPTY;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_ESCAPED_CLOSE_PARENTHESES;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_ESCAPED_CLOSE_SQUARE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_ESCAPED_OPEN_PARENTHESES;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_ESCAPED_OPEN_SQUARE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_OPEN_SQUARE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_OPEN_SQUARE_BRACKET;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_POLYMORPHIC_SIGNATURE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.ATTR_COMPILE_ID;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.ATTR_COMPILE_KIND;
Expand Down Expand Up @@ -596,7 +596,7 @@ public static String expandParamRegEx(String inParamType)
{
paramType = ParseUtil.expandParameterType(paramType);

paramType = paramType.replace(S_OPEN_SQUARE, S_ESCAPED_OPEN_SQUARE).replace(S_CLOSE_SQUARE, S_ESCAPED_CLOSE_SQUARE);
paramType = paramType.replace(S_OPEN_SQUARE_BRACKET, S_ESCAPED_OPEN_SQUARE).replace(S_CLOSE_SQUARE_BRACKET, S_ESCAPED_CLOSE_SQUARE);
}

if (paramType.contains(S_DOT))
Expand Down
Expand Up @@ -8,7 +8,7 @@
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.C_COLON;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.C_SPACE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.DEBUG_LOGGING_ASSEMBLY;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_ASSEMBLY_ADDRESS;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_HEX_PREFIX;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_COMMA;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_DOUBLE_SPACE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_NEWLINE;
Expand Down Expand Up @@ -208,7 +208,7 @@ public String toString(int annoWidth, boolean useLocalLabels)
}
else
{
builder.append(S_ASSEMBLY_ADDRESS).append(StringUtil.pad(Long.toHexString(address), 16, '0', true));
builder.append(S_HEX_PREFIX).append(StringUtil.pad(Long.toHexString(address), 16, '0', true));
}

builder.append(C_COLON).append(C_SPACE);
Expand Down Expand Up @@ -285,7 +285,7 @@ public String toString(int annoWidth, int line, boolean useLocalLabels)
}
else
{
builder.append(S_ASSEMBLY_ADDRESS).append(StringUtil.pad(Long.toHexString(address), 16, '0', true));
builder.append(S_HEX_PREFIX).append(StringUtil.pad(Long.toHexString(address), 16, '0', true));
}

builder.append(C_COLON).append(C_SPACE);
Expand Down
Expand Up @@ -7,7 +7,7 @@
package org.adoptopenjdk.jitwatch.model.assembly;

import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.C_SPACE;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_ASSEMBLY_ADDRESS;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_HEX_PREFIX;
import static org.adoptopenjdk.jitwatch.core.JITWatchConstants.S_COMMA;

import java.util.HashMap;
Expand Down Expand Up @@ -97,7 +97,7 @@ public void formatAddress(long address, StringBuilder builder)
}
else
{
builder.append(S_ASSEMBLY_ADDRESS);
builder.append(S_HEX_PREFIX);
builder.append(StringUtil.pad(Long.toHexString(address), 16, '0', true));
}
}
Expand Down
Expand Up @@ -20,9 +20,11 @@ public final class AssemblyUtil
// http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html
private static final Logger logger = LoggerFactory.getLogger(AssemblyUtil.class);

private static final String PART_ADDRESS = "(0x[a-f0-9]+):";
private static final String PART_ADDRESS = "(" + S_HEX_PREFIX + "[a-f0-9]+):";
private static final String PART_INSTRUCTION = "([0-9a-zA-Z:_\\(\\)\\[\\]\\+\\*\\$,\\-%\\s]+)";
private static final String PART_COMMENT = "([;#].*)?";

private static final Pattern ASSEMBLY_CONSTANT = Pattern.compile("^([\\$]?(" + S_HEX_PREFIX + ")?[a-f0-9]+[" + S_HEX_POSTFIX + "]?)$");

private static final Pattern PATTERN_ASSEMBLY_INSTRUCTION = Pattern
.compile("^" + PART_ADDRESS + "\\s+" + PART_INSTRUCTION + PART_COMMENT);
Expand Down Expand Up @@ -72,7 +74,7 @@ public static AssemblyMethod parseAssembly(final String assemblyString)

headerBuilder.append(line).append(S_NEWLINE);
}
else if (line.startsWith(S_OPEN_SQUARE))
else if (line.startsWith(S_OPEN_SQUARE_BRACKET))
{
if (DEBUG_LOGGING_ASSEMBLY)
{
Expand All @@ -99,8 +101,8 @@ else if (line.startsWith(S_SEMICOLON))
{
AssemblyInstruction instr = createInstruction(labels, line);

if (instr == null && lastLine.trim().startsWith(S_HASH) && !line.startsWith(S_ASSEMBLY_ADDRESS)
&& !line.contains(' ' + S_ASSEMBLY_ADDRESS))
if (instr == null && lastLine.trim().startsWith(S_HASH) && !line.startsWith(S_HEX_PREFIX)
&& !line.contains(' ' + S_HEX_PREFIX))
{
// remove last newline
headerBuilder.setLength(headerBuilder.length() - S_NEWLINE.length());
Expand Down Expand Up @@ -188,9 +190,9 @@ public static AssemblyInstruction createInstruction(final AssemblyLabels labels,

String annotation = S_EMPTY;

if (!line.startsWith(S_ASSEMBLY_ADDRESS))
if (!line.startsWith(S_HEX_PREFIX))
{
int addressIndex = line.indexOf(' ' + S_ASSEMBLY_ADDRESS);
int addressIndex = line.indexOf(' ' + S_HEX_PREFIX);

if (addressIndex != -1)
{
Expand Down Expand Up @@ -280,7 +282,7 @@ else if (c == C_CLOSE_PARENTHESES || c == C_CLOSE_SQUARE_BRACKET)

if (mnemonic == null)
{
if ("data32".equals(part))
if ("data64".equals(part) || "data32".equals(part) || "data16".equals(part) || "data8".equals(part) || "lock".equals(part))
{
prefixes.add(part);
}
Expand Down Expand Up @@ -337,20 +339,20 @@ else if (c == C_COMMA && !inBrackets)
return new AssemblyInstruction(annotation, address, prefixes, mnemonic, operands, comment, labels);
}

static long getValueFromAddress(final String address)
public static long getValueFromAddress(final String address)
{
long addressValue = 0;

if (address != null)
{
String trimmedAddress = address.trim();

if (trimmedAddress.startsWith(S_ASSEMBLY_ADDRESS))
if (trimmedAddress.startsWith(S_HEX_PREFIX))
{
trimmedAddress = trimmedAddress.substring(S_ASSEMBLY_ADDRESS.length());
trimmedAddress = trimmedAddress.substring(S_HEX_PREFIX.length());
}

if (trimmedAddress.endsWith(HEXA_POSTFIX))
if (trimmedAddress.endsWith(S_HEX_POSTFIX))
{
trimmedAddress = trimmedAddress.substring(0, trimmedAddress.length() - 1);
}
Expand All @@ -359,4 +361,82 @@ static long getValueFromAddress(final String address)
}
return addressValue;
}

public static boolean isConstant(String mnemonic, String operand)
{
return ASSEMBLY_CONSTANT.matcher(operand).find() && !isJump(mnemonic);
}

public static boolean isRegister(String mnemonic, String operand)
{
return operand.startsWith(S_PERCENT) || operand.contains("(%") || operand.contains(S_OPEN_SQUARE_BRACKET) || (!isConstant(mnemonic, operand) && !isAddress(mnemonic, operand));
}

public static boolean isAddress(String mnemonic, String operand)
{
return (operand.startsWith(S_HEX_PREFIX) || operand.endsWith(S_HEX_POSTFIX)) && isJump(mnemonic);
}

public static boolean isJump(String mnemonic)
{
boolean result = false;

if (mnemonic != null)
{
result = mnemonic.toLowerCase().startsWith("j") || mnemonic.toLowerCase().startsWith("call");
}

return result;
}

public static String extractRegisterName(final String input)
{
String regName = input;

int indexOpenParentheses = input.indexOf(C_OPEN_PARENTHESES);
int indexCloseParentheses = input.indexOf(C_CLOSE_PARENTHESES);

if (indexOpenParentheses != -1 && indexCloseParentheses != -1)
{
regName = regName.substring(indexOpenParentheses+1, indexCloseParentheses);
}

int indexOpenSquareBracket = regName.indexOf(C_OPEN_SQUARE_BRACKET);
int indexCloseSquareBracket = regName.indexOf(C_CLOSE_SQUARE_BRACKET);

if (indexOpenSquareBracket != -1 && indexCloseSquareBracket != -1)
{
regName = regName.substring(indexOpenSquareBracket+1, indexCloseSquareBracket);
}

if (regName.startsWith("*"))
{
regName = regName.substring(1);
}

if (regName.startsWith(S_PERCENT))
{
regName = regName.substring(1);
}

StringBuilder builder = new StringBuilder();

for (int i = 0; i < regName.length(); i++)
{
char c = regName.charAt(i);

if (Character.isAlphabetic(c) || Character.isDigit(c))
{
builder.append(c);
}
else
{
break;
}
}

regName = builder.toString();

return regName;
}
}

0 comments on commit 1171c92

Please sign in to comment.