3 /* Copyright (C) 1991 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
32 int ldgram_had_equals;
43 unsigned int lineno = 1;
52 while (c !='*' && c != EOF)
54 if (c == '\n') lineno++;
65 break; /* found the end */
70 einfo( "%F%P :EOF in comment");
78 #define YY_INPUT(buf,result,max_size) yy_input(buf, &result, max_size)
80 #define YY_FATAL_ERROR ;
81 #define MAX_INCLUDE_DEPTH 10
82 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
83 unsigned int include_stack_ptr = 0;
87 COMMAND on command line
88 COMMENT in a C comment
89 EXPRESSION definiatelyt in an expression
90 SCRIPT definately in a script
91 SOMEWHERE either EXPRESSION or SCRIPT
94 #define RTOKEN(x) { yylval.token = x; return x; }
100 CMDFILENAMECHAR [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\-\~]
101 CMDFILENAMECHAR1 [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\~]
102 FILENAMECHAR1 [_a-zA-Z\/\.\\\$\_\~]
103 SYMBOLCHARN [_a-zA-Z\/\.\\0-9]
104 FILENAMECHAR [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\\,\~]
105 FILENAME {FILENAMECHAR}+
118 <BOTH,SCRIPT,EXPRESSION,COMMAND>"/*" { comment(); }
121 <DEFSYMEXP>"-" { RTOKEN('-');}
122 <DEFSYMEXP>"+" { RTOKEN('+');}
123 <DEFSYMEXP>{FILENAMECHAR1}{SYMBOLCHARN}* { yylval.name = buystring(yytext); return NAME; }
124 <DEFSYMEXP>[ \t] { RTOKEN(DEFSYMEND); }
125 <DEFSYMEXP>"=" { RTOKEN('='); }
126 <COMMAND>"-defsym"{WHITE}* { return OPTION_defsym; }
127 <COMMAND>"-noinhibit_exec" { return OPTION_noinhibit_exec; }
128 <COMMAND>"-noinhibit-exec" { return OPTION_noinhibit_exec; }
129 <COMMAND>"-sort_common" { return OPTION_sort_common;}
130 <COMMAND>"-sort-common" { return OPTION_sort_common;}
131 <COMMAND>"-format" { return OPTION_format; }
132 <COMMAND>"-n" { return OPTION_n; }
133 <COMMAND>"-N" { return OPTION_N; }
134 <COMMAND>"-r" { return OPTION_r; }
135 <COMMAND>"-relax" { return OPTION_relax; }
136 <COMMAND>"-i" { return OPTION_r; }
137 <COMMAND>"-Ur" { return OPTION_Ur; }
138 <COMMAND>"-o" { return OPTION_o; }
139 <COMMAND>"-g" { return OPTION_g; }
140 <COMMAND>"-e" { return OPTION_e; }
141 <COMMAND>"-b" { return OPTION_b; }
142 <COMMAND>"-dc" { return OPTION_dc; }
143 <COMMAND>"-dp" { return OPTION_dp; }
144 <COMMAND>"-d" { return OPTION_d; }
145 <COMMAND>"-v" { return OPTION_v; }
146 <COMMAND>"-V" { return OPTION_V; }
147 <COMMAND>"-M" { return OPTION_M; }
148 <COMMAND>"-Map" { return OPTION_Map;}
149 <COMMAND>"-t" { return OPTION_t; }
150 <COMMAND>"-X" { return OPTION_X; }
151 <COMMAND>"-x" { return OPTION_x; }
152 <COMMAND>"-c" { return OPTION_c; }
153 <COMMAND>"-R" { return OPTION_R; }
154 <COMMAND>"-u" { return OPTION_u; }
155 <COMMAND>"-s" { return OPTION_s; }
156 <COMMAND>"-S" { return OPTION_S; }
157 <COMMAND>"-Bstat" { return OPTION_Bstatic; }
158 <COMMAND>"-B"{FILENAME} { /* Ignored */ }
159 <COMMAND>"-l"{FILENAME} {
160 yylval.name = buystring(yytext+2);
164 <COMMAND>"-L"{FILENAME} {
165 yylval.name = buystring(yytext+2);
169 yylval.name = ".text";
173 yylval.name = ".data";
177 yylval.name = ".bss";
180 <COMMAND>"-O"{FILENAME} {
181 yylval.name = buystring(yytext+2);
185 <COMMAND>"-T"{FILENAME} {
186 yylval.name = buystring(yytext+2);
193 <COMMAND>"-F"{FILENAME} {
200 <COMMAND>"-A"{FILENAME} {
201 yylval.name = buystring(yytext+2);
205 <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
206 yylval.integer = strtoul(yytext+1, 0,16);
210 <MRI,EXPRESSION>([0-9A-Fa-f])+(H|X|B|O|D) {
212 switch (yytext[yyleng-1]) {
226 yylval.integer = strtoul(yytext+1, 0, base);
229 <DEFSYMEXP,MRI,BOTH,EXPRESSION>"$"?"0x"?([0-9A-Fa-f])+(M|K|m|k)? {
230 yylval.integer = strtoul(yytext,0,hex_mode);
231 if (yytext[yyleng-1]=='M'
232 || yytext[yyleng-1] == 'm') {
233 yylval.integer *= 1024*1024;
235 if (yytext[yyleng-1]=='K'
236 || yytext[yyleng-1]=='k') {
237 yylval.integer *= 1024;
241 <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');}
242 <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');}
243 <BOTH,SCRIPT,EXPRESSION>"<<=" { RTOKEN(LSHIFTEQ);}
244 <BOTH,SCRIPT,EXPRESSION>">>=" { RTOKEN(RSHIFTEQ);}
245 <BOTH,SCRIPT,EXPRESSION>"||" { RTOKEN(OROR);}
246 <BOTH,SCRIPT,EXPRESSION>"==" { RTOKEN(EQ);}
247 <BOTH,SCRIPT,EXPRESSION>"!=" { RTOKEN(NE);}
248 <BOTH,SCRIPT,EXPRESSION>">=" { RTOKEN(GE);}
249 <BOTH,SCRIPT,EXPRESSION>"<=" { RTOKEN(LE);}
250 <BOTH,SCRIPT,EXPRESSION>"<<" { RTOKEN(LSHIFT);}
251 <BOTH,SCRIPT,EXPRESSION>">>" { RTOKEN(RSHIFT);}
252 <BOTH,SCRIPT,EXPRESSION>"+=" { RTOKEN(PLUSEQ);}
253 <BOTH,SCRIPT,EXPRESSION>"-=" { RTOKEN(MINUSEQ);}
254 <BOTH,SCRIPT,EXPRESSION>"*=" { RTOKEN(MULTEQ);}
255 <BOTH,SCRIPT,EXPRESSION>"/=" { RTOKEN(DIVEQ);}
256 <BOTH,SCRIPT,EXPRESSION>"&=" { RTOKEN(ANDEQ);}
257 <BOTH,SCRIPT,EXPRESSION>"|=" { RTOKEN(OREQ);}
258 <BOTH,SCRIPT,EXPRESSION>"&&" { RTOKEN(ANDAND);}
259 <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');}
260 <MRI,BOTH,SCRIPT,EXPRESSION>"," { RTOKEN(',');}
261 <BOTH,SCRIPT,EXPRESSION>"&" { RTOKEN('&');}
262 <BOTH,SCRIPT,EXPRESSION>"|" { RTOKEN('|');}
263 <BOTH,SCRIPT,EXPRESSION>"~" { RTOKEN('~');}
264 <BOTH,SCRIPT,EXPRESSION>"!" { RTOKEN('!');}
265 <BOTH,SCRIPT,EXPRESSION>"?" { RTOKEN('?');}
266 <BOTH,SCRIPT,EXPRESSION>"*" { RTOKEN('*');}
267 <BOTH,SCRIPT,EXPRESSION>"+" { RTOKEN('+');}
268 <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');}
269 <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');}
270 <BOTH,SCRIPT,EXPRESSION>"%" { RTOKEN('%');}
271 <BOTH,SCRIPT,EXPRESSION>"<" { RTOKEN('<');}
272 <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');}
273 <MRI,BOTH,SCRIPT,EXPRESSION>"=" { RTOKEN('=');}
274 <BOTH,SCRIPT,EXPRESSION>"}" { RTOKEN('}') ; }
275 <BOTH,SCRIPT,EXPRESSION>"{" { RTOKEN('{'); }
276 <BOTH,SCRIPT,EXPRESSION>")" { RTOKEN(')');}
277 <BOTH,SCRIPT,EXPRESSION>"(" { RTOKEN('(');}
278 <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');}
279 <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');}
280 <BOTH,SCRIPT,EXPRESSION>":" { RTOKEN(':'); }
281 <BOTH,SCRIPT,EXPRESSION>";" { RTOKEN(';');}
282 <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');}
283 <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');}
284 <BOTH,SCRIPT>"MEMORY" { RTOKEN(MEMORY);}
285 <BOTH,SCRIPT>"ORIGIN" { RTOKEN(ORIGIN);}
286 <BOTH,SCRIPT>"BLOCK" { RTOKEN(BLOCK);}
287 <BOTH,SCRIPT>"LENGTH" { RTOKEN(LENGTH);}
288 <EXPRESSION,BOTH,SCRIPT>"ALIGN" { RTOKEN(ALIGN_K);}
289 <EXPRESSION,BOTH,SCRIPT>"ADDR" { RTOKEN(ADDR);}
290 <BOTH,SCRIPT>"ENTRY" { RTOKEN(ENTRY);}
291 <EXPRESSION,BOTH,SCRIPT>"NEXT" { RTOKEN(NEXT);}
292 <EXPRESSION,BOTH,SCRIPT>"sizeof_headers" { RTOKEN(SIZEOF_HEADERS);}
293 <EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS" { RTOKEN(SIZEOF_HEADERS);}
294 <BOTH,SCRIPT>"MAP" { RTOKEN(MAP);}
295 <EXPRESSION,BOTH,SCRIPT>"SIZEOF" { RTOKEN(SIZEOF);}
296 <BOTH,SCRIPT>"TARGET" { RTOKEN(TARGET_K);}
297 <BOTH,SCRIPT>"SEARCH_DIR" { RTOKEN(SEARCH_DIR);}
298 <BOTH,SCRIPT>"OUTPUT" { RTOKEN(OUTPUT);}
299 <BOTH,SCRIPT>"INPUT" { RTOKEN(INPUT);}
300 <BOTH,SCRIPT>"DEFINED" { RTOKEN(DEFINED);}
301 <BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS" { RTOKEN(CREATE_OBJECT_SYMBOLS);}
302 <BOTH,SCRIPT>"CONSTRUCTORS" { RTOKEN( CONSTRUCTORS);}
303 <BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION);}
304 <BOTH,SCRIPT>"SECTIONS" { RTOKEN(SECTIONS);}
305 <BOTH,SCRIPT>"FILL" { RTOKEN(FILL);}
306 <BOTH,SCRIPT>"STARTUP" { RTOKEN(STARTUP);}
307 <BOTH,SCRIPT>"OUTPUT_FORMAT" { RTOKEN(OUTPUT_FORMAT);}
308 <BOTH,SCRIPT>"OUTPUT_ARCH" { RTOKEN( OUTPUT_ARCH);}
309 <BOTH,SCRIPT>"HLL" { RTOKEN(HLL);}
310 <BOTH,SCRIPT>"SYSLIB" { RTOKEN(SYSLIB);}
311 <BOTH,SCRIPT>"FLOAT" { RTOKEN(FLOAT);}
312 <BOTH,SCRIPT>"LONG" { RTOKEN( LONG);}
313 <BOTH,SCRIPT>"SHORT" { RTOKEN( SHORT);}
314 <BOTH,SCRIPT>"BYTE" { RTOKEN( BYTE);}
315 <BOTH,SCRIPT>"NOFLOAT" { RTOKEN(NOFLOAT);}
316 <BOTH,SCRIPT>"NOLOAD" { RTOKEN(NOLOAD);}
317 <BOTH,SCRIPT>"DSECT" { RTOKEN(DSECT);}
318 <BOTH,SCRIPT>"COPY" { RTOKEN(COPY);}
319 <BOTH,SCRIPT>"INFO" { RTOKEN(INFO);}
320 <BOTH,SCRIPT>"OVERLAY" { RTOKEN(OVERLAY);}
321 <BOTH,SCRIPT>"o" { RTOKEN(ORIGIN);}
322 <BOTH,SCRIPT>"org" { RTOKEN(ORIGIN);}
323 <BOTH,SCRIPT>"l" { RTOKEN( LENGTH);}
324 <BOTH,SCRIPT>"len" { RTOKEN( LENGTH);}
325 <MRI>"\n" { ++ lineno; RTOKEN(NEWLINE); }
326 <MRI>"*".* { /* Mri comment line */ }
327 <MRI>"END" { RTOKEN(ENDWORD); }
328 <MRI>"CHIP" { RTOKEN(CHIP); }
329 <MRI>"BASE" { RTOKEN(BASE); }
330 <MRI>"ALIAS" { RTOKEN(ALIAS); }
331 <MRI>"LOAD" { RTOKEN(LOAD); }
332 <MRI>"PUBLIC" { RTOKEN(PUBLIC); }
333 <MRI>"ORDER" { RTOKEN(ORDER); }
334 <MRI>"NAME" { RTOKEN(NAMEWORD); }
335 <MRI>"FORMAT" { RTOKEN(FORMAT); }
336 <MRI>"LIST".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
337 <MRI>"SECT" { RTOKEN(SECT); }
338 <EXPRESSION,BOTH,SCRIPT,MRI>"ABSOLUTE" { RTOKEN(ABSOLUTE); }
339 <MRI>"end" { RTOKEN(ENDWORD); }
340 <MRI>"chip" { RTOKEN(CHIP); }
341 <MRI>"load" { RTOKEN(LOAD); }
342 <MRI>"order" { RTOKEN(ORDER); }
343 <MRI>"name" { RTOKEN(NAMEWORD); }
344 <MRI>"format" { RTOKEN(FORMAT); }
345 <MRI>"list".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
346 <MRI>"sect" { RTOKEN(SECT); }
347 <EXPRESSION,BOTH,SCRIPT,MRI>"absolute" { RTOKEN(ABSOLUTE); }
349 <COMMAND>{CMDFILENAMECHAR1}{CMDFILENAMECHAR}* {
350 yylval.name = buystring(yytext);
355 <MRI,BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}* {
356 yylval.name = buystring(yytext);
359 <SCRIPT,COMMAND>{FILENAMECHAR}* { yylval.name = buystring(yytext);
363 <EXPRESSION,BOTH,COMMAND,SCRIPT>"\""[^\"]*"\"" {
364 /* No matter the state, quotes
365 give what's inside */
366 yylval.name = buystring(yytext+1);
367 yylval.name[yyleng-2] = 0;
370 <BOTH,SCRIPT,EXPRESSION>"\n" { lineno++;}
371 <MRI,COMMAND,BOTH,SCRIPT,EXPRESSION>[ \t]
376 if (include_stack_ptr == 0)
382 yy_switch_to_buffer(include_stack[include_stack_ptr]);
391 DEFUN(lex_push_file,(file),
394 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
396 einfo("%F:includes nested too deeply");
398 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
400 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
406 DEFUN(yy_create_string_buffer,(string, size),
407 CONST char *string AND
412 b = (YY_BUFFER_STATE) malloc( sizeof( struct yy_buffer_state ) );
413 b->yy_input_file = 0;
416 YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
418 b->yy_buf_size = size;
420 /* yy_ch_buf has to be 2 characters longer than the size given because
421 * we need to put in 2 end-of-buffer characters.
423 b->yy_ch_buf = (YY_CHAR *) malloc( (unsigned) (b->yy_buf_size + 3) );
425 if ( ! b->yy_ch_buf )
426 YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
429 b->yy_ch_buf[0] = '\n';
430 strcpy(b->yy_ch_buf+1, string);
431 b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
432 b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
433 b->yy_n_chars = size+1;
434 b->yy_buf_pos = &b->yy_ch_buf[1];
436 b->yy_eof_status = EOF_NOT_SEEN;
444 DEFUN(lex_redirect,( string),
449 int len = strlen(string);
451 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
453 einfo("%F: macros nested too deeply");
455 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
457 tmp = yy_create_string_buffer(string, len);
459 yy_switch_to_buffer(tmp);
465 int *state_stack_p = state_stack;
468 DEFUN_VOID(ldlex_script)
470 *(state_stack_p)++ = yy_start;
477 DEFUN_VOID(ldlex_mri_script)
479 *(state_stack_p)++ = yy_start;
484 DEFUN_VOID(ldlex_defsym)
486 *(state_stack_p)++ = yy_start;
491 DEFUN_VOID(ldlex_expression)
493 *(state_stack_p)++ = yy_start;
498 DEFUN_VOID(ldlex_both)
500 *(state_stack_p)++ = yy_start;
504 DEFUN_VOID(ldlex_command)
506 *(state_stack_p)++ = yy_start;
511 DEFUN_VOID(ldlex_popstate)
513 yy_start = *(--state_stack_p);
516 yy_input(buf, result, max_size)
522 if (yy_current_buffer->yy_input_file)
525 if ( (*result = read( fileno(yyin), (char *) buf, max_size )) < 0 )
526 YY_FATAL_ERROR( "read() in flex scanner failed" );