summaryrefslogtreecommitdiffstats
path: root/src/piklab-test/base/generator_check.cpp
blob: f7ce96fc8eaabb86600a50eb9b6541861cb17948 (plain)
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
/***************************************************************************
 *   Copyright (C) 2007 Nicolas Hadacek <hadacek@kde.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.                                   *
 ***************************************************************************/
#include "generator_check.h"

#include "devices/base/device_group.h"
#include "devices/pic/pic/pic_memory.h"
#include "tools/gputils/gputils.h"
#include "tools/gputils/gputils_generator.h"
#include "tools/sdcc/sdcc.h"
#include "tools/sdcc/sdcc_generator.h"
#include "devices/list/device_list.h"

//----------------------------------------------------------------------------
GeneratorCheckHelper::GeneratorCheckHelper()
  : _cprocess(0), _lprocess(0), _generator(0)
{}

GeneratorCheckHelper::~GeneratorCheckHelper()
{
  delete _generator;
}

void GeneratorCheckHelper::cleanup()
{
  delete _lprocess;
  _lprocess = 0;
  delete _cprocess;
  _cprocess = 0;
}

//----------------------------------------------------------------------------
GeneratorCheck::GeneratorCheck(GeneratorCheckHelper *helper)
  : _helper(helper), _fdest(0), _fhex(0), _memory1(0)
{
  _view = new CLI::View;
}

GeneratorCheck::~GeneratorCheck()
{
  delete _view;
  delete _helper;
}

void GeneratorCheck::runTest()
{
  _helper->initSupported();
  DeviceTest::runTest();
}

bool GeneratorCheck::init(const Device::Data &data)
{
  PURL::Url dest(PURL::Directory::current(), "test.xxx");
  dest = dest.toFileType(_helper->sourceFileType());
  _fdest = new PURL::File(dest, *_view);
  _helper->init(data);
  PURL::Url hex(PURL::Directory::current(), "test.hex");
  _fhex = new PURL::File(hex, *_view);
  _memory1 = static_cast<Pic::Memory *>(data.group().createMemory(data));
  return true;
}

bool GeneratorCheck::skip(const Device::Data &data) const
{
  return !_helper->isSupported(data);
}

void GeneratorCheck::cleanup(const Device::Data &)
{
  delete _memory1;
  _memory1 = 0;
  delete _fhex;
  _fhex = 0;
//  _fdest->remove();
  delete _fdest;
  _fdest = 0;
  _helper->cleanup();
}

bool GeneratorCheck::execute(const Device::Data &data)
{
  // create asm file from template source code
  if ( !_fdest->openForWrite() ) TEST_FAILED_RETURN("");
  _fdest->appendText(_source);
  _fdest->close();

  // run compiler
  Process::State state = Process::runSynchronously(*_helper->_cprocess, Process::Start, 2000); // 2s timeout
  if ( state!=Process::Exited ) TEST_FAILED_RETURN("Error while running compilation")
  if ( _helper->_cprocess->exitCode()!=0 ) TEST_FAILED_RETURN(TQString("Error in compilation for %1:\n%2%3").tqarg(data.name()).tqarg(_helper->_cprocess->sout()+_helper->_cprocess->serr()).tqarg(TQString()))

  // run linker
  if (_helper->_lprocess) {
    state = Process::runSynchronously(*_helper->_lprocess, Process::Start, 2000); // 2s timeout
    if ( state!=Process::Exited ) TEST_FAILED_RETURN("Error while running linking")
    if ( _helper->_lprocess->exitCode()!=0 ) TEST_FAILED_RETURN(TQString("Error in linking for %1:\n%2%3").tqarg(data.name()).tqarg(_helper->_lprocess->sout()+_helper->_lprocess->serr()).tqarg(TQString()))
  }

  // load hex file
  if ( !_fhex->openForRead() ) TEST_FAILED_RETURN("")
  TQStringList errors, warnings;
  Device::Memory::WarningTypes warningTypes;
  if ( !_memory1->load(_fhex->stream(), errors, warningTypes, warnings) ) TEST_FAILED_RETURN(TQString("Error loading hex into memory: %1").tqarg(errors.join(" ")))
  //if ( warningTypes!=Device::Memory::NoWarning ) TEST_FAILED(TQString("Warning loading hex into memory: %1").tqarg(warnings.join(" ")))

  TEST_PASSED
  return true;
}

//----------------------------------------------------------------------------
bool ConfigGeneratorCheck::init(const Device::Data &data)
{
  if ( !GeneratorCheck::init(data) ) return false;
  _memory2 = static_cast<Pic::Memory *>(data.group().createMemory(data));
  return true;
}

bool ConfigGeneratorCheck::execute(const Device::Data &data)
{
  // create configuration
  const Pic::Config &config = static_cast<const Pic::Data &>(data).config();
  for (uint l=0; ; l++) {
    // set config bits
    bool ok = false;
    for (uint i=0; i<config._words.count(); i++) {
      const Pic::Config::Word &cword = config._words[i];
      for (uint k=0; k<cword.masks.count(); k++) {
        const Pic::Config::Mask &ctqmask = cword.masks[k];
        if ( l<ctqmask.values.count() ) {
          ok = true;
          if ( !ctqmask.values[l].name.isEmpty() ) _memory2->setConfigValue(ctqmask.name, ctqmask.values[l].name);
        }
      }
    }
    if ( !ok ) break;

    // create source code
    PURL::SourceFamily sfamily = _helper->sourceFileType().data().sourceFamily;
    PURL::ToolType ttype = sfamily.data().toolType;
    SourceLine::List lines = _helper->generator()->includeLines(ttype, data);
    lines += _helper->generator()->configLines(ttype, *_memory2, ok);
    lines += _helper->configEndLines();
    _source = SourceLine::text(sfamily, lines, 2);
    if (!ok) TEST_FAILED_RETURN("Config lines generation incomplete")

    if ( !GeneratorCheck::execute(data) ) return false;

    // check that config bits are the same
    uint nbChars = static_cast<const Pic::Data &>(data).nbCharsWord(Pic::MemoryRangeType::Config);
    for (uint i=0; i<config._words.count(); i++) {
      const Pic::Config::Word &cword = config._words[i];
      BitValue word1 = _memory1->word(Pic::MemoryRangeType::Config, i);
      BitValue word2 = _memory2->word(Pic::MemoryRangeType::Config, i);
      if ( word1==word2 ) continue;
      for (uint k=0; k<cword.masks.count(); k++) {
        const Pic::Config::Mask &ctqmask = cword.masks[k];
        if ( ctqmask.value.isInside(cword.ptqmask) ) continue;
        BitValue value1 = word1.maskWith(ctqmask.value);
        BitValue value2 = word2.maskWith(ctqmask.value);
        if ( value1==value2 ) continue;
        TQString name1, name2;
        uint l1, l2;
        for (uint l=0; l<ctqmask.values.count(); l++) {
          const Pic::Config::Value &value = ctqmask.values[l];
          if ( value.value==value1 ) { name1 = value.name; l1 = l; }
          if ( value.value==value2 ) { name2 = value.name; l2 = l; }
        }
        if ( name1==name2 ) continue;
        TEST_FAILED_RETURN(TQString("Config bits are different in %1: set\"%2\"=(%3) != compiled=%4)")
                           .tqarg(ctqmask.name).tqarg(ctqmask.values[l2].name)
                           .tqarg(toHexLabel(word2.maskWith(ctqmask.value), nbChars)).tqarg(toHexLabel(word1.maskWith(ctqmask.value), nbChars)))
      }
    }
  }

  TEST_PASSED
  return true;
}

void ConfigGeneratorCheck::cleanup(const Device::Data &data)
{
  GeneratorCheck::cleanup(data);
  delete _memory2;
  _memory2 = 0;
}

//----------------------------------------------------------------------------
bool TemplateGeneratorCheck::init(const Device::Data &data)
{
  if ( !GeneratorCheck::init(data) ) return false;
  bool ok;
  PURL::SourceFamily sfamily = _helper->sourceFileType().data().sourceFamily;
  PURL::ToolType ttype = sfamily.data().toolType;
  SourceLine::List lines = _helper->generator()->templateSourceFile(ttype, data, ok);
  _source = SourceLine::text(sfamily, lines, 2);
  if (!ok) TEST_FAILED_RETURN(TQString("Incomplete template generator for %1").tqarg(data.name()))
  return true;
}

//----------------------------------------------------------------------------
GPUtilsGeneratorCheckHelper::GPUtilsGeneratorCheckHelper()
{
  _generator = new GPUtils::SourceGenerator;
}

void GPUtilsGeneratorCheckHelper::initSupported()
{
  Process::StringOutput p;
  TQStringList options;
  options += "-l";
  p.setup("gpasm", options, false);
  Process::runSynchronously(p, Process::Start, 2000); // 2s timeout
  _supported = GPUtils::getSupportedDevices(p.sout());
}

bool GPUtilsGeneratorCheckHelper::init(const Device::Data &data)
{
  _cprocess = new Process::StringOutput;
  TQStringList options;
  options = "-c";
  options += "-p" + GPUtils::toDeviceName(data.name());
  options += "test.asm";
  _cprocess->setup("gpasm", options, false);
  _lprocess = new Process::StringOutput;
  options = "-o";
  options += "test.hex";
  options += "test.o";
  _lprocess->setup("gplink", options, false);
  return true;
}

SourceLine::List GPUtilsGeneratorCheckHelper::configEndLines() const
{
  SourceLine::List lines;
  lines.appendIndentedCode("end");
  return lines;
}

//----------------------------------------------------------------------------
SDCCGeneratorCheckHelper::SDCCGeneratorCheckHelper()
{
  _generator = new SDCC::SourceGenerator;
}

void SDCCGeneratorCheckHelper::initSupported()
{
  PURL::Url url(PURL::Directory::current(), "test.c");
  Log::StringView view;
  PURL::File file(url, view);
  if ( file.openForWrite() ) file.appendText("void main(void) {}\n");
  file.close();
  _supported.clear();
  for (uint i=0; i<SDCC::Nb_Families; i++) {
    Process::StringOutput p;
    TQStringList options;
    options += TQString("-m") + SDCC::FAMILY_DATA[i].name;
    options += "-phelp";
    options += "test.c";
    p.setup("sdcc", options, false);
    Process::runSynchronously(p, Process::Start, 2000); // 2s timeout
    _supported += SDCC::getSupportedDevices(p.serr());
  }
}

bool SDCCGeneratorCheckHelper::init(const Device::Data &data)
{
  _cprocess = new Process::StringOutput;
  TQStringList options;
  options += TQString("-m") + SDCC::FAMILY_DATA[SDCC::family(data.name())].name;
  options += "-" + SDCC::toDeviceName(data.name());
  options += "test.c";
  options += "-I/usr/share/gputils/header";
  options += "-Wl-otext.hex";
  _cprocess->setup("sdcc", options, false);
  return true;
}

SourceLine::List SDCCGeneratorCheckHelper::configEndLines() const
{
  SourceLine::List lines;
  lines.appendIndentedCode("void main() {}");
  return lines;
}