OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_42/] [or1ksim/] [cuc/] [cuc.c] - Blame information for rev 933

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 879 markom
/* cuc.c -- OpenRISC Custom Unit Compiler
2
 *    Copyright (C) 2002 Marko Mlinar, markom@opencores.org
3
 *
4
 *    This file is part of OpenRISC 1000 Architectural Simulator.
5
 *
6
 *    This program is free software; you can redistribute it and/or modify
7
 *    it under the terms of the GNU General Public License as published by
8
 *    the Free Software Foundation; either version 2 of the License, or
9
 *    (at your option) any later version.
10
 *
11
 *    This program is distributed in the hope that it will be useful,
12
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *    GNU General Public License for more details.
15
 *
16
 *    You should have received a copy of the GNU General Public License
17
 *    along with this program; if not, write to the Free Software
18
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
/* Main file, including code optimization and command prompt */
21
 
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <stdarg.h>
25
#include <assert.h>
26 915 markom
#include <ctype.h>
27 879 markom
#include "sim-config.h"
28
#include "cuc.h"
29
#include "insn.h"
30
#include "profiler.h"
31 883 markom
#include "opcode/or32.h"
32 897 markom
#include "parse.h"
33 879 markom
 
34
FILE *flog;
35 883 markom
int cuc_debug = 0;
36 879 markom
 
37
/* Last used registers by software convention */
38
const int call_saved[MAX_REGS] = {
39
  0, 0, 0, 1, 1, 1, 1, 1,
40
  1, 1, 0, 1, 0, 1, 0, 1,
41
  0, 1, 0, 1, 0, 1, 0, 1,
42
  0, 1, 0, 1, 0, 1, 0, 1,
43
  1, 1};
44
 
45 933 markom
/* returns log2(x) */
46
int log2 (unsigned long x)
47
{
48
  int c = 0;
49
  assert (x >= 0);
50
  if (!x) return 0; /* not by the book, but practical */
51
  while (x != 1) x >>= 1, c++;
52
  return c;
53
}
54
 
55 931 markom
/* Does all known instruction optimizations */
56
void cuc_optimize (cuc_func *func)
57
{
58
  int modified = 0;
59
  log ("Optimizing.\n");
60 932 markom
  insert_conditional_facts (func);
61 933 markom
  if (cuc_debug >= 3) print_cuc_bb (func, "AFTER_COND_FACT");
62 931 markom
  do {
63
    modified = 0;
64
    if (optimize_cmovs (func)) {
65
      if (cuc_debug >= 6) print_cuc_bb (func, "AFTER_OPT_CMOVS");
66
      modified = 1;
67
    }
68
    if (optimize_tree (func)) {
69
      if (cuc_debug >= 6) print_cuc_bb (func, "AFTER_OPT_TREE1");
70
      modified = 1;
71
    }
72
    if (remove_nops (func)) {
73
      if (cuc_debug >= 6) print_cuc_bb (func, "NO_NOPS");
74
      modified = 1;
75
    }
76
    if (remove_dead (func)) {
77
      if (cuc_debug >= 5) print_cuc_bb (func, "AFTER_DEAD");
78
      modified = 1;
79
    }
80
    if (cse (func)) {
81
      log ("Common subexpression elimination.\n");
82
      if (cuc_debug >= 3) print_cuc_bb (func, "AFTER_CSE");
83
      modified = 1;
84
    }
85
    if (optimize_bb (func)) {
86
      if (cuc_debug >= 5) print_cuc_bb (func, "AFTER_OPT_BB");
87
      modified = 1;
88
    }
89
    if (remove_nops (func)) {
90
      if (cuc_debug >= 6) print_cuc_bb (func, "NO_NOPS");
91
      modified = 1;
92
    }
93
    if (remove_dead_bb (func)) {
94
      if (cuc_debug >= 5) print_cuc_bb (func, "AFTER_DEAD_BB");
95
      modified = 1;
96
    }
97
    if (remove_trivial_regs (func)) {
98
      if (cuc_debug >= 2) print_cuc_bb (func, "AFTER_TRIVIAL");
99
      modified = 1;
100
    }
101
    if (remove_nops (func)) {
102
      if (cuc_debug >= 6) print_cuc_bb (func, "NO_NOPS");
103
      modified = 1;
104
    }
105
  } while (modified);
106
  set_io (func);
107
}
108
 
109
/* Pre/unrolls basic block and optimizes it */
110 879 markom
cuc_timings *preunroll_bb (char *bb_filename, cuc_func *f, cuc_timings *timings, int b, int i, int j)
111
{
112
  cuc_func *func;
113 883 markom
  cucdebug (2, "BB%i unroll %i times preroll %i times\n", b, j, i);
114 879 markom
  func = preunroll_loop (f, b, i, j, bb_filename);
115 883 markom
  if (cuc_debug >= 2) print_cuc_bb (func, "AFTER_PREUNROLL");
116 931 markom
  cuc_optimize (func);
117 879 markom
 
118 883 markom
  add_latches (func);
119
  if (cuc_debug >= 1) print_cuc_bb (func, "AFTER_LATCHES");
120 897 markom
  add_memory_dep (func, func->memory_order);
121 883 markom
  if (cuc_debug >= 7) print_cuc_bb (func, "AFTER_MEMORY_DEP");
122 879 markom
  add_data_dep (func);
123 883 markom
  if (cuc_debug >= 8) print_cuc_bb (func, "AFTER_DATA_DEP");
124 897 markom
  schedule_memory (func, func->memory_order);
125 883 markom
  if (cuc_debug >= 7) print_cuc_bb (func, "AFTER_SCHEDULE_MEM");
126 879 markom
 
127
  analyse_timings (func, timings);
128 883 markom
  cucdebug (2, "new_time = %i, old_time = %i, size = %f\n",
129 879 markom
           timings->new_time, func->orig_time, timings->size);
130
  log ("new time = %icyc, old_time = %icyc, size = %.0f gates\n",
131
         timings->new_time, func->orig_time, timings->size);
132
  //output_verilog (func, argv[1]);
133
  free_func (func);
134
  timings->b = b;
135
  timings->unroll = j;
136
  timings->preroll = i;
137 883 markom
  timings->nshared = 0;
138 879 markom
  return timings;
139
}
140
 
141 931 markom
/* Simple comparison function */
142 879 markom
int tim_comp (cuc_timings *a, cuc_timings *b)
143
{
144
  if (a->new_time < b->new_time) return -1;
145
  else if (a->new_time > b->new_time) return 1;
146
  else return 0;
147
}
148
 
149 931 markom
/* Analyses function; done when cuc command is entered in (sim) prompt */
150 879 markom
cuc_func *analyse_function (char *module_name, long orig_time,
151 897 markom
                unsigned long start_addr, unsigned long end_addr,
152
                int memory_order)
153 879 markom
{
154
  cuc_timings timings;
155
  cuc_func *func = (cuc_func *) malloc (sizeof (cuc_func));
156
  cuc_func *saved;
157
  int b, i, j;
158
  char tmp1[256];
159
  char tmp2[256];
160
 
161
  func->orig_time = orig_time;
162
  func->start_addr = start_addr;
163
  func->end_addr = end_addr;
164 897 markom
  func->memory_order = memory_order;
165 906 markom
  func->nfdeps = 0;
166
  func->fdeps = NULL;
167 879 markom
 
168
  sprintf (tmp1, "%s.bin", module_name);
169 883 markom
  cucdebug (2, "Loading %s.bin\n", module_name);
170 897 markom
  if (cuc_load (tmp1)) {
171
    free (func);
172
    return NULL;
173
  }
174 879 markom
 
175
  log ("Detecting basic blocks\n");
176
  detect_bb (func);
177 883 markom
  if (cuc_debug >= 2) print_cuc_insns ("WITH_BB_LIMITS", 0);
178 879 markom
 
179
  //sprintf (tmp1, "%s.bin.mp", module_name);
180
  sprintf (tmp2, "%s.bin.bb", module_name);
181
  generate_bb_seq (func, config.sim.mprof_fn, tmp2);
182 897 markom
  log ("Assuming %i clk cycle load (%i cyc burst)\n", runtime.cuc.mdelay[0], runtime.cuc.mdelay[2]);
183
  log ("Assuming %i clk cycle store (%i cyc burst)\n", runtime.cuc.mdelay[1], runtime.cuc.mdelay[3]);
184 879 markom
 
185
  build_bb (func);
186 883 markom
  if (cuc_debug >= 5) print_cuc_bb (func, "AFTER_BUILD_BB");
187 879 markom
  reg_dep (func);
188
 
189
  log ("Detecting dependencies\n");
190 883 markom
  if (cuc_debug >= 2) print_cuc_bb (func, "AFTER_REG_DEP");
191 931 markom
  cuc_optimize (func);
192 879 markom
 
193 897 markom
#if 0
194 883 markom
  csm (func);
195 897 markom
#endif
196 879 markom
  assert (saved = dup_func (func));
197 883 markom
 
198
  timings.preroll = timings.unroll = 1;
199
  timings.nshared = 0;
200 931 markom
 
201 883 markom
  add_latches (func);
202
  if (cuc_debug >= 1) print_cuc_bb (func, "AFTER_LATCHES");
203
  analyse_timings (func, &timings);
204 897 markom
  add_memory_dep (func, func->memory_order);
205 883 markom
  if (cuc_debug >= 7) print_cuc_bb (func, "AFTER_MEMORY_DEP");
206 879 markom
  add_data_dep (func);
207 883 markom
  if (cuc_debug >= 8) print_cuc_bb (func, "AFTER_DATA_DEP");
208 879 markom
  schedule_memory (func, memory_order);
209 883 markom
  if (cuc_debug >= 7) print_cuc_bb (func, "AFTER_SCHEDULE_MEM");
210 879 markom
 
211 883 markom
  //output_verilog (func, module_name);
212 879 markom
  free_func (func);
213 883 markom
  log ("Base option: pre%i,un%i,sha%i: %icyc %.1f\n",
214
        timings.preroll, timings.unroll, timings.nshared, timings.new_time, timings.size);
215
  saved->timings = timings;
216 879 markom
 
217
#if 1
218
  /* detect and unroll simple loops */
219
  for (b = 0; b < saved->num_bb; b++) {
220
    cuc_timings t[MAX_UNROLL * MAX_PREROLL];
221
    cuc_timings *ut;
222
    cuc_timings *cut = &t[0];
223
    int nt = 1;
224
    double csize;
225 897 markom
    saved->bb[b].selected_tim = -1;
226 879 markom
 
227
    /* Is it a loop? */
228
    if (saved->bb[b].next[0] != b && saved->bb[b].next[1] != b) continue;
229
    t[0] = timings;
230
    t[0].b = b;
231
    t[0].preroll = 1;
232
    t[0].unroll = 1;
233 883 markom
    t[0].nshared = 0;
234 879 markom
 
235
    sprintf (tmp1, "%s.bin.bb", module_name);
236
    i = 1;
237
    do {
238
      cuc_timings *pt;
239
      cuc_timings *cpt = cut;
240
      j = 1;
241
 
242
      do {
243
        pt = cpt;
244
        cpt = preunroll_bb (tmp1, saved, &t[nt++], b, ++j, i);
245 915 markom
      } while (j <= MAX_PREROLL && pt->new_time > cpt->new_time);
246 879 markom
      i++;
247
      ut = cut;
248
      cut = preunroll_bb (tmp1, saved, &t[nt++], b, 1, i);
249 915 markom
    } while (i <= MAX_UNROLL && ut->new_time > cut->new_time);
250 879 markom
 
251
    /* Sort the timings */
252 883 markom
#if 0
253
    if (cuc_debug >= 3)
254 879 markom
    for (i = 0; i < nt; i++) printf ("%i:%i,%i: %icyc\n",
255
                    t[i].b, t[i].preroll, t[i].unroll, t[i].new_time);
256 883 markom
#endif
257 879 markom
 
258
    qsort (t, nt, sizeof (cuc_timings), (int (*)(const void *, const void *))tim_comp);
259
 
260
    /* Delete timings, that have worst time and bigger size than other */
261
    j = 1;
262
    csize = t[0].size;
263
    for (i = 1; i < nt; i++)
264
      if (t[i].size < csize) t[j++] = t[i];
265
    nt = j;
266 883 markom
 
267
    cucdebug (1, "Available options\n");
268
    for (i = 0; i < nt; i++) cucdebug (1, "%i:%i,%i: %icyc %.1f\n",
269
        t[i].b, t[i].preroll, t[i].unroll, t[i].new_time, t[i].size);
270
    /* Add results from CSM */
271
    j = nt;
272
    for (i = 0; i < saved->bb[b].ntim; i++) {
273
      int i1;
274
      for (i1 = 0; i1 < nt; i1++) {
275
        t[j] = t[i1];
276
        t[j].size += saved->bb[b].tim[i].size - timings.size;
277
        t[j].new_time += saved->bb[b].tim[i].new_time - timings.new_time;
278
        t[j].nshared = saved->bb[b].tim[i].nshared;
279
        t[j].shared = saved->bb[b].tim[i].shared;
280
        if (++j >= MAX_UNROLL * MAX_PREROLL) goto full;
281
      }
282
    }
283
 
284
full:
285
    nt = j;
286 879 markom
 
287 883 markom
    cucdebug (1, "Available options:\n");
288
    for (i = 0; i < nt; i++) cucdebug (1, "%i:%i,%i: %icyc %.1f\n",
289
        t[i].b, t[i].preroll, t[i].unroll, t[i].new_time, t[i].size);
290 879 markom
 
291 883 markom
    /* Sort again with new timings added */
292
    qsort (t, nt, sizeof (cuc_timings), (int (*)(const void *, const void *))tim_comp);
293
 
294
    /* Delete timings, that have worst time and bigger size than other */
295
    j = 1;
296
    csize = t[0].size;
297
    for (i = 1; i < nt; i++)
298
      if (t[i].size < csize) t[j++] = t[i];
299
    nt = j;
300
 
301
    cucdebug (1, "Available options:\n");
302
    for (i = 0; i < nt; i++) cucdebug (1, "%i:%i,%i: %icyc %.1f\n",
303
                               t[i].b, t[i].preroll, t[i].unroll, t[i].new_time, t[i].size);
304
 
305
    if (saved->bb[b].ntim) free (saved->bb[b].tim);
306 879 markom
    saved->bb[b].ntim = nt;
307
    assert (saved->bb[b].tim = (cuc_timings *) malloc (sizeof (cuc_timings) * nt));
308
 
309
    /* Copy options in reverse order -- smallest first */
310
    for (i = 0; i < nt; i++) saved->bb[b].tim[i] = t[nt - 1 - i];
311 883 markom
 
312
    log ("Available options:\n");
313
    for (i = 0; i < saved->bb[b].ntim; i++) {
314
      log ("%i:pre%i,un%i,sha%i: %icyc %.1f\n",
315
        saved->bb[b].tim[i].b, saved->bb[b].tim[i].preroll, saved->bb[b].tim[i].unroll,
316
        saved->bb[b].tim[i].nshared, saved->bb[b].tim[i].new_time, saved->bb[b].tim[i].size);
317
    }
318 879 markom
  }
319
#endif
320
  return saved;
321
}
322
 
323 897 markom
/* Utility option formatting functions */
324
static const char *option_char = "?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
325
 
326
/*static */char *gen_option (char *s, int bb_no, int f_opt)
327 883 markom
{
328 897 markom
  if (bb_no >= 0) sprintf (s, "%i", bb_no);
329
  assert (f_opt <= strlen (option_char));
330
  sprintf (s, "%s%c", s, option_char[f_opt]);
331
  return s;
332
}
333
 
334
/*static */void print_option (int bb_no, int f_opt)
335
{
336
  char tmp1[10];
337
  char tmp2[10];
338
  sprintf (tmp2, "%s", gen_option (tmp1, bb_no, f_opt));
339
  printf ("%3s", tmp2);
340
}
341
 
342
static char *format_func_options (char *s, cuc_func *f)
343
{
344
  int b, first = 1;
345
  *s = '\0';
346
  for (b = 0; b < f->num_bb; b++)
347
    if (f->bb[b].selected_tim >= 0) {
348
      char tmp[10];
349
      sprintf (s, "%s%s%s", s, first ? "" : ",", gen_option (tmp, b, f->bb[b].selected_tim));
350
      first = 0;
351
    }
352
  return s;
353
}
354
 
355
static void options_cmd (int func_no, cuc_func *f)
356
{
357 883 markom
  int b, i;
358 903 markom
  char tmp[30];
359 897 markom
  char *name = prof_func[func_no].name;
360 904 markom
  printf ("-----------------------------------------------------------------------------\n");
361
  printf ("|%-28s|pre/unrolled|shared|  time  |  gates |old_time|\n",
362
            strstrip (tmp, name, 28));
363
  printf ("|                    BASE    |%4i / %4i | %4i |%8i|%8.f|%8i|\n", 1, 1, 0,
364 903 markom
          f->timings.new_time, f->timings.size, f->orig_time);
365 883 markom
  for (b = 0; b < f->num_bb; b++) {
366
    /* Print out results */
367 897 markom
    for (i = 1; i < f->bb[b].ntim; i++) { /* First one is base option */
368
      int time = f->bb[b].tim[i].new_time - f->timings.new_time;
369
      double size = f->bb[b].tim[i].size - f->timings.size;
370 904 markom
      printf ("|                   ");
371 897 markom
      print_option (b, i);
372 903 markom
      printf ("      |%4i / %4i | %4i |%+8i|%+8.f|        |\n",
373 897 markom
        f->bb[b].tim[i].preroll, f->bb[b].tim[i].unroll, f->bb[b].tim[i].nshared,
374
        time, size);
375 883 markom
    }
376
  }
377
}
378
 
379 897 markom
/* Generates a function, based on specified parameters */
380 915 markom
cuc_func *generate_function (cuc_func *rf, char *name, char *cut_filename)
381 897 markom
{
382
  int b, i, j;
383
  char tmp[256];
384
  cuc_timings tt;
385
  cuc_func *f;
386
  assert (f = dup_func (rf));
387
 
388 915 markom
  if (cuc_debug >= 2) print_cuc_bb (f, "BEFORE_GENERATE");
389 897 markom
  log ("Generating function %s.\n", name);
390
  printf ("Generating function %s.\n", name);
391
 
392
  format_func_options (tmp, rf);
393
  if (strlen (tmp)) printf ("Applying options: %s\n", tmp);
394 902 markom
  else printf ("Using basic options.\n");
395 897 markom
 
396
  /* Generate function as specified by options */
397
  for (b = 0; b < f->num_bb; b++) {
398
    cuc_timings *st;
399
    if (rf->bb[b].selected_tim < 0) continue;
400
    st = &rf->bb[b].tim[rf->bb[b].selected_tim];
401
    sprintf (tmp, "%s.bin.bb", name);
402
    preunroll_bb (&tmp[0], f, &tt, b, st->preroll, st->unroll);
403
    if (cuc_debug >= 1) print_cuc_bb (f, "AFTER_PREUNROLL");
404
  }
405
  for (b = 0; b < f->num_bb; b++) {
406
    cuc_timings *st;
407
    if (rf->bb[b].selected_tim < 0) continue;
408
    st = &rf->bb[b].tim[rf->bb[b].selected_tim];
409
    if (!st->nshared) continue;
410
    assert (0);
411
    //csm_gen (f, rf, st->nshared, st->shared);
412
  }
413 915 markom
  add_latches (f);
414
  if (cuc_debug >= 1) print_cuc_bb (f, "AFTER_LATCHES");
415 897 markom
  analyse_timings (f, &tt);
416
  add_memory_dep (f, f->memory_order);
417
  if (cuc_debug >= 7) print_cuc_bb (f, "AFTER_MEMORY_DEP");
418
  add_data_dep (f);
419
  if (cuc_debug >= 8) print_cuc_bb (f, "AFTER_DATA_DEP");
420
  schedule_memory (f, f->memory_order);
421
  if (cuc_debug >= 7) print_cuc_bb (f, "AFTER_SCHEDULE_MEM");
422 915 markom
 
423
  sprintf (tmp, "%s%s", cut_filename, name);
424
  output_verilog (f, tmp);
425 897 markom
  return f;
426
}
427
 
428
/* Calculates required time, based on selected options */
429
int calc_cycles (cuc_func *f)
430
{
431
  int b, i, ntime = f->timings.new_time;
432
  for (b = 0; b < f->num_bb; b++)
433
    if (f->bb[b].selected_tim >= 0) {
434
      assert (f->bb[b].selected_tim < f->bb[b].ntim);
435
      ntime += f->bb[b].tim[f->bb[b].selected_tim].new_time - f->timings.new_time;
436
    }
437
  return ntime;
438
}
439
 
440
/* Calculates required size, based on selected options */
441
double calc_size (cuc_func *f)
442
{
443
  int b, i;
444
  double size = f->timings.size;
445
  for (b = 0; b < f->num_bb; b++)
446
    if (f->bb[b].selected_tim >= 0) {
447
      assert (f->bb[b].selected_tim < f->bb[b].ntim);
448
      size += f->bb[b].tim[f->bb[b].selected_tim].size - f->timings.size;
449
    }
450
  return size;
451
}
452
 
453 879 markom
/* Dumps specified function to file (hex) */
454
unsigned long extract_function (char *out_fn, unsigned long start_addr)
455
{
456
  FILE *fo;
457
  unsigned long a = start_addr;
458
  int x = 0;
459
  assert (fo = fopen (out_fn, "wt+"));
460
 
461
  do {
462
    unsigned long d = evalsim_mem32 (a);
463
    int index = insn_decode (d);
464
    assert (index >= 0);
465
    if (x) x++;
466
    if (strcmp (insn_name (index), "l.jr") == 0) x = 1;
467
    a += 4;
468
    fprintf (fo, "%08x\n", d);
469
  } while (x < 2);
470
 
471
  fclose (fo);
472
  return a - 4;
473
}
474
 
475
static cuc_func *func[MAX_FUNCS];
476 897 markom
static int func_v[MAX_FUNCS];
477 879 markom
 
478 906 markom
/* Detects function dependencies and removes  */
479
static void set_func_deps ()
480
{
481
  int f, b, i, j;
482
restart:
483
  for (f = 0; f < prof_nfuncs - 1; f++) if (func[f]) {
484
    int fused[MAX_FUNCS] = {0};
485
    int c;
486
    for (b = 0; b < func[f]->num_bb; b++)
487
      for (i = 0; i < func[f]->bb[b].ninsn; i++) {
488
        cuc_insn *ii = &func[f]->bb[b].insn[i];
489
        if (ii->index == II_CALL) {
490
          assert (ii->opt[0] == OPT_CONST);
491
          for (j = 0; j < prof_nfuncs - 1; j++)
492
            if (func[j] && func[j]->start_addr == ii->op[0]) break;
493
          if (j >= prof_nfuncs - 1) {
494
            log ("%s is calling unknown function, address %08x\n",
495
                            prof_func[f].name, ii->op[0]);
496
            debug (1, "%s is calling unknown function, address %08x\n",
497
                            prof_func[f].name, ii->op[0]);
498
            free_func (func[f]);
499
            func[f] = NULL;
500
            goto restart;
501
          } else if (f == j) {
502
            log ("%s is recursive, ignoring\n", prof_func[f].name);
503
            debug (1, "%s is recursive, ignoring\n", prof_func[f].name);
504
            free_func (func[f]);
505
            func[f] = NULL;
506
            goto restart;
507
          } else fused[j]++;
508
        }
509
      }
510
    for (i = 0; i < MAX_FUNCS; i++) if (fused[i]) c++;
511
    if (func[f]->nfdeps) free (func[f]->fdeps);
512
    func[f]->nfdeps = c;
513
    func[f]->fdeps = (cuc_func **) malloc (sizeof (cuc_func *) * c);
514
    for (i = 0, j = 0; i < MAX_FUNCS; i++)
515
      if (fused[i]) func[f]->fdeps[j++] = func[i];
516
  }
517
 
518
  /* Detect loops */
519
  {
520
    int change;
521
    for (f = 0; f < MAX_FUNCS; f++) if (func[f]) func[f]->tmp = 0;
522
    do {
523
      change = 0;
524
      for (f = 0; f < MAX_FUNCS; f++) if (func[f] && !func[f]->tmp) {
525
        int o = 1;
526
        for (i = 0; i < func[f]->nfdeps; i++)
527
          if (!func[f]->fdeps[i]->tmp) {o = 0; break;}
528
        if (o) {
529
          func[f]->tmp = 1;
530
          change = 1;
531
        }
532
      }
533
    } while (change);
534
 
535
    change = 0;
536
    for (f = 0; f < MAX_FUNCS; f++) if (func[f] && !func[f]->tmp) {
537
      free_func (func[f]);
538
      func[f] = NULL;
539
      change = 1;
540
    }
541
    if (change) goto restart;
542
  }
543
}
544
 
545 879 markom
void main_cuc (char *filename)
546
{
547 883 markom
  int i, j;
548 879 markom
  char tmp1[256];
549 915 markom
  char filename_cut[256];
550
  for (i = 0; i < 256; i++) {
551
    if (isalpha(filename[i])) filename_cut[i] = filename[i];
552
    else {
553
      filename_cut[i] = '\0';
554
      break;
555
    }
556
  }
557 879 markom
 
558 883 markom
  printf ("Entering OpenRISC Custom Unit Compiler command prompt\n");
559
  printf ("Using profile file \"%s\" and memory profile file \"%s\".\n", config.sim.prof_fn, config.sim.mprof_fn);
560 915 markom
  sprintf (tmp1, "%s.log", filename_cut);
561 883 markom
  printf ("Analyzing. (log file \"%s\").\n", tmp1);
562 879 markom
  assert (flog = fopen (tmp1, "wt+"));
563
 
564
  /* Loads in the specified timings table */
565 897 markom
  printf ("Using timings from \"%s\" at %s\n",config.cuc.timings_fn,
566
                 generate_time_pretty (tmp1, config.sim.clkcycle_ps));
567
  load_timing_table (config.cuc.timings_fn);
568
  runtime.cuc.cycle_duration = 1000. * config.sim.clkcycle_ps;
569
  printf ("Multicycle logic %s, bursts %s, %s memory order.\n",
570
    config.cuc.no_multicycle ? "OFF" : "ON", config.cuc.enable_bursts ? "ON" : "OFF",
571
    config.cuc.memory_order == MO_NONE ? "no" : config.cuc.memory_order == MO_WEAK ? "weak" :
572
    config.cuc.memory_order == MO_STRONG ? "strong" : "exact");
573 879 markom
 
574
  prof_set (1, 0);
575
  assert (prof_acquire (config.sim.prof_fn) == 0);
576 924 markom
  cuc_debug = 9;
577 897 markom
 
578
  if (config.cuc.calling_convention)
579
    printf ("Assuming OpenRISC standard calling convention.\n");
580 879 markom
 
581
  /* Try all functions except "total" */
582
  for (i = 0; i < prof_nfuncs - 1; i++) {
583
    long orig_time;
584
    unsigned long start_addr, end_addr;
585
    orig_time = prof_func[i].cum_cycles;
586
    start_addr = prof_func[i].addr;
587
 
588
    /* Extract the function from the binary */
589
    sprintf (tmp1, "%s.bin", prof_func[i].name);
590
    end_addr = extract_function (tmp1, start_addr);
591
 
592
    log ("Testing function %s (%08x - %08x)\n", prof_func[i].name, start_addr, end_addr);
593 897 markom
    printf ("Testing function %s (%08x - %08x)\n", prof_func[i].name, start_addr, end_addr);
594
    func[i] = analyse_function (prof_func[i].name, orig_time, start_addr,
595
                   end_addr, config.cuc.memory_order);
596
    func_v[i] = 0;
597 879 markom
  }
598 906 markom
  set_func_deps ();
599
 
600 883 markom
  while (1) {
601
    char *s;
602 906 markom
wait_command:
603 883 markom
    printf ("(cuc) ");
604
    fflush (stdout);
605
    fgets(tmp1, sizeof tmp1, stdin);
606
    for (s = tmp1; *s != '\0' && *s != '\n' && *s != '\r'; s++);
607
    *s = '\0';
608
 
609 906 markom
      /* quit command */
610 883 markom
    if (strcmp (tmp1, "q") == 0 || strcmp (tmp1, "quit") == 0) {
611
      break;
612 906 markom
 
613
      /* profile command */
614 883 markom
    } else if (strcmp (tmp1, "p") == 0 || strcmp (tmp1, "profile") == 0) {
615 897 markom
      int ntime = 0;
616
      int size = 0;
617
      printf ("-----------------------------------------------------------------------------\n");
618
      printf ("|function name       |calls|avg cycles  |old%| max. f.  | impr. f.| options |\n");
619
      printf ("|--------------------+-----+------------+----+----------|---------+---------|\n");
620 883 markom
      for (j = 0; j < prof_nfuncs; j++) {
621
        int bestcyc = 0, besti = 0;
622 897 markom
        char tmp[100];
623 883 markom
        for (i = 0; i < prof_nfuncs; i++)
624
          if (prof_func[i].cum_cycles > bestcyc) {
625
            bestcyc = prof_func[i].cum_cycles;
626
            besti = i;
627
          }
628
        i = besti;
629 897 markom
        printf ("|%-20s|%5i|%12.1f|%3.0f%%| ",
630
                strstrip (tmp, prof_func[i].name, 20),  prof_func[i].calls,
631 883 markom
                ((double)prof_func[i].cum_cycles / prof_func[i].calls),
632
                (100. * prof_func[i].cum_cycles / prof_cycles));
633
        if (func[i]) {
634 897 markom
          double f = 1.0;
635
          if (func_v[i]) {
636
            int nt = calc_cycles (func[i]);
637
            int s = calc_size (func[i]);
638
            f = func[i]->orig_time / nt;
639
            ntime += nt * func[i]->num_runs;
640
            size += s;
641
          } else ntime += prof_func[i].cum_cycles;
642 905 markom
          printf ("%8.1f |%8.1f | %-8s|\n", 1.f * prof_func[i].cum_cycles
643
                          / func[i]->timings.new_time, f, format_func_options (tmp, func[i]));
644 897 markom
        } else {
645
          printf ("     N/A |     N/A |         |\n");
646
          ntime += prof_func[i].cum_cycles;
647
        }
648
        prof_func[i].cum_cycles = -prof_func[i].cum_cycles;
649 883 markom
      }
650 897 markom
      for (i = 0; i < prof_nfuncs; i++)
651
        prof_func[i].cum_cycles = -prof_func[i].cum_cycles;
652
      printf ("-----------------------------------------------------------------------------\n");
653 905 markom
      printf ("Total %i cycles (was %i), total added gates = %i. Speed factor %.1f\n",
654
                      ntime, prof_cycles, size, 1. * prof_cycles / ntime);
655 906 markom
 
656
      /* debug command */
657 883 markom
    } else if (strncmp (tmp1, "d", 1) == 0 || strncmp (tmp1, "debug", 5) == 0) {
658
      sscanf (tmp1, "%*s %i", &cuc_debug);
659
      if (cuc_debug < 0) cuc_debug = 0;
660
      if (cuc_debug > 9) cuc_debug = 9;
661 906 markom
 
662
      /* generate command */
663 883 markom
    } else if (strcmp (tmp1, "g") == 0 || strcmp (tmp1, "generate") == 0) {
664 906 markom
      /* check for function dependencies */
665 897 markom
      for (i = 0; i < prof_nfuncs; i++)
666 906 markom
        if (func[i]) func[i]->tmp = func_v[i];
667 915 markom
      for (i = 0; i < prof_nfuncs; i++) if (func[i])
668 906 markom
        for (j = 0; j < func[i]->nfdeps; j++)
669
          if (!func[i]->fdeps[j] || !func[i]->fdeps[j]->tmp) {
670
            printf ("Function %s must be selected for translation (required by %s)\n",
671
                    prof_func[j].name, prof_func[i].name);
672
            goto wait_command;
673
          }
674
      for (i = 0; i < prof_nfuncs; i++)
675 915 markom
        if (func[i] && func_v[i]) generate_function (func[i], prof_func[i].name, filename_cut);
676
      generate_main (prof_nfuncs, func, filename_cut);
677
 
678 918 markom
      /* selectall command */
679
    } else if (strcmp (tmp1, "sa") == 0 || strcmp (tmp1, "selectall") == 0) {
680
      char tmp[50], ch;
681
      int p, o, b, f;
682
      for (f = 0; f < prof_nfuncs; f++) if (func[f]) {
683
        func_v[f] = 1;
684
        printf ("Function %s selected for translation.\n", prof_func[f].name);
685
      }
686
 
687 906 markom
      /* select command */
688 897 markom
    } else if (strncmp (tmp1, "s", 1) == 0 || strncmp (tmp1, "select", 6) == 0) {
689
      char tmp[50], ch;
690
      int p, o, b, f;
691
      p = sscanf (tmp1, "%*s %s %i%c", tmp, &b, &ch);
692
      if (p < 1) printf ("Invalid parameters.\n");
693
      else {
694
        /* Check if we have valid option */
695
        for (f = 0; f < prof_nfuncs; f++)
696
          if (strcmp (prof_func[f].name, tmp) == 0 && func[f]) break;
697
        if (f < prof_nfuncs) {
698
          if (p == 1) {
699
            if (func[f]) {
700
              func_v[f] = 1;
701
              printf ("Function %s selected for translation.\n", prof_func[f].name);
702
            } else printf ("Function %s not suitable for translation.\n", prof_func[f].name);
703
          } else {
704
            if (!func_v[f])
705
              printf ("Function %s not yet selected for translation.\n", prof_func[f].name);
706
            if (p < 3) goto invalid_option;
707
            for (o = 0; option_char[o] != '\0' && option_char[o] != ch; o++);
708
            if (!option_char[o]) goto invalid_option;
709
            if (b < 0 || b >= func[f]->num_bb) goto invalid_option;
710
            if (o < 0 || o >= func[f]->bb[b].ntim) goto invalid_option;
711
 
712
            /* select an option */
713
            func[f]->bb[b].selected_tim = o;
714
            if (func[f]->bb[b].tim[o].nshared) {
715
              printf ("Option has shared instructions: ");
716
              print_shared (func[f], func[f]->bb[b].tim[o].shared, func[f]->bb[b].tim[o].nshared);
717
              printf ("\n");
718
            }
719 906 markom
            goto wait_command;
720 897 markom
invalid_option:
721
            printf ("Invalid option.\n");
722
          }
723
        } else printf ("Invalid function.\n");
724
      }
725 906 markom
 
726
      /* unselect command */
727 897 markom
    } else if (strncmp (tmp1, "u", 1) == 0 || strncmp (tmp1, "unselect", 8) == 0) {
728
      char tmp[50], ch;
729
      int p, o, b, f;
730
      p = sscanf (tmp1, "%*s %s %i%c", tmp, &b, &ch);
731
      if (p < 1) printf ("Invalid parameters.\n");
732
      else {
733
        /* Check if we have valid option */
734
        for (f = 0; f < prof_nfuncs; f++)
735
          if (strcmp (prof_func[f].name, tmp) == 0 && func[f]) break;
736
        if (f < prof_nfuncs) {
737
          if (p == 1) {
738
            if (func[f]) {
739
              func_v[f] = 0;
740
              printf ("Function %s unselected for translation.\n", prof_func[f].name);
741
            } else printf ("Function %s not suitable for translation.\n", prof_func[f].name);
742
          } else {
743
            if (p < 3) goto invalid_option;
744
            for (o = 0; option_char[o] != '\0' && option_char[o] != ch; o++);
745
            if (!option_char[o]) goto invalid_option;
746
            if (b < 0 || b >= func[f]->num_bb) goto invalid_option;
747
            if (o < 0 || o >= func[f]->bb[b].ntim) goto invalid_option;
748
 
749
            /* select an option */
750
            func[f]->bb[b].selected_tim = -1;
751
          }
752
        } else printf ("Invalid function.\n");
753
      }
754 906 markom
 
755
      /* options command */
756 883 markom
    } else if (strcmp (tmp1, "o") == 0 || strcmp (tmp1, "options") == 0) {
757 897 markom
      int any = 0;
758 883 markom
      printf ("Available options:\n");
759
      for (i = 0; i < prof_nfuncs; i++)
760 897 markom
        if (func[i]) {
761
          options_cmd (i, func[i]);
762
          any = 1;
763
        }
764 904 markom
      if (any) printf ("-----------------------------------------------------------------------------\n");
765 897 markom
      else printf ("Sorry. No available options.\n");
766 906 markom
 
767
      /* Ignore empty string */
768 902 markom
    } else if (strcmp (tmp1, "") == 0) {
769 906 markom
 
770
      /* help command */
771 883 markom
    } else {
772
      if (strcmp (tmp1, "h") != 0 && strcmp (tmp1, "help") != 0)
773
        printf ("Unknown command.\n");
774
      printf ("OpenRISC Custom Unit Compiler command prompt\n");
775 897 markom
      printf ("Available commands:\n");
776
      printf ("  h | help                   displays this help\n");
777
      printf ("  q | quit                   returns to or1ksim prompt\n");
778
      printf ("  p | profile                displays function profiling\n");
779
      printf ("  d | debug #                sets debug level (0-9)\n");
780
      printf ("  o | options                displays available options\n");
781
      printf ("  s | select func [option]   selects an option/function\n");
782
      printf ("  u | unselect func [option] unselects an option/function\n");
783
      printf ("  g | generate               generates verilog file\n");
784 883 markom
    }
785
  }
786
 
787 879 markom
  /* Dispose memory */
788
  for (i = 0; i < prof_nfuncs -1; i++)
789
    if (func[i]) free_func (func[i]);
790
 
791
  fclose (flog);
792
}
793
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.