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