Path: blob/master/thirdparty/graphite/src/direct_machine.cpp
9902 views
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later1// Copyright 2010, SIL International, All rights reserved.23// This direct threaded interpreter implmentation for machine.h4// Author: Tim Eves56// Build either this interpreter or the call_machine implementation.7// The direct threaded interpreter is relies upon a gcc feature called8// labels-as-values so is only portable to compilers that support the9// extension (gcc only as far as I know) however it should build on any10// architecture gcc supports.11// This is twice as fast as the call threaded model and is likely faster on12// inorder processors with short pipelines and little branch prediction such13// as the ARM and possibly Atom chips.141516#include <cassert>17#include <cstring>18#include "inc/Machine.h"19#include "inc/Segment.h"20#include "inc/Slot.h"21#include "inc/Rule.h"2223#define STARTOP(name) name: {24#define ENDOP }; goto *((sp - sb)/Machine::STACK_MAX ? &&end : *++ip);25#define EXIT(status) { push(status); goto end; }2627#define do_(name) &&name282930using namespace graphite2;31using namespace vm;3233namespace {3435// The GCC manual has this to say about labels as values:36// The &&foo expressions for the same label might have different values37// if the containing function is inlined or cloned. If a program relies38// on them being always the same, __attribute__((__noinline__,__noclone__))39// should be used to prevent inlining and cloning.40//41// is_return in Code.cpp relies on being able to do comparisons, so it needs42// them to be always the same.43//44// The GCC manual further adds:45// If &&foo is used in a static variable initializer, inlining and46// cloning is forbidden.47//48// In this file, &&foo *is* used in a static variable initializer, and it's not49// entirely clear whether this should prevent inlining of the function or not.50// In practice, though, clang 7 can end up inlining the function with ThinLTO,51// which breaks at least is_return. https://bugs.llvm.org/show_bug.cgi?id=3924152// So all in all, we need at least the __noinline__ attribute. __noclone__53// is not supported by clang.54__attribute__((__noinline__))55const void * direct_run(const bool get_table_mode,56const instr * program,57const byte * data,58Machine::stack_t * stack,59slotref * & __map,60uint8 _dir,61Machine::status_t & status,62SlotMap * __smap=0)63{64// We need to define and return to opcode table from within this function65// other inorder to take the addresses of the instruction bodies.66#include "inc/opcode_table.h"67if (get_table_mode)68return opcode_table;6970// Declare virtual machine registers71const instr * ip = program;72const byte * dp = data;73Machine::stack_t * sp = stack + Machine::STACK_GUARD,74* const sb = sp;75SlotMap & smap = *__smap;76Segment & seg = smap.segment;77slotref is = *__map,78* map = __map,79* const mapb = smap.begin()+smap.context();80uint8 dir = _dir;81int8 flags = 0;8283// start the program84goto **ip;8586// Pull in the opcode definitions87#include "inc/opcodes.h"8889end:90__map = map;91*__map = is;92return sp;93}9495}9697const opcode_t * Machine::getOpcodeTable() throw()98{99slotref * dummy;100Machine::status_t dumstat = Machine::finished;101return static_cast<const opcode_t *>(direct_run(true, 0, 0, 0, dummy, 0, dumstat));102}103104105Machine::stack_t Machine::run(const instr * program,106const byte * data,107slotref * & is)108{109assert(program != 0);110111const stack_t *sp = static_cast<const stack_t *>(112direct_run(false, program, data, _stack, is, _map.dir(), _status, &_map));113const stack_t ret = sp == _stack+STACK_GUARD+1 ? *sp-- : 0;114check_final_stack(sp);115return ret;116}117118119