summaryrefslogtreecommitdiffstats
path: root/tdecachegrind/tdecachegrind/instritem.cpp
blob: ce5e81bbb894fd9d8d2ea21e8840a9780f27cce7 (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
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
/* This file is part of KCachegrind.
   Copyright (C) 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>

   KCachegrind 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, version 2.

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

/*
 * Items of instruction view.
 */

#include <tqpixmap.h>
#include <tqpainter.h>

#include <klocale.h>
#include <kapplication.h>
#include <kiconloader.h>
#include <kdebug.h>

#include "configuration.h"
#include "listutils.h"
#include "instritem.h"
#include "instrview.h"


// InstrItem

// for messages
InstrItem::InstrItem(InstrView* iv, TQListView* parent,
		     Addr addr, const TQString& msg)
    : TQListViewItem(parent)
{
  _view = iv;
  _addr = addr;
  _instr = 0;
  _instrCall = 0;
  _instrJump = 0;
  _inside = false;
  
  setText(0, addr.pretty());
  setText(6, msg);
  
  updateGroup();
  updateCost();
}

// for code lines
InstrItem::InstrItem(InstrView* iv, TQListView* parent,
		     Addr addr, bool inside,
		     const TQString& code, const TQString& cmd,
		     const TQString& args, TraceInstr* instr)
    : TQListViewItem(parent)
{
  _view = iv;
  _addr = addr;
  _instr = instr;
  _instrCall = 0;
  _instrJump = 0;
  _inside = inside;

  if (args == "...")
      setText(0, args);
  else
      setText(0, addr.pretty());
  setText(4, code);
  setText(5, cmd);
  setText(6, args);

  TraceLine* l;
  if (instr && (l = instr->line()))
      setText(7, l->name());

  updateGroup();
  updateCost();
}

// for call lines
InstrItem::InstrItem(InstrView* iv, TQListViewItem* parent, Addr addr,
		     TraceInstr* instr, TraceInstrCall* instrCall)
    : TQListViewItem(parent)
{
  _view = iv;
  _addr = addr;
  _instr = instr;
  _instrCall = instrCall;
  _instrJump = 0;
  _inside = true;

  //qDebug("InstrItem: (file %d, line %d) Linecall to %s",
  //       fileno, lineno, _lineCall->call()->called()->prettyName().ascii());

  SubCost cc = _instrCall->callCount();
  TQString templ = "  ";
  if (cc==0)
    templ += i18n("Active call to '%1'");
  else
    templ += i18n("%n call to '%1'", "%n calls to '%1'", cc);

  TQString callStr = templ.arg(_instrCall->call()->calledName());
  TraceFunction* calledF = _instrCall->call()->called();
  calledF->addPrettyLocation(callStr);

  setText(6, callStr);

  updateGroup();
  updateCost();
}

// for jump lines
InstrItem::InstrItem(InstrView* iv, TQListViewItem* parent, Addr addr,
		     TraceInstr* instr, TraceInstrJump* instrJump)
    : TQListViewItem(parent)
{
  _view = iv;
  _addr = addr;
  _inside = true;
  _instr = instr;
  _instrCall = 0;
  _instrJump = instrJump;

  //qDebug("SourceItem: (file %d, line %d) Linecall to %s",
  //       fileno, lineno, _lineCall->call()->called()->prettyName().ascii());

  TQString jStr;
  if (_instrJump->isCondJump())
      jStr = i18n("Jump %1 of %2 times to 0x%3")
	  .arg(_instrJump->followedCount().pretty())
	  .arg(_instrJump->executedCount().pretty())
	  .arg(_instrJump->instrTo()->addr().toString());
  else
      jStr = i18n("Jump %1 times to 0x%2")
	  .arg(_instrJump->executedCount().pretty())
	  .arg(_instrJump->instrTo()->addr().toString());

  setText(6, jStr);

  updateGroup();
  updateCost();
}


void InstrItem::updateGroup()
{
  if (!_instrCall) return;

  TraceFunction* f = _instrCall->call()->called();
  TQColor c = Configuration::functionColor(_view->groupType(), f);
  setPixmap(6, colorPixmap(10, 10, c));
}

void InstrItem::updateCost()
{
  _pure = SubCost(0);
  _pure2 = SubCost(0);

  if (!_instr) return;
  if (_instrJump) return;

  TraceCost* instrCost = _instrCall ?
      (TraceCost*)_instrCall : (TraceCost*)_instr;

  // don't show any cost inside of cycles
  if (_instrCall &&
      ((_instrCall->call()->inCycle()>0) ||
       (_instrCall->call()->isRecursion()>0))) {
    TQString str;
    TQPixmap p;

    TQString icon = "undo";
    KIconLoader* loader = KApplication::kApplication()->iconLoader();
    p= loader->loadIcon(icon, KIcon::Small, 0,
                        KIcon::DefaultState, 0, true);
    if (p.isNull())
      str = i18n("(cycle)");

    setText(1, str);
    setPixmap(1, p);
    setText(2, str);
    setPixmap(2, p);
    return;
  }

  TraceCost* totalCost;
  if (Configuration::showExpanded())
      totalCost = _instr->function()->inclusive();
  else
      totalCost = _instr->function()->data();

  TraceCostType *ct = _view->costType();
  _pure = ct ? instrCost->subCost(ct) : SubCost(0);
  if (_pure == 0) {
    setText(1, TQString());
    setPixmap(1, TQPixmap());
  }
  else {
    double total = totalCost->subCost(ct);
    double pure  = 100.0 * _pure / total;

    if (Configuration::showPercentage())
      setText(1, TQString("%1")
	      .arg(pure, 0, 'f', Configuration::percentPrecision()));
    else
      setText(1, _pure.pretty());
    
    setPixmap(1, costPixmap(ct, instrCost, total, false));
  }

  TraceCostType *ct2 = _view->costType2();
  _pure2 = ct2 ? instrCost->subCost(ct2) : SubCost(0);
  if (_pure2 == 0) {
    setText(2, TQString());
    setPixmap(2, TQPixmap());
  }
  else {
    double total = totalCost->subCost(ct2);
    double pure  = 100.0 * _pure2 / total;

    if (Configuration::showPercentage())
      setText(2, TQString("%1")
	      .arg(pure, 0, 'f', Configuration::percentPrecision()));
    else
      setText(2, _pure2.pretty());
    
    setPixmap(2, costPixmap(ct2, instrCost, total, false));
  }
}


int InstrItem::compare(TQListViewItem * i, int col, bool ascending ) const
{
  const InstrItem* ii1 = this;
  const InstrItem* ii2 = (InstrItem*) i;

  // we always want descending order
  if (((col>0) && ascending) ||
      ((col==0) && !ascending) ) {
    ii1 = ii2;
    ii2 = this;
  }

  if (col==1) {
    if (ii1->_pure < ii2->_pure) return -1;
    if (ii1->_pure > ii2->_pure) return 1;
    return 0;
  }
  if (col==2) {
    if (ii1->_pure2 < ii2->_pure2) return -1;
    if (ii1->_pure2 > ii2->_pure2) return 1;
    return 0;
  }
  if (col==0) {
    if (ii1->_addr < ii2->_addr) return -1;
    if (ii1->_addr > ii2->_addr) return 1;

    // Same address: code gets above calls/jumps
    if (!ii1->_instrCall && !ii1->_instrJump) return -1;
    if (!ii2->_instrCall && !ii2->_instrJump) return 1;

    // calls above jumps
    if (ii1->_instrCall && !ii2->_instrCall) return -1;
    if (ii2->_instrCall && !ii1->_instrCall) return 1;

    if (ii1->_instrCall && ii2->_instrCall) {
	// Two calls: desending sort according costs
	if (ii1->_pure < ii2->_pure) return 1;
	if (ii1->_pure > ii2->_pure) return -1;

	// Two calls: sort according function names
	TraceFunction* f1 = ii1->_instrCall->call()->called();
	TraceFunction* f2 = ii2->_instrCall->call()->called();
	if (f1->prettyName() > f2->prettyName()) return 1;
	return -1;
    }

    // Two jumps: descending sort according target address
    if (ii1->_instrJump->instrTo()->addr() <
	ii2->_instrJump->instrTo()->addr())
	return -1;
    if (ii1->_instrJump->instrTo()->addr() >
	ii2->_instrJump->instrTo()->addr())
	return 1;
    return 0;

  }
  return TQListViewItem::compare(i, col, ascending);
}

void InstrItem::paintCell( TQPainter *p, const TQColorGroup &cg,
			   int column, int width, int alignment )
{
  TQColorGroup _cg( cg );

  if ( !_inside || ((column==1) || column==2))
    _cg.setColor( TQColorGroup::Base, cg.button() );
  else if ((_instrCall || _instrJump) && column>2)
    _cg.setColor( TQColorGroup::Base, cg.midlight() );

  if (column == 3)
    paintArrows(p, _cg, width);
  else
    TQListViewItem::paintCell( p, _cg, column, width, alignment );
}

void InstrItem::setJumpArray(const TQMemArray<TraceInstrJump*>& a)
{
    _jump.duplicate(a);
}

void InstrItem::paintArrows(TQPainter *p, const TQColorGroup &cg, int width)
{
  TQListView *lv = listView();
  if ( !lv ) return;
  InstrView* iv = (InstrView*) lv;

  const BackgroundMode bgmode = lv->viewport()->backgroundMode();
  const TQColorGroup::ColorRole crole
    = TQPalette::backgroundRoleFromMode( bgmode );
  if ( cg.brush( crole ) != lv->colorGroup().brush( crole ) )
    p->fillRect( 0, 0, width, height(), cg.brush( crole ) );
  else
    iv->paintEmptyArea( p, TQRect( 0, 0, width, height() ) );

  if ( isSelected() && lv->allColumnsShowFocus() )
    p->fillRect( 0, 0, width, height(), cg.brush( TQColorGroup::Highlight ) );

  int marg = lv->itemMargin();
  int yy = height()/2, y1, y2;
  TQColor c;

  int start = -1, end = -1;

  // draw line borders, detect start/stop of a line
  for(int i=0;i< (int)_jump.size();i++) {
      if (_jump[i] == 0) continue;

      y1 = 0;
      y2 = height();
      if ((_instrJump == _jump[i]) &&
	  (_jump[i]->instrFrom()->addr() == _addr)) {

	  //kdDebug() << "InstrItem " << _addr.toString() << ": start " << i << endl;
	  if (start<0) start = i;
	  if (_jump[i]->instrTo()->addr() <= _addr)
	      y2 = yy;
	  else
	      y1 = yy;
      }
      else if (!_instrJump && !_instrCall &&
	       (_jump[i]->instrTo()->addr() == _addr)) {

	  //kdDebug() << "InstrItem " << _addr.toString() << ": end " << i << endl;
	  if (end<0) end = i;
	  if (_jump[i]->instrFrom()->addr() < _addr)
	      y2 = yy;
	  else
	      y1 = yy;
      }

      c = _jump[i]->isCondJump() ? red : blue;
#if 0
      if (_jump[i] == ((TraceItemView*)_view)->selectedItem()) {	  
	  p->fillRect( marg + 6*i-2, (y1==0) ? y1: y1-2,
		       8, (y2-y1==height())? y2:y2+2,
		       cg.brush( TQColorGroup::Highlight ) );
	  c = lv->colorGroup().highlightedText();
      }
#endif
      p->fillRect( marg + 6*i, y1, 4, y2, c);
      p->setPen(c.light());
      p->drawLine( marg + 6*i, y1, marg + 6*i, y2);
      p->setPen(c.dark());
      p->drawLine( marg + 6*i +3, y1, marg + 6*i +3, y2);
  }

  // draw start/stop horizontal line
  int x, y = yy-2, w, h = 4;
  if (start >= 0) {
#if 0
      if (_jump[start] == ((TraceItemView*)_view)->selectedItem()) {	  
	  c = lv->colorGroup().highlightedText();
      }
#endif
      c = _jump[start]->isCondJump() ? red : blue;
      x = marg + 6*start;
      w = 6*(iv->arrowLevels() - start) + 10;
      p->fillRect( x, y, w, h, c);
      p->setPen(c.light());
      p->drawLine(x, y, x+w-1, y);
      p->drawLine(x, y, x, y+h-1);
      p->setPen(c.dark());
      p->drawLine(x+w-1, y, x+w-1, y+h-1);
      p->drawLine(x+1, y+h-1, x+w-1, y+h-1);
  }
  if (end >= 0) {
      c = _jump[end]->isCondJump() ? red : blue;
      x = marg + 6*end;
      w = 6*(iv->arrowLevels() - end) + 10;

      TQPointArray a;
      a.putPoints(0, 7, x, y+h,
		  x,y, x+w-8, y, x+w-8, y-2,
		  x+w, yy,
		  x+w-8, y+h+2, x+w-8, y+h);
      p->setBrush(c);
      p->drawConvexPolygon(a);

      p->setPen(c.light());
      p->drawPolyline(a, 0, 5);
      p->setPen(c.dark());
      p->drawPolyline(a, 4, 2);
      p->setPen(c.light());
      p->drawPolyline(a, 5, 2);
      p->setPen(c.dark());
      p->drawPolyline(a, 6, 2);
  }

  // draw inner vertical line for start/stop
  // this overwrites borders of horizontal line
  for(int i=0;i< (int)_jump.size();i++) {
      if (_jump[i] == 0) continue;

      c = _jump[i]->isCondJump() ? red : blue;

      if (_jump[i]->instrFrom()->addr() == _addr) {
	  bool drawUp = true;
	  if (_jump[i]->instrTo()->addr() == _addr)
	      if (start<0) drawUp=false;	  
	  if (_jump[i]->instrTo()->addr() > _addr) drawUp=false;
	  if (drawUp)
	      p->fillRect( marg + 6*i +1, 0, 2, yy, c);
	  else
	      p->fillRect( marg + 6*i +1, yy, 2, height()-yy, c);
      }
      else if (_jump[i]->instrTo()->addr() == _addr) {
	  if (end<0) end = i;
	  if (_jump[i]->instrFrom()->addr() < _addr)
	      p->fillRect( marg + 6*i +1, 0, 2, yy, c);
	  else
	      p->fillRect( marg + 6*i +1, yy, 2, height()-yy, c);
      }
  }

}

int InstrItem::width( const TQFontMetrics& fm,
                      const TQListView* lv, int c ) const
{
  if (c != 3) return TQListViewItem::width(fm, lv, c);

  InstrView* iv = (InstrView*) lv;
  int levels = iv->arrowLevels();

  if (levels == 0) return 0;

  // 10 pixels for the arrow
  return 10 + 6*levels + lv->itemMargin() * 2;
}