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 class MockBackend : public Backend {
 28 public:
 29     class MockConfig : public Backend::Config {
 30     public :
 31     };
 32 
 33     class MockProgram : public Backend::Program {
 34         class MockKernel : public Backend::Program::Kernel {
 35         public:
 36             MockKernel(Backend::Program *program, char *name)
 37                     : Backend::Program::Kernel(program, name) {
 38             }
 39 
 40             ~MockKernel() {
 41             }
 42 
 43             long ndrange(void *argArray) {
 44                 std::cout << "mock ndrange() " << std::endl;
 45                 return 0;
 46             }
 47         };
 48 
 49     public:
 50         MockProgram(Backend *backend, BuildInfo *buildInfo)
 51                 : Backend::Program(backend, buildInfo) {
 52         }
 53 
 54         ~MockProgram() {
 55         }
 56 
 57         long getKernel(int nameLen, char *name) {
 58             return (long) new MockKernel(this, name);
 59         }
 60 
 61         bool programOK() {
 62             return true;
 63         }
 64     };
 65 
 66 public:
 67 
 68     MockBackend(MockConfig *mockConfig, int mockConfigSchemeLen, char *mockBackendSchema)
 69             : Backend(mockConfig, mockConfigSchemeLen, mockBackendSchema) {
 70         if (mockConfig == nullptr) {
 71             std::cout << "mockConfig == null" << std::endl;
 72         } else {
 73             std::cout << "mockConfig != null" << std::endl;
 74         }
 75     }
 76 
 77     ~MockBackend() {
 78     }
 79 
 80     int getMaxComputeUnits() {
 81         std::cout << "mock getMaxComputeUnits()" << std::endl;
 82         return 0;
 83     }
 84 
 85     void info() {
 86         std::cout << "mock info()" << std::endl;
 87     }
 88 
 89     long compileProgram(int len, char *source) {
 90         std::cout << "mock compileProgram()" << std::endl;
 91         size_t srcLen = ::strlen(source);
 92         char *src = new char[srcLen + 1];
 93         ::strncpy(src, source, srcLen);
 94         src[srcLen] = '\0';
 95         std::cout << "native compiling " << src << std::endl;
 96         return (long) new MockProgram(this, new BuildInfo(src, nullptr, false));
 97     }
 98 };
 99 
100 long getBackend(void *config, int configSchemaLen, char *configSchema) {
101     MockBackend::MockConfig *mockConfig = (MockBackend::MockConfig *) config;
102     return (long) new MockBackend(mockConfig, configSchemaLen, configSchema);
103 }