Skip to content

Commit

Permalink
Merge remote-tracking branch
Browse files Browse the repository at this point in the history
'origin/GP-4712_emteere_PR-6650_sad-dev_ParallelPerf' (Closes #6650,
Closes #6649, #2791)
  • Loading branch information
ryanmkurtz committed Aug 8, 2024
2 parents 87e259e + 4ff585d commit e5df54d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* ###
* IP: GHIDRA
* REVIEWED: YES
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,19 +15,19 @@
*/
package ghidra.app.plugin.processors.sleigh;

import ghidra.program.model.address.Address;
import ghidra.program.model.lang.*;

import java.math.BigInteger;
import java.util.Arrays;

import generic.stl.Pair;
import ghidra.program.model.address.Address;
import ghidra.program.model.lang.*;

public class ContextCache {
private int context_size = 0;
private Register contextBaseRegister = null;

private BigInteger lastContextValue;
private int[] lastContextWords;

Pair <BigInteger, int []> lastValue = null;

public ContextCache() {
}

Expand Down Expand Up @@ -57,12 +56,15 @@ public void getContext(ProcessorContextView ctx, int[] buf) {
}
}

private synchronized int[] getWords(BigInteger value) {
if (value.equals(lastContextValue)) {
return lastContextWords;
}
private int[] getWords(BigInteger value) {

Pair <BigInteger, int []> lastValueTmp = lastValue;
if (lastValueTmp != null && value.equals(lastValueTmp.first)) {
return lastValueTmp.second;
}

int[] words = new int[context_size];

byte[] bytes = value.toByteArray();
int byteIndexDiff = context_size * 4 - bytes.length;
for (int i = 0; i < context_size; i++) {
Expand All @@ -73,8 +75,10 @@ private synchronized int[] getWords(BigInteger value) {
}
words[i] = word;
}
lastContextValue = value;
lastContextWords = words;

lastValueTmp = new Pair<BigInteger, int[]>(value, words);
lastValue = lastValueTmp;

return words;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -88,7 +89,7 @@ public class SleighLanguage implements Language {
/**
* Cached instruction prototypes
*/
private LinkedHashMap<Integer, SleighInstructionPrototype> instructProtoMap;
private ConcurrentHashMap<Integer, SleighInstructionPrototype> instructProtoMap;
private DecisionNode root = null;
/**
* table of AddressSpaces
Expand Down Expand Up @@ -148,7 +149,7 @@ private void initialize(SleighLanguageDescription langDescription)
buildVolatileSymbolAddresses();
xrefRegisters();

instructProtoMap = new LinkedHashMap<>();
instructProtoMap = new ConcurrentHashMap<>();

initParallelHelper();
}
Expand Down Expand Up @@ -374,20 +375,15 @@ public InstructionPrototype parse(MemBuffer buf, ProcessorContext context, boole
new SleighInstructionPrototype(this, buf, context, contextcache, inDelaySlot, null);
Integer hashcode = newProto.hashCode();

if (!instructProtoMap.containsKey(hashcode)) {
newProto.cacheInfo(buf, context, true);
}

synchronized (instructProtoMap) {
res = instructProtoMap.get(hashcode);
if (res == null) { // We have a prototype we have never seen
// before, build it fully
instructProtoMap.put(hashcode, newProto);
res = newProto;
}
if (inDelaySlot && res.hasDelaySlots()) {
throw new NestedDelaySlotException();
}
// get existing proto and use it
// if doesn't exist in map, cache info and store new proto
res = instructProtoMap.computeIfAbsent(hashcode, h -> {
newProto.cacheInfo(buf, context, true);
return newProto;
});

if (inDelaySlot && res.hasDelaySlots()) {
throw new NestedDelaySlotException();
}
}
catch (MemoryAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class HighFunction extends PcodeSyntaxTree {
private GlobalSymbolMap globalSymbols;
private List<JumpTable> jumpTables;
private List<DataTypeSymbol> protoOverrides;
private Address entryPoint;

/**
* @param function function associated with the higher level function abstraction.
Expand All @@ -64,6 +65,7 @@ public HighFunction(Function function, Language language, CompilerSpec compilerS
this.language = language;
this.compilerSpec = compilerSpec;
AddressSpace stackSpace = function.getProgram().getAddressFactory().getStackSpace();
entryPoint = function.getEntryPoint();
localSymbols = new LocalSymbolMap(this, stackSpace);
globalSymbols = new GlobalSymbolMap(this);
proto = new FunctionPrototype(localSymbols, function);
Expand All @@ -87,7 +89,7 @@ public long getID() {
if (func instanceof FunctionDB) {
return func.getSymbol().getID();
}
return func.getProgram().getSymbolTable().getDynamicSymbolID(func.getEntryPoint());
return func.getProgram().getSymbolTable().getDynamicSymbolID(entryPoint);
}

/**
Expand Down Expand Up @@ -255,7 +257,7 @@ public void decode(Decoder decoder) throws DecoderException {
}
if (subel == ELEM_ADDR.id()) {
Address addr = AddressXML.decode(decoder);
if (!func.getEntryPoint().equals(addr)) {
if (!entryPoint.equals(addr)) {
throw new DecoderException("Mismatched address in function tag");
}
}
Expand Down Expand Up @@ -300,7 +302,7 @@ else if (subel == ELEM_SCOPE.id()) {
private void decodeJumpTableList(Decoder decoder) throws DecoderException {
int el = decoder.openElement(ELEM_JUMPTABLELIST);
while (decoder.peekElement() != 0) {
JumpTable table = new JumpTable(func.getEntryPoint().getAddressSpace());
JumpTable table = new JumpTable(entryPoint.getAddressSpace());
table.decode(decoder);
if (!table.isEmpty()) {
if (jumpTables == null) {
Expand All @@ -318,10 +320,10 @@ protected Address getPCAddress(Varnode rep) {
pcaddr = rep.getPCAddress();
if (pcaddr == Address.NO_ADDRESS) {
try {
pcaddr = func.getEntryPoint().add(-1);
pcaddr = entryPoint.add(-1);
}
catch (AddressOutOfBoundsException e) {
pcaddr = func.getEntryPoint();
pcaddr = entryPoint;
}
}
}
Expand Down Expand Up @@ -446,7 +448,7 @@ public void encode(Encoder encoder, long id, Namespace namespace, Address entryP
encoder.writeBool(ATTRIB_NORETURN, true);
}
if (entryPoint == null) {
AddressXML.encode(encoder, func.getEntryPoint());
AddressXML.encode(encoder, this.entryPoint);
}
else {
AddressXML.encode(encoder, entryPoint); // Address is forced on XML
Expand Down

0 comments on commit e5df54d

Please sign in to comment.