Adding copyright notices to most files. Also add readme file, and some
[jitcs.git] / include / jitcs_instruction.h
blob3ace21ca88326750734c31ef973e9f0ca3c2afec
1 //===-- jitcs_instruction.h -------------------------------------*- C++ -*-===//
2 // Interface to a single instruction object.
3 //
4 // Copyright (C) 2013-2014 Dirk Steinke.
5 // See copyright and license notice in COPYRIGHT or include/jitcs.h
6 //
7 // Actually, multiple instructions are stored as an array of iptr, and
8 // Instruction is only used as a typecast from a pointer to those iptr.
9 // Instructions are never hand-constructed by the application, but the
10 // application uses helper functions in jitcs_{arch}_cons.h.
11 //===----------------------------------------------------------------------===//
13 #ifndef _JITCS_INSTRUCTION_H_
14 #define _JITCS_INSTRUCTION_H_
16 #include "jitcs_adt_ref.h"
17 #include "jitcs_base.h"
18 #include "jitcs_ids.h"
20 namespace jitcs {
21 class BasicBlock;
22 class BasicBlockImpl;
23 struct MemoryReference;
24 struct VirtualRegister;
26 struct Instruction {
27 iptr _ptr[32];
29 template <typename ...T>
30 void setAll(InsId id, T... args) { setInsId(id); _setAll(1, args...); }
32 void setInsId(InsId id) { _ptr[0] = id; }
33 void setRRefOp(size_t i, Ref<const VirtualRegister> r) { _setOpRef<const VirtualRegister>(i, r); }
34 void setMRefOp(size_t i, Ref<MemoryReference> m) { _setOpRef<MemoryReference>(i, m); }
35 void setBBOp (size_t i, Ref<BasicBlock> bb) { _setOpRef<BasicBlock>(i, bb); }
36 void setImmOp (size_t i, iptr imm) { _ptr[i] = imm; }
38 InsId getInsId() const { return static_cast<InsId>(_ptr[0]); }
40 Ref<const VirtualRegister> getRRefOp(size_t i) const {
41 return _getOpRef<const VirtualRegister>(i);
43 Ref<const VirtualRegister> getConstRRefOp(size_t i) const {
44 return _getConstOpRef<VirtualRegister>(i);
47 Ref<MemoryReference> getMRefOp(size_t i) const { return _getOpRef<MemoryReference>(i); }
48 Ref<BasicBlock> getBBOp (size_t i) const { return _getOpRef<BasicBlock>(i); }
49 Ref<BasicBlockImpl> getBBIOp (size_t i) const { return _getOpRef<BasicBlockImpl>(i); }
50 iptr getImmOp (size_t i) const { return _ptr[i]; }
52 Ref<MemoryReference> getMRefOp(size_t i) { return _getOpRef<MemoryReference>(i); }
53 Ref<BasicBlock> getBBOp (size_t i) { return _getOpRef<BasicBlock>(i); }
54 Ref<BasicBlockImpl> getBBIOp (size_t i) { return _getOpRef<BasicBlockImpl>(i); }
56 Ref<const MemoryReference> getConstMRefOp(size_t i) const { return _getConstOpRef<MemoryReference>(i); }
57 Ref<const BasicBlock> getConstBBOp (size_t i) const { return _getConstOpRef<BasicBlock>(i); }
58 Ref<const BasicBlockImpl> getConstBBIOp (size_t i) const { return _getConstOpRef<BasicBlockImpl>(i); }
60 private:
61 template <typename T>
62 inline void _setOp(int i, T* p) { _ptr[i] = reinterpret_cast<intptr_t>(p); }
63 template <typename T>
64 inline void _setOpRef(int i, Ref<T> p) { _setOp(i, p._ptr); }
65 template <typename T>
66 inline T _getOp(int i) const { return reinterpret_cast<T>(_ptr[i]); }
67 template <typename T>
68 inline Ref<T> _getOpRef(int i) const { return Ref<T>(_getOp<T*>(i)); }
69 template <typename T>
70 inline Ref<const T> _getConstOpRef(int i) const { return Ref<const T>(_getOp<T*>(i)); }
72 inline void _set(int i, Ref<const VirtualRegister> r) { setRRefOp(i, r); }
73 inline void _set(int i, Ref<MemoryReference> m) { setMRefOp(i, m); }
74 inline void _set(int i, Ref<BasicBlock> bb) { setBBOp(i, bb); }
75 inline void _set(int i, iptr imm) { setImmOp(i, imm); }
76 inline void _setAll(int i) {}
77 template <typename U, typename ...T>
78 inline void _setAll(int i, U first, T... args) { _set(i, first); _setAll(i + 1, args...); }
81 } // end of namespace jitcs
83 #endif
84 // _JITCS_INSTRUCTION_H_