Changes in / [3d618a0:f5dbc8d]
- Location:
- doc/theses/jiada_liang_MMath
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
r3d618a0 rf5dbc8d 147 147 const T E_values [3] = { t1, t2, t3 }; 148 148 \end{cfa} 149 The generated C enumeration will have enumerator values resemble \CFA enumerator positions thanks to C's auto-initialization scheme. 150 A \CFA enumeration variable definition is same in \CFA and C, before or after the transpilation. 151 For example: 152 \begin{cfa} 153 enum E e = E1; 154 e; 155 \end{cfa} 156 These two expressions will not change by \CFA-cc. A \CFA enumeration variable will always have the same underlying representation as its generated 157 C enumeration. This implies \CFA enumeration variable does not take up extra memory and \CFA enumeration use @posn@ as its underlying representation. 158 159 Notice that value and label arrays are dynamically allocated data structures that take up 149 The generated C enumeration will have enumerator values equals to their positions thanks to C's auto-initialization scheme. Notice that value and label arrays are dynamically allocated data structures that take up 160 150 memory. If an enumeration is globally defined, the arrays are allocated in the @.data@ section and will be initialized before the program execution. 161 151 Otherwise, if an enumeration has its definition in a local scope, these arrays will be allocated on the stack and be initialized when the program counter … … 222 212 223 213 224 \section{ Initialization}214 \section{Auto Initialization} 225 215 \CFA extends C's auto-initialization scheme to \CFA enumeration. For an enumeration type with base type T, the initialization scheme is the following: 226 216 \begin{enumerate} -
doc/theses/jiada_liang_MMath/conclusion.tex
r3d618a0 rf5dbc8d 71 71 Label arrays are auxiliary data structures that are always generated for \CFA enumeration, which is a considerable program overhead. 72 72 It is helpful to provide a new syntax or annotation for a \CFA enumeration definition that tells the compiler the labels will not be used 73 throughout the execution. Therefore, \CFA optimizes the label array away. The @ label()@ function can still be used on an enumeration constant,74 and the function called is reduced to a @char *@ constant expression that holds the name of the enumerator. But if @ label()@ is called on73 throughout the execution. Therefore, \CFA optimizes the label array away. The @value()@ function can still be used on an enumeration constant, 74 and the function called is reduced to a @char *@ constant expression that holds the name of the enumerator. But if @value()@ is called on 75 75 a variable with an enumerated type, it returns an empty string since the label information is lost for the runtime. 76 \item77 \CFA enumeration has its limitation in separate compilation. Consider the followings example:78 \begin{cfa}79 // libvacodec.h: v180 enum(int) C_Codec ! {81 FIRST_VIDEO = 0,82 VP8 = 2,83 VP9 = 4,84 LAST_VIDEO = 8,85 86 FIRST_AUDIO = 3,87 VORBIS = 9,88 OPUS = 27,89 LAST_AUDIO = 8190 };91 92 enum(int) CFA_Codec ! {93 FIRST_VIDEO = 0,94 VP8 = 2,95 VP9 = 4,96 LAST_VIDEO = 8,97 98 FIRST_AUDIO = 3,99 VORBIS = 9,100 OPUS = 27,101 LAST_AUDIO = 81102 };103 104 // main.c105 C_Codec c_code = C_Codec.VORBIS;106 CFA_Codec cfa_code = CFA_Codec.VORBIS;107 \end{cfa}108 In the preceding example, the memory location of @c_code@ stores a value 9, the integral value of VORBIS.109 The memory of @cfa_code@ stores 5, which is the position of @CFA_Codec.VORBIS@ and can be mapped to value 9.110 111 If to insert a new enumerator in both @C_Codec@ and @CFA_Codec@112 before the relative position of VORBIS, without a re-compilation of @main.c@, variable @cfa_code@ would be mapped113 to an unintented value.114 // libvacodec.h: v2115 enum(int) C_Codec ! {116 FIRST_VIDEO = 0,117 VP8 = 2,118 VP9 = 4,119 @AV1@,120 LAST_VIDEO = 8,121 122 FIRST_AUDIO = 3,123 VORBIS = 9,124 OPUS = 27,125 LAST_AUDIO = 81126 };127 128 enum(int) CFA_Codec ! {129 FIRST_VIDEO = 0,130 VP8 = 2,131 VP9 = 4,132 @AV1@,133 LAST_VIDEO = 8,134 135 FIRST_AUDIO = 3,136 VORBIS = 9,137 OPUS = 27,138 LAST_AUDIO = 81139 };140 141 \end{cfa}142 While @c_code@ still has the correct value of 9, @cfa_code@ (5) is now mapped to @First_AUDIO@. In other words, it is143 necessary to to recompile @main.c@ when to update @libvacodec.h@.144 145 To avoid a recompilation, enumeration positions can be represented as @extern const@. For example, \CFA-cc can generate the following code for @CFA_Codec@:146 \begin{cfa}147 // CFA-cc transpile: libvacodec.h148 const int FIRST_VIDEO = 0;149 const int VP8 = 1;150 const int VP9 = 2;151 const int LAST_VIDEO = 3;152 const int FIRST_AUDIO = 4;153 const int VORBIS = 5;154 const int OPUS = 6;155 const int LAST_AUDIO = 7;156 157 // CFA-cc transpile: main.c158 extern const int FIRST_VIDEO;159 extern const int VP8;160 extern const int VP9;161 extern const int LAST_VIDEO;162 extern const int FIRST_AUDIO;163 extern const int VORBIS;164 extern const int OPUS;165 extern const int LAST_AUDIO;166 167 int cfa_code = VORBIS;168 \end{cfa}169 If later a position of an enumerator is changed, the generated expression @int cfa_code = VORBIS;@ will still have the correct170 value, because it is linked to a external constant @VORBIS@ from @libvacodec.h@. This implementation requires extra memory to store position, but171 solve the separate compilation problem.172 173 76 \end{enumerate}
Note: See TracChangeset
for help on using the changeset viewer.