File: stack.cc

package info (click to toggle)
cc1111 2.9.0-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 38,692 kB
  • ctags: 132,262
  • sloc: ansic: 442,650; cpp: 37,006; sh: 10,334; makefile: 5,511; asm: 5,279; yacc: 2,953; lisp: 1,524; perl: 807; awk: 493; python: 468; lex: 447
file content (540 lines) | stat: -rw-r--r-- 10,985 bytes parent folder | download | duplicates (8)
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
/*
 * Simulator of microcontrollers (stack.cc)
 *
 * Copyright (C) 2000,00 Drotos Daniel, Talker Bt.
 * 
 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
 *
 */

/* This file is part of microcontroller simulator: ucsim.

UCSIM 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.

UCSIM 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 UCSIM; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
/*@1@*/

#include <stdlib.h>

// cmd.src
#include "newcmdcl.h"

// sim.src
#include "stackcl.h"


static class cl_stack_error_registry stack_error_registry;

cl_stack_op::cl_stack_op(enum stack_op op,
                         t_addr iPC,
                         t_addr iSP_before, t_addr iSP_after):
  cl_base()
{
  operation= op;
  PC= iPC;
  //addr= iaddr;
  //data= idata;
  SP_before= iSP_before;
  SP_after= iSP_after;
}

cl_stack_op::~cl_stack_op(void)
{
}


class cl_stack_op *
cl_stack_op::mk_copy(void)
{
  class cl_stack_op *so= new cl_stack_op(*this);
  return(so);
}

void
cl_stack_op::info_head(class cl_console_base *con)
{
  con->dd_printf("OP   SP before-after   L DATA/ADDR   INSTRUCTION\n");
}

void
cl_stack_op::info(class cl_console_base *con, class cl_uc *uc)
{
  con->dd_printf("%-4s 0x%06"_A_"x-0x%06"_A_"x %d ",
                get_op_name(), SP_before, SP_after, abs(SP_before-SP_after));
  print_info(con);
  con->dd_printf(" ");
  uc->print_disass(PC, con);
  //con->dd_printf("\n");
}

const char *
cl_stack_op::get_op_name(void)
{
  return("op");
}

void
cl_stack_op::print_info(class cl_console_base *con)
{
  con->dd_printf("-");
}

bool
cl_stack_op::sp_increased(void)
{
  if (operation & stack_write_operation)
    return(SP_after > SP_before);
  else // read operation
    return(SP_after < SP_before);
}

int
cl_stack_op::data_size(void)
{
  int r= SP_after - SP_before;

  return(r<0?-r:r);
}

int
cl_stack_op::match(class cl_stack_op *op)
{
  return(DD_FALSE);
}

bool
cl_stack_op::can_removed(class cl_stack_op *op)
{
  bool incr= sp_increased(); // FIXME
  bool op_incr= op->sp_increased(); // FIXME

  if ((incr && !op_incr) ||
      (!incr && op_incr))
    {
      printf("BIGBIG ERROR!\n");
      return(DD_FALSE);
    }
  if (incr)
    {
      t_mem opa= op->get_after();
      return(SP_before >= opa);
    }
  else
    {
      t_mem opa= op->get_after();
      return(SP_before <= opa);
    }
}


/*
 * CALL operation on stack
 */

cl_stack_call::cl_stack_call(t_addr iPC, t_addr called, t_addr pushed,
                            t_addr iSP_before, t_addr iSP_after):
  cl_stack_op(stack_call, iPC, iSP_before, iSP_after)
{
  called_addr= called;
  pushed_addr= pushed;
}

class cl_stack_op *
cl_stack_call::mk_copy(void)
{
  class cl_stack_call *so= new cl_stack_call(*this);
  return(so);
}

const char *
cl_stack_call::get_op_name(void)
{
  return("call");
}

void
cl_stack_call::print_info(class cl_console_base *con)
{
  con->dd_printf("0x%06"_A_"x", called_addr);
}

const char *
cl_stack_call::get_matching_name(void)
{
  return("ret");
}

enum stack_op
cl_stack_call::get_matching_op(void)
{
  return(stack_ret);
}

bool
cl_stack_call::match(class cl_stack_op *op)
{
  return(op->get_op() == stack_ret);
}


/*
 * INTERRUPT operation (call) on stack
 */

cl_stack_intr::cl_stack_intr(t_addr iPC, t_addr called, t_addr pushed,
                            t_addr iSP_before, t_addr iSP_after):
  cl_stack_call(iPC, called, pushed, iSP_before, iSP_after)
{
  //called_addr= called;
  //pushed_addr= pushed;
  operation= stack_intr;
}

class cl_stack_op *
cl_stack_intr::mk_copy(void)
{
  class cl_stack_intr *so= new cl_stack_intr(*this);
  return(so);
}

const char *
cl_stack_intr::get_op_name(void)
{
  return("intr");
}

void
cl_stack_intr::print_info(class cl_console_base *con)
{
  con->dd_printf("0x%06"_A_"x", called_addr);
}

const char *
cl_stack_intr::get_matching_name(void)
{
  return("iret");
}

enum stack_op
cl_stack_intr::get_matching_op(void)
{
  return(stack_iret);
}

int
cl_stack_intr::match(class cl_stack_op *op)
{
  return(op->get_op() == stack_iret);
}


/*
 * PUSH operation on stack
 */

cl_stack_push::cl_stack_push(t_addr iPC, t_mem idata,
                            t_addr iSP_before, t_addr iSP_after):
  cl_stack_op(stack_push, iPC, iSP_before, iSP_after)
{
  data= idata;
}

class cl_stack_op *
cl_stack_push::mk_copy(void)
{
  class cl_stack_push *so= new cl_stack_push(*this);
  return(so);
}

const char *
cl_stack_push::get_op_name(void)
{
  return("push");
}

const char *
cl_stack_push::get_matching_name(void)
{
  return("pop");
}

enum stack_op
cl_stack_push::get_matching_op(void)
{
  return(stack_pop);
}

void
cl_stack_push::print_info(class cl_console_base *con)
{
  t_addr d= data;
  con->dd_printf("0x%06"_A_"x", d);
}

int
cl_stack_push::match(class cl_stack_op *op)
{
  return(op->get_op() == stack_pop);
}


/*
 * RETURN operation on stack
 */

cl_stack_ret::cl_stack_ret(t_addr iPC, t_addr iaddr,
                          t_addr iSP_before, t_addr iSP_after):
  cl_stack_call(iPC, iaddr, 0, iSP_before, iSP_after)
{
  operation= stack_ret;
}

class cl_stack_op *
cl_stack_ret::mk_copy(void)
{
  class cl_stack_ret *so= new cl_stack_ret(*this);
  return(so);
}

const char *
cl_stack_ret::get_op_name(void)
{
  return("ret");
}

const char *
cl_stack_ret::get_matching_name(void)
{
  return("call");
}

enum stack_op
cl_stack_ret::get_matching_op(void)
{
  return(stack_call);
}

int
cl_stack_ret::match(class cl_stack_op *op)
{
  return(op->get_op() == stack_call);
}


/*
 * RETURN from interrupt operation on stack
 */

cl_stack_iret::cl_stack_iret(t_addr iPC, t_addr iaddr,
                            t_addr iSP_before, t_addr iSP_after):
  cl_stack_ret(iPC, iaddr, iSP_before, iSP_after)
{
  operation= stack_iret;
}

class cl_stack_op *
cl_stack_iret::mk_copy(void)
{
  class cl_stack_iret *so= new cl_stack_iret(*this);
  return(so);
}

const char *
cl_stack_iret::get_op_name(void)
{
  return("iret");
}

const char *
cl_stack_iret::get_matching_name(void)
{
  return("intr");
}

enum stack_op
cl_stack_iret::get_matching_op(void)
{
  return(stack_intr);
}

int
cl_stack_iret::match(class cl_stack_op *op)
{
  return(op->get_op() == stack_intr);
}


/*
 * POP operation on stack
 */

cl_stack_pop::cl_stack_pop(t_addr iPC, t_mem idata,
                          t_addr iSP_before, t_addr iSP_after):
  cl_stack_push(iPC, idata, iSP_before, iSP_after)
{
  operation= stack_pop;
}

class cl_stack_op *
cl_stack_pop::mk_copy(void)
{
  class cl_stack_pop *so= new cl_stack_pop(*this);
  return(so);
}

const char *
cl_stack_pop::get_op_name(void)
{
  return("pop");
}

const char *
cl_stack_pop::get_matching_name(void)
{
  return("push");
}

enum stack_op
cl_stack_pop::get_matching_op(void)
{
  return(stack_push);
}

int
cl_stack_pop::match(class cl_stack_op *op)
{
  return(op->get_op() == stack_push);
}


/*
 * Stack Errors
 */

cl_error_stack::cl_error_stack(void)
{
  classification = stack_error_registry.find("stack");
}

/* Stack Tracker Errors */

cl_error_stack_tracker::cl_error_stack_tracker(void)
{
  classification = stack_error_registry.find("stack_tracker");
}

/* Stack Tracker: wrong handle */

cl_error_stack_tracker_wrong_handle::cl_error_stack_tracker_wrong_handle(bool write_op):
  cl_error_stack_tracker()
{
  write_operation= write_op;
  classification = stack_error_registry.find("stack_tracker_wrong_handle");
}

void
cl_error_stack_tracker_wrong_handle::print(class cl_commander_base *c)
{
  c->dd_printf("%s: wrong stack tracker handle called for %s operation\n",
               get_type_name(), write_operation?"write":"read");
}

/* Stack Tracker: operation on empty stack */

cl_error_stack_tracker_empty::
cl_error_stack_tracker_empty(class cl_stack_op *op):
  cl_error_stack_tracker()
{
  operation= op->mk_copy();
  classification = stack_error_registry.find("operation_on_empty_stack");
}

cl_error_stack_tracker_empty::~cl_error_stack_tracker_empty(void)
{
  delete operation;
}

void
cl_error_stack_tracker_empty::print(class cl_commander_base *c)
{
  c->dd_printf("%s(0x%06"_A_"x: %s on empty stack, PC="
               "0x06"_A_"x, SP=0x%06"_A_"x->0x%06"_A_"x\n",
               get_type_name(), operation->get_pc(), operation->get_op_name(),
               operation->get_before(), operation->get_after());
}

/* Stack Tracker: operation on empty stack */

cl_error_stack_tracker_unmatch::
cl_error_stack_tracker_unmatch(class cl_stack_op *Top, class cl_stack_op *op):
  cl_error_stack_tracker()
{
  top= Top->mk_copy();
  operation= op->mk_copy();
  classification = stack_error_registry.find("stack_operation_unmatched_to_top_of_stack");
}

cl_error_stack_tracker_unmatch::~cl_error_stack_tracker_unmatch(void)
{
  delete operation;
  delete top;
}

void
cl_error_stack_tracker_unmatch::print(class cl_commander_base *c)
{
  c->dd_printf("%s(0x%06"_A_"x): %s when %s expected, "
               "SP=0x%06"_A_"x->0x%06"_A_"x\n",
               get_type_name(), operation->get_pc(),
               operation->get_op_name(), top->get_matching_name(),
               operation->get_before(), operation->get_after());
}

/* Stack Tracker: stack is inconsistent */

cl_error_stack_tracker_inconsistent::
cl_error_stack_tracker_inconsistent(class cl_stack_op *op,
                                   int the_unread_data_size)
{
  operation= op->mk_copy();
  unread_data_size= the_unread_data_size;
  classification = stack_error_registry.find("stack_looks_corrupted");
}

cl_error_stack_tracker_inconsistent::~cl_error_stack_tracker_inconsistent(void)
{
  delete operation;
}

void
cl_error_stack_tracker_inconsistent::print(class cl_commander_base *c)
{
  c->dd_printf("%s(0x%06"_A_"x): %d byte(s) unread from the stack\n",
               get_type_name(), operation->get_pc(),
               unread_data_size);
}

cl_stack_error_registry::cl_stack_error_registry(void)
{
  class cl_error_class *prev = stack_error_registry.find("non-classified");
  prev = register_error(new cl_error_class(err_error, "stack", prev, ERROR_OFF));
  prev = register_error(new cl_error_class(err_error, "stack_tracker", prev));
  prev = register_error(new cl_error_class(err_error, "stack_tracker_wrong_handle", prev));
  prev = register_error(new cl_error_class(err_error, "operation_on_empty_stack", prev));
  prev = register_error(new cl_error_class(err_warning, "stack_operation_unmatched_to_top_of_stack", prev));
  prev = register_error(new cl_error_class(err_warning, "stack_looks_corrupted", prev));
}


/* End of sim.src/stack.cc */