aboutsummaryrefslogtreecommitdiff
path: root/gas/config/loongarch-parse.y
blob: 63a4fcf0cdab9b63453e84f9ee67c98fd9c146b6 (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
425
426
427
428
429
430
431
/*
   Copyright (C) 2021-2024 Free Software Foundation, Inc.

   This file is part of GAS, the GNU Assembler.

   GAS 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.

   GAS 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; see the file COPYING3.  If not,
   see <http://www.gnu.org/licenses/>.  */
%{
#include "as.h"
#include "loongarch-lex.h"
#include "loongarch-parse.h"
static void yyerror (const char *s ATTRIBUTE_UNUSED)
{
};
int yylex (void);


static struct reloc_info *top, *end;

static expressionS const_0 =
{
  .X_op = O_constant,
  .X_add_number = 0
};

static int
is_const (struct reloc_info *info)
{
  return (info->type == BFD_RELOC_LARCH_SOP_PUSH_ABSOLUTE
	  && info->value.X_op == O_constant);
}

int
loongarch_parse_expr (const char *expr,
		      struct reloc_info *reloc_stack_top,
		      size_t max_reloc_num,
		      size_t *reloc_num,
		      offsetT *imm)
{
  int ret;
  struct yy_buffer_state *buffstate;
  top = reloc_stack_top;
  end = top + max_reloc_num;
  buffstate = yy_scan_string (expr);
  ret = yyparse ();

  if (ret == 0)
    {
      if (is_const (top - 1))
	*imm = (--top)->value.X_add_number;
      else
	*imm = 0;
      *reloc_num = top - reloc_stack_top;
    }
  yy_delete_buffer (buffstate);

  return ret;
}

static void
emit_const (offsetT imm)
{
  if (end <= top)
    as_fatal (_("expr too huge"));
  top->type = BFD_RELOC_LARCH_SOP_PUSH_ABSOLUTE;
  top->value.X_op = O_constant;
  top->value.X_add_number = imm;
  top++;
}

static const char *
my_getExpression (expressionS *ep, const char *str)
{
  char *save_in, *ret;

  if (*str == ':')
    {
      unsigned long j;
      char *str_1 = (char *) str;
      j = strtol (str_1, &str_1, 10);
      get_internal_label (ep, j, *str_1 == 'f');
      return NULL;
    }
  save_in = input_line_pointer;
  input_line_pointer = (char *)str;
  expression (ep);
  ret = input_line_pointer;
  input_line_pointer = save_in;
  return ret;
}

static void
emit_const_var (const char *op)
{
  expressionS ep;

  if (end <= top)
    as_fatal (_("expr too huge"));

  my_getExpression (&ep, op);

  if (ep.X_op != O_constant)
    as_bad ("illegal operand: %s", op);

  top->value.X_op = O_constant;
  top->value.X_add_number = ep.X_add_number;
  top->type = BFD_RELOC_LARCH_SOP_PUSH_ABSOLUTE;
  top++;
}

static void
reloc (const char *op_c_str, const char *id_c_str, offsetT addend)
{
  expressionS id_sym_expr;
  bfd_reloc_code_real_type btype;

  if (end <= top)
    as_fatal (_("expr too huge"));

  /* For compatible old asm code.  */
  if (0 == strcmp (op_c_str, "plt"))
    btype = BFD_RELOC_LARCH_B26;
  else
    {
      btype = loongarch_larch_reloc_name_lookup (NULL, op_c_str);
      if (btype == BFD_RELOC_NONE)
	as_fatal (_("unsupported modifier %s"), op_c_str);
    }

  if (id_c_str)
  {
    my_getExpression (&id_sym_expr, id_c_str);
    id_sym_expr.X_add_number += addend;
  }
  else
  {
    id_sym_expr.X_op = O_constant;
    id_sym_expr.X_add_number = addend;
  }

  top->value = id_sym_expr;
  top->type = btype;
  top++;
}

static void
emit_unary (char op)
{
  struct reloc_info *s_top = top - 1;
  if (is_const (s_top))
    {
      offsetT opr = s_top->value.X_add_number;
      switch (op)
	{
	case '+':
	  break;
	case '-':
	  opr = -opr;
	  break;
	case '~':
	  opr = ~opr;
	  break;
	case '!':
	  opr = !opr;
	  break;
	default:
	  abort ();
	}
      s_top->value.X_add_number = opr;
    }
  else
    {
      if (end <= top)
	as_fatal (_("expr too huge"));
      switch (op)
	{
	case '!':
	  top->type = BFD_RELOC_LARCH_SOP_NOT;
	  break;
	default:
	  abort ();
	}
      top->value = const_0;
      top++;
    }
}

static void
emit_bin (int op)
{
  struct reloc_info *last_1st = top - 1, *last_2nd = top - 2;
  if (is_const (last_1st) && is_const (last_2nd))
    {
      offsetT opr1 = last_2nd->value.X_add_number;
      offsetT opr2 = last_1st->value.X_add_number;
      switch (op)
	{
	case '*':
	  opr1 = opr1 * opr2;
	  break;
	case '/':
	  opr1 = opr1 / opr2;
	  break;
	case '%':
	  opr1 = opr1 % opr2;
	  break;
	case '+':
	  opr1 = opr1 + opr2;
	  break;
	case '-':
	  opr1 = opr1 - opr2;
	  break;
	case LEFT_OP:
	  opr1 = opr1 << opr2;
	  break;
	case RIGHT_OP:
	  /* Algorithm right shift.  */
	  opr1 = (offsetT)opr1 >> (offsetT)opr2;
	  break;
	case '<':
	  opr1 = opr1 < opr2;
	  break;
	case '>':
	  opr1 = opr1 > opr2;
	  break;
	case LE_OP:
	  opr1 = opr1 <= opr2;
	  break;
	case GE_OP:
	  opr1 = opr1 >= opr2;
	  break;
	case EQ_OP:
	  opr1 = opr1 == opr2;
	  break;
	case NE_OP:
	  opr1 = opr1 != opr2;
	  break;
	case '&':
	  opr1 = opr1 & opr2;
	  break;
	case '^':
	  opr1 = opr1 ^ opr2;
	  break;
	case '|':
	  opr1 = opr1 | opr2;
	  break;
	case AND_OP:
	  opr1 = opr1 && opr2;
	  break;
	case OR_OP:
	  opr1 = opr1 || opr2;
	  break;
	default:
	  abort ();
	}
      last_2nd->value.X_add_number = opr1;
      last_1st->type = 0;
      top--;
    }
  else
    {
      if (end <= top)
	as_fatal (_("expr too huge"));
      switch (op)
	{
	case '+':
	  top->type = BFD_RELOC_LARCH_SOP_ADD;
	  break;
	case '-':
	  top->type = BFD_RELOC_LARCH_SOP_SUB;
	  break;
	case LEFT_OP:
	  top->type = BFD_RELOC_LARCH_SOP_SL;
	  break;
	case RIGHT_OP:
	  top->type = BFD_RELOC_LARCH_SOP_SR;
	  break;
	case '&':
	  top->type = BFD_RELOC_LARCH_SOP_AND;
	  break;
	default:
	  abort ();
	}
      top->value = const_0;
      top++;
    }
}

static void
emit_if_else (void)
{
  struct reloc_info *last_1st = top - 1;
  struct reloc_info *last_2nd = top - 2;
  struct reloc_info *last_3rd = top - 3;
  if (is_const (last_1st) && is_const (last_2nd) && is_const (last_3rd))
    {
      offsetT opr1 = last_3rd->value.X_add_number;
      offsetT opr2 = last_2nd->value.X_add_number;
      offsetT opr3 = last_1st->value.X_add_number;
      opr1 = opr1 ? opr2 : opr3;
      last_3rd->value.X_add_number = opr1;
      last_2nd->type = 0;
      last_1st->type = 0;
      top -= 2;
    }
  else
    {
      if (end <= top)
	as_fatal (_("expr too huge"));
      top->type = BFD_RELOC_LARCH_SOP_IF_ELSE;
      top->value = const_0;
      top++;
    }
}

%}

%union {
char *c_str;
offsetT imm;
}

%token <imm> INTEGER
%token <c_str> IDENTIFIER
%type <imm> addend

%token LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP AND_OP OR_OP
%start expression
%%

primary_expression
	: INTEGER {emit_const ($1);}
	| IDENTIFIER {emit_const_var ($1);}
	| '(' expression ')'
	| '%' IDENTIFIER '(' IDENTIFIER addend ')' {reloc ($2, $4, $5); free ($2); free ($4);}
	| '%' IDENTIFIER '(' INTEGER addend ')' {reloc ($2, NULL, $4 + $5); free ($2);}
	;

addend
	: addend '-' INTEGER {$$ -= $3;}
	| addend '+' INTEGER {$$ += $3;}
	| {$$ = 0;}
	;

unary_expression
	: primary_expression
	| '+' unary_expression {emit_unary ('+');}
	| '-' unary_expression {emit_unary ('-');}
	| '~' unary_expression {emit_unary ('~');}
	| '!' unary_expression {emit_unary ('!');}
	;

multiplicative_expression
	: unary_expression
	| multiplicative_expression '*' unary_expression {emit_bin ('*');}
	| multiplicative_expression '/' unary_expression {emit_bin ('/');}
	| multiplicative_expression '%' unary_expression {emit_bin ('%');}
	;

additive_expression
	: multiplicative_expression
	| additive_expression '+' multiplicative_expression {emit_bin ('+');}
	| additive_expression '-' multiplicative_expression {emit_bin ('-');}
	;

shift_expression
	: additive_expression
	| shift_expression LEFT_OP additive_expression {emit_bin (LEFT_OP);}
	| shift_expression RIGHT_OP additive_expression {emit_bin (RIGHT_OP);}
	;

relational_expression
	: shift_expression
	| relational_expression '<' shift_expression {emit_bin ('<');}
	| relational_expression '>' shift_expression {emit_bin ('>');}
	| relational_expression LE_OP shift_expression {emit_bin (LE_OP);}
	| relational_expression GE_OP shift_expression {emit_bin (GE_OP);}
	;

equality_expression
	: relational_expression
	| equality_expression EQ_OP relational_expression {emit_bin (EQ_OP);}
	| equality_expression NE_OP relational_expression {emit_bin (NE_OP);}
	;

and_expression
	: equality_expression
	| and_expression '&' equality_expression {emit_bin ('&');}
	;

exclusive_or_expression
	: and_expression
	| exclusive_or_expression '^' and_expression {emit_bin ('^');}
	;

inclusive_or_expression
	: exclusive_or_expression
	| inclusive_or_expression '|' exclusive_or_expression {emit_bin ('|');}
	;

logical_and_expression
	: inclusive_or_expression
	| logical_and_expression AND_OP inclusive_or_expression {emit_bin (AND_OP);}
	;

logical_or_expression
	: logical_and_expression
	| logical_or_expression OR_OP logical_and_expression {emit_bin (OR_OP);}
	;

conditional_expression
	: logical_or_expression
	| logical_or_expression '?' expression ':' conditional_expression {emit_if_else ();}
	;

expression
	: conditional_expression
	;
%%