3 /* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 This was written by steve chamberlain
29 /* start-sanitize-mpw */
31 /* Prevent enum redefinition problems. */
32 #define TRUE_FALSE_ALREADY_DEFINED
34 /* end-sanitize-mpw */
46 /* The type of top-level parser input.
47 yylex and yyparse (indirectly) both check this. */
48 input_type parser_input;
50 /* Radix to use for bfd_scan_vma -- 0 (default to base 10) or 16. */
53 /* Line number in the current input file.
54 (FIXME Actually, it doesn't appear to get reset for each file?) */
55 unsigned int lineno = 1;
57 /* Support for flex reading from more than one input file (stream).
58 `include_stack' is flex's input state for each open file;
59 `file_name_stack' is the file names.
61 If `include_stack_ptr' is 0, we haven't started reading anything yet.
62 Otherwise, stack elements 0 through `include_stack_ptr - 1' are valid. */
65 #define YY_INPUT(buf,result,max_size) yy_input(buf, &result, max_size)
67 #define MAX_INCLUDE_DEPTH 10
68 static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
69 static char *file_name_stack[MAX_INCLUDE_DEPTH];
70 static unsigned int include_stack_ptr = 0;
72 static YY_BUFFER_STATE yy_create_string_buffer PARAMS ((const char *string,
74 static void yy_input PARAMS ((char *, int *result, int max_size));
76 static void comment PARAMS ((void));
77 static void lex_warn_invalid PARAMS ((char *where, char *what));
80 EXPRESSION definitely in an expression
81 SCRIPT definitely in a script
82 BOTH either EXPRESSION or SCRIPT
83 DEFSYMEXP in an argument to -defsym
86 #define RTOKEN(x) { yylval.token = x; return x; }
88 /* Some versions of flex want this. */
90 int yywrap () { return 1; }
97 CMDFILENAMECHAR [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\-\~]
98 CMDFILENAMECHAR1 [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\~]
99 FILENAMECHAR1 [_a-zA-Z\/\.\\\$\_\~]
100 SYMBOLCHARN [_a-zA-Z\/\.\\0-9]
101 FILENAMECHAR [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\\,\~]
102 FILENAME {FILENAMECHAR}+
105 NOCFILENAMECHAR [_a-zA-Z0-9\/\.\-\_\+\$\:\[\]\\\~]
115 if (parser_input != input_selected)
117 /* The first token of the input determines the initial parser state. */
118 input_type t = parser_input;
119 parser_input = input_selected;
122 case input_script: return INPUT_SCRIPT; break;
123 case input_mri_script: return INPUT_MRI_SCRIPT; break;
124 case input_defsym: return INPUT_DEFSYM; break;
129 <BOTH,SCRIPT,EXPRESSION>"/*" { comment(); }
132 <DEFSYMEXP>"-" { RTOKEN('-');}
133 <DEFSYMEXP>"+" { RTOKEN('+');}
134 <DEFSYMEXP>{FILENAMECHAR1}{SYMBOLCHARN}* { yylval.name = buystring(yytext); return NAME; }
135 <DEFSYMEXP>"=" { RTOKEN('='); }
137 <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
138 yylval.integer = bfd_scan_vma (yytext+1, 0,16);
142 <MRI,EXPRESSION>([0-9A-Fa-f])+(H|X|B|O|D) {
144 switch (yytext[yyleng-1]) {
158 yylval.integer = bfd_scan_vma (yytext+1, 0,
162 <SCRIPT,DEFSYMEXP,MRI,BOTH,EXPRESSION>"$"?"0x"?([0-9A-Fa-f])+(M|K|m|k)? {
163 yylval.integer = bfd_scan_vma (yytext, 0,
165 if (yytext[yyleng-1]=='M'
166 || yytext[yyleng-1] == 'm') {
167 yylval.integer *= 1024*1024;
169 if (yytext[yyleng-1]=='K'
170 || yytext[yyleng-1]=='k') {
171 yylval.integer *= 1024;
175 <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');}
176 <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');}
177 <BOTH,SCRIPT,EXPRESSION>"<<=" { RTOKEN(LSHIFTEQ);}
178 <BOTH,SCRIPT,EXPRESSION>">>=" { RTOKEN(RSHIFTEQ);}
179 <BOTH,SCRIPT,EXPRESSION>"||" { RTOKEN(OROR);}
180 <BOTH,SCRIPT,EXPRESSION>"==" { RTOKEN(EQ);}
181 <BOTH,SCRIPT,EXPRESSION>"!=" { RTOKEN(NE);}
182 <BOTH,SCRIPT,EXPRESSION>">=" { RTOKEN(GE);}
183 <BOTH,SCRIPT,EXPRESSION>"<=" { RTOKEN(LE);}
184 <BOTH,SCRIPT,EXPRESSION>"<<" { RTOKEN(LSHIFT);}
185 <BOTH,SCRIPT,EXPRESSION>">>" { RTOKEN(RSHIFT);}
186 <BOTH,SCRIPT,EXPRESSION>"+=" { RTOKEN(PLUSEQ);}
187 <BOTH,SCRIPT,EXPRESSION>"-=" { RTOKEN(MINUSEQ);}
188 <BOTH,SCRIPT,EXPRESSION>"*=" { RTOKEN(MULTEQ);}
189 <BOTH,SCRIPT,EXPRESSION>"/=" { RTOKEN(DIVEQ);}
190 <BOTH,SCRIPT,EXPRESSION>"&=" { RTOKEN(ANDEQ);}
191 <BOTH,SCRIPT,EXPRESSION>"|=" { RTOKEN(OREQ);}
192 <BOTH,SCRIPT,EXPRESSION>"&&" { RTOKEN(ANDAND);}
193 <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');}
194 <MRI,BOTH,SCRIPT,EXPRESSION>"," { RTOKEN(',');}
195 <BOTH,SCRIPT,EXPRESSION>"&" { RTOKEN('&');}
196 <BOTH,SCRIPT,EXPRESSION>"|" { RTOKEN('|');}
197 <BOTH,SCRIPT,EXPRESSION>"~" { RTOKEN('~');}
198 <BOTH,SCRIPT,EXPRESSION>"!" { RTOKEN('!');}
199 <BOTH,SCRIPT,EXPRESSION>"?" { RTOKEN('?');}
200 <BOTH,SCRIPT,EXPRESSION>"*" { RTOKEN('*');}
201 <BOTH,SCRIPT,EXPRESSION>"+" { RTOKEN('+');}
202 <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');}
203 <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');}
204 <BOTH,SCRIPT,EXPRESSION>"%" { RTOKEN('%');}
205 <BOTH,SCRIPT,EXPRESSION>"<" { RTOKEN('<');}
206 <MRI,BOTH,SCRIPT,EXPRESSION>"=" { RTOKEN('=');}
207 <BOTH,SCRIPT,EXPRESSION>"}" { RTOKEN('}') ; }
208 <BOTH,SCRIPT,EXPRESSION>"{" { RTOKEN('{'); }
209 <BOTH,SCRIPT,EXPRESSION>")" { RTOKEN(')');}
210 <BOTH,SCRIPT,EXPRESSION>"(" { RTOKEN('(');}
211 <BOTH,SCRIPT,EXPRESSION>":" { RTOKEN(':'); }
212 <BOTH,SCRIPT,EXPRESSION>";" { RTOKEN(';');}
213 <BOTH,SCRIPT>"MEMORY" { RTOKEN(MEMORY);}
214 <BOTH,SCRIPT>"ORIGIN" { RTOKEN(ORIGIN);}
215 <BOTH,SCRIPT>"BLOCK" { RTOKEN(BLOCK);}
216 <BOTH,SCRIPT>"LENGTH" { RTOKEN(LENGTH);}
217 <EXPRESSION,BOTH,SCRIPT>"ALIGN" { RTOKEN(ALIGN_K);}
218 <EXPRESSION,BOTH,SCRIPT>"ADDR" { RTOKEN(ADDR);}
219 <BOTH,SCRIPT>"ENTRY" { RTOKEN(ENTRY);}
220 <EXPRESSION,BOTH,SCRIPT>"NEXT" { RTOKEN(NEXT);}
221 <EXPRESSION,BOTH,SCRIPT>"sizeof_headers" { RTOKEN(SIZEOF_HEADERS);}
222 <EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS" { RTOKEN(SIZEOF_HEADERS);}
223 <BOTH,SCRIPT>"MAP" { RTOKEN(MAP);}
224 <EXPRESSION,BOTH,SCRIPT>"SIZEOF" { RTOKEN(SIZEOF);}
225 <BOTH,SCRIPT>"TARGET" { RTOKEN(TARGET_K);}
226 <BOTH,SCRIPT>"SEARCH_DIR" { RTOKEN(SEARCH_DIR);}
227 <BOTH,SCRIPT>"OUTPUT" { RTOKEN(OUTPUT);}
228 <BOTH,SCRIPT>"INPUT" { RTOKEN(INPUT);}
229 <EXPRESSION,BOTH,SCRIPT>"DEFINED" { RTOKEN(DEFINED);}
230 <BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS" { RTOKEN(CREATE_OBJECT_SYMBOLS);}
231 <BOTH,SCRIPT>"CONSTRUCTORS" { RTOKEN( CONSTRUCTORS);}
232 <BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION);}
233 <BOTH,SCRIPT>"SECTIONS" { RTOKEN(SECTIONS);}
234 <BOTH,SCRIPT>"FILL" { RTOKEN(FILL);}
235 <BOTH,SCRIPT>"STARTUP" { RTOKEN(STARTUP);}
236 <BOTH,SCRIPT>"OUTPUT_FORMAT" { RTOKEN(OUTPUT_FORMAT);}
237 <BOTH,SCRIPT>"OUTPUT_ARCH" { RTOKEN( OUTPUT_ARCH);}
238 <BOTH,SCRIPT>"HLL" { RTOKEN(HLL);}
239 <BOTH,SCRIPT>"SYSLIB" { RTOKEN(SYSLIB);}
240 <BOTH,SCRIPT>"FLOAT" { RTOKEN(FLOAT);}
241 <BOTH,SCRIPT>"QUAD" { RTOKEN( QUAD);}
242 <BOTH,SCRIPT>"LONG" { RTOKEN( LONG);}
243 <BOTH,SCRIPT>"SHORT" { RTOKEN( SHORT);}
244 <BOTH,SCRIPT>"BYTE" { RTOKEN( BYTE);}
245 <BOTH,SCRIPT>"NOFLOAT" { RTOKEN(NOFLOAT);}
246 <EXPRESSION,BOTH,SCRIPT>"NOLOAD" { RTOKEN(NOLOAD);}
247 <BOTH,SCRIPT>"DSECT" { RTOKEN(DSECT);}
248 <BOTH,SCRIPT>"COPY" { RTOKEN(COPY);}
249 <BOTH,SCRIPT>"INFO" { RTOKEN(INFO);}
250 <BOTH,SCRIPT>"OVERLAY" { RTOKEN(OVERLAY);}
251 <BOTH,SCRIPT>"o" { RTOKEN(ORIGIN);}
252 <BOTH,SCRIPT>"org" { RTOKEN(ORIGIN);}
253 <BOTH,SCRIPT>"l" { RTOKEN( LENGTH);}
254 <BOTH,SCRIPT>"len" { RTOKEN( LENGTH);}
255 <BOTH,SCRIPT>"INCLUDE" { RTOKEN(INCLUDE);}
256 <EXPRESSION,BOTH,SCRIPT>"AT" { RTOKEN(AT);}
257 <BOTH,SCRIPT>"PROVIDE" { RTOKEN(PROVIDE); }
258 <MRI>"\n" { ++ lineno; RTOKEN(NEWLINE); }
259 <MRI>"\r" { ++ lineno; RTOKEN(NEWLINE); }
260 <MRI>"*".* { /* Mri comment line */ }
261 <MRI>"END" { RTOKEN(ENDWORD); }
262 <MRI>"ALIGNMOD" { RTOKEN(ALIGNMOD);}
263 <MRI>"ALIGN" { RTOKEN(ALIGN_K);}
265 <MRI>"CHIP" { RTOKEN(CHIP); }
266 <MRI>"BASE" { RTOKEN(BASE); }
267 <MRI>"ALIAS" { RTOKEN(ALIAS); }
268 <MRI>"TRUNCATE" { RTOKEN(TRUNCATE); }
269 <MRI>"LOAD" { RTOKEN(LOAD); }
270 <MRI>"PUBLIC" { RTOKEN(PUBLIC); }
271 <MRI>"ORDER" { RTOKEN(ORDER); }
272 <MRI>"NAME" { RTOKEN(NAMEWORD); }
273 <MRI>"FORMAT" { RTOKEN(FORMAT); }
274 <MRI>"LIST".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
275 <MRI>"SECT" { RTOKEN(SECT); }
276 <EXPRESSION,BOTH,SCRIPT,MRI>"ABSOLUTE" { RTOKEN(ABSOLUTE); }
277 <MRI>"end" { RTOKEN(ENDWORD); }
278 <MRI>"chip" { RTOKEN(CHIP); }
279 <MRI>"load" { RTOKEN(LOAD); }
280 <MRI>"order" { RTOKEN(ORDER); }
281 <MRI>"name" { RTOKEN(NAMEWORD); }
282 <MRI>"format" { RTOKEN(FORMAT); }
283 <MRI>"list".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
284 <MRI>"sect" { RTOKEN(SECT); }
285 <EXPRESSION,BOTH,SCRIPT,MRI>"absolute" { RTOKEN(ABSOLUTE); }
287 <MRI>{FILENAMECHAR1}{NOCFILENAMECHAR}* {
288 /* Filename without commas, needed to parse mri stuff */
289 yylval.name = buystring(yytext);
294 <BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}* {
295 yylval.name = buystring(yytext);
298 <SCRIPT>{FILENAMECHAR}* { yylval.name = buystring(yytext);
302 <EXPRESSION,BOTH,SCRIPT>"\""[^\"]*"\"" {
303 /* No matter the state, quotes
304 give what's inside */
305 yylval.name = buystring(yytext+1);
306 yylval.name[yyleng-2] = 0;
309 <BOTH,SCRIPT,EXPRESSION>"\n" { lineno++;}
310 <BOTH,SCRIPT,EXPRESSION>"\r" { lineno++;}
311 <MRI,BOTH,SCRIPT,EXPRESSION>[ \t]
316 if (include_stack_ptr == 0)
322 yy_switch_to_buffer(include_stack[include_stack_ptr]);
326 ldfile_input_filename = file_name_stack[include_stack_ptr-1];
331 <SCRIPT,MRI>. lex_warn_invalid(" in script", yytext);
332 <EXPRESSION,DEFSYMEXP,BOTH>. lex_warn_invalid(" in expression", yytext);
337 /* Switch flex to reading script file NAME, open on FILE,
338 saving the current input info on the include stack. */
341 lex_push_file (file, name)
345 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
347 einfo("%F:includes nested too deeply\n");
349 file_name_stack[include_stack_ptr] = name;
350 include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
354 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
358 /* Return a newly created flex input buffer containing STRING,
359 which is SIZE bytes long. */
361 static YY_BUFFER_STATE
362 yy_create_string_buffer (string, size)
368 /* Calls to m-alloc get turned by sed into xm-alloc. */
369 b = (YY_BUFFER_STATE) malloc (sizeof (struct yy_buffer_state));
370 b->yy_input_file = 0;
371 b->yy_buf_size = size;
373 /* yy_ch_buf has to be 2 characters longer than the size given because
374 we need to put in 2 end-of-buffer characters. */
375 b->yy_ch_buf = (YY_CHAR *) malloc ((unsigned) (b->yy_buf_size + 3));
377 b->yy_ch_buf[0] = '\n';
378 strcpy (b->yy_ch_buf+1, string);
379 b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
380 b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
381 b->yy_n_chars = size+1;
382 b->yy_buf_pos = &b->yy_ch_buf[1];
383 b->yy_eof_status = EOF_NOT_SEEN;
388 /* Switch flex to reading from STRING, saving the current input info
389 on the include stack. */
392 lex_redirect (string)
398 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
400 einfo("%F: macros nested too deeply\n");
402 file_name_stack[include_stack_ptr] = "redirect";
403 include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
405 tmp = yy_create_string_buffer (string, strlen (string));
406 yy_switch_to_buffer (tmp);
410 /* Functions to switch to a different flex start condition,
411 saving the current start condition on `state_stack'. */
413 static int state_stack[MAX_INCLUDE_DEPTH * 2];
414 static int *state_stack_p = state_stack;
419 *(state_stack_p)++ = yy_start;
426 *(state_stack_p)++ = yy_start;
433 *(state_stack_p)++ = yy_start;
440 *(state_stack_p)++ = yy_start;
447 *(state_stack_p)++ = yy_start;
454 yy_start = *(--state_stack_p);
458 /* Place up to MAX_SIZE characters in BUF and return in *RESULT
459 either the number of characters read, or 0 to indicate EOF. */
462 yy_input (buf, result, max_size)
468 if (yy_current_buffer->yy_input_file)
472 *result = read (fileno (yyin), (char *) buf, max_size);
474 einfo ("%F%P: read in flex scanner failed");
479 /* Eat the rest of a C-style comment. */
489 while (c != '*' && c != EOF)
491 if (c == '\n' || c == '\r')
502 break; /* found the end */
505 if (c == '\n' || c == '\r')
510 einfo( "%F%P: EOF in comment\n");
516 /* Warn the user about a garbage character WHAT in the input
520 lex_warn_invalid (where, what)
524 "%s: ignoring invalid character `%s'%s\n",
525 program_name, what, where);