A first step towards construction of functions. A lot of work went into
[jitcs.git] / tests / test_simplefunction.cpp
blobdb1a1c61fd95737340120b40729941c2f5232a0a
1 #include "jitcs.h"
2 #include "unittest.h"
3 #include "jitcs_instructionstream.h"
5 #include <stdio.h>
7 using namespace jitcs;
9 static void test(UnitTest& t) {
10 typedef int (__cdecl *FT0_c)(int, int);
11 RefCounter<IMachineInfo> mi = host::GetMachineInfo();
12 RefCounter<TempAllocator> alloc(new TempAllocator);
14 std::unique_ptr<Function> fn = mi->createFnc<FT0_c>(alloc);
15 InstructionStreamBuffer<256> ibuf;
16 Ref<VirtualRegister> param0 = fn->getArgumentRegister(0, host::RCL_GR);
17 Ref<VirtualRegister> param1 = fn->getArgumentRegister(1, host::RCL_GR);
18 Ref<VirtualRegister> result0 = fn->getResultRegister(0, host::RCL_GR);
19 Ref<BasicBlock> bb1 = fn->getStartBlock();
20 Ref<BasicBlock> bb2 = fn->createBasicBlock();
21 Ref<BasicBlock> bb3 = fn->createBasicBlock();
22 ibuf.start(bb1);
23 host::CMP_RR(ibuf, param0, param1);
24 host::JGE_BB_FT(ibuf, bb2, bb3);
25 ibuf.end();
27 ibuf.start(bb2);
28 host::MOV_RR(ibuf, result0, param0);
29 host::RET(ibuf);
30 ibuf.end();
32 ibuf.start(bb3);
33 host::MOV_RR(ibuf, result0, param1);
34 host::RET(ibuf);
35 ibuf.end();
38 Enumerator<const BasicBlock> enumbb = fn->enumerateBasicBlocks();
39 size_t c = 0, c1 = 0, c2 = 0, c3 = 0;
40 while (!enumbb.empty()) {
41 const BasicBlock& bb = enumbb.front();
42 ++c;
43 if (&bb == bb1._ptr) ++c1;
44 if (&bb == bb2._ptr) ++c2;
45 if (&bb == bb3._ptr) ++c3;
47 t.check("Build/Enumerate basic blocks", c1 == 1 && c2 == 1 && c3 == 1 && c == 3);
50 Enumerator<const Instruction> enumins = bb1->instructions();
51 bool ok = true;
52 ok = !enumins.empty();
53 if (ok) {
54 const Instruction& ins = enumins.front();
55 ok = ok && (ins.getInsId() == host::I_CMP_RR);
57 t.check("Build/Enumerate instructions on BB1/1", ok);
58 ok = ok && !enumins.empty();
59 if (ok) {
60 const Instruction& ins = enumins.front();
61 ok = ok && (ins.getInsId() == host::I_JGE_BB_FT);
63 t.check("Build/Enumerate instructions on BB1/2", ok);
64 ok = ok && enumins.empty();
65 t.check("Build/Enumerate instructions on BB1/3", ok);
68 Enumerator<const BasicBlock> predbb = bb1->predecessors();
69 Enumerator<const BasicBlock> succbb = bb1->successors();
70 t.check("Build/Pred+succ are empty before CFG fix", predbb.empty() && succbb.empty());
72 t.check("Fixing CFG not implemented", false);
73 t.check("Generating code not implemented", false);
74 t.check("Running function not implemented", false);
78 static UnitTestRun _("SimpleFunction", test);