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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_68/] [or1ksim/] [cuc/] [cuc.c] - Blame information for rev 937

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

powered by: WebSVN 2.1.0

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