The check to see if an item was already on the heap was randomly hitting.
[jitcs.git] / include / jitcs_instructionstream.h
blob8b88cee8fb74a42eb5b9c1cac4c50d8f180bb21f
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 start(Ref<BasicBlock> block) { bb._ptr = block._ptr; init(&originalBuffer[0], INSSZ2); }
47 void end() { _flush(); bb = nullptr; }
49 protected:
50 virtual void _flush() {
51 if (!bb.isNull())
52 bb.removeNullType()->append_copy(&originalBuffer[0], INSSZ2 - space);
53 init(&originalBuffer[0], INSSZ2);
57 } // end of namespace jitcs
59 #endif
60 // _JITCS_INSTRUCTIONSTREAM_H_