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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc2/] [or1ksim/] [cuc/] [cuc.c] - Blame information for rev 936

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

powered by: WebSVN 2.1.0

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