Renamed DynArrayRef to DynSlice, and renamed adt_array header to
[jitcs.git] / src / jitcs_int_codegenerator.h
blob940d3dc7f4551dd0ac6cbe17971c700d50be8bc9
1 //===-- evm/bblock.h - a single basic block --*- C++ -*-===//
2 //
3 // A basic block contains a list of instructions.
4 //
5 //===----------------------------------------------------------------------===//
7 #ifndef _JITCS_INT_CODEGENERATOR_H_
8 #define _JITCS_INT_CODEGENERATOR_H_
10 #include "jitcs_base.h"
11 #include "jitcs_adt_ref.h"
12 #include "jitcs_adt_refcounter.h"
13 #include "jitcs_adt_slice.h"
14 #include "jitcs_int_bblock_impl.h"
15 #include "jitcs_ids.h"
16 #include "jitcs_tmpalloc.h"
17 #include "jitcs_memmgr.h"
19 namespace jitcs {
20 class FunctionImpl;
21 class CFGAnalysis;
23 class CodeGenerator {
24 public:
25 enum RelocType {
26 REL_PCREL32_TAIL = 32,
27 REL_PCREL16_TAIL = 16,
28 REL_PCREL8_TAIL = 8,
30 struct FrameRegisterInfo {
31 RegId reg;
32 int ofs;
35 public:
36 CodeGenerator();
37 CodeGenerator(const CodeGenerator &) = delete;
38 void operator=(const CodeGenerator &) = delete;
40 MemoryMgr::CodeAndData generate(FunctionImpl&, CFGAnalysis const&,
41 MemoryMgr& vm, Slice<u8> data);
43 public:
44 // methods for use when generating code
45 bool hasEstimatedPos(Ref<const BasicBlockImpl> bb) const {
46 return _testFlags(bb, BBLOC_ESTIMATED);
48 bool hasFinalAddress(Ref<const BasicBlockImpl> bb) const {
49 return _testFlags(bb, BBLOC_FINAL);
51 bool hasEstimatedOrFinalPos(Ref<const BasicBlockImpl> bb) const {
52 return _testFlags(bb, BBLOC_ESTIMATED | BBLOC_FINAL);
54 size_t getEstimatedOrFinalPos(Ref<const BasicBlockImpl> bb) const {
55 return _getPos(bb);
57 Ref<u8> getEstimatedOrFinalAddress(Ref<const BasicBlockImpl> bb) const {
58 return _codespace._ptr + _getPos(bb);
60 void addRelocation(Ref<const BasicBlockImpl> bb, Ref<u8> x, RelocType rel);
62 FrameRegisterInfo getFrameRegisterInfo(size_t i) {
63 assert(i < MMODE_FPCount);
64 return _frameRegisterInfo[i];
67 private:
68 enum Layout {
69 BBLOC_ESTIMATED = 1, // position of this basic block is estimated and cannot be further away
70 BBLOC_FINAL = 2, // position of this basic block is precisely defined
71 BBLOC_SHIFT = 2, // lowest two bits are flags
73 struct RelocInfo {
74 RefOrNull<RelocInfo> next;
75 RefOrNull<u8> tgt;
76 RelocType type;
78 struct BBlockInfo {
79 RefOrNull<RelocInfo> relocChain;
80 size_t posAndFlags;
83 private:
84 void _setEstimatedPosAndClearChain(Ref<const BasicBlockImpl> bb, size_t x);
85 void _setFinalPos(Ref<const BasicBlockImpl> bb, size_t x, Ref<u8> y);
87 bool _testFlags(Ref<const BasicBlockImpl> bb, u8 f) const { return (_bblocks[bb->id()].posAndFlags & f) != 0; }
88 size_t _getPos(Ref<const BasicBlockImpl> bb) const { return _bblocks[bb->id()].posAndFlags >> BBLOC_SHIFT; }
90 private:
91 Slice<BBlockInfo> _bblocks;
92 StreamAllocator<RelocInfo>* _relocalloc;
93 Slice<u8> _codespace;
94 FrameRegisterInfo _frameRegisterInfo[MMODE_FPCount];
97 } // end of namespace jitcs
99 #endif
100 // _JITCS_INT_CODEGENERATOR_H_