Advertisement
gravgun

MT Tesselator vm.h [WIP]

Oct 9th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU Lesser General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Lesser General Public License along
  16. with this program; if not, write to the Free Software Foundation, Inc.,
  17. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19.  
  20. #ifndef TRIANGULATOR_VM_HEADER
  21. #define TRIANGULATOR_VM_HEADER
  22.  
  23. #include <iostream>
  24. #include <vector>
  25. // Next line: should be <cstdint>, but we're not C++11 yet
  26. #include <stdint.h>
  27.  
  28. class TriangulatorVM {
  29. public:
  30.     const static int TVM_ASM_VERSION = 0;
  31.     typedef uint64_t IntType;
  32.     typedef double FloatType;
  33.    
  34.     enum Opcode {
  35.         nop = 0,    // No OPeration
  36.         halt,       // HALT vm
  37.         trap,       // debug TRAP
  38.         dbgpv,      // DeBuG Print Value
  39.         dbgds,      // DeBuG Dump Stack
  40.         dbgdc,      // DeBuG Dump Call stack
  41.        
  42.         push = 20// PUSH value on TOS
  43.         pop,        // POP value from TOS
  44.         dup,        // DUPlicate TOS
  45.         load,       // LOAD from mem
  46.         store,      // STORE to mem
  47.        
  48.         call = 30// CALL sub
  49.         ret,        // RETurn from call
  50.        
  51.         jmp = 40,   // JUMP to
  52.         jrel,       // JUMP to realtive pc
  53.         ifz,        // IF Zero
  54.         ifnz,       // IF Not Zero
  55.         ifeq,       // IF Equal
  56.         ifne,       // IF Not Equal
  57.         ifs,        // IF Smaller than
  58.         ifse,       // IF Smaller or Equal
  59.         ifg,        // IF Greater than
  60.         ifge,       // IF Greater or Equal
  61.        
  62.         // Math operators. Resulting type is int only if
  63.         // values used by op are all int; float otherwise
  64.         add = 60,   // ADD
  65.         sub,        // SUBstract
  66.         mul,        // MULtiply
  67.         div,        // DIVide
  68.         inv,        // INVerse
  69.         sqrt,       // SQuare RooT
  70.         invsqrt,    // INVerse SQuare RooT
  71.         cbrt,       // CuBic RooT
  72.         round,      // ROUND float
  73.         floor,      // FLOOR float
  74.         ceil,       // CEIL float
  75.        
  76.         // Bitwise operators
  77.         lsr,
  78.         rsr,
  79.         band,
  80.         bor,
  81.         bxor,
  82.         bnot,
  83.        
  84.         // Value type conversion
  85.         cint = 80,      // Cast to INTeger (floors value)
  86.         cfloat,     // Cast to FLOATing point
  87.        
  88.         srgb = 100, // Send RGB color
  89.         srgba,      // Send RGBA color
  90.         suv,        // Send UV texture coords
  91.         svert,      // Send XYZ vertex coords
  92.        
  93.         ptime = 120,        // Push TIME value (float, in seconds) on TOS
  94.         ppar1,              // Push node's PARam 1 on TOS
  95.         ppar2,              // Push node's PARam 2 on TOS
  96.     };
  97.    
  98.     struct Val {
  99.         // This struct *must* be kept as POD struct as it is stored
  100.         // in a C-style array by TriangulatorVM::Memory
  101.         enum Type {
  102.             None,
  103.             Int,
  104.             Float
  105.         } type;
  106.         union {
  107.             IntType i;
  108.             FloatType f;
  109.         };
  110.        
  111.         void setI(IntType i);
  112.         void setF(FloatType f);
  113.     };
  114.     static Val make_valI(IntType i) {
  115.         Val val; val.setI(i); return val;
  116.     }
  117.     static Val make_valF(FloatType f) {
  118.         Val val; val.setF(f); return val;
  119.     }
  120.    
  121.     struct Instruction {
  122.         Instruction(Opcode op = nop) : opcode(op) {}
  123.         Opcode opcode;
  124.         std::vector<Val> args;
  125.        
  126.         bool is_valid() const;
  127.     };
  128.    
  129.     class Program : private std::vector<Instruction> {
  130.     public:
  131.         int memory_size_hint;
  132.        
  133.         Program();
  134.        
  135.         void save(std::ostream &os) const;
  136.         void load(std::istream &is);
  137.        
  138.         int size() const;
  139.         const Instruction& operator[] (const int idx) const;
  140.        
  141.         void emit(Opcode op);
  142.         void emit(Opcode op, Val arg0);
  143.         void emit(Opcode op, Val arg0, Val arg1);
  144.         void emit(Opcode op, Val arg0, Val arg1, Val arg2);
  145.         void emit(Instruction instr);
  146.     };
  147.    
  148.     class Memory {
  149.     protected:
  150.         Val *array;
  151.         int array_size;
  152.         int alloc_size_step;
  153.         void resize_array(int size);
  154.        
  155.     public:
  156.         Memory(int size = 64);
  157.         ~Memory();
  158.        
  159.         Val& operator[] (const int idx);
  160.         Val& operator[] (const int idx) const;
  161.         bool exists(const int idx) const;
  162.         int size() const;
  163.         void clear();
  164.        
  165.         void save(std::ostream &os) const;
  166.         void load(std::istream &is);
  167.     };
  168.    
  169.     class ValStack : public std::vector<Val> {
  170.     public:
  171.         void push(const Val &v) { push_back(v); }
  172.         Val pop() { Val v = back(); pop_back(); return v; }
  173.         const Val& top() const { return back(); }
  174.     };
  175.     class IntStack : public std::vector<int> {
  176.     public:
  177.         void push(const int &v) { push_back(v); }
  178.         int pop() { int v = back(); pop_back(); return v; }
  179.         const int& top() const { return back(); }
  180.     };
  181.    
  182.     struct ProgramState {
  183.         ProgramState() : pc(0) {}
  184.        
  185.         Memory mem;
  186.         ValStack stack;
  187.         IntStack callstack;
  188.         int pc;
  189.        
  190.         void save(std::ostream &os) const;
  191.         void load(std::istream &is);
  192.     };
  193.    
  194.     Program program;
  195.     ProgramState state;
  196.    
  197.     void saveVM(std::ostream &os) const;
  198.     void loadVM(std::istream &is);
  199.    
  200.     // Basically, those are saveVM()&loadVM() base64-encoded
  201.     void serialize(std::ostream &os) const;
  202.     void unserialize(std::istream &is);
  203.    
  204.     void run();
  205. };
  206.  
  207. typedef TriangulatorVM TVM;
  208.  
  209. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement