Nicolas Winkler há 5 anos atrás
pai
commit
c518a65e08

+ 1 - 0
libmandel/CMakeLists.txt

@@ -30,6 +30,7 @@ SET(MandelSources
     src/IterationGenerator.cpp
     src/IterationFormula.cpp
     src/IterationCompiler.cpp
+    src/IterationIR.cpp
 )
 FILE(GLOB MandelHeaders include/*.h)
 

+ 39 - 0
libmandel/include/Arena.h

@@ -0,0 +1,39 @@
+#ifndef MANDEL_ARENA_H
+#define MANDEL_ARENA_H
+
+#include <array>
+#include <utility>
+#include <memory>
+
+namespace mnd
+{
+    namespace util
+    {
+        template <typename T, int chunkSize = 32>
+        class Arena
+        {
+            struct Chunk
+            {
+                std::array<T, chunkSize> data;
+                int used = 0;
+
+                bool full(void) const { return used = chunkSize; }
+                T* allocate() { return data[used++]; }
+            };
+
+            std::vector<std::unique_ptr<Chunk>> chunks;
+        public:
+            T* allocate(void)
+            {
+                if (chunks.empty() || chunks[chunks.size() - 1].full()) {
+                    chunks.push_back(Chunk{});
+                }
+
+                return chunks[chunks.size() - 1].allocate();
+            }
+        };
+    }
+}
+
+
+#endif // MANDEL_ARENA_H

+ 74 - 0
libmandel/include/IterationIR.h

@@ -0,0 +1,74 @@
+#ifndef MANDEL_ITERATIONIR_H
+#define MANDEL_ITERATIONIR_H
+
+#include <string>
+#include <vector>
+
+#include "IterationFormula.h"
+#include "Arena.h"
+
+namespace mnd
+{
+    namespace ir
+    {
+        struct Constant;
+        struct Variable;
+        struct Negation;
+        struct BinaryOperation;
+        struct Addition;
+        struct Multiplication;
+
+        using Node = std::variant<
+            Constant,
+            Variable,
+            Negation,
+            Addition,
+            Multiplication
+        >;
+
+        class Formula
+        {
+            util::Arena<Node> nodeArena;
+            Node* newA;
+            Node* newB;
+        };
+    }
+}
+
+
+struct mnd::ir::Constant
+{
+    double value;
+};
+
+
+struct mnd::ir::Variable
+{
+    std::string name;
+};
+
+
+struct mnd::ir::Negation
+{
+    Node* value;
+};
+
+
+struct mnd::ir::BinaryOperation
+{
+    Node* a;
+    Node* b;
+};
+
+
+struct mnd::ir::Addition : mnd::ir::BinaryOperation
+{
+};
+
+
+struct mnd::ir::Multiplication : mnd::ir::BinaryOperation
+{
+};
+
+
+#endif // MANDEL_ITERATIONIR_H

+ 51 - 0
libmandel/src/IterationCompiler.cpp

@@ -1,4 +1,5 @@
 #include "IterationCompiler.h"
+#include "IterationIR.h"
 
 #include <asmjit/asmjit.h>
 #include "Mandel.h"
@@ -178,6 +179,56 @@ namespace mnd
         }
         return ed;
     }
+
+
+    mnd::ExecData compile(mnd::MandelContext& mndCtxt, const IterationFormula& formula)
+    {
+        mnd::ExecData ed;
+        JitRuntime& jitRuntime = *ed.jitRuntime;
+        ed.code->init(jitRuntime.codeInfo());
+
+        x86::Compiler& comp = *ed.compiler;
+
+        x86::Mem sixteen = comp.newDoubleConst(ConstPool::kScopeLocal, 16.0);
+
+        Label startLoop = comp.newLabel();
+        Label endLoop = comp.newLabel();
+        x86::Gp maxIter = comp.newInt32();
+        x86::Gp k = comp.newInt32();
+        x86::Xmm x = comp.newXmmSd();
+        x86::Xmm y = comp.newXmmSd();
+        x86::Xmm a = comp.newXmmSd();
+        x86::Xmm b = comp.newXmmSd();
+        comp.addFunc(FuncSignatureT<int, double, double, int>(CallConv::kIdHost));
+        comp.setArg(0, x);
+        comp.setArg(1, y);
+        comp.setArg(2, maxIter);
+        comp.movapd(a, x);
+        comp.movapd(b, y);
+
+        comp.xor_(k, k);
+
+        comp.bind(startLoop);
+
+        // loop body
+
+        comp.inc(k);
+        comp.cmp(k, maxIter);
+        comp.jne(startLoop);
+        comp.bind(endLoop);
+        comp.ret(k);
+        comp.endFunc();
+        auto err = comp.finalize();
+        if (err == asmjit::kErrorOk) {
+            err = jitRuntime.add(&ed.iterationFunc, ed.code.get());
+            if (err != asmjit::kErrorOk)
+                throw "error adding function";
+        }
+        else {
+            throw "error compiling";
+        }
+        return ed;
+    }
 }
 
 

+ 5 - 0
libmandel/src/IterationIR.cpp

@@ -0,0 +1,5 @@
+#include "IterationIR.h"
+
+
+
+using namespace mnd;