install ld in the gcclibdir if present
[deliverable/binutils-gdb.git] / ld / ldlex.l
CommitLineData
2fa0b342
DHW
1%{
2/* Copyright (C) 1991 Free Software Foundation, Inc.
3
4This file is part of GLD, the Gnu Linker.
5
6GLD is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GLD is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GLD; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20/*
21 * $Id$
7ca04d28 22
2fa0b342
DHW
23 *
24*/
25
26
27
28/*SUPPRESS 529*/
29/*SUPPRESS 26*/
30/*SUPPRESS 29*/
1418c83b 31#define LEXDEBUG 0
2fa0b342
DHW
32#include "sysdep.h"
33#include "bfd.h"
34
35#include <ctype.h>
36#include "ldlex.h"
37
38#include "ld.h"
39#include "ldexp.h"
48491e2e 40#include "ldgramtb.h"
2fa0b342
DHW
41#include "ldmisc.h"
42
43#undef input
44#undef unput
45#define input lex_input
46#define unput lex_unput
47int debug;
7ca04d28 48
1d45ccb3 49
a37cc0c0 50static boolean ldgram_in_defsym;
1d45ccb3 51static boolean ldgram_had_equals;
1418c83b 52extern boolean ldgram_in_script;
2fa0b342
DHW
53static char *command_line;
54
55extern int fgetc();
56extern int yyparse();
57
58typedef struct {
59 char *name;
60int value;
61} keyword_type;
62#define RTOKEN(x) { yylval.token = x; return x; }
63keyword_type keywords[] =
64{
1d45ccb3 65"/", '/',
2fa0b342
DHW
66"MEMORY",MEMORY,
67"ORIGIN",ORIGIN,
68"BLOCK",BLOCK,
69"LENGTH",LENGTH,
70"ALIGN",ALIGN_K,
2fa0b342
DHW
71"ADDR",ADDR,
72"ENTRY",ENTRY,
73"NEXT",NEXT,
65c552e3
SC
74"sizeof_headers",SIZEOF_HEADERS,
75"SIZEOF_HEADERS",SIZEOF_HEADERS,
2fa0b342
DHW
76"MAP",MAP,
77"SIZEOF",SIZEOF,
78"TARGET",TARGET_K,
79"SEARCH_DIR",SEARCH_DIR,
80"OUTPUT",OUTPUT,
81"INPUT",INPUT,
82"DEFINED",DEFINED,
83"CREATE_OBJECT_SYMBOLS",CREATE_OBJECT_SYMBOLS,
fd846434 84"FORCE_COMMON_ALLOCATION",FORCE_COMMON_ALLOCATION,
2fa0b342
DHW
85"SECTIONS",SECTIONS,
86"FILL",FILL,
87"STARTUP",STARTUP,
fd846434 88"OUTPUT_FORMAT",OUTPUT_FORMAT,
a37cc0c0 89"OUTPUT_ARCH", OUTPUT_ARCH,
2fa0b342
DHW
90"HLL",HLL,
91"SYSLIB",SYSLIB,
92"FLOAT",FLOAT,
93"LONG", LONG,
94"SHORT", SHORT,
95"BYTE", BYTE,
96"NOFLOAT",NOFLOAT,
97"o",ORIGIN,
98"org",ORIGIN,
99"l", LENGTH,
100"len", LENGTH,
1010,0};
102unsigned int lineno;
103extern boolean hex_mode;
104FILE *ldlex_input_stack;
105static unsigned int have_pushback;
1d45ccb3 106
2fa0b342
DHW
107#define NPUSHBACK 10
108int pushback[NPUSHBACK];
109int thischar;
110extern char *ldfile_input_filename;
1418c83b 111int donehash = 0;
2fa0b342
DHW
112int
113lex_input()
114{
2fa0b342 115 if (have_pushback > 0)
1418c83b
SC
116 {
117 have_pushback --;
118 return thischar = pushback[have_pushback];
119 }
2fa0b342
DHW
120 if (ldlex_input_stack) {
121 thischar = fgetc(ldlex_input_stack);
122
123 if (thischar == EOF) {
124 fclose(ldlex_input_stack);
125 ldlex_input_stack = (FILE *)NULL;
126 ldfile_input_filename = (char *)NULL;
1418c83b 127 /* First char after script eof is a @ so that we can tell the grammer
a37cc0c0 128 that we've left */
1418c83b 129 thischar = '@';
2fa0b342
DHW
130
131 }
132 }
133 else if (command_line && *command_line) {
134 thischar = *(command_line++);
135 }
1418c83b
SC
136 else {
137 thischar = 0;
138 }
2fa0b342 139 if(thischar == '\t') thischar = ' ';
7ca04d28 140 if (thischar == '\n') { thischar = ' '; lineno++; }
2fa0b342
DHW
141 return thischar ;
142}
143
144void
145lex_unput(c)
146int c;
147{
148 if (have_pushback > NPUSHBACK) {
149 info("%F%P Too many pushbacks\n");
150 }
151
152 pushback[have_pushback] = c;
153 have_pushback ++;
154}
155
156
157 int
158yywrap()
159 { return 1; }
160/*VARARGS*/
161
162void
163allprint(x)
164int x;
165{
166fprintf(yyout,"%d",x);
167}
168
169void
170sprint(x)
171char *x;
172{
173fprintf(yyout,"%s",x);
174}
175
176int thischar;
177
178void parse_line(arg)
179char *arg;
180{
181 command_line = arg;
182 have_pushback = 0;
183 yyparse();
184}
185
186
187
188void
189parse_args(ac, av)
190int ac;
191char **av;
192{
193 char *p;
194 int i;
195 size_t size = 0;
196 char *dst;
197 debug = 1;
198 for (i= 1; i < ac; i++) {
199 size += strlen(av[i]) + 2;
200 }
201 dst = p = (char *)ldmalloc(size + 2);
202/* Put a space arount each option */
203
204
205 for (i =1; i < ac; i++) {
206
207 unsigned int s = strlen(av[i]);
208 *dst++ = ' ';
209 memcpy(dst, av[i], s);
210 dst[s] = ' ';
211 dst += s + 1;
212 }
213 *dst = 0;
214 parse_line(p);
215
216 free(p);
217
218
219}
220
1d45ccb3
SC
221static long
222DEFUN(number,(default_if_zero,base),
223 int default_if_zero AND
224 int base)
2fa0b342 225{
1418c83b 226 unsigned long l = 0;
1d45ccb3
SC
227 int ch = yytext[0];
228 if (ch == 0) {
229 base = default_if_zero;
230 }
231 while (1) {
232 switch (ch) {
233 case 'x':
234 base = 16;
235 break;
236 case 'k':
237 case 'K':
2fa0b342 238 l =l * 1024;
1d45ccb3
SC
239 break;
240 case 'm':
241 case 'M':
2fa0b342 242 l =l * 1024 * 1024;
1d45ccb3
SC
243 break;
244 case '0': case '1': case '2': case '3': case '4':
245 case '5': case '6': case '7': case '8': case '9':
246 l = l * base + ch - '0';
247 break;
248 case 'a': case 'b': case 'c' : case 'd' : case 'e': case 'f':
249 l =l *base + ch - 'a' + 10;
250 break;
251 case 'A': case 'B': case 'C' : case 'D' : case 'E': case 'F':
252 l =l *base + ch - 'A' + 10;
253 break;
254 default:
255 unput(ch);
256 yylval.integer = l;
257 return INT;
2fa0b342 258 }
1d45ccb3 259ch = input();
2fa0b342 260 }
2fa0b342
DHW
261}
262%}
263
264%a 4000
265%o 5000
1418c83b 266FILENAMECHAR [a-zA-Z0-9\/\.\-\_\+\=]
2fa0b342 267FILENAME {FILENAMECHAR}+
1d45ccb3 268WHITE [ \t]+
2fa0b342
DHW
269
270%%
2fa0b342 271
a37cc0c0
SC
272"@" { return '}'; }
273"\ -defsym\ " { ldgram_in_defsym = true; return OPTION_defsym; }
1418c83b 274"\ -noinhibit_exec\ " { return OPTION_noinhibit_exec; }
b6316534 275"\ -sort_common\ " { return OPTION_sort_common;}
1418c83b
SC
276"\ -format\ " { return OPTION_format; }
277"\ -n\ " { return OPTION_n; }
278"\ -r\ " { return OPTION_r; }
a37cc0c0 279"\ -i\ " { return OPTION_r; }
1418c83b
SC
280"\ -Ur\ " { return OPTION_Ur; }
281"\ -o\ " { return OPTION_o; }
282"\ -g\ " { return OPTION_g; }
283"\ -e\ " { return OPTION_e; }
284"\ -b\ " { return OPTION_b; }
285"\ -dc\ " { return OPTION_dc; }
286"\ -dp\ " { return OPTION_dp; }
287"\ -d\ " { return OPTION_d; }
288"\ -v\ " { return OPTION_v; }
289"\ -M\ " { return OPTION_M; }
290"\ -t\ " { return OPTION_t; }
291"\ -X\ " { return OPTION_X; }
292"\ -x\ " { return OPTION_x; }
293"\ -c\ " { return OPTION_c; }
294"\ -R\ " { return OPTION_R; }
295"\ -u\ " { return OPTION_u; }
296"\ -s\ " { return OPTION_s; }
297"\ -S\ " { return OPTION_S; }
a37cc0c0 298"\ -B{FILENAME}\ " { /* Ignored */ }
2fa0b342
DHW
299"\ -l"{FILENAME} {
300 yylval.name = buystring(yytext+3);
301 return OPTION_l;
302 }
303
304"\ -L"{FILENAME} {
305 yylval.name = buystring(yytext+3);
306 return OPTION_L;
307 }
1418c83b 308"\ -Ttext\ " {
2fa0b342
DHW
309 yylval.name = ".text";
310 return OPTION_Texp;
311 }
1418c83b 312"\ -Tdata\ " {
2fa0b342
DHW
313 yylval.name = ".data";
314 return OPTION_Texp;
315 }
1418c83b 316"\ -Tbss\ " {
2fa0b342
DHW
317 yylval.name = ".bss";
318 return OPTION_Texp;
319 }
48491e2e
SC
320"\ -O"{FILENAME} {
321 yylval.name = buystring(yytext+3);
322 return OPTION_Texp;
323 }
2fa0b342
DHW
324
325"\ -T"{FILENAME} {
326 yylval.name = buystring(yytext+3);
327 return OPTION_Tfile;
328 }
1418c83b 329"\ -T\ " {
2fa0b342
DHW
330 return OPTION_T;
331 }
332
7ca04d28
SC
333"\ -F"{FILENAME} {
334 return OPTION_F;
335 }
1418c83b 336"\ -F\ " {
7ca04d28
SC
337 return OPTION_F;
338 }
339
2fa0b342
DHW
340"\ -A"{FILENAME} {
341 yylval.name = buystring(yytext+3);
342 return OPTION_Aarch;
343 }
1d45ccb3 344
a37cc0c0
SC
345" " {
346 if (ldgram_had_equals == true) {
347 ldgram_in_defsym = false;
348 ldgram_had_equals = false;
349 }
350 }
2fa0b342
DHW
351"<<=" { RTOKEN(LSHIFTEQ);}
352">>=" { RTOKEN(RSHIFTEQ);}
353"||" { RTOKEN(OROR);}
354"==" { RTOKEN(EQ);}
355"!=" { RTOKEN(NE);}
356">=" { RTOKEN(GE);}
357"<=" { RTOKEN(LE);}
358"<<" { RTOKEN(LSHIFT);}
359">>" { RTOKEN(RSHIFT);}
360"+=" { RTOKEN(PLUSEQ);}
361"-=" { RTOKEN(MINUSEQ);}
362"*=" { RTOKEN(MULTEQ);}
363"/=" { RTOKEN(DIVEQ);}
364"&=" { RTOKEN(ANDEQ);}
365"|=" { RTOKEN(OREQ);}
2fa0b342
DHW
366"&&" { RTOKEN(ANDAND);}
367">" { RTOKEN('>');}
368"," { RTOKEN(',');}
369"&" { RTOKEN('&');}
370"|" { RTOKEN('|');}
371"~" { RTOKEN('~');}
372"!" { RTOKEN('!');}
373"?" { RTOKEN('?');}
374"*" { RTOKEN('*');}
375"%" { RTOKEN('%');}
376"<" { RTOKEN('<');}
2fa0b342
DHW
377">" { RTOKEN('>');}
378"}" { RTOKEN('}') ; }
379"{" { RTOKEN('{'); }
380")" { RTOKEN(')');}
381"(" { RTOKEN('(');}
382"]" { RTOKEN(']');}
383"[" { RTOKEN('[');}
384":" { RTOKEN(':'); }
fd846434 385";" { RTOKEN('\;');}
2fa0b342 386"-" { RTOKEN('-');}
1418c83b 387
2fa0b342
DHW
388
389
390"/*" {
391 while (1) {
392 int ch;
393 ch = input();
394 while (ch != '*') {
2fa0b342
DHW
395 ch = input();
396 }
397
398
399
400 if (input() == '/') {
401 break;
402 }
403 unput(yytext[yyleng-1]);
404 }
405}
406
407"\""[^\"]*"\"" {
408
409 yylval.name = buystring(yytext+1);
410 yylval.name[yyleng-2] = 0; /* Fry final quote */
411 return NAME;
412}
2fa0b342 413
7ca04d28 414{FILENAMECHAR} {
2fa0b342 415
7ca04d28 416 boolean loop = false;
7ca04d28 417 int ch;
2fa0b342 418 keyword_type *k;
1418c83b 419
1d45ccb3
SC
420 /* If we're in hex mode (only after a -T) then all we can see are numbers
421 hex digit we see will be a number. */
422
423 if (hex_mode) {
424 return number(16, 16);
1418c83b
SC
425 }
426
1d45ccb3
SC
427 /* If we're in a defsym then all things starting with a digit are in
428 hex */
429
430 if (isdigit(yytext[0]) && ldgram_in_defsym) {
431 return number(16,16);
432 }
433
434
435 /* Otherwise if we're in a script we will parse the numbers
436 normally */
437
438 if (ldgram_in_script == true && isdigit(yytext[0])) {
439 return number(8,10);
1418c83b
SC
440 }
441
1d45ccb3
SC
442 /* Anywhere not in a script or defsym, an opertor is part of a
443 filename, except / and, which is an operator when on its own */
444 if (ldgram_in_script == true|| ldgram_in_defsym == true) {
1418c83b 445
1d45ccb3
SC
446 switch (yytext[0]) {
447 case '*': RTOKEN('*');
1418c83b 448
1d45ccb3
SC
449 case '=': {
450 ldgram_had_equals = true;
451 RTOKEN('=');
452 }
453 break;
454 case '/': {
455 if (ldgram_in_defsym) RTOKEN('/');
456 }
457 break;
7ca04d28
SC
458 case '+': RTOKEN('+');
459 case '-': RTOKEN('-');
1d45ccb3
SC
460 case '!': RTOKEN('!');
461 case '~': RTOKEN('~');
7ca04d28 462 }
2fa0b342 463 }
2fa0b342 464
1d45ccb3
SC
465
466/* Otherwise this must be a file or a symbol name, and it will continue to be a
467 filename until we get to something strange. In scripts operator looking
468 things are taken to be operators, except /, which will be left
469 */
7ca04d28
SC
470 ch = input();
471 while (true)
472 {
1d45ccb3
SC
473 if (ldgram_in_defsym == true) {
474 switch (ch) {
475 case '*':
476 case '=':
477 case '+':
478 case '/':
479 case '-':
480 case '!':
481 case '~':
482 goto quit;
483 }
484
ac004870 485 }
a37cc0c0
SC
486 if(ldgram_in_script == true) {
487 switch (ch) {
488 case '*':
489 case '=':
490 case '+':
491 case '-':
492 case '!':
493 case '~':
494 goto quit;
495 }
496 }
497
1d45ccb3
SC
498 if (isalpha(ch) || isdigit(ch) || ch == '.' || ch == '_' ||
499 ch == '/' || ch == '.' || ch == '+' || ch == '-' || ch =='=') {
7ca04d28
SC
500 yytext[yyleng++] = ch;
501 }
502 else
503 break;
504 ch = input();
505 }
1d45ccb3 506 quit:;
2fa0b342
DHW
507 yytext[yyleng] = 0;
508 unput(ch);
2fa0b342 509
1d45ccb3 510 for(k = keywords; k ->name != (char *)NULL; k++) {
2fa0b342
DHW
511 if (strcmp(k->name, yytext)==0) {
512 yylval.token = k->value;
513 return k->value;
514 }
515 }
516 yylval.name = buystring(yytext);
517 return NAME;
518}
519
520
521
522
523
524%%
This page took 0.074463 seconds and 4 git commands to generate.