[105ed6]: microbe / microbe.cpp  Maximize  Restore  History

Download this file

543 lines (455 with data), 16.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/***************************************************************************
* Copyright (C) 2004-2005 by Daniel Clarke daniel.jc@gmail.com *
* Copyright (C) 2005 by David Saxton *
* *
* 24-04-2007 *
* Modified to add pic 16f877,16f627 and 16f628 *
* by george john george@space-kerala.org *
* supported by SPACE www.space-kerala.org *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "instruction.h"
#include "microbe.h"
#include "parser.h"
#include "optimizer.h"
#include "pic14.h"
#include <kdebug.h>
#include <klocale.h>
#include <qfile.h>
#include <iostream>
using namespace std;
typedef QMap<QString, QString> AliasMap;
//BEGIN class Microbe
Microbe::Microbe()
{
m_maxDelaySubroutine = PIC14::Delay_None;
m_dest = 0;
m_uniqueLabel = 0;
// Hardwired constants
m_aliasList["true"] = "1";
m_aliasList["false"] = "0";
// Things starting with b are reserved by gpasm (for binary numbers)
m_aliasList["b"] = "_b";
//BEGIN Keypad values
int bv[4][6] = {
{ 1, 2, 3, 10, 14, 18 },
{ 4, 5, 6, 11, 15, 19 },
{ 7, 8, 9, 12, 16, 20 },
{ 253, 0, 254, 13, 17, 21 }
};
for ( unsigned row = 0; row < 4; ++row )
{
for ( unsigned col = 0; col < 6; ++col )
{
m_aliasList[ QString("Keypad_%1_%2").arg(row+1).arg(col+1) ] = QString::number( bv[row][col] );
}
}
m_aliasList[ "Keypad_None" ] = "0xff";
//END Keypad values
}
Microbe::~Microbe()
{
}
QString Microbe::compile( const QString & url, bool optimize )
{
QFile file( url );
if ( file.open( QIODevice::ReadOnly ) )
{
QTextStream stream(&file);
unsigned line = 0;
while ( !stream.atEnd() )
m_program += SourceLine( stream.readLine(), url, line++ );
file.close();
simplifyProgram();
}
else
{
m_errorReport += i18n("Could not open file '%1'\n").arg(url);
return 0;
}
Parser parser(this);
// Extract the PIC ID
if ( !m_program.isEmpty() )
{
m_picType = PIC14::toType( m_program[0].text() );
m_program.removeFirst();
}
PIC14 * pic = makePic();
if ( !pic )
return 0;
Code * code = parser.parse( m_program );
pic->setCode( code );
pic->addCommonFunctions( (PIC14::DelaySubroutine)m_maxDelaySubroutine );
pic->postCompileConstruct( m_usedInterrupts );
code->postCompileConstruct();
if ( optimize )
{
Optimizer opt;
opt.optimize( code );
}
return code->generateCode( pic );
}
PIC14 * Microbe::makePic()
{
return new PIC14( this, (PIC14::Type)m_picType );
}
void Microbe::simplifyProgram()
{
SourceLineList simplified;
enum CommentType { None, SingleLine, MultiLine };
CommentType commentType = None;
SourceLineList::const_iterator end = m_program.end();
for ( SourceLineList::const_iterator it = m_program.begin(); it != end; ++it )
{
QString code = (*it).text();
QString simplifiedLine;
if ( commentType == SingleLine )
commentType = None;
unsigned l = code.length();
for ( unsigned i = 0; i < l; ++i )
{
QChar c = code[i];
switch ( c.toAscii() )
{
case '/':
// Look for start of comments in form "//" and "/*"
if ( commentType == None && (i+1 < l) )
{
if ( code[i+1] == '/' )
{
commentType = SingleLine;
i++;
}
else if ( code[i+1] == '*' )
{
commentType = MultiLine;
i++;
}
}
break;
case '*':
// Look for end of comments in form "*/"
if ( commentType == MultiLine && (i+1 < l) && code[i+1] == '/' )
{
i++;
commentType = None;
continue;
}
break;
case '{':
case '}':
// Put braces on seperate lines
if ( commentType != None )
break;
simplified << SourceLine( simplifiedLine.simplified(), (*it).url(), (*it).line() );
simplified << SourceLine( c, (*it).url(), (*it).line() );
simplifiedLine = "";
continue;
}
if ( commentType == None )
simplifiedLine += c;
}
simplified << SourceLine( simplifiedLine.simplified(), (*it).url(), (*it).line() );
}
m_program.clear();
end = simplified.end();
for ( SourceLineList::const_iterator it = simplified.begin(); it != end; ++it )
{
if ( !(*it).text().isEmpty() )
m_program << *it;
}
}
void Microbe::compileError( MistakeType type, const QString & context, const SourceLine & sourceLine )
{
QString message;
switch (type)
{
case UnknownStatement:
message = i18n("Unknown statement");
break;
case InvalidPort:
message = i18n("Port '%1' is not supported by target PIC").arg(context);
break;
case UnassignedPin:
message = i18n("Pin identifier was not followed by '='");
break;
case NonHighLowPinState:
message = i18n("Pin state can only be 'high' or 'low'");
break;
case UnassignedPort:
message = i18n("Invalid token '%1'. Port identifier should be followed by '='").arg(context);
break;
case UnexpectedStatementBeforeBracket:
message = i18n("Unexpected statement before '{'");
break;
case MismatchedBrackets:
message = i18n("Mismatched brackets in expression '%1'").arg(context);
break;
case InvalidEquals:
message = i18n("Invalid '=' found in expression");
break;
case ReservedKeyword:
message = i18n("Reserved keyword '%1' cannot be a variable name.").arg(context);
break;
case ConsecutiveOperators:
message = i18n("Nothing between operators");
break;
case MissingOperator:
message = i18n("Missing operator or space in operand");
break;
case UnknownVariable:
if ( context.isEmpty() )
message = i18n("Unknown variable");
else
message = i18n("Unknown variable '%1'").arg(context);
break;
case UnopenableInclude:
message = i18n("Could not open include file '%1'").arg(context);
break;
case DivisionByZero:
message = i18n("Division by zero");
break;
case NumberTooBig:
message = i18n("Number too big");
break;
case NonConstantStep:
message = i18n("Step can only be a constant expression");
break;
case NonConstantDelay:
message = i18n("Delay must be a positive constant value");
break;
case HighLowExpected:
message = i18n("'high' or 'low' expected after pin expression '%1'").arg(context);
break;
case InvalidComparison:
message = i18n("Comparison operator in '%1' is not recognized");
break;
case SubBeforeEnd:
message = i18n("Subroutine definition before end of program");
break;
case InterruptBeforeEnd:
message = i18n("Interrupt routine definition before end of program");
break;
case LabelExpected:
message = i18n("Label expected");
break;
case TooManyTokens:
message = i18n("Extra tokens at end of line");
break;
case FixedStringExpected:
message = i18n("Expected '%1'").arg(context);
break;
case PinListExpected:
message = i18n("Pin list expected");
break;
case AliasRedefined:
message = i18n("Alias already definied");
break;
case InvalidInterrupt:
message = i18n("Interrupt type not supported by target PIC");
break;
case InterruptRedefined:
message = i18n("Interrupt already definied");
break;
case ReadOnlyVariable:
message = i18n("Variable '%1' is read only").arg(context);
break;
case WriteOnlyVariable:
message = i18n("Variable '%1' is write only").arg(context);
break;
case InvalidPinMapSize:
message = i18n("Invalid pin list size");
break;
case VariableRedefined:
message = i18n("Variable '%1' is already defined").arg(context);
break;
case InvalidVariableName:
message = i18n("'%1' is not a valid variable name").arg(context);
break;
case VariableExpected:
message = i18n("Variable expected");
break;
case NameExpected:
message = i18n("Name expected");
break;
}
m_errorReport += QString("%1:%2:Error [%3] %4\n")
.arg( sourceLine.url() )
.arg( sourceLine.line()+1 )
.arg( type )
.arg( message );
}
bool Microbe::isValidVariableName( const QString & variableName)
{
//*****modified checking is included for preventing the uses of registername as varable name*****
//Prevent usage of register/port name as varable
if (/*PORT-NAME*/ variableName == "PORTA"
|| variableName == "PORTB"
|| variableName == "PORTC"
|| variableName == "PORTD"
|| variableName == "PORTE" /*TRIS-NAME*/
|| variableName == "TRISA"
|| variableName == "TRISB"
|| variableName == "TRISC"
|| variableName == "TRISD"
|| variableName == "TRISE" /**REGISTER-NAME**/
|| variableName == "TMR0"
|| variableName == "PCL"
|| variableName == "STATUS"
|| variableName == "FSR"
|| variableName == "PCLATH"
|| variableName == "INTCON"
|| variableName == "PIR1"
|| variableName == "PIR2"
|| variableName == "TMR1L"
|| variableName == "TMR1H"
|| variableName == "T1CON"
|| variableName == "TMR2"
|| variableName == "T2CON"
|| variableName == "SSPBUF"
|| variableName == "SSPCON"
|| variableName == "CCPR1L"
|| variableName == "CCPR1H"
|| variableName == "CCP1CON"
|| variableName == "RCSTA"
|| variableName == "TXREG"
|| variableName == "RCREG"
|| variableName == "CCPR2L"
|| variableName == "CCPR2H"
|| variableName == "CCP2CON"
|| variableName == "ADRESH"
|| variableName == "ADCON0" /*bank0ends*/
|| variableName == "OPTION_REG"
|| variableName == "PIE1"
|| variableName == "PIE2"
|| variableName == "PCON"
|| variableName == "SSPCON2"
|| variableName == "PR2"
|| variableName == "SSPADD"
|| variableName == "SSPSTAT"
|| variableName == "TXSTA"
|| variableName == "SPBRG"
|| variableName == "ADRESL"
|| variableName == "ADCON1" /*bank1ends*/
|| variableName == "EEDATA "
|| variableName == "EEADR"
|| variableName == "EEDATH"
|| variableName == "EEADRH" /*bank2ends*/
|| variableName == "EECON1"
|| variableName == "EECON2" /*bank3ends*/ )
return false;
//****************************modification ends*******************************
if ( variableName.isEmpty() )
return false;
if ( !variableName[0].isLetter() && !variableName.startsWith('_') )
return false;
for ( int i = 1; i < variableName.length(); ++i )
{
if ( !variableName[i].isLetterOrNumber() && variableName[i] != '_' )
return false;
}
return true;
}
void Microbe::addVariable( const Variable & variable )
{
if ( variable.type() == Variable::invalidType )
return;
if ( !isVariableKnown( variable.name() ) )
m_variables << variable;
}
Variable Microbe::variable( const QString & name ) const
{
VariableList::const_iterator end = m_variables.end();
for ( VariableList::const_iterator it = m_variables.begin(); it != end; ++it )
{
if ( (*it).name() == name )
return *it;
}
return Variable();
}
bool Microbe::isVariableKnown( const QString & name ) const
{
return variable(name).type() != Variable::invalidType;
}
void Microbe::addDelayRoutineWanted( unsigned routine )
{
if ( m_maxDelaySubroutine < routine )
m_maxDelaySubroutine = routine;
}
void Microbe::addAlias( const QString & name, const QString & dest )
{
m_aliasList[name] = dest;
}
QString Microbe::alias( const QString & alias ) const
{
// If the string is an alias, return the real string,
// otherwise just return the alias as that is the real string.
AliasMap::const_iterator it = m_aliasList.find(alias);
if ( it != m_aliasList.constEnd() )
return it.value();
return alias;
}
void Microbe::setInterruptUsed(const QString &interruptName)
{
// Don't add it again if it is already in the list
if ( m_usedInterrupts.contains( interruptName ) )
return;
m_usedInterrupts.append(interruptName);
}
bool Microbe::isInterruptUsed( const QString & interruptName )
{
return m_usedInterrupts.contains( interruptName );
}
QString Microbe::dest() const
{
return QString("__op%1").arg(m_dest);
}
void Microbe::incDest()
{
m_dest++;
// if ( ++m_dest > m_highestDest )
// m_highestDest = m_dest;
}
void Microbe::decDest()
{
m_dest--;
}
void Microbe::resetDest()
{
m_dest = 0;
}
//END class Microbe
//BEGIN class SourceLine
SourceLine::SourceLine( const QString & text, const QString & url, int line )
{
m_text = text;
m_url = url;
m_line = line;
}
SourceLine::SourceLine()
{
m_line = -1;
}
QStringList SourceLine::toStringList( const SourceLineList & lines )
{
QStringList joined;
SourceLineList::const_iterator end = lines.end();
for ( SourceLineList::const_iterator it = lines.begin(); it != end; ++it )
joined << (*it).text();
return joined;
}
//END class SourceLine

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks