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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_39/] [or1ksim/] [cuc/] [cuc.c] - Blame information for rev 934

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

powered by: WebSVN 2.1.0

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