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
50 changes: 45 additions & 5 deletions src/main/java/com/aparapi/internal/writer/BlockWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,37 @@ public String convertCast(String _cast) {
return ("(" + raw + ")");
}

private String getNewArrayDescriptor(final I_NEWARRAY _newArray) throws CodeGenException {
switch (_newArray.getType()) {
case 4:
return "[Z";
case 5:
return "[C";
case 6:
return "[F";
case 7:
return "[D";
case 8:
return "[B";
case 9:
return "[S";
case 10:
return "[I";
case 11:
return "[J";
default:
throw new CodeGenException("Unsupported newarray type " + _newArray.getType());
}
}

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

public void writeInstruction(Instruction _instruction) throws CodeGenException {
if (_instruction instanceof CompositeIfElseInstruction) {
boolean needParenthesis = isNeedParenthesis(_instruction);
Expand All @@ -400,11 +431,7 @@ public void writeInstruction(Instruction _instruction) throws CodeGenException {
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 +659,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 = "__aparapi_return_array";
write(convertType(getNewArrayDescriptor(newArray), true, true));
write(returnArrayName);
writeNewArrayDimensions(newArray);
write(";");
newLine();
write("return(");
write(returnArrayName);
write(")");
return;
}
write("return");
if (ret.getStackConsumeCount() > 0) {
write("(");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,26 @@ 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 __aparapi_return_array[1024];
return(__aparapi_return_array);
}
__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,34 @@
*/
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 = {
"typedef struct This_s{\n"
+ " int passid;\n"
+ "}This;\n"
+ "int get_pass_id(This *this){\n"
+ " return this->passid;\n"
+ "}\n"
+ " __global char* com_aparapi_codegen_test_ReturnBooleanNewArray__returnBooleanNewArray(This *this){\n"
+ " char __aparapi_return_array[1024];\n"
+ " return(__aparapi_return_array);\n"
+ "}\n"
+ "__kernel void run(\n"
+ " int passid\n"
+ "){\n"
+ " This thisStruct;\n"
+ " This* this=&thisStruct;\n"
+ " this->passid = passid;\n"
+ " {\n"
+ " com_aparapi_codegen_test_ReturnBooleanNewArray__returnBooleanNewArray(this);\n"
+ " return;\n"
+ " }\n"
+ "}"
};
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = null;

@Test
public void ReturnBooleanNewArrayTest() {
Expand Down
24 changes: 23 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,26 @@ 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 __aparapi_return_array[1024];
return(__aparapi_return_array);
}
__kernel void run(
int passid
){
This thisStruct;
This* this=&thisStruct;
this->passid = passid;
{
com_aparapi_codegen_test_ReturnByteArrayNew__returnByteArrayNew(this);
return;
}
}
}OpenCL}**/
27 changes: 24 additions & 3 deletions src/test/java/com/aparapi/codegen/test/ReturnByteArrayNewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,33 @@
*/
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 = {
"typedef struct This_s{\n"
+ " int passid;\n"
+ "}This;\n"
+ "int get_pass_id(This *this){\n"
+ " return this->passid;\n"
+ "}\n"
+ " __global char* com_aparapi_codegen_test_ReturnByteArrayNew__returnByteArrayNew(This *this){\n"
+ " char __aparapi_return_array[1024];\n"
+ " return(__aparapi_return_array);\n"
+ "}\n"
+ "__kernel void run(\n"
+ " int passid\n"
+ "){\n"
+ " This thisStruct;\n"
+ " This* this=&thisStruct;\n"
+ " this->passid = passid;\n"
+ " {\n"
+ " com_aparapi_codegen_test_ReturnByteArrayNew__returnByteArrayNew(this);\n"
+ " return;\n"
+ " }\n"
+ "}"
};
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = null;


@Test
Expand Down
24 changes: 23 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,26 @@ 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 __aparapi_return_array[1024];
return(__aparapi_return_array);
}
__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,34 @@
*/
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 = {
"typedef struct This_s{\n"
+ " int passid;\n"
+ "}This;\n"
+ "int get_pass_id(This *this){\n"
+ " return this->passid;\n"
+ "}\n"
+ " __global double* com_aparapi_codegen_test_ReturnDoubleArrayNew__returnDoubleArrayNew(This *this){\n"
+ " double __aparapi_return_array[1024];\n"
+ " return(__aparapi_return_array);\n"
+ "}\n"
+ "__kernel void run(\n"
+ " int passid\n"
+ "){\n"
+ " This thisStruct;\n"
+ " This* this=&thisStruct;\n"
+ " this->passid = passid;\n"
+ " {\n"
+ " com_aparapi_codegen_test_ReturnDoubleArrayNew__returnDoubleArrayNew(this);\n"
+ " return;\n"
+ " }\n"
+ "}"
};
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = null;

@Test
public void ReturnDoubleArrayNewTest() {
Expand Down
24 changes: 23 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,26 @@ 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 __aparapi_return_array[1024];
return(__aparapi_return_array);
}
__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,34 @@
*/
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 = {
"typedef struct This_s{\n"
+ " int passid;\n"
+ "}This;\n"
+ "int get_pass_id(This *this){\n"
+ " return this->passid;\n"
+ "}\n"
+ " __global float* com_aparapi_codegen_test_ReturnFloatArrayNew__returnFloatArrayNew(This *this){\n"
+ " float __aparapi_return_array[1024];\n"
+ " return(__aparapi_return_array);\n"
+ "}\n"
+ "__kernel void run(\n"
+ " int passid\n"
+ "){\n"
+ " This thisStruct;\n"
+ " This* this=&thisStruct;\n"
+ " this->passid = passid;\n"
+ " {\n"
+ " com_aparapi_codegen_test_ReturnFloatArrayNew__returnFloatArrayNew(this);\n"
+ " return;\n"
+ " }\n"
+ "}"
};
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = null;

@Test
public void ReturnFloatArrayNewTest() {
Expand Down
24 changes: 23 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,26 @@ 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 __aparapi_return_array[1024];
return(__aparapi_return_array);
}
__kernel void run(
int passid
){
This thisStruct;
This* this=&thisStruct;
this->passid = passid;
{
com_aparapi_codegen_test_ReturnIntArrayNew__returnIntArrayNew(this);
return;
}
}
}OpenCL}**/
27 changes: 24 additions & 3 deletions src/test/java/com/aparapi/codegen/test/ReturnIntArrayNewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,34 @@
*/
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 = {
"typedef struct This_s{\n"
+ " int passid;\n"
+ "}This;\n"
+ "int get_pass_id(This *this){\n"
+ " return this->passid;\n"
+ "}\n"
+ " __global int* com_aparapi_codegen_test_ReturnIntArrayNew__returnIntArrayNew(This *this){\n"
+ " int __aparapi_return_array[1024];\n"
+ " return(__aparapi_return_array);\n"
+ "}\n"
+ "__kernel void run(\n"
+ " int passid\n"
+ "){\n"
+ " This thisStruct;\n"
+ " This* this=&thisStruct;\n"
+ " this->passid = passid;\n"
+ " {\n"
+ " com_aparapi_codegen_test_ReturnIntArrayNew__returnIntArrayNew(this);\n"
+ " return;\n"
+ " }\n"
+ "}"
};
private static final Class<? extends com.aparapi.internal.exception.AparapiException> expectedException = null;

@Test
public void ReturnIntArrayNewTest() {
Expand Down
Loading