summaryrefslogtreecommitdiff
path: root/src/pdk/peep.c
blob: 153b0907a08855317803f958a877e41299777ed0 (plain) (blame)
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
#include "common.h"
#include "SDCCgen.h"

#include "peep.h"

#define NOTUSEDERROR() do {werror(E_INTERNAL_ERROR, __FILE__, __LINE__, "error in notUsed()");} while(0)

// #define D(_s) { printf _s; fflush(stdout); }
#define D(_s)

#define ISINST(l, i) (!STRNCASECMP((l), (i), sizeof(i) - 1) && (!(l)[sizeof(i) - 1] || isspace((unsigned char)((l)[sizeof(i) - 1]))))

typedef enum
{
  S4O_CONDJMP,
  S4O_WR_OP,
  S4O_RD_OP,
  S4O_TERM,
  S4O_VISITED,
  S4O_ABORT,
  S4O_CONTINUE
} S4O_RET;

static struct
{
  lineNode *head;
} _G;

static bool
isReturned(const char *what)
{
  symbol *sym;
  sym_link *sym_lnk;
  int size;
  lineNode *l;

  l = _G.head;
  do
  {
    l = l->next;
  } while(l->isComment || l->ic == NULL || l->ic->op != FUNCTION);

  sym = OP_SYMBOL(IC_LEFT(l->ic));

  if(sym && IS_DECL(sym->type))
    {
      // Find size of return value.
      specifier *spec;
      if(sym->type->select.d.dcl_type != FUNCTION)
        NOTUSEDERROR();
      spec = &(sym->etype->select.s);
      if(spec->noun == V_VOID)
        size = 0;
      else if(spec->noun == V_CHAR || spec->noun == V_BOOL)
        size = 1;
      else if(spec->noun == V_INT && !(spec->b_long))
        size = 2;
      else
        size = 4;

      // Check for returned pointer.
      sym_lnk = sym->type;
      while (sym_lnk && !IS_PTR (sym_lnk))
        sym_lnk = sym_lnk->next;
      if(IS_PTR(sym_lnk))
        size = 2;
    }
  else
    {
      NOTUSEDERROR();
      return TRUE;
    }

  switch(*what)
    {
    case 'a':
      return(size == 1 || size == 2);
    case 'p':
      return(size == 2);
    default:
      return false;
    }
}

/*-----------------------------------------------------------------*/
/* incLabelJmpToCount - increment counter "jmpToCount" in entry    */
/* of the list labelHash                                           */
/*-----------------------------------------------------------------*/
static bool
incLabelJmpToCount (const char *label)
{
  labelHashEntry *entry;

  entry = getLabelRef (label, _G.head);
  if (!entry)
    return FALSE;
  entry->jmpToCount++;
  return TRUE;
}

/*-----------------------------------------------------------------*/
/* findLabel -                                                     */
/* 1. extracts label in the opcode pl                              */
/* 2. increment "label jump-to count" in labelHash                 */
/* 3. search lineNode with label definition and return it          */
/*-----------------------------------------------------------------*/
static lineNode *
findLabel (const lineNode *pl)
{
  char *p;
  lineNode *cpl;

  /* 1. extract label in opcode */

  /* In each jump the label is at the end */
  p = strlen (pl->line) - 1 + pl->line;

  /* Skip trailing whitespace */
  while(isspace(*p))
    p--;

  /* scan backward until space or ',' */
  for (; p > pl->line; p--)
    if (isspace(*p) || *p == ',')
      break;

  /* sanity check */
  if (p == pl->line)
    {
      NOTUSEDERROR();
      return NULL;
    }

  /* skip ',' resp. '\t' */
  ++p;

  /* 2. increment "label jump-to count" */
  if (!incLabelJmpToCount (p))
    return NULL;

  /* 3. search lineNode with label definition and return it */
  for (cpl = _G.head; cpl; cpl = cpl->next)
    {
      if (   cpl->isLabel
          && strncmp (p, cpl->line, strlen(p)) == 0)
        {
          return cpl;
        }
    }
  return NULL;
}

/* Check if reading arg implies reading what. */
static bool argIs(const char *arg, const char *what)
{
  if (arg == NULL || strlen (arg) == 0)
    return false;

  while (isblank ((unsigned char)(arg[0])))
    arg++;

  if (arg[0] == ',')
    arg++;

  while (isblank ((unsigned char)(arg[0])))
    arg++;

  return !strncmp(arg, what, strlen(what)) &&
    (!arg[strlen(what)] || isspace((unsigned char)(arg[strlen(what)])) || arg[strlen(what)] == ',');
}

static bool
stm8MightReadFlag(const lineNode *pl, const char *what)
{
  if (ISINST (pl->line, "push") && argIs (pl->line + 4, "af") || ISINST (pl->line, "pushaf"))
    return true;

  if (ISINST (pl->line, "t0sn") || ISINST (pl->line, "t1sn"))
    return argIs(strchr (pl->line, ','), what);

  if(ISINST (pl->line, "addc") ||
    ISINST (pl->line, "subc"))
    return !strcmp(what, "c");

  return false;
}

static bool
pdkMightRead(const lineNode *pl, const char *what)
{
//printf("pdkMightRead() for |%s|%s|\n", pl->line, what);
  if (!strcmp(what, "z") || !strcmp(what, "c") || !strcmp(what, "ac") || !strcmp(what, "ov"))
    return (stm8MightReadFlag(pl, what));
  else if (strcmp(what, "a") && strcmp(what, "p"))
    return true;

  // Instructions that never read anything.
  if (ISINST(pl->line, "engint") ||
    ISINST(pl->line, "disgint") ||
    ISINST(pl->line, "nop") ||
    ISINST(pl->line, "pop") || ISINST(pl->line, "popaf") ||
    ISINST(pl->line, "stopsys") ||
    ISINST(pl->line, "wdreset"))
    return false;

  if (ISINST(pl->line, "ret") && strchr(pl->line + 2, '#') && !strcmp(what, "a"))
    return false;
  if (ISINST(pl->line, "ret"))
    return isReturned(what);

  if (ISINST(pl->line, "mov"))
    return argIs (strchr (pl->line, ','), what);

  if (ISINST (pl->line, "push") && argIs (pl->line + 4, "af") || ISINST (pl->line, "pushaf"))
    return !strcmp(what, "a");

  // Two-operand instructions that read both operands
  if (ISINST (pl->line, "add") ||
    ISINST (pl->line, "and") ||
    ISINST (pl->line, "or") ||
    ISINST (pl->line, "sub") ||
    ISINST (pl->line, "xor"))
    return argIs (pl->line + 3, what) || argIs (strchr (pl->line, ','), what);
  if (ISINST(pl->line, "idxm"))
    return argIs (pl->line + 4, what) || argIs (strchr (pl->line, ','), what);
  if (ISINST (pl->line, "ceqsn") ||
    ISINST (pl->line, "cneqsn"))
    return argIs (pl->line + 6, what) || argIs (strchr (pl->line, ','), what);

  // One-operand instructions
  if (ISINST (pl->line, "neg") ||
    ISINST (pl->line, "not") ||
    ISINST (pl->line, "sl") ||
    ISINST (pl->line, "slc") ||
    ISINST (pl->line, "sr") ||
    ISINST (pl->line, "src"))
    return argIs (pl->line + 3, what);
  if (ISINST (pl->line, "dzsn") ||
    ISINST (pl->line, "izsn") ||
    ISINST (pl->line, "pcadd") ||
    ISINST (pl->line, "stt16"))
    return argIs (pl->line + 5, what);

  // Todo: addc, subc, xch

  return true;
}

static bool
stm8SurelyWritesFlag(const lineNode *pl, const char *what)
{
  if (ISINST (pl->line, "pop") && argIs (pl->line + 4, "af") || ISINST (pl->line, "popaf"))
    return true;

  // Instructions that write all flags
  if (ISINST (pl->line, "add") ||
    ISINST (pl->line, "addc") ||
    ISINST (pl->line, "ceqsn") ||
    ISINST (pl->line, "cneqsn") ||
    ISINST (pl->line, "dec") ||
    ISINST (pl->line, "dzsn") ||
    ISINST (pl->line, "inc") ||
    ISINST (pl->line, "izsn") ||
    ISINST (pl->line, "sub") ||
    ISINST (pl->line, "subc"))
      return true;

  // Instructions that write c only
  if (ISINST (pl->line, "sl") ||
    ISINST (pl->line, "slc") ||
    ISINST (pl->line, "sr") ||
    ISINST (pl->line, "src"))
      return !strcmp(what, "c");

  // Instructions that write z only
  if (ISINST (pl->line, "and") ||
    ISINST (pl->line, "neg") ||
    ISINST (pl->line, "not") ||
    ISINST (pl->line, "or") ||
    ISINST (pl->line, "xor"))
      return !strcmp(what, "z");

  // mov writes z when the destination is a and hte source not an immediate only.
  if (ISINST (pl->line, "mov") && !strcmp(what, "z") && pl->line[4] == 'a' && pl->line[5] == ',' && !strchr(pl->line, '#'))
    return true;

  return false;
}

static bool
pdkSurelyWrites(const lineNode *pl, const char *what)
{
  if (!strcmp(what, "z") || !strcmp(what, "c") || !strcmp(what, "ac") || !strcmp(what, "ov"))
    return (stm8SurelyWritesFlag(pl, what));

  if (ISINST(pl->line, "mov") || ISINST(pl->line, "idxm"))
    return argIs (pl->line + 4, what);

  if (ISINST (pl->line, "pop") && argIs (pl->line + 4, "af") || ISINST (pl->line, "popaf"))
    return !strcmp(what, "a");

  // TODO: Other instructions

  // One-operand instructions that write their argument
  if (ISINST (pl->line, "neg") ||
    ISINST (pl->line, "not") ||
    ISINST (pl->line, "sl") ||
    ISINST (pl->line, "slc") ||
    ISINST (pl->line, "sr") ||
    ISINST (pl->line, "src"))
    return argIs (pl->line + 3, what);
  if (ISINST (pl->line, "dzsn") ||
    ISINST (pl->line, "izsn") ||
    ISINST (pl->line, "ldt16"))
    return argIs (pl->line + 5, what);

  return false;
}


static bool
pdkUncondJump(const lineNode *pl)
{
  return (ISINST(pl->line, "goto") || ISINST(pl->line, "pcadd"));
}

static bool
pdkCondJump(const lineNode *pl)
{
  return (ISINST(pl->line, "ceqsn") || ISINST(pl->line, "cneqsn") ||
    ISINST(pl->line, "t0sn") || ISINST(pl->line, "t1sn") ||
    ISINST(pl->line, "izsn") || ISINST(pl->line, "dzsn"));
}

static bool
pdkSurelyReturns(const lineNode *pl)
{
  return(ISINST(pl->line, "ret"));
}

/*-----------------------------------------------------------------*/
/* scan4op - "executes" and examines the assembler opcodes,        */
/* follows conditional and un-conditional jumps.                   */
/* Moreover it registers all passed labels.                        */
/*                                                                 */
/* Parameter:                                                      */
/*    lineNode **pl                                                */
/*       scanning starts from pl;                                  */
/*       pl also returns the last scanned line                     */
/*    const char *pReg                                             */
/*       points to a register (e.g. "ar0"). scan4op() tests for    */
/*       read or write operations with this register               */
/*    const char *untilOp                                          */
/*       points to NULL or a opcode (e.g. "push").                 */
/*       scan4op() returns if it hits this opcode.                 */
/*    lineNode **plCond                                            */
/*       If a conditional branch is met plCond points to the       */
/*       lineNode of the conditional branch                        */
/*                                                                 */
/* Returns:                                                        */
/*    S4O_ABORT                                                    */
/*       on error                                                  */
/*    S4O_VISITED                                                  */
/*       hit lineNode with "visited" flag set: scan4op() already   */
/*       scanned this opcode.                                      */
/*    S4O_FOUNDOPCODE                                              */
/*       found opcode and operand, to which untilOp and pReg are   */
/*       pointing to.                                              */
/*    S4O_RD_OP, S4O_WR_OP                                         */
/*       hit an opcode reading or writing from pReg                */
/*    S4O_CONDJMP                                                  */
/*       hit a conditional jump opcode. pl and plCond return the   */
/*       two possible branches.                                    */
/*    S4O_TERM                                                     */
/*       acall, lcall, ret and reti "terminate" a scan.            */
/*-----------------------------------------------------------------*/
static S4O_RET
scan4op (lineNode **pl, const char *what, const char *untilOp,
         lineNode **plCond)
{
  for (; *pl; *pl = (*pl)->next)
    {
      if (!(*pl)->line || (*pl)->isDebug || (*pl)->isComment || (*pl)->isLabel)
        continue;
      D(("Scanning %s for %s\n", (*pl)->line, what));
      /* don't optimize across inline assembler,
         e.g. isLabel doesn't work there */
      if ((*pl)->isInline)
        {
          D(("S4O_ABORT at inline asm\n"));
          return S4O_ABORT;
        }

      if ((*pl)->visited)
        {
          D(("S4O_VISITED\n"));
          return S4O_VISITED;
        }

      (*pl)->visited = TRUE;

      if(pdkMightRead(*pl, what))
        {
          D(("S4O_RD_OP\n"));
          return S4O_RD_OP;
        }

      // Check writes before conditional jumps, some jumps (btjf, btjt) write 'c'
      if(pdkSurelyWrites(*pl, what))
        {
          D(("S4O_WR_OP\n"));
          return S4O_WR_OP;
        }

      if(pdkUncondJump(*pl))
        {
          *pl = findLabel (*pl);
            if (!*pl)
              {
                D(("S4O_ABORT at unconditional jump\n"));
                return S4O_ABORT;
              }
        }
      if(pdkCondJump(*pl))
        {
          *plCond = (*pl)->next->next;
          if (!*plCond)
            {
              D(("S4O_ABORT at conditional jump\n"));
              return S4O_ABORT;
            }
          D(("S4O_CONDJMP\n"));
          return S4O_CONDJMP;
        }

      /* Don't need to check for de, hl since stm8MightRead() does that */
      if(pdkSurelyReturns(*pl))
        {
          D(("S4O_TERM\n"));
          return S4O_TERM;
        }
    }
  D(("S4O_ABORT\n"));
  return S4O_ABORT;
}

/*-----------------------------------------------------------------*/
/* doTermScan - scan through area 2. This small wrapper handles:   */
/* - action required on different return values                    */
/* - recursion in case of conditional branches                     */
/*-----------------------------------------------------------------*/
static bool
doTermScan (lineNode **pl, const char *what)
{
  lineNode *plConditional;
  for (;; *pl = (*pl)->next)
    {
      switch (scan4op (pl, what, NULL, &plConditional))
        {
          case S4O_TERM:
          case S4O_VISITED:
          case S4O_WR_OP:
            /* all these are terminating conditions */
            return true;
          case S4O_CONDJMP:
            /* two possible destinations: recurse */
              {
                lineNode *pl2 = plConditional;
                D(("CONDJMP trying other branch first\n"));
                if (!doTermScan (&pl2, what))
                  return false;
                D(("Other branch OK.\n"));
              }
            continue;
          case S4O_RD_OP:
          default:
            /* no go */
            return false;
        }
    }
}

/*-----------------------------------------------------------------*/
/* univisitLines - clear "visited" flag in all lines               */
/*-----------------------------------------------------------------*/
static void
unvisitLines (lineNode *pl)
{
  for (; pl; pl = pl->next)
    pl->visited = false;
}

bool pdknotUsed(const char *what, lineNode *endPl, lineNode *head)
{
  lineNode *pl;

  _G.head = head;

  unvisitLines (_G.head);

  pl = endPl->next;
  return (doTermScan (&pl, what));
}

bool pdknotUsedFrom(const char *what, const char *label, lineNode *head)
{
  lineNode *cpl;

  for (cpl = head; cpl; cpl = cpl->next)
    if (cpl->isLabel && !strncmp (label, cpl->line, strlen(label)))
      return (pdknotUsed (what, cpl, head));

  return false;
}

int
pdkinstructionSize(lineNode *pl)
{
  return 1;
}