/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * File:   memory.h
 * Author: pgess <v.melnychenko@xreate.org>
 *
 * Created on 27/05/2020
 */
#ifndef XREATE_MEMORY_H
#define XREATE_MEMORY_H

#include "pass/compilepass.h"

namespace xreate {namespace compilation{

enum class AllocatorType{
  CONTEXTPAD,
  LOCAL, // local allocation
  DEFAULT = LOCAL
};

class Allocator{
public:
  static Allocator* create(const Expression& expr, const Context& ctx);
  static AllocatorType getType(const Expression& expr, AST* ast);

  Allocator(LLVMLayer* l): llvm(l){}
  virtual llvm::Value* allocate(llvm::Type* elRawT, llvm::Value* sizeRaw, const std::string& hintAlias) = 0;

protected:
  LLVMLayer* llvm;
};

class ContextAllocator: public Allocator{
public:
  static ContextAllocator* create(const Expression& expr, const Context& ctx);
  ContextAllocator(llvm::Value* mem, LLVMLayer* l): Allocator(l), __mem(mem){}
  virtual llvm::Value* allocate(llvm::Type* elRawT, llvm::Value* sizeRaw, const std::string& hintAlias) override;

private:
  llvm::Value* __mem;
};

class LocalAllocator: public Allocator{
public:
  LocalAllocator(LLVMLayer* l): Allocator(l){}
  virtual llvm::Value* allocate(llvm::Type* elRawT, llvm::Value* sizeRaw, const std::string& hintAlias) override;
};

}} //xreate::compilation
#endif //XREATE_MEMORY_H
