4 * (C) Copyright 1989-1995
12 * - outsym: show s_id as string rather than array [NCPS]
13 * - Added outr11 to support 8051's 11 bit destination address
25 * The module asout.c contains all the functions used to
26 * generate the .REL assembler output file.
29 * The assemblers' output object file is an ascii file containing
30 * the information needed by the linker to bind multiple object
31 * modules into a complete loadable memory image.
33 * The object module contains the following designators:
40 * H Most significant byte first
41 * L Least significant byte first
48 * R Relocation information
49 * P Paging information
54 * The first line of an object module contains the [XDQ][HL]
55 * format specifier (i.e. XH indicates a hexadecimal file with
56 * most significant byte first) for the following designators.
61 * H aa areas gg global symbols
63 * The header line specifies the number of areas(aa) and the
64 * number of global symbols(gg) defined or referenced in this ob-
65 * ject module segment.
72 * The module line specifies the module name from which this
73 * header segment was assembled. The module line will not appear
74 * if the .module directive was not used in the source program.
85 * The symbol line defines (Def) or references (Ref) the symbol
86 * 'string' with the value nnnn. The defined value is relative to
87 * the current area base address. References to constants and ex-
88 * ternal global symbols will always appear before the first area
89 * definition. References to external symbols will have a value of
95 * A label size ss flags ff
97 * The area line defines the area label, the size (ss) of the
98 * area in bytes, and the area flags (ff). The area flags specify
99 * the ABS, REL, CON, OVR, and PAG parameters:
101 * OVR/CON (0x04/0x00 i.e. bit position 2)
103 * ABS/REL (0x08/0x00 i.e. bit position 3)
105 * PAG (0x10 i.e. bit position 4)
110 * T xx xx nn nn nn nn nn ...
112 * The T line contains the assembled code output by the assem-
113 * bler with xx xx being the offset address from the current area
114 * base address and nn being the assembled instructions and data in
120 * R 0 0 nn nn n1 n2 xx xx ...
122 * The R line provides the relocation information to the linker.
123 * The nn nn value is the current area index, i.e. which area the
124 * current values were assembled. Relocation information is en-
125 * coded in groups of 4 bytes:
127 * 1. n1 is the relocation mode and object format
128 * 1. bit 0 word(0x00)/byte(0x01)
129 * 2. bit 1 relocatable area(0x00)/symbol(0x02)
130 * 3. bit 2 normal(0x00)/PC relative(0x04) relocation
131 * 4. bit 3 1-byte(0x00)/2-byte(0x08) object format for
133 * 5. bit 4 signed(0x00)/unsigned(0x10) byte data
134 * 6. bit 5 normal(0x00)/page '0'(0x20) reference
135 * 7. bit 6 normal(0x00)/page 'nnn'(0x40) reference
136 * 8. bit 7 normal(0x00)/MSB of value
138 * 2. n2 is a byte index into the corresponding (i.e. pre-
139 * ceeding) T line data (i.e. a pointer to the data to be
140 * updated by the relocation). The T line data may be
141 * 1-byte or 2-byte byte data format or 2-byte word
144 * 3. xx xx is the area/symbol index for the area/symbol be-
145 * ing referenced. the corresponding area/symbol is found
146 * in the header area/symbol lists.
149 * The groups of 4 bytes are repeated for each item requiring relo-
150 * cation in the preceeding T line.
155 * P 0 0 nn nn n1 n2 xx xx
157 * The P line provides the paging information to the linker as
158 * specified by a .setdp directive. The format of the relocation
159 * information is identical to that of the R line. The correspond-
160 * ing T line has the following information:
161 * T xx xx aa aa bb bb
163 * Where aa aa is the area reference number which specifies the
164 * selected page area and bb bb is the base address of the page.
165 * bb bb will require relocation processing if the 'n1 n2 xx xx' is
166 * specified in the P line. The linker will verify that the base
167 * address is on a 256 byte boundary and that the page length of an
168 * area defined with the PAG type is not larger than 256 bytes.
170 * The linker defaults any direct page references to the first
171 * area defined in the input REL file. All ASxxxx assemblers will
172 * specify the _CODE area first, making this the default page area.
175 * asout.c contains the following functions:
196 * The module asout.c contains the following local variables:
197 * int rel[] relocation data for code/data array
198 * int * relp pointer to rel array
199 * int txt[] assembled code/data array
200 * int * txtp pointer to txt array
209 char *txtp = { &txt[0] };
210 char *relp = { &rel[0] };
212 /*)Function VOID outab(b)
214 * int b assembler data word
216 * The function outab() processes a single word of
217 * assembled data in absolute format.
220 * int * txtp pointer to data word
223 * int oflag -o, generate relocatable output flag
224 * int pass assembler pass number
227 * VOID outchk() asout.c
228 * VOID out_lb() asout.c
231 * The current assembly address is incremented by 1.
247 /*)Function VOID outaw(w)
249 * int w assembler data word
251 * The function outaw() processes a single word of
252 * assembled data in absolute format.
255 * int * txtp pointer to data word
258 * int oflag -o, generate relocatable output flag
259 * int pass assembler pass number
262 * VOID outchk() asout.c
263 * VOID out_lw() asout.c
266 * The current assembly address is incremented by 2.
282 /*)Function VOID outrb(esp, r)
284 * expr * esp pointer to expr structure
285 * int r relocation mode
287 * The function outrb() processes a byte of generated code
288 * in either absolute or relocatable format dependent upon
289 * the data contained in the expr structure esp. If the
290 * .REL output is enabled then the appropriate information
291 * is loaded into the txt and rel buffers.
294 * int n symbol/area reference number
295 * int * relp pointer to rel array
296 * int * txtp pointer to txt array
299 * sym dot defined as sym[0]
300 * int oflag -o, generate relocatable output flag
301 * int pass assembler pass number
304 * VOID aerr() assubr.c
305 * VOID outchk() asout.c
306 * VOID out_lb() asout.c
307 * VOID out_rb() asout.c
308 * VOID out_tb() asout.c
311 * The current assembly address is incremented by 1.
316 register struct expr *esp;
322 if (esp->e_flag==0 && esp->e_base.e_ap==NULL) {
323 out_lb(lobyte(esp->e_addr),0);
326 *txtp++ = lobyte(esp->e_addr);
329 r |= R_BYTE | R_BYT2 | esp->e_rlcf;
331 out_lb(hibyte(esp->e_addr),r|R_RELOC|R_HIGH);
333 out_lb(lobyte(esp->e_addr),r|R_RELOC);
339 n = esp->e_base.e_sp->s_ref;
342 n = esp->e_base.e_ap->a_ref;
345 *relp++ = txtp - txt - 2;
353 /*)Function VOID outrw(esp, r)
355 * expr * esp pointer to expr structure
356 * int r relocation mode
358 * The function outrw() processes a word of generated code
359 * in either absolute or relocatable format dependent upon
360 * the data contained in the expr structure esp. If the
361 * .REL output is enabled then the appropriate information
362 * is loaded into the txt and rel buffers.
365 * int n symbol/area reference number
366 * int * relp pointer to rel array
367 * int * txtp pointer to txt array
370 * sym dot defined as sym[0]
371 * int oflag -o, generate relocatable output flag
372 * int pass assembler pass number
375 * VOID aerr() assubr.c
376 * VOID outchk() asout.c
377 * VOID out_lw() asout.c
378 * VOID out_rw() asout.c
379 * VOID out_tw() asout.c
382 * The current assembly address is incremented by 2.
387 register struct expr *esp;
394 if (esp->e_addr > 0xffff)
398 "large constant 0x%x truncated to 16 bits\n",
401 if (esp->e_flag==0 && esp->e_base.e_ap==NULL) {
402 out_lw(esp->e_addr,0);
408 r |= R_WORD | esp->e_rlcf;
412 out_lw(hibyte(esp->e_addr),r|R_RELOC);
414 out_lw(lobyte(esp->e_addr),r|R_RELOC);
417 out_lw(esp->e_addr,r|R_RELOC);
423 n = esp->e_base.e_sp->s_ref;
426 n = esp->e_base.e_ap->a_ref;
431 /* If this happens, the linker will
432 * attempt to process this 16 bit field
433 * as 24 bits. That would be bad.
436 "***Internal error: C24 out in "
441 *relp++ = txtp - txt - 2;
449 /*)Function VOID outr24(esp, r)
451 * expr * esp pointer to expr structure
452 * int r relocation mode
454 * The function outr24() processes 24 bits of generated code
455 * in either absolute or relocatable format dependent upon
456 * the data contained in the expr structure esp. If the
457 * .REL output is enabled then the appropriate information
458 * is loaded into the txt and rel buffers.
461 * int n symbol/area reference number
462 * int * relp pointer to rel array
463 * int * txtp pointer to txt array
466 * sym dot defined as sym[0]
467 * int oflag -o, generate relocatable output flag
468 * int pass assembler pass number
471 * VOID aerr() assubr.c
472 * VOID outchk() asout.c
473 * VOID out_l24() asout.c
474 * VOID out_rw() asout.c
475 * VOID out_t24() asout.c
478 * The current assembly address is incremented by 3.
482 outr24(struct expr *esp, int r)
487 if (esp->e_flag==0 && esp->e_base.e_ap==NULL) {
488 /* This is a constant expression. */
489 out_l24(esp->e_addr,0);
492 out_t24(esp->e_addr);
495 /* This is a symbol. */
496 r |= R_WORD | esp->e_rlcf;
498 /* I have no idea what this case is. */
501 out_lw(hibyte(esp->e_addr),r|R_RELOC);
503 out_lw(lobyte(esp->e_addr),r|R_RELOC);
506 out_l24(esp->e_addr,r|R_RELOC);
510 out_t24(esp->e_addr);
512 n = esp->e_base.e_sp->s_ref;
515 n = esp->e_base.e_ap->a_ref;
520 /* If this occurs, we cannot properly
521 * code the relocation data with the
522 * R_C24 flag. This means the linker
523 * will fail to do the 24 bit relocation.
527 "***Internal error: BYTE out in 24 "
528 "bit flat mode unexpected.\n");
533 *relp++ = txtp - txt - 3;
541 /*)Function VOID outdp(carea, esp)
543 * area * carea pointer to current area strcuture
544 * expr * esp pointer to expr structure
546 * The function outdp() flushes the output buffer and
547 * outputs paging information to the .REL file.
550 * int n symbol/area reference number
551 * int r relocation mode
552 * int * relp pointer to rel array
553 * int * txtp pointer to txt array
556 * int oflag -o, generate relocatable output flag
557 * int pass assembler pass number
560 * VOID outbuf() asout.c
561 * VOID outchk() asout.c
562 * VOID out_rw() asout.c
563 * VOID out_tw() asout.c
566 * Output buffer flushed to .REL fiel.
567 * Paging information dumped to .REL file.
572 register struct area *carea;
573 register struct expr *esp;
577 if (oflag && pass==2) {
579 out_tw(carea->a_ref);
581 if (esp->e_flag || esp->e_base.e_ap!=NULL) {
584 n = esp->e_base.e_sp->s_ref;
587 n = esp->e_base.e_ap->a_ref;
590 *relp++ = txtp - txt - 2;
597 /*)Function VOID outall()
599 * The function outall() will output any bufferred assembled
600 * data and relocation information (during pass 2 if the .REL
601 * output has been enabled).
607 * int oflag -o, generate relocatable output flag
608 * int pass assembler pass number
611 * VOID outbuf() asout.c
614 * assembled data and relocation buffers will be cleared.
620 if (oflag && pass==2)
624 /*)Function VOID outdot()
626 * The function outdot() outputs information about the
627 * current program counter value (during pass 2 if the .REL
628 * output has been enabled).
634 * int oflag -o, generate relocatable output flag
635 * int pass assembler pass number
638 * int fprintf() c_library
642 * assembled data and relocation buffers will be cleared.
648 if (oflag && pass==2) {
650 out(txt,(int) (txtp-txt));
653 out(rel,(int) (relp-rel));
660 /*)Function outchk(nt, nr)
662 * int nr number of additional relocation words
663 * int nt number of additional data words
665 * The function outchk() checks the data and relocation buffers
666 * for space to insert the nt data words and nr relocation words.
667 * If space is not available then output the current data and
668 * initialize the data buffers to receive the new data.
671 * area * ap pointer to an area structure
672 * int * relp pointer to rel array
673 * int * txtp pointer to txt array
676 * sym dot defined as sym[0]
679 * VOID outbuf() asout.c
682 * Data and relocation buffers may be emptied and initialized.
688 register struct area *ap;
690 if (txtp+nt > &txt[NTXT] || relp+nr > &rel[NREL]) {
695 if ((ap = dot.s_area) != NULL) {
696 *relp++ = R_WORD|R_AREA;
703 /*)Function VOID outbuf()
705 * The function outbuf() will output any bufferred data
706 * and relocation information to the .REL file. The output
707 * buffer pointers and counters are initialized.
710 * int rel[] relocation data for code/data array
711 * int * relp pointer to rel array
712 * int txt[] assembled code/data array
713 * int * txtp pointer to txt array
716 * FILE * ofp relocation output file handle
722 * All bufferred data written to .REL file and
723 * buffer pointers and counters initialized.
730 if (txtp > &txt[2]) {
732 out(txt,(int) (txtp-txt));
734 fprintf(ofp, "%s", s);
735 out(rel,(int) (relp-rel));
742 /*)Function VOID outgsd()
744 * The function outgsd() performs the following:
745 * (1) outputs the .REL file radix
746 * (2) outputs the header specifying the number
747 * of areas and global symbols
748 * (3) outputs the module name
749 * (4) set the reference number and output a symbol line
750 * for all external global variables and absolutes
751 * (5) output an area name, set reference number and output
752 * a symbol line for all global relocatables in the area.
753 * Repeat this proceedure for all areas.
756 * area * ap pointer to an area structure
757 * sym * sp pointer to a sym structure
760 * int c string character value
761 * int narea number of code areas
762 * char * ptr string pointer
763 * int nglob number of global symbols
764 * int rn symbol reference number
767 * area * areap pointer to an area structure
768 * char module[] module name string
769 * sym * symhash[] array of pointers to NHASH
770 * linked symbol lists
771 * int xflag -x, listing radix flag
774 * int fprintf() c_library
775 * VOID outarea() asout.c
776 * VOID outsym() asout.c
777 * int putc() c_library
780 * All symbols are given reference numbers, all symbol
781 * and area information is output to the .REL file.
787 register struct area *ap;
788 register struct sym *sp;
791 int c, narea, nglob, rn;
796 narea = areap->a_ref + 1;
799 * Number of global references/absolutes
802 for (i = 0; i < NHASH; ++i) {
805 if (sp->s_flag&S_GBL)
812 * Output Radix and number of areas and symbols
815 fprintf(ofp, "X%c\n", hilo ? 'H' : 'L');
816 fprintf(ofp, "H %X areas %X global symbols\n", narea, nglob);
819 fprintf(ofp, "Q%c\n", hilo ? 'H' : 'L');
820 fprintf(ofp, "H %o areas %o global symbols\n", narea, nglob);
823 fprintf(ofp, "D%c\n", hilo ? 'H' : 'L');
824 fprintf(ofp, "H %u areas %u global symbols\n", narea, nglob);
833 while (ptr < &module[NCPS]) {
834 if ((c = *ptr++) != 0)
841 * Global references and absolutes.
844 for (i=0; i<NHASH; ++i) {
847 if (sp->s_area==NULL && sp->s_flag&S_GBL) {
856 * Global relocatables.
858 for (i=0; i<narea; ++i) {
860 while (ap->a_ref != i)
863 for (j=0; j<NHASH; ++j) {
866 if (sp->s_area==ap && sp->s_flag&S_GBL) {
876 /*)Function VOID outarea(ap)
878 * area * ap pointer to an area structure
880 * The function outarea() outputs the A line to the .REL
881 * file. The A line contains the area's name, size, and
885 * char * ptr pointer to area id string
886 * int c character value
889 * FILE * ofp relocation output file handle
890 * int xflag -x, listing radix flag
893 * int fprintf() c_library
894 * int putc() c_library
897 * The A line is sent to the .REL file.
902 register struct area *ap;
909 while (ptr < &ap->a_id[NCPS]) {
910 if ((c = *ptr++) != 0)
914 fprintf(ofp, " size %X flags %X\n", ap->a_size, ap->a_flag);
917 fprintf(ofp, " size %o flags %o\n", ap->a_size, ap->a_flag);
920 fprintf(ofp, " size %u flags %u\n", ap->a_size, ap->a_flag);
924 /*)Function VOID outsym(sp)
926 * sym * sp pointer to a sym structure
928 * The function outsym() outputs the S line to the .REL
929 * file. The S line contains the symbols name and whether the
930 * the symbol is defined or referenced.
933 * char * ptr pointer to symbol id string
934 * int c character value
937 * FILE * ofp relocation output file handle
938 * int xflag -x, listing radix flag
941 * int fprintf() c_library
942 * int putc() c_library
945 * The S line is sent to the .REL file.
950 register struct sym *sp;
956 fprintf(ofp, "%s", ptr );
957 fprintf(ofp, " %s", sp->s_type==S_NEW ? "Ref" : "Def");
959 fprintf(ofp, "%04X\n", sp->s_addr);
962 fprintf(ofp, "%06o\n", sp->s_addr);
965 fprintf(ofp, "%05u\n", sp->s_addr);
969 /*)Function VOID out(p, n)
971 * int n number of words to output
972 * int * p pointer to data words
974 * The function out() outputs the data words to the .REL file
975 * int the specified radix.
981 * FILE * ofp relocation output file handle
982 * int xflag -x, listing radix flag
985 * int fprintf() c_library
988 * Data is sent to the .REL file.
998 fprintf(ofp, " %02X", (*p++)&0377);
1001 fprintf(ofp, " %03o", (*p++)&0377);
1004 fprintf(ofp, " %03u", (*p++)&0377);
1009 /*)Function VOID out_lb(b, t)
1011 * int b assembled data
1012 * int t relocation type
1014 * The function out_lb() copies the assembled data and
1015 * its relocation type to the list data buffers.
1021 * int * cp pointer to assembler output array cb[]
1022 * int * cpt pointer to assembler relocation type
1023 * output array cbt[]
1029 * Pointers to data and relocation buffers incremented by 1.
1036 if (cp < &cb[NCODE]) {
1042 /*)Function VOID out_lw(n, t)
1044 * int n assembled data
1045 * int t relocation type
1047 * The function out_lw() copies the assembled data and
1048 * its relocation type to the list data buffers.
1054 * int * cp pointer to assembler output array cb[]
1055 * int * cpt pointer to assembler relocation type
1056 * output array cbt[]
1062 * Pointers to data and relocation buffers incremented by 2.
1070 out_lb(hibyte(n),t ? t|R_HIGH : 0);
1071 out_lb(lobyte(n),t);
1073 out_lb(lobyte(n),t);
1074 out_lb(hibyte(n),t ? t|R_HIGH : 0);
1078 /*)Function VOID out_l24(n, t)
1080 * int n assembled data
1081 * int t relocation type
1083 * The function out_l24() copies the assembled data and
1084 * its relocation type to the list data buffers.
1090 * int * cp pointer to assembler output array cb[]
1091 * int * cpt pointer to assembler relocation type
1092 * output array cbt[]
1098 * Pointers to data and relocation buffers incremented by 3.
1102 out_l24(int n, int t)
1105 out_lb(byte3(n),t ? t|R_HIGH : 0);
1106 out_lb(hibyte(n),t);
1107 out_lb(lobyte(n),t);
1109 out_lb(lobyte(n),t);
1110 out_lb(hibyte(n),t);
1111 out_lb(byte3(n),t ? t|R_HIGH : 0);
1115 /*)Function VOID out_rw(n)
1119 * The function out_rw() outputs the relocation (R)
1120 * data word as two bytes ordered according to hilo.
1123 * int * relp pointer to rel array
1129 * int lobyte() asout.c
1130 * int hibyte() asout.c
1133 * Pointer to relocation buffer incremented by 2.
1141 *relp++ = hibyte(n);
1142 *relp++ = lobyte(n);
1144 *relp++ = lobyte(n);
1145 *relp++ = hibyte(n);
1149 /*)Function VOID out_tw(n)
1153 * The function out_tw() outputs the text (T)
1154 * data word as two bytes ordered according to hilo.
1157 * int * txtp pointer to txt array
1163 * int lobyte() asout.c
1164 * int hibyte() asout.c
1167 * Pointer to relocation buffer incremented by 2.
1175 *txtp++ = hibyte(n);
1176 *txtp++ = lobyte(n);
1178 *txtp++ = lobyte(n);
1179 *txtp++ = hibyte(n);
1183 /*)Function VOID out_t24(n)
1187 * The function out_t24() outputs the text (T)
1188 * data word as three bytes ordered according to hilo.
1191 * int * txtp pointer to txt array
1197 * int lobyte() asout.c
1198 * int hibyte() asout.c
1201 * Pointer to relocation buffer incremented by 3.
1209 *txtp++ = hibyte(n);
1210 *txtp++ = lobyte(n);
1212 *txtp++ = lobyte(n);
1213 *txtp++ = hibyte(n);
1218 /*)Function int lobyte(n)
1222 * The function lobyte() returns the lower byte of
1244 /*)Function int hibyte(n)
1248 * The function hibyte() returns the higher byte of
1267 return ((n>>8)&0377);
1270 /*)Function int byte3(n)
1274 * The function byte3() returns the MSB of the
1292 return ((n >> 16) & 0xff);
1296 * JLH: Output relocatable 11 bit jump/call
1298 * This function is derived from outrw(), adding the parameter for the
1299 * 11 bit address. This form of address is used only on the 8051 and 8048.
1303 register struct expr *esp;
1310 if (esp->e_flag==0 && esp->e_base.e_ap==NULL) {
1311 /* equated absolute destination. Assume value
1312 * relative to current area */
1313 esp->e_base.e_ap = dot.s_area;
1316 /* Relocatable destination. Build THREE
1317 * byte output: relocatable word, followed
1318 * by op-code. Linker will combine them.
1319 * Listing shows only the address.
1321 r |= R_WORD | esp->e_rlcf;
1322 out_lw(esp->e_addr,r|R_RELOC);
1325 out_tw(esp->e_addr);
1329 n = esp->e_base.e_sp->s_ref;
1332 n = esp->e_base.e_ap->a_ref;
1335 *relp++ = txtp - txt - 3;
1343 * Output relocatable 19 bit jump/call
1345 * This function is derived from outrw(), adding the parameter for the
1346 * 19 bit address. This form of address is used only in the DS80C390
1350 outr19(struct expr * esp, int op, int r)
1355 if (esp->e_flag==0 && esp->e_base.e_ap==NULL) {
1356 /* equated absolute destination. Assume value
1357 * relative to current area */
1358 esp->e_base.e_ap = dot.s_area;
1361 /* Relocatable destination. Build FOUR
1362 * byte output: relocatable 24-bit entity, followed
1363 * by op-code. Linker will combine them.
1364 * Listing shows only the address.
1366 r |= R_WORD | esp->e_rlcf;
1367 out_l24(esp->e_addr,r|R_RELOC);
1370 out_t24(esp->e_addr);
1374 n = esp->e_base.e_sp->s_ref;
1377 n = esp->e_base.e_ap->a_ref;
1380 *relp++ = txtp - txt - 4;