1 /*
  2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 #include "shared.h"
 26 
 27 #define INFO 0
 28 
 29 /*
 30 extern void dump(FILE *file, size_t len, void *ptr) {
 31     for (int i = 0; i < len; i++) {
 32         if (i % 16 == 0) {
 33             if (i != 0) {
 34                 fprintf(file, "\n");
 35             }
 36             fprintf(file, "%lx :", ((unsigned long) ptr) + i);
 37         }
 38         fprintf(file, " %02x", ((int) (((unsigned char *) ptr)[i]) & 0xff));
 39     }
 40 }
 41 
 42 */
 43 /*extern "C" void dumpArgArray(void *ptr) {
 44     ArgSled argSled((ArgArray_t *) ptr);
 45     std::cout << "ArgArray->argc = " << argSled.argc() << std::endl;
 46     for (int i = 0; i < argSled.argc(); i++) {
 47         argSled.dumpArg(i);
 48     }
 49     std::cout << "schema = " << argSled.schema() << std::endl;
 50 }*/
 51 
 52 void hexdump(void *ptr, int buflen) {
 53     unsigned char *buf = (unsigned char *) ptr;
 54     int i, j;
 55     for (i = 0; i < buflen; i += 16) {
 56         printf("%06x: ", i);
 57         for (j = 0; j < 16; j++)
 58             if (i + j < buflen)
 59                 printf("%02x ", buf[i + j]);
 60             else
 61                 printf("   ");
 62         printf(" ");
 63         for (j = 0; j < 16; j++)
 64             if (i + j < buflen)
 65                 printf("%c", isprint(buf[i + j]) ? buf[i + j] : '.');
 66         printf("\n");
 67     }
 68 }
 69 
 70 void Sled::show(std::ostream &out, void *argArray) {
 71     ArgSled argSled(static_cast<ArgArray_s *>(argArray));
 72     for (int i = 0; i < argSled.argc(); i++) {
 73         Arg_s *arg = argSled.arg(i);
 74         switch (arg->variant) {
 75             case '&': {
 76                 out << "Buf: of " << arg->value.buffer.sizeInBytes << " bytes " << std::endl;
 77                 break;
 78             }
 79             case 'B': {
 80                 out << "S8:" << arg->value.s8 << std::endl;
 81                 break;
 82             }
 83             case 'Z': {
 84                 out << "Z:" << arg->value.z1 << std::endl;
 85                 break;
 86             }
 87             case 'C': {
 88                 out << "U16:" << arg->value.u16 << std::endl;
 89                 break;
 90             }
 91             case 'S': {
 92                 out << "S16:" << arg->value.s16 << std::endl;
 93                 break;
 94             }
 95             case 'I': {
 96                 out << "S32:" << arg->value.s32 << std::endl;
 97                 break;
 98             }
 99             case 'F': {
100                 out << "F32:" << arg->value.f32 << std::endl;
101                 break;
102             }
103             case 'J': {
104                 out << "S64:" << arg->value.s64 << std::endl;
105                 break;
106             }
107             case 'D': {
108                 out << "F64:" << arg->value.f64 << std::endl;
109                 break;
110             }
111             default: {
112                 std::cerr << "unexpected variant '" << (char) arg->variant << "'" << std::endl;
113                 exit(1);
114             }
115         }
116     }
117     out << "schema len = " << argSled.schemaLen() << std::endl;
118 
119     out << "schema = " << argSled.schema() << std::endl;
120 }
121 
122 
123 extern "C" int getMaxComputeUnits(long backendHandle) {
124     if (INFO){
125         std::cout << "trampolining through backendHandle to backend.getMaxComputeUnits()" << std::endl;
126     }
127     auto *backend = reinterpret_cast<Backend*>(backendHandle);
128     return backend->getMaxComputeUnits();
129 }
130 
131 extern "C" void info(long backendHandle) {
132     if (INFO){
133        std::cout << "trampolining through backendHandle to backend.info()" << std::endl;
134     }
135     auto *backend = reinterpret_cast<Backend*>(backendHandle);
136     backend->info();
137 }
138 extern "C" void releaseBackend(long backendHandle) {
139     auto *backend = reinterpret_cast<Backend*>(backendHandle);
140     delete backend;
141 }
142 extern "C" long compileProgram(long backendHandle, int len, char *source) {
143     if (INFO){
144        std::cout << "trampolining through backendHandle to backend.compileProgram() "
145            <<std::hex<<backendHandle<< std::dec <<std::endl;
146     }
147     auto *backend = reinterpret_cast<Backend*>(backendHandle);
148     auto programHandle = backend->compileProgram(len, source);
149     if (INFO){
150        std::cout << "programHandle = "<<std::hex<<backendHandle<< std::dec <<std::endl;
151     }
152     return programHandle;
153 }
154 extern "C" long getKernel(long programHandle, int nameLen, char *name) {
155     if (INFO){
156         std::cout << "trampolining through programHandle to program.getKernel()"
157             <<std::hex<<programHandle<< std::dec <<std::endl;
158     }
159     auto program = reinterpret_cast<Backend::Program *>(programHandle);
160     return program->getKernel(nameLen, name);
161 }
162 
163 extern "C" long ndrange(long kernelHandle, void *argArray) {
164     if (INFO){
165        std::cout << "trampolining through kernelHandle to kernel.ndrange(...) " << std::endl;
166     }
167     auto kernel = reinterpret_cast<Backend::Program::Kernel *>(kernelHandle);
168     kernel->ndrange( argArray);
169     return (long) 0;
170 }
171 extern "C" void releaseKernel(long kernelHandle) {
172     if (INFO){
173        std::cout << "trampolining through to releaseKernel " << std::endl;
174     }
175     auto kernel = reinterpret_cast<Backend::Program::Kernel *>(kernelHandle);
176     delete kernel;
177 }
178 
179 extern "C" void releaseProgram(long programHandle) {
180     if (INFO){
181        std::cout << "trampolining through to releaseProgram " << std::endl;
182     }
183     auto program = reinterpret_cast<Backend::Program *>(programHandle);
184     delete program;
185 }
186 extern "C" bool programOK(long programHandle) {
187     if (INFO){
188        std::cout << "trampolining through to programOK " << std::endl;
189     }
190     auto program = reinterpret_cast<Backend::Program *>(programHandle);
191     return program->programOK();
192 }
193 
194