1 //===-- evm/bblock.h - a single basic block --*- C++ -*-===//
3 // A basic block contains a list of instructions.
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"
26 REL_PCREL32_TAIL
= 32,
27 REL_PCREL16_TAIL
= 16,
30 struct FrameRegisterInfo
{
37 CodeGenerator(const CodeGenerator
&) = delete;
38 void operator=(const CodeGenerator
&) = delete;
40 MemoryMgr::CodeAndData
generate(FunctionImpl
&, CFGAnalysis
const&,
41 MemoryMgr
& vm
, Slice
<u8
> data
);
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 {
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
];
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
74 RefOrNull
<RelocInfo
> next
;
79 RefOrNull
<RelocInfo
> relocChain
;
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
; }
91 Slice
<BBlockInfo
> _bblocks
;
92 StreamAllocator
<RelocInfo
>* _relocalloc
;
94 FrameRegisterInfo _frameRegisterInfo
[MMODE_FPCount
];
97 } // end of namespace jitcs
100 // _JITCS_INT_CODEGENERATOR_H_