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

Subversion Repositories or1k

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

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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