summaryrefslogtreecommitdiff
path: root/backend/src/backend/sim_program.cpp
blob: 5c18af7a35d77af4da79ac2674665478f13bd6b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* 
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Benjamin Segovia <benjamin.segovia@intel.com>
 */

/**
 * \file program.cpp
 * \author Benjamin Segovia <benjamin.segovia@intel.com>
 */

#include "backend/program.h"
#include "backend/sim_program.h"
#include "backend/sim_program.hpp"
#include "backend/sim_context.hpp"
#include <cstring>
#include <dlfcn.h>

namespace gbe {

  SimKernel::SimKernel(const std::string &name) :
    Kernel(name), fn(NULL), handle(NULL) {}
  SimKernel::~SimKernel(void) { if (this->handle) dlclose(this->handle); }

  SimProgram::SimProgram(void) {}
  SimProgram::~SimProgram(void) {}

  Kernel *SimProgram::compileKernel(const ir::Unit &unit, const std::string &name) {
    Context *ctx = GBE_NEW(SimContext, unit, name);
    Kernel *ker = ctx->compileKernel();
    GBE_DELETE(ctx);
    return ker;
  }

  static gbe_program simProgramNewFromBinary(const char *binary, size_t size) {
    NOT_IMPLEMENTED;
    return NULL;
  }

  static gbe_program simProgramNewFromLLVM(const char *fileName,
                                           size_t stringSize,
                                           char *err,
                                           size_t *errSize)
  {
    using namespace gbe;
    SimProgram *program = GBE_NEW(SimProgram);
    std::string error;
    // Try to compile the program
    if (program->buildFromLLVMFile(fileName, error) == false) {
      if (err != NULL && errSize != NULL && stringSize > 0u) {
        const size_t msgSize = std::min(error.size(), stringSize-1u);
        std::memcpy(err, error.c_str(), msgSize);
        *errSize = error.size();
      }
      GBE_DELETE(program);
      return NULL;
    }
    // Everything run fine
    return (gbe_program) program;
  }

} /* namespace gen */

void simSetupCallBacks(void)
{
  gbe_program_new_from_binary = gbe::simProgramNewFromBinary;
  gbe_program_new_from_llvm = gbe::simProgramNewFromLLVM;
}