[692] | 1 | %start VS10Program |
---|
| 2 | %name-prefix="vs10_" |
---|
| 3 | %{ |
---|
| 4 | void yyerror(char *s); |
---|
| 5 | int yylex(void); |
---|
| 6 | |
---|
| 7 | #include <math.h> |
---|
| 8 | #include <string> |
---|
| 9 | |
---|
| 10 | #include <stdlib.h> |
---|
| 11 | #include "vs1.0_inst_list.h" |
---|
| 12 | #include "nvparse_errors.h" |
---|
| 13 | #include "nvparse_externs.h" |
---|
| 14 | |
---|
| 15 | //extern bool gbTempInsideMacro; |
---|
| 16 | //extern unsigned int &base_linenumber; |
---|
| 17 | void LexError(char *format, ...); |
---|
| 18 | extern int line_incr; |
---|
| 19 | |
---|
| 20 | #define do_linenum_incr() { line_number+=line_incr; line_incr = 0; } |
---|
| 21 | //int get_linenum() { return( gbTempInsideMacro ? base_linenumber : line_number ); } |
---|
| 22 | int get_linenum() { return( line_number ); } |
---|
| 23 | |
---|
| 24 | #define YYDEBUG 1 |
---|
| 25 | |
---|
| 26 | %} |
---|
| 27 | %union { |
---|
| 28 | int ival; |
---|
| 29 | unsigned int lval; |
---|
| 30 | float fval; |
---|
| 31 | char mask[4]; |
---|
| 32 | char *comment; |
---|
| 33 | VS10Reg reg; |
---|
| 34 | VS10InstPtr inst; |
---|
| 35 | VS10InstListPtr instList; |
---|
| 36 | }; |
---|
| 37 | |
---|
| 38 | %token VERTEX_SHADER |
---|
| 39 | |
---|
| 40 | %token ADD_INSTR |
---|
| 41 | %token DP3_INSTR |
---|
| 42 | %token DP4_INSTR |
---|
| 43 | %token DST_INSTR |
---|
| 44 | %token EXP_INSTR |
---|
| 45 | %token EXPP_INSTR |
---|
| 46 | %token FRC_INSTR |
---|
| 47 | %token LIT_INSTR |
---|
| 48 | %token LOG_INSTR |
---|
| 49 | %token LOGP_INSTR |
---|
| 50 | %token M3X2_INSTR |
---|
| 51 | %token M3X3_INSTR |
---|
| 52 | %token M3X4_INSTR |
---|
| 53 | %token M4X3_INSTR |
---|
| 54 | %token M4X4_INSTR |
---|
| 55 | %token MAD_INSTR |
---|
| 56 | %token MAX_INSTR |
---|
| 57 | %token MIN_INSTR |
---|
| 58 | %token MOV_INSTR |
---|
| 59 | %token MUL_INSTR |
---|
| 60 | %token NOP_INSTR |
---|
| 61 | %token RCP_INSTR |
---|
| 62 | %token RSQ_INSTR |
---|
| 63 | %token SGE_INSTR |
---|
| 64 | %token SLT_INSTR |
---|
| 65 | %token SUB_INSTR |
---|
| 66 | |
---|
| 67 | %token ILLEGAL |
---|
| 68 | %token UNKNOWN_STRING |
---|
| 69 | |
---|
| 70 | %token <ival> INTVAL |
---|
| 71 | %token <reg> REGISTER |
---|
| 72 | %token <mask> XYZW_MODIFIER |
---|
| 73 | %token <comment> COMMENT |
---|
| 74 | |
---|
| 75 | %type <instList> InstSequence |
---|
| 76 | %type <inst> InstLine |
---|
| 77 | %type <inst> Instruction |
---|
| 78 | %type <inst> VECTORopinstruction |
---|
| 79 | %type <inst> SCALARopinstruction |
---|
| 80 | %type <inst> UNARYopinstruction |
---|
| 81 | %type <inst> BINopinstruction |
---|
| 82 | %type <inst> TRIopinstruction |
---|
| 83 | %type <ival> VECTORop |
---|
| 84 | %type <ival> SCALARop |
---|
| 85 | %type <ival> UNARYop |
---|
| 86 | %type <ival> BINop |
---|
| 87 | %type <ival> TRIop |
---|
| 88 | |
---|
| 89 | %type <reg> genericReg |
---|
| 90 | %type <reg> genReg |
---|
| 91 | %type <reg> constantReg |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | %% /*beginning of rules section */ |
---|
| 95 | |
---|
| 96 | VS10Program: InstSequence |
---|
| 97 | { |
---|
| 98 | $1->Validate(); |
---|
| 99 | $1->Translate(); |
---|
| 100 | delete $1; |
---|
| 101 | } |
---|
| 102 | ; |
---|
| 103 | |
---|
| 104 | InstSequence: InstSequence InstLine |
---|
| 105 | { |
---|
| 106 | *($1) += $2; |
---|
| 107 | delete $2; |
---|
| 108 | $$ = $1 |
---|
| 109 | } |
---|
| 110 | | InstLine |
---|
| 111 | { |
---|
| 112 | VS10InstListPtr instList = new VS10InstList; |
---|
| 113 | if ( $1 != NULL ) |
---|
| 114 | { |
---|
| 115 | *instList += $1; |
---|
| 116 | delete $1; |
---|
| 117 | } |
---|
| 118 | $$ = instList; |
---|
| 119 | } |
---|
| 120 | ; |
---|
| 121 | |
---|
| 122 | InstLine: Instruction |
---|
| 123 | { |
---|
| 124 | $$ = $1; |
---|
| 125 | do_linenum_incr(); |
---|
| 126 | } |
---|
| 127 | | '\n' |
---|
| 128 | { |
---|
| 129 | $$ = new VS10Inst( get_linenum() ); |
---|
| 130 | do_linenum_incr(); |
---|
| 131 | } |
---|
| 132 | ; |
---|
| 133 | |
---|
| 134 | Instruction: VECTORopinstruction |
---|
| 135 | | SCALARopinstruction |
---|
| 136 | | UNARYopinstruction |
---|
| 137 | | BINopinstruction |
---|
| 138 | | TRIopinstruction |
---|
| 139 | | NOP_INSTR |
---|
| 140 | { |
---|
| 141 | $$ = new VS10Inst( get_linenum(), VS10_NOP ); |
---|
| 142 | } |
---|
| 143 | | COMMENT |
---|
| 144 | { |
---|
| 145 | $$ = new VS10Inst( get_linenum(), VS10_COMMENT, $1 ); |
---|
| 146 | } |
---|
| 147 | | VERTEX_SHADER |
---|
| 148 | { |
---|
| 149 | $$ = new VS10Inst( get_linenum(), VS10_HEADER ); |
---|
| 150 | } |
---|
| 151 | ; |
---|
| 152 | |
---|
| 153 | VECTORopinstruction: VECTORop genericReg ',' genericReg |
---|
| 154 | { |
---|
| 155 | $$ = new VS10Inst( get_linenum(), $1, $2, $4 ); |
---|
| 156 | } |
---|
| 157 | ; |
---|
| 158 | |
---|
| 159 | genericReg : '-' genReg '.' XYZW_MODIFIER |
---|
| 160 | { |
---|
| 161 | VS10Reg reg; |
---|
| 162 | reg = $2; |
---|
| 163 | reg.sign = -1; |
---|
| 164 | reg.type = $2.type; |
---|
| 165 | reg.index = $2.index; |
---|
| 166 | for ( int i = 0; i < 4; i++ ) reg.mask[i] = $4[i]; |
---|
| 167 | $$ = reg; |
---|
| 168 | } |
---|
| 169 | | genReg '.' XYZW_MODIFIER |
---|
| 170 | { |
---|
| 171 | VS10Reg reg; |
---|
| 172 | reg = $1; |
---|
| 173 | reg.sign = 1; |
---|
| 174 | reg.type = $1.type; |
---|
| 175 | reg.index = $1.index; |
---|
| 176 | for ( int i = 0; i < 4; i++ ) reg.mask[i] = $3[i]; |
---|
| 177 | $$ = reg; |
---|
| 178 | } |
---|
| 179 | | '-' genReg |
---|
| 180 | { |
---|
| 181 | VS10Reg reg; |
---|
| 182 | reg = $2; |
---|
| 183 | reg.sign = -1; |
---|
| 184 | reg.type = $2.type; |
---|
| 185 | reg.index = $2.index; |
---|
| 186 | for ( int i = 0; i < 4; i++ ) reg.mask[i] = 0; |
---|
| 187 | $$ = reg; |
---|
| 188 | } |
---|
| 189 | | genReg |
---|
| 190 | { |
---|
| 191 | VS10Reg reg; |
---|
| 192 | reg = $1; |
---|
| 193 | reg.sign = 1; |
---|
| 194 | reg.type = $1.type; |
---|
| 195 | reg.index = $1.index; |
---|
| 196 | for ( int i = 0; i < 4; i++ ) reg.mask[i] = 0; |
---|
| 197 | $$ = reg; |
---|
| 198 | } |
---|
| 199 | ; |
---|
| 200 | genReg: REGISTER | constantReg |
---|
| 201 | { |
---|
| 202 | } |
---|
| 203 | ; |
---|
| 204 | constantReg: 'c' '[' INTVAL ']' |
---|
| 205 | { |
---|
| 206 | VS10Reg reg; |
---|
| 207 | reg.type = TYPE_CONSTANT_MEM_REG; |
---|
| 208 | reg.index = $3; |
---|
| 209 | $$ = reg; |
---|
| 210 | } |
---|
| 211 | | 'c' '[' REGISTER '.' XYZW_MODIFIER ']' |
---|
| 212 | { |
---|
| 213 | // Register is valid only if |
---|
| 214 | // type = TYPE_ADDRESS_REG |
---|
| 215 | // index = 0 |
---|
| 216 | // len(mask) = 1 |
---|
| 217 | // mask[0] = 'x' |
---|
| 218 | VS10Reg reg; |
---|
| 219 | $$.type = TYPE_CONSTANT_A0_REG; |
---|
| 220 | if ( $3.type != TYPE_ADDRESS_REG ) |
---|
| 221 | { |
---|
| 222 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 223 | } |
---|
| 224 | else if ( $3.index != 0 ) |
---|
| 225 | { |
---|
| 226 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 227 | } |
---|
| 228 | else |
---|
| 229 | { |
---|
| 230 | int len = 0; |
---|
| 231 | while ( len < 2 ) |
---|
| 232 | { |
---|
| 233 | if ( $5[len] == 0 ) |
---|
| 234 | break; |
---|
| 235 | len++; |
---|
| 236 | } |
---|
| 237 | if ( len != 1 || $5[0] != 'x' ) |
---|
| 238 | { |
---|
| 239 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | reg.type = TYPE_CONSTANT_A0_REG; |
---|
| 243 | $$ = reg; |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | | 'c' '[' REGISTER '.' XYZW_MODIFIER '+' INTVAL ']' |
---|
| 247 | { |
---|
| 248 | // Register is valid only if |
---|
| 249 | // type = TYPE_ADDRESS_REG |
---|
| 250 | // index = 0 |
---|
| 251 | // len(mask) = 1 |
---|
| 252 | // mask[0] = 'x' |
---|
| 253 | VS10Reg reg; |
---|
| 254 | $$.type = TYPE_CONSTANT_A0_OFFSET_REG; |
---|
| 255 | if ( $3.type != TYPE_ADDRESS_REG ) |
---|
| 256 | { |
---|
| 257 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 258 | } |
---|
| 259 | else if ( $3.index != 0 ) |
---|
| 260 | { |
---|
| 261 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
| 265 | int len = 0; |
---|
| 266 | while ( len < 2 ) |
---|
| 267 | { |
---|
| 268 | if ( $5[len] == 0 ) |
---|
| 269 | break; |
---|
| 270 | len++; |
---|
| 271 | } |
---|
| 272 | if ( len != 1 || $5[0] != 'x' ) |
---|
| 273 | { |
---|
| 274 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | reg.type = TYPE_CONSTANT_A0_OFFSET_REG; |
---|
| 278 | reg.index = $7; |
---|
| 279 | $$ = reg; |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | | 'c' '[' REGISTER ']' |
---|
| 283 | { |
---|
| 284 | $$.type = TYPE_CONSTANT_A0_REG; |
---|
| 285 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 286 | } |
---|
| 287 | | 'c' '[' REGISTER '+' INTVAL ']' |
---|
| 288 | { |
---|
| 289 | $$.type = TYPE_CONSTANT_A0_REG; |
---|
| 290 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 291 | } |
---|
| 292 | | 'c' '[' '-' REGISTER ']' |
---|
| 293 | { |
---|
| 294 | $$.type = TYPE_CONSTANT_A0_REG; |
---|
| 295 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 296 | } |
---|
| 297 | | 'c' '[' '-' REGISTER '+' INTVAL ']' |
---|
| 298 | { |
---|
| 299 | $$.type = TYPE_CONSTANT_A0_REG; |
---|
| 300 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 301 | } |
---|
| 302 | | 'c' '[' '-' REGISTER '.' XYZW_MODIFIER ']' |
---|
| 303 | { |
---|
| 304 | $$.type = TYPE_CONSTANT_A0_REG; |
---|
| 305 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 306 | } |
---|
| 307 | | 'c' '[' '-' REGISTER '.' XYZW_MODIFIER '+' INTVAL ']' |
---|
| 308 | { |
---|
| 309 | $$.type = TYPE_CONSTANT_A0_REG; |
---|
| 310 | LexError( "constant register index must be:\t<int>, a0.x, or a0.x + <int>.\n" ); |
---|
| 311 | } |
---|
| 312 | ; |
---|
| 313 | |
---|
| 314 | SCALARopinstruction: SCALARop genericReg ',' genericReg |
---|
| 315 | { |
---|
| 316 | $$ = new VS10Inst( get_linenum(), $1, $2, $4 ); |
---|
| 317 | } |
---|
| 318 | ; |
---|
| 319 | |
---|
| 320 | UNARYopinstruction: UNARYop genericReg ',' genericReg |
---|
| 321 | { |
---|
| 322 | $$ = new VS10Inst( get_linenum(), $1, $2, $4 ); |
---|
| 323 | } |
---|
| 324 | ; |
---|
| 325 | |
---|
| 326 | BINopinstruction: BINop genericReg ',' genericReg ',' genericReg |
---|
| 327 | { |
---|
| 328 | $$ = new VS10Inst( get_linenum(), $1, $2, $4, $6 ); |
---|
| 329 | } |
---|
| 330 | ; |
---|
| 331 | |
---|
| 332 | TRIopinstruction: TRIop genericReg ',' |
---|
| 333 | genericReg ',' genericReg ',' genericReg |
---|
| 334 | { |
---|
| 335 | $$ = new VS10Inst( get_linenum(), $1, $2, $4, $6, $8 ); |
---|
| 336 | } |
---|
| 337 | ; |
---|
| 338 | |
---|
| 339 | VECTORop: MOV_INSTR |
---|
| 340 | { |
---|
| 341 | $$ = VS10_MOV; |
---|
| 342 | } |
---|
| 343 | | LIT_INSTR |
---|
| 344 | { |
---|
| 345 | $$ = VS10_LIT; |
---|
| 346 | } |
---|
| 347 | ; |
---|
| 348 | |
---|
| 349 | SCALARop: RCP_INSTR |
---|
| 350 | { |
---|
| 351 | $$ = VS10_RCP; |
---|
| 352 | } |
---|
| 353 | | RSQ_INSTR |
---|
| 354 | { |
---|
| 355 | $$ = VS10_RSQ; |
---|
| 356 | } |
---|
| 357 | | EXP_INSTR |
---|
| 358 | { |
---|
| 359 | $$ = VS10_EXP; |
---|
| 360 | } |
---|
| 361 | | EXPP_INSTR |
---|
| 362 | { |
---|
| 363 | $$ = VS10_EXPP; |
---|
| 364 | } |
---|
| 365 | | LOG_INSTR |
---|
| 366 | { |
---|
| 367 | $$ = VS10_LOG; |
---|
| 368 | } |
---|
| 369 | | LOGP_INSTR |
---|
| 370 | { |
---|
| 371 | $$ = VS10_LOGP; |
---|
| 372 | } |
---|
| 373 | ; |
---|
| 374 | |
---|
| 375 | UNARYop: FRC_INSTR |
---|
| 376 | { |
---|
| 377 | $$ = VS10_FRC; |
---|
| 378 | } |
---|
| 379 | ; |
---|
| 380 | |
---|
| 381 | BINop: MUL_INSTR |
---|
| 382 | { |
---|
| 383 | $$ = VS10_MUL; |
---|
| 384 | } |
---|
| 385 | | ADD_INSTR |
---|
| 386 | { |
---|
| 387 | $$ = VS10_ADD; |
---|
| 388 | } |
---|
| 389 | | DP3_INSTR |
---|
| 390 | { |
---|
| 391 | $$ = VS10_DP3; |
---|
| 392 | } |
---|
| 393 | | DP4_INSTR |
---|
| 394 | { |
---|
| 395 | $$ = VS10_DP4; |
---|
| 396 | } |
---|
| 397 | | DST_INSTR |
---|
| 398 | { |
---|
| 399 | $$ = VS10_DST; |
---|
| 400 | } |
---|
| 401 | | MIN_INSTR |
---|
| 402 | { |
---|
| 403 | $$ = VS10_MIN; |
---|
| 404 | } |
---|
| 405 | | MAX_INSTR |
---|
| 406 | { |
---|
| 407 | $$ = VS10_MAX; |
---|
| 408 | } |
---|
| 409 | | SLT_INSTR |
---|
| 410 | { |
---|
| 411 | $$ = VS10_SLT; |
---|
| 412 | } |
---|
| 413 | | SGE_INSTR |
---|
| 414 | { |
---|
| 415 | $$ = VS10_SGE; |
---|
| 416 | } |
---|
| 417 | | M3X2_INSTR |
---|
| 418 | { |
---|
| 419 | $$ = VS10_M3X2; |
---|
| 420 | } |
---|
| 421 | | M3X3_INSTR |
---|
| 422 | { |
---|
| 423 | $$ = VS10_M3X3; |
---|
| 424 | } |
---|
| 425 | | M3X4_INSTR |
---|
| 426 | { |
---|
| 427 | $$ = VS10_M3X4; |
---|
| 428 | } |
---|
| 429 | | M4X3_INSTR |
---|
| 430 | { |
---|
| 431 | $$ = VS10_M4X3; |
---|
| 432 | } |
---|
| 433 | | M4X4_INSTR |
---|
| 434 | { |
---|
| 435 | $$ = VS10_M4X4; |
---|
| 436 | } |
---|
| 437 | | SUB_INSTR |
---|
| 438 | { |
---|
| 439 | $$ = VS10_SUB; |
---|
| 440 | } |
---|
| 441 | ; |
---|
| 442 | |
---|
| 443 | TRIop: MAD_INSTR |
---|
| 444 | { |
---|
| 445 | $$ = VS10_MAD; |
---|
| 446 | } |
---|
| 447 | ; |
---|
| 448 | |
---|
| 449 | |
---|
| 450 | %% |
---|
| 451 | void yyerror(char* s) |
---|
| 452 | { |
---|
| 453 | LexError( "Syntax Error.\n" ); |
---|
| 454 | //errors.set(s); |
---|
| 455 | //errors.set("unrecognized token"); |
---|
| 456 | } |
---|