1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
/*
* Copyright (c) 2017, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include "cm_printf_base.h"
#include <string>
#define CM_PRINTF(f_, ...) fprintf((f_), __VA_ARGS__)
#define PRINT_BUFFER_HEADER_SIZE 32
#define PRINT_PAYLOAD_ALIGN 16
#define PRINT_HEADER_SIZE 32
#define PRINT_FORMAT_STRING_SIZE 128
#define CM_PRINT_SIZE_WITH_PAYLOAD(msize) (PRINT_HEADER_SIZE + (msize-1) / PRINT_PAYLOAD_ALIGN * PRINT_PAYLOAD_ALIGN + PRINT_PAYLOAD_ALIGN)
/// Structure of header:
/// vector<int, 8> header
/// [0]: Object type: matrix,vector,scalar,string, or format string.
/// [1]: Data type: [u]*int[136]*[6248]_t, float, or double.
/// [2]: width
/// [3]: height
/// [4]: tid
/// [5]: reserved3
/// [6]: Scalar lower 32bits: [u]*int[13]*[628]_t, float. Lower 32bits of double and [u]*int64_t.
/// [7]: Scalar upper 32bits: Upper 32bits of double and [u]*int64_t.
typedef struct _CM_PRINT_HEADER{
unsigned int objectType;
unsigned int dataType;
unsigned int width;
unsigned int height;
unsigned int tid;
unsigned int reserved3;
unsigned long long scalar64;
}CM_PRINT_HEADER, *PCM_PRINT_HEADER;
enum PRINT_FMT_STATUS
{
PF_FAIL = 0,
PF_SUCCESS = 1
};
// A small class used to contain the state machine used when parsing the printf string
// The issue we have to deal with is that there is a format string with some directives in it - the
// format string is received as an object from the client as an object in memory
// Any arguments are then passed as separate objects in memory. How many objects are required is
// determined by the format string.
// The cleanest way to deal with this is to use a very simple recursive descent parser on the format
// string in order to process the arguments in the correct way
// Here's the grammar for printf format strings (using EBNF). Only one format directive is to be
// returned from the input at a time:
//
// format:
// { STRING } directive
//
//
// directive:
// PERCENT flags { width } { PERIOD precision } { length_modifier } conversion
//
// flags:
// { MINUS } { PLUS } { SPACE } { ZERO } { HASH }
//
// width:
// INTEGER
// | STAR
//
// precision:
// INTEGER
// | STAR
//
// length_modifier:
// hh_MOD | h_MOD | l_MOD | ll_MOD | j_MOD | t_MOD | z_MOD | L_MOD
//
// conversion:
// PERCENT
// | c_CONV
// | s_CONV
// | d_CONV
// | i_CONV
// | o_CONV
// | x_CONV
// | X_CONV
// | u_CONV
// | f_CONV
// | F_CONV
// | e_CONV
// | E_CONV
// | a_CONV
// | A_CONV
// | g_CONV
// | G_CONV
// | n_CONV
// | p_CONV
//
// STRING : [any ASCII character that isn't a %]+
// PERCENT : '%'
// MINUS : '-'
// PLUS : '+'
// SPACE : ' '
// ZERO : '0'
// INTEGER : [
// PERIOD : '.'
// HASH : '#'
// hh_MOD : 'hh'
// h_MOD : 'h'
// l_MOD : 'l'
// ll_MOD : 'll'
// j_MOD : 'j'
// z_MOD : 'z'
// t_MOD : 't'
// L_MOD : 'L'
// STAR : '*'
// c_CONV : 'c'
// s_CONV : 's'
// d_CONV : 'd'
// i_CONV : 'i'
// o_CONV : 'o'
// x_CONV : 'x'
// X_CONV : 'X'
// u_CONV : 'u'
// f_CONV : 'f'
// F_CONV : 'F'
// e_CONV : 'e'
// E_CONV : 'E'
// a_CONV : 'a'
// A_CONV : 'A'
// g_CONV : 'g'
// G_CONV : 'G'
// n_CONV : 'n'
// p_CONV : 'p'
class PFParser
{
public:
PFParser(FILE* streamout) : m_inSpec(false), m_inputStart(nullptr), m_currLoc(nullptr), m_argsExpected(0),
m_numMultArg(0), m_unsupported(false), m_error(false), m_streamOut(streamout) {};
void SetStart(char *start)
{
m_inputStart= m_currLoc = start;
// Prime the system with the first token
getToken();
}
void DumpMemory(unsigned char * memory);
void Flush(void);
protected:
private:
class Token
{
public:
enum TokenType { _None_, Error,
String, Percent, Minus, Plus, Space, Zero, Integer, Period, Hash, Star,
hh_Mod, h_Mod, l_Mod, ll_Mod, j_Mod, z_Mod, t_Mod, L_Mod,
c_Conv, s_Conv, d_Conv, i_Conv, o_Conv, x_Conv, X_Conv, u_Conv, f_Conv,
F_Conv, e_Conv, E_Conv, a_Conv, A_Conv, g_Conv, G_Conv, n_Conv, p_Conv,
End
};
Token() : tokenType(_None_), tokenInt(0) {};
bool operator==(const Token &other) const {
return tokenType == other.tokenType;
}
bool operator==(const TokenType &other) const {
return tokenType == other;
}
bool operator!=(const Token &other) const {
return tokenType != other.tokenType;
}
bool operator!=(const TokenType &other) const {
return tokenType != other;
}
TokenType tokenType;
std::string tokenString;
int tokenInt;
};
bool m_inSpec; // Mode for lexer - in spec mode or not
Token m_currToken; // The currently lexed token
Token m_prevToken; // The previously lexed token
char *m_inputStart; // The start of the input string
char *m_currLoc; // The current point of processing
int m_argsExpected; // For multi-arg format directives - how many still to process
int m_numMultArg; // Total number of multi-arg format directives in total
int m_args[2]; // Up to 2 int args can be used in multi-arg format directives
bool m_unsupported; // Has the latest parsed format directive contained unsupported
// directives (VS doesn't support them all so we can't print them)
bool m_error; // Error in latest parsed format directive
FILE *m_streamOut; // Output stream for kernel print
PRINT_FMT_STATUS GetNextFmtToken(char * tkn, size_t size);
bool outputToken(const char *tkn, PCM_PRINT_HEADER header);
void getToken(void);
void reset(void)
{
m_inputStart = m_currLoc;
m_unsupported = false;
m_error = false;
m_numMultArg = m_argsExpected = 0;
}
void error()
{
// We don't throw an error in this case
// Just set the error flag
m_error = true;
}
bool accept(Token::TokenType s);
bool expect(Token::TokenType s);
int format(void); // This function returns the number of args for the next format 0,1,2 or 3
int directive(void);
void flags(void);
int width(void);
int precision(void);
void length_modifier(void);
int conversion(void);
};
void DumpAllThreadOutput( FILE *streamOut, unsigned char * dumpMem, size_t buffersize);
|