summaryrefslogtreecommitdiff
path: root/gcc/config/mep/mep-pragma.c
blob: 45a4b4496a41d168ae13212156957bb7eb508b04 (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
/* Definitions of Toshiba Media Processor
   Copyright (C) 2001-2013 Free Software Foundation, Inc.
   Contributed by Red Hat, Inc.

This file is part of GCC.

GCC 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, or (at your option) any later
version.

GCC 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 GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "diagnostic-core.h"
#include "c-family/c-pragma.h"
#include "cpplib.h"
#include "hard-reg-set.h"
#include "output.h" /* for decode_reg_name */
#include "mep-protos.h"
#include "function.h"
#define MAX_RECOG_OPERANDS 10
#include "reload.h"
#include "target.h"

enum cw_which { CW_AVAILABLE, CW_CALL_SAVED };

/* This is normally provided by rtl.h but we can't include that file
   here.  It's safe to copy the definition here because we're only
   using it internally; the value isn't passed to functions outside
   this file.  */
#ifndef INVALID_REGNUM
#define INVALID_REGNUM                    (~(unsigned int) 0)
#endif

static enum cpp_ttype
mep_pragma_lex (tree *valp)
{
  enum cpp_ttype t = pragma_lex (valp);
  if (t == CPP_EOF)
    t = CPP_PRAGMA_EOL;
  return t;
}

static void
mep_pragma_io_volatile (cpp_reader *reader ATTRIBUTE_UNUSED)
{
  /* On off.  */
  tree val;
  enum cpp_ttype type;
  const char * str;

  type = mep_pragma_lex (&val);
  if (type == CPP_NAME)
    {
      str = IDENTIFIER_POINTER (val);

      type = mep_pragma_lex (&val);
      if (type != CPP_PRAGMA_EOL)
	warning (0, "junk at end of #pragma io_volatile");

      if (strcmp (str, "on") == 0)
	{
	  target_flags |= MASK_IO_VOLATILE;
	  return;
	}
      if (strcmp (str, "off") == 0)
	{
	  target_flags &= ~ MASK_IO_VOLATILE;
	  return;
	}
    }

  error ("#pragma io_volatile takes only on or off");
}

static unsigned int
parse_cr_reg (const char * str)
{
  unsigned int regno;

  regno = decode_reg_name (str);
  if (regno >= FIRST_PSEUDO_REGISTER)
    return INVALID_REGNUM;

  /* Verify that the regno is in CR_REGS.  */
  if (! TEST_HARD_REG_BIT (reg_class_contents[CR_REGS], regno))
    return INVALID_REGNUM;
  return regno;
}

static bool
parse_cr_set (HARD_REG_SET * set)
{
  tree val;
  enum cpp_ttype type;
  unsigned int last_regno = INVALID_REGNUM;
  bool do_range = false;

  CLEAR_HARD_REG_SET (*set);

  while ((type = mep_pragma_lex (&val)) != CPP_PRAGMA_EOL)
    {
      if (type == CPP_COMMA)
	{
	  last_regno = INVALID_REGNUM;
	  do_range = false;
	}
      else if (type == CPP_ELLIPSIS)
	{
	  if (last_regno == INVALID_REGNUM)
	    {
	      error ("invalid coprocessor register range");
	      return false;
	    }
	  do_range = true;
	}
      else if (type == CPP_NAME || type == CPP_STRING)
	{
	  const char *str;
	  unsigned int regno, i;

	  if (TREE_CODE (val) == IDENTIFIER_NODE)
	    str = IDENTIFIER_POINTER (val);
  	  else if (TREE_CODE (val) == STRING_CST)
	    str = TREE_STRING_POINTER (val);
	  else
	    gcc_unreachable ();

	  regno = parse_cr_reg (str);
	  if (regno == INVALID_REGNUM)
	    {
	      error ("invalid coprocessor register %qE", val);
	      return false;
	    }

	  if (do_range)
	    {
	      if (last_regno > regno)
		i = regno, regno = last_regno;
	      else
		i = last_regno;
	      do_range = false;
	    }
	  else
	    last_regno = i = regno;

	  while (i <= regno)
	    {
	      SET_HARD_REG_BIT (*set, i);
	      i++;
	    }
	}
      else
	{
	  error ("malformed coprocessor register");
	  return false;
	}
    }
  return true;
}

static void
mep_pragma_coprocessor_which (enum cw_which cw_which)
{
  HARD_REG_SET set;

  /* Process the balance of the pragma and turn it into a hard reg set.  */
  if (! parse_cr_set (&set))
    return;

  /* Process the collected hard reg set.  */
  switch (cw_which)
    {
    case CW_AVAILABLE:
      {
	int i;
	for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
	  if (TEST_HARD_REG_BIT (set, i))
	    fixed_regs[i] = 0;
      }
      break;

    case CW_CALL_SAVED:
      {
	int i;
	for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
	  if (TEST_HARD_REG_BIT (set, i))
	    fixed_regs[i] = call_used_regs[i] = 0;
      }
      break;

    default:
      gcc_unreachable ();
    }

  /* Fix up register class hierarchy.  */
  mep_save_register_info ();
  mep_reinit_regs ();

  if (cfun == 0)
    {
      init_dummy_function_start ();
      init_caller_save ();
      expand_dummy_function_end ();
    }
  else
    {
      init_caller_save ();
    }
}

static void
mep_pragma_coprocessor_width (void)
{
  tree val;
  enum cpp_ttype type;
  HOST_WIDE_INT i;

  type = mep_pragma_lex (&val);
  switch (type)
    {
    case CPP_NUMBER:
      if (! tree_fits_uhwi_p (val))
	break;
      i = tree_to_uhwi (val);
      /* This pragma no longer has any effect.  */
#if 0
      if (i == 32)
	target_flags &= ~MASK_64BIT_CR_REGS;
      else if (i == 64)
	target_flags |= MASK_64BIT_CR_REGS;
      else
	break;
      targetm.init_builtins ();
#else
      if (i != 32 && i != 64)
	break;
#endif

      type = mep_pragma_lex (&val);
      if (type != CPP_PRAGMA_EOL)
	warning (0, "junk at end of #pragma GCC coprocessor width");
      return;

    default:
      break;
    }

  error ("#pragma GCC coprocessor width takes only 32 or 64");
}

static void
mep_pragma_coprocessor_subclass (void)
{
  tree val;
  enum cpp_ttype type;
  HARD_REG_SET set;
  int class_letter;
  enum reg_class rclass;

  type = mep_pragma_lex (&val);
  if (type != CPP_CHAR)
    goto syntax_error;
  class_letter = tree_to_uhwi (val);
  if (class_letter >= 'A' && class_letter <= 'D')
    switch (class_letter)
      {
      case 'A':
	rclass = USER0_REGS;
	break;
      case 'B':
	rclass = USER1_REGS;
	break;
      case 'C':
	rclass = USER2_REGS;
	break;
      case 'D':
	rclass = USER3_REGS;
	break;
      }
  else
    {
      error ("#pragma GCC coprocessor subclass letter must be in [ABCD]");
      return;
    }
  if (reg_class_size[rclass] > 0)
    {
      error ("#pragma GCC coprocessor subclass '%c' already defined",
	     class_letter);
      return;
    }

  type = mep_pragma_lex (&val);
  if (type != CPP_EQ)
    goto syntax_error;

  if (! parse_cr_set (&set))
    return;

  /* Fix up register class hierarchy.  */
  COPY_HARD_REG_SET (reg_class_contents[rclass], set);
  mep_init_regs ();
  return;

 syntax_error:
  error ("malformed #pragma GCC coprocessor subclass");
}

static void
mep_pragma_disinterrupt (cpp_reader *reader ATTRIBUTE_UNUSED)
{
  tree val;
  enum cpp_ttype type;
  int saw_one = 0;

  for (;;)
    {
      type = mep_pragma_lex (&val);
      if (type == CPP_COMMA)
	continue;
      if (type != CPP_NAME)
	break;
      mep_note_pragma_disinterrupt (IDENTIFIER_POINTER (val));
      saw_one = 1;
    }
  if (!saw_one || type != CPP_PRAGMA_EOL)
    {
      error ("malformed #pragma disinterrupt");
      return;
    }
}

static void
mep_pragma_coprocessor (cpp_reader *reader ATTRIBUTE_UNUSED)
{
  tree val;
  enum cpp_ttype type;

  type = mep_pragma_lex (&val);
  if (type != CPP_NAME)
    {
      error ("malformed #pragma GCC coprocessor");
      return;
    }

  if (!TARGET_COP)
    error ("coprocessor not enabled");

  if (strcmp (IDENTIFIER_POINTER (val), "available") == 0)
    mep_pragma_coprocessor_which (CW_AVAILABLE);
  else if (strcmp (IDENTIFIER_POINTER (val), "call_saved") == 0)
    mep_pragma_coprocessor_which (CW_CALL_SAVED);
  else if (strcmp (IDENTIFIER_POINTER (val), "width") == 0)
    mep_pragma_coprocessor_width ();
  else if (strcmp (IDENTIFIER_POINTER (val), "subclass") == 0)
    mep_pragma_coprocessor_subclass ();
  else
    error ("unknown #pragma GCC coprocessor %E", val);
}

static void
mep_pragma_call (cpp_reader *reader ATTRIBUTE_UNUSED)
{
  tree val;
  enum cpp_ttype type;
  int saw_one = 0;

  for (;;)
    {
      type = mep_pragma_lex (&val);
      if (type == CPP_COMMA)
	continue;
      if (type != CPP_NAME)
	break;
      mep_note_pragma_call (IDENTIFIER_POINTER (val));
      saw_one = 1;
    }
  if (!saw_one || type != CPP_PRAGMA_EOL)
    {
      error ("malformed #pragma call");
      return;
    }
}

void
mep_register_pragmas (void)
{
  c_register_pragma ("custom", "io_volatile", mep_pragma_io_volatile);
  c_register_pragma ("GCC", "coprocessor", mep_pragma_coprocessor);
  c_register_pragma (0, "disinterrupt", mep_pragma_disinterrupt);
  c_register_pragma (0, "call", mep_pragma_call);
}