1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <random>
#include <cmath>
#include <string>
#include <chrono>
#include <vector>
#include <windows.h>
using namespace std;
// Instructions implementation
void add(int &a, int &b, int &c) { c = a + b; }
void sub(int &a, int &b, int &c) { b = c - b; }
void mult(int &a, int &b, int &c) { a = c * b; }
void divide(int &a, int &b, int &c) { if (a!=0) c = b / a; }
void inc(int &a, int &b, int &c) { a++; b++; c++; }
void print(int c) { cout << c << endl; }
typedef void(*funcPointer)(int&,int&,int&); // function pointer used to get the function from the dll
int main()
{
int a = 50; // some random initialization
int b = 200;
int c = 3;
std::random_device seedDevice;
std::mt19937 rEngine(seedDevice());
std::uniform_int_distribution<int> InstNumDist(100000, 200000);
std::uniform_int_distribution<int> InstOpcodeDist(1, 5);
int numberOfInst = InstNumDist(rEngine);
vector<int> input;
for (int i = 0; i != numberOfInst; i++) // emulate user input with random numbers
input.push_back(InstOpcodeDist(rEngine));
/// Interpreter
auto interpreterStart = chrono::steady_clock::now();
for (int i = 0; i != input.size(); i++)
{
switch (input[i])
{
case 1: add(a, b, c); break;
case 2: sub(a, b, c); break;
case 3: mult(a, b, c); break;
case 4: divide(a, b, c); break;
case 5: inc(a, b, c); break;
case 6: print(c); break;
}
}
cout << "Interpreter: " << endl << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl;
auto interpreterEnd = chrono::steady_clock::now();
cout << "Interpreter took " << chrono::duration <double, milli>(interpreterEnd - interpreterStart).count() << " ms" << endl;
/// Recompiler:
a = 50; // Same initial state
b = 200;
c = 3;
ofstream CompileFile;
CompileFile.open("GeneratedCode.cpp");
CompileFile << R"(
#include <iostream>
extern "C"
{
void __declspec(dllexport) GeneratedCode(int &a, int &b, int &c)
{
)" << '\n';
// Code Generation
for (int i = 0; i != input.size(); i++)
{
switch (input[i])
{
case 1: CompileFile << "c = a + b;\n"; break;
case 2: CompileFile << "b = c - b;\n"; break;
case 3: CompileFile << "a = c * b;\n"; break;
case 4: CompileFile << "if (a != 0) c = b / a;\n"; break;
case 5: CompileFile << "a++; b++; c++;\n"; break;
case 6: print(c); break;
}
}
CompileFile << R"(
}
}
)" << '\n';
CompileFile.close(); // call before compiling
auto compilerStart = chrono::steady_clock::now();
// compile the generated code as dll
system(R"(call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" && cl /LD GeneratedCode.cpp)");
auto compilerEnd = chrono::steady_clock::now();
cout << "Compilation took " << chrono::duration <double, milli>(compilerEnd - compilerStart).count() << " ms" << endl;
HINSTANCE calledCode = LoadLibrary("GeneratedCode.dll");
if (!calledCode) {
cout << "Import dll failed" << endl;
system("pause");
return EXIT_FAILURE;
}
else
std::cout << "Import dll succeeded" << std::endl;
funcPointer fp = (funcPointer)GetProcAddress(calledCode, "GeneratedCode"); // get the function from the dll
if (!fp)
{
cout << "dll function get failed" << endl;
system("pause");
return EXIT_FAILURE;
}
auto recompilerStart = chrono::steady_clock::now();
fp(a,b,c); // execute the dll function
auto recompilerEnd = chrono::steady_clock::now();
cout << "Recompiler took " << chrono::duration <double, milli>(recompilerEnd - recompilerStart).count() << " ms" << endl;
cout << "Recompiler: " << endl << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl;
system("pause");
return 0;
}
|