annotate docs/ccguide/chap2.chapter @ 618:7a4d7a896b8f

Chapter 2 finished
author roug
date Sat, 30 Nov 2002 09:44:49 +0000
parents 0e84dcd81835
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
1 <chapter>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
2 <title>Characteristics of Compiled Programs</title>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
3
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
4 <section>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
5 <title>The Object Code Module</title>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
6 <para>
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
7 The compiler produces position-independent, reentrant 6809 code
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
8 in a standard OS-9 memory module format. The format of an executable
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
9 program module is shown below. Detailed descriptions of each
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
10 section of the module are given on following pages.
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
11 </para>
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
12 <informalfigure>
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
13 <screen>
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
14 Module Section
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
15 Offset Size (bytes)
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
16
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
17 +-------------------------------+
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
18 $00 ! !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
19 ! Module Header ! 8
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
20 ! !
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
21 !-------------------------------!
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
22 $09 ! Execution Offset ! ---+ 2
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
23 !-------------------------------! !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
24 $0B ! Permanent Storage Size ! ! 2
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
25 !-------------------------------! !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
26 $0D ! Module Name ! !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
27 ! . . . . . . . . . . . . . . . ! !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
28 v v &lt;--+
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
29 : Executable code :
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
30 : . . . . . . . . . . . . . . . :
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
31 : String Literals :
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
32 ^ ^
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
33 !-------------------------------!
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
34 ! Initializing Data Size ! 2
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
35 !-------------------------------!
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
36 v v
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
37 : Initializing Data :
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
38 ^ ^
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
39 !-------------------------------!
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
40 ! Data-text Reference Count ! 2
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
41 !-------------------------------!
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
42 v v
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
43 : Data-text Reference Offsets :
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
44 ^ ^
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
45 !-------------------------------!
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
46 ! Data-data Reference Count ! 2
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
47 !-------------------------------!
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
48 v v
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
49 : Data-data Reference Offsets :
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
50 ^ ^
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
51 !-------------------------------!
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
52 ! CRC Check Value ! 3
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
53 !-------------------------------!
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
54 </screen>
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
55 </informalfigure>
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
56 <section>
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
57 <title>Module Header</title>
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
58 <para>
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
59 This is a standard module header with the type/language byte set to
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
60 $11 (Program + 6809 Object Code), and the attribute/revision byte
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
61 set to $81 (Reentrant + 1).
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
62 </para>
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
63 </section>
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
64
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
65 <section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
66 <title>Execution Offset</title>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
67 <para>
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
68 Used by OS-9 to locate where to start execution of the program.
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
69 </para>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
70 </section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
71
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
72 <section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
73 <title>Storage Size</title>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
74 <para>
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
75 Storage size is the initial default allocation of memory for data,
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
76 stack, and parameter area. For a full description of memory
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
77 allowcation, see the section entitled
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
78 <quote><link linkend="memorymanagement">Memory Management</link></quote> located
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
79 elsewhere in this manual.
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
80 </para>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
81 </section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
82
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
83 <section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
84 <title>Module Name</title>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
85 <para>
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
86 Module name is used by OS-9 to enter the module in the module
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
87 directory. The module name is followed by the edition byte encoded
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
88 in cstart. If this situation is not desired it may be overridden by
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
89 the -E= option in cc.
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
90 </para>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
91 </section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
92
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
93 <section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
94 <title>Information</title>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
95 <para>
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
96 Any strings preceded by the directive "info" in an assembly code
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
97 file will be placed here. A major use of this facility is to place
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
98 in the module the version number and/or a copyright notice. Note
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
99 that the '#asm' pre-compiler instruction may be used in a C source
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
100 file to enable the inclusion of this directive in the compiler-generated
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
101 assembly code file.
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
102 </para>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
103 </section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
104
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
105 <section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
106 <title>Executable Code</title>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
107 <para>
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
108 The machine code instructions of the program.
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
109 </para>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
110 </section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
111
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
112 <section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
113 <title>String Literals</title>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
114 <para>
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
115 Quoted string in the C source are placed here. They are in the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
116 null-terminated form expected by the functions in the Standard
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
117 Library. NOTE: the definition of the C language assumes that
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
118 strings are in the DATA area and are therefore subject to alteration
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
119 without making the program non-reentrant. However, in order to avoid
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
120 the dublication of memory requirements which would be necesary if
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
121 they were to be in the data area, they are placed in the TEXT
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
122 (executable) section of the module. Putting the strings in the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
123 executable section implies that no attempt should be made by a C
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
124 programmer to alter string literals. They should be copied out
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
125 first. The exception that proves the rule is the initialization of
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
126 an array of type char like this:
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
127 <programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
128 char message[] = "Hello world\n";
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
129 </programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
130 The string will be found in the array 'message' in the data area and
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
131 can be altered.
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
132 </para>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
133 </section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
134
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
135 <section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
136 <title>Initializing Data and its Size</title>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
137 <para>
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
138 If a C program contains initializers, the data for the initial
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
139 values of the variables is placed in this section. The definition
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
140 of C states that all uninitialized global and static variables have
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
141 the value zero when the program starts running, so the startuo
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
142 rougtine of each C program first copies the data from the module into
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
143 the data area and then clears the rest of the data memory to nulls.
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
144 </para>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
145 </section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
146
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
147 <section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
148 <title>Data References</title>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
149 <para>
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
150 No absolute addresses are known at compile time under OS-9, so where
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
151 there are pointer values in the initializating data, they must be
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
152 adjusted at run time so that they reflect the absolute values at
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
153 that time. The startup routine uses the two data reference tables
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
154 to locate the values that need alteration and adjusts them by the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
155 absolute values of the bases of the executable code and data
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
156 respectively.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
157 </para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
158 <para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
159 For example, suppose there are the following statements in the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
160 program being compiled:
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
161 <programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
162 char *p = "I'm a string!";
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
163 char **q = &amp;p;
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
164 </programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
165 These declarations tell the compiler that there is to be a char
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
166 pointer variable, 'p', whose initial value is the address of the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
167 string and a pointer to a char pointer, 'q', whose initial value is
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
168 the address of 'p'. The variables must be in the DATA section of
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
169 memory at run time because they are potentially alterable, but
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
170 absolute addresses are not known until run time, so the values that
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
171 'p' and 'q' must have are not known at compile time. The string
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
172 will be placed by the compiler in the TEXT section and will not be
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
173 copied out to DATA memory by the startup routine. The initializing
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
174 data section of the program module will contain entries for 'p' and
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
175 'q'. They will have as values the offsets of the string from the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
176 base of the TEXT section and the offset of the location of 'p' from
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
177 the base of the DATA section respectively.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
178 </para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
179 <para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
180 The startup routine will first copy all the entries in the initializing
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
181 data section into their allotted places in the DATA section.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
182 Then it will scan the data-text reference table for the offsets of
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
183 values that need to have the addresses of the base of the TEXT
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
184 section added to them. Among these will be the "p" which, after
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
185 updating, will point to the string which is in the TEXT section.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
186 Similarly, after a scan of the data-data references, "q" will point
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
187 to (contain the absolute of) "p".
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
188 </para>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
189 </section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
190 </section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
191
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
192 <section id="memorymanagement">
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
193 <title>Memory Management</title>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
194 <para>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
195 The C compiler and its support programs have default conditions
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
196 such that the average programmer need not be concerned with details
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
197 of memory management. However, there are situations where advanced
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
198 programmers may wish to tailor the storage allocation of a program
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
199 for special situations. The following information explains in
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
200 detail how a C program's data area is allocated and used.
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
201 </para>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
202
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
203 <section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
204 <title>Typical C Program Memory Map</title>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
205 <para>
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
206 A storage area is allocated by OS-9 when the C program is executed.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
207 The layout of this memory is as follows:
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
208 </para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
209 <screen>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
210 high addresses
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
211 | | &lt;- SBRK() adds more
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
212 | | memory here
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
213 | |
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
214 !------------------! &lt;- memend
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
215 ! parameters !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
216 !------------------!
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
217 ! !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
218 Current stack | stack | &lt;- sp register
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
219 reservation -> !..................!
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
220 ! v !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
221 ! ! &lt;- standard I/O buffers
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
222 ! free memory ! allocated here
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
223 Current top ! !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
224 of data -> !........^.........! &lt;- IBRK() changes this
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
225 ! ! memory bound upward
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
226 ! requested memory !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
227 !------------------! &lt;-- end
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
228 ! uninitialized !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
229 ! data !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
230 !------------------! &lt;-- edata
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
231 ! initialized !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
232 ! data !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
233 !------------------!
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
234 ^ ! direct page !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
235 dpsiz ! variables !
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
236 v +------------------+ &lt;-- y,dp registers
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
237 low addresses
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
238 </screen>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
239 <para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
240 The overall size of this memory area is defined by the "storage
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
241 size" value stored in the program's module header. This can be
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
242 overridden to assign the program additional memory if the OS-9 Shell
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
243 "#" command is used.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
244 </para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
245 <para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
246 The parameter area is where the parameter string from the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
247 calling process (typically the OS-9 Shell) is placed by the system.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
248 The initializing routine for C programs converts the parameters into
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
249 null-terminated strings and makes pointers to them available to
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
250 'main()' via 'argc' and 'argv'.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
251 </para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
252 <para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
253 The stack area is the currently reserved memory for exclusive
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
254 use of the stack. As each C function is entered, a routine in the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
255 system interface is called to reserve enough stack space for the use
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
256 of the function with an addition of 64 bytes. The 64 bytes are for
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
257 the use of user-written assembly code functions and/or the system
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
258 interface and/or arithmetic routines. A record is kept of the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
259 lowest address so far granted for the stack. If the area requested
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
260 would not bring this lower then the C function is allowed to
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
261 proceed. If the new lower limit would mean that the stack area
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
262 would overlap the data area, the program stops with the message:
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
263 <screen>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
264 **** STACK OVERFLOW ****
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
265 </screen>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
266 on the standard error output. Otherwise, the new lower limit is
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
267 set, and the C function resumes as before.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
268 </para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
269 <para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
270 The direct page variables area is where variables reside
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
271 that have been defined with the storage class 'direct' in the C
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
272 source code or in the 'direct' segment in assembly code source.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
273 Notice that the size of this area is always at least one byte (to
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
274 ensure that no pointer to a variable can have the value NULL or 0)
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
275 and that it is not necessarily 256 bytes.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
276 </para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
277 <para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
278 The uninitialized data area is where the remainder of the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
279 uninitialized program variables reside. These two areas are, in
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
280 fact, cleared to all zeros by the program entry routine. The
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
281 initialized data area is where the initialized variables of the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
282 program reside. There are two globally defined values which may be
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
283 referred to: 'edata' and 'end', which are the addresses of one byte
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
284 higher than the initialized data and one byte higher than the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
285 uninitialized data respectively. Note that these are not variables;
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
286 the values are accesses in C by using the '&amp;' operator as in:
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
287 <programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
288 high = &amp;end;
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
289 low = &amp;edata;
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
290 </programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
291 and in assembler:
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
292 <programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
293 leax end,y
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
294 stx high,y
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
295 </programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
296 The Y register points to the base of the data area and variables are
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
297 addresses using Y-offset indexed instructions.
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
298 </para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
299 <para>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
300 When the program starts running, the remaining memory is
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
301 assigned to the "free" area. A program may call "ibrk()" to
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
302 request additional working memory (initialized to zeros) from the
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
303 free memory area. Alternatively, more memory can be dynamically
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
304 obtained using the "sbrk()" which requests additional memory from
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
305 the operating system and returns its lower bound. If this fails
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
306 because OS-9 refuses to grant more memory for any reason "sbrk()"
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
307 will return -1.
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
308 </para>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
309 </section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
310
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
311 <section>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
312 <title>Compile Time Memory Allocation</title>
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
313 <para>
618
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
314 If not instructed otherwise, the linker will automatically
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
315 allocate 1k bytes more than the total size of the program's
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
316 variables and strings. This size will normally be adequate to cover
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
317 the parameter area, stack requirements, and Standard Library file
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
318 buffers. The allocation size may be altered when using the compiler
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
319 by using the "-m" option on the command line. The memory
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
320 requirements may be stated in pages, for example,
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
321 <programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
322 cc prg.c -m=2
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
323 </programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
324 which allocates 512 bytes extra, or in kilobytes, for example:
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
325 <programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
326 cc prg.c -m=10k
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
327 </programlisting>
7a4d7a896b8f Chapter 2 finished
roug
parents: 565
diff changeset
328 The linker will ignore the request if the size is less than 256 bytes.
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
329 </para>
565
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
330 <para>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
331 The following rules can serve as a rough guide to estimate how
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
332 much memory to specify:
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
333 </para>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
334 <orderedlist>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
335 <listitem><para>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
336 The parameter area should be large enough for any anticipated
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
337 command line string.
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
338 </para></listitem>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
339 <listitem><para>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
340 The stack should not be less than 128 bytes and should take
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
341 into account the depth of function calling chains and any
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
342 recursion.
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
343 </para></listitem>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
344 <listitem><para>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
345 All function arguments and local variables occupy stack space
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
346 and each function entered needs 4 bytes more for the return
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
347 address and temporary storage of the calling function's register
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
348 variable.
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
349 </para></listitem>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
350 <listitem><para>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
351 Free memory is requested by the Standard Library I/O
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
352 functions for buffers at the rate of 256 bytes per accessed
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
353 file. The does not apply to the lower level service request I/O
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
354 functions such as "open()", "read()" or "write()" not to
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
355 "stderr" which is always un-buffered, but it does apply to both
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
356 "stdin" and "stdout" (see the Standard Library documentation).
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
357 </para></listitem>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
358 </orderedlist>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
359 <para>
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
360 A good method for getting a feel for how much memory is
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
361 needed by your program is to allow the linker to set the memory size
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
362 to its usually conservative value. Then, if the program runs
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
363 with a variety of input satisfactorily but memory is limited on the
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
364 system, try reducing the allocation at the next compilation. If a
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
365 stack overflow occurs or an "ibrk()" call returns -1, then try
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
366 increasing the memory next time. You cannot damage the system by
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
367 getting it wrong, but data may be lost if the program runs out of
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
368 space at a crucial time. It pays to be in error on the generous
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
369 side.
0e84dcd81835 Several functions ready.
roug
parents: 530
diff changeset
370 </para>
530
c43b678c0055 Mainly section structures added.
roug
parents: 468
diff changeset
371 </section>
468
60b821f18853 A little here and there.
roug
parents: 466
diff changeset
372 </section>
466
bea58398bb15 Skeletons for the C Compiler User's Guide, 1983
roug
parents:
diff changeset
373 </chapter>