The check to see if an item was already on the heap was randomly hitting.
[jitcs.git] / include / jitcs_machine.h
blob517f1cb7141c21051e9cdaa19b09d3e70501f384
1 //===-- jitcs_machine.h - Machine info --------------------------*- C++ -*-===//
2 //
3 // IMachineInfo holds the complete state necessary for the compiler component
4 // to handle machine instructions for a particular machine. Particular
5 // architectures (e.g. x86) are defined in different header files and will fill
6 // in all fields with the relevant values.
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef _JITCS_MACHINE_H_
11 #define _JITCS_MACHINE_H_
13 #include "jitcs_adt_ref.h"
14 #include "jitcs_adt_refcounter.h"
15 #include "jitcs_adt_slice.h"
16 #include "jitcs_base.h"
17 #include "jitcs_cpu.h"
18 #include "jitcs_fnctypeinfo.h"
19 #include "jitcs_ids.h"
20 #include <memory>
22 #ifdef __GNUC__
23 #define INLINEIT inline __attribute__((always_inline))
24 #else
25 #define INLINEIT inline
26 #endif
28 namespace jitcs {
29 struct Instruction;
30 struct VirtualRegister;
31 //class BBlock;
32 //struct CCInfo;
33 class BasicBlock;
34 class CallingConvention;
35 class CodeGenHelper;
36 class Function;
37 class IDumper;
38 class TempAllocator;
40 class IMachineDetails;
42 class IMachineInfo {
43 protected:
44 IMachineInfo(const CPUInfo&);
45 virtual ~IMachineInfo() = default;
46 private:
47 IMachineInfo() = delete;
48 IMachineInfo(const IMachineInfo&) = delete;
49 IMachineInfo& operator =(const IMachineInfo&) = delete;
50 public:
51 CPUInfo cpu;
53 virtual Ref<const IMachineDetails> details() const = 0;
54 virtual std::shared_ptr<const CallingConvention>
55 getCC(FTSubType, Slice<FTTypeId> results, Slice<FTTypeId> params) = 0;
57 std::unique_ptr<Function>
58 createFnc(RefCounter<TempAllocator>, std::shared_ptr<const CallingConvention>);
60 template <typename T>
61 INLINEIT std::shared_ptr<const CallingConvention> getCC() {
62 return _getCC<typename GetFTSubType<T>::FType>(GetFTSubType<T>::Value);
64 template <typename T>
65 INLINEIT std::unique_ptr<Function> createFnc(RefCounter<TempAllocator> tmp) {
66 return createFnc(tmp, _getCC<typename GetFTSubType<T>::FType>
67 (GetFTSubType<T>::Value));
70 protected:
71 template <typename T>
72 std::shared_ptr<const CallingConvention> _getCC(FTSubType sub) {
73 FTTypeId res[1];
74 FTTypeId par[10];
75 res[0] = GetFTResultId<T>::Value;
76 static const int N = GetFTParamCount<T>::Value;
77 static_assert(N <= 10, "too many parameters in function type");
78 _fillFTParams<T, N>(par);
79 return getCC(sub, Slice<FTTypeId>(res, 1), Slice<FTTypeId>(par, N));
82 private:
83 template <typename T, uint N, bool B> struct _FTParamsFiller_;
84 template <typename T, uint N>
85 struct _FTParamsFiller_<T, N, true> {
86 static inline void run(FTTypeId* arr) {
87 static_assert(GetFTParamId<T, N>::OK, "unsupported parameter type");
88 arr[N] = GetFTParamId<T, N>::Value;
89 _FTParamsFiller_<T, N + 1, (N + 1 < GetFTParamCount<T>::Value)>::run(arr);
92 template <typename T, uint N>
93 struct _FTParamsFiller_<T, N, false> {
94 static inline void run(FTTypeId* arr) {}
96 template <typename T, uint N>
97 void _fillFTParams(FTTypeId* arr) {
98 _FTParamsFiller_<T, 0, (0 < GetFTParamCount<T>::Value)>::run(arr);
101 private:
102 // called from RefCounter
103 friend class RefCounter<IMachineInfo>;
104 void _incRef() { ++_refCnt; }
105 void _decRef() { if (!--_refCnt) _release(); }
106 void _release();
108 private:
109 size_t _refCnt;
112 } // end namespace jitcs
114 #endif
115 // _JITCS_MACHINE_H_