4 * (C) Copyright 1989-1995
20 * The module asout.c contains all the functions used to
21 * generate the .REL assembler output file.
24 * The assemblers' output object file is an ascii file containing
25 * the information needed by the linker to bind multiple object
26 * modules into a complete loadable memory image.
28 * The object module contains the following designators:
35 * H Most significant byte first
36 * L Least significant byte first
43 * R Relocation information
44 * P Paging information
49 * The first line of an object module contains the [XDQ][HL]
50 * format specifier (i.e. XH indicates a hexidecimal file with
51 * most significant byte first) for the following designators.
56 * H aa areas gg global symbols
58 * The header line specifies the number of areas(aa) and the
59 * number of global symbols(gg) defined or referenced in this ob-
60 * ject module segment.
67 * The module line specifies the module name from which this
68 * header segment was assembled. The module line will not appear
69 * if the .module directive was not used in the source program.
80 * The symbol line defines (Def) or references (Ref) the symbol
81 * 'string' with the value nnnn. The defined value is relative to
82 * the current area base address. References to constants and ex-
83 * ternal global symbols will always appear before the first area
84 * definition. References to external symbols will have a value of
90 * A label size ss flags ff
92 * The area line defines the area label, the size (ss) of the
93 * area in bytes, and the area flags (ff). The area flags specify
94 * the ABS, REL, CON, OVR, and PAG parameters:
96 * OVR/CON (0x04/0x00 i.e. bit position 2)
98 * ABS/REL (0x08/0x00 i.e. bit position 3)
100 * PAG (0x10 i.e. bit position 4)
105 * T xx xx nn nn nn nn nn ...
107 * The T line contains the assembled code output by the assem-
108 * bler with xx xx being the offset address from the current area
109 * base address and nn being the assembled instructions and data in
115 * R 0 0 nn nn n1 n2 xx xx ...
117 * The R line provides the relocation information to the linker.
118 * The nn nn value is the current area index, i.e. which area the
119 * current values were assembled. Relocation information is en-
120 * coded in groups of 4 bytes:
122 * 1. n1 is the relocation mode and object format
123 * 1. bit 0 word(0x00)/byte(0x01)
124 * 2. bit 1 relocatable area(0x00)/symbol(0x02)
125 * 3. bit 2 normal(0x00)/PC relative(0x04) relocation
126 * 4. bit 3 1-byte(0x00)/2-byte(0x08) object format for
128 * 5. bit 4 signed(0x00)/unsigned(0x10) byte data
129 * 6. bit 5 normal(0x00)/page '0'(0x20) reference
130 * 7. bit 6 normal(0x00)/page 'nnn'(0x40) reference
131 * 8. bit 7 normal(0x00)/MSB of value
133 * 2. n2 is a byte index into the corresponding (i.e. pre-
134 * ceeding) T line data (i.e. a pointer to the data to be
135 * updated by the relocation). The T line data may be
136 * 1-byte or 2-byte byte data format or 2-byte word
139 * 3. xx xx is the area/symbol index for the area/symbol be-
140 * ing referenced. the corresponding area/symbol is found
141 * in the header area/symbol lists.
144 * The groups of 4 bytes are repeated for each item requiring relo-
145 * cation in the preceeding T line.
150 * P 0 0 nn nn n1 n2 xx xx
152 * The P line provides the paging information to the linker as
153 * specified by a .setdp directive. The format of the relocation
154 * information is identical to that of the R line. The correspond-
155 * ing T line has the following information:
156 * T xx xx aa aa bb bb
158 * Where aa aa is the area reference number which specifies the
159 * selected page area and bb bb is the base address of the page.
160 * bb bb will require relocation processing if the 'n1 n2 xx xx' is
161 * specified in the P line. The linker will verify that the base
162 * address is on a 256 byte boundary and that the page length of an
163 * area defined with the PAG type is not larger than 256 bytes.
165 * The linker defaults any direct page references to the first
166 * area defined in the input REL file. All ASxxxx assemblers will
167 * specify the _CODE area first, making this the default page area.
170 * asout.c contains the following functions:
191 * The module asout.c contains the following local variables:
192 * int rel[] relocation data for code/data array
193 * int * relp pointer to rel array
194 * int txt[] assembled code/data array
195 * int * txtp pointer to txt array
204 char *txtp = { &txt[0] };
205 char *relp = { &rel[0] };
207 /*)Function VOID outab(b)
209 * int b assembler data word
211 * The function outab() processes a single word of
212 * assembled data in absolute format.
215 * int * txtp pointer to data word
218 * int oflag -o, generate relocatable output flag
219 * int pass assembler pass number
222 * VOID outchk() asout.c
223 * VOID out_lb() asout.c
226 * The current assembly address is incremented by 1.
242 /*)Function VOID outaw(w)
244 * int w assembler data word
246 * The function outaw() processes a single word of
247 * assembled data in absolute format.
250 * int * txtp pointer to data word
253 * int oflag -o, generate relocatable output flag
254 * int pass assembler pass number
257 * VOID outchk() asout.c
258 * VOID out_lw() asout.c
261 * The current assembly address is incremented by 2.
277 /*)Function VOID outrb(esp, r)
279 * expr * esp pointer to expr structure
280 * int r relocation mode
282 * The function outrb() processes a byte of generated code
283 * in either absolute or relocatable format dependent upon
284 * the data contained in the expr structure esp. If the
285 * .REL output is enabled then the appropriate information
286 * is loaded into the txt and rel buffers.
289 * int n symbol/area reference number
290 * int * relp pointer to rel array
291 * int * txtp pointer to txt array
294 * sym dot defined as sym[0]
295 * int oflag -o, generate relocatable output flag
296 * int pass assembler pass number
299 * VOID aerr() assubr.c
300 * VOID outchk() asout.c
301 * VOID out_lb() asout.c
302 * VOID out_rb() asout.c
303 * VOID out_tb() asout.c
306 * The current assembly address is incremented by 1.
311 register struct expr *esp;
317 if (esp->e_flag==0 && esp->e_base.e_ap==NULL) {
318 out_lb(lobyte(esp->e_addr),0);
321 *txtp++ = lobyte(esp->e_addr);
324 r |= R_BYTE | R_BYT2 | esp->e_rlcf;
326 out_lb(hibyte(esp->e_addr),r|R_RELOC|R_HIGH);
328 out_lb(lobyte(esp->e_addr),r|R_RELOC);
334 n = esp->e_base.e_sp->s_ref;
337 n = esp->e_base.e_ap->a_ref;
340 *relp++ = txtp - txt - 2;
348 /*)Function VOID outrw(esp, r)
350 * expr * esp pointer to expr structure
351 * int r relocation mode
353 * The function outrw() processes a word of generated code
354 * in either absolute or relocatable format dependent upon
355 * the data contained in the expr structure esp. If the
356 * .REL output is enabled then the appropriate information
357 * is loaded into the txt and rel buffers.
360 * int n symbol/area reference number
361 * int * relp pointer to rel array
362 * int * txtp pointer to txt array
365 * sym dot defined as sym[0]
366 * int oflag -o, generate relocatable output flag
367 * int pass assembler pass number
370 * VOID aerr() assubr.c
371 * VOID outchk() asout.c
372 * VOID out_lw() asout.c
373 * VOID out_rw() asout.c
374 * VOID out_tw() asout.c
377 * The current assembly address is incremented by 2.
382 register struct expr *esp;
388 if (esp->e_flag==0 && esp->e_base.e_ap==NULL) {
389 out_lw(esp->e_addr,0);
395 r |= R_WORD | esp->e_rlcf;
399 out_lw(hibyte(esp->e_addr),r|R_RELOC);
401 out_lw(lobyte(esp->e_addr),r|R_RELOC);
404 out_lw(esp->e_addr,r|R_RELOC);
410 n = esp->e_base.e_sp->s_ref;
413 n = esp->e_base.e_ap->a_ref;
416 *relp++ = txtp - txt - 2;
424 /*)Function VOID outdp(carea, esp)
426 * area * carea pointer to current area strcuture
427 * expr * esp pointer to expr structure
429 * The function outdp() flushes the output buffer and
430 * outputs paging information to the .REL file.
433 * int n symbol/area reference number
434 * int r relocation mode
435 * int * relp pointer to rel array
436 * int * txtp pointer to txt array
439 * int oflag -o, generate relocatable output flag
440 * int pass assembler pass number
443 * VOID outbuf() asout.c
444 * VOID outchk() asout.c
445 * VOID out_rw() asout.c
446 * VOID out_tw() asout.c
449 * Output buffer flushed to .REL fiel.
450 * Paging information dumped to .REL file.
455 register struct area *carea;
456 register struct expr *esp;
460 if (oflag && pass==2) {
462 out_tw(carea->a_ref);
464 if (esp->e_flag || esp->e_base.e_ap!=NULL) {
467 n = esp->e_base.e_sp->s_ref;
470 n = esp->e_base.e_ap->a_ref;
473 *relp++ = txtp - txt - 2;
480 /*)Function VOID outall()
482 * The function outall() will output any bufferred assembled
483 * data and relocation information (during pass 2 if the .REL
484 * output has been enabled).
490 * int oflag -o, generate relocatable output flag
491 * int pass assembler pass number
494 * VOID outbuf() asout.c
497 * assembled data and relocation buffers will be cleared.
503 if (oflag && pass==2)
507 /*)Function VOID outdot()
509 * The function outdot() outputs information about the
510 * current program counter value (during pass 2 if the .REL
511 * output has been enabled).
517 * int oflag -o, generate relocatable output flag
518 * int pass assembler pass number
521 * int fprintf() c_library
525 * assembled data and relocation buffers will be cleared.
531 if (oflag && pass==2) {
533 out(txt,(int) (txtp-txt));
536 out(rel,(int) (relp-rel));
543 /*)Function outchk(nt, nr)
545 * int nr number of additional relocation words
546 * int nt number of additional data words
548 * The function outchk() checks the data and relocation buffers
549 * for space to insert the nt data words and nr relocation words.
550 * If space is not available then output the current data and
551 * initialize the data buffers to receive the new data.
554 * area * ap pointer to an area structure
555 * int * relp pointer to rel array
556 * int * txtp pointer to txt array
559 * sym dot defined as sym[0]
562 * VOID outbuf() asout.c
565 * Data and relocation buffers may be emptied and initialized.
571 register struct area *ap;
573 if (txtp+nt > &txt[NTXT] || relp+nr > &rel[NREL]) {
578 if ((ap = dot.s_area) != NULL) {
579 *relp++ = R_WORD|R_AREA;
586 /*)Function VOID outbuf()
588 * The function outbuf() will output any bufferred data
589 * and relocation information to the .REL file. The output
590 * buffer pointers and counters are initialized.
593 * int rel[] relocation data for code/data array
594 * int * relp pointer to rel array
595 * int txt[] assembled code/data array
596 * int * txtp pointer to txt array
599 * FILE * ofp relocation output file handle
605 * All bufferred data written to .REL file and
606 * buffer pointers and counters initialized.
613 if (txtp > &txt[2]) {
615 out(txt,(int) (txtp-txt));
618 out(rel,(int) (relp-rel));
625 /*)Function VOID outgsd()
627 * The function outgsd() performs the following:
628 * (1) outputs the .REL file radix
629 * (2) outputs the header specifying the number
630 * of areas and global symbols
631 * (3) outputs the module name
632 * (4) set the reference number and output a symbol line
633 * for all external global variables and absolutes
634 * (5) output an area name, set reference number and output
635 * a symbol line for all global relocatables in the area.
636 * Repeat this proceedure for all areas.
639 * area * ap pointer to an area structure
640 * sym * sp pointer to a sym structure
643 * int c string character value
644 * int narea number of code areas
645 * char * ptr string pointer
646 * int nglob number of global symbols
647 * int rn symbol reference number
650 * area * areap pointer to an area structure
651 * char module[] module name string
652 * sym * symhash[] array of pointers to NHASH
653 * linked symbol lists
654 * int xflag -x, listing radix flag
657 * int fprintf() c_library
658 * VOID outarea() asout.c
659 * VOID outsym() asout.c
660 * int putc() c_library
663 * All symbols are given reference numbers, all symbol
664 * and area information is output to the .REL file.
670 register struct area *ap;
671 register struct sym *sp;
674 int c, narea, nglob, rn;
679 narea = areap->a_ref + 1;
682 * Number of global references/absolutes
685 for (i = 0; i < NHASH; ++i) {
688 if (sp->s_flag&S_GBL)
695 * Output Radix and number of areas and symbols
698 fprintf(ofp, "X%c\n", hilo ? 'H' : 'L');
699 fprintf(ofp, "H %X areas %X global symbols\n", narea, nglob);
702 fprintf(ofp, "Q%c\n", hilo ? 'H' : 'L');
703 fprintf(ofp, "H %o areas %o global symbols\n", narea, nglob);
706 fprintf(ofp, "D%c\n", hilo ? 'H' : 'L');
707 fprintf(ofp, "H %u areas %u global symbols\n", narea, nglob);
716 while (ptr < &module[NCPS]) {
717 if ((c = *ptr++) != 0)
724 * Global references and absolutes.
727 for (i=0; i<NHASH; ++i) {
730 if (sp->s_area==NULL && sp->s_flag&S_GBL) {
739 * Global relocatables.
741 for (i=0; i<narea; ++i) {
743 while (ap->a_ref != i)
746 for (j=0; j<NHASH; ++j) {
749 if (sp->s_area==ap && sp->s_flag&S_GBL) {
759 /*)Function VOID outarea(ap)
761 * area * ap pointer to an area structure
763 * The function outarea() outputs the A line to the .REL
764 * file. The A line contains the area's name, size, and
768 * char * ptr pointer to area id string
769 * int c character value
772 * FILE * ofp relocation output file handle
773 * int xflag -x, listing radix flag
776 * int fprintf() c_library
777 * int putc() c_library
780 * The A line is sent to the .REL file.
785 register struct area *ap;
792 while (ptr < &ap->a_id[NCPS]) {
793 if ((c = *ptr++) != 0)
797 fprintf(ofp, " size %X flags %X\n", ap->a_size, ap->a_flag);
800 fprintf(ofp, " size %o flags %o\n", ap->a_size, ap->a_flag);
803 fprintf(ofp, " size %u flags %u\n", ap->a_size, ap->a_flag);
807 /*)Function VOID outsym(sp)
809 * sym * sp pointer to a sym structure
811 * The function outsym() outputs the S line to the .REL
812 * file. The S line contains the symbols name and whether the
813 * the symbol is defined or referenced.
816 * char * ptr pointer to symbol id string
817 * int c character value
820 * FILE * ofp relocation output file handle
821 * int xflag -x, listing radix flag
824 * int fprintf() c_library
825 * int putc() c_library
828 * The S line is sent to the .REL file.
833 register struct sym *sp;
840 while (ptr < &sp->s_id[NCPS]) {
841 if ((c = *ptr++) != 0)
844 fprintf(ofp, " %s", sp->s_type==S_NEW ? "Ref" : "Def");
846 fprintf(ofp, "%04X\n", sp->s_addr);
849 fprintf(ofp, "%06o\n", sp->s_addr);
852 fprintf(ofp, "%05u\n", sp->s_addr);
856 /*)Function VOID out(p, n)
858 * int n number of words to output
859 * int * p pointer to data words
861 * The function out() outputs the data words to the .REL file
862 * int the specified radix.
868 * FILE * ofp relocation output file handle
869 * int xflag -x, listing radix flag
872 * int fprintf() c_library
875 * Data is sent to the .REL file.
885 fprintf(ofp, " %02X", (*p++)&0377);
888 fprintf(ofp, " %03o", (*p++)&0377);
891 fprintf(ofp, " %03u", (*p++)&0377);
896 /*)Function VOID out_lb(b, t)
898 * int b assembled data
899 * int t relocation type
901 * The function out_lb() copies the assembled data and
902 * its relocation type to the list data buffers.
908 * int * cp pointer to assembler output array cb[]
909 * int * cpt pointer to assembler relocation type
916 * Pointers to data and relocation buffers incremented by 1.
923 if (cp < &cb[NCODE]) {
929 /*)Function VOID out_lw(n, t)
931 * int n assembled data
932 * int t relocation type
934 * The function out_lw() copies the assembled data and
935 * its relocation type to the list data buffers.
941 * int * cp pointer to assembler output array cb[]
942 * int * cpt pointer to assembler relocation type
949 * Pointers to data and relocation buffers incremented by 2.
957 out_lb(hibyte(n),t ? t|R_HIGH : 0);
961 out_lb(hibyte(n),t ? t|R_HIGH : 0);
965 /*)Function VOID out_rw(n)
969 * The function out_rw() outputs the relocation (R)
970 * data word as two bytes ordered according to hilo.
973 * int * relp pointer to rel array
979 * int lobyte() asout.c
980 * int hibyte() asout.c
983 * Pointer to relocation buffer incremented by 2.
999 /*)Function VOID out_tw(n)
1003 * The function out_tw() outputs the text (T)
1004 * data word as two bytes ordered according to hilo.
1007 * int * txtp pointer to txt array
1013 * int lobyte() asout.c
1014 * int hibyte() asout.c
1017 * Pointer to relocation buffer incremented by 2.
1025 *txtp++ = hibyte(n);
1026 *txtp++ = lobyte(n);
1028 *txtp++ = lobyte(n);
1029 *txtp++ = hibyte(n);
1033 /*)Function int lobyte(n)
1037 * The function lobyte() returns the lower byte of
1059 /*)Function int hibyte(n)
1063 * The function hibyte() returns the higher byte of
1082 return ((n>>8)&0377);