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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_42/] [or1ksim/] [cuc/] [cuc.c] - Blame information for rev 931

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

powered by: WebSVN 2.1.0

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