First effort to revive BasicBlock and Function classes and
[jitcs.git] / include / jitcs_instructionstream.h
blob5bdc73d2e0bdd3c1dd966df3de735ecfb9777340
1 //===-- jitcs_instructionstream.h - blockwise instructions ------*- C++ -*-===//
2 //
3 //
4 //
5 //===----------------------------------------------------------------------===//
7 #ifndef _JITCS_INSTRUCTIONSTREAM_H_
8 #define _JITCS_INSTRUCTIONSTREAM_H_
10 #include "jitcs_adt_ref.h"
11 #include "jitcs_base.h"
12 #include "jitcs_ids.h"
13 #include "jitcs_instruction.h"
14 #include "jitcs_bblock.h"
16 namespace jitcs {
17 class BasicBlock;
18 struct MemoryReference;
19 struct VirtualRegister;
21 struct InstructionStream {
22 Ref<Instruction> alloc(u32 d) {
23 if (d > space) _flush();
24 iptr* t = cur;
25 cur += d; space -= d;
26 return reinterpret_cast<Instruction*>(t);
29 protected:
30 void init(iptr* dp, u32 spc) { cur = dp; space = spc; }
31 virtual void _flush() = 0;
33 protected:
34 iptr *cur;
35 u32 space;
38 template <uint INSSZ>
39 struct InstructionStreamBuffer : InstructionStream {
40 enum { INSSZ2 = (INSSZ + 31) & (~31) };
41 RefOrNull<BasicBlock> bb;
42 iptr originalBuffer[INSSZ2];
44 InstructionStreamBuffer() = default;
45 void start(RefOrNull<BasicBlock> block) { bb = block; init(&originalBuffer[0], INSSZ2); }
46 void end() { _flush(); bb = nullptr; }
48 protected:
49 virtual void _flush() {
50 if (!bb.isNull())
51 bb.removeNullType()->append_copy(&originalBuffer[0], INSSZ2 - space);
52 init(&originalBuffer[0], INSSZ2);
56 } // end of namespace jitcs
58 #endif
59 // _JITCS_INSTRUCTIONSTREAM_H_