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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_69/] [or1ksim/] [cuc/] [cuc.c] - Blame information for rev 915

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

powered by: WebSVN 2.1.0

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