Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions src/main/java/com/aparapi/internal/model/Entrypoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,26 @@ public ClassModelMethod resolveAccessorCandidate(MethodCall _methodCall, MethodE
return null;
}

private void recordArrayField(Instruction arrayRef, Set<String> arrayFields) {
final FieldEntry field = getArrayFieldEntry(arrayRef);
if (field != null) {
final String arrayFieldName = field.getNameAndTypeEntry().getNameUTF8Entry().getUTF8();
arrayFields.add(arrayFieldName);
referencedFieldNames.add(arrayFieldName);
}
}

private FieldEntry getArrayFieldEntry(Instruction arrayRef) {
Instruction ref = arrayRef;
while (ref instanceof AccessArrayElement) {
ref = ((AccessArrayElement) ref).getArrayRef();
}
if (ref instanceof AccessField) {
return ((AccessField) ref).getConstantPoolFieldEntry();
}
return null;
}

/*
* Update accessor structures when there is a direct access to an
* obect array element's data members
Expand Down Expand Up @@ -552,29 +572,11 @@ public Entrypoint(ClassModel _classModel, MethodModel _methodModel, Object _k) t
if (instruction instanceof AssignToArrayElement) {
final AssignToArrayElement assignment = (AssignToArrayElement) instruction;

final Instruction arrayRef = assignment.getArrayRef();
// AccessField here allows instance and static array refs
if (arrayRef instanceof I_GETFIELD) {
final I_GETFIELD getField = (I_GETFIELD) arrayRef;
final FieldEntry field = getField.getConstantPoolFieldEntry();
final String assignedArrayFieldName = field.getNameAndTypeEntry().getNameUTF8Entry().getUTF8();
arrayFieldAssignments.add(assignedArrayFieldName);
referencedFieldNames.add(assignedArrayFieldName);

}
recordArrayField(assignment.getArrayRef(), arrayFieldAssignments);
} else if (instruction instanceof AccessArrayElement) {
final AccessArrayElement access = (AccessArrayElement) instruction;

final Instruction arrayRef = access.getArrayRef();
// AccessField here allows instance and static array refs
if (arrayRef instanceof I_GETFIELD) {
final I_GETFIELD getField = (I_GETFIELD) arrayRef;
final FieldEntry field = getField.getConstantPoolFieldEntry();
final String accessedArrayFieldName = field.getNameAndTypeEntry().getNameUTF8Entry().getUTF8();
arrayFieldAccesses.add(accessedArrayFieldName);
referencedFieldNames.add(accessedArrayFieldName);

}
recordArrayField(access.getArrayRef(), arrayFieldAccesses);
} else if (instruction instanceof I_ARRAYLENGTH) {
Instruction child = instruction.getFirstChild();
while(child instanceof I_AALOAD) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright (c) 2016 - 2018 Syncleus, Inc.
*
* 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.
*/
package com.aparapi.internal.model;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class EntrypointFieldAccessTest {

public static class StaticArrayKernel {
static final int[] source = new int[8];

static final int[] target = new int[8];

public void run() {
int id = 0;
target[id] = source[id];
}
}

public static class InstanceArrayKernel {
final int[] source = new int[8];

final int[] target = new int[8];

public void run() {
int id = 0;
target[id] = source[id];
}
}

@Test
public void recordsStaticArrayAssignmentsAndAccesses() throws Exception {
Entrypoint entrypoint = entrypointFor(StaticArrayKernel.class);

assertTrue("static array assignment should be recorded",
entrypoint.getArrayFieldAssignments().contains("target"));
assertTrue("static array access should be recorded",
entrypoint.getArrayFieldAccesses().contains("source"));
}

@Test
public void recordsInstanceArrayAssignmentsAndAccesses() throws Exception {
Entrypoint entrypoint = entrypointFor(InstanceArrayKernel.class);

assertTrue("instance array assignment should be recorded",
entrypoint.getArrayFieldAssignments().contains("target"));
assertTrue("instance array access should be recorded",
entrypoint.getArrayFieldAccesses().contains("source"));
}

private Entrypoint entrypointFor(Class<?> clazz) throws Exception {
ClassModel classModel = ClassModel.createClassModel(clazz);
return classModel.getEntrypoint("run", null);
}
}