Path: blob/a-new-beginning/SharedDependencies/Sources/nihstro/parser_assembly/floatop.cpp
2 views
// Copyright 2014 Tony Wasserka1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are met:5//6// * Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// * Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11// * Neither the name of the owner nor the names of its contributors may12// be used to endorse or promote products derived from this software13// without specific prior written permission.14//15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS16// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT17// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR18// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT19// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT21// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.262728// Enable this for detailed XML overview of parser results29// #define BOOST_SPIRIT_DEBUG3031#include <boost/fusion/include/adapt_struct.hpp>32#include <boost/phoenix/core/reference.hpp>33#include <boost/spirit/include/qi.hpp>3435#include "nihstro/parser_assembly.h"36#include "nihstro/parser_assembly_private.h"3738#include "nihstro/shader_binary.h"39#include "nihstro/shader_bytecode.h"4041namespace spirit = boost::spirit;42namespace qi = boost::spirit::qi;43namespace ascii = boost::spirit::qi::ascii;44namespace phoenix = boost::phoenix;4546using spirit::_1;47using spirit::_2;48using spirit::_3;49using spirit::_4;5051using namespace nihstro;5253// Adapt parser data structures for use with boost::spirit5455BOOST_FUSION_ADAPT_STRUCT(56StatementInstruction,57(OpCode, opcode)58(std::vector<Expression>, expressions)59)6061template<>62FloatOpParser<ParserIterator>::FloatOpParser(const ParserContext& context)63: FloatOpParser::base_type(float_instruction),64common(context),65opcodes_float(common.opcodes_float),66expression(common.expression),67end_of_statement(common.end_of_statement),68diagnostics(common.diagnostics) {6970// Setup rules7172auto comma_rule = qi::lit(',');7374for (int i = 0; i < 4; ++i) {75// Make sure that a mnemonic is always followed by a space (such that e.g. "addbla" fails to match)76opcode[i] = qi::no_case[qi::lexeme[opcodes_float[i] >> &ascii::space]];77}7879// chain of arguments for each group of opcodes80expression_chain[0] = expression;81for (int i = 1; i < 4; ++i) {82expression_chain[i] = (expression_chain[i - 1] >> comma_rule) > expression;83}8485// e.g. "add o1, t2, t5"86float_instr[0] = opcode[0] > expression_chain[0];87float_instr[1] = opcode[1] > expression_chain[1];88float_instr[2] = opcode[2] > expression_chain[2];89float_instr[3] = opcode[3] > expression_chain[3];9091float_instruction %= (float_instr[0] | float_instr[1] | float_instr[2] | float_instr[3]) > end_of_statement;9293// Error handling94// BOOST_SPIRIT_DEBUG_NODE(opcode[0]);95// BOOST_SPIRIT_DEBUG_NODE(opcode[1]);96// BOOST_SPIRIT_DEBUG_NODE(opcode[2]);97// BOOST_SPIRIT_DEBUG_NODE(opcode[3]);9899// BOOST_SPIRIT_DEBUG_NODE(expression_chain[0]);100// BOOST_SPIRIT_DEBUG_NODE(expression_chain[1]);101// BOOST_SPIRIT_DEBUG_NODE(expression_chain[2]);102// BOOST_SPIRIT_DEBUG_NODE(expression_chain[3]);103104// BOOST_SPIRIT_DEBUG_NODE(float_instr[0]);105// BOOST_SPIRIT_DEBUG_NODE(float_instr[1]);106// BOOST_SPIRIT_DEBUG_NODE(float_instr[2]);107// BOOST_SPIRIT_DEBUG_NODE(float_instr[3]);108// BOOST_SPIRIT_DEBUG_NODE(float_instruction);109110diagnostics.Add(expression_chain[0].name(), "one argument");111diagnostics.Add(expression_chain[1].name(), "two arguments");112diagnostics.Add(expression_chain[2].name(), "three arguments");113diagnostics.Add(expression_chain[3].name(), "four arguments");114115// qi::on_error<qi::fail>(float_instruction, error_handler(phoenix::ref(diagnostics), _1, _2, _3, _4));116}117118119