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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [cuc/] [cuc.c] - Blame information for rev 932

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

powered by: WebSVN 2.1.0

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