tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

direct_machine.cpp (5302B)


      1 /*  GRAPHITE2 LICENSING
      2 
      3    Copyright 2010, SIL International
      4    All rights reserved.
      5 
      6    This library is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU Lesser General Public License as published
      8    by the Free Software Foundation; either version 2.1 of License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14    Lesser General Public License for more details.
     15 
     16    You should also have received a copy of the GNU Lesser General Public
     17    License along with this library in the file named "LICENSE".
     18    If not, write to the Free Software Foundation, 51 Franklin Street,
     19    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
     20    internet at http://www.fsf.org/licenses/lgpl.html.
     21 
     22 Alternatively, the contents of this file may be used under the terms of the
     23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
     24 License, as published by the Free Software Foundation, either version 2
     25 of the License or (at your option) any later version.
     26 */
     27 // This direct threaded interpreter implmentation for machine.h
     28 // Author: Tim Eves
     29 
     30 // Build either this interpreter or the call_machine implementation.
     31 // The direct threaded interpreter is relies upon a gcc feature called
     32 // labels-as-values so is only portable to compilers that support the
     33 // extension (gcc only as far as I know) however it should build on any
     34 // architecture gcc supports.
     35 // This is twice as fast as the call threaded model and is likely faster on
     36 // inorder processors with short pipelines and little branch prediction such
     37 // as the ARM and possibly Atom chips.
     38 
     39 
     40 #include <cassert>
     41 #include <cstring>
     42 #include "inc/Machine.h"
     43 #include "inc/Segment.h"
     44 #include "inc/Slot.h"
     45 #include "inc/Rule.h"
     46 
     47 #define STARTOP(name)           name: {
     48 #define ENDOP                   }; goto *((sp - sb)/Machine::STACK_MAX ? &&end : *++ip);
     49 #define EXIT(status)            { push(status); goto end; }
     50 
     51 #define do_(name)               &&name
     52 
     53 
     54 using namespace graphite2;
     55 using namespace vm;
     56 
     57 namespace {
     58 
     59 // The GCC manual has this to say about labels as values:
     60 //   The &&foo expressions for the same label might have different values
     61 //   if the containing function is inlined or cloned. If a program relies
     62 //   on them being always the same, __attribute__((__noinline__,__noclone__))
     63 //   should be used to prevent inlining and cloning.
     64 //
     65 // is_return in Code.cpp relies on being able to do comparisons, so it needs
     66 // them to be always the same.
     67 //
     68 // The GCC manual further adds:
     69 //   If &&foo is used in a static variable initializer, inlining and
     70 //   cloning is forbidden.
     71 //
     72 // In this file, &&foo *is* used in a static variable initializer, and it's not
     73 // entirely clear whether this should prevent inlining of the function or not.
     74 // In practice, though, clang 7 can end up inlining the function with ThinLTO,
     75 // which breaks at least is_return. https://bugs.llvm.org/show_bug.cgi?id=39241
     76 // So all in all, we need at least the __noinline__ attribute. __noclone__
     77 // is not supported by clang.
     78 __attribute__((__noinline__))
     79 const void * direct_run(const bool          get_table_mode,
     80                        const instr       * program,
     81                        const byte        * data,
     82                        Machine::stack_t  * stack,
     83                        slotref         * & __map,
     84                        uint8                _dir,
     85                        Machine::status_t & status,
     86                        SlotMap           * __smap=0)
     87 {
     88    // We need to define and return to opcode table from within this function
     89    // other inorder to take the addresses of the instruction bodies.
     90    #include "inc/opcode_table.h"
     91    if (get_table_mode)
     92        return opcode_table;
     93 
     94    // Declare virtual machine registers
     95    const instr           * ip = program;
     96    const byte            * dp = data;
     97    Machine::stack_t      * sp = stack + Machine::STACK_GUARD,
     98                    * const sb = sp;
     99    SlotMap             & smap = *__smap;
    100    Segment              & seg = smap.segment;
    101    slotref                 is = *__map,
    102                         * map = __map,
    103                  * const mapb = smap.begin()+smap.context();
    104    uint8                  dir = _dir;
    105    int8                 flags = 0;
    106 
    107    // start the program
    108    goto **ip;
    109 
    110    // Pull in the opcode definitions
    111    #include "inc/opcodes.h"
    112 
    113    end:
    114    __map  = map;
    115    *__map = is;
    116    return sp;
    117 }
    118 
    119 }
    120 
    121 const opcode_t * Machine::getOpcodeTable() throw()
    122 {
    123    slotref * dummy;
    124    Machine::status_t dumstat = Machine::finished;
    125    return static_cast<const opcode_t *>(direct_run(true, 0, 0, 0, dummy, 0, dumstat));
    126 }
    127 
    128 
    129 Machine::stack_t  Machine::run(const instr   * program,
    130                               const byte    * data,
    131                               slotref     * & is)
    132 {
    133    assert(program != 0);
    134 
    135    const stack_t *sp = static_cast<const stack_t *>(
    136                direct_run(false, program, data, _stack, is, _map.dir(), _status, &_map));
    137    const stack_t ret = sp == _stack+STACK_GUARD+1 ? *sp-- : 0;
    138    check_final_stack(sp);
    139    return ret;
    140 }