OpenCores
URL https://opencores.org/ocsvn/theia_gpu/theia_gpu/trunk

Subversion Repositories theia_gpu

[/] [theia_gpu/] [branches/] [new_compiler/] [parser.y] - Blame information for rev 208

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 208 diegovalve
 
2
/**********************************************************************************
3
Theia, Ray Cast Programable graphic Processing Unit.
4
Copyright (C) 2012  Diego Valverde (diego.valverde.g@gmail.com)
5
 
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of the GNU General Public License
8
as published by the Free Software Foundation; either version 2
9
of the License, or (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
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
 
20
***********************************************************************************/
21
 
22
 
23
%require "2.4.1"
24
%skeleton "lalr1.cc"
25
%defines
26
%error-verbose
27
%locations
28
%define namespace "Theia"
29
%define parser_class_name "Parser"
30
%parse-param { Theia::Scanner &scanner }
31
%parse-param { std::map  & mSymbolMap }
32
%parse-param { std::vector< Instruction > &mInstructions }
33
%parse-param { bool &mGenerateFixedPointArithmetic }
34
%lex-param   { Theia::Scanner &scanner }
35
 
36
%code requires {
37
        #include 
38
        #include 
39
        #include 
40
        #include 
41
        #include 
42
        #include "Instruction.h"
43
        #include 
44
 
45
 
46
        // We want to return a string
47
        #define YYSTYPE std::string
48
 
49
 
50
                namespace Theia
51
                {
52
                        // Forward-declare the Scanner class; the Parser needs to be assigned a
53
                        // Scanner, but the Scanner can't be declared without the Parser
54
                        class Scanner;
55
 
56
                        // We use a map to store the INI data
57
                        typedef std::map > mapData;
58
 
59
 
60
 
61
 
62
 
63
                }
64
 
65
}
66
 
67
%code
68
{
69
        #include "Instruction.h"
70
        #include 
71
        Instruction I,tmpI;
72
 
73
        std::vector< unsigned int > gBranchStack;
74
        static int gInsertedInstructions = 0;
75
#define FUNCTION_PARAM_START_REGION 2
76
#define FUNCTION_PARAM_LAST_REGION  7
77
        std::map gFunctionParameters;
78
        std::map gAutoVarMap;
79
#define AUTOVAR_START_REGION 8
80
unsigned int gAutoVarIndex = AUTOVAR_START_REGION;
81
unsigned int gFunctionParameterIndex = FUNCTION_PARAM_START_REGION;
82
//----------------------------------------------------------
83
unsigned int GetNextFunctionParamRegister()
84
{
85
        unsigned Ret = gFunctionParameterIndex++;
86
        return Ret;
87
}
88
 
89
//----------------------------------------------------------
90
void AddFunctionParameter( std::string aVar, Theia::Parser::location_type  yylloc)
91
{
92
        //std::cout << "Adding " << aVar << "\n";
93
        if (gFunctionParameterIndex+1 > FUNCTION_PARAM_LAST_REGION)
94
        {
95
                        std::ostringstream ret;
96
                        ret << "Cannot allocate moere parameters '" << aVar << "' at line " << yylloc << " \n";
97
                        throw ret.str();
98
        }
99
        if (gFunctionParameters.find(aVar) != gFunctionParameters.end())
100
        {
101
                        std::ostringstream ret;
102
                        ret << "Parameter '" << aVar << "' at line " << yylloc << " is already defined\n";
103
                        throw ret.str();
104
        }
105
 
106
        gFunctionParameters[ aVar ] = gFunctionParameterIndex++;
107
 
108
}
109
//----------------------------------------------------------
110
std::string  GetRegisterFromFunctionParameter( std::string aVar )
111
{
112
        //std::cout << "Looking for " << aVar << "\n";
113
        if (gFunctionParameters.find(aVar) == gFunctionParameters.end())
114
                return "NULL";
115
 
116
        std::ostringstream ss;
117
        ss << gFunctionParameters[ aVar ];
118
        return  ("R" + ss.str());
119
}
120
//----------------------------------------------------------
121
unsigned int GetCurretAutoVarFrameSize()
122
{
123
 
124
        return gAutoVarMap.size();
125
 
126
}
127
//----------------------------------------------------------
128
std::string GetRegisterFromAutoVar( std::string aVar, Theia::Parser::location_type  yylloc )
129
{
130
        if (gAutoVarMap.find(aVar) == gAutoVarMap.end())
131
        {
132
                        std::ostringstream ret;
133
                        ret << "Undefined variable '" << aVar << "' at line " << yylloc << " \n";
134
                        throw ret.str();
135
        }
136
 
137
                std::ostringstream ss;
138
                ss << gAutoVarMap[ aVar ];
139
                return  ("R" + ss.str());
140
}
141
//----------------------------------------------------------
142
int GetCurrentLineNumber( const Theia::Parser::location_type &loc )
143
{
144
                int ret = -1;
145
                std::stringstream ss2;
146
                std::string where;
147
                ss2 << loc;
148
                ss2 >> where;
149
                where.erase(where.find_first_of("."));
150
                std::stringstream ss3;
151
                ss3 << where;
152
                ss3 >> ret;
153
                return ret;
154
}
155
//----------------------------------------------------------
156
unsigned int GetAutoVarIndex()
157
{
158
        //std::cout << gAutoVarIndex+1 << "\n";
159
        return gAutoVarIndex++;
160
}
161
//----------------------------------------------------------
162
void ClearFunctionParameterMap()
163
{
164
        gFunctionParameters.clear();
165
        gFunctionParameterIndex = FUNCTION_PARAM_START_REGION;
166
}
167
//----------------------------------------------------------
168
void ClearAutoVarMap()
169
{
170
        gAutoVarMap.clear();
171
        gAutoVarIndex = AUTOVAR_START_REGION;
172
}
173
//----------------------------------------------------------
174
unsigned int gTempRegisterIndex = 32;
175
unsigned int GetFreeTempRegister()
176
{
177
        return gTempRegisterIndex++;
178
}
179
//----------------------------------------------------------
180
void ResetTempRegisterIndex( void )
181
{
182
        gTempRegisterIndex = 32;
183
}
184
//----------------------------------------------------------
185
bool IsSwizzled( std::string aSource)
186
{
187
        if (aSource.find(".") != std::string::npos)
188
                return true;
189
        else
190
                return false;
191
}
192
 
193
//----------------------------------------------------------
194
void SetSwizzleAndSign( unsigned int aSourceIndex, std::string aSwizzle, Instruction & I )
195
{
196
        std::string Reg,X,Y,Z;
197
        std::stringstream ss( aSwizzle );
198
        ss >> Reg >> X >> Y >> Z;
199
        //std::cout << X << " " << Y << " " << Z << "\n";
200
 
201
        if (aSourceIndex == 1)
202
        {
203
                if (X == "X") { I.SetSrc1SwizzleX(SWX_X);       }
204
                if (X == "Y") { I.SetSrc1SwizzleX(SWX_Y);       }
205
                if (X == "Z") { I.SetSrc1SwizzleX(SWX_Z);       }
206
                if (X == "-X") { I.SetSrc1SignX( true ); I.SetSrc1SwizzleX(SWX_X);      }
207
                if (X == "-Y") { I.SetSrc1SignX( true ); I.SetSrc1SwizzleX(SWX_Y);      }
208
                if (X == "-Z") { I.SetSrc1SignX( true ); I.SetSrc1SwizzleX(SWX_Z);      }
209
 
210
                if (Y == "X") { I.SetSrc1SwizzleY(SWY_X);       }
211
                if (Y == "Y") { I.SetSrc1SwizzleY(SWY_Y);       }
212
                if (Y == "Z") { I.SetSrc1SwizzleY(SWY_Z);       }
213
                if (Y == "-X") { I.SetSrc1SignY( true ); I.SetSrc1SwizzleY(SWY_X);      }
214
                if (Y == "-Y") { I.SetSrc1SignY( true ); I.SetSrc1SwizzleY(SWY_Y);      }
215
                if (Y == "-Z") { I.SetSrc1SignY( true ); I.SetSrc1SwizzleY(SWY_Z);      }
216
 
217
                if (Z == "X") { I.SetSrc1SwizzleZ(SWZ_X);       }
218
                if (Z == "Y") { I.SetSrc1SwizzleZ(SWZ_Y);       }
219
                if (Z == "Z") { I.SetSrc1SwizzleZ(SWZ_Z);       }
220
                if (Z == "-X") { I.SetSrc1SignZ( true ); I.SetSrc1SwizzleZ(SWZ_X);      }
221
                if (Z == "-Y") { I.SetSrc1SignZ( true ); I.SetSrc1SwizzleZ(SWZ_Y);      }
222
                if (Z == "-Z") { I.SetSrc1SignZ( true ); I.SetSrc1SwizzleZ(SWZ_Z);      }
223
        } else {
224
                if (X == "X") { I.SetSrc0SwizzleX(SWX_X);       }
225
                if (X == "Y") { I.SetSrc0SwizzleX(SWX_Y);       }
226
                if (X == "Z") { I.SetSrc0SwizzleX(SWX_Z);       }
227
                if (X == "-X") { I.SetSrc0SignX( true ); I.SetSrc0SwizzleX(SWX_X);      }
228
                if (X == "-Y") { I.SetSrc0SignX( true ); I.SetSrc0SwizzleX(SWX_Y);      }
229
                if (X == "-Z") { I.SetSrc0SignX( true ); I.SetSrc0SwizzleX(SWX_Z);      }
230
 
231
                if (Y == "X") { I.SetSrc0SwizzleY(SWY_X);       }
232
                if (Y == "Y") { I.SetSrc0SwizzleY(SWY_Y);       }
233
                if (Y == "Z") { I.SetSrc0SwizzleY(SWY_Z);       }
234
                if (Y == "-X") { I.SetSrc0SignY( true ); I.SetSrc0SwizzleY(SWY_X);      }
235
                if (Y == "-Y") { I.SetSrc0SignY( true ); I.SetSrc0SwizzleY(SWY_Y);      }
236
                if (Y == "-Z") { I.SetSrc0SignY( true ); I.SetSrc0SwizzleY(SWY_Z);      }
237
 
238
                if (Z == "X") { I.SetSrc0SwizzleZ(SWZ_X);       }
239
                if (Z == "Y") { I.SetSrc0SwizzleZ(SWZ_Y);       }
240
                if (Z == "Z") { I.SetSrc0SwizzleZ(SWZ_Z);       }
241
                if (Z == "-X") { I.SetSrc0SignZ( true ); I.SetSrc0SwizzleZ(SWZ_X);      }
242
                if (Z == "-Y") { I.SetSrc0SignZ( true ); I.SetSrc0SwizzleZ(SWZ_Y);      }
243
                if (Z == "-Z") { I.SetSrc0SignZ( true ); I.SetSrc0SwizzleZ(SWZ_Z);      }
244
        }
245
}
246
//----------------------------------------------------------
247
void StoreReturnAddress( std::vector & aInstructions, Theia::Parser::location_type & yylloc )
248
{
249
                I.SetCode( EOPERATION_ADD );
250
                I.mComment = "store return address";
251
                I.SetImm( aInstructions.size()+4 );
252
                I.SetWriteChannel(ECHANNEL_XYZ);
253
                I.SetDestinationAddress( RETURN_ADDRESS_REGISTER );
254
                I.SetDestZero( true );
255
                I.mSourceLine = GetCurrentLineNumber( yylloc );
256
                aInstructions.push_back( I );
257
                I.Clear();
258
}
259
//----------------------------------------------------------
260
void SavePreviousFramePointer( std::vector & aInstructions )
261
{
262
                I.SetCode( EOPERATION_ADD );
263
                I.mComment = "store current frame offset";
264
                I.SetWriteChannel(ECHANNEL_Y);
265
                I.SetDestinationAddress( SPR_CONTROL_REGISTER );
266
                I.SetSrc1Address(SPR_CONTROL_REGISTER);
267
                I.SetSrc1SwizzleX(SWX_X);
268
                I.SetSrc1SwizzleY(SWY_X);
269
                I.SetSrc1SwizzleZ(SWZ_X);
270
                I.SetSrc0Address(0);
271
                I.SetSrc0SwizzleX(SWX_X);
272
                I.SetSrc0SwizzleY(SWY_X);
273
                I.SetSrc0SwizzleZ(SWZ_X);
274
                aInstructions.push_back( I );
275
                I.Clear();
276
}
277
//----------------------------------------------------------
278
void UpdateFramePointer( std::vector & aInstructions )
279
{
280
                I.SetCode( EOPERATION_ADD );
281
                I.mComment = "displace next frame offset by the number of auto variables in current frame";
282
                I.SetWriteChannel(ECHANNEL_X);
283
                I.SetDestinationAddress( SPR_CONTROL_REGISTER );
284
                I.SetImm( GetCurretAutoVarFrameSize() );
285
                I.SetDestZero( false );
286
                aInstructions.push_back( I );
287
                I.Clear();
288
}
289
//----------------------------------------------------------
290
void CallFunction( std::string aFunctionName,  std::vector & aInstructions, std::map  &  aSymbolMap)
291
{
292
                I.SetCode( EOPERATION_ADD );
293
                I.mComment = "call the function";
294
                I.SetBranchFlag( true );
295
                I.SetBranchType( EBRANCH_ALWAYS );
296
                //Now do the branch
297
                if (aSymbolMap.find(aFunctionName) == aSymbolMap.end())
298
                        I.SetDestinationSymbol( "@"+aFunctionName );
299
                else
300
                        I.SetDestinationAddress( aSymbolMap[ aFunctionName ] );
301
 
302
                aInstructions.push_back( I );
303
                I.Clear();
304
}
305
//----------------------------------------------------------
306
void SetDestinationFromRegister( std::string aDestination, Instruction & aInst )
307
{
308
                //Look for displament addressing mode
309
 
310
                if (aDestination.find("&&") != std::string::npos)
311
                {
312
                        aDestination.erase(aDestination.find("&"));
313
                        //std::cout << "^_^ left_hand_side " << Destination << "\n";
314
                        aInst.SetDestZero( true ); //When Imm != 0, DestZero means DST = DSTINDEX + offset
315
                }
316
                if (aDestination.find(".") != std::string::npos)
317
                {
318
                        aInst.ClearWriteChannel();
319
                        if (aDestination.find("x") != std::string::npos)
320
                                aInst.SetWriteChannel(ECHANNEL_X);
321
                        if (aDestination.find("y") != std::string::npos)
322
                                aInst.SetWriteChannel(ECHANNEL_Y);
323
                        if (aDestination.find("z") != std::string::npos)
324
                                aInst.SetWriteChannel(ECHANNEL_Z);
325
 
326
                        aDestination.erase(aDestination.find("."));
327
 
328
                }
329
                aInst.SetDestinationAddress( atoi(aDestination.c_str()+1) );
330
}
331
//----------------------------------------------------------
332
void PopulateSourceRegisters( std::string a1, std::string a2, Instruction & I )
333
{
334
                        if ( a1.find("R") == std::string::npos )
335
                        {
336
                                unsigned int ImmediateValue;
337
                                std::string StringHex = a1;
338
                                std::stringstream ss;
339
                                ss << std::hex << StringHex;
340
                                ss >> ImmediateValue;
341
                                I.SetImm( ImmediateValue );
342
                        } else {
343
 
344
                                //Look for displament addressing mode
345
                        if (a1.find("&&") != std::string::npos)
346
                        {
347
                                a1.erase(a1.find("&"));
348
                                //std::cout << "^_^ a1" << a1 << "\n";
349
                                I.SetSrc1Displace( true ); //When Imm != 0, DestZero means DST = DSTINDEX + offset
350
                        }
351
 
352
                                std::string Src1 = a1;
353
                                if (IsSwizzled( Src1 ))
354
                                {
355
                                        SetSwizzleAndSign( 1, Src1,  I );
356
                                        Src1.erase(Src1.find("."));
357
                                }
358
                                I.SetSrc1Address( atoi( Src1.c_str()+1 ) );
359
                        }
360
 
361
                        if ( a2.find("R") == std::string::npos)
362
                        {
363
                        } else {
364
                                        //Look for displament addressing mode
365
                                if (a2.find("&&") != std::string::npos)
366
                                {
367
                                        a2.erase(a2.find("&"));
368
                                        //std::cout << "^_^ a2 " << a2 << "\n";
369
                                        I.SetSrc0Displace( true ); //When Imm != 0, DestZero means DST = DSTINDEX + offset
370
                                }
371
 
372
                                std::string Src0 = a2;
373
                                if (IsSwizzled( Src0 ))
374
                                {
375
                                        SetSwizzleAndSign( 0, Src0,  I );
376
                                        Src0.erase(Src0.find("."));
377
                                }
378
                                I.SetSrc0Address( atoi( Src0.c_str()+1 ) );
379
                        }
380
}
381
//----------------------------------------------------------
382
void ClearNextFunctionParamRegister()
383
{
384
        gFunctionParameterIndex = FUNCTION_PARAM_START_REGION;
385
}
386
//----------------------------------------------------------
387
void AddFunctionInputList( std::string aVar, std::vector & aInstructions,Theia::Parser::location_type  yylloc)
388
{
389
        //Get the value from the variable
390
        std::string Reg = GetRegisterFromAutoVar( aVar, yylloc );
391
        //Copy the value into function parameter register
392
        unsigned FunctionParamReg = GetNextFunctionParamRegister();
393
 
394
        I.SetCode( EOPERATION_ADD );
395
        I.mComment = "copy the value into function parameter register";
396
        I.SetWriteChannel(ECHANNEL_XYZ);
397
        I.SetDestZero( true ); //When Imm != 0, DestZero means DST = DSTINDEX + offset
398
        I.SetDestinationAddress( FunctionParamReg );
399
        I.SetSrc1Address(atoi(Reg.c_str()+1));
400
        I.SetSrc1SwizzleX(SWX_X);
401
        I.SetSrc1SwizzleY(SWY_Y);
402
        I.SetSrc1SwizzleZ(SWZ_Z);
403
        I.SetSrc0Address(0);
404
        I.SetSrc0SwizzleX(SWX_X);
405
        I.SetSrc0SwizzleY(SWY_X);
406
        I.SetSrc0SwizzleZ(SWZ_X);
407
        aInstructions.push_back( I );
408
        I.Clear();
409
}
410
//----------------------------------------------------------
411
        // Prototype for the yylex function
412
        static int yylex(Theia::Parser::semantic_type * yylval,
413
                         Theia::Parser::location_type * yylloc,
414
                         Theia::Scanner &scanner);
415
}
416
 
417
%token AUTO RETURN FUNCTION JMP EXIT EQUAL NOT_EQUAL GREATER_THAN LESS_THAN LESS_OR_EQUAL_THAN GREATER_OR_EQUAL_THAN IF ELSE OPEN_ROUND_BRACE CLOSE_ROUND_BRACE OPEN_BRACE CLOSE_BRACE ASSIGN DIV MUL ADD DECCONST HEXCONST BINCONST EOS DOT MINUS TK_X TK_Y TK_Z TK_N REG
418
%token IDENTIFIER SQRT SCALE UNSCALE USING FIXED_POINT COMMA OPEN_SQUARE_BRACE CLOSE_SQUARE_BRACE
419
%%
420
 
421
statement_list: //empty
422
        |
423
        statement_list statement
424
        |
425
        statement
426
        ;
427
 
428
statement
429
        :
430
        AUTO auto_var_list EOS
431
        |
432
        USING FIXED_POINT EOS
433
        {
434
                mGenerateFixedPointArithmetic = true;
435
        }
436
        |
437
        EXIT EOS
438
        {
439
                I.SetEofFlag(true);
440
                I.mComment = "Set the Exit bit";
441
                I.SetCode( EOPERATION_ADD );
442
                mInstructions.push_back(I);
443
                I.Clear();
444
        }
445
        |
446
        RETURN expression EOS
447
        {
448
 
449
 
450
 
451
                mInstructions[ mInstructions.size() - gInsertedInstructions].mSourceLine = GetCurrentLineNumber(yylloc);
452
            gInsertedInstructions = 0;
453
                mInstructions.back().mComment ="Assigning return value";
454
                mInstructions.back().SetDestinationAddress( RETURN_VALUE_REGISTER );
455
                ResetTempRegisterIndex();
456
 
457
 
458
                I.SetCode( EOPERATION_ADD );
459
                I.mComment = "Restore previous function frame offset";
460
                I.SetWriteChannel(ECHANNEL_X);
461
                I.SetDestinationAddress( SPR_CONTROL_REGISTER );
462
                I.SetSrc1Address(SPR_CONTROL_REGISTER);
463
                I.SetSrc1SwizzleX(SWX_Y);
464
                I.SetSrc1SwizzleY(SWY_Y);
465
                I.SetSrc1SwizzleZ(SWZ_Y);
466
                I.SetSrc0Address(0);
467
                I.SetSrc0SwizzleX(SWX_X);
468
                I.SetSrc0SwizzleY(SWY_X);
469
                I.SetSrc0SwizzleZ(SWZ_X);
470
                mInstructions.push_back( I );
471
                I.Clear();
472
 
473
                //Now return
474
                I.SetImm( 0 );
475
                I.SetCode( EOPERATION_ADD );
476
                I.mComment = "return from function";
477
                I.SetBranchFlag( true );
478
                I.SetBranchType( EBRANCH_ALWAYS );
479
                I.SetDestinationAddress( RETURN_ADDRESS_REGISTER );
480
                mInstructions.push_back(I);
481
                I.Clear();
482
 
483
        }
484
        |
485
        RETURN EOS
486
        {
487
                I.SetCode( EOPERATION_ADD );
488
                I.mComment = "Restore previous function frame offset";
489
                I.SetWriteChannel(ECHANNEL_X);
490
                I.SetDestinationAddress( SPR_CONTROL_REGISTER );
491
                I.SetSrc1Address(SPR_CONTROL_REGISTER);
492
                I.SetSrc1SwizzleX(SWX_Y);
493
                I.SetSrc1SwizzleY(SWY_Y);
494
                I.SetSrc1SwizzleZ(SWZ_Y);
495
                I.SetSrc0Address(0);
496
                I.SetSrc0SwizzleX(SWX_X);
497
                I.SetSrc0SwizzleY(SWY_X);
498
                I.SetSrc0SwizzleZ(SWZ_X);
499
                mInstructions.push_back( I );
500
                I.Clear();
501
 
502
                I.SetImm( 0 );
503
                I.SetCode( EOPERATION_ADD );
504
                I.mComment = "return from function";
505
                I.SetBranchFlag( true );
506
                I.SetBranchType( EBRANCH_ALWAYS );
507
                I.SetDestinationAddress( RETURN_ADDRESS_REGISTER );
508
                mInstructions.push_back(I);
509
                I.Clear();
510
        }
511
        | RETURN constant EOS
512
        {
513
                I.SetCode( EOPERATION_ADD );
514
                I.mComment = "Restore previous function frame offset";
515
                I.SetWriteChannel(ECHANNEL_X);
516
                I.SetDestinationAddress( SPR_CONTROL_REGISTER );
517
                I.SetSrc1Address(SPR_CONTROL_REGISTER);
518
                I.SetSrc1SwizzleX(SWX_Y);
519
                I.SetSrc1SwizzleY(SWY_Y);
520
                I.SetSrc1SwizzleZ(SWZ_Y);
521
                I.SetSrc0Address(0);
522
                I.SetSrc0SwizzleX(SWX_X);
523
                I.SetSrc0SwizzleY(SWY_X);
524
                I.SetSrc0SwizzleZ(SWZ_X);
525
                mInstructions.push_back( I );
526
                I.Clear();
527
 
528
                //Set the return value
529
                unsigned int ImmediateValue;
530
                std::string StringHex = $3;
531
                std::stringstream ss;
532
                ss << std::hex << StringHex;
533
                ss >> ImmediateValue;
534
                I.SetImm( ImmediateValue );
535
                I.SetDestZero(true);
536
                I.mComment = "Set the return value";
537
                I.SetWriteChannel(ECHANNEL_XYZ);
538
                I.SetCode( EOPERATION_ADD );
539
                I.SetDestinationAddress( RETURN_VALUE_REGISTER );
540
                mInstructions.push_back(I);
541
                I.Clear();
542
                //Now return
543
                I.SetImm( 0 );
544
                I.SetCode( EOPERATION_ADD );
545
                I.mComment = "return from function";
546
                I.SetBranchFlag( true );
547
                I.SetBranchType( EBRANCH_ALWAYS );
548
                I.SetDestinationAddress( RETURN_ADDRESS_REGISTER );
549
                mInstructions.push_back(I);
550
                I.Clear();
551
 
552
        }
553
        |
554
         left_hand_side ASSIGN constant EOS
555
         {
556
 
557
                std::string Destination = $1;
558
 
559
                //Look for displament addressing mode
560
                if (Destination.find("&&") != std::string::npos)
561
                {
562
                        Destination.erase(Destination.find("&"));
563
                        //std::cout << "^_^ " << Destination << "\n";
564
                        I.SetDestZero( true );
565
                        I.SetSrc1Displace( false );
566
                        I.SetSrc0Displace( true );
567
                }
568
 
569
                if (Destination.find(".") != std::string::npos)
570
                {
571
                        I.ClearWriteChannel();
572
                        if (Destination.find("x") != std::string::npos)
573
                                I.SetWriteChannel(ECHANNEL_X);
574
                        if (Destination.find("y") != std::string::npos)
575
                                I.SetWriteChannel(ECHANNEL_Y);
576
                        if (Destination.find("z") != std::string::npos)
577
                                I.SetWriteChannel(ECHANNEL_Z);
578
 
579
                        Destination.erase(Destination.find("."));
580
 
581
                }
582
 
583
                I.SetDestinationAddress( atoi($1.c_str()+1) );
584
                unsigned int ImmediateValue;
585
 
586
                std::string StringHex = $3;
587
                std::stringstream ss;
588
                ss << std::hex << StringHex;
589
                ss >> ImmediateValue;
590
                I.SetImm( ImmediateValue );
591
                I.SetDestZero( true );
592
                I.SetCode( EOPERATION_ADD );
593
 
594
                I.mSourceLine = GetCurrentLineNumber( yylloc );
595
 
596
                mInstructions.push_back(I);
597
                I.Clear();
598
                ResetTempRegisterIndex();
599
     }
600
         |
601
         left_hand_side ASSIGN OPEN_ROUND_BRACE constant COMMA constant COMMA constant CLOSE_ROUND_BRACE EOS
602
         {
603
                std::string Destination = $1;
604
 
605
                //Look for displament addressing mode
606
                bool HasOffset = false;
607
                if (Destination.find("&&") != std::string::npos)
608
                {
609
                        Destination.erase(Destination.find("&"));
610
                        HasOffset = true;
611
                }
612
 
613
                I.ClearWriteChannel();
614
                unsigned int ImmediateValue;
615
                {
616
                I.SetDestinationAddress( atoi($1.c_str()+1) );
617
                I.SetWriteChannel(ECHANNEL_X);
618
                std::string StringHex = $4;
619
                std::stringstream ss;
620
                ss << std::hex << StringHex;
621
                ss >> ImmediateValue;
622
                I.SetImm( ImmediateValue );
623
                I.SetDestZero( true );
624
                if (HasOffset)
625
                {
626
                        I.SetSrc1Displace( false );
627
                        I.SetSrc0Displace( true );
628
                }
629
                I.SetCode( EOPERATION_ADD );
630
                I.mSourceLine = GetCurrentLineNumber( yylloc );
631
                mInstructions.push_back(I);
632
                I.Clear();
633
                }
634
                {
635
                I.SetDestinationAddress( atoi($1.c_str()+1) );
636
                I.SetWriteChannel(ECHANNEL_Y);
637
                std::string StringHex = $6;
638
                std::stringstream ss;
639
                ss << std::hex << StringHex;
640
                ss >> ImmediateValue;
641
                I.SetImm( ImmediateValue );
642
                I.SetDestZero( true );
643
                if (HasOffset)
644
                {
645
                        I.SetSrc1Displace( false );
646
                        I.SetSrc0Displace( true );
647
                }
648
                I.SetCode( EOPERATION_ADD );
649
                mInstructions.push_back(I);
650
                I.Clear();
651
                }
652
                {
653
                I.SetDestinationAddress( atoi($1.c_str()+1) );
654
                I.SetWriteChannel(ECHANNEL_Z);
655
                std::string StringHex = $8;
656
                std::stringstream ss;
657
                ss << std::hex << StringHex;
658
                ss >> ImmediateValue;
659
                I.SetImm( ImmediateValue );
660
                I.SetDestZero( true );
661
                if (HasOffset)
662
                {
663
                        I.SetSrc1Displace( false );
664
                        I.SetSrc0Displace( true );
665
                }
666
                I.SetCode( EOPERATION_ADD );
667
                mInstructions.push_back(I);
668
                I.Clear();
669
                }
670
 
671
         }
672
         |
673
         left_hand_side MINUS MINUS EOS
674
         {
675
 
676
                I.mSourceLine = GetCurrentLineNumber( yylloc );
677
                I.SetCode( EOPERATION_ADD );
678
                SetDestinationFromRegister( $1, I );
679
                I.SetSrc0SignX( true );
680
                I.SetSrc0SignY( true );
681
                I.SetSrc0SignZ( true );
682
                std::string Destination = $1;
683
                if (Destination.find("&&") != std::string::npos)
684
                        Destination.erase(Destination.find("&&"));
685
 
686
                if (Destination.find(".") != std::string::npos)
687
                        Destination.erase(Destination.find("."));
688
 
689
                I.SetSrc1Address(atoi(Destination.c_str()+1));
690
                I.SetSrc0Address(0);
691
                I.SetSrc0SwizzleX(SWX_Y);
692
                I.SetSrc0SwizzleY(SWY_Y);
693
                I.SetSrc0SwizzleZ(SWZ_Y);
694
                mInstructions.push_back( I );
695
                I.Clear();
696
         }
697
         |
698
         left_hand_side ADD ADD EOS
699
         {
700
 
701
                I.mSourceLine = GetCurrentLineNumber( yylloc );
702
                I.SetCode( EOPERATION_ADD );
703
                SetDestinationFromRegister( $1, I );
704
                std::string Destination = $1;
705
                if (Destination.find("&&") != std::string::npos)
706
                        Destination.erase(Destination.find("&&"));
707
 
708
                if (Destination.find(".") != std::string::npos)
709
                        Destination.erase(Destination.find("."));
710
 
711
                I.SetSrc1Address(atoi(Destination.c_str()+1));
712
                I.SetSrc0Address(0);
713
                I.SetSrc0SwizzleX(SWX_Y);
714
                I.SetSrc0SwizzleY(SWY_Y);
715
                I.SetSrc0SwizzleZ(SWZ_Y);
716
                mInstructions.push_back( I );
717
                I.Clear();
718
         }
719
         |
720
         left_hand_side ASSIGN expression  EOS
721
        {
722
 
723
                mInstructions[ mInstructions.size() - gInsertedInstructions].mSourceLine = GetCurrentLineNumber(yylloc);
724
            gInsertedInstructions = 0;
725
                std::string Destination = $1;
726
 
727
                //Look for displament addressing mode
728
                if (Destination.find("&&") != std::string::npos)
729
                {
730
                        Destination.erase(Destination.find("&"));
731
                        //std::cout << "^_^ left_hand_side " << Destination << "\n";
732
                        mInstructions.back().SetDestZero( true ); //When Imm != 0, DestZero means DST = DSTINDEX + offset
733
                }
734
                if (Destination.find(".") != std::string::npos)
735
                {
736
                        mInstructions.back().ClearWriteChannel();
737
                        if (Destination.find("x") != std::string::npos)
738
                                mInstructions.back().SetWriteChannel(ECHANNEL_X);
739
                        if (Destination.find("y") != std::string::npos)
740
                                mInstructions.back().SetWriteChannel(ECHANNEL_Y);
741
                        if (Destination.find("z") != std::string::npos)
742
                                mInstructions.back().SetWriteChannel(ECHANNEL_Z);
743
 
744
                        Destination.erase(Destination.find("."));
745
 
746
                }
747
                mInstructions.back().SetDestinationAddress( atoi($1.c_str()+1) );
748
                ResetTempRegisterIndex();
749
        }
750
        | IF
751
          OPEN_ROUND_BRACE boolean_expression CLOSE_ROUND_BRACE
752
          OPEN_BRACE statement_list CLOSE_BRACE
753
      ELSE
754
        {
755
 
756
           //jump out of the if
757
           I.Clear();
758
           I.SetCode( EOPERATION_ADD );
759
           I.SetBranchFlag( true );
760
           I.SetBranchType( EBRANCH_ALWAYS );
761
           mInstructions.push_back(I);
762
           I.Clear();
763
           //Take care of the destination addr of the if statement.
764
           mInstructions[gBranchStack.back()].SetDestinationAddress(mInstructions.size());
765
          gBranchStack.pop_back();
766
          //push the inconditional jump into the stack
767
          gBranchStack.push_back(mInstructions.size() - 1);
768
          //std::cout << "else\n";
769
 
770
        }
771
          OPEN_BRACE  statement_list CLOSE_BRACE
772
        {
773
 
774
           mInstructions[gBranchStack.back()].SetDestinationAddress(mInstructions.size());
775
           gBranchStack.pop_back();
776
           //Now push the JMP
777
 
778
                //std::cout << "END elseif\n";
779
        }
780
        |
781
        //NOW the if statement
782
        IF OPEN_ROUND_BRACE boolean_expression CLOSE_ROUND_BRACE OPEN_BRACE statement_list CLOSE_BRACE
783
        {
784
                mInstructions[gBranchStack.back()].SetDestinationAddress(mInstructions.size());
785
                //mInstructions[gBranchStack.back()].mSourceLine = GetCurrentLineNumber(yylloc);
786
 
787
                gBranchStack.pop_back();
788
                //std::cout << "if closing at " << mInstructions.size() << "\n";
789
 
790
        }
791
        |
792
        FUNCTION IDENTIFIER OPEN_ROUND_BRACE function_argument_list CLOSE_ROUND_BRACE
793
        {
794
          //std::cout << "Function declaration for " << $2 << " at " << mInstructions.size() << "\n" ;
795
          mSymbolMap[ $2 ] = mInstructions.size();
796
        } OPEN_BRACE statement_list CLOSE_BRACE
797
        {
798
                //Clear the auto var index now that we leave the function scope
799
                ClearAutoVarMap();
800
                ClearFunctionParameterMap();
801
 
802
                //Now uddate the current SPR_CONTROL_REGISTER.x = SPR_CONTROL_REGISTER.y
803
                I.SetCode( EOPERATION_ADD );
804
                I.mComment = "Restore previous function frame offset";
805
                I.SetWriteChannel(ECHANNEL_X);
806
                I.SetDestinationAddress( SPR_CONTROL_REGISTER );
807
                I.SetSrc1Address(SPR_CONTROL_REGISTER);
808
                I.SetSrc1SwizzleX(SWX_Y);
809
                I.SetSrc1SwizzleY(SWY_Y);
810
                I.SetSrc1SwizzleZ(SWZ_Y);
811
                I.SetSrc0Address(0);
812
                I.SetSrc0SwizzleX(SWX_X);
813
                I.SetSrc0SwizzleY(SWY_X);
814
                I.SetSrc0SwizzleZ(SWZ_X);
815
 
816
                mInstructions.push_back( I );
817
                I.Clear();
818
        }
819
        |
820
        left_hand_side ASSIGN IDENTIFIER OPEN_ROUND_BRACE function_input_list CLOSE_ROUND_BRACE EOS
821
        {
822
                //std::cout << "Function call returning to var\n";
823
                StoreReturnAddress( mInstructions, yylloc );
824
                SavePreviousFramePointer( mInstructions );
825
                UpdateFramePointer( mInstructions );
826
                CallFunction( $3, mInstructions, mSymbolMap );
827
 
828
 
829
                //Return value comes in R1, so let's store this in our variable
830
                I.SetCode( EOPERATION_ADD );
831
                SetDestinationFromRegister( $1, I );
832
                I.mComment = "grab the return value from the function";
833
                I.SetSrc1Address( RETURN_VALUE_REGISTER);
834
                I.SetSrc0Address(0);
835
                I.SetSrc0SwizzleX(SWX_X);
836
                I.SetSrc0SwizzleY(SWY_X);
837
                I.SetSrc0SwizzleZ(SWZ_X);
838
                mInstructions.push_back( I );
839
                I.Clear();
840
                ClearNextFunctionParamRegister();
841
        }
842
        // |
843
        // left_hand_side ASSIGN IDENTIFIER OPEN_ROUND_BRACE CLOSE_ROUND_BRACE EOS
844
        // {
845
                // std::cout << "Function call returning to var\n";
846
                // StoreReturnAddress( mInstructions, yylloc );
847
                // SavePreviousFramePointer( mInstructions );
848
                // UpdateFramePointer( mInstructions );
849
                // CallFunction( $3, mInstructions, mSymbolMap );
850
 
851
 
852
                // //Return value comes in R1, so let's store this in our variable
853
                // I.SetCode( EOPERATION_ADD );
854
                // SetDestinationFromRegister( $1, I );
855
                // I.mComment = "grab the return value from the function";
856
                // I.SetSrc1Address(1);
857
                // I.SetSrc0Address(0);
858
                // I.SetSrc0SwizzleX(SWX_X);
859
                // I.SetSrc0SwizzleY(SWY_X);
860
                // I.SetSrc0SwizzleZ(SWZ_X);
861
                // mInstructions.push_back( I );
862
                // I.Clear();
863
        // }
864
        |
865
        //Function call
866
        IDENTIFIER  OPEN_ROUND_BRACE CLOSE_ROUND_BRACE EOS
867
        {
868
                //Store the return address
869
                I.SetCode( EOPERATION_ADD );
870
                I.mComment = "store return address";
871
                I.SetImm( mInstructions.size()+4 );
872
                I.SetWriteChannel(ECHANNEL_XYZ);
873
                I.SetDestinationAddress( RETURN_ADDRESS_REGISTER );
874
                I.SetDestZero( true );
875
                I.mSourceLine = GetCurrentLineNumber( yylloc );
876
                mInstructions.push_back( I );
877
                I.Clear();
878
                //Store the current SPR_CONTROL_REGISTER.x into the previous SPR_CONTROL_REGISTER.y
879
                //SPR_CONTROL_REGISTER.y = SPR_CONTROL_REGISTER.xxx + 0;
880
                I.SetCode( EOPERATION_ADD );
881
                I.mComment = "store current frame offset";
882
                I.SetWriteChannel(ECHANNEL_Y);
883
                I.SetDestinationAddress( SPR_CONTROL_REGISTER );
884
                I.SetSrc1Address(SPR_CONTROL_REGISTER);
885
                I.SetSrc1SwizzleX(SWX_X);
886
                I.SetSrc1SwizzleY(SWY_X);
887
                I.SetSrc1SwizzleZ(SWZ_X);
888
                I.SetSrc0Address(0);
889
                I.SetSrc0SwizzleX(SWX_X);
890
                I.SetSrc0SwizzleY(SWY_X);
891
                I.SetSrc0SwizzleZ(SWZ_X);
892
                mInstructions.push_back( I );
893
                I.Clear();
894
                //Now uddate the current SPR_CONTROL_REGISTER.x += number of auto variables
895
                I.SetCode( EOPERATION_ADD );
896
                I.mComment = "displace next frame offset by the number of auto variables in current frame";
897
                I.SetWriteChannel(ECHANNEL_X);
898
                I.SetDestinationAddress( SPR_CONTROL_REGISTER );
899
                I.SetImm( GetCurretAutoVarFrameSize() );
900
                I.SetDestZero( false );
901
                mInstructions.push_back( I );
902
                I.Clear();
903
                //Call the function with a JMP
904
                I.SetCode( EOPERATION_ADD );
905
                I.mComment = "call the function";
906
                I.SetBranchFlag( true );
907
                I.SetBranchType( EBRANCH_ALWAYS );
908
                //Now do the branch
909
                if (mSymbolMap.find($1) == mSymbolMap.end())
910
                {
911
                //      //std::cout << "Error in line : " << $1 <<" undelcared IDENTIFIER\n";
912
                        I.SetDestinationSymbol( "@"+$1 );
913
                //      exit(1);
914
                } else {
915
                        I.SetDestinationAddress( mSymbolMap[ $1 ] );
916
                }
917
 
918
 
919
                mInstructions.push_back( I );
920
                I.Clear();
921
 
922
        }
923
        ;
924
        function_input_list
925
                                          :
926
                                          |//empty
927
                                          IDENTIFIER COMMA function_input_list
928
                                          {
929
                                                AddFunctionInputList( $1, mInstructions,yylloc );
930
                                          }
931
                                          |
932
                                          IDENTIFIER
933
                                          {
934
                                                AddFunctionInputList( $1,mInstructions, yylloc );
935
                                          }
936
                                          // |
937
                                          // constant COMMA function_input_list
938
                                          // {
939
                                                // unsigned FunctionParamReg = GetNextFunctionParamRegister();
940
                                                // I.SetDestinationAddress( FunctionParamReg );
941
                                                // unsigned int ImmediateValue;
942
                                                // std::string StringHex = $1;
943
                                                // std::stringstream ss;
944
                                                // ss << std::hex << StringHex;
945
                                                // ss >> ImmediateValue;
946
                                                // I.SetImm( ImmediateValue );
947
                                                // I.SetDestZero( true );
948
                                                // I.SetCode( EOPERATION_ADD );
949
                                                // I.mComment = "Adding literal as function input param";
950
                                                // mInstructions.push_back(I);
951
                                                // I.Clear();
952
                                          // }
953
                                          // |
954
                                          // constant
955
                                          // {
956
                                                // unsigned FunctionParamReg = GetNextFunctionParamRegister();
957
                                                // I.SetDestinationAddress( FunctionParamReg );
958
                                                // unsigned int ImmediateValue;
959
                                                // std::string StringHex = $1;
960
                                                // std::stringstream ss;
961
                                                // ss << std::hex << StringHex;
962
                                                // ss >> ImmediateValue;
963
                                                // I.SetImm( ImmediateValue );
964
                                                // I.SetDestZero( true );
965
                                                // I.SetCode( EOPERATION_ADD );
966
                                                // I.mComment = "Adding literal as function input param";
967
                                                // mInstructions.push_back(I);
968
                                                // I.Clear();
969
                                          // }
970
                                          ;
971
 
972
        function_argument_list
973
                                                :
974
                                                | //empty
975
                                                IDENTIFIER COMMA function_argument_list
976
                                                {
977
                                                        AddFunctionParameter( $1, yylloc );
978
                                                }
979
                                                |
980
                                                IDENTIFIER
981
                                                {
982
                                                        AddFunctionParameter( $1, yylloc );
983
                                                }
984
                                                ;
985
 
986
//  ::=  +  |
987
          //  -  |
988
          // 
989
 
990
//  ::=  *  |
991
           //  /  |
992
           // 
993
 
994
//  ::= x | y | ... |
995
             // (  ) |
996
             // -  |
997
             // 
998
 
999
expression
1000
                :
1001
                expression ADD term
1002
                {
1003
                        unsigned int TempRegIndex  = GetFreeTempRegister();
1004
                        I.SetCode( EOPERATION_ADD );
1005
                        I.SetDestinationAddress( TempRegIndex );
1006
                        I.SetWriteChannel(ECHANNEL_XYZ);
1007
 
1008
                        PopulateSourceRegisters( $1, $3, I);
1009
                        mInstructions.push_back(I);
1010
                        gInsertedInstructions++;
1011
                        I.Clear();
1012
 
1013
                        std::stringstream ss;
1014
                        ss << "R" << TempRegIndex;
1015
                        $$ = ss.str();
1016
 
1017
                }
1018
                |
1019
                expression MINUS term
1020
                {
1021
                        unsigned int TempRegIndex  = GetFreeTempRegister();
1022
                        I.SetCode( EOPERATION_ADD );
1023
                        I.SetDestinationAddress( TempRegIndex );
1024
                        I.SetWriteChannel(ECHANNEL_XYZ);
1025
                        I.SetSrc0SignX( true );
1026
                        I.SetSrc0SignY( true );
1027
                        I.SetSrc0SignZ( true );
1028
 
1029
                        PopulateSourceRegisters( $1, $3, I);
1030
                        mInstructions.push_back(I);
1031
                        gInsertedInstructions++;
1032
                        I.Clear();
1033
 
1034
                        std::stringstream ss;
1035
                        ss << "R" << TempRegIndex;
1036
                        $$ = ss.str();
1037
                }
1038
                |
1039
                term
1040
                {
1041
                        $$ = $1;
1042
                }
1043
                ;
1044
 
1045
                term
1046
                :
1047
                term MUL factor
1048
                {
1049
 
1050
                        unsigned int TempRegIndex  = GetFreeTempRegister();
1051
                        I.SetDestinationAddress( TempRegIndex );
1052
                        I.SetWriteChannel(ECHANNEL_XYZ);
1053
                        I.SetCode( EOPERATION_MUL );
1054
 
1055
                        PopulateSourceRegisters( $1, $3, I);
1056
 
1057
                        //If we are using fixed point aritmethic then we need to apply the scale
1058
                        //R = A * ( B >> SCALE)
1059
                        if (mGenerateFixedPointArithmetic)
1060
                                I.SetSrc0Rotation( EROT_RESULT_RIGHT );
1061
 
1062
                        mInstructions.push_back(I);
1063
                        gInsertedInstructions++;
1064
                        I.Clear();
1065
 
1066
                        std::stringstream ss;
1067
                        ss << "R" << TempRegIndex;
1068
                        $$ = ss.str();
1069
                }
1070
                |
1071
                term DIV factor
1072
                {
1073
                        unsigned int TempRegIndex  = GetFreeTempRegister();
1074
                        I.SetDestinationAddress( TempRegIndex );
1075
                        I.SetWriteChannel(ECHANNEL_XYZ);
1076
                        I.SetCode( EOPERATION_DIV );
1077
 
1078
                        PopulateSourceRegisters( $1, $3, I);
1079
 
1080
                        //If we are using fixed point aritmethic then we need to apply the scale
1081
                        // R = (A << N) / B
1082
                        if (mGenerateFixedPointArithmetic)
1083
                                I.SetSrc1Rotation( EROT_SRC1_LEFT );
1084
 
1085
                        mInstructions.push_back(I);
1086
                        gInsertedInstructions++;
1087
                        I.Clear();
1088
 
1089
                        std::stringstream ss;
1090
                        ss << "R" << TempRegIndex;
1091
                        $$ = ss.str();
1092
                }
1093
                |
1094
                factor
1095
                {
1096
                        $$ = $1;
1097
                }
1098
                ;
1099
 
1100
                factor
1101
                :
1102
                source
1103
                {
1104
                        $$ = $1;
1105
                }
1106
                |
1107
                SQRT OPEN_ROUND_BRACE expression CLOSE_ROUND_BRACE
1108
                {
1109
                        unsigned int TempRegIndex  = GetFreeTempRegister();
1110
                        I.SetDestinationAddress( TempRegIndex );
1111
                        I.SetWriteChannel(ECHANNEL_XYZ);
1112
                        I.SetCode( EOPERATION_SQRT );
1113
                        I.SetSrc0Address( 0 );
1114
                        PopulateSourceRegisters( $3 ,"R0.XXX", I);
1115
                        mInstructions.push_back(I);
1116
                        gInsertedInstructions++;
1117
                        I.Clear();
1118
 
1119
                        std::stringstream ss;
1120
                        ss << "R" << TempRegIndex;
1121
                        $$ = ss.str();
1122
                }
1123
                |
1124
                OPEN_ROUND_BRACE expression CLOSE_ROUND_BRACE
1125
                {
1126
                        $$ = $2;
1127
                }
1128
                ;
1129
 
1130
 
1131
 
1132
        source
1133
        :
1134
        IDENTIFIER
1135
        {
1136
                std::string Register;
1137
                if ((Register = GetRegisterFromFunctionParameter($1)) != "NULL")
1138
                        $$ = Register;
1139
                 else
1140
                        $$ = GetRegisterFromAutoVar( $1, yylloc) + " && ";
1141
        }
1142
        |
1143
        IDENTIFIER DOT coordinate coordinate coordinate
1144
        {
1145
                std::string X = $3,Y = $4,Z = $5;
1146
                std::string Register;
1147
                if ((Register = GetRegisterFromFunctionParameter($1)) != "NULL")
1148
                        $$ = (Register + "." + " " + X + " " + Y  + " " + Z + " && ");
1149
                else
1150
                        $$ = (GetRegisterFromAutoVar( $1, yylloc) + "." + " " + X + " " + Y  + " " + Z + " && ");
1151
        }
1152
        |
1153
        REG
1154
        {
1155
                std::string R = $1;
1156
                R.erase(0,1);
1157
                $$ = "R" + R;
1158
 
1159
        }
1160
        |
1161
        SCALE OPEN_ROUND_BRACE REG CLOSE_ROUND_BRACE
1162
        {
1163
 
1164
                std::string R = $1;
1165
                R.erase(0,1);
1166
                $$ = "<
1167
        }
1168
        |
1169
        UNSCALE OPEN_ROUND_BRACE REG CLOSE_ROUND_BRACE
1170
        {
1171
 
1172
                std::string R = $1;
1173
                R.erase(0,1);
1174
                $$ = ">>R" + R;
1175
        }
1176
        |
1177
        REG DOT coordinate coordinate coordinate
1178
        {
1179
                std::string R = $1;
1180
                std::string X = $3,Y = $4,Z = $5;
1181
                R.erase(0,1);
1182
                $$ = "R" + R + "." + " " + X + " " + Y  + " " + Z;
1183
 
1184
        }
1185
        ;
1186
 
1187
 
1188
        coordinate
1189
        :
1190
        TK_X
1191
        {
1192
                $$ = "X";
1193
        }
1194
        |
1195
        MINUS TK_X
1196
        {
1197
                $$ = "-X";
1198
        }
1199
        |
1200
        TK_Y
1201
        {
1202
                $$ = "Y";
1203
        }
1204
        |
1205
        MINUS TK_Y
1206
        {
1207
                $$ = "-Y";
1208
        }
1209
        |
1210
        TK_Z
1211
        {
1212
                $$ = "Z";
1213
        }
1214
        |
1215
        MINUS TK_Z
1216
        {
1217
                $$ = "-Z";
1218
        }
1219
        ;
1220
 
1221
left_hand_side
1222
        :
1223
        IDENTIFIER
1224
        {
1225
            $$ = GetRegisterFromAutoVar( $1, yylloc ) + ".xyz" + " && ";
1226
        }
1227
        |
1228
        IDENTIFIER DOT TK_X
1229
        {
1230
                $$ = GetRegisterFromAutoVar( $1, yylloc ) + ".x" + " && ";
1231
        }
1232
        |
1233
        IDENTIFIER DOT TK_Y
1234
        {
1235
                $$ = GetRegisterFromAutoVar( $1, yylloc ) + ".y" + " && ";
1236
        }
1237
        |
1238
        IDENTIFIER DOT TK_Z
1239
        {
1240
                $$ = GetRegisterFromAutoVar( $1, yylloc ) + ".z" + " && ";
1241
        }
1242
        |
1243
        REG
1244
        {
1245
                std::string R = $1;
1246
                R.erase(0,1);
1247
                $$ = "R" + R + ".xyz";
1248
        }
1249
        |
1250
        REG DOT TK_X
1251
        {
1252
                std::string R = $1;
1253
                R.erase(0,1);
1254
                $$ = "R" + R + ".x";
1255
        }
1256
        |
1257
        REG DOT TK_Y
1258
        {
1259
                std::string R = $1;
1260
                R.erase(0,1);
1261
                $$ = "R" + R + ".y";
1262
        }
1263
        |
1264
        REG DOT TK_Z
1265
        {
1266
 
1267
                std::string R = $1;
1268
                R.erase(0,1);
1269
                $$ = "R" + R + ".z";
1270
        }
1271
        |
1272
        REG DOT TK_X TK_Y TK_N
1273
        {
1274
                std::string R = $1;
1275
                R.erase(0,1);
1276
                $$ = "R" + R + ".xy";
1277
        }
1278
        |
1279
        REG DOT TK_X TK_N TK_Z
1280
        {
1281
                std::string R = $1;
1282
                R.erase(0,1);
1283
                $$ = "R" + R + ".xz";
1284
        }
1285
        |
1286
        REG DOT TK_N TK_Y TK_Z
1287
        {
1288
                std::string R = $1;
1289
                R.erase(0,1);
1290
                $$ = "R" + R + ".yz";
1291
        }
1292
        ;
1293
 
1294
 
1295
boolean_expression
1296
                                :
1297
                                source NOT_EQUAL constant
1298
                                {
1299
                                        I.mSourceLine = GetCurrentLineNumber( yylloc );
1300
                                        I.SetCode( EOPERATION_ADD );
1301
                                        I.SetSrc0SignX( true );
1302
                                        I.SetSrc0SignY( true );
1303
                                        I.SetSrc0SignZ( true );
1304
                                        I.SetBranchFlag( true );
1305
                                        I.SetBranchType( EBRANCH_IF_ZERO );
1306
                                        if ($3 == "0")
1307
                                                PopulateSourceRegisters( $1, "R0 . X X X", I);
1308
                                        mInstructions.push_back(I);
1309
                                        I.Clear();
1310
 
1311
                                        //std::cout << "pushing code at position " << (mInstructions.size() - 1) << "\n";
1312
                                        gBranchStack.push_back(mInstructions.size() - 1);
1313
                                }
1314
                                |
1315
                                source EQUAL constant
1316
                                {
1317
                                        I.mSourceLine = GetCurrentLineNumber( yylloc );
1318
                                        I.SetCode( EOPERATION_ADD );
1319
                                        I.SetSrc0SignX( true );
1320
                                        I.SetSrc0SignY( true );
1321
                                        I.SetSrc0SignZ( true );
1322
                                        I.SetBranchFlag( true );
1323
                                        I.SetBranchType( EBRANCH_IF_NOT_ZERO );
1324
                                        if ($3 == "0")
1325
                                                PopulateSourceRegisters( $1, "R0 . X X X", I);
1326
                                        mInstructions.push_back(I);
1327
                                        I.Clear();
1328
 
1329
                                        gBranchStack.push_back(mInstructions.size() - 1);
1330
                                }
1331
                                |
1332
                                source EQUAL source
1333
                                {
1334
                                        I.mSourceLine = GetCurrentLineNumber( yylloc );
1335
                                        I.SetCode( EOPERATION_ADD );
1336
                                        I.SetSrc0SignX( true );
1337
                                        I.SetSrc0SignY( true );
1338
                                        I.SetSrc0SignZ( true );
1339
                                        I.SetBranchFlag( true );
1340
                                        I.SetBranchType( EBRANCH_IF_NOT_ZERO );
1341
                                        PopulateSourceRegisters( $1, $3, I);
1342
                                        mInstructions.push_back(I);
1343
                                        I.Clear();
1344
 
1345
                                        //std::cout << "pushing code at position " << (mInstructions.size() - 1) << "\n";
1346
                                        gBranchStack.push_back(mInstructions.size() - 1);
1347
 
1348
                                        //std::cout << "== \n";
1349
                                }
1350
                                |
1351
                                source NOT_EQUAL source
1352
                                {
1353
                                        I.mSourceLine = GetCurrentLineNumber( yylloc );
1354
                                        I.SetCode( EOPERATION_ADD );
1355
                                        I.SetSrc0SignX( true );
1356
                                        I.SetSrc0SignY( true );
1357
                                        I.SetSrc0SignZ( true );
1358
                                        I.SetBranchFlag( true );
1359
                                        I.SetBranchType( EBRANCH_IF_ZERO );
1360
                                        PopulateSourceRegisters( $1, $3, I);
1361
                                        mInstructions.push_back(I);
1362
                                        I.Clear();
1363
                                        //std::cout << "pushing code at position " << (mInstructions.size() - 1) << "\n";
1364
                                        gBranchStack.push_back(mInstructions.size() - 1);
1365
 
1366
                                        //std::cout << "!= \n";
1367
                                }
1368
                                |
1369
                                source GREATER_THAN source
1370
                                {
1371
                                        I.mSourceLine = GetCurrentLineNumber( yylloc );
1372
                                        I.SetCode( EOPERATION_ADD );
1373
                                        I.SetSrc0SignX( true );
1374
                                        I.SetSrc0SignY( true );
1375
                                        I.SetSrc0SignZ( true );
1376
                                        I.SetBranchFlag( true );
1377
                                        I.SetBranchType( EBRANCH_IF_ZERO_OR_SIGN );
1378
                                        PopulateSourceRegisters( $1, $3, I);
1379
                                        mInstructions.push_back(I);
1380
                                        I.Clear();
1381
                                        //std::cout << "pushing code at position " << (mInstructions.size() - 1) << "\n";
1382
                                        gBranchStack.push_back(mInstructions.size() - 1);
1383
 
1384
                                        //std::cout << "> \n";
1385
                                }
1386
                                |
1387
                                source LESS_THAN source
1388
                                {
1389
                                        I.mSourceLine = GetCurrentLineNumber( yylloc );
1390
                                        I.SetCode( EOPERATION_ADD );
1391
                                        I.SetSrc0SignX( true );
1392
                                        I.SetSrc0SignY( true );
1393
                                        I.SetSrc0SignZ( true );
1394
                                        I.SetBranchFlag( true );
1395
                                        I.SetBranchType( EBRANCH_IF_ZERO_OR_NOT_SIGN );
1396
                                        PopulateSourceRegisters( $1, $3, I);
1397
                                        mInstructions.push_back(I);
1398
                                        I.Clear();
1399
                                        //std::cout << "pushing code at position " << (mInstructions.size() - 1) << "\n";
1400
                                        gBranchStack.push_back(mInstructions.size() - 1);
1401
 
1402
                                        //std::cout << "< \n";
1403
                                }
1404
                                |
1405
                                source LESS_OR_EQUAL_THAN source
1406
                                {
1407
                                        I.mSourceLine = GetCurrentLineNumber( yylloc );
1408
                                        I.SetCode( EOPERATION_ADD );
1409
                                        I.SetSrc0SignX( true );
1410
                                        I.SetSrc0SignY( true );
1411
                                        I.SetSrc0SignZ( true );
1412
                                        I.SetBranchFlag( true );
1413
                                        I.SetBranchType( EBRANCH_IF_NOT_SIGN );
1414
                                        PopulateSourceRegisters( $1, $3, I);
1415
                                        mInstructions.push_back(I);
1416
                                        I.Clear();
1417
                                        //std::cout << "pushing code at position " << (mInstructions.size() - 1) << "\n";
1418
                                        gBranchStack.push_back(mInstructions.size() - 1);
1419
 
1420
                                        //std::cout << "<= \n";
1421
                                }
1422
                                |
1423
                                source GREATER_OR_EQUAL_THAN source
1424
                                {
1425
                                        I.mSourceLine = GetCurrentLineNumber( yylloc );
1426
                                        I.SetCode( EOPERATION_ADD );
1427
                                        I.SetSrc0SignX( true );
1428
                                        I.SetSrc0SignY( true );
1429
                                        I.SetSrc0SignZ( true );
1430
                                        I.SetBranchFlag( true );
1431
                                        I.SetBranchType( EBRANCH_IF_SIGN );
1432
                                        PopulateSourceRegisters( $1, $3, I);
1433
                                        mInstructions.push_back(I);
1434
                                        I.Clear();
1435
                                        //std::cout << "pushing code at position " << (mInstructions.size() - 1) << "\n";
1436
                                        gBranchStack.push_back(mInstructions.size() - 1);
1437
 
1438
                                        //std::cout << ">= \n";
1439
                                }
1440
                                |
1441
                                source GREATER_OR_EQUAL_THAN constant
1442
                                {
1443
                                        I.mSourceLine = GetCurrentLineNumber( yylloc );
1444
                                        I.SetCode( EOPERATION_ADD );
1445
                                        I.SetSrc0SignX( true );
1446
                                        I.SetSrc0SignY( true );
1447
                                        I.SetSrc0SignZ( true );
1448
                                        I.SetBranchFlag( true );
1449
                                        I.SetBranchType( EBRANCH_IF_SIGN );
1450
                                        if ($3 == "0")
1451
                                        {
1452
                                                PopulateSourceRegisters( $1, "R0 . X X X", I);
1453
                                        }
1454
                                        mInstructions.push_back(I);
1455
                                        I.Clear();
1456
                                        //std::cout << "pushing code at position " << (mInstructions.size() - 1) << "\n";
1457
                                        gBranchStack.push_back(mInstructions.size() - 1);
1458
 
1459
                                        //std::cout << ">= \n";
1460
                                }
1461
                                ;
1462
 
1463
constant
1464
        :
1465
                DECCONST
1466
                {
1467
                        // Transform to HEX string
1468
                        unsigned int Val;
1469
                        std::string StringDec = $1;
1470
                        std::stringstream ss;
1471
                        ss << StringDec;
1472
                        ss >> Val;
1473
                        std::stringstream ss2;
1474
                        ss2 << std::hex << Val;
1475
                        $$ = ss2.str();
1476
                }
1477
                |
1478
                HEXCONST
1479
                {
1480
                        unsigned int Val;
1481
                        std::string StringHex = $1;
1482
                        // Get rid of the 0x
1483
                        StringHex.erase(StringHex.begin(),StringHex.begin()+2);
1484
                        std::stringstream ss;
1485
                        ss << std::hex << StringHex;
1486
 
1487
                        $$ = ss.str();
1488
                }
1489
                |
1490
                BINCONST
1491
                {
1492
                        // Transform to HEX string
1493
                        unsigned long Val;
1494
                        std::string StringBin = $1;
1495
                        // Get rid of the 0b
1496
                        StringBin.erase(StringBin.begin(),StringBin.begin()+2);
1497
                        std::bitset<32> Bitset( StringBin );
1498
                        std::stringstream ss2;
1499
                        ss2 << std::hex <<  Bitset.to_ulong();
1500
                        $$ = ss2.str();
1501
                }
1502
        ;
1503
auto_var_list
1504
                        :
1505
                        IDENTIFIER COMMA auto_var_list
1506
                        {
1507
                                if (gAutoVarMap.find($1) != gAutoVarMap.end())
1508
                                {
1509
                                std::ostringstream ret;
1510
                                ret << "Duplicated symbol " << $1 << "'\n";
1511
                                throw ret.str();
1512
                                }
1513
                                gAutoVarMap[ $1 ] = GetAutoVarIndex();
1514
                        }
1515
                        |
1516
                        IDENTIFIER ASSIGN OPEN_ROUND_BRACE constant COMMA constant COMMA constant CLOSE_ROUND_BRACE COMMA auto_var_list
1517
                        {
1518
                                if (gAutoVarMap.find($1) != gAutoVarMap.end())
1519
                                {
1520
                                std::ostringstream ret;
1521
                                ret << "Duplicated symbol " << $1 << "'\n";
1522
                                throw ret.str();
1523
                                }
1524
                                gAutoVarMap[ $1 ] = GetAutoVarIndex();
1525
 
1526
                                unsigned int Destination = gAutoVarMap[ $1 ];
1527
 
1528
 
1529
 
1530
                                I.ClearWriteChannel();
1531
                                unsigned int ImmediateValue;
1532
                                {
1533
                                I.SetDestinationAddress( Destination );
1534
                                I.SetWriteChannel(ECHANNEL_X);
1535
                                std::string StringHex = $4;
1536
                                std::stringstream ss;
1537
                                ss << std::hex << StringHex;
1538
                                ss >> ImmediateValue;
1539
                                I.SetImm( ImmediateValue );
1540
                                I.SetDestZero( true );
1541
                                I.SetSrc1Displace( false );
1542
                                I.SetSrc0Displace( true );
1543
                                I.SetCode( EOPERATION_ADD );
1544
                                I.mSourceLine = GetCurrentLineNumber( yylloc );
1545
                                mInstructions.push_back(I);
1546
                                I.Clear();
1547
                                }
1548
                                {
1549
                                I.SetDestinationAddress( Destination );
1550
                                I.SetWriteChannel(ECHANNEL_Y);
1551
                                std::string StringHex = $6;
1552
                                std::stringstream ss;
1553
                                ss << std::hex << StringHex;
1554
                                ss >> ImmediateValue;
1555
                                I.SetImm( ImmediateValue );
1556
                                I.SetDestZero( true );
1557
                                I.SetSrc1Displace( false );
1558
                                I.SetSrc0Displace( true );
1559
                                I.SetCode( EOPERATION_ADD );
1560
                                mInstructions.push_back(I);
1561
                                I.Clear();
1562
                                }
1563
                                {
1564
                                I.SetDestinationAddress( Destination );
1565
                                I.SetWriteChannel(ECHANNEL_Z);
1566
                                std::string StringHex = $8;
1567
                                std::stringstream ss;
1568
                                ss << std::hex << StringHex;
1569
                                ss >> ImmediateValue;
1570
                                I.SetImm( ImmediateValue );
1571
                                I.SetDestZero( true );
1572
                                I.SetSrc1Displace( false );
1573
                                I.SetSrc0Displace( true );
1574
                                I.SetCode( EOPERATION_ADD );
1575
                                mInstructions.push_back(I);
1576
                                I.Clear();
1577
                                }
1578
                        }
1579
                        |
1580
                        IDENTIFIER ASSIGN OPEN_ROUND_BRACE constant COMMA constant COMMA constant CLOSE_ROUND_BRACE
1581
                        {
1582
                                if (gAutoVarMap.find($1) != gAutoVarMap.end())
1583
                                {
1584
                                std::ostringstream ret;
1585
                                ret << "Duplicated symbol " << $1 << "'\n";
1586
                                throw ret.str();
1587
                                }
1588
                                gAutoVarMap[ $1 ] = GetAutoVarIndex();
1589
 
1590
                                unsigned int Destination = gAutoVarMap[ $1 ];
1591
 
1592
 
1593
                                I.SetDestZero( true );
1594
                                I.SetSrc1Displace( false );
1595
                                I.SetSrc0Displace( true );
1596
 
1597
 
1598
                                I.ClearWriteChannel();
1599
                                unsigned int ImmediateValue;
1600
                                {
1601
                                I.SetDestinationAddress( Destination );
1602
                                I.SetWriteChannel(ECHANNEL_X);
1603
                                std::string StringHex = $4;
1604
                                std::stringstream ss;
1605
                                ss << std::hex << StringHex;
1606
                                ss >> ImmediateValue;
1607
                                I.SetImm( ImmediateValue );
1608
                                I.SetDestZero( true );
1609
                                I.SetSrc1Displace( false );
1610
                                I.SetSrc0Displace( true );
1611
                                I.SetCode( EOPERATION_ADD );
1612
                                I.mSourceLine = GetCurrentLineNumber( yylloc );
1613
                                mInstructions.push_back(I);
1614
                                I.Clear();
1615
                                }
1616
                                {
1617
                                I.SetDestinationAddress( Destination );
1618
                                I.SetWriteChannel(ECHANNEL_Y);
1619
                                std::string StringHex = $6;
1620
                                std::stringstream ss;
1621
                                ss << std::hex << StringHex;
1622
                                ss >> ImmediateValue;
1623
                                I.SetImm( ImmediateValue );
1624
                                I.SetDestZero( true );
1625
                                I.SetSrc1Displace( false );
1626
                                I.SetSrc0Displace( true );
1627
                                I.SetCode( EOPERATION_ADD );
1628
                                mInstructions.push_back(I);
1629
                                I.Clear();
1630
                                }
1631
                                {
1632
                                I.SetDestinationAddress( Destination );
1633
                                I.SetWriteChannel(ECHANNEL_Z);
1634
                                std::string StringHex = $8;
1635
                                std::stringstream ss;
1636
                                ss << std::hex << StringHex;
1637
                                ss >> ImmediateValue;
1638
                                I.SetImm( ImmediateValue );
1639
                                I.SetDestZero( true );
1640
                                I.SetSrc1Displace( false );
1641
                                I.SetSrc0Displace( true );
1642
                                I.SetCode( EOPERATION_ADD );
1643
                                mInstructions.push_back(I);
1644
                                I.Clear();
1645
                                }
1646
                        }
1647
                        |
1648
                        IDENTIFIER
1649
                        {
1650
                                //std::cout << "\n\n\nHERE!!! " << $1 << "\n\n\n";
1651
                                if (gAutoVarMap.find($1) != gAutoVarMap.end())
1652
                                {
1653
                                std::ostringstream ret;
1654
                                ret << "Duplicated symbol " << $1 << "'\n";
1655
                                throw ret.str();
1656
                                }
1657
                                gAutoVarMap[ $1 ] = GetAutoVarIndex();
1658
                        }
1659
                        ;
1660
%%
1661
 
1662
 
1663
 
1664
// Error function throws an exception (std::string) with the location and error message
1665
void Theia::Parser::error(const Theia::Parser::location_type &loc,
1666
                                          const std::string &msg) {
1667
        std::ostringstream ret;
1668
        ret << "Parser Error at " << loc << ": "  << msg;
1669
        throw ret.str();
1670
}
1671
 
1672
// Now that we have the Parser declared, we can declare the Scanner and implement
1673
// the yylex function
1674
#include "Scanner.h"
1675
static int yylex(Theia::Parser::semantic_type * yylval,
1676
                 Theia::Parser::location_type * yylloc,
1677
                 Theia::Scanner &scanner) {
1678
        return scanner.yylex(yylval, yylloc);
1679
}
1680
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.