annotate paper/rectype.ind @ 37:bda0b56c9231

fix
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 16 Jun 2012 01:02:37 +0900
parents f9e7052a380e
children 34a726a5c0d4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 -title: Recursive type syntax in Continuation based C
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
32
be591bf4e2b5 merge 31
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
3 \newcommand{\rectype}{{\tt \_\_rectype}}
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
32
be591bf4e2b5 merge 31
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
5 --author: Nobuyasu Oshiro, Shinji KONO
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 --abstract:
32
be591bf4e2b5 merge 31
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
8
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 We have implemented Continuation based C (CbC).
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 CbC is an extension of C, which has parameterized goto statement.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 It is useful for finite state automaton or many core tasks.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 Goto statement is a way to force tail call elimination.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 The destination of goto statement is called Code Segment, which is actually a normal function of C.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 To represent recursive function call, the type system of C is not enough, because
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 it has no recursive types.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 We introduce \rectype keyword for recursive type, and it is implemented in GCC-4.6.0.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 We will compare the conventional methods, \rectype keyword and a method using C structure.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 Also we show usage of CbC and it's benchmark.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
32
be591bf4e2b5 merge 31
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
20 --Motivation
be591bf4e2b5 merge 31
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
21
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
22 The C programming language is used in many practical programs, operating system
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
23 kernels, byte code machines, network servers or embeded systems. Low level feature
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
24 of C is useful, but sometimes it requres programming hacks. For example, in case of
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
25 byte code machine often entire program is a huge switch statement which has many
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
26 labels which corespond the byte codes. Operating system or network server has
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
27 many layers such as system call dispatch, transport or presentation layer. It
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
28 requires deep call levels, 2 or 3 for each layer, resulting 10-30 call levels.
32
be591bf4e2b5 merge 31
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
29
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
30 Continuation based C (CbC) provids a structured way to represent these situations. It
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
31 is a small modication of C. It consists of a syntax to force tail-call-elimation
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
32 and parametarized goto statement.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
33
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
34 C has capable of recursive functions, but we find it's type system is not enough
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
35 to represent CbC programming. In this paper, we introduce \rectype
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
36 keyward as a recursive type. To handle recursive type, conventionally tagged struct
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
37 is used. It is also fit for CbC program.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
38
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
39 We will describe the CbC and \rectype implementation of CbC on GCC 4.6.0.
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 --Continuation based C
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42
28
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
43 CbC's basic programming unit is a Code Segment. It is not a subroutine, but it
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 looks like a function, because it has input and output. We can use C struct
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 as input and output interfaces.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 struct interface1 { int i; };
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 struct interface2 { int o; };
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 __code f(struct interface1 a) {
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 struct interface2 b; b.o=a.i;
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 goto g(b);
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 }
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54
28
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
55 In this example, a Code Segment
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
56 \verb+f+ has \verb+input a+ and sends \verb+output b+ to a Code Segment \verb+g+.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
57 There is no return from Code Segment \verb+b+, \verb+b+ should call another
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 continuation using \verb+goto+. Any control structure in C is allowed in CwC
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 language, but in case of CbC, we restrict ourselves to use \verb+if+ statement
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 only, because it is sufficient to implement C to CbC translation. In this case,
28
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
61 Code Segment has one input interface and several output interfaces (fig.\ref{code}).
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62
30
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
63 <center><img src="figure/code.pdf" alt="code"></center>
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 \verb+__code+ and parameterized global goto statement is an extension of
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 Continuation based C. Unlike \verb+C--+ \cite{cminusminus}'s parameterized goto,
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 we cannot goto into normal C function.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
28
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
70 In CwC, we can go to a Code Segment from a C function and we can call C functions
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
71 in a Code Segment. So we don't have to shift completely from C to CbC. The later
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 one is straight forward, but the former one needs further extensions.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 void *env;
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 __code (*exit)(int);
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 __code h(char *s) {
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 printf(s);
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 goto (*exit)(0),env;
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 }
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 int main() {
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 env = __environment;
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 exit = __return;
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 goto h("hello World\n");
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 }
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 In this hello world example, the environment of \verb+main()+
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 and its continuation is kept in global variables. The environment
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 and the continuation can be get using \verb+__environment+,
28
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
91 and \verb+__return+. Arbitrary mixture of Code Segments and functions
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 are allowed (in CwC). The continuation of \verb+goto+ statement
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 never returns to original function, but it goes to caller of original
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 function. In this case, it returns result 0 to the operating system.
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
14
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
98 --Recursive type syntax
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
99
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
100 A continuation is a pointer to a Code Segment to be executed after.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
101 For example, it is passed as follows;
14
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
102
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
103 __code csA( __code (*p)() ) {
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
104 goto p(csB);
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
105 }
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
106
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
107 where {\tt p} is the continuation and {\tt csB} is a Code Segment which
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
108 has the same type of {\tt csA}. This declaration is not enough because
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
109 it lacks the type of the argument. If add the type declaration, we
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
110 get following program;
14
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
111
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
112 __code csA( __code (*p)( __code (*)( __code (*)( __code (*)())))) {
14
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
113 goto p(csB);
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
114 }
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
115
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
116 or
26
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
117 __code csA( __code (*p)( __code(*)())) {
15
68d2c32f74cf modify explanation of rectype syntax
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
118 goto p(csB);
68d2c32f74cf modify explanation of rectype syntax
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
119 }
68d2c32f74cf modify explanation of rectype syntax
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
120
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
121 It is enough to type {\csB}, but of course it is not completly wll typed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
122 Omitting types of the arguments of a function is allowed, but it requires
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
123 complex declaration and it is imcomplete.
15
68d2c32f74cf modify explanation of rectype syntax
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
124
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
125 We introduce \rectype syntax for this situation. In an argument declation,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
126 \rectype represents it's fuction type. Using \rectype, previous example can
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
127 be written like this;
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 __code csA( __rectype *p) {
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 goto p(csB);
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 }
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
133 {\tt *p} has a type of {\tt csA} itself.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
134
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
135 The same situation is happen in convetional C, since Code Segment is a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
136 mere C function with tail-call-elimination. \rectype provides exact and
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
137 concise way to describe recursive type. If we have extra arguments,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
139 __code csBs(int a, __rectype *p) ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
140
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
141 __code csAs(int a, __rectype *p) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
142 goto p(a, csBs);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
143 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
144
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
145 In this case, we have two \rectype keywords. It may points the same type or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
146 different types. There is no ambiguity here, because it point the exact
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
147 position. If it points the same position in the same type declaration,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
148 it is the same type.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
149
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153
13
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
154 --Recursive type syntax
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
155
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
156 struct interface {
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
157 __code (*next)(struct interface);
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
158 };
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
159
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
160 __code csA(struct interface p) {
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
161 struct interface ds = { csB };
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
162 goto p.next(ds);
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
163 }
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
164
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
165 int main() {
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
166 struct interface ds = { print };
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
167 goto csA(ds);
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
168 return 0;
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
169 }
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
170
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
171
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
172
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
173 __code fibonacci(__rectype *p, int num, int count, int result, int prev)
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
174
22dbcdbcae5f add CbC.mm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 11
diff changeset
175
33
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13 30
diff changeset
176
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13 30
diff changeset
177
24
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
178 --How to implement \rectype
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179
29
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
180 \rectype syntax is implemented overriding AST.
24
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
181 First, \rectype syntax make Tree same \code(\ref{fig:tree1}).
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
182 Second, tree was created to be rectype flag.
29
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
183 Third, to override AST(\ref{fig:tree2}).
24
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
184
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
185 \begin{figure}[htpb]
30
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
186 \begin{minipage}{0.5\hsize}
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
187 \begin{center}
24
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
188 \scalebox{0.35}{\includegraphics{figure/tree1.pdf}}
30
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
189 \end{center}
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
190 \caption{AST of function pointer}
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
191 \label{fig:tree1}
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
192 \end{minipage}
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
193 \begin{minipage}{0.2\hsize}
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
194 \begin{center}
24
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
195 \scalebox{0.35}{\includegraphics{figure/tree2.pdf}}
30
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
196 \end{center}
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
197 \caption{AST of \rectype}
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
198 \label{fig:tree2}
24
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
199 \end{minipage}
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
200 \end{figure}
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
201
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
202 This AST(\ref{fig:tree2}) is made by syntax of \verb+__code csA(__rectype *p)+ .
30
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
203 \treelist have infomation of argument.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
204 First \treelist represent that argument is function pointer(\verb+__code (*p)()+) .
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
205 Second \treelist represent that csA is Fixed-length argument.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
206 First \treelist is connected with \pointertype.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
207 \pointertype have pointer of function(\functiontype).
24
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
208 We have to override it in the pointer of csA.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
209
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
210
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
211
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 23
diff changeset
212
21
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
213 --Problems with implementation of \rectype.
33
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13 30
diff changeset
214
21
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
215 Segmentation fault has occurred in the following program on compile.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
216
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
217 __code csA(__rectype *p) {
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
218 goto p(3);
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
219 }
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
220
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
221 The above code is the wrong argument of p.
22
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 21
diff changeset
222 The p's argument is converted by GCC.
28
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
223 3 of type int is converted to a pionter type of Code Segment.
22
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 21
diff changeset
224 At this time, GCC looks at the type of the argument.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 21
diff changeset
225 p's argument is pointer of csA.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 21
diff changeset
226 csA's argument is p.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 21
diff changeset
227 GCC is also an infinite recursion happens to see the type of tye argument of the argument.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 21
diff changeset
228
23
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
229 We Solve this problem that does not check the arguments if the \rectype flag is true.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
230 The following program become part was fixed gcc/c-family/c-pretty-print.c.
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
231
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
232 static void
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
233 pp_c_abstract_declarator (c_pretty_printer *pp, tree t)
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
234 {
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
235 if (TREE_CODE (t) == POINTER_TYPE)
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
236 {
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
237 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
238 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
239 pp_c_right_paren (pp);
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
240 #ifndef noCbC
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
241 if (IS_RECTYPE(t)) return;
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
242 #endif
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
243 t = TREE_TYPE (t);
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
244 }
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
245 pp_direct_abstract_declarator (pp, t);
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
246 }
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
247
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
248
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
249 Variable t have information of p's argument.
30
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 29
diff changeset
250 If t is \pointertype, t is assigned type of \pointertype(\verb+t = TREE_TYPE (t);+).
23
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
251 We have added code \verb+if (IS_RECTYPE(t)) return;+
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 22
diff changeset
252 Thereby we have solved type checking and infinite recursion problem.
21
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
253
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
254
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
255
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 20
diff changeset
256
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 --Method other than \rectype
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258
18
33b7a54edaa9 method other rectype
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
259 The recursively program of C's syntax can be solved using struct syntax.
33b7a54edaa9 method other rectype
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
260 For example, if we write
33b7a54edaa9 method other rectype
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
261
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 struct interface {
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 __code (*next)(struct interface);
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 };
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
265
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 __code csA(struct interface p) {
18
33b7a54edaa9 method other rectype
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
267 struct interface ds;
33b7a54edaa9 method other rectype
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
268 ds.next = csB;
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 goto p.next(ds);
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 }
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
271
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 int main() {
18
33b7a54edaa9 method other rectype
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
273 struct interface ds;
33b7a54edaa9 method other rectype
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
274 ds = print;
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 goto csA(ds);
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 return 0;
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 }
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
278
18
33b7a54edaa9 method other rectype
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
279 there is no need to write recursively.
33b7a54edaa9 method other rectype
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
280 Because the struct syntax wrapped in a function pointer.
28
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
281 Code Segment does not receive function pointer in arguments.
18
33b7a54edaa9 method other rectype
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
282 Recursively program does not occur.
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
283
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
284
26
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
285 --Comparison
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
286
26
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
287 Here is CbC program that finds the Fibonacci sequence.
11
bf3c780d3039 changed to outline format
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
288
26
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
289 __code print(__rectype *p, long int num,
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
290 long int count, long int result, long int prev) {
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
291 printf("fibonacci(%d) = %ld\n",num,result);
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
292 goto cs_while(print, num, count, result, prev);
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
293 }
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
294
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
295 __code fibonacci(__rectype *p, long int num,
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
296 long int count, long int result, long int prev) {
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
297 if (count == 0) {
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
298 result += 0;
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
299 count++;
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
300 } else if (count == 1) {
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
301 result += 1;
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
302 count++;
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
303 } else if (count > 1){
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
304 long int tmp = prev;
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
305 prev = result;
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
306 result = result + tmp;
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
307 count++;
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
308 } else {
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
309 printf("please enter nutural number\n");
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
310 exit(0);
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
311 }
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
312 if (num < count) {
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
313 goto p(fibonacci, num, count, result, prev);
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
314 }
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
315 goto fibonacci(p, num, count, result, prev);
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
316 }
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
317 __code cs_while(__rectype *p, long int num,
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
318 long int count, long int result, long int prev) {
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
319 if (num > 0) {
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
320 num--;
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
321 goto fibonacci(print, num, 0, 0, 0);
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
322 }
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
323 exit(0);
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
324 }
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
325
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
326 It is written using \rectype syntax.
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
327 Do not use the \rectype syntax program would be the following declaration.
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
328
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
329 __code print(__code (*p)(__code(*)(),long int,long int,long int,long int),
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
330 long int num, long int count, long int result, long int prev);
28
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
331
26
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
332 __code fibonacci(__code (*p)(__code(*)(),long int,long int, long int,long int),
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
333 long int num, long int count, long int result, long int prev);
28
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
334
26
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
335 __code cs_while(__code (*p)(__code(*)(),long int, long int, long int, long int),
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
336 long int num, long int count, long int result, long int prev);
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
337
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
338 Comparing the program that made the declaration of each.
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
339 AST is almost the same should be created both.
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
340 Therefore, there should be no difference was the result.
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
341
680becde8c9f modify coparison
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
342 Here is the result.
19
dc62dc1fe059 modify compression
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
343
dc62dc1fe059 modify compression
Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 18
diff changeset
344 --Conclusion