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