struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / sdas / linksrc / lkrloc.c
blob30f01a7a3ba03f759440cab96e719c4f26f6db44
1 /* lkrloc.c */
3 /*
4 * Copyright (C) 1989-2009 Alan R. Baldwin
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Alan R. Baldwin
21 * 721 Berkeley St.
22 * Kent, Ohio 44240
24 * With enhancements from:
26 * John L. Hartman (JLH)
27 * jhartman@compuserve.com
29 * Bill McKinnon (BM)
30 * w_mckinnon@conknet.com
33 #include <ctype.h>
34 #include "aslink.h"
36 /*)Module lkrloc.c
38 * The module lkrloc.c contains the functions which
39 * perform the relocation calculations.
41 * lkrloc.c contains the following functions:
42 * a_uint adb_1b()
43 * a_uint adb_2b()
44 * a_uint adb_3b()
45 * a_uint adb_4b()
46 * a_uint adb_xb()
47 * a_uint evword()
48 * VOID prntval()
49 * VOID reloc()
54 /*)Function VOID reloc(c)
56 * int c process code
58 * The function reloc() calls the proper version
59 * of the linker code.
61 * local variable:
62 * none
64 * global variables:
65 * ASxxxx_VERSION ASxxxx REL file version
67 * called functions:
68 * VOID reloc3() lkrloc3.c
69 * VOID reloc4() lkrloc4.c
71 * side effects:
72 * Refer to the called relocation functions.
76 VOID
77 reloc(int c)
79 switch(ASxxxx_VERSION) {
80 case 3:
81 reloc3(c);
82 break;
84 // case 4:
85 // reloc4(c);
86 // break;
88 default:
89 fprintf(stderr, "Internal Version Error");
90 lkexit(ER_FATAL);
91 break;
96 /*)Function a_uint evword()
98 * The function evword() combines two byte values
99 * into a single word value.
101 * local variable:
102 * a_uint v temporary evaluation variable
104 * global variables:
105 * hilo byte ordering parameter
107 * called functions:
108 * int eval() lkeval.c
110 * side effects:
111 * Relocation text line is scanned to combine
112 * two byte values into a single word value.
116 a_uint
117 evword(void)
119 a_uint v;
121 if (hilo) {
122 v = (eval() << 8);
123 v += eval();
124 } else {
125 v = eval();
126 v += (eval() << 8);
128 return(v);
131 /*)Function a_uint adb_1b(v, i)
133 * a_uint v value to add to byte
134 * int i rtval[] index
136 * The function adb_1b() adds the value of v to
137 * the single byte value contained in rtval[i].
138 * The new value of rtval[i] is returned.
140 * local variable:
141 * a_uint j temporary evaluation variable
143 * global variables:
144 * none
146 * called functions:
147 * none
149 * side effects:
150 * The byte value of rtval[] is changed.
154 a_uint
155 adb_1b(a_uint v, int i)
157 a_uint j;
159 j = v + rtval[i];
160 rtval[i] = j & ((a_uint) 0x000000FF);
162 return(j);
165 /*)Function a_uint adb_2b(v, i)
167 * a_uint v value to add to word
168 * int i rtval[] index
170 * The function adb_2b() adds the value of v to the
171 * 2 byte value contained in rtval[i] and rtval[i+1].
172 * The new value of rtval[i] / rtval[i+1] is returned.
174 * local variable:
175 * a_uint j temporary evaluation variable
177 * global variables:
178 * hilo byte ordering parameter
180 * called functions:
181 * none
183 * side effects:
184 * The 2 byte value of rtval[] is changed.
188 a_uint
189 adb_2b(a_uint v, int i)
191 a_uint j;
193 if (hilo) {
194 j = v + (rtval[i+0] << 8) +
195 (rtval[i+1] << 0);
196 rtval[i+0] = (j >> 8) & ((a_uint) 0x000000FF);
197 rtval[i+1] = (j >> 0) & ((a_uint) 0x000000FF);
198 } else {
199 j = v + (rtval[i+0] << 0) +
200 (rtval[i+1] << 8);
201 rtval[i+0] = (j >> 0) & ((a_uint) 0x000000FF);
202 rtval[i+1] = (j >> 8) & ((a_uint) 0x000000FF);
204 return(j);
207 /*)Function a_uint adb_3b(v, i)
209 * a_uint v value to add to word
210 * int i rtval[] index
212 * The function adb_3b() adds the value of v to the
213 * three byte value contained in rtval[i], rtval[i+1], and rtval[i+2].
214 * The new value of rtval[i] / rtval[i+1] / rtval[i+2] is returned.
216 * local variable:
217 * a_uint j temporary evaluation variable
219 * global variables:
220 * hilo byte ordering parameter
222 * called functions:
223 * none
225 * side effects:
226 * The 3 byte value of rtval[] is changed.
230 a_uint
231 adb_3b(a_uint v, int i)
233 a_uint j;
235 if (hilo) {
236 j = v + (rtval[i+0] << 16) +
237 (rtval[i+1] << 8) +
238 (rtval[i+2] << 0);
239 rtval[i+0] = (j >> 16) & ((a_uint) 0x000000FF);
240 rtval[i+1] = (j >> 8) & ((a_uint) 0x000000FF);
241 rtval[i+2] = (j >> 0) & ((a_uint) 0x000000FF);
242 } else {
243 j = v + (rtval[i+0] << 0) +
244 (rtval[i+1] << 8) +
245 (rtval[i+2] << 16);
246 rtval[i+0] = (j >> 0) & ((a_uint) 0x000000FF);
247 rtval[i+1] = (j >> 8) & ((a_uint) 0x000000FF);
248 rtval[i+2] = (j >> 16) & ((a_uint) 0x000000FF);
250 return(j);
253 /*)Function a_uint adb_4b(v, i)
255 * a_uint v value to add to word
256 * int i rtval[] index
258 * The function adb_4b() adds the value of v to the
259 * four byte value contained in rtval[i], ..., rtval[i+3].
260 * The new value of rtval[i], ..., rtval[i+3] is returned.
262 * local variable:
263 * a_uint j temporary evaluation variable
265 * global variables:
266 * hilo byte ordering parameter
268 * called functions:
269 * none
271 * side effects:
272 * The 4 byte value of rtval[] is changed.
276 a_uint
277 adb_4b(a_uint v, int i)
279 a_uint j;
281 if (hilo) {
282 j = v + (rtval[i+0] << 24) +
283 (rtval[i+1] << 16) +
284 (rtval[i+2] << 8) +
285 (rtval[i+3] << 0);
286 rtval[i+0] = (j >> 24) & ((a_uint) 0x000000FF);
287 rtval[i+1] = (j >> 16) & ((a_uint) 0x000000FF);
288 rtval[i+2] = (j >> 8) & ((a_uint) 0x000000FF);
289 rtval[i+3] = (j >> 0) & ((a_uint) 0x000000FF);
290 } else {
291 j = v + (rtval[i+0] << 0) +
292 (rtval[i+1] << 8) +
293 (rtval[i+2] << 16) +
294 (rtval[i+3] << 24);
295 rtval[i+0] = (j >> 0) & ((a_uint) 0x000000FF);
296 rtval[i+1] = (j >> 8) & ((a_uint) 0x000000FF);
297 rtval[i+2] = (j >> 16) & ((a_uint) 0x000000FF);
298 rtval[i+3] = (j >> 24) & ((a_uint) 0x000000FF);
300 return(j);
303 /*)Function a_uint adb_xb(v, i)
305 * a_uint v value to add to x-bytes
306 * int i rtval[] index
308 * The function adb_xb() adds the value of v to
309 * the value contained in rtval[i] for x-bytes.
310 * The new value of rtval[i] for x-bytes is returned.
312 * local variable:
313 * none
315 * global variables:
316 * int a_bytes T Line Address Bytes
318 * called functions:
319 * a_uint adb_1b() lkrloc.c
320 * a_uint adb_2b() lkrloc.c
321 * a_uint adb_3b() lkrloc.c
322 * a_uint adb_4b() lkrloc.c
324 * side effects:
325 * The x-byte value of rtval[] is changed.
329 a_uint
330 adb_xb(a_uint v, int i)
332 a_uint j;
334 switch(a_bytes){
335 case 1:
336 j = adb_1b(v, i);
337 j = (j & ((a_uint) 0x00000080) ? j | ~((a_uint) 0x0000007F) : j & ((a_uint) 0x0000007F));
338 break;
339 case 2:
340 j = adb_2b(v, i);
341 j = (j & ((a_uint) 0x00008000) ? j | ~((a_uint) 0x00007FFF) : j & ((a_uint) 0x00007FFF));
342 break;
343 case 3:
344 j = adb_3b(v, i);
345 j = (j & ((a_uint) 0x00800000) ? j | ~((a_uint) 0x007FFFFF) : j & ((a_uint) 0x007FFFFF));
346 break;
347 case 4:
348 j = adb_4b(v, i);
349 j = (j & ((a_uint) 0x80000000) ? j | ~((a_uint) 0x7FFFFFFF) : j & ((a_uint) 0x7FFFFFFF));
350 break;
351 default:
352 j = 0;
353 break;
355 return(j);
358 /*)Function VOID prntval(fptr, v)
360 * FILE *fptr output file handle
361 * a_uint v value to output
363 * The function prntval() outputs the value v, in the
364 * currently selected radix, to the device specified
365 * by fptr.
367 * local variable:
368 * none
370 * global variables:
371 * int xflag current radix
373 * called functions:
374 * int fprintf() c_library
376 * side effects:
377 * none
381 VOID
382 prntval(FILE *fptr, a_uint v)
384 char *frmt;
386 switch(xflag) {
387 default:
388 case 0:
389 switch(a_bytes) {
390 default:
391 case 2: frmt = " %04X\n"; break;
392 case 3: frmt = " %06X\n"; break;
393 case 4: frmt = " %08X\n"; break;
395 break;
396 case 1:
397 switch(a_bytes) {
398 default:
399 case 2: frmt = " %06o\n"; break;
400 case 3: frmt = " %08o\n"; break;
401 case 4: frmt = "%011o\n"; break;
403 break;
404 case 2:
405 switch(a_bytes) {
406 default:
407 case 2: frmt = " %05u\n"; break;
408 case 3: frmt = " %08u\n"; break;
409 case 4: frmt = " %010u\n"; break;
411 break;
413 fprintf(fptr, frmt, v & a_mask);