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
31 changes: 22 additions & 9 deletions src/main/java/com/aparapi/internal/writer/BlockWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,7 @@ public void writeInstruction(Instruction _instruction) throws CodeGenException {
writeComposite((CompositeInstruction) _instruction);

} else if (_instruction instanceof I_NEWARRAY) {
if (_instruction.getParentExpr() instanceof Return) {
throw new CodeGenException("'newarray' is not allowed after 'return'");
}

for (Instruction operand = _instruction.getFirstChild(); operand != null; operand = operand.getNextExpr()) {
write("[");
writeInstruction(operand);
write("]");
}
writeNewArrayDimensions((I_NEWARRAY) _instruction);

} else if (_instruction instanceof AssignToLocalVariable) {
final AssignToLocalVariable assignToLocalVariable = (AssignToLocalVariable) _instruction;
Expand Down Expand Up @@ -632,6 +624,19 @@ public void writeInstruction(Instruction _instruction) throws CodeGenException {
} else if (_instruction instanceof Return) {

final Return ret = (Return) _instruction;
if (ret.getFirstChild() instanceof I_NEWARRAY) {
final I_NEWARRAY newArray = (I_NEWARRAY) ret.getFirstChild();
final String returnArrayName = "returnArray" + ret.getThisPC();
write(convertType(ret.getMethod().getReturnType(), true, true));
write(returnArrayName);
writeNewArrayDimensions(newArray);
write(";");
newLine();
write("return(");
write(returnArrayName);
write(")");
return;
}
write("return");
if (ret.getStackConsumeCount() > 0) {
write("(");
Expand Down Expand Up @@ -758,6 +763,14 @@ public void writeInstruction(Instruction _instruction) throws CodeGenException {

}

private void writeNewArrayDimensions(I_NEWARRAY newArray) throws CodeGenException {
for (Instruction operand = newArray.getFirstChild(); operand != null; operand = operand.getNextExpr()) {
write("[");
writeInstruction(operand);
write("]");
}
}

private boolean isNeedParenthesis(Instruction instruction){
final Instruction parent = instruction.getParentExpr();
boolean needsParenthesis = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 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.codegen.test;

final class ReturnArrayNewTestSupport {

private ReturnArrayNewTestSupport() {
}

static String[] expectedOpenCL(String openCLType, String className, String methodName) {
String qualifiedMethodName = "com_aparapi_codegen_test_" + className + "__" + methodName;
return new String[] {
"typedef struct This_s{\n"
+ " int passid;\n"
+ "}This;\n"
+ "int get_pass_id(This *this){\n"
+ " return this->passid;\n"
+ "}\n"
+ " __global " + openCLType + "* " + qualifiedMethodName + "(This *this){\n"
+ " " + openCLType + " returnArray5[1024];\n"
+ " return(returnArray5);\n"
+ "}\n"
+ "__kernel void run(\n"
+ " int passid\n"
+ "){\n"
+ " This thisStruct;\n"
+ " This* this=&thisStruct;\n"
+ " this->passid = passid;\n"
+ " {\n"
+ " " + qualifiedMethodName + "(this);\n"
+ " return;\n"
+ " }\n"
+ "}"
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,27 @@ public void run() {
returnBooleanNewArray();
}
}
/**{Throws{CodeGenException}Throws}**/
/**{OpenCL{
typedef struct This_s{
int passid;
}This;
int get_pass_id(This *this){
return this->passid;
}
__global char* com_aparapi_codegen_test_ReturnBooleanNewArray__returnBooleanNewArray(This *this){
char returnArray5[1024];
return(returnArray5);
}
__kernel void run(
int passid
){
This thisStruct;
This* this=&thisStruct;
this->passid = passid;
{
com_aparapi_codegen_test_ReturnBooleanNewArray__returnBooleanNewArray(this);
return;
}
}

}OpenCL}**/
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package com.aparapi.codegen.test;

import com.aparapi.internal.exception.CodeGenException;
import org.junit.Test;

public class ReturnBooleanNewArrayTest extends com.aparapi.codegen.CodeGenJUnitBase {

private static final String[] expectedOpenCL = null;
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = CodeGenException.class;
private static final String[] expectedOpenCL = ReturnArrayNewTestSupport.expectedOpenCL("char",
"ReturnBooleanNewArray", "returnBooleanNewArray");
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = null;

@Test
public void ReturnBooleanNewArrayTest() {
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/com/aparapi/codegen/test/ReturnByteArrayNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,27 @@ public void run() {
returnByteArrayNew();
}
}
/**{Throws{CodeGenException}Throws}**/
/**{OpenCL{
typedef struct This_s{
int passid;
}This;
int get_pass_id(This *this){
return this->passid;
}
__global char* com_aparapi_codegen_test_ReturnByteArrayNew__returnByteArrayNew(This *this){
char returnArray5[1024];
return(returnArray5);
}
__kernel void run(
int passid
){
This thisStruct;
This* this=&thisStruct;
this->passid = passid;
{
com_aparapi_codegen_test_ReturnByteArrayNew__returnByteArrayNew(this);
return;
}
}

}OpenCL}**/
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
*/
package com.aparapi.codegen.test;

import com.aparapi.internal.exception.CodeGenException;
import org.junit.Test;

public class ReturnByteArrayNewTest extends com.aparapi.codegen.CodeGenJUnitBase {
private static final String[] expectedOpenCL = null;
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = CodeGenException.class;


private static final String[] expectedOpenCL = ReturnArrayNewTestSupport.expectedOpenCL("char",
"ReturnByteArrayNew", "returnByteArrayNew");
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = null;

@Test
public void ReturnByteArrayNewTest() {
test(com.aparapi.codegen.test.ReturnByteArrayNew.class, expectedException, expectedOpenCL);
}


@Test
public void ReturnByteArrayNewTestWorksWithCaching() {
test(com.aparapi.codegen.test.ReturnByteArrayNew.class, expectedException, expectedOpenCL);
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/com/aparapi/codegen/test/ReturnDoubleArrayNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,27 @@ public void run() {
returnDoubleArrayNew();
}
}
/**{Throws{CodeGenException}Throws}**/
/**{OpenCL{
typedef struct This_s{
int passid;
}This;
int get_pass_id(This *this){
return this->passid;
}
__global double* com_aparapi_codegen_test_ReturnDoubleArrayNew__returnDoubleArrayNew(This *this){
double returnArray5[1024];
return(returnArray5);
}
__kernel void run(
int passid
){
This thisStruct;
This* this=&thisStruct;
this->passid = passid;
{
com_aparapi_codegen_test_ReturnDoubleArrayNew__returnDoubleArrayNew(this);
return;
}
}

}OpenCL}**/
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package com.aparapi.codegen.test;

import com.aparapi.internal.exception.CodeGenException;
import org.junit.Test;

public class ReturnDoubleArrayNewTest extends com.aparapi.codegen.CodeGenJUnitBase {

private static final String[] expectedOpenCL = null;
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = CodeGenException.class;
private static final String[] expectedOpenCL = ReturnArrayNewTestSupport.expectedOpenCL("double",
"ReturnDoubleArrayNew", "returnDoubleArrayNew");
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = null;

@Test
public void ReturnDoubleArrayNewTest() {
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/com/aparapi/codegen/test/ReturnFloatArrayNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,27 @@ public void run() {
returnFloatArrayNew();
}
}
/**{Throws{CodeGenException}Throws}**/
/**{OpenCL{
typedef struct This_s{
int passid;
}This;
int get_pass_id(This *this){
return this->passid;
}
__global float* com_aparapi_codegen_test_ReturnFloatArrayNew__returnFloatArrayNew(This *this){
float returnArray5[1024];
return(returnArray5);
}
__kernel void run(
int passid
){
This thisStruct;
This* this=&thisStruct;
this->passid = passid;
{
com_aparapi_codegen_test_ReturnFloatArrayNew__returnFloatArrayNew(this);
return;
}
}

}OpenCL}**/
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package com.aparapi.codegen.test;

import com.aparapi.internal.exception.CodeGenException;
import org.junit.Test;

public class ReturnFloatArrayNewTest extends com.aparapi.codegen.CodeGenJUnitBase {

private static final String[] expectedOpenCL = null;
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = CodeGenException.class;
private static final String[] expectedOpenCL = ReturnArrayNewTestSupport.expectedOpenCL("float",
"ReturnFloatArrayNew", "returnFloatArrayNew");
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = null;

@Test
public void ReturnFloatArrayNewTest() {
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/com/aparapi/codegen/test/ReturnIntArrayNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,27 @@ public void run() {
returnIntArrayNew();
}
}
/**{Throws{CodeGenException}Throws}**/
/**{OpenCL{
typedef struct This_s{
int passid;
}This;
int get_pass_id(This *this){
return this->passid;
}
__global int* com_aparapi_codegen_test_ReturnIntArrayNew__returnIntArrayNew(This *this){
int returnArray5[1024];
return(returnArray5);
}
__kernel void run(
int passid
){
This thisStruct;
This* this=&thisStruct;
this->passid = passid;
{
com_aparapi_codegen_test_ReturnIntArrayNew__returnIntArrayNew(this);
return;
}
}

}OpenCL}**/
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package com.aparapi.codegen.test;

import com.aparapi.internal.exception.CodeGenException;
import org.junit.Test;

public class ReturnIntArrayNewTest extends com.aparapi.codegen.CodeGenJUnitBase {

private static final String[] expectedOpenCL = null;
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = CodeGenException.class;
private static final String[] expectedOpenCL = ReturnArrayNewTestSupport.expectedOpenCL("int",
"ReturnIntArrayNew", "returnIntArrayNew");
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = null;

@Test
public void ReturnIntArrayNewTest() {
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/com/aparapi/codegen/test/ReturnLongArrayNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,27 @@ public void run() {
returnLongArrayNew();
}
}
/**{Throws{CodeGenException}Throws}**/
/**{OpenCL{
typedef struct This_s{
int passid;
}This;
int get_pass_id(This *this){
return this->passid;
}
__global long* com_aparapi_codegen_test_ReturnLongArrayNew__returnLongArrayNew(This *this){
long returnArray5[1024];
return(returnArray5);
}
__kernel void run(
int passid
){
This thisStruct;
This* this=&thisStruct;
this->passid = passid;
{
com_aparapi_codegen_test_ReturnLongArrayNew__returnLongArrayNew(this);
return;
}
}

}OpenCL}**/
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package com.aparapi.codegen.test;

import com.aparapi.internal.exception.CodeGenException;
import org.junit.Test;

public class ReturnLongArrayNewTest extends com.aparapi.codegen.CodeGenJUnitBase {

private static final String[] expectedOpenCL = null;
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = CodeGenException.class;
private static final String[] expectedOpenCL = ReturnArrayNewTestSupport.expectedOpenCL("long",
"ReturnLongArrayNew", "returnLongArrayNew");
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = null;

@Test
public void ReturnLongArrayNewTest() {
Expand Down
Loading