Preliminary checkin for a mini-assembler used for checking the library.
[jitcs.git] / tests / test_allinstructions.cpp
blob24d3157d445889579537aac4f3634d3bec65abc7
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"
7 #include "jitcs_x86_64.h"
8 #include <unordered_set>
9 #include "jitcs_int_machine.h"
10 #include "jitcs_int_bitfuncs.h"
12 using namespace jitcs;
14 static void test(UnitTest& t) {
15 typedef void (__cdecl *FT0_c)();
16 RefCounter<IMachineInfo> mi = GetX86_32WinMachineInfo();
17 RefCounter<TempAllocator> alloc(new TempAllocator);
18 RefCounter<MemoryMgr> mgr = MemoryMgr::Create();
20 std::unique_ptr<Function> fn = mi->createFnc<FT0_c>(alloc);
21 InstructionStreamBuffer<256> ibuf;
23 Ref<BasicBlock> bb1 = fn->getStartBlock();
25 ibuf.start(bb1);
26 x86_32::MOV_WI(ibuf, x86_32::EAX, 0);
27 x86_32::ADD_WI(ibuf, x86_32::EAX, 1);
28 x86_32::RET(ibuf);
29 ibuf.end();
31 MemoryMgr::CodeAndData code = fn->generate(mgr, Function::Direct_Code_Gen);
33 StringDumper dumper;
34 Dump(dumper, code);
35 std::string dumpresult = dumper.takeResult();
37 size_t n = 0;
38 for (x86_32::InsClassId i = x86_32::ICL_First; i < x86_32::ICL_Last; ++i) {
39 n += popcnt(x86_32::ToValidMask(i));
41 t.check("x86_32", dumpresult == expected);
45 static UnitTestRun _("AllInstructions", test);