michael@0: /* GRAPHITE2 LICENSING michael@0: michael@0: Copyright 2010, SIL International michael@0: All rights reserved. michael@0: michael@0: This library is free software; you can redistribute it and/or modify michael@0: it under the terms of the GNU Lesser General Public License as published michael@0: by the Free Software Foundation; either version 2.1 of License, or michael@0: (at your option) any later version. michael@0: michael@0: This program is distributed in the hope that it will be useful, michael@0: but WITHOUT ANY WARRANTY; without even the implied warranty of michael@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU michael@0: Lesser General Public License for more details. michael@0: michael@0: You should also have received a copy of the GNU Lesser General Public michael@0: License along with this library in the file named "LICENSE". michael@0: If not, write to the Free Software Foundation, 51 Franklin Street, michael@0: Suite 500, Boston, MA 02110-1335, USA or visit their web page on the michael@0: internet at http://www.fsf.org/licenses/lgpl.html. michael@0: michael@0: Alternatively, the contents of this file may be used under the terms of the michael@0: Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public michael@0: License, as published by the Free Software Foundation, either version 2 michael@0: of the License or (at your option) any later version. michael@0: */ michael@0: // This class represents loaded graphite stack machine code. It performs michael@0: // basic sanity checks, on the incoming code to prevent more obvious problems michael@0: // from crashing graphite. michael@0: // Author: Tim Eves michael@0: michael@0: #pragma once michael@0: michael@0: #include michael@0: #include michael@0: #include "inc/Main.h" michael@0: #include "inc/Machine.h" michael@0: michael@0: namespace graphite2 { michael@0: michael@0: class Silf; michael@0: class Face; michael@0: michael@0: namespace vm { michael@0: michael@0: class Machine::Code michael@0: { michael@0: public: michael@0: enum status_t michael@0: { michael@0: loaded, michael@0: alloc_failed, michael@0: invalid_opcode, michael@0: unimplemented_opcode_used, michael@0: out_of_range_data, michael@0: jump_past_end, michael@0: arguments_exhausted, michael@0: missing_return, michael@0: nested_context_item michael@0: }; michael@0: michael@0: private: michael@0: class decoder; michael@0: michael@0: instr * _code; michael@0: byte * _data; michael@0: size_t _data_size, michael@0: _instr_count; michael@0: byte _max_ref; michael@0: mutable status_t _status; michael@0: bool _constraint, michael@0: _modify, michael@0: _delete; michael@0: mutable bool _own; michael@0: michael@0: void release_buffers() throw (); michael@0: void failure(const status_t) throw(); michael@0: michael@0: public: michael@0: Code() throw(); michael@0: Code(bool is_constraint, const byte * bytecode_begin, const byte * const bytecode_end, michael@0: uint8 pre_context, uint16 rule_length, const Silf &, const Face &); michael@0: Code(const Machine::Code &) throw(); michael@0: ~Code() throw(); michael@0: michael@0: Code & operator=(const Code &rhs) throw(); michael@0: operator bool () const throw(); michael@0: status_t status() const throw(); michael@0: bool constraint() const throw(); michael@0: size_t dataSize() const throw(); michael@0: size_t instructionCount() const throw(); michael@0: bool immutable() const throw(); michael@0: bool deletes() const throw(); michael@0: size_t maxRef() const throw(); michael@0: michael@0: int32 run(Machine &m, slotref * & map) const; michael@0: michael@0: CLASS_NEW_DELETE; michael@0: }; michael@0: michael@0: inline Machine::Code::Code() throw() michael@0: : _code(0), _data(0), _data_size(0), _instr_count(0), _max_ref(0), michael@0: _status(loaded), _constraint(false), _modify(false),_delete(false), michael@0: _own(false) michael@0: { michael@0: } michael@0: michael@0: inline Machine::Code::Code(const Machine::Code &obj) throw () michael@0: : _code(obj._code), michael@0: _data(obj._data), michael@0: _data_size(obj._data_size), michael@0: _instr_count(obj._instr_count), michael@0: _max_ref(obj._max_ref), michael@0: _status(obj._status), michael@0: _constraint(obj._constraint), michael@0: _modify(obj._modify), michael@0: _delete(obj._delete), michael@0: _own(obj._own) michael@0: { michael@0: obj._own = false; michael@0: } michael@0: michael@0: inline Machine::Code & Machine::Code::operator=(const Machine::Code &rhs) throw() { michael@0: if (_instr_count > 0) michael@0: release_buffers(); michael@0: _code = rhs._code; michael@0: _data = rhs._data; michael@0: _data_size = rhs._data_size; michael@0: _instr_count = rhs._instr_count; michael@0: _status = rhs._status; michael@0: _constraint = rhs._constraint; michael@0: _modify = rhs._modify; michael@0: _delete = rhs._delete; michael@0: _own = rhs._own; michael@0: rhs._own = false; michael@0: return *this; michael@0: } michael@0: michael@0: inline Machine::Code::operator bool () const throw () { michael@0: return _code && status() == loaded; michael@0: } michael@0: michael@0: inline Machine::Code::status_t Machine::Code::status() const throw() { michael@0: return _status; michael@0: } michael@0: michael@0: inline bool Machine::Code::constraint() const throw() { michael@0: return _constraint; michael@0: } michael@0: michael@0: inline size_t Machine::Code::dataSize() const throw() { michael@0: return _data_size; michael@0: } michael@0: michael@0: inline size_t Machine::Code::instructionCount() const throw() { michael@0: return _instr_count; michael@0: } michael@0: michael@0: inline bool Machine::Code::immutable() const throw() michael@0: { michael@0: return !(_delete || _modify); michael@0: } michael@0: michael@0: inline bool Machine::Code::deletes() const throw() michael@0: { michael@0: return _delete; michael@0: } michael@0: michael@0: inline size_t Machine::Code::maxRef() const throw() michael@0: { michael@0: return _max_ref; michael@0: } michael@0: michael@0: } // namespace vm michael@0: } // namespace graphite2