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;
47 #define YY_INPUT(buf,result,max_size) yy_input(buf, &result, max_size)
49 #define YY_FATAL_ERROR ;
50 #define MAX_INCLUDE_DEPTH 10
51 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
52 unsigned int include_stack_ptr = 0;
56 COMMAND on command line
57 COMMENT in a C comment
58 EXPRESSION definiatelyt in an expression
59 SCRIPT definately in a script
60 SOMEWHERE either EXPRESSION or SCRIPT
63 #define RTOKEN(x) { yylval.token = x; return x; }
68 FILENAMECHAR1 [_a-zA-Z\/\.\\]
69 SYMBOLCHARN [_a-zA-Z\/\.\\0-9]
70 FILENAMECHAR [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\]
71 FILENAME {FILENAMECHAR}+
83 <DEFSYMEXP>"-" { RTOKEN('-');}
84 <DEFSYMEXP>"+" { RTOKEN('+');}
85 <DEFSYMEXP>{FILENAMECHAR1}{SYMBOLCHARN}* { yylval.name = buystring(yytext); return NAME; }
86 <DEFSYMEXP>[ \t] { RTOKEN(DEFSYMEND); }
87 <DEFSYMEXP>"=" { RTOKEN('='); }
88 <COMMAND>"-defsym"{WHITE}* { return OPTION_defsym; }
89 <COMMAND>"-noinhibit_exec" { return OPTION_noinhibit_exec; }
90 <COMMAND>"-noinhibit-exec" { return OPTION_noinhibit_exec; }
91 <COMMAND>"-sort_common" { return OPTION_sort_common;}
92 <COMMAND>"-sort-common" { return OPTION_sort_common;}
93 <COMMAND>"-format" { return OPTION_format; }
94 <COMMAND>"-n" { return OPTION_n; }
95 <COMMAND>"-N" { return OPTION_N; }
96 <COMMAND>"-r" { return OPTION_r; }
97 <COMMAND>"-relax" { return OPTION_relax; }
98 <COMMAND>"-i" { return OPTION_r; }
99 <COMMAND>"-Ur" { return OPTION_Ur; }
100 <COMMAND>"-o" { return OPTION_o; }
101 <COMMAND>"-g" { return OPTION_g; }
102 <COMMAND>"-e" { return OPTION_e; }
103 <COMMAND>"-b" { return OPTION_b; }
104 <COMMAND>"-dc" { return OPTION_dc; }
105 <COMMAND>"-dp" { return OPTION_dp; }
106 <COMMAND>"-d" { return OPTION_d; }
107 <COMMAND>"-v" { return OPTION_v; }
108 <COMMAND>"-V" { return OPTION_V; }
109 <COMMAND>"-M" { return OPTION_M; }
110 <COMMAND>"-Map" { return OPTION_Map;}
111 <COMMAND>"-t" { return OPTION_t; }
112 <COMMAND>"-X" { return OPTION_X; }
113 <COMMAND>"-x" { return OPTION_x; }
114 <COMMAND>"-c" { return OPTION_c; }
115 <COMMAND>"-R" { return OPTION_R; }
116 <COMMAND>"-u" { return OPTION_u; }
117 <COMMAND>"-s" { return OPTION_s; }
118 <COMMAND>"-S" { return OPTION_S; }
119 <COMMAND>"-Bstat" { return OPTION_Bstatic; }
120 <COMMAND>"-B"{FILENAME} { /* Ignored */ }
121 <COMMAND>"-l"{FILENAME} {
122 yylval.name = buystring(yytext+2);
126 <COMMAND>"-L"{FILENAME} {
127 yylval.name = buystring(yytext+2);
131 yylval.name = ".text";
135 yylval.name = ".data";
139 yylval.name = ".bss";
142 <COMMAND>"-O"{FILENAME} {
143 yylval.name = buystring(yytext+2);
147 <COMMAND>"-T"{FILENAME} {
148 yylval.name = buystring(yytext+2);
155 <COMMAND>"-F"{FILENAME} {
162 <COMMAND>"-A"{FILENAME} {
163 yylval.name = buystring(yytext+2);
167 <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
168 yylval.integer = strtol(yytext+1, 0,16);
172 <MRI,EXPRESSION>([0-9A-Fa-f])+(H|X|B|O|D)
175 switch (yytext[yyleng-1]) {
189 yylval.integer = strtol(yytext+1, 0, base);
192 <MRI,BOTH,EXPRESSION>"$"?"0x"?([0-9A-Fa-f])+(M|K|m|k)? {
193 yylval.integer = strtol(yytext,0,hex_mode);
194 if (yytext[yyleng-1]=='M'
195 || yytext[yyleng-1] == 'm') {
196 yylval.integer *= 1024*1024;
198 if (yytext[yyleng-1]=='K'
199 || yytext[yyleng-1]=='k') {
200 yylval.integer *= 1024;
204 <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');}
205 <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');}
206 <BOTH,SCRIPT,EXPRESSION>"<<=" { RTOKEN(LSHIFTEQ);}
207 <BOTH,SCRIPT,EXPRESSION>">>=" { RTOKEN(RSHIFTEQ);}
208 <BOTH,SCRIPT,EXPRESSION>"||" { RTOKEN(OROR);}
209 <BOTH,SCRIPT,EXPRESSION>"==" { RTOKEN(EQ);}
210 <BOTH,SCRIPT,EXPRESSION>"!=" { RTOKEN(NE);}
211 <BOTH,SCRIPT,EXPRESSION>">=" { RTOKEN(GE);}
212 <BOTH,SCRIPT,EXPRESSION>"<=" { RTOKEN(LE);}
213 <BOTH,SCRIPT,EXPRESSION>"<<" { RTOKEN(LSHIFT);}
214 <BOTH,SCRIPT,EXPRESSION>">>" { RTOKEN(RSHIFT);}
215 <BOTH,SCRIPT,EXPRESSION>"+=" { RTOKEN(PLUSEQ);}
216 <BOTH,SCRIPT,EXPRESSION>"-=" { RTOKEN(MINUSEQ);}
217 <BOTH,SCRIPT,EXPRESSION>"*=" { RTOKEN(MULTEQ);}
218 <BOTH,SCRIPT,EXPRESSION>"/=" { RTOKEN(DIVEQ);}
219 <BOTH,SCRIPT,EXPRESSION>"&=" { RTOKEN(ANDEQ);}
220 <BOTH,SCRIPT,EXPRESSION>"|=" { RTOKEN(OREQ);}
221 <BOTH,SCRIPT,EXPRESSION>"&&" { RTOKEN(ANDAND);}
222 <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');}
223 <MRI,BOTH,SCRIPT,EXPRESSION>"," { RTOKEN(',');}
224 <BOTH,SCRIPT,EXPRESSION>"&" { RTOKEN('&');}
225 <BOTH,SCRIPT,EXPRESSION>"|" { RTOKEN('|');}
226 <BOTH,SCRIPT,EXPRESSION>"~" { RTOKEN('~');}
227 <BOTH,SCRIPT,EXPRESSION>"!" { RTOKEN('!');}
228 <BOTH,SCRIPT,EXPRESSION>"?" { RTOKEN('?');}
229 <BOTH,SCRIPT,EXPRESSION>"*" { RTOKEN('*');}
230 <BOTH,SCRIPT,EXPRESSION>"+" { RTOKEN('+');}
231 <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');}
232 <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');}
233 <BOTH,SCRIPT,EXPRESSION>"%" { RTOKEN('%');}
234 <BOTH,SCRIPT,EXPRESSION>"<" { RTOKEN('<');}
235 <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');}
236 <MRI,BOTH,SCRIPT,EXPRESSION>"=" { RTOKEN('=');}
237 <BOTH,SCRIPT,EXPRESSION>"}" { RTOKEN('}') ; }
238 <BOTH,SCRIPT,EXPRESSION>"{" { RTOKEN('{'); }
239 <BOTH,SCRIPT,EXPRESSION>")" { RTOKEN(')');}
240 <BOTH,SCRIPT,EXPRESSION>"(" { RTOKEN('(');}
241 <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');}
242 <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');}
243 <BOTH,SCRIPT,EXPRESSION>":" { RTOKEN(':'); }
244 <BOTH,SCRIPT,EXPRESSION>";" { RTOKEN(';');}
245 <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');}
246 <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');}
247 <BOTH,SCRIPT>"MEMORY" { RTOKEN(MEMORY);}
248 <BOTH,SCRIPT>"ORIGIN" { RTOKEN(ORIGIN);}
249 <BOTH,SCRIPT>"BLOCK" { RTOKEN(BLOCK);}
250 <BOTH,SCRIPT>"LENGTH" { RTOKEN(LENGTH);}
251 <EXPRESSION,BOTH,SCRIPT>"ALIGN" { RTOKEN(ALIGN_K);}
252 <EXPRESSION,BOTH,SCRIPT>"ADDR" { RTOKEN(ADDR);}
253 <BOTH,SCRIPT>"ENTRY" { RTOKEN(ENTRY);}
254 <EXPRESSION,BOTH,SCRIPT>"NEXT" { RTOKEN(NEXT);}
255 <EXPRESSION,BOTH,SCRIPT>"sizeof_headers" { RTOKEN(SIZEOF_HEADERS);}
256 <EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS" { RTOKEN(SIZEOF_HEADERS);}
257 <BOTH,SCRIPT>"MAP" { RTOKEN(MAP);}
258 <EXPRESSION,BOTH,SCRIPT>"SIZEOF" { RTOKEN(SIZEOF);}
259 <BOTH,SCRIPT>"TARGET" { RTOKEN(TARGET_K);}
260 <BOTH,SCRIPT>"SEARCH_DIR" { RTOKEN(SEARCH_DIR);}
261 <BOTH,SCRIPT>"OUTPUT" { RTOKEN(OUTPUT);}
262 <BOTH,SCRIPT>"INPUT" { RTOKEN(INPUT);}
263 <BOTH,SCRIPT>"DEFINED" { RTOKEN(DEFINED);}
264 <BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS" { RTOKEN(CREATE_OBJECT_SYMBOLS);}
265 <BOTH,SCRIPT>"CONSTRUCTORS" { RTOKEN( CONSTRUCTORS);}
266 <BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION);}
267 <BOTH,SCRIPT>"SECTIONS" { RTOKEN(SECTIONS);}
268 <BOTH,SCRIPT>"FILL" { RTOKEN(FILL);}
269 <BOTH,SCRIPT>"STARTUP" { RTOKEN(STARTUP);}
270 <BOTH,SCRIPT>"OUTPUT_FORMAT" { RTOKEN(OUTPUT_FORMAT);}
271 <BOTH,SCRIPT>"OUTPUT_ARCH" { RTOKEN( OUTPUT_ARCH);}
272 <BOTH,SCRIPT>"HLL" { RTOKEN(HLL);}
273 <BOTH,SCRIPT>"SYSLIB" { RTOKEN(SYSLIB);}
274 <BOTH,SCRIPT>"FLOAT" { RTOKEN(FLOAT);}
275 <BOTH,SCRIPT>"LONG" { RTOKEN( LONG);}
276 <BOTH,SCRIPT>"SHORT" { RTOKEN( SHORT);}
277 <BOTH,SCRIPT>"BYTE" { RTOKEN( BYTE);}
278 <BOTH,SCRIPT>"NOFLOAT" { RTOKEN(NOFLOAT);}
279 <BOTH,SCRIPT>"NOLOAD" { RTOKEN(NOLOAD);}
280 <BOTH,SCRIPT>"DSECT" { RTOKEN(DSECT);}
281 <BOTH,SCRIPT>"COPY" { RTOKEN(COPY);}
282 <BOTH,SCRIPT>"INFO" { RTOKEN(INFO);}
283 <BOTH,SCRIPT>"OVERLAY" { RTOKEN(OVERLAY);}
284 <BOTH,SCRIPT>"o" { RTOKEN(ORIGIN);}
285 <BOTH,SCRIPT>"org" { RTOKEN(ORIGIN);}
286 <BOTH,SCRIPT>"l" { RTOKEN( LENGTH);}
287 <BOTH,SCRIPT>"len" { RTOKEN( LENGTH);}
288 <MRI>"\n" { ++ lineno; RTOKEN(NEWLINE); }
289 <MRI>^"*".* { /* Mri comment line */ }
290 <MRI>"END" { RTOKEN(ENDWORD); }
291 <MRI>"CHIP" { RTOKEN(CHIP); }
292 <MRI>"LOAD" { RTOKEN(LOAD); }
293 <MRI>"ORDER" { RTOKEN(ORDER); }
294 <MRI>"NAME" { RTOKEN(NAMEWORD); }
295 <MRI>"FORMAT" { RTOKEN(FORMAT); }
296 <MRI>"LIST".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
297 <MRI>"SECT" { RTOKEN(SECT); }
298 <MRI>"ABSOLUTE" { RTOKEN(ABSOLUTE); }
299 <MRI>"end" { RTOKEN(ENDWORD); }
300 <MRI>"chip" { RTOKEN(CHIP); }
301 <MRI>"load" { RTOKEN(LOAD); }
302 <MRI>"order" { RTOKEN(ORDER); }
303 <MRI>"name" { RTOKEN(NAMEWORD); }
304 <MRI>"format" { RTOKEN(FORMAT); }
305 <MRI>"list".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
306 <MRI>"sect" { RTOKEN(SECT); }
307 <MRI>"absolute" { RTOKEN(ABSOLUTE); }
309 <MRI,BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}* {
310 yylval.name = buystring(yytext);
313 <SCRIPT,COMMAND>{FILENAMECHAR}* { yylval.name = buystring(yytext);
317 <EXPRESSION,BOTH,COMMAND,SCRIPT>"\""[^\"]*"\"" {
318 /* No matter the state, quotes
319 give what's inside */
320 yylval.name = buystring(yytext+1);
321 yylval.name[yyleng-2] = 0;
324 <BOTH,SCRIPT,EXPRESSION>"\n" { lineno++;}
325 <MRI,COMMAND,BOTH,SCRIPT,EXPRESSION>[ \t]
327 "/*" { old = INITIAL; BEGIN(COMMENT); }
328 <COMMAND>"/*" { old = COMMAND; BEGIN(COMMENT); }
329 <BOTH>"/*" { old =BOTH; BEGIN(COMMENT); }
330 <SCRIPT>"/*" { old = EXPRESSION; BEGIN(COMMENT); }
331 <EXPRESSION>"/*" { old = SCRIPT; BEGIN(COMMENT); }
332 "/*" { old = INITIAL; BEGIN(COMMENT); }
334 <COMMENT>"*"+[^*/\\n]*
335 <COMMENT>\\n { ++lineno;}
336 <COMMENT>"*"+"/" { BEGIN(old); }
342 if (include_stack_ptr == 0)
348 yy_switch_to_buffer(include_stack[include_stack_ptr]);
364 DEFUN(lex_push_file,(file),
367 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
369 einfo("%F:includes nested too deeply");
371 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
373 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
379 DEFUN(yy_create_string_buffer,(string, size),
380 CONST char *string AND
385 b = (YY_BUFFER_STATE) malloc( sizeof( struct yy_buffer_state ) );
386 b->yy_input_file = 0;
389 YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
391 b->yy_buf_size = size;
393 /* yy_ch_buf has to be 2 characters longer than the size given because
394 * we need to put in 2 end-of-buffer characters.
396 b->yy_ch_buf = (YY_CHAR *) malloc( (unsigned) (b->yy_buf_size + 3) );
398 if ( ! b->yy_ch_buf )
399 YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
402 b->yy_ch_buf[0] = '\n';
403 strcpy(b->yy_ch_buf+1, string);
404 b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
405 b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
406 b->yy_n_chars = size+1;
407 b->yy_buf_pos = &b->yy_ch_buf[1];
409 b->yy_eof_status = EOF_NOT_SEEN;
417 DEFUN(lex_redirect,( string),
422 int len = strlen(string);
424 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
426 einfo("%F: macros nested too deeply");
428 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
430 tmp = yy_create_string_buffer(string, len);
432 yy_switch_to_buffer(tmp);
438 int *state_stack_p = state_stack;
441 DEFUN_VOID(ldlex_script)
443 *(state_stack_p)++ = yy_start;
450 DEFUN_VOID(ldlex_mri_script)
452 *(state_stack_p)++ = yy_start;
457 DEFUN_VOID(ldlex_defsym)
459 *(state_stack_p)++ = yy_start;
464 DEFUN_VOID(ldlex_expression)
466 *(state_stack_p)++ = yy_start;
471 DEFUN_VOID(ldlex_both)
473 *(state_stack_p)++ = yy_start;
477 DEFUN_VOID(ldlex_command)
479 *(state_stack_p)++ = yy_start;
484 DEFUN_VOID(ldlex_popstate)
486 yy_start = *(--state_stack_p);
489 yy_input(buf, result, max_size)
495 if (yy_current_buffer->yy_input_file)
498 if ( (*result = read( fileno(yyin), (char *) buf, max_size )) < 0 )
499 YY_FATAL_ERROR( "read() in flex scanner failed" );