]>
Commit | Line | Data |
---|---|---|
c611e285 SC |
1 | /* This module handles expression trees. |
2 | Copyright (C) 1991 Free Software Foundation, Inc. | |
3 | Written by Steve Chamberlain of Cygnus Support ([email protected]). | |
2fa0b342 DHW |
4 | |
5 | This file is part of GLD, the Gnu Linker. | |
6 | ||
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 | |
c611e285 | 9 | the Free Software Foundation; either version 2, or (at your option) |
2fa0b342 DHW |
10 | any later version. |
11 | ||
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. | |
16 | ||
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. */ | |
20 | ||
21 | /* | |
c611e285 | 22 | This module is in charge of working out the contents of expressions. |
2fa0b342 | 23 | |
c611e285 SC |
24 | It has to keep track of the relative/absness of a symbol etc. This is |
25 | done by keeping all values in a struct (an etree_value_type) which | |
26 | contains a value, a section to which it is relative and a valid bit. | |
27 | ||
28 | */ | |
2fa0b342 DHW |
29 | |
30 | ||
2fa0b342 | 31 | #include "bfd.h" |
c611e285 | 32 | #include "sysdep.h" |
2fa0b342 DHW |
33 | |
34 | #include "ld.h" | |
35 | #include "ldmain.h" | |
36 | #include "ldmisc.h" | |
37 | #include "ldexp.h" | |
c611e285 | 38 | #include "ldgram.h" |
2fa0b342 DHW |
39 | #include "ldsym.h" |
40 | #include "ldlang.h" | |
41 | ||
42 | extern char *output_filename; | |
43 | extern unsigned int undefined_global_sym_count; | |
44 | extern unsigned int defined_global_sym_count; | |
45 | extern bfd *output_bfd; | |
c611e285 | 46 | extern bfd_size_type largest_section; |
2fa0b342 DHW |
47 | extern lang_statement_list_type file_chain; |
48 | extern args_type command_line; | |
49 | extern ld_config_type config; | |
50 | ||
51 | extern lang_input_statement_type *script_file; | |
52 | extern unsigned int defined_global_sym_count; | |
53 | ||
54 | extern bfd_vma print_dot; | |
55 | ||
56 | ||
57 | static void | |
c611e285 SC |
58 | DEFUN(exp_print_token,(outfile, code), |
59 | FILE *outfile AND | |
60 | token_code_type code) | |
2fa0b342 DHW |
61 | { |
62 | static struct { | |
63 | token_code_type code; | |
64 | char *name; | |
65 | } table[] = | |
c611e285 SC |
66 | { |
67 | INT, "int", | |
68 | NAME,"NAME", | |
69 | PLUSEQ,"+=", | |
70 | MINUSEQ,"-=", | |
71 | MULTEQ,"*=", | |
72 | DIVEQ,"/=", | |
73 | LSHIFTEQ,"<<=", | |
74 | RSHIFTEQ,">>=", | |
75 | ANDEQ,"&=", | |
76 | OREQ,"|=", | |
77 | OROR,"||", | |
78 | ANDAND,"&&", | |
79 | EQ,"==", | |
80 | NE,"!=", | |
81 | LE,"<=", | |
82 | GE,">=", | |
83 | LSHIFT,"<<", | |
84 | RSHIFT,">>=", | |
85 | ALIGN_K,"ALIGN", | |
86 | BLOCK,"BLOCK", | |
87 | SECTIONS,"SECTIONS", | |
88 | SIZEOF_HEADERS,"SIZEOF_HEADERS", | |
89 | NEXT,"NEXT", | |
90 | SIZEOF,"SIZEOF", | |
91 | ADDR,"ADDR", | |
92 | MEMORY,"MEMORY", | |
93 | ||
94 | ||
95 | ||
96 | ||
97 | ||
98 | DEFINED,"DEFINED", | |
99 | TARGET_K,"TARGET", | |
100 | SEARCH_DIR,"SEARCH_DIR", | |
101 | MAP,"MAP", | |
102 | LONG,"LONG", | |
103 | SHORT,"SHORT", | |
104 | BYTE,"BYTE", | |
105 | ENTRY,"ENTRY", | |
106 | 0,(char *)NULL} ; | |
2fa0b342 DHW |
107 | |
108 | ||
109 | ||
110 | unsigned int idx; | |
111 | for (idx = 0; table[idx].name != (char*)NULL; idx++) { | |
112 | if (table[idx].code == code) { | |
113 | fprintf(outfile, "%s", table[idx].name); | |
114 | return; | |
115 | } | |
116 | } | |
117 | /* Not in table, just print it alone */ | |
118 | fprintf(outfile, "%c",code); | |
119 | } | |
120 | ||
121 | static void | |
c611e285 SC |
122 | DEFUN(make_abs,(ptr), |
123 | etree_value_type *ptr) | |
2fa0b342 DHW |
124 | { |
125 | if (ptr->section != (lang_output_section_statement_type *)NULL) { | |
126 | asection *s = ptr->section->bfd_section; | |
127 | ptr->value += s->vma; | |
128 | ptr->section = (lang_output_section_statement_type *)NULL; | |
129 | } | |
130 | ||
131 | } | |
c611e285 | 132 | |
2fa0b342 | 133 | static |
c611e285 SC |
134 | DEFUN(etree_value_type new_abs,(value), |
135 | bfd_vma value) | |
2fa0b342 DHW |
136 | { |
137 | etree_value_type new; | |
138 | new.valid = true; | |
139 | new.section = (lang_output_section_statement_type *)NULL; | |
140 | new.value = value; | |
141 | return new; | |
142 | } | |
143 | ||
3a399523 SC |
144 | static void |
145 | DEFUN(check, (os, name, op), | |
146 | lang_output_section_statement_type *os AND | |
147 | CONST char *name AND | |
148 | CONST char *op) | |
2fa0b342 DHW |
149 | { |
150 | if (os == (lang_output_section_statement_type *)NULL) { | |
c611e285 | 151 | einfo("%F%P %s uses undefined section %s\n", op, name); |
2fa0b342 DHW |
152 | } |
153 | if (os->processed == false) { | |
c611e285 | 154 | einfo("%F%P %s forward reference of section %s\n",op, name); |
2fa0b342 DHW |
155 | } |
156 | } | |
157 | ||
c611e285 SC |
158 | etree_type * |
159 | DEFUN(exp_intop,(value), | |
160 | bfd_vma value) | |
2fa0b342 | 161 | { |
c611e285 | 162 | etree_type *new = (etree_type *)ldmalloc((bfd_size_type)(sizeof(new->value))); |
2fa0b342 DHW |
163 | new->type.node_code = INT; |
164 | new->value.value = value; | |
165 | new->type.node_class = etree_value; | |
166 | return new; | |
167 | ||
168 | } | |
169 | ||
170 | ||
171 | static | |
c611e285 SC |
172 | DEFUN(etree_value_type new_rel,(value, section), |
173 | bfd_vma value AND | |
174 | lang_output_section_statement_type *section) | |
2fa0b342 DHW |
175 | { |
176 | etree_value_type new; | |
177 | new.valid = true; | |
178 | new.value = value; | |
179 | new.section = section; | |
180 | return new; | |
181 | } | |
182 | ||
183 | static | |
c611e285 SC |
184 | DEFUN(etree_value_type |
185 | new_rel_from_section, (value, section), | |
186 | bfd_vma value AND | |
187 | lang_output_section_statement_type *section) | |
2fa0b342 DHW |
188 | { |
189 | etree_value_type new; | |
190 | new.valid = true; | |
191 | new.value = value; | |
192 | new.section = section; | |
193 | if (new.section != (lang_output_section_statement_type *)NULL) { | |
194 | new.value -= section->bfd_section->vma; | |
195 | } | |
196 | return new; | |
197 | } | |
198 | ||
199 | static etree_value_type | |
c611e285 SC |
200 | DEFUN(fold_binary,(tree, current_section, allocation_done, dot, dotp), |
201 | etree_type *tree AND | |
202 | lang_output_section_statement_type *current_section AND | |
203 | lang_phase_type allocation_done AND | |
204 | bfd_vma dot AND | |
205 | bfd_vma *dotp) | |
2fa0b342 DHW |
206 | { |
207 | etree_value_type result; | |
208 | ||
209 | result = exp_fold_tree(tree->binary.lhs, current_section, | |
210 | allocation_done, dot, dotp); | |
211 | if (result.valid) { | |
212 | etree_value_type other; | |
213 | other = exp_fold_tree(tree->binary.rhs, | |
214 | current_section, | |
215 | allocation_done, dot,dotp) ; | |
216 | if (other.valid) { | |
217 | /* If values are from different sections, or this is an */ | |
218 | /* absolute expression, make both source args absolute */ | |
219 | if (result.section != other.section || | |
220 | current_section == (lang_output_section_statement_type *)NULL) { | |
221 | ||
222 | make_abs(&result); | |
223 | make_abs(&other); | |
224 | } | |
225 | ||
226 | switch (tree->type.node_code) | |
227 | { | |
228 | case '%': | |
229 | /* Mod, both absolule*/ | |
230 | ||
231 | if (other.value == 0) { | |
c611e285 | 232 | einfo("%F%S % by zero\n"); |
2fa0b342 DHW |
233 | } |
234 | result.value %= other.value; | |
235 | break; | |
236 | case '/': | |
237 | if (other.value == 0) { | |
c611e285 | 238 | einfo("%F%S / by zero\n"); |
2fa0b342 DHW |
239 | } |
240 | result.value /= other.value; | |
241 | break; | |
242 | #define BOP(x,y) case x : result.value = result.value y other.value;break; | |
243 | BOP('+',+); | |
244 | BOP('*',*); | |
245 | BOP('-',-); | |
246 | BOP(LSHIFT,<<); | |
247 | BOP(RSHIFT,>>); | |
248 | BOP(EQ,==); | |
249 | BOP(NE,!=); | |
250 | BOP('<',<); | |
251 | BOP('>',>); | |
252 | BOP(LE,<=); | |
253 | BOP(GE,>=); | |
254 | BOP('&',&); | |
255 | BOP('^',^); | |
256 | BOP('|',|); | |
257 | BOP(ANDAND,&&); | |
258 | BOP(OROR,||); | |
259 | default: | |
260 | FAIL(); | |
261 | } | |
262 | } | |
070aa819 SC |
263 | else { |
264 | result.valid = false; | |
265 | } | |
2fa0b342 DHW |
266 | } |
267 | return result; | |
268 | } | |
c611e285 SC |
269 | etree_value_type |
270 | DEFUN_VOID(invalid) | |
2fa0b342 DHW |
271 | { |
272 | etree_value_type new; | |
273 | new.valid = false; | |
274 | return new; | |
275 | } | |
276 | ||
c611e285 SC |
277 | etree_value_type |
278 | DEFUN(fold_name, (tree, current_section, allocation_done, dot), | |
279 | etree_type *tree AND | |
280 | lang_output_section_statement_type *current_section AND | |
281 | lang_phase_type allocation_done AND | |
282 | bfd_vma dot) | |
2fa0b342 DHW |
283 | { |
284 | etree_value_type result; | |
285 | switch (tree->type.node_code) | |
ac004870 | 286 | { |
65c552e3 SC |
287 | case SIZEOF_HEADERS: |
288 | if (allocation_done != lang_first_phase_enum) | |
289 | { | |
290 | result = new_abs(bfd_sizeof_headers(output_bfd, | |
291 | config.relocateable_output)); | |
292 | ||
293 | } | |
294 | else { | |
295 | result.valid = false; | |
296 | } | |
297 | break; | |
ac004870 SC |
298 | case DEFINED: |
299 | result.value = | |
300 | ldsym_get_soft(tree->name.name) != (ldsym_type *)NULL; | |
301 | result.section = 0; | |
302 | result.valid = true; | |
303 | break; | |
304 | case NAME: | |
305 | result.valid = false; | |
306 | if (tree->name.name[0] == '.' && tree->name.name[1] == 0) { | |
307 | ||
308 | if (allocation_done != lang_first_phase_enum) { | |
309 | result = new_rel_from_section(dot, current_section); | |
310 | } | |
311 | else { | |
312 | result = invalid(); | |
313 | } | |
2fa0b342 DHW |
314 | } |
315 | else { | |
ac004870 SC |
316 | if (allocation_done == lang_final_phase_enum) { |
317 | ldsym_type *sy = ldsym_get_soft(tree->name.name); | |
2fa0b342 | 318 | |
ac004870 SC |
319 | if (sy) { |
320 | asymbol **sdefp = sy->sdefs_chain; | |
321 | ||
322 | if (sdefp) { | |
323 | asymbol *sdef = *sdefp; | |
324 | if (sdef->section == (asection *)NULL) { | |
325 | /* This is an absolute symbol */ | |
326 | result = new_abs(sdef->value); | |
327 | } | |
328 | else { | |
329 | lang_output_section_statement_type *os = | |
330 | lang_output_section_statement_lookup( | |
331 | sdef->section->output_section->name); | |
332 | /* If the symbol is from a file which we are not | |
333 | relocating (-R) then return an absolute for its | |
334 | value */ | |
335 | if (sdef->the_bfd->usrdata && | |
336 | ((lang_input_statement_type*)(sdef->the_bfd->usrdata))->just_syms_flag == true) | |
337 | { | |
b0f36869 | 338 | result = new_abs(sdef->value + (sdef->section ? |
65c552e3 | 339 | sdef->section->vma : 0)); |
ac004870 SC |
340 | } |
341 | else { | |
a37cc0c0 | 342 | result = new_rel(sdef->value + sdef->section->output_offset, os); |
ac004870 SC |
343 | } |
344 | } | |
2fa0b342 DHW |
345 | } |
346 | } | |
ac004870 | 347 | if (result.valid == false) { |
c611e285 | 348 | einfo("%F%S: undefined symbol `%s' referenced in expression.\n", |
2fa0b342 | 349 | tree->name.name); |
ac004870 | 350 | } |
2fa0b342 | 351 | |
ac004870 | 352 | } |
2fa0b342 | 353 | } |
2fa0b342 | 354 | |
ac004870 | 355 | break; |
2fa0b342 | 356 | |
ac004870 | 357 | case ADDR: |
2fa0b342 | 358 | |
ac004870 SC |
359 | if (allocation_done != lang_first_phase_enum) { |
360 | lang_output_section_statement_type *os = | |
361 | lang_output_section_find(tree->name.name); | |
362 | check(os,tree->name.name,"ADDR"); | |
363 | result = new_rel((bfd_vma)0, os); | |
364 | } | |
365 | else { | |
366 | result = invalid(); | |
367 | } | |
368 | break; | |
369 | case SIZEOF: | |
370 | if(allocation_done != lang_first_phase_enum) { | |
371 | lang_output_section_statement_type *os = | |
372 | lang_output_section_find(tree->name.name); | |
373 | check(os,tree->name.name,"SIZEOF"); | |
c611e285 | 374 | result = new_abs((bfd_vma)(os->bfd_section->_raw_size)); |
ac004870 SC |
375 | } |
376 | else { | |
377 | result = invalid(); | |
378 | } | |
379 | break; | |
2fa0b342 | 380 | |
ac004870 SC |
381 | default: |
382 | FAIL(); | |
383 | break; | |
384 | } | |
2fa0b342 DHW |
385 | |
386 | return result; | |
387 | } | |
c611e285 SC |
388 | etree_value_type |
389 | DEFUN(exp_fold_tree,(tree, current_section, allocation_done, | |
390 | dot, dotp), | |
391 | etree_type *tree AND | |
392 | lang_output_section_statement_type *current_section AND | |
393 | lang_phase_type allocation_done AND | |
394 | bfd_vma dot AND | |
395 | bfd_vma *dotp) | |
2fa0b342 DHW |
396 | { |
397 | etree_value_type result; | |
398 | ||
399 | if (tree == (etree_type *)NULL) { | |
c611e285 SC |
400 | result.valid = false; |
401 | } | |
2fa0b342 | 402 | else { |
c611e285 SC |
403 | switch (tree->type.node_class) |
404 | { | |
405 | case etree_value: | |
406 | result = new_rel(tree->value.value, current_section); | |
407 | break; | |
408 | case etree_unary: | |
409 | result = exp_fold_tree(tree->unary.child, | |
410 | current_section, | |
411 | allocation_done, dot, dotp); | |
412 | if (result.valid == true) | |
d4c02e29 | 413 | { |
c611e285 SC |
414 | switch(tree->type.node_code) |
415 | { | |
416 | case ALIGN_K: | |
417 | if (allocation_done != lang_first_phase_enum) { | |
418 | result = new_rel_from_section(ALIGN(dot, | |
419 | result.value) , | |
420 | current_section); | |
d4c02e29 | 421 | |
2fa0b342 | 422 | } |
c611e285 SC |
423 | else { |
424 | result.valid = false; | |
425 | } | |
426 | break; | |
427 | case '~': | |
428 | make_abs(&result); | |
429 | result.value = ~result.value; | |
430 | break; | |
431 | case '!': | |
432 | make_abs(&result); | |
433 | result.value = !result.value; | |
434 | break; | |
435 | case '-': | |
436 | make_abs(&result); | |
437 | result.value = -result.value; | |
438 | break; | |
439 | case NEXT: | |
440 | if (allocation_done ==lang_allocating_phase_enum) { | |
441 | make_abs(&result); | |
442 | result.value = ALIGN(dot, result.value); | |
443 | } | |
444 | else { | |
445 | /* Return next place aligned to value */ | |
446 | result.valid = false; | |
447 | } | |
448 | break; | |
449 | default: | |
450 | FAIL(); | |
451 | } | |
452 | } | |
2fa0b342 | 453 | |
c611e285 SC |
454 | break; |
455 | case etree_trinary: | |
2fa0b342 | 456 | |
c611e285 SC |
457 | result = exp_fold_tree(tree->trinary.cond, |
458 | current_section, | |
459 | allocation_done, dot, dotp); | |
460 | if (result.valid) { | |
d4c02e29 SC |
461 | result = exp_fold_tree(result.value ? |
462 | tree->trinary.lhs:tree->trinary.rhs, | |
2fa0b342 | 463 | current_section, |
d4c02e29 SC |
464 | allocation_done, dot, dotp); |
465 | } | |
466 | ||
c611e285 SC |
467 | break; |
468 | case etree_binary: | |
469 | result = fold_binary(tree, current_section, allocation_done, | |
470 | dot, dotp); | |
471 | break; | |
472 | case etree_assign: | |
473 | if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) { | |
d4c02e29 SC |
474 | /* Assignment to dot can only be done during allocation */ |
475 | if (allocation_done == lang_allocating_phase_enum) { | |
c611e285 SC |
476 | result = exp_fold_tree(tree->assign.src, |
477 | current_section, | |
478 | lang_allocating_phase_enum, dot, dotp); | |
479 | if (result.valid == false) { | |
480 | einfo("%F%S invalid assignment to location counter\n"); | |
d4c02e29 | 481 | } |
c611e285 SC |
482 | else { |
483 | if (current_section == | |
484 | (lang_output_section_statement_type *)NULL) { | |
485 | einfo("%F%S assignment to location counter invalid outside of SECTION\n"); | |
486 | } | |
487 | else { | |
488 | unsigned long nextdot =result.value + | |
489 | current_section->bfd_section->vma; | |
490 | if (nextdot < dot) { | |
491 | einfo("%F%S cannot move location counter backwards"); | |
492 | } | |
493 | else { | |
494 | *dotp = nextdot; | |
495 | } | |
496 | } | |
d4c02e29 | 497 | } |
2fa0b342 | 498 | } |
2fa0b342 | 499 | } |
c611e285 | 500 | else { |
d4c02e29 SC |
501 | ldsym_type *sy = ldsym_get(tree->assign.dst); |
502 | ||
503 | /* If this symbol has just been created then we'll place it into | |
504 | * a section of our choice | |
505 | */ | |
506 | result = exp_fold_tree(tree->assign.src, | |
507 | current_section, allocation_done, | |
508 | dot, dotp); | |
509 | if (result.valid) | |
c611e285 SC |
510 | { |
511 | asymbol *def; | |
512 | asymbol **def_ptr ; | |
513 | /* Add this definition to script file */ | |
514 | if (sy->sdefs_chain) | |
515 | { | |
516 | def_ptr = sy->sdefs_chain; | |
517 | def = *def_ptr; | |
518 | ||
519 | } | |
520 | else | |
521 | { | |
522 | def_ptr = (asymbol **)ldmalloc((bfd_size_type)(sizeof(asymbol **))); | |
523 | def = (asymbol | |
524 | *)bfd_make_empty_symbol(script_file->the_bfd); | |
525 | ||
526 | ||
527 | ||
528 | *def_ptr = def; | |
529 | } | |
530 | ||
531 | def->value = result.value; | |
532 | if (result.section != | |
533 | (lang_output_section_statement_type *)NULL) { | |
534 | if (current_section != | |
535 | (lang_output_section_statement_type *)NULL) { | |
2fa0b342 | 536 | |
d4c02e29 SC |
537 | def->section = result.section->bfd_section; |
538 | def->flags = BSF_GLOBAL | BSF_EXPORT; | |
539 | } | |
c611e285 | 540 | else { |
d4c02e29 SC |
541 | /* Force to absolute */ |
542 | def->value += result.section->bfd_section->vma; | |
c611e285 SC |
543 | def->section = &bfd_abs_section; |
544 | def->flags = BSF_GLOBAL | BSF_EXPORT ; | |
d4c02e29 SC |
545 | } |
546 | ||
547 | ||
c611e285 SC |
548 | } |
549 | else { | |
550 | def->section = &bfd_abs_section; | |
551 | def->flags = BSF_GLOBAL | BSF_EXPORT ; | |
552 | } | |
d4c02e29 SC |
553 | |
554 | ||
c611e285 SC |
555 | def->udata = (PTR)NULL; |
556 | def->name = sy->name; | |
557 | ||
558 | if (sy->sdefs_chain == 0) Q_enter_global_ref(def_ptr); | |
559 | } | |
2fa0b342 | 560 | |
d4c02e29 | 561 | } |
2fa0b342 DHW |
562 | |
563 | ||
c611e285 SC |
564 | break; |
565 | case etree_name: | |
566 | result = fold_name(tree, current_section, allocation_done, dot); | |
567 | break; | |
568 | default: | |
569 | einfo("%F%S Need more of these %d",tree->type.node_class ); | |
2fa0b342 | 570 | |
c611e285 SC |
571 | } |
572 | } | |
2fa0b342 DHW |
573 | |
574 | return result; | |
575 | } | |
576 | ||
577 | ||
c611e285 SC |
578 | etree_value_type |
579 | DEFUN(exp_fold_tree_no_dot,(tree, current_section, allocation_done), | |
580 | etree_type *tree AND | |
581 | lang_output_section_statement_type *current_section AND | |
582 | lang_phase_type allocation_done) | |
2fa0b342 DHW |
583 | { |
584 | return exp_fold_tree(tree, current_section, allocation_done, (bfd_vma) | |
585 | 0, (bfd_vma *)NULL); | |
586 | } | |
587 | ||
588 | etree_type * | |
c611e285 SC |
589 | DEFUN(exp_binop,(code, lhs, rhs), |
590 | int code AND | |
591 | etree_type *lhs AND | |
592 | etree_type *rhs) | |
2fa0b342 DHW |
593 | { |
594 | etree_type value, *new; | |
595 | etree_value_type r; | |
596 | ||
597 | value.type.node_code = code; | |
598 | value.binary.lhs = lhs; | |
599 | value.binary.rhs = rhs; | |
600 | value.type.node_class = etree_binary; | |
601 | r = exp_fold_tree_no_dot(&value, (lang_output_section_statement_type *)NULL, | |
602 | lang_first_phase_enum ); | |
603 | if (r.valid) | |
604 | { | |
605 | return exp_intop(r.value); | |
606 | } | |
c611e285 | 607 | new = (etree_type *)ldmalloc((bfd_size_type)(sizeof(new->binary))); |
2fa0b342 DHW |
608 | memcpy((char *)new, (char *)&value, sizeof(new->binary)); |
609 | return new; | |
610 | } | |
611 | ||
612 | etree_type * | |
c611e285 SC |
613 | DEFUN(exp_trinop,(code, cond, lhs, rhs), |
614 | int code AND | |
615 | etree_type *cond AND | |
616 | etree_type *lhs AND | |
617 | etree_type *rhs) | |
2fa0b342 DHW |
618 | { |
619 | etree_type value, *new; | |
620 | etree_value_type r; | |
621 | value.type.node_code = code; | |
622 | value.trinary.lhs = lhs; | |
623 | value.trinary.cond = cond; | |
624 | value.trinary.rhs = rhs; | |
625 | value.type.node_class = etree_trinary; | |
626 | r= exp_fold_tree_no_dot(&value, (lang_output_section_statement_type | |
627 | *)NULL,lang_first_phase_enum); | |
628 | if (r.valid) { | |
629 | return exp_intop(r.value); | |
630 | } | |
c611e285 | 631 | new = (etree_type *)ldmalloc((bfd_size_type)(sizeof(new->trinary))); |
2fa0b342 DHW |
632 | memcpy((char *)new,(char *) &value, sizeof(new->trinary)); |
633 | return new; | |
634 | } | |
635 | ||
636 | ||
637 | etree_type * | |
c611e285 SC |
638 | DEFUN(exp_unop,(code, child), |
639 | int code AND | |
640 | etree_type *child) | |
2fa0b342 DHW |
641 | { |
642 | etree_type value, *new; | |
643 | ||
644 | etree_value_type r; | |
645 | value.unary.type.node_code = code; | |
646 | value.unary.child = child; | |
647 | value.unary.type.node_class = etree_unary; | |
c611e285 SC |
648 | r = exp_fold_tree_no_dot(&value,(lang_output_section_statement_type *)NULL, |
649 | lang_first_phase_enum); | |
650 | if (r.valid) { | |
2fa0b342 DHW |
651 | return exp_intop(r.value); |
652 | } | |
c611e285 | 653 | new = (etree_type *)ldmalloc((bfd_size_type)(sizeof(new->unary))); |
2fa0b342 DHW |
654 | memcpy((char *)new, (char *)&value, sizeof(new->unary)); |
655 | return new; | |
656 | } | |
657 | ||
658 | ||
659 | etree_type * | |
c611e285 SC |
660 | DEFUN(exp_nameop,(code, name), |
661 | int code AND | |
662 | CONST char *name) | |
2fa0b342 DHW |
663 | { |
664 | ||
665 | etree_type value, *new; | |
666 | ||
667 | etree_value_type r; | |
668 | value.name.type.node_code = code; | |
669 | value.name.name = name; | |
670 | value.name.type.node_class = etree_name; | |
671 | ||
672 | ||
673 | r = exp_fold_tree_no_dot(&value,(lang_output_section_statement_type *)NULL, | |
674 | lang_first_phase_enum); | |
675 | if (r.valid) { | |
676 | return exp_intop(r.value); | |
677 | } | |
c611e285 | 678 | new = (etree_type *)ldmalloc((bfd_size_type)(sizeof(new->name))); |
2fa0b342 DHW |
679 | memcpy((char *)new, (char *)&value, sizeof(new->name)); |
680 | return new; | |
681 | ||
682 | } | |
683 | ||
684 | ||
685 | ||
686 | ||
687 | etree_type * | |
c611e285 SC |
688 | DEFUN(exp_assop,(code, dst, src), |
689 | int code AND | |
690 | CONST char *dst AND | |
691 | etree_type *src) | |
2fa0b342 DHW |
692 | { |
693 | etree_type value, *new; | |
694 | ||
695 | value.assign.type.node_code = code; | |
696 | ||
697 | ||
698 | value.assign.src = src; | |
699 | value.assign.dst = dst; | |
700 | value.assign.type.node_class = etree_assign; | |
701 | ||
702 | #if 0 | |
703 | if (exp_fold_tree_no_dot(&value, &result)) { | |
704 | return exp_intop(result); | |
705 | } | |
706 | #endif | |
c611e285 | 707 | new = (etree_type*)ldmalloc((bfd_size_type)(sizeof(new->assign))); |
2fa0b342 DHW |
708 | memcpy((char *)new, (char *)&value, sizeof(new->assign)); |
709 | return new; | |
710 | } | |
711 | ||
712 | void | |
c611e285 SC |
713 | DEFUN(exp_print_tree,(outfile, tree), |
714 | FILE *outfile AND | |
715 | etree_type *tree) | |
2fa0b342 DHW |
716 | { |
717 | switch (tree->type.node_class) { | |
718 | case etree_value: | |
c611e285 | 719 | print_address(tree->value.value); |
2fa0b342 | 720 | return; |
c611e285 | 721 | |
2fa0b342 DHW |
722 | case etree_assign: |
723 | #if 0 | |
724 | if (tree->assign.dst->sdefs != (asymbol *)NULL){ | |
725 | fprintf(outfile,"%s (%x) ",tree->assign.dst->name, | |
726 | tree->assign.dst->sdefs->value); | |
727 | } | |
728 | else { | |
729 | fprintf(outfile,"%s (UNDEFINED)",tree->assign.dst->name); | |
730 | } | |
731 | #endif | |
732 | fprintf(outfile,"%s ",tree->assign.dst); | |
733 | exp_print_token(outfile,tree->type.node_code); | |
734 | exp_print_tree(outfile,tree->assign.src); | |
735 | break; | |
736 | case etree_binary: | |
737 | exp_print_tree(outfile,tree->binary.lhs); | |
738 | exp_print_token(outfile,tree->type.node_code); | |
739 | exp_print_tree(outfile,tree->binary.rhs); | |
740 | break; | |
741 | case etree_trinary: | |
742 | exp_print_tree(outfile,tree->trinary.cond); | |
743 | fprintf(outfile,"?"); | |
744 | exp_print_tree(outfile,tree->trinary.lhs); | |
745 | fprintf(outfile,":"); | |
746 | exp_print_tree(outfile,tree->trinary.rhs); | |
747 | break; | |
748 | case etree_unary: | |
749 | exp_print_token(outfile,tree->unary.type.node_code); | |
750 | fprintf(outfile,"("); | |
751 | exp_print_tree(outfile,tree->unary.child); | |
752 | fprintf(outfile,")"); | |
753 | break; | |
754 | case etree_undef: | |
755 | fprintf(outfile,"????????"); | |
756 | break; | |
757 | case etree_name: | |
758 | if (tree->type.node_code == NAME) { | |
759 | fprintf(outfile,"%s", tree->name.name); | |
760 | } | |
761 | else { | |
762 | exp_print_token(outfile,tree->type.node_code); | |
763 | fprintf(outfile,"(%s)", tree->name.name); | |
764 | } | |
765 | break; | |
766 | default: | |
767 | FAIL(); | |
768 | break; | |
769 | } | |
770 | } | |
771 | ||
772 | ||
773 | ||
774 | ||
775 | bfd_vma | |
c611e285 SC |
776 | DEFUN(exp_get_vma,(tree, def, name, allocation_done), |
777 | etree_type *tree AND | |
778 | bfd_vma def AND | |
779 | char *name AND | |
780 | lang_phase_type allocation_done) | |
2fa0b342 DHW |
781 | { |
782 | etree_value_type r; | |
783 | ||
784 | if (tree != (etree_type *)NULL) { | |
785 | r = exp_fold_tree_no_dot(tree, | |
786 | (lang_output_section_statement_type *)NULL, | |
787 | allocation_done); | |
788 | if (r.valid == false && name) { | |
c611e285 | 789 | einfo("%F%S Nonconstant expression for %s\n",name); |
2fa0b342 DHW |
790 | } |
791 | return r.value; | |
792 | } | |
793 | else { | |
794 | return def; | |
795 | } | |
796 | } | |
797 | ||
798 | int | |
c611e285 SC |
799 | DEFUN(exp_get_value_int,(tree,def,name, allocation_done), |
800 | etree_type *tree AND | |
801 | int def AND | |
802 | char *name AND | |
803 | lang_phase_type allocation_done) | |
2fa0b342 DHW |
804 | { |
805 | return (int)exp_get_vma(tree,(bfd_vma)def,name, allocation_done); | |
806 | } | |
65c552e3 | 807 |