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 897

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

powered by: WebSVN 2.1.0

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