Changed current relation from BasicBlock to BasicBlockImpl, and Function
[jitcs.git] / tests / test_simplecodegen.cpp
blobd67ae719df3e90fb6fefb859bd9d557f5d089511
1 #include "jitcs.h"
2 #include "unittest.h"
3 #include "jitcs_instructionstream.h"
4 #include "jitcs_dumper.h"
5 #include "jitcs_memmgr.h"
6 #include "jitcs_x86_32.h"
8 using namespace jitcs;
10 void Dump(IDumper& d, Slice<u8> mem) {
11 size_t n = mem.size();
12 while (n > 0 && mem[n - 1] == 0) --n;
13 for (size_t i = 0; i < n; ++i) {
14 d.writef("%02x", mem[i]);
16 if (n < mem.size())
17 d.writef("%s00 * %d", n > 0 ? " + " : "", mem.size() - n);
19 void Dump(IDumper& d, MemoryMgr::CodeAndData code) {
20 d.writef("Data %db: ", code.data.size());
21 Dump(d, code.data);
22 d.write("\n");
23 d.writef("Code %db: ", code.code.size());
24 Dump(d, code.code);
25 d.write("\n");
28 static void test(UnitTest& t) {
29 typedef int (__cdecl *FT0_c)(int, int);
30 RefCounter<IMachineInfo> mi = GetX86_32WinMachineInfo();
31 RefCounter<TempAllocator> alloc(new TempAllocator);
32 RefCounter<MemoryMgr> mgr = MemoryMgr::Create();
34 std::unique_ptr<Function> fn = mi->createFnc<FT0_c>(alloc);
35 InstructionStreamBuffer<256> ibuf;
37 Ref<BasicBlock> bb1 = fn->getStartBlock();
39 ibuf.start(bb1);
40 x86_32::MOV_WI(ibuf, x86_32::EAX, 0);
41 x86_32::ADD_WI(ibuf, x86_32::EAX, 1);
42 x86_32::RET(ibuf);
43 ibuf.end();
45 MemoryMgr::CodeAndData code = fn->generate(mgr, Function::Direct_Code_Gen);
47 StringDumper dumper;
48 Dump(dumper, code);
49 std::string dumpresult = dumper.takeResult();
51 std::string expected =
52 "Data 48b: 00 * 48\n"
53 "Code 64b: b80000000083c001c3 + 00 * 55\n";
55 t.check("x86_32/1", dumpresult == expected);
59 static UnitTestRun _("SimpleCodeGen", test);