nicolaswinkler 8 vuotta sitten
vanhempi
commit
76a18016a3
15 muutettua tiedostoa jossa 125 lisäystä ja 20 poistoa
  1. BIN
      .uomap.cpp.swp
  2. BIN
      a.out
  3. 1 0
      asmjit
  4. 41 0
      jittest.cpp
  5. BIN
      jittest.o
  6. 8 2
      src/AltParser.h
  7. 34 13
      src/Optimizer.cpp
  8. 5 2
      src/Optimizer.h
  9. BIN
      src/Optimizer.o
  10. 14 3
      src/Parser.cpp
  11. 10 0
      src/Parser.h
  12. BIN
      src/Parser.o
  13. BIN
      src/a.out
  14. BIN
      src/zombie
  15. 12 0
      uomap.cpp

BIN
.uomap.cpp.swp


BIN
a.out


+ 1 - 0
asmjit

@@ -0,0 +1 @@
+Subproject commit 1370fe6a26a82f18500faedac66798953961a916

+ 41 - 0
jittest.cpp

@@ -0,0 +1,41 @@
+#include "asmjit/src/asmjit/asmjit.h"
+
+
+
+using namespace asmjit;
+
+// Signature of the generated function.
+typedef int (*Func)(void);
+
+int value()
+{
+  return 42;
+}
+
+int main(int argc, char* argv[]) {
+  JitRuntime rt;                          // Runtime specialized for JIT code execution.
+
+  CodeHolder code;                        // Holds code and relocation information.
+  code.init(rt.getCodeInfo());            // Initialize to the same arch as JIT runtime.
+
+  X86Assembler a(&code);                  // Create and attach X86Assembler to `code`.
+  a.mov(x86::eax, 1);                     // Move one to 'eax' register.
+  a.call(reinterpret_cast<uint64_t> (&value));
+  a.ret();                                // Return from function.
+  // ----> X86Assembler is no longer needed from here and can be destroyed <----
+
+  Func fn;
+  Error err = rt.add(&fn, &code);         // Add the generated code to the runtime.
+  if (err) return 1;                      // Handle a possible error returned by AsmJit.
+  // ----> CodeHolder is no longer needed from here and can be destroyed <----
+
+  int result = fn();                      // Execute the generated code.
+  printf("%d\n", result);                 // Print the resulting "1".
+
+  // All classes use RAII, all resources will be released before `main()` returns,
+  // the generated function can be, however, released explicitly if you intend to
+  // reuse or keep the runtime alive, which you should in a production-ready code.
+  rt.release(fn);
+
+  return 0;
+}

BIN
jittest.o


+ 8 - 2
src/AltParser.h

@@ -23,8 +23,9 @@ struct BfIterator
     std::istream& source;
     Instruction currentInst;
 
+    inline BfIterator(std::istream& source) : source(source) {}
     BfIterator(const BfIterator& other) = delete;
-    BfIterator(BfIterator&& other) :
+    inline BfIterator(BfIterator&& other) :
         source(other.source), currentInst(other.currentInst) {}
 
     inline BfIterator& operator++(void)
@@ -81,7 +82,7 @@ class BlockIterator
     bool isSpecialCharacter(void)
     {
         return *it == Instruction::LEFT_PARAN ||
-               *it == Instruction::LEFT_PARAN ||
+               *it == Instruction::RIGHT_PARAN ||
                *it == Instruction::COMMA ||
                *it == Instruction::DOT;
     }
@@ -92,6 +93,11 @@ public:
             it++;
         return *this;
     }
+
+    Instruction operator *(void)
+    {
+        return *it;
+    }
 };
 
 

+ 34 - 13
src/Optimizer.cpp

@@ -1,47 +1,68 @@
 #include "Optimizer.h"
 #include "Parser.h"
 #include <iostream>
+#include <vector>
 
 using namespace zp;
+using namespace std;
 
-std::unique_ptr<BlockInstruction> AstVisitor::visit(Block& block)
+unique_ptr<BlockInstruction> AstVisitor::visit(Block& block)
 {
     return block.accept(*this);
 }
 
 
-std::unique_ptr<BlockInstruction> Optimizer::visitInstructionBlock(InstructionBlock& ib)
+unique_ptr<BlockInstruction> Optimizer::visitInstructionBlock(InstructionBlock& ib)
 {
     std::cout << "InstructionBlock" << std::endl;
-    bool isSimple = true;
+    unique_ptr<SimpleBlockInstruction> sbi = make_unique<SimpleBlockInstruction>();
     for (auto instr : ib.instructions) {
         switch(instr) {
-            case Instruction::COMMA:
-            case Instruction::DOT:
-            
+            case Instruction::LEFT:
+                sbi->pointerMoved--;
+                break;
+            case Instruction::RIGHT:
+                sbi->pointerMoved++;
+                break;
+            case Instruction::PLUS:
+                sbi->added[sbi->pointerMoved]++;
+                break;
+            case Instruction::MINUS:
+                sbi->added[sbi->pointerMoved]--;
+                break;
+            default:
+                break;
         }
     }
-    return std::make_unique<SimpleBlockInstruction>();
+    return sbi;
 }
 
 
-std::unique_ptr<BlockInstruction> Optimizer::visitUnionBlock(UnionBlock& ub)
+unique_ptr<BlockInstruction> Optimizer::visitIOBlock(IOBlock& io)
 {
-    std::cout << "UnionBlock" << std::endl;
+    cout << "IOBlock" << std::endl;
+    return make_unique<SimpleBlockInstruction>();
+}
+
+
+unique_ptr<BlockInstruction> Optimizer::visitUnionBlock(UnionBlock& ub)
+{
+    cout << "UnionBlock" << std::endl;
+    
     for (auto& block : ub.instructions) {
         visit(*block.get());
     }
-    return std::make_unique<SimpleBlockInstruction>();
+    return make_unique<SimpleBlockInstruction>();
 }
 
 
-std::unique_ptr<BlockInstruction> Optimizer::visitLoop(Loop& loop)
+unique_ptr<BlockInstruction> Optimizer::visitLoop(Loop& loop)
 {
-    std::cout << "Loop" << std::endl;
+    cout << "Loop" << std::endl;
     for (auto& block : loop.instructions) {
         visit(*block.get());
     }
-    return std::make_unique<SimpleBlockInstruction>();
+    return make_unique<SimpleBlockInstruction>();
 }
 
 

+ 5 - 2
src/Optimizer.h

@@ -4,6 +4,7 @@
 #include "Parser.h"
 
 #include <unordered_map>
+#include <memory>
 
 namespace zp
 {
@@ -40,6 +41,8 @@ public:
     virtual std::unique_ptr<BlockInstruction>
         visitInstructionBlock(InstructionBlock& ib) = 0;
     virtual std::unique_ptr<BlockInstruction>
+        visitIOBlock(IOBlock& io) = 0;
+    virtual std::unique_ptr<BlockInstruction>
         visitUnionBlock(UnionBlock& ub) = 0;
 };
 
@@ -60,7 +63,7 @@ struct zp::UnionBlockInstruction :
 struct zp::SimpleBlockInstruction :
     public BlockInstruction
 {
-    Index pointerMove;
+    Index pointerMoved;
     std::unordered_map<Index, Cell> added;
 };
 
@@ -68,7 +71,6 @@ struct zp::SimpleBlockInstruction :
 struct zp::LinearLoopInstruction :
     public BlockInstruction
 {
-    
 };
 
 
@@ -84,6 +86,7 @@ class zp::Optimizer :
 public:
     std::unique_ptr<BlockInstruction> visitLoop(Loop& loop) override;
     std::unique_ptr<BlockInstruction> visitInstructionBlock(InstructionBlock& ib) override;
+    std::unique_ptr<BlockInstruction> visitIOBlock(IOBlock& io) override;
     std::unique_ptr<BlockInstruction> visitUnionBlock(UnionBlock& ub) override;
 
 };

BIN
src/Optimizer.o


+ 14 - 3
src/Parser.cpp

@@ -12,6 +12,12 @@ std::unique_ptr<BlockInstruction> InstructionBlock::accept(AstVisitor& v)
 }
 
 
+std::unique_ptr<BlockInstruction> IOBlock::accept(AstVisitor& v)
+{
+    return v.visitIOBlock(*this);
+}
+
+
 std::unique_ptr<BlockInstruction> UnionBlock::accept(AstVisitor& v)
 {
     return v.visitUnionBlock(*this);
@@ -57,10 +63,15 @@ unique_ptr<Block> Parser::parseBlock(bool isLoop)
                 block->addInstruction(Instruction::RIGHT);
                 break;
             case '.':
-                block->addInstruction(Instruction::DOT);
-                break;
             case ',':
-                block->addInstruction(Instruction::COMMA);
+                if (wrapper == nullptr) {
+                    wrapper = isLoop ? make_unique<Loop>() : make_unique<UnionBlock>();
+                }
+                if (!block->isEmpty()) {
+                    wrapper->addBlock(move(block));
+                    block = make_unique<InstructionBlock>();
+                }
+                wrapper->addBlock(make_unique<IOBlock>(chr == '.' ? Instruction::DOT : Instruction::COMMA));
                 break;
             case '[':
                 if (wrapper == nullptr) {

+ 10 - 0
src/Parser.h

@@ -12,6 +12,7 @@ namespace zp
 
     struct Block;
     struct InstructionBlock;
+    struct IOBlock;
     struct UnionBlock;
     struct Loop;
 
@@ -55,6 +56,15 @@ struct zp::InstructionBlock :
 };
 
 
+struct zp::IOBlock :
+    public Block
+{
+    Instruction ioInstruction;
+    inline IOBlock(Instruction i) : ioInstruction{i} {}
+    std::unique_ptr<BlockInstruction> accept(AstVisitor& v);
+};
+
+
 struct zp::UnionBlock :
     public Block
 {

BIN
src/Parser.o


BIN
src/a.out


BIN
src/zombie


+ 12 - 0
uomap.cpp

@@ -0,0 +1,12 @@
+#include <unordered_map>
+#include <iostream>
+
+int main()
+{
+    std::unordered_map<int, int> a;
+    a[5] = 3;
+    a[2]++;
+
+    std::cout << a[2] << std::endl;
+}
+