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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or1ksim/] [cuc/] [cuc.c] - Blame information for rev 903

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
#include "sim-config.h"
27
#include "cuc.h"
28
#include "insn.h"
29
#include "profiler.h"
30 883 markom
#include "opcode/or32.h"
31 897 markom
#include "parse.h"
32 879 markom
 
33
FILE *flog;
34 883 markom
int cuc_debug = 0;
35 879 markom
 
36
/* Last used registers by software convention */
37
const int call_saved[MAX_REGS] = {
38
  0, 0, 0, 1, 1, 1, 1, 1,
39
  1, 1, 0, 1, 0, 1, 0, 1,
40
  0, 1, 0, 1, 0, 1, 0, 1,
41
  0, 1, 0, 1, 0, 1, 0, 1,
42
  1, 1};
43
 
44
cuc_timings *preunroll_bb (char *bb_filename, cuc_func *f, cuc_timings *timings, int b, int i, int j)
45
{
46
  cuc_func *func;
47 883 markom
  cucdebug (2, "BB%i unroll %i times preroll %i times\n", b, j, i);
48 879 markom
  func = preunroll_loop (f, b, i, j, bb_filename);
49 883 markom
  if (cuc_debug >= 2) print_cuc_bb (func, "AFTER_PREUNROLL");
50 879 markom
 
51
  log ("Optimizing.\n");
52 902 markom
  optimize_cmovs (func);
53
  if (cuc_debug >= 6) print_cuc_bb (func, "AFTER_OPT_CMOVS");
54 879 markom
  optimize_tree (func);
55 902 markom
  if (cuc_debug >= 6) print_cuc_bb (func, "AFTER_OPT_TREE1");
56 879 markom
  remove_nops (func);
57 902 markom
  if (cuc_debug >= 6) print_cuc_bb (func, "NO_NOPS");
58 879 markom
  remove_dead (func);
59 883 markom
  if (cuc_debug >= 5) print_cuc_bb (func, "AFTER_DEAD1");
60 879 markom
  optimize_bb (func);
61 883 markom
  if (cuc_debug >= 5) print_cuc_bb (func, "AFTER_OPT_BB");
62 879 markom
  remove_dead_bb (func);
63 883 markom
  if (cuc_debug >= 5) print_cuc_bb (func, "AFTER_DEAD_BB");
64 879 markom
  optimize_tree (func);
65 883 markom
  if (cuc_debug >= 3) print_cuc_bb (func, "AFTER_OPT_TREE");
66
  log ("Common subexpression elimination.\n");
67
  cse (func);
68
  if (cuc_debug >= 3) print_cuc_bb (func, "AFTER_CSE");
69 879 markom
  remove_dead (func);
70 883 markom
  if (cuc_debug >= 5) print_cuc_bb (func, "AFTER_DEAD");
71 879 markom
  remove_trivial_regs (func);
72 883 markom
  if (cuc_debug >= 2) print_cuc_bb (func, "AFTER_TRIVIAL");
73
  add_latches (func);
74
  if (cuc_debug >= 1) print_cuc_bb (func, "AFTER_LATCHES");
75
  set_io (func);
76 897 markom
  add_memory_dep (func, func->memory_order);
77 883 markom
  if (cuc_debug >= 7) print_cuc_bb (func, "AFTER_MEMORY_DEP");
78 879 markom
  add_data_dep (func);
79 883 markom
  if (cuc_debug >= 8) print_cuc_bb (func, "AFTER_DATA_DEP");
80 897 markom
  schedule_memory (func, func->memory_order);
81 883 markom
  if (cuc_debug >= 7) print_cuc_bb (func, "AFTER_SCHEDULE_MEM");
82 879 markom
 
83
  analyse_timings (func, timings);
84 883 markom
  cucdebug (2, "new_time = %i, old_time = %i, size = %f\n",
85 879 markom
           timings->new_time, func->orig_time, timings->size);
86
  log ("new time = %icyc, old_time = %icyc, size = %.0f gates\n",
87
         timings->new_time, func->orig_time, timings->size);
88
  //output_verilog (func, argv[1]);
89
  free_func (func);
90
  timings->b = b;
91
  timings->unroll = j;
92
  timings->preroll = i;
93 883 markom
  timings->nshared = 0;
94 879 markom
  return timings;
95
}
96
 
97
int tim_comp (cuc_timings *a, cuc_timings *b)
98
{
99
  if (a->new_time < b->new_time) return -1;
100
  else if (a->new_time > b->new_time) return 1;
101
  else return 0;
102
}
103
 
104
cuc_func *analyse_function (char *module_name, long orig_time,
105 897 markom
                unsigned long start_addr, unsigned long end_addr,
106
                int memory_order)
107 879 markom
{
108
  cuc_timings timings;
109
  cuc_func *func = (cuc_func *) malloc (sizeof (cuc_func));
110
  cuc_func *saved;
111
  int b, i, j;
112
  char tmp1[256];
113
  char tmp2[256];
114
 
115
  func->orig_time = orig_time;
116
  func->start_addr = start_addr;
117
  func->end_addr = end_addr;
118 897 markom
  func->memory_order = memory_order;
119 879 markom
 
120
  sprintf (tmp1, "%s.bin", module_name);
121 883 markom
  cucdebug (2, "Loading %s.bin\n", module_name);
122 897 markom
  if (cuc_load (tmp1)) {
123
    free (func);
124
    return NULL;
125
  }
126 879 markom
 
127
  log ("Detecting basic blocks\n");
128
  detect_bb (func);
129 883 markom
  if (cuc_debug >= 2) print_cuc_insns ("WITH_BB_LIMITS", 0);
130 879 markom
 
131
  //sprintf (tmp1, "%s.bin.mp", module_name);
132
  sprintf (tmp2, "%s.bin.bb", module_name);
133
  generate_bb_seq (func, config.sim.mprof_fn, tmp2);
134 897 markom
  log ("Assuming %i clk cycle load (%i cyc burst)\n", runtime.cuc.mdelay[0], runtime.cuc.mdelay[2]);
135
  log ("Assuming %i clk cycle store (%i cyc burst)\n", runtime.cuc.mdelay[1], runtime.cuc.mdelay[3]);
136 879 markom
 
137
  build_bb (func);
138 883 markom
  if (cuc_debug >= 5) print_cuc_bb (func, "AFTER_BUILD_BB");
139 879 markom
  reg_dep (func);
140
 
141
  log ("Detecting dependencies\n");
142 883 markom
  if (cuc_debug >= 2) print_cuc_bb (func, "AFTER_REG_DEP");
143 902 markom
  optimize_cmovs (func);
144
  if (cuc_debug >= 6) print_cuc_bb (func, "AFTER_OPT_CMOVS");
145 879 markom
  optimize_tree (func);
146
  log ("Optimizing.\n");
147 883 markom
  if (cuc_debug >= 2) print_cuc_bb (func, "AFTER_OPT_TREE1");
148 879 markom
  remove_nops (func);
149 883 markom
  if (cuc_debug >= 6) print_cuc_bb (func, "NO_NOPS");
150 879 markom
  remove_dead (func);
151 883 markom
  if (cuc_debug >= 6) print_cuc_bb (func, "AFTER_DEAD1");
152 879 markom
  optimize_bb (func);
153 883 markom
  if (cuc_debug >= 6) print_cuc_bb (func, "AFTER_OPT_BB");
154 879 markom
  remove_dead_bb (func);
155 883 markom
  if (cuc_debug >= 2) print_cuc_bb (func, "AFTER_DEAD_BB");
156 879 markom
  optimize_tree (func);
157 883 markom
  if (cuc_debug >= 2) print_cuc_bb (func, "AFTER_OPT_TREE");
158
  log ("Common subexpression elimination.\n");
159
  cse (func);
160
  if (cuc_debug >= 3) print_cuc_bb (func, "AFTER_CSE");
161 879 markom
  remove_dead (func);
162 883 markom
  if (cuc_debug >= 5) print_cuc_bb (func, "AFTER_DEAD");
163 879 markom
  remove_trivial_regs (func);
164 883 markom
  if (cuc_debug >= 2) print_cuc_bb (func, "AFTER_TRIVIAL");
165 879 markom
 
166 897 markom
#if 0
167 883 markom
  csm (func);
168 897 markom
#endif
169 879 markom
  assert (saved = dup_func (func));
170 883 markom
 
171
  timings.preroll = timings.unroll = 1;
172
  timings.nshared = 0;
173
  add_latches (func);
174
  set_io (func);
175
 
176
  if (cuc_debug >= 1) print_cuc_bb (func, "AFTER_LATCHES");
177
  analyse_timings (func, &timings);
178 897 markom
  add_memory_dep (func, func->memory_order);
179 883 markom
  if (cuc_debug >= 7) print_cuc_bb (func, "AFTER_MEMORY_DEP");
180 879 markom
  add_data_dep (func);
181 883 markom
  if (cuc_debug >= 8) print_cuc_bb (func, "AFTER_DATA_DEP");
182 879 markom
  schedule_memory (func, memory_order);
183 883 markom
  if (cuc_debug >= 7) print_cuc_bb (func, "AFTER_SCHEDULE_MEM");
184 879 markom
 
185 883 markom
  //output_verilog (func, module_name);
186 879 markom
  free_func (func);
187 883 markom
  log ("Base option: pre%i,un%i,sha%i: %icyc %.1f\n",
188
        timings.preroll, timings.unroll, timings.nshared, timings.new_time, timings.size);
189
  saved->timings = timings;
190 879 markom
 
191
#if 1
192
  /* detect and unroll simple loops */
193
  for (b = 0; b < saved->num_bb; b++) {
194
    cuc_timings t[MAX_UNROLL * MAX_PREROLL];
195
    cuc_timings *ut;
196
    cuc_timings *cut = &t[0];
197
    int nt = 1;
198
    double csize;
199 897 markom
    saved->bb[b].selected_tim = -1;
200 879 markom
 
201
    /* Is it a loop? */
202
    if (saved->bb[b].next[0] != b && saved->bb[b].next[1] != b) continue;
203
    t[0] = timings;
204
    t[0].b = b;
205
    t[0].preroll = 1;
206
    t[0].unroll = 1;
207 883 markom
    t[0].nshared = 0;
208 879 markom
 
209
    sprintf (tmp1, "%s.bin.bb", module_name);
210
    i = 1;
211
    do {
212
      cuc_timings *pt;
213
      cuc_timings *cpt = cut;
214
      j = 1;
215
 
216
      do {
217
        pt = cpt;
218
        cpt = preunroll_bb (tmp1, saved, &t[nt++], b, ++j, i);
219
      } while (j <= MAX_PREROLL && pt->new_time >= cpt->new_time);
220
      i++;
221
      ut = cut;
222
      cut = preunroll_bb (tmp1, saved, &t[nt++], b, 1, i);
223
    } while (i <= MAX_UNROLL && ut->new_time >= cut->new_time);
224
 
225
    /* Sort the timings */
226 883 markom
#if 0
227
    if (cuc_debug >= 3)
228 879 markom
    for (i = 0; i < nt; i++) printf ("%i:%i,%i: %icyc\n",
229
                    t[i].b, t[i].preroll, t[i].unroll, t[i].new_time);
230 883 markom
#endif
231 879 markom
 
232
    qsort (t, nt, sizeof (cuc_timings), (int (*)(const void *, const void *))tim_comp);
233
 
234
    /* Delete timings, that have worst time and bigger size than other */
235
    j = 1;
236
    csize = t[0].size;
237
    for (i = 1; i < nt; i++)
238
      if (t[i].size < csize) t[j++] = t[i];
239
    nt = j;
240 883 markom
 
241
    cucdebug (1, "Available options\n");
242
    for (i = 0; i < nt; i++) cucdebug (1, "%i:%i,%i: %icyc %.1f\n",
243
        t[i].b, t[i].preroll, t[i].unroll, t[i].new_time, t[i].size);
244
    /* Add results from CSM */
245
    j = nt;
246
    for (i = 0; i < saved->bb[b].ntim; i++) {
247
      int i1;
248
      for (i1 = 0; i1 < nt; i1++) {
249
        t[j] = t[i1];
250
        t[j].size += saved->bb[b].tim[i].size - timings.size;
251
        t[j].new_time += saved->bb[b].tim[i].new_time - timings.new_time;
252
        t[j].nshared = saved->bb[b].tim[i].nshared;
253
        t[j].shared = saved->bb[b].tim[i].shared;
254
        if (++j >= MAX_UNROLL * MAX_PREROLL) goto full;
255
      }
256
    }
257
 
258
full:
259
    nt = j;
260 879 markom
 
261 883 markom
    cucdebug (1, "Available options:\n");
262
    for (i = 0; i < nt; i++) cucdebug (1, "%i:%i,%i: %icyc %.1f\n",
263
        t[i].b, t[i].preroll, t[i].unroll, t[i].new_time, t[i].size);
264 879 markom
 
265 883 markom
    /* Sort again with new timings added */
266
    qsort (t, nt, sizeof (cuc_timings), (int (*)(const void *, const void *))tim_comp);
267
 
268
    /* Delete timings, that have worst time and bigger size than other */
269
    j = 1;
270
    csize = t[0].size;
271
    for (i = 1; i < nt; i++)
272
      if (t[i].size < csize) t[j++] = t[i];
273
    nt = j;
274
 
275
    cucdebug (1, "Available options:\n");
276
    for (i = 0; i < nt; i++) cucdebug (1, "%i:%i,%i: %icyc %.1f\n",
277
                               t[i].b, t[i].preroll, t[i].unroll, t[i].new_time, t[i].size);
278
 
279
    if (saved->bb[b].ntim) free (saved->bb[b].tim);
280 879 markom
    saved->bb[b].ntim = nt;
281
    assert (saved->bb[b].tim = (cuc_timings *) malloc (sizeof (cuc_timings) * nt));
282
 
283
    /* Copy options in reverse order -- smallest first */
284
    for (i = 0; i < nt; i++) saved->bb[b].tim[i] = t[nt - 1 - i];
285 883 markom
 
286
    log ("Available options:\n");
287
    for (i = 0; i < saved->bb[b].ntim; i++) {
288
      log ("%i:pre%i,un%i,sha%i: %icyc %.1f\n",
289
        saved->bb[b].tim[i].b, saved->bb[b].tim[i].preroll, saved->bb[b].tim[i].unroll,
290
        saved->bb[b].tim[i].nshared, saved->bb[b].tim[i].new_time, saved->bb[b].tim[i].size);
291
    }
292 879 markom
  }
293
#endif
294
  return saved;
295
}
296
 
297 897 markom
/* Utility option formatting functions */
298
static const char *option_char = "?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
299
 
300
/*static */char *gen_option (char *s, int bb_no, int f_opt)
301 883 markom
{
302 897 markom
  if (bb_no >= 0) sprintf (s, "%i", bb_no);
303
  assert (f_opt <= strlen (option_char));
304
  sprintf (s, "%s%c", s, option_char[f_opt]);
305
  return s;
306
}
307
 
308
/*static */void print_option (int bb_no, int f_opt)
309
{
310
  char tmp1[10];
311
  char tmp2[10];
312
  sprintf (tmp2, "%s", gen_option (tmp1, bb_no, f_opt));
313
  printf ("%3s", tmp2);
314
}
315
 
316
static char *format_func_options (char *s, cuc_func *f)
317
{
318
  int b, first = 1;
319
  *s = '\0';
320
  for (b = 0; b < f->num_bb; b++)
321
    if (f->bb[b].selected_tim >= 0) {
322
      char tmp[10];
323
      sprintf (s, "%s%s%s", s, first ? "" : ",", gen_option (tmp, b, f->bb[b].selected_tim));
324
      first = 0;
325
    }
326
  return s;
327
}
328
 
329
static void options_cmd (int func_no, cuc_func *f)
330
{
331 883 markom
  int b, i;
332 903 markom
  char tmp[30];
333 897 markom
  char *name = prof_func[func_no].name;
334 903 markom
  printf ("---------------------------------------------------------------------------\n");
335
  printf ("|%-26s|pre/unrolled|shared|  time  |  gates |old_time|\n",
336
            strstrip (tmp, name, 26));
337
  printf ("|                  BASE    |%4i / %4i | %4i |%8i|%8.f|%8i|\n", 1, 1, 0,
338
          f->timings.new_time, f->timings.size, f->orig_time);
339 883 markom
  for (b = 0; b < f->num_bb; b++) {
340
    /* Print out results */
341 897 markom
    for (i = 1; i < f->bb[b].ntim; i++) { /* First one is base option */
342
      int time = f->bb[b].tim[i].new_time - f->timings.new_time;
343
      double size = f->bb[b].tim[i].size - f->timings.size;
344 903 markom
      printf ("|                 ");
345 897 markom
      print_option (b, i);
346 903 markom
      printf ("      |%4i / %4i | %4i |%+8i|%+8.f|        |\n",
347 897 markom
        f->bb[b].tim[i].preroll, f->bb[b].tim[i].unroll, f->bb[b].tim[i].nshared,
348
        time, size);
349 883 markom
    }
350
  }
351
}
352
 
353 897 markom
/* Generates a function, based on specified parameters */
354
cuc_func *generate_function (cuc_func *rf, char *name)
355
{
356
  int b, i, j;
357
  char tmp[256];
358
  cuc_timings tt;
359
  cuc_func *f;
360
  assert (f = dup_func (rf));
361
 
362
  log ("Generating function %s.\n", name);
363
  printf ("Generating function %s.\n", name);
364
 
365 902 markom
  if (cuc_debug >= 2) print_cuc_bb (f, "BEFORE_GENERATE");
366 897 markom
  add_latches (f);
367
  set_io (f);
368
  if (cuc_debug >= 1) print_cuc_bb (f, "AFTER_LATCHES");
369
 
370
  format_func_options (tmp, rf);
371
  if (strlen (tmp)) printf ("Applying options: %s\n", tmp);
372 902 markom
  else printf ("Using basic options.\n");
373 897 markom
 
374
  /* Generate function as specified by options */
375
  for (b = 0; b < f->num_bb; b++) {
376
    cuc_timings *st;
377
    if (rf->bb[b].selected_tim < 0) continue;
378
    st = &rf->bb[b].tim[rf->bb[b].selected_tim];
379
    sprintf (tmp, "%s.bin.bb", name);
380
    preunroll_bb (&tmp[0], f, &tt, b, st->preroll, st->unroll);
381
    if (cuc_debug >= 1) print_cuc_bb (f, "AFTER_PREUNROLL");
382
  }
383
  for (b = 0; b < f->num_bb; b++) {
384
    cuc_timings *st;
385
    if (rf->bb[b].selected_tim < 0) continue;
386
    st = &rf->bb[b].tim[rf->bb[b].selected_tim];
387
    if (!st->nshared) continue;
388
    assert (0);
389
    //csm_gen (f, rf, st->nshared, st->shared);
390
  }
391
  analyse_timings (f, &tt);
392
  add_memory_dep (f, f->memory_order);
393
  if (cuc_debug >= 7) print_cuc_bb (f, "AFTER_MEMORY_DEP");
394
  add_data_dep (f);
395
  if (cuc_debug >= 8) print_cuc_bb (f, "AFTER_DATA_DEP");
396
  schedule_memory (f, f->memory_order);
397
  if (cuc_debug >= 7) print_cuc_bb (f, "AFTER_SCHEDULE_MEM");
398
  output_verilog (f, name);
399
  return f;
400
}
401
 
402
/* Calculates required time, based on selected options */
403
int calc_cycles (cuc_func *f)
404
{
405
  int b, i, ntime = f->timings.new_time;
406
  for (b = 0; b < f->num_bb; b++)
407
    if (f->bb[b].selected_tim >= 0) {
408
      assert (f->bb[b].selected_tim < f->bb[b].ntim);
409
      ntime += f->bb[b].tim[f->bb[b].selected_tim].new_time - f->timings.new_time;
410
    }
411
  return ntime;
412
}
413
 
414
/* Calculates required size, based on selected options */
415
double calc_size (cuc_func *f)
416
{
417
  int b, i;
418
  double size = f->timings.size;
419
  for (b = 0; b < f->num_bb; b++)
420
    if (f->bb[b].selected_tim >= 0) {
421
      assert (f->bb[b].selected_tim < f->bb[b].ntim);
422
      size += f->bb[b].tim[f->bb[b].selected_tim].size - f->timings.size;
423
    }
424
  return size;
425
}
426
 
427 879 markom
/* Dumps specified function to file (hex) */
428
unsigned long extract_function (char *out_fn, unsigned long start_addr)
429
{
430
  FILE *fo;
431
  unsigned long a = start_addr;
432
  int x = 0;
433
  assert (fo = fopen (out_fn, "wt+"));
434
 
435
  do {
436
    unsigned long d = evalsim_mem32 (a);
437
    int index = insn_decode (d);
438
    assert (index >= 0);
439
    if (x) x++;
440
    if (strcmp (insn_name (index), "l.jr") == 0) x = 1;
441
    a += 4;
442
    fprintf (fo, "%08x\n", d);
443
  } while (x < 2);
444
 
445
  fclose (fo);
446
  return a - 4;
447
}
448
 
449
static cuc_func *func[MAX_FUNCS];
450 897 markom
static int func_v[MAX_FUNCS];
451 879 markom
 
452
void main_cuc (char *filename)
453
{
454 883 markom
  int i, j;
455 879 markom
  char tmp1[256];
456
 
457 883 markom
  printf ("Entering OpenRISC Custom Unit Compiler command prompt\n");
458
  printf ("Using profile file \"%s\" and memory profile file \"%s\".\n", config.sim.prof_fn, config.sim.mprof_fn);
459 879 markom
  sprintf (tmp1, "%s.log", filename);
460 883 markom
  printf ("Analyzing. (log file \"%s\").\n", tmp1);
461 879 markom
  assert (flog = fopen (tmp1, "wt+"));
462
 
463
  /* Loads in the specified timings table */
464 897 markom
  printf ("Using timings from \"%s\" at %s\n",config.cuc.timings_fn,
465
                 generate_time_pretty (tmp1, config.sim.clkcycle_ps));
466
  load_timing_table (config.cuc.timings_fn);
467
  runtime.cuc.cycle_duration = 1000. * config.sim.clkcycle_ps;
468
  printf ("Multicycle logic %s, bursts %s, %s memory order.\n",
469
    config.cuc.no_multicycle ? "OFF" : "ON", config.cuc.enable_bursts ? "ON" : "OFF",
470
    config.cuc.memory_order == MO_NONE ? "no" : config.cuc.memory_order == MO_WEAK ? "weak" :
471
    config.cuc.memory_order == MO_STRONG ? "strong" : "exact");
472 879 markom
 
473
  prof_set (1, 0);
474
  assert (prof_acquire (config.sim.prof_fn) == 0);
475 898 markom
  //cuc_debug = 9;
476 897 markom
 
477
  if (config.cuc.calling_convention)
478
    printf ("Assuming OpenRISC standard calling convention.\n");
479 879 markom
 
480
  /* Try all functions except "total" */
481
  for (i = 0; i < prof_nfuncs - 1; i++) {
482
    long orig_time;
483
    unsigned long start_addr, end_addr;
484
    orig_time = prof_func[i].cum_cycles;
485
    start_addr = prof_func[i].addr;
486
 
487
    /* Extract the function from the binary */
488
    sprintf (tmp1, "%s.bin", prof_func[i].name);
489
    end_addr = extract_function (tmp1, start_addr);
490
 
491
    log ("Testing function %s (%08x - %08x)\n", prof_func[i].name, start_addr, end_addr);
492 897 markom
    printf ("Testing function %s (%08x - %08x)\n", prof_func[i].name, start_addr, end_addr);
493
    func[i] = analyse_function (prof_func[i].name, orig_time, start_addr,
494
                   end_addr, config.cuc.memory_order);
495
    func_v[i] = 0;
496 879 markom
  }
497
 
498 883 markom
  while (1) {
499
    char *s;
500
    printf ("(cuc) ");
501
    fflush (stdout);
502
    fgets(tmp1, sizeof tmp1, stdin);
503
    for (s = tmp1; *s != '\0' && *s != '\n' && *s != '\r'; s++);
504
    *s = '\0';
505
 
506
    if (strcmp (tmp1, "q") == 0 || strcmp (tmp1, "quit") == 0) {
507
      break;
508
    } else if (strcmp (tmp1, "p") == 0 || strcmp (tmp1, "profile") == 0) {
509 897 markom
      int ntime = 0;
510
      int size = 0;
511
      printf ("-----------------------------------------------------------------------------\n");
512
      printf ("|function name       |calls|avg cycles  |old%| max. f.  | impr. f.| options |\n");
513
      printf ("|--------------------+-----+------------+----+----------|---------+---------|\n");
514 883 markom
      for (j = 0; j < prof_nfuncs; j++) {
515
        int bestcyc = 0, besti = 0;
516 897 markom
        char tmp[100];
517 883 markom
        for (i = 0; i < prof_nfuncs; i++)
518
          if (prof_func[i].cum_cycles > bestcyc) {
519
            bestcyc = prof_func[i].cum_cycles;
520
            besti = i;
521
          }
522
        i = besti;
523 897 markom
        printf ("|%-20s|%5i|%12.1f|%3.0f%%| ",
524
                strstrip (tmp, prof_func[i].name, 20),  prof_func[i].calls,
525 883 markom
                ((double)prof_func[i].cum_cycles / prof_func[i].calls),
526
                (100. * prof_func[i].cum_cycles / prof_cycles));
527
        if (func[i]) {
528 897 markom
          double f = 1.0;
529
          if (func_v[i]) {
530
            int nt = calc_cycles (func[i]);
531
            int s = calc_size (func[i]);
532
            f = func[i]->orig_time / nt;
533
            ntime += nt * func[i]->num_runs;
534
            size += s;
535
          } else ntime += prof_func[i].cum_cycles;
536
          printf ("%8.1f |%8.1f | %-8s|\n", 1.f * prof_func[i].cum_cycles / func[i]->timings.new_time, f, format_func_options (tmp, func[i]));
537
        } else {
538
          printf ("     N/A |     N/A |         |\n");
539
          ntime += prof_func[i].cum_cycles;
540
        }
541
        prof_func[i].cum_cycles = -prof_func[i].cum_cycles;
542 883 markom
      }
543 897 markom
      for (i = 0; i < prof_nfuncs; i++)
544
        prof_func[i].cum_cycles = -prof_func[i].cum_cycles;
545
      printf ("-----------------------------------------------------------------------------\n");
546 903 markom
      printf ("Total %i cycles (was %i), total added gates = %i. Speed factor %.1f\n", ntime, prof_cycles, size, 1. * prof_cycles / ntime);
547 883 markom
    } else if (strncmp (tmp1, "d", 1) == 0 || strncmp (tmp1, "debug", 5) == 0) {
548 897 markom
      /* debug command */
549 883 markom
      sscanf (tmp1, "%*s %i", &cuc_debug);
550
      if (cuc_debug < 0) cuc_debug = 0;
551
      if (cuc_debug > 9) cuc_debug = 9;
552
    } else if (strcmp (tmp1, "g") == 0 || strcmp (tmp1, "generate") == 0) {
553 897 markom
      /* generate command */
554
      for (i = 0; i < prof_nfuncs; i++)
555
        if (func[i] && func_v[i]) generate_function (func[i], prof_func[i].name);
556
    } else if (strncmp (tmp1, "s", 1) == 0 || strncmp (tmp1, "select", 6) == 0) {
557
      /* select command */
558
      char tmp[50], ch;
559
      int p, o, b, f;
560
      p = sscanf (tmp1, "%*s %s %i%c", tmp, &b, &ch);
561
      if (p < 1) printf ("Invalid parameters.\n");
562
      else {
563
        /* Check if we have valid option */
564
        for (f = 0; f < prof_nfuncs; f++)
565
          if (strcmp (prof_func[f].name, tmp) == 0 && func[f]) break;
566
        if (f < prof_nfuncs) {
567
          if (p == 1) {
568
            if (func[f]) {
569
              func_v[f] = 1;
570
              printf ("Function %s selected for translation.\n", prof_func[f].name);
571
            } else printf ("Function %s not suitable for translation.\n", prof_func[f].name);
572
          } else {
573
            if (!func_v[f])
574
              printf ("Function %s not yet selected for translation.\n", prof_func[f].name);
575
            if (p < 3) goto invalid_option;
576
            for (o = 0; option_char[o] != '\0' && option_char[o] != ch; o++);
577
            if (!option_char[o]) goto invalid_option;
578
            if (b < 0 || b >= func[f]->num_bb) goto invalid_option;
579
            if (o < 0 || o >= func[f]->bb[b].ntim) goto invalid_option;
580
 
581
            /* select an option */
582
            func[f]->bb[b].selected_tim = o;
583
            if (func[f]->bb[b].tim[o].nshared) {
584
              printf ("Option has shared instructions: ");
585
              print_shared (func[f], func[f]->bb[b].tim[o].shared, func[f]->bb[b].tim[o].nshared);
586
              printf ("\n");
587
            }
588
            continue;
589
invalid_option:
590
            printf ("Invalid option.\n");
591
          }
592
        } else printf ("Invalid function.\n");
593
      }
594
    } else if (strncmp (tmp1, "u", 1) == 0 || strncmp (tmp1, "unselect", 8) == 0) {
595
      /* unselect command */
596
      char tmp[50], ch;
597
      int p, o, b, f;
598
      p = sscanf (tmp1, "%*s %s %i%c", tmp, &b, &ch);
599
      if (p < 1) printf ("Invalid parameters.\n");
600
      else {
601
        /* Check if we have valid option */
602
        for (f = 0; f < prof_nfuncs; f++)
603
          if (strcmp (prof_func[f].name, tmp) == 0 && func[f]) break;
604
        if (f < prof_nfuncs) {
605
          if (p == 1) {
606
            if (func[f]) {
607
              func_v[f] = 0;
608
              printf ("Function %s unselected for translation.\n", prof_func[f].name);
609
            } else printf ("Function %s not suitable for translation.\n", prof_func[f].name);
610
          } else {
611
            if (p < 3) goto invalid_option;
612
            for (o = 0; option_char[o] != '\0' && option_char[o] != ch; o++);
613
            if (!option_char[o]) goto invalid_option;
614
            if (b < 0 || b >= func[f]->num_bb) goto invalid_option;
615
            if (o < 0 || o >= func[f]->bb[b].ntim) goto invalid_option;
616
 
617
            /* select an option */
618
            func[f]->bb[b].selected_tim = -1;
619
          }
620
        } else printf ("Invalid function.\n");
621
      }
622 883 markom
    } else if (strcmp (tmp1, "o") == 0 || strcmp (tmp1, "options") == 0) {
623 897 markom
      int any = 0;
624
      /* options command */
625 883 markom
      printf ("Available options:\n");
626
      for (i = 0; i < prof_nfuncs; i++)
627 897 markom
        if (func[i]) {
628
          options_cmd (i, func[i]);
629
          any = 1;
630
        }
631 903 markom
      if (any) printf ("---------------------------------------------------------------------------\n");
632 897 markom
      else printf ("Sorry. No available options.\n");
633 902 markom
    } else if (strcmp (tmp1, "") == 0) {
634
      /* Ignore empty string */
635 883 markom
    } else {
636 897 markom
      /* help command */
637 883 markom
      if (strcmp (tmp1, "h") != 0 && strcmp (tmp1, "help") != 0)
638
        printf ("Unknown command.\n");
639
      printf ("OpenRISC Custom Unit Compiler command prompt\n");
640 897 markom
      printf ("Available commands:\n");
641
      printf ("  h | help                   displays this help\n");
642
      printf ("  q | quit                   returns to or1ksim prompt\n");
643
      printf ("  p | profile                displays function profiling\n");
644
      printf ("  d | debug #                sets debug level (0-9)\n");
645
      printf ("  o | options                displays available options\n");
646
      printf ("  s | select func [option]   selects an option/function\n");
647
      printf ("  u | unselect func [option] unselects an option/function\n");
648
      printf ("  g | generate               generates verilog file\n");
649 883 markom
    }
650
  }
651
 
652 879 markom
  /* Dispose memory */
653
  for (i = 0; i < prof_nfuncs -1; i++)
654
    if (func[i]) free_func (func[i]);
655
 
656
  fclose (flog);
657
}
658
 

powered by: WebSVN 2.1.0

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