summaryrefslogtreecommitdiff
path: root/sim/igen/lf.c
blob: b63d981c1714fa595326947760edd77ae317b2fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* The IGEN simulator generator for GDB, the GNU Debugger.

   Copyright 2002-2013 Free Software Foundation, Inc.

   Contributed by Andrew Cagney.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */



#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>

#include "config.h"
#include "misc.h"
#include "lf.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif

struct _lf
{
  FILE *stream;
  int line_nr;			/* nr complete lines written, curr line is line_nr+1 */
  int indent;
  int line_blank;
  const char *name;
  const char *program;
  lf_file_references references;
  lf_file_type type;
};


lf *
lf_open (char *name,
	 char *real_name,
	 lf_file_references references,
	 lf_file_type type, const char *program)
{
  /* create a file object */
  lf *new_lf = ZALLOC (lf);
  ASSERT (new_lf != NULL);
  new_lf->references = references;
  new_lf->type = type;
  new_lf->name = (real_name == NULL ? name : real_name);
  new_lf->program = program;
  /* attach to stdout if pipe */
  if (!strcmp (name, "-"))
    {
      new_lf->stream = stdout;
    }
  else
    {
      /* create a new file */
      new_lf->stream = fopen (name, "w");
      if (new_lf->stream == NULL)
	{
	  perror (name);
	  exit (1);
	}
    }
  return new_lf;
}


void
lf_close (lf *file)
{
  if (file->stream != stdout)
    {
      if (fclose (file->stream))
	{
	  perror ("lf_close.fclose");
	  exit (1);
	}
      free (file);
    }
}


int
lf_putchr (lf *file, const char chr)
{
  int nr = 0;
  if (chr == '\n')
    {
      file->line_nr += 1;
      file->line_blank = 1;
    }
  else if (file->line_blank)
    {
      int pad;
      for (pad = file->indent; pad > 0; pad--)
	putc (' ', file->stream);
      nr += file->indent;
      file->line_blank = 0;
    }
  putc (chr, file->stream);
  nr += 1;
  return nr;
}

int
lf_write (lf *file, const char *string, int strlen_string)
{
  int nr = 0;
  int i;
  for (i = 0; i < strlen_string; i++)
    nr += lf_putchr (file, string[i]);
  return nr;
}


void
lf_indent_suppress (lf *file)
{
  file->line_blank = 0;
}


int
lf_putstr (lf *file, const char *string)
{
  int nr = 0;
  const char *chp;
  if (string != NULL)
    {
      for (chp = string; *chp != '\0'; chp++)
	{
	  nr += lf_putchr (file, *chp);
	}
    }
  return nr;
}

static int
do_lf_putunsigned (lf *file, unsigned u)
{
  int nr = 0;
  if (u > 0)
    {
      nr += do_lf_putunsigned (file, u / 10);
      nr += lf_putchr (file, (u % 10) + '0');
    }
  return nr;
}


int
lf_putint (lf *file, int decimal)
{
  int nr = 0;
  if (decimal == 0)
    nr += lf_putchr (file, '0');
  else if (decimal < 0)
    {
      nr += lf_putchr (file, '-');
      nr += do_lf_putunsigned (file, -decimal);
    }
  else if (decimal > 0)
    {
      nr += do_lf_putunsigned (file, decimal);
    }
  else
    ASSERT (0);
  return nr;
}


int
lf_printf (lf *file, const char *fmt, ...)
{
  int nr = 0;
  char buf[1024];
  va_list ap;

  va_start (ap, fmt);
  vsprintf (buf, fmt, ap);
  /* FIXME - this is really stuffed but so is vsprintf() on a sun! */
  ASSERT (strlen (buf) < sizeof (buf));
  nr += lf_putstr (file, buf);
  va_end (ap);
  return nr;
}


int
lf_print__line_ref (lf *file, line_ref *line)
{
  return lf_print__external_ref (file, line->line_nr, line->file_name);
}

int
lf_print__external_ref (lf *file, int line_nr, const char *file_name)
{
  int nr = 0;
  switch (file->references)
    {
    case lf_include_references:
      lf_indent_suppress (file);
      nr += lf_putstr (file, "#line ");
      nr += lf_putint (file, line_nr);
      nr += lf_putstr (file, " \"");
      nr += lf_putstr (file, file_name);
      nr += lf_putstr (file, "\"\n");
      break;
    case lf_omit_references:
      nr += lf_putstr (file, "/* ");
      nr += lf_putstr (file, file_name);
      nr += lf_putstr (file, ":");
      nr += lf_putint (file, line_nr);
      nr += lf_putstr (file, "*/\n");
      break;
    }
  return nr;
}

int
lf_print__internal_ref (lf *file)
{
  int nr = 0;
  nr += lf_print__external_ref (file, file->line_nr + 2, file->name);
  /* line_nr == last_line, want to number from next */
  return nr;
}

void
lf_indent (lf *file, int delta)
{
  file->indent += delta;
}


int
lf_print__gnu_copyleft (lf *file)
{
  int nr = 0;
  switch (file->type)
    {
    case lf_is_c:
    case lf_is_h:
      nr += lf_printf (file, "\
/* This file is part of GDB.\n\
\n\
   Copyright 2002, 2007 Free Software Foundation, Inc.\n\
\n\
   This program is free software; you can redistribute it and/or modify\n\
   it under the terms of the GNU General Public License as published by\n\
   the Free Software Foundation; either version 3 of the License, or\n\
   (at your option) any later version.\n\
\n\
   This program is distributed in the hope that it will be useful,\n\
   but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
   GNU General Public License for more details.\n\
\n\
   You should have received a copy of the GNU General Public License\n\
   along with this program.  If not, see <http://www.gnu.org/licenses/>.\n\
\n\
   --\n\
\n\
   This file was generated by the program %s */\n\
", filter_filename (file->program));
      break;
    default:
      ASSERT (0);
      break;
    }
  return nr;
}


int
lf_putbin (lf *file, int decimal, int width)
{
  int nr = 0;
  int bit;
  ASSERT (width > 0);
  for (bit = 1 << (width - 1); bit != 0; bit >>= 1)
    {
      if (decimal & bit)
	nr += lf_putchr (file, '1');
      else
	nr += lf_putchr (file, '0');
    }
  return nr;
}

int
lf_print__this_file_is_empty (lf *file, const char *reason)
{
  int nr = 0;
  switch (file->type)
    {
    case lf_is_c:
    case lf_is_h:
      nr += lf_printf (file,
		       "/* This generated file (%s) is intentionally left blank",
		       file->name);
      if (reason != NULL)
	nr += lf_printf (file, " - %s", reason);
      nr += lf_printf (file, " */\n");
      break;
    default:
      ERROR ("Bad switch");
    }
  return nr;
}

int
lf_print__ucase_filename (lf *file)
{
  int nr = 0;
  const char *chp = file->name;
  while (*chp != '\0')
    {
      char ch = *chp;
      if (islower (ch))
	{
	  nr += lf_putchr (file, toupper (ch));
	}
      else if (ch == '.')
	nr += lf_putchr (file, '_');
      else
	nr += lf_putchr (file, ch);
      chp++;
    }
  return nr;
}

int
lf_print__file_start (lf *file)
{
  int nr = 0;
  switch (file->type)
    {
    case lf_is_h:
    case lf_is_c:
      nr += lf_print__gnu_copyleft (file);
      nr += lf_printf (file, "\n");
      nr += lf_printf (file, "#ifndef ");
      nr += lf_print__ucase_filename (file);
      nr += lf_printf (file, "\n");
      nr += lf_printf (file, "#define ");
      nr += lf_print__ucase_filename (file);
      nr += lf_printf (file, "\n");
      nr += lf_printf (file, "\n");
      break;
    default:
      ASSERT (0);
    }
  return nr;
}


int
lf_print__file_finish (lf *file)
{
  int nr = 0;
  switch (file->type)
    {
    case lf_is_h:
    case lf_is_c:
      nr += lf_printf (file, "\n");
      nr += lf_printf (file, "#endif /* _");
      nr += lf_print__ucase_filename (file);
      nr += lf_printf (file, "_*/\n");
      break;
    default:
      ASSERT (0);
    }
  return nr;
}


int
lf_print__function_type (lf *file,
			 const char *type,
			 const char *prefix, const char *trailing_space)
{
  int nr = 0;
  nr += lf_printf (file, "%s\\\n(%s)", prefix, type);
  if (trailing_space != NULL)
    nr += lf_printf (file, "%s", trailing_space);
  return nr;
}

int
lf_print__function_type_function (lf *file,
				  print_function * print_type,
				  const char *prefix,
				  const char *trailing_space)
{
  int nr = 0;
  nr += lf_printf (file, "%s\\\n(", prefix);
  nr += print_type (file);
  nr += lf_printf (file, ")");
  if (trailing_space != NULL)
    nr += lf_printf (file, "%s", trailing_space);
  return nr;
}