Go to the first, previous, next, last section, table of contents.

A Comprehensive Example in C

Now we'll examine a second program, example2, which builds on the first example to introduce the rest of the stab types, symbol descriptors, and type descriptors used in C. See section Example2.c - source code for extended example for the complete `.c' source, and see section Example2.s - assembly code for extended example for the `.s' assembly code. This description includes parts of those files.

Flow of control and nested scopes

Directive:
.stabn
Types:
N_SLINE, N_LBRAC, N_RBRAC (cont.)

Consider the body of main, from `example2.c'. It shows more about how N_SLINE, N_RBRAC, and N_LBRAC stabs are used.

20 {
21      static float s_flap;
22      int times;
23      for (times=0; times < s_g_repeat; times++){
24        int inner;
25        printf ("Hello world\n");
26      }
27 };

Here we have a single source line, the `for' line, that generates non-linear flow of control, and non-contiguous code. In this case, an N_SLINE stab with the same line number proceeds each block of non-contiguous code generated from the same source line.

The example also shows nested scopes. The N_LBRAC and N_LBRAC stabs that describe block structure are nested in the same order as the corresponding code blocks, those of the for loop inside those for the body of main.

This is the label for the N_LBRAC (left brace) stab marking the start of main.

57 LBB2:

In the first code range for C source line 23, the for loop initialize and test, N_SLINE (68) records the line number:

.stabn N_SLINE, NIL,
       line,
       address

58 .stabn 68,0,23,LM2
59 LM2:
60      st %g0,[%fp-20]
61 L2:
62      sethi %hi(_s_g_repeat),%o0
63      ld [%fp-20],%o1
64      ld [%o0+%lo(_s_g_repeat)],%o0
65      cmp %o1,%o0
66      bge L3
67      nop

label for the N_LBRAC (start block) marking the start of for loop

68 LBB3:
69 .stabn 68,0,25,LM3
70 LM3:
71      sethi %hi(LC0),%o1
72      or %o1,%lo(LC0),%o0
73      call _printf,0
74      nop
75 .stabn 68,0,26,LM4
76 LM4:

label for the N_RBRAC (end block) stab marking the end of the for loop

77 LBE3:

Now we come to the second code range for source line 23, the for loop increment and return. Once again, N_SLINE (68) records the source line number:

.stabn, N_SLINE, NIL,
        line,
        address

78 .stabn 68,0,23,LM5
79 LM5:
80 L4:
81      ld [%fp-20],%o0
82      add %o0,1,%o1
83      st %o1,[%fp-20]
84      b,a L2
85 L3:
86 .stabn 68,0,27,LM6
87 LM6:

label for the N_RBRAC (end block) stab marking the end of the for loop

88 LBE2:
89 .stabn 68,0,27,LM7
90 LM7:
91 L1:
92      ret
93      restore
94 .stabs "main:F1",36,0,0,_main
95 .stabs "argc:p1",160,0,0,68
96 .stabs "argv:p20=*21=*2",160,0,0,72
97 .stabs "s_flap:V12",40,0,0,_s_flap.0
98 .stabs "times:1",128,0,0,-20

Here is an illustration of stabs describing nested scopes. The scope nesting is reflected in the nested bracketing stabs (N_LBRAC, 192, appears here).

.stabn N_LBRAC,NIL,NIL,
       block-start-address

99  .stabn 192,0,0,LBB2      ## begin proc label
100 .stabs "inner:1",128,0,0,-24
101 .stabn 192,0,0,LBB3      ## begin for label

N_RBRAC (224), "right brace" ends a lexical block (scope).

.stabn N_RBRAC,NIL,NIL,
       block-end-address

102 .stabn 224,0,0,LBE3      ## end for label
103 .stabn 224,0,0,LBE2      ## end proc label

Go to the first, previous, next, last section, table of contents.