Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Minetest
- Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation; either version 2.1 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #ifndef TRIANGULATOR_VM_HEADER
- #define TRIANGULATOR_VM_HEADER
- #include <iostream>
- #include <vector>
- // Next line: should be <cstdint>, but we're not C++11 yet
- #include <stdint.h>
- class TriangulatorVM {
- public:
- const static int TVM_ASM_VERSION = 0;
- typedef uint64_t IntType;
- typedef double FloatType;
- enum Opcode {
- nop = 0, // No OPeration
- halt, // HALT vm
- trap, // debug TRAP
- dbgpv, // DeBuG Print Value
- dbgds, // DeBuG Dump Stack
- dbgdc, // DeBuG Dump Call stack
- push = 20, // PUSH value on TOS
- pop, // POP value from TOS
- dup, // DUPlicate TOS
- load, // LOAD from mem
- store, // STORE to mem
- call = 30, // CALL sub
- ret, // RETurn from call
- jmp = 40, // JUMP to
- jrel, // JUMP to realtive pc
- ifz, // IF Zero
- ifnz, // IF Not Zero
- ifeq, // IF Equal
- ifne, // IF Not Equal
- ifs, // IF Smaller than
- ifse, // IF Smaller or Equal
- ifg, // IF Greater than
- ifge, // IF Greater or Equal
- // Math operators. Resulting type is int only if
- // values used by op are all int; float otherwise
- add = 60, // ADD
- sub, // SUBstract
- mul, // MULtiply
- div, // DIVide
- inv, // INVerse
- sqrt, // SQuare RooT
- invsqrt, // INVerse SQuare RooT
- cbrt, // CuBic RooT
- round, // ROUND float
- floor, // FLOOR float
- ceil, // CEIL float
- // Bitwise operators
- lsr,
- rsr,
- band,
- bor,
- bxor,
- bnot,
- // Value type conversion
- cint = 80, // Cast to INTeger (floors value)
- cfloat, // Cast to FLOATing point
- srgb = 100, // Send RGB color
- srgba, // Send RGBA color
- suv, // Send UV texture coords
- svert, // Send XYZ vertex coords
- ptime = 120, // Push TIME value (float, in seconds) on TOS
- ppar1, // Push node's PARam 1 on TOS
- ppar2, // Push node's PARam 2 on TOS
- };
- struct Val {
- // This struct *must* be kept as POD struct as it is stored
- // in a C-style array by TriangulatorVM::Memory
- enum Type {
- None,
- Int,
- Float
- } type;
- union {
- IntType i;
- FloatType f;
- };
- void setI(IntType i);
- void setF(FloatType f);
- };
- static Val make_valI(IntType i) {
- Val val; val.setI(i); return val;
- }
- static Val make_valF(FloatType f) {
- Val val; val.setF(f); return val;
- }
- struct Instruction {
- Instruction(Opcode op = nop) : opcode(op) {}
- Opcode opcode;
- std::vector<Val> args;
- bool is_valid() const;
- };
- class Program : private std::vector<Instruction> {
- public:
- int memory_size_hint;
- Program();
- void save(std::ostream &os) const;
- void load(std::istream &is);
- int size() const;
- const Instruction& operator[] (const int idx) const;
- void emit(Opcode op);
- void emit(Opcode op, Val arg0);
- void emit(Opcode op, Val arg0, Val arg1);
- void emit(Opcode op, Val arg0, Val arg1, Val arg2);
- void emit(Instruction instr);
- };
- class Memory {
- protected:
- Val *array;
- int array_size;
- int alloc_size_step;
- void resize_array(int size);
- public:
- Memory(int size = 64);
- ~Memory();
- Val& operator[] (const int idx);
- Val& operator[] (const int idx) const;
- bool exists(const int idx) const;
- int size() const;
- void clear();
- void save(std::ostream &os) const;
- void load(std::istream &is);
- };
- class ValStack : public std::vector<Val> {
- public:
- void push(const Val &v) { push_back(v); }
- Val pop() { Val v = back(); pop_back(); return v; }
- const Val& top() const { return back(); }
- };
- class IntStack : public std::vector<int> {
- public:
- void push(const int &v) { push_back(v); }
- int pop() { int v = back(); pop_back(); return v; }
- const int& top() const { return back(); }
- };
- struct ProgramState {
- ProgramState() : pc(0) {}
- Memory mem;
- ValStack stack;
- IntStack callstack;
- int pc;
- void save(std::ostream &os) const;
- void load(std::istream &is);
- };
- Program program;
- ProgramState state;
- void saveVM(std::ostream &os) const;
- void loadVM(std::istream &is);
- // Basically, those are saveVM()&loadVM() base64-encoded
- void serialize(std::ostream &os) const;
- void unserialize(std::istream &is);
- void run();
- };
- typedef TriangulatorVM TVM;
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement