3 Copyright (C) 1989-1995 Alan R. Baldwin
4 721 Berkeley St., Kent, Ohio 44240
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
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/>. */
21 * - applied changes from 28-Oct-97 JLH:
22 * - outsym: show s_id as string rather than array [NCPS]
33 * The module asout.c contains all the functions used to
34 * generate the .REL assembler output file.
37 * The assemblers' output object file is an ascii file containing
38 * the information needed by the linker to bind multiple object
39 * modules into a complete loadable memory image.
41 * The object module contains the following designators:
48 * H Most significant byte first
49 * L Least significant byte first
56 * R Relocation information
57 * P Paging information
62 * The first line of an object module contains the [XDQ][HL]
63 * format specifier (i.e. XH indicates a hexidecimal file with
64 * most significant byte first) for the following designators.
69 * H aa areas gg global symbols
71 * The header line specifies the number of areas(aa) and the
72 * number of global symbols(gg) defined or referenced in this ob-
73 * ject module segment.
80 * The module line specifies the module name from which this
81 * header segment was assembled. The module line will not appear
82 * if the .module directive was not used in the source program.
93 * The symbol line defines (Def) or references (Ref) the symbol
94 * 'string' with the value nnnn. The defined value is relative to
95 * the current area base address. References to constants and ex-
96 * ternal global symbols will always appear before the first area
97 * definition. References to external symbols will have a value of
103 * A label size ss flags ff
105 * The area line defines the area label, the size (ss) of the
106 * area in bytes, and the area flags (ff). The area flags specify
107 * the ABS, REL, CON, OVR, and PAG parameters:
109 * OVR/CON (0x04/0x00 i.e. bit position 2)
111 * ABS/REL (0x08/0x00 i.e. bit position 3)
113 * PAG (0x10 i.e. bit position 4)
118 * T xx xx nn nn nn nn nn ...
120 * The T line contains the assembled code output by the assem-
121 * bler with xx xx being the offset address from the current area
122 * base address and nn being the assembled instructions and data in
128 * R 0 0 nn nn n1 n2 xx xx ...
130 * The R line provides the relocation information to the linker.
131 * The nn nn value is the current area index, i.e. which area the
132 * current values were assembled. Relocation information is en-
133 * coded in groups of 4 bytes:
135 * 1. n1 is the relocation mode and object format
136 * 1. bit 0 word(0x00)/byte(0x01)
137 * 2. bit 1 relocatable area(0x00)/symbol(0x02)
138 * 3. bit 2 normal(0x00)/PC relative(0x04) relocation
139 * 4. bit 3 1-byte(0x00)/2-byte(0x08) object format for
141 * 5. bit 4 signed(0x00)/unsigned(0x10) byte data
142 * 6. bit 5 normal(0x00)/page '0'(0x20) reference
143 * 7. bit 6 normal(0x00)/page 'nnn'(0x40) reference
144 * 8. bit 7 normal(0x00)/MSB of value
146 * 2. n2 is a byte index into the corresponding (i.e. pre-
147 * ceeding) T line data (i.e. a pointer to the data to be
148 * updated by the relocation). The T line data may be
149 * 1-byte or 2-byte byte data format or 2-byte word
152 * 3. xx xx is the area/symbol index for the area/symbol be-
153 * ing referenced. the corresponding area/symbol is found
154 * in the header area/symbol lists.
157 * The groups of 4 bytes are repeated for each item requiring relo-
158 * cation in the preceeding T line.
163 * P 0 0 nn nn n1 n2 xx xx
165 * The P line provides the paging information to the linker as
166 * specified by a .setdp directive. The format of the relocation
167 * information is identical to that of the R line. The correspond-
168 * ing T line has the following information:
169 * T xx xx aa aa bb bb
171 * Where aa aa is the area reference number which specifies the
172 * selected page area and bb bb is the base address of the page.
173 * bb bb will require relocation processing if the 'n1 n2 xx xx' is
174 * specified in the P line. The linker will verify that the base
175 * address is on a 256 byte boundary and that the page length of an
176 * area defined with the PAG type is not larger than 256 bytes.
178 * The linker defaults any direct page references to the first
179 * area defined in the input REL file. All ASxxxx assemblers will
180 * specify the _CODE area first, making this the default page area.
183 * asout.c contains the following functions:
204 * The module asout.c contains the following local variables:
205 * int rel[] relocation data for code/data array
206 * int * relp pointer to rel array
207 * int txt[] assembled code/data array
208 * int * txtp pointer to txt array
217 char *txtp = { &txt[0] };
218 char *relp = { &rel[0] };
220 /*)Function VOID outab(b)
222 * int b assembler data word
224 * The function outab() processes a single word of
225 * assembled data in absolute format.
228 * int * txtp pointer to data word
231 * int oflag -o, generate relocatable output flag
232 * int pass assembler pass number
235 * VOID outchk() asout.c
236 * VOID out_lb() asout.c
239 * The current assembly address is incremented by 1.
255 /*)Function VOID outaw(w)
257 * int w assembler data word
259 * The function outaw() processes a single word of
260 * assembled data in absolute format.
263 * int * txtp pointer to data word
266 * int oflag -o, generate relocatable output flag
267 * int pass assembler pass number
270 * VOID outchk() asout.c
271 * VOID out_lw() asout.c
274 * The current assembly address is incremented by 2.
290 /*)Function VOID outrb(esp, r)
292 * expr * esp pointer to expr structure
293 * int r relocation mode
295 * The function outrb() processes a byte of generated code
296 * in either absolute or relocatable format dependent upon
297 * the data contained in the expr structure esp. If the
298 * .REL output is enabled then the appropriate information
299 * is loaded into the txt and rel buffers.
302 * int n symbol/area reference number
303 * int * relp pointer to rel array
304 * int * txtp pointer to txt array
307 * sym dot defined as sym[0]
308 * int oflag -o, generate relocatable output flag
309 * int pass assembler pass number
312 * VOID aerr() assubr.c
313 * VOID outchk() asout.c
314 * VOID out_lb() asout.c
315 * VOID out_rb() asout.c
316 * VOID out_tb() asout.c
319 * The current assembly address is incremented by 1.
323 outrb(struct expr *esp, int r)
328 if (esp->e_flag==0 && esp->e_base.e_ap==NULL) {
329 out_lb(lobyte(esp->e_addr),0);
332 *txtp++ = lobyte(esp->e_addr);
335 r |= R_BYTE | R_BYT2 | esp->e_rlcf;
337 out_lb(hibyte(esp->e_addr),r|R_RELOC|R_HIGH);
339 out_lb(lobyte(esp->e_addr),r|R_RELOC);
345 n = esp->e_base.e_sp->s_ref;
348 n = esp->e_base.e_ap->a_ref;
351 *relp++ = txtp - txt - 2;
359 /*)Function VOID outrw(esp, r)
361 * expr * esp pointer to expr structure
362 * int r relocation mode
364 * The function outrw() processes a word of generated code
365 * in either absolute or relocatable format dependent upon
366 * the data contained in the expr structure esp. If the
367 * .REL output is enabled then the appropriate information
368 * is loaded into the txt and rel buffers.
371 * int n symbol/area reference number
372 * int * relp pointer to rel array
373 * int * txtp pointer to txt array
376 * sym dot defined as sym[0]
377 * int oflag -o, generate relocatable output flag
378 * int pass assembler pass number
381 * VOID aerr() assubr.c
382 * VOID outchk() asout.c
383 * VOID out_lw() asout.c
384 * VOID out_rw() asout.c
385 * VOID out_tw() asout.c
388 * The current assembly address is incremented by 2.
392 outrw(struct expr *esp, int r)
397 if (esp->e_flag==0 && esp->e_base.e_ap==NULL) {
398 out_lw(esp->e_addr,0);
404 r |= R_WORD | esp->e_rlcf;
408 out_lw(hibyte(esp->e_addr),r|R_RELOC);
410 out_lw(lobyte(esp->e_addr),r|R_RELOC);
413 out_lw(esp->e_addr,r|R_RELOC);
419 n = esp->e_base.e_sp->s_ref;
422 n = esp->e_base.e_ap->a_ref;
425 *relp++ = txtp - txt - 2;
433 /*)Function VOID outdp(carea, esp)
435 * area * carea pointer to current area strcuture
436 * expr * esp pointer to expr structure
438 * The function outdp() flushes the output buffer and
439 * outputs paging information to the .REL file.
442 * int n symbol/area reference number
443 * int r relocation mode
444 * int * relp pointer to rel array
445 * int * txtp pointer to txt array
448 * int oflag -o, generate relocatable output flag
449 * int pass assembler pass number
452 * VOID outbuf() asout.c
453 * VOID outchk() asout.c
454 * VOID out_rw() asout.c
455 * VOID out_tw() asout.c
458 * Output buffer flushed to .REL fiel.
459 * Paging information dumped to .REL file.
463 outdp(struct area *carea, struct expr *esp)
467 if (oflag && pass==2) {
469 out_tw(carea->a_ref);
471 if (esp->e_flag || esp->e_base.e_ap!=NULL) {
474 n = esp->e_base.e_sp->s_ref;
477 n = esp->e_base.e_ap->a_ref;
480 *relp++ = txtp - txt - 2;
487 /*)Function VOID outall()
489 * The function outall() will output any bufferred assembled
490 * data and relocation information (during pass 2 if the .REL
491 * output has been enabled).
497 * int oflag -o, generate relocatable output flag
498 * int pass assembler pass number
501 * VOID outbuf() asout.c
504 * assembled data and relocation buffers will be cleared.
510 if (oflag && pass==2)
514 /*)Function VOID outdot()
516 * The function outdot() outputs information about the
517 * current program counter value (during pass 2 if the .REL
518 * output has been enabled).
524 * int oflag -o, generate relocatable output flag
525 * int pass assembler pass number
528 * int fprintf() c_library
532 * assembled data and relocation buffers will be cleared.
538 if (oflag && pass==2) {
540 out(txt,(int) (txtp-txt));
543 out(rel,(int) (relp-rel));
550 /*)Function outchk(nt, nr)
552 * int nr number of additional relocation words
553 * int nt number of additional data words
555 * The function outchk() checks the data and relocation buffers
556 * for space to insert the nt data words and nr relocation words.
557 * If space is not available then output the current data and
558 * initialize the data buffers to receive the new data.
561 * area * ap pointer to an area structure
562 * int * relp pointer to rel array
563 * int * txtp pointer to txt array
566 * sym dot defined as sym[0]
569 * VOID outbuf() asout.c
572 * Data and relocation buffers may be emptied and initialized.
576 outchk(int nt, int nr)
578 register struct area *ap;
580 if (txtp+nt > &txt[NTXT] || relp+nr > &rel[NREL]) {
585 if ((ap = dot.s_area) != NULL) {
586 *relp++ = R_WORD|R_AREA;
593 /*)Function VOID outbuf()
595 * The function outbuf() will output any bufferred data
596 * and relocation information to the .REL file. The output
597 * buffer pointers and counters are initialized.
600 * int rel[] relocation data for code/data array
601 * int * relp pointer to rel array
602 * int txt[] assembled code/data array
603 * int * txtp pointer to txt array
606 * FILE * ofp relocation output file handle
612 * All bufferred data written to .REL file and
613 * buffer pointers and counters initialized.
619 if (txtp > &txt[2]) {
621 out(txt,(int) (txtp-txt));
623 fprintf(ofp, "%s", s);
624 out(rel,(int) (relp-rel));
631 /*)Function VOID outgsd()
633 * The function outgsd() performs the following:
634 * (1) outputs the .REL file radix
635 * (2) outputs the header specifying the number
636 * of areas and global symbols
637 * (3) outputs the module name
638 * (4) set the reference number and output a symbol line
639 * for all external global variables and absolutes
640 * (5) output an area name, set reference number and output
641 * a symbol line for all global relocatables in the area.
642 * Repeat this proceedure for all areas.
645 * area * ap pointer to an area structure
646 * sym * sp pointer to a sym structure
649 * int c string character value
650 * int narea number of code areas
651 * char * ptr string pointer
652 * int nglob number of global symbols
653 * int rn symbol reference number
656 * area * areap pointer to an area structure
657 * char module[] module name string
658 * sym * symhash[] array of pointers to NHASH
659 * linked symbol lists
660 * int xflag -x, listing radix flag
663 * int fprintf() c_library
664 * VOID outarea() asout.c
665 * VOID outsym() asout.c
666 * int putc() c_library
669 * All symbols are given reference numbers, all symbol
670 * and area information is output to the .REL file.
676 register struct area *ap;
677 register struct sym *sp;
680 int c, narea, nglob, rn;
685 narea = areap->a_ref + 1;
688 * Number of global references/absolutes
691 for (i = 0; i < NHASH; ++i) {
694 if (sp->s_flag&S_GBL)
701 * Output Radix and number of areas and symbols
704 fprintf(ofp, "X%c\n", hilo ? 'H' : 'L');
705 fprintf(ofp, "H %X areas %X global symbols\n", narea, nglob);
708 fprintf(ofp, "Q%c\n", hilo ? 'H' : 'L');
709 fprintf(ofp, "H %o areas %o global symbols\n", narea, nglob);
712 fprintf(ofp, "D%c\n", hilo ? 'H' : 'L');
713 fprintf(ofp, "H %u areas %u global symbols\n", narea, nglob);
722 while (ptr < &module[NCPS]) {
723 if ((c = *ptr++) != 0)
730 * Sdcc compile options
732 if (strlen(optsdcc)) fprintf(ofp, "O %s\n", optsdcc);
735 * Global references and absolutes.
738 for (i=0; i<NHASH; ++i) {
741 if (sp->s_area==NULL && sp->s_flag&S_GBL) {
750 * Global relocatables.
752 for (i=0; i<narea; ++i) {
754 while (ap->a_ref != i)
757 for (j=0; j<NHASH; ++j) {
760 if (sp->s_area==ap && sp->s_flag&S_GBL) {
770 /*)Function VOID outarea(ap)
772 * area * ap pointer to an area structure
774 * The function outarea() outputs the A line to the .REL
775 * file. The A line contains the area's name, size, and
779 * char * ptr pointer to area id string
780 * int c character value
783 * FILE * ofp relocation output file handle
784 * int xflag -x, listing radix flag
787 * int fprintf() c_library
788 * int putc() c_library
791 * The A line is sent to the .REL file.
795 outarea(struct area *ap)
797 fprintf(ofp, "A %s", ap->a_id);
799 fprintf(ofp, " size %X flags %X\n", ap->a_size, ap->a_flag);
802 fprintf(ofp, " size %o flags %o\n", ap->a_size, ap->a_flag);
805 fprintf(ofp, " size %u flags %u\n", ap->a_size, ap->a_flag);
809 /*)Function VOID outsym(sp)
811 * sym * sp pointer to a sym structure
813 * The function outsym() outputs the S line to the .REL
814 * file. The S line contains the symbols name and whether the
815 * the symbol is defined or referenced.
818 * char * ptr pointer to symbol id string
819 * int c character value
822 * FILE * ofp relocation output file handle
823 * int xflag -x, listing radix flag
826 * int fprintf() c_library
827 * int putc() c_library
830 * The S line is sent to the .REL file.
834 outsym(struct sym *sp)
836 fprintf(ofp, "S %s", sp->s_id);
837 fprintf(ofp, " %s", sp->s_type==S_NEW ? "Ref" : "Def");
839 fprintf(ofp, "%04X\n", sp->s_addr);
842 fprintf(ofp, "%06o\n", sp->s_addr);
845 fprintf(ofp, "%05u\n", sp->s_addr);
849 /*)Function VOID out(p, n)
851 * int n number of words to output
852 * int * p pointer to data words
854 * The function out() outputs the data words to the .REL file
855 * int the specified radix.
861 * FILE * ofp relocation output file handle
862 * int xflag -x, listing radix flag
865 * int fprintf() c_library
868 * Data is sent to the .REL file.
876 fprintf(ofp, " %02X", (*p++)&0377);
879 fprintf(ofp, " %03o", (*p++)&0377);
882 fprintf(ofp, " %03u", (*p++)&0377);
887 /*)Function VOID out_lb(b, t)
889 * int b assembled data
890 * int t relocation type
892 * The function out_lb() copies the assembled data and
893 * its relocation type to the list data buffers.
899 * int * cp pointer to assembler output array cb[]
900 * int * cpt pointer to assembler relocation type
907 * Pointers to data and relocation buffers incremented by 1.
913 if (cp < &cb[NCODE]) {
919 /*)Function VOID out_lw(n, t)
921 * int n assembled data
922 * int t relocation type
924 * The function out_lw() copies the assembled data and
925 * its relocation type to the list data buffers.
931 * int * cp pointer to assembler output array cb[]
932 * int * cpt pointer to assembler relocation type
939 * Pointers to data and relocation buffers incremented by 2.
946 out_lb(hibyte(n),t ? t|R_HIGH : 0);
950 out_lb(hibyte(n),t ? t|R_HIGH : 0);
954 /*)Function VOID out_rw(n)
958 * The function out_rw() outputs the relocation (R)
959 * data word as two bytes ordered according to hilo.
962 * int * relp pointer to rel array
968 * int lobyte() asout.c
969 * int hibyte() asout.c
972 * Pointer to relocation buffer incremented by 2.
987 /*)Function VOID out_tw(n)
991 * The function out_tw() outputs the text (T)
992 * data word as two bytes ordered according to hilo.
995 * int * txtp pointer to txt array
1001 * int lobyte() asout.c
1002 * int hibyte() asout.c
1005 * Pointer to relocation buffer incremented by 2.
1012 *txtp++ = hibyte(n);
1013 *txtp++ = lobyte(n);
1015 *txtp++ = lobyte(n);
1016 *txtp++ = hibyte(n);
1020 /*)Function int lobyte(n)
1024 * The function lobyte() returns the lower byte of
1046 /*)Function int hibyte(n)
1050 * The function hibyte() returns the higher byte of
1069 return ((n>>8)&0377);