Commit | Line | Data |
---|---|---|
3edcb7c8 KB |
1 | .\" Copyright (c) 1980 The Regents of the University of California. |
2 | .\" All rights reserved. | |
86d6ee2d | 3 | .\" |
3edcb7c8 KB |
4 | .\" %sccs.include.redist.man% |
5 | .\" | |
6 | .\" @(#)puman2.n 6.3 (Berkeley) %G% | |
86d6ee2d | 7 | .\" |
86d6ee2d KM |
8 | .if !\n(xx \{\ |
9 | .so tmac.p \} | |
10 | 'if n 'ND | |
11 | .nr H1 1 | |
12 | .NH | |
13 | Basic UNIX Pascal | |
14 | .PP | |
15 | The following sections | |
16 | explain the basics of using | |
17 | .UP . | |
18 | In examples here we use the text editor | |
19 | .I ex | |
20 | (1). | |
21 | Users of the text editor | |
22 | .I ed | |
23 | should have little trouble following these examples, | |
24 | as | |
25 | .I ex | |
26 | is similar to | |
27 | .I ed . | |
28 | We use | |
29 | .I ex | |
30 | because it | |
31 | allows us to make clearer examples.\(dg | |
32 | .FS | |
33 | \(dg Users with \s-2CRT\s0 terminals should find the editor | |
34 | .I vi | |
35 | more pleasant to use; | |
36 | we do not show its use here because its display oriented nature | |
37 | makes it difficult to illustrate. | |
38 | .FE | |
39 | The new | |
40 | .UX | |
41 | user will find it helpful to read one of the text editor documents | |
42 | described in section 1.4 before continuing with this section. | |
43 | .NH 2 | |
44 | A first program | |
45 | .PP | |
46 | To prepare a program for | |
47 | .UP | |
48 | we first need to have an account on | |
49 | .UX | |
50 | and to `login' | |
51 | to the system on this account. | |
52 | These procedures are described in the documents | |
53 | .I "Communicating with UNIX" | |
54 | and | |
55 | .I "UNIX for Beginners". | |
56 | .PP | |
57 | Once we are logged in we need to choose a name for our program; | |
58 | let us call it `first' as this is the first example. | |
59 | We must also choose a name for the file in which the program will be stored. | |
60 | The | |
61 | .UP | |
62 | system requires that programs reside in files which have names ending with | |
63 | the sequence `.p' so we will call our file `first.p'. | |
64 | .PP | |
65 | A sample editing session to create this file would begin: | |
66 | .LS | |
67 | % \*bex first.p\fR | |
68 | "first.p" [New file] | |
69 | : | |
70 | .LE | |
71 | We didn't expect the file to exist, so the error diagnostic doesn't | |
72 | bother us. | |
73 | The editor now knows the name of the file we are creating. | |
74 | The `:' prompt indicates that it is ready for command input. | |
75 | We can add the text for our program using the `append' | |
76 | command as follows. | |
77 | .LS | |
78 | :\*bappend\fR | |
79 | .B | |
80 | program first(output) | |
81 | begin | |
82 | writeln('Hello, world!') | |
83 | end. | |
84 | \&. | |
85 | .R | |
86 | : | |
87 | .LE | |
88 | The line containing the single `\*b.\fR' character here indicated | |
89 | the end of the appended text. | |
90 | The `:' prompt indicates that | |
91 | .I ex | |
92 | is ready for another command. | |
93 | As the editor operates in a temporary work space we must now store the contents | |
94 | of this work space in the file `first.p' | |
95 | so we can use the Pascal | |
96 | translator and executor | |
97 | .IX | |
98 | on it. | |
99 | .LS | |
100 | :\*bwrite\fR | |
101 | "first.p" [New file] 4 lines, 59 characters | |
102 | :\*bquit\fR | |
103 | % | |
104 | .LE | |
105 | We wrote out the file from the edit buffer here with the | |
106 | `write' | |
107 | command, and | |
108 | .I ex | |
109 | indicated the number of lines and characters written. | |
110 | We then quit the editor, and now have a prompt from the shell.\(dd | |
111 | .FS | |
112 | \(dd Our examples here assume you are using | |
113 | .I csh. | |
114 | .FE | |
115 | .KS | |
116 | .PP | |
117 | We are ready to try | |
118 | to translate and execute our program. | |
119 | .DS | |
120 | .tr '\(aa^\(ua | |
121 | % \*bpix first.p\fR | |
122 | .so firstout | |
123 | .tr ''^^ | |
124 | % | |
125 | .DE | |
126 | .KE | |
127 | .PP | |
128 | The translator first printed a syntax error diagnostic. | |
129 | The number 2 here indicates that the rest of the line is an image | |
130 | of the second line of our program. | |
131 | The translator is saying that it expected to find a `;' before the | |
132 | keyword | |
133 | .B begin | |
134 | on this line. | |
135 | If we look at the Pascal syntax charts in the Jensen-Wirth | |
136 | .I "User Manual" , | |
137 | or at some of the sample programs therein, we will see that | |
138 | we have omitted the terminating `;' of the | |
139 | .B program | |
140 | statement on the first | |
141 | line of our program. | |
142 | .PP | |
143 | One other thing to notice about the error diagnostic is the letter `e' | |
144 | at the beginning. | |
145 | It stands for `error', | |
146 | indicating that our input was not legal Pascal. | |
147 | The fact that it is an `e' rather than an `E' | |
148 | indicates that the translator managed to recover from this error well | |
149 | enough that generation of code and execution could take place. | |
150 | Execution is possible whenever no fatal `E' errors | |
151 | occur during translation. | |
152 | The other classes of diagnostics are `w' warnings, | |
153 | which do not necessarily indicate errors in the program, | |
154 | but point out inconsistencies which are likely to be due to program bugs, | |
155 | and `s' standard-Pascal violations.\*(dg | |
156 | .FS | |
157 | \*(dgThe standard Pascal warnings occur only when the associated | |
158 | .B s | |
159 | translator option is enabled. | |
160 | The | |
161 | .B s | |
162 | option is discussed in sections 5.1 and A.6 below. | |
163 | Warning diagnostics are discussed at the end of section 3.2, | |
164 | the associated | |
165 | .B w | |
166 | option is described in section 5.2. | |
167 | .FE | |
168 | .PP | |
169 | After completing the translation of the program to interpretive code, | |
170 | the Pascal system indicates that execution of the translated program began. | |
171 | The output from the execution of the program then appeared. | |
172 | At program termination, the Pascal runtime system indicated the | |
173 | number of statements executed, and the amount of cpu time | |
174 | used, with the resolution of the latter being 1/60'th of a second. | |
175 | .PP | |
176 | Let us now fix the error in the program and translate it to a permanent | |
177 | object code file | |
178 | .I obj | |
179 | using | |
180 | .PI . | |
181 | The program | |
182 | .PI | |
183 | translates Pascal programs but stores the object code instead of executing it\*(dd. | |
184 | .FS | |
185 | \*(ddThis script indicates some other useful approaches to debugging | |
186 | Pascal programs. | |
187 | As in | |
188 | .I ed | |
189 | we can shorten commands in | |
190 | .I ex | |
191 | to an initial prefix of the command name as we did | |
192 | with the | |
193 | .I substitute | |
194 | command here. | |
195 | We have also used the `!' shell escape command here to execute other | |
196 | commands with a shell without leaving the editor. | |
197 | .FE | |
198 | .LS | |
199 | % \*bex first.p\fR | |
200 | "first.p" 4 lines, 59 characters | |
201 | :\*b1 print\fR | |
202 | program first(output) | |
203 | :\*bs/$/;\fR | |
204 | program first(output); | |
205 | :\*bwrite\fR | |
206 | "first.p" 4 lines, 60 characters | |
207 | :\*bquit\fR | |
208 | % \*bpi first.p\fR | |
209 | % | |
210 | .LE | |
211 | If we now use the | |
212 | .UX | |
213 | .I ls | |
214 | list files command we can see what files we have: | |
215 | .LS | |
216 | % \*bls\fR | |
217 | first.p | |
218 | obj | |
219 | % | |
220 | .LE | |
221 | The file `obj' here contains the Pascal interpreter code. | |
222 | We can execute this by typing: | |
223 | .LS | |
224 | % \*bpx obj\fR | |
225 | .so firstobjout | |
226 | % | |
227 | .LE | |
228 | Alternatively, the command: | |
229 | .LS | |
230 | % \*bobj\fR | |
231 | .LE | |
232 | will have the same effect. | |
233 | Some examples of different ways to execute the program follow. | |
234 | .LS | |
235 | % \*bpx\fR | |
236 | .so firstobjout | |
237 | % \*bpi -p first.p\fR | |
238 | % \*bpx obj\fR | |
239 | .so firstobjout2 | |
240 | % \*bpix -p first.p\fR | |
241 | .so firstobjout2 | |
242 | % | |
243 | .LE | |
244 | .PP | |
245 | Note that | |
246 | .I px | |
247 | will assume that `obj' is the file we wish to execute | |
248 | if we don't tell it otherwise. | |
249 | The last two translations use the | |
250 | .B \-p | |
251 | no-post-mortem option to eliminate | |
252 | execution statistics and | |
253 | `Execution begins' | |
254 | and | |
255 | `Execution terminated' | |
256 | messages. | |
257 | See section 5.2 for more details. | |
258 | If we now look at the files in our directory we will see: | |
259 | .LS | |
260 | % \*bls\fR | |
261 | first.p | |
262 | obj | |
263 | % | |
264 | .LE | |
265 | We can give our object program a name other than `obj' by using the move | |
266 | command | |
267 | .I mv | |
268 | (1). | |
269 | Thus to name our program `hello': | |
270 | .LS | |
271 | % \*bmv obj hello\fR | |
272 | % \*bhello\fR | |
273 | Hello, world! | |
274 | % \*bls\fR | |
275 | first.p | |
276 | hello | |
277 | % | |
278 | .LE | |
279 | Finally we can get rid of the Pascal object code by using the | |
280 | .I rm | |
281 | (1) remove file command, e.g.: | |
282 | .LS | |
283 | % \*brm hello\fR | |
284 | % \*bls\fR | |
285 | first.p | |
286 | % | |
287 | .LE | |
288 | .PP | |
289 | For small programs which are being developed | |
290 | .IX | |
291 | tends to be more convenient to use than | |
292 | .PI | |
293 | and | |
294 | .X . | |
295 | Except for absence of the | |
296 | .I obj | |
297 | file after a | |
298 | .IX | |
299 | run, | |
300 | a | |
301 | .IX | |
302 | command is equivalent to a | |
303 | .PI | |
304 | command followed by a | |
305 | .X | |
306 | command. | |
307 | For larger programs, | |
308 | where a number of runs testing different parts of the program are | |
309 | to be made, | |
310 | .PI | |
311 | is useful as this | |
312 | .I obj | |
313 | file can be executed any desired number of times. | |
314 | .. >>> INSERT SECTION FOR PC <<< | |
315 | .NH 2 | |
316 | A larger program | |
317 | .PP | |
318 | Suppose that we have used the editor to put a larger program | |
319 | in the file `bigger.p'. | |
320 | We can list this program with line numbers by using the program | |
321 | .I cat -n | |
322 | i.e.: | |
323 | .LS | |
324 | % \*bcat -n bigger.p\fR | |
325 | .so bigger3.p | |
326 | % | |
327 | .LE | |
328 | This program is similar to program 4.9 on page 30 of the | |
329 | Jensen-Wirth | |
330 | .I "User Manual" . | |
331 | A number of problems have been introduced into this example for | |
332 | pedagogical reasons. | |
333 | .br | |
334 | .PP | |
335 | If we attempt to translate and execute the program using | |
336 | .IX | |
337 | we get the following response: | |
338 | .LS | |
339 | % \*bpix bigger.p\fR | |
340 | .so bigout1 | |
341 | % | |
342 | .LE | |
343 | .PP | |
344 | Since there were fatal `E' errors in our program, | |
345 | no code was generated and execution was necessarily suppressed. | |
346 | One thing which would be useful at this point is a listing of the | |
347 | program with the error messages. | |
348 | We can get this by using the command: | |
349 | .LS | |
350 | % \*bpi -l bigger.p\fR | |
351 | .LE | |
352 | There is no point in using | |
353 | .IX | |
354 | here, since we know there are fatal errors in the program. | |
355 | This command will produce the output at our terminal. | |
356 | If we are at a terminal which does not produce a hard copy | |
357 | we may wish to print this | |
358 | listing off-line on a line printer. | |
359 | We can do this with the command: | |
360 | .LS | |
361 | % \*bpi -l bigger.p | lpr\fR | |
362 | .LE | |
363 | .PP | |
364 | In the next few sections we will illustrate various aspects of the | |
365 | Berkeley | |
366 | Pascal system by correcting this program. | |
367 | .NH 2 | |
368 | Correcting the first errors | |
369 | .PP | |
370 | Most of the errors which occurred in this program were | |
371 | .I syntactic | |
372 | errors, those in the format and structure of the program rather than | |
373 | its content. | |
374 | Syntax errors are flagged by printing the offending line, and then a line | |
375 | which flags the location at which an error was detected. | |
376 | The flag line also gives an explanation | |
377 | stating either a possible cause of the error, | |
378 | a simple action which can be taken to recover from the error so | |
379 | as to be able to continue the analysis, | |
380 | a symbol which was expected at the point of error, | |
381 | or an indication that the input was `malformed'. | |
382 | In the last case, the recovery may skip ahead in the input | |
383 | to a point where analysis of the program can continue. | |
384 | .PP | |
385 | In this example, | |
386 | the first error diagnostic indicates that the translator detected | |
387 | a comment within a comment. | |
388 | While this is not considered an error in `standard' | |
389 | Pascal, it usually corresponds to an error in the program which | |
390 | is being translated. | |
391 | In this case, we have accidentally omitted the trailing `*)' of the comment | |
392 | on line 8. | |
393 | We can begin an editor session to correct this problem by doing: | |
394 | .LS | |
395 | % \*bex bigger.p\fR | |
396 | "bigger.p" 24 lines, 512 characters | |
397 | :\*b8s/$/ *)\fR | |
398 | s = 32; (* 32 character width for interval [x, x+1] *) | |
399 | : | |
400 | .LE | |
401 | .PP | |
402 | The second diagnostic, given after line 16, | |
403 | indicates that the keyword | |
404 | .B do | |
405 | was expected before the keyword | |
406 | .B begin | |
407 | in the | |
408 | .B for | |
409 | statement. | |
410 | If we examine the | |
411 | .I statement | |
412 | syntax chart on page 118 of the | |
413 | Jensen-Wirth | |
414 | .I "User Manual" | |
415 | we will discover that | |
416 | .B do | |
417 | is a necessary part of the | |
418 | .B for | |
419 | statement. | |
420 | Similarly, we could have referred to section C.3 of the | |
421 | Jensen-Wirth | |
422 | .I "User Manual" | |
423 | to learn about the | |
424 | .B for | |
425 | statement and gotten the same information there. | |
426 | It is often useful to refer to these syntax charts and to the | |
427 | relevant sections of this book. | |
428 | .PP | |
429 | We can correct this problem by first scanning for the keyword | |
430 | .B for | |
431 | in the file and then substituting the keyword | |
432 | .B do | |
433 | to appear in front of the keyword | |
434 | .B begin | |
435 | there. | |
436 | Thus: | |
437 | .LS | |
438 | :\*b/for\fR | |
439 | for i := 0 to lim begin | |
440 | :\*bs/begin/do &\fR | |
441 | for i := 0 to lim do begin | |
442 | : | |
443 | .LE | |
444 | The next error in the program is easy to pinpoint. | |
445 | On line 18, we didn't hit the shift key and got a `9' | |
446 | instead of a `)'. | |
447 | The translator diagnosed that `x9' | |
448 | was an undefined variable and, later, | |
449 | that a `)' was missing in the statement. | |
450 | It should be stressed that | |
451 | .PI | |
452 | is not suggesting that you should insert a `)' before the `;'. | |
453 | It is only indicating that making this change will help it to be able to | |
454 | continue analyzing the program so as to be able to diagnose further | |
455 | errors. | |
456 | You must then determine the true cause of the error and make the | |
457 | appropriate correction to the source text. | |
458 | .PP | |
459 | This error also illustrates the fact that one error in the input may lead | |
460 | to multiple error diagnostics. | |
461 | .I Pi | |
462 | attempts | |
463 | to give only one diagnostic for each error, | |
464 | but single errors in the input sometimes appear to be more than | |
465 | one error. | |
466 | It is also the case that | |
467 | .PI | |
468 | may not detect an error when it occurs, but may detect it later in | |
469 | the input. | |
470 | This would have happened | |
471 | in this example if we had typed `x' instead of `x9'. | |
472 | .PP | |
473 | The translator next detected, on line 19, that the function | |
474 | .I Round | |
475 | and the variable | |
476 | .I h | |
477 | were undefined. | |
478 | It does not know about | |
479 | .I Round | |
480 | because | |
481 | .UP | |
482 | normally distinguishes between upper and lower case.\*(dg | |
483 | .FS | |
484 | \*(dgIn ``standard'' Pascal no distinction is made based on case. | |
485 | .FE | |
486 | On | |
487 | .UX | |
488 | lower-case is preferred\*(dd, | |
489 | .FS | |
490 | \*(ddOne good reason for using lower-case is that it is easier to type. | |
491 | .FE | |
492 | and all keywords and built-in | |
493 | .B procedure | |
494 | and | |
495 | .B function | |
496 | names are composed of lower-case letters, | |
497 | just as they are in the Jensen-Wirth | |
498 | .I "Pascal Report" . | |
499 | Thus we need to use the function | |
500 | .I round | |
501 | here. | |
502 | As far as | |
503 | .I h | |
504 | is concerned, | |
505 | we can see why it is undefined if we look back to line 9 | |
506 | and note that its definition was lost in the non-terminated | |
507 | comment. | |
508 | This diagnostic need not, therefore, concern us. | |
509 | .PP | |
510 | The next error which occurred in the program caused the translator | |
511 | to insert a `;' before the statement calling | |
512 | .I writeln | |
513 | on line 23. | |
514 | If we examine the program around the point of error we will see | |
515 | that the actual error is that the keyword | |
516 | .B until | |
517 | and an associated expression have been omitted here. | |
518 | Note that the diagnostic from the translator does not indicate the actual | |
519 | error, and is somewhat misleading. | |
520 | The translator made the correction which seemed to be most plausible. | |
521 | As the omission of a `;' character is a common mistake, | |
522 | the translator chose to indicate this as a possible fix here. | |
523 | It later detected that the keyword | |
524 | .B until | |
525 | was missing, but not until it saw the keyword | |
526 | .B end | |
527 | on line 24. | |
528 | The combination of these diagnostics indicate to us the true problem. | |
529 | .PP | |
530 | The final syntactic error message indicates that the translator needed an | |
531 | .B end | |
532 | keyword to match the | |
533 | .B begin | |
534 | at line 15. | |
535 | Since the | |
536 | .B end | |
537 | at line 24 is supposed to match this | |
538 | .B begin , | |
539 | we can infer that another | |
540 | .B begin | |
541 | must have been mismatched, and have matched this | |
542 | .B end . | |
543 | Thus we see that we need an | |
544 | .B end | |
545 | to match the | |
546 | .B begin | |
547 | at line 16, | |
548 | and to appear before the final | |
549 | .B end . | |
550 | We can make these corrections: | |
551 | .LS | |
552 | :\*b/x9/s//x)\fR | |
553 | y := exp(-x) * sin(i * x); | |
554 | :\*b+s/Round/round\fR | |
555 | n := round(s * y) + h; | |
556 | :\*b/write\fR | |
557 | write(' '); | |
558 | :\*b/\fR | |
559 | writeln('*') | |
560 | :\*binsert\fR | |
561 | \*buntil n = 0;\fR | |
562 | \&\*b.\fR | |
563 | :\*b$\fR | |
564 | end. | |
565 | :\*binsert\fR | |
566 | \*bend\fR | |
567 | \&\*b.\fR | |
568 | : | |
569 | .LE | |
570 | .PP | |
571 | At the end of each | |
572 | .B procedure | |
573 | or | |
574 | .B function | |
575 | and the end of the | |
576 | .B program | |
577 | the translator summarizes references to undefined variables | |
578 | and improper usages of variables. | |
579 | It also gives | |
580 | warnings about potential errors. | |
581 | In our program, the summary errors do not indicate any further problems | |
582 | but the warning that | |
583 | .I c | |
584 | is unused is somewhat suspicious. | |
585 | Examining the program we see that the constant was intended | |
586 | to be used in the expression which is an argument to | |
587 | .I sin , | |
588 | so we can correct this expression, and translate the program. | |
589 | We have now made a correction for each diagnosed error | |
590 | in our program. | |
591 | .LS | |
592 | :\*b?i ?s//c /\fR | |
593 | y := exp(-x) * sin(c * x); | |
594 | :\*bwrite\fR | |
595 | "bigger.p" 26 lines, 538 characters | |
596 | :\*bquit\fR | |
597 | % \*bpi bigger.p\fR | |
598 | % | |
599 | .LE | |
600 | It should be noted that the translator suppresses warning | |
601 | diagnostics for a particular | |
602 | .B procedure , | |
603 | .B function | |
604 | or the main | |
605 | .B program | |
606 | when it finds severe syntax errors in that part of the source | |
607 | text. | |
608 | This is to prevent possibly confusing and | |
609 | incorrect warning diagnostics from being produced. | |
610 | Thus these warning diagnostics may not appear in a program with | |
611 | bad syntax errors until these errors are corrected. | |
612 | .KS | |
613 | .PP | |
614 | We are now ready to execute our program for the first | |
615 | time. | |
616 | We will do so in the next section after giving a listing | |
617 | of the corrected program for reference purposes. | |
618 | .LS | |
619 | % \*bcat -n bigger.p\fR | |
620 | .so bigger6.p | |
621 | % | |
622 | .LE | |
623 | .NH 2 | |
624 | Executing the second example | |
625 | .PP | |
626 | We are now ready to execute the second example. | |
627 | The following output was produced by our first run. | |
628 | .LS | |
629 | % \*bpx\fR | |
630 | .so bigout2 | |
631 | % | |
632 | .LE | |
633 | Here the interpreter is presenting us with a runtime error diagnostic. | |
634 | It detected a `division by zero' at line 17. | |
635 | Examining line 17, we see that we have written | |
636 | the statement `x := d / i' instead of `x := d * i'. | |
637 | We can correct this and rerun the program: | |
638 | .LS | |
639 | % \*bex bigger.p\fR | |
640 | "bigger.p" 26 lines, 538 characters | |
641 | :\*b17\fR | |
642 | x := d / i | |
643 | :\*bs'/'*\fR | |
644 | x := d * i | |
645 | :\*bwrite\fR | |
646 | "bigger.p" 26 lines, 538 characters | |
647 | :\*bq\fR | |
648 | % \*bpix bigger.p\fR | |
649 | .so bigout3 | |
650 | % | |
651 | .LE | |
652 | .KS | |
653 | .PP | |
654 | This appears to be the output we wanted. | |
655 | We could now save the output in a file if we wished by using the shell | |
656 | to redirect the output: | |
657 | .LS | |
658 | % \*bpx > graph\fR | |
659 | .LE | |
660 | .KE | |
661 | We can use | |
662 | .I cat | |
663 | (1) to see the contents of the file graph. | |
664 | We can also make a listing of the graph on the line printer without | |
665 | putting it into a file, e.g. | |
666 | .LS | |
667 | % \*bpx | lpr\fR | |
668 | .so bigout4 | |
669 | % | |
670 | .LE | |
671 | Note here that the statistics lines came out on our terminal. | |
672 | The statistics line comes out on the diagnostic output (unit 2.) | |
673 | There are two ways to get rid of the statistics line. | |
674 | We can redirect the statistics message to the printer using the | |
675 | syntax `|\|&' to the shell rather than `|', i.e.: | |
676 | .LS | |
677 | % \*bpx |\|& lpr\fR | |
678 | % | |
679 | .LE | |
680 | or we can translate the program with the | |
681 | .B p | |
682 | option disabled on the command line as we did above. | |
683 | This will disable all post-mortem dumping including the statistics line, | |
684 | thus: | |
685 | .LS | |
686 | % \*bpi -p bigger.p\fR | |
687 | % \*bpx | lpr\fR | |
688 | % | |
689 | .LE | |
690 | This option also disables the statement limit which normally guards | |
691 | against infinite looping. | |
692 | You should not use it until your program is debugged. | |
693 | Also if | |
694 | .B p | |
695 | is specified and an error occurs, you will | |
696 | not get run time diagnostic information to help you | |
697 | determine what the problem is. | |
698 | .NH 2 | |
699 | Formatting the program listing | |
700 | .PP | |
701 | It is possible to use special lines within the source text of a program | |
702 | to format the program listing. | |
703 | An empty line (one with no characters on it) corresponds to a | |
704 | `space' macro in an assembler, leaving a completely blank line | |
705 | without a line number. | |
706 | A line containing only a control-l (form-feed) character | |
707 | will cause a page eject in the listing with the corresponding line number | |
708 | suppressed. | |
709 | This corresponds to an `eject' pseudo-instruction. | |
710 | See also section 5.2 for details on the | |
711 | .B n | |
712 | and | |
713 | .B i | |
714 | options of | |
715 | .PI . | |
716 | .NH 2 | |
717 | Execution profiling | |
718 | .PP | |
719 | An execution profile consists of a structured listing of (all or part of) | |
720 | a program with information about the number of times each statement in | |
721 | the program was executed for a particular run of the program. | |
722 | These profiles can be used for several purposes. | |
723 | In a program which was abnormally terminated due to excessive looping | |
724 | or recursion or by a program fault, the counts can facilitate location | |
725 | of the error. | |
726 | Zero counts mark portions of the program which were not executed; | |
727 | during the early debugging stages they should prompt new test data or | |
728 | a re-examination of the program logic. | |
729 | The profile is perhaps most valuable, however, in drawing | |
730 | attention to the (typically small) | |
731 | portions of the program that dominate execution time. | |
732 | This information can be used for source level optimization. | |
733 | .SH | |
734 | An example | |
735 | .PP | |
736 | A prime number is a number which is divisible only by itself and the | |
737 | number one. | |
738 | The program | |
739 | .I primes , | |
740 | written by Niklaus Wirth, | |
741 | determines the first few prime numbers. | |
742 | In translating the program we have specified the | |
743 | .B z | |
744 | option to | |
745 | .IX . | |
746 | This option causes the translator to generate counters and count instructions | |
747 | sufficient in number to determine the number of times each statement in the | |
748 | program was executed.\*(dg | |
749 | .FS | |
750 | \*(dgThe counts | |
751 | are completely accurate only in the absence of runtime errors and nonlocal | |
752 | .B goto | |
753 | statements. | |
754 | This is not generally a problem, however, as in structured programs | |
755 | nonlocal | |
756 | .B goto | |
757 | statements occur infrequently, | |
758 | and counts are incorrect after abnormal termination only when the | |
759 | .I "upward look" | |
760 | described below to get a count passes a suspended call point. | |
761 | .FE | |
762 | When execution of the program completes, either normally or abnormally, | |
763 | this count data is written to the file | |
764 | .I pmon.out | |
765 | in the current directory.\*(dd | |
766 | .FS | |
767 | \*(dd\c | |
768 | .I Pmon.out | |
769 | has a name similar to | |
770 | .I mon.out | |
771 | the monitor file produced by the profiling facility of the C compiler | |
772 | .I cc | |
773 | (1). | |
774 | See | |
775 | .I prof | |
776 | (1) for a discussion of the C compiler profiling facilities. | |
777 | .FE | |
778 | It is then possible to prepare an execution profile by giving | |
779 | .XP | |
780 | the name of the file associated with this data, as was done in the following | |
781 | example. | |
782 | .LS | |
783 | % \*bpix -l -z primes.p\fR | |
784 | .so primeout1 | |
785 | % | |
786 | .LE | |
787 | .SH | |
788 | Discussion | |
789 | .PP | |
790 | The header lines of the outputs of | |
791 | .IX | |
792 | and | |
793 | .XP | |
794 | in this example indicate the version of the translator and execution | |
795 | profiler in use at the time this example was prepared. | |
796 | The time given with the file name (also on the header line) | |
797 | indicates the time of last modification of the program source file. | |
798 | This time serves to | |
799 | .I "version stamp" | |
800 | the input program. | |
801 | .I Pxp | |
802 | also indicates the time at which the profile data was gathered. | |
803 | .LS | |
804 | % \*bpxp -z primes.p\fR | |
805 | .so primeout2 | |
806 | % | |
807 | .LE | |
808 | .KE | |
809 | .PP | |
810 | To determine the number of times a statement was executed, | |
811 | one looks to the left of the statement and finds the corresponding | |
812 | vertical bar `|'. | |
813 | If this vertical bar is labelled with a count then that count gives the | |
814 | number of times the statement was executed. | |
815 | If the bar is not labelled, we look up in the listing to find the first | |
816 | `|' which directly above the original one which has a count and that | |
817 | is the answer. | |
818 | Thus, in our example, | |
819 | .I k | |
820 | was incremented 157 times on line 18, | |
821 | while the | |
822 | .I write | |
823 | procedure call on line 24 was executed 48 times as given by the count | |
824 | on the | |
825 | .B repeat . | |
826 | .PP | |
827 | More information on | |
828 | .I pxp | |
829 | can be found in its manual section | |
830 | .XP | |
831 | (1) | |
832 | and in sections 5.4, 5.5 and 5.10. |