summaryrefslogtreecommitdiffstats
path: root/src/libgui/object_view.cpp
blob: 1854b8a117435efdf8d543520f9eba1469a5d3a1 (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
/***************************************************************************
 *   Copyright (C) 2006 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 "object_view.h"

#include <klocale.h>

#include "devices/base/device_group.h"
#include "devices/pic/pic/pic_memory.h"
#include "tools/gputils/gputils.h"
#include "hex_editor.h"
#include "coff/base/text_coff.h"
#include "coff/base/coff_archive.h"
#include "main_global.h"
#include "tools/list/compile_process.h"
#include "coff/base/disassembler.h"
#include "common/gui/misc_gui.h"

//-----------------------------------------------------------------------------
Coff::BaseEditor::BaseEditor(const PURL::Url &source, const Device::Data *data, TQWidget *parent)
  : SimpleTextEditor(true, parent), _source(source), _ok(false), _created(0), _device(data)
{
  setReadOnly(true);
  _view->setDynWordWrap(false);
  setView(&Main::compileLog());
}

Coff::BaseEditor::~BaseEditor()
{
  delete _created;
}

//-----------------------------------------------------------------------------
Coff::CoffEditor::CoffEditor(const PURL::Url &source, const Device::Data &data, TQWidget *parent)
  : BaseEditor(source, &data, parent), _provided(0)
{}

Coff::CoffEditor::CoffEditor(const TextObject &object, TQWidget *parent)
  : BaseEditor(PURL::Url(), object.device(), parent), _provided(&object)
{}

bool Coff::CoffEditor::open(const PURL::Url &url)
{
  _url = url;
  if (_provided) return setText(_provided->disassembly());
  if ( _created==0 ) {
    _created = new TextObject(_device, _source);
    _ok = _created->parse(*this);
  }
  return setText(static_cast<Coff::TextObject *>(_created)->disassembly());
}

//-----------------------------------------------------------------------------
Coff::ObjectEditor::ObjectEditor(const PURL::Url &source, TQWidget *parent)
  : BaseEditor(source, 0, parent)
{}

bool Coff::ObjectEditor::open(const PURL::Url &url)
{
  _url = url;
  if ( _created==0 ) {
    _created = new TextObject(0, _source);
    _ok = _created->parse(*this);
  }
  if ( !_ok ) return setText(i18n("Error parsing object:\n") + error());
  TQString s = coff()->information().text() + "\n";
  return setText(s);
}

//-----------------------------------------------------------------------------
Coff::LibraryEditor::LibraryEditor(const PURL::Url &source, TQWidget *parent)
  : BaseEditor(source, 0, parent)
{}

bool Coff::LibraryEditor::open(const PURL::Url &url)
{
  _url = url;
  if ( _created==0 ) {
    _created = new Archive(_source);
    _ok = _created->parse(*this);
  }
  if ( !_ok ) return setText(i18n("Error parsing library:\n") + error());
  TQString s = coff()->information().text() + "\n";
  if ( coff()->members().count()!=0 ) s += coff()->membersInformation().text() + "\n";
  if ( coff()->symbols().count()!=0 ) s += coff()->symbolsInformation().text() + "\n";
  return setText(s);
}

//-----------------------------------------------------------------------------
DisassemblyEditor::DisassemblyEditor(const PURL::Url &source, const Device::Data &data, TQWidget *parent)
  : SimpleTextEditor(true, parent), _source(source), _device(data), _editor(0)
{
  setReadOnly(true);
  _view->setDynWordWrap(false);
  setView(&Main::compileLog());
}

DisassemblyEditor::DisassemblyEditor(const HexEditor &e, const Device::Data &data, TQWidget *parent)
  : SimpleTextEditor(true, parent), _device(data), _editor(&e)
{
  setReadOnly(true);
  _view->setDynWordWrap(false);
  setView(&Main::compileLog());
}

bool DisassemblyEditor::open(const PURL::Url &url)
{
  _url = url;
  if ( Main::toolGroup().name()!="sdcc" && Main::toolGroup().name()!="gputils" ) {
    MessageBox::sorry(i18n("Disassembly not supported for the selected toolchain."), Log::Show);
    return false;
  }
  Pic::Architecture arch = (_device.group().name()=="pic" ? static_cast<const Pic::Data &>(_device).architecture() : Pic::Architecture(Pic::Architecture::Nb_Types));
  if ( arch==Pic::Architecture::Nb_Types ) {
    MessageBox::sorry(i18n("Disassembly not supported for the selected device."), Log::Show);
    return false;
  }

  Device::Memory *memory = 0;
  if ( _editor==0 ) {
    log(Log::LineType::Information, i18n("Disassembling hex file: %1").arg(_source.pretty()));
    PURL::File file(_source, Main::compileLog());
    if ( !file.openForRead() ) return false;
    memory = _device.group().createMemory(_device);
    TQStringList errors, warnings;
    Device::Memory::WarningTypes warningTypes;
    if ( !memory->load(file.stream(), errors, warningTypes, warnings) ) {
      delete memory;
      log(Log::LineType::Error, errors[0]);
      return false;
    }
    for (uint i=0; i<warnings.count(); i++) log(Log::LineType::Warning, warnings[i]);
  } else {
    log(Log::LineType::Information, i18n("Disassembling content of hex file editor."));
    memory = const_cast<Device::Memory *>(_editor->memory());
  }
  SourceLine::List list = GPUtils::disassemble(static_cast<const Pic::Memory &>(*memory));
  if ( _editor==0 ) delete memory;
  return setText(SourceLine::text(PURL::SourceFamily::Asm, list, 4));
}