⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extend.texi

📁 早期freebsd实现
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
pointed to must @emph{not} be declared @code{const}.  Likewise, afunction that calls a non-@code{const} function usually must not be@code{const}.  It does not make sense for a @code{const} function toreturn @code{void}.We recommend placing the keyword @code{const} after the function'sreturn type.  It makes no difference in the example above, but when thereturn type is a pointer, it is the only way to make the function itselfconst.  For example,@exampleconst char *mincp (int);@end example@noindentsays that @code{mincp} returns @code{const char *}---a pointer to aconst object.  To declare @code{mincp} const, you must write this:@examplechar * const mincp (int);@end example  @cindex @code{#pragma}, reason for not using@cindex pragma, reason for not usingSome people object to this feature, suggesting that ANSI C's@code{#pragma} should be used instead.  There are two reasons for notdoing this.@enumerate@itemIt is impossible to generate @code{#pragma} commands from a macro.@itemThe @code{#pragma} command is just as likely as these keywords to meansomething else in another compiler.@end enumerateThese two reasons apply to almost any application that might be proposedfor @code{#pragma}.  It is basically a mistake to use @code{#pragma} for@emph{anything}.The keyword @code{__attribute__} allows you to specify specialattributes when making a declaration.  This keyword is followed by anattribute specification inside double parentheses.  One attribute,@code{format}, is currently defined for functions.  Others areimplemented for variables and structure fields (@pxref{FunctionAttributes}).@table @code@item format (@var{archetype}, @var{string-index}, @var{first-to-check})@cindex @code{format} attributeThe @code{format} attribute specifies that a function takes @code{printf}or @code{scanf} style arguments which should be type-checked against aformat string.  For example, the declaration:@exampleextern intmy_printf (void *my_object, const char *my_format, ...)      __attribute__ ((format (printf, 2, 3)));@end example@noindentcauses the compiler to check the arguments in calls to @code{my_printf}for consistency with the @code{printf} style format string argument@code{my_format}.The parameter @var{archetype} determines how the format string isinterpreted, and should be either @code{printf} or @code{scanf}.  Theparameter @var{string-index} specifies which argument is the formatstring argument (starting from 1), while @var{first-to-check} is thenumber of the first argument to check against the format string.  Forfunctions where the arguments are not available to be checked (such as@code{vprintf}), specify the third parameter as zero.  In this case thecompiler only checks the format string for consistency.In the example above, the format string (@code{my_format}) is the secondargument of the function @code{my_print}, and the arguments to checkstart with the third argument, so the correct parameters for the formatattribute are 2 and 3.The @code{format} attribute allows you to identify your own functionswhich take format strings as arguments, so that GNU CC can check thecalls to these functions for errors.  The compiler always checks formatsfor the ANSI library functions @code{printf}, @code{fprintf},@code{sprintf}, @code{scanf}, @code{fscanf}, @code{sscanf},@code{vprintf}, @code{vfprintf} and @code{vsprintf} whenever suchwarnings are requested (using @samp{-Wformat}), so there is no need tomodify the header file @file{stdio.h}.@end table@node Function Prototypes@section Prototypes and Old-Style Function Definitions@cindex function prototype declarations@cindex old-style function definitions@cindex promotion of formal parametersGNU C extends ANSI C to allow a function prototype to override a laterold-style non-prototype definition.  Consider the following example:@example/* @r{Use prototypes unless the compiler is old-fashioned.}  */#if __STDC__#define P(x) (x)#else#define P(x) ()#endif/* @r{Prototype function declaration.}  */int isroot P((uid_t));/* @r{Old-style function definition.}  */intisroot (x)   /* ??? lossage here ??? */     uid_t x;@{  return x == 0;@}@end exampleSuppose the type @code{uid_t} happens to be @code{short}.  ANSI C doesnot allow this example, because subword arguments in old-stylenon-prototype definitions are promoted.  Therefore in this example thefunction definition's argument is really an @code{int}, which does notmatch the prototype argument type of @code{short}.This restriction of ANSI C makes it hard to write code that is portableto traditional C compilers, because the programmer does not knowwhether the @code{uid_t} type is @code{short}, @code{int}, or@code{long}.  Therefore, in cases like these GNU C allows a prototypeto override a later old-style definition.  More precisely, in GNU C, afunction prototype argument type overrides the argument type specifiedby a later old-style definition if the former type is the same as thelatter type before promotion.  Thus in GNU C the above example isequivalent to the following:@exampleint isroot (uid_t);intisroot (uid_t x)@{  return x == 0;@}@end example@node Dollar Signs@section Dollar Signs in Identifier Names@cindex $@cindex dollar signs in identifier names@cindex identifier names, dollar signs inIn GNU C, you may use dollar signs in identifier names.  This is becausemany traditional C implementations allow such identifiers.On some machines, dollar signs are allowed in identifiers if you specify@w{@samp{-traditional}}.  On a few systems they are allowed by default,even if you do not use @w{@samp{-traditional}}.  But they are neverallowed if you specify @w{@samp{-ansi}}.There are certain ANSI C programs (obscure, to be sure) that wouldcompile incorrectly if dollar signs were permitted in identifiers.  Forexample:@example#define foo(a) #a#define lose(b) foo (b)#define test$lose (test)@end example@node Character Escapes@section The Character @key{ESC} in ConstantsYou can use the sequence @samp{\e} in a string or character constant tostand for the ASCII character @key{ESC}.@node Alignment@section Inquiring on Alignment of Types or Variables@cindex alignment@cindex type alignment@cindex variable alignmentThe keyword @code{__alignof__} allows you to inquire about how an objectis aligned, or the minimum alignment usually required by a type.  Itssyntax is just like @code{sizeof}.For example, if the target machine requires a @code{double} value to bealigned on an 8-byte boundary, then @code{__alignof__ (double)} is 8.This is true on many RISC machines.  On more traditional machinedesigns, @code{__alignof__ (double)} is 4 or even 2.Some machines never actually require alignment; they allow reference to anydata type even at an odd addresses.  For these machines, @code{__alignof__}reports the @emph{recommended} alignment of a type.When the operand of @code{__alignof__} is an lvalue rather than a type, thevalue is the largest alignment that the lvalue is known to have.  It mayhave this alignment as a result of its data type, or because it is part ofa structure and inherits alignment from that structure. For example, afterthis declaration:@examplestruct foo @{ int x; char y; @} foo1;@end example@noindentthe value of @code{__alignof__ (foo1.y)} is probably 2 or 4, the same as@code{__alignof__ (int)}, even though the data type of @code{foo1.y}does not itself demand any alignment.@refillA related feature which lets you specify the alignment of an object is@code{__attribute__ ((aligned (@var{alignment})))}; see the followingsection.@node Variable Attributes@section Specifying Attributes of Variables@cindex attribute of variables@cindex variable attributesThe keyword @code{__attribute__} allows you to specify specialattributes of variables or structure fields.  This keyword is followedby an attribute specification inside double parentheses.  Fourattributes are currently defined: @code{aligned}, @code{format},@code{mode} and @code{packed}.  @code{format} is used for functions,and thus not documented here; see @ref{Function Attributes}.@table @code@cindex @code{aligned} attribute@item aligned (@var{alignment})This attribute specifies the alignment of the variable or structurefield, measured in bytes.  For example, the declaration:@exampleint x __attribute__ ((aligned (16))) = 0;@end example@noindentcauses the compiler to allocate the global variable @code{x} on a16-byte boundary.  On a 68040, this could be used in conjunction withan @code{asm} expression to access the @code{move16} instruction whichrequires 16-byte aligned operands.You can also specify the alignment of structure fields.  For example, tocreate a double-word aligned @code{int} pair, you could write:@examplestruct foo @{ int x[2] __attribute__ ((aligned (8))); @};@end example@noindentThis is an alternative to creating a union with a @code{double} memberthat forces the union to be double-word aligned.It is not possible to specify the alignment of functions; the alignmentof functions is determined by the machine's requirements and cannot bechanged.  You cannot specify alignment for a typedef name because such aname is just an alias, not a distinct type.The linker of your operating system imposes a maximum alignment.  If thelinker aligns each object file on a four byte boundary, then it isbeyond the compiler's power to cause anything to be aligned to a largerboundary than that.  For example, if  the linker happens to put this objectfile at address 136 (eight more than a multiple of 64), then the compilercannot guarantee an alignment of more than 8 just by aligning variables inthe object file.@item mode (@var{mode})@cindex @code{mode} attributeThis attribute specifies the data type for the declaration---whichevertype corresponds to the mode @var{mode}.  This in effect lets yourequest an integer or floating point type according to its width.@item packed@cindex @code{packed} attributeThe @code{packed} attribute specifies that a variable or structure fieldshould have the smallest possible alignment---one byte for a variable,and one bit for a field, unless you specify a larger value with the@code{aligned} attribute.@end table@node Inline@section An Inline Function is As Fast As a Macro@cindex inline functions@cindex integrating function code@cindex open coding@cindex macros, inline alternativeBy declaring a function @code{inline}, you can direct GNU CC tointegrate that function's code into the code for its callers.  Thismakes execution faster by eliminating the function-call overhead; inaddition, if any of the actual argument values are constant, their knownvalues may permit simplifications at compile time so that not all of theinline function's code needs to be included.  Inlining of functions isan optimization and it really ``works'' only in optimizing compilation.If you don't use @samp{-O}, no function is really inline.To declare a function inline, use the @code{inline} keyword in itsdeclaration, like this:@exampleinline intinc (int *a)@{  (*a)++;@}@end example(If you are writing a header file to be included in ANSI C programs, write@code{__inline__} instead of @code{inline}.  @xref{Alternate Keywords}.)You can also make all ``simple enough'' functions inline with the option@samp{-finline-functions}.  Note that certain usages in a functiondefinition can make it unsuitable for inline substitution.@cindex inline functions, omission ofWhen a function is both inline and @code{static}, if all calls to thefunction are integrated into the caller, and the function's address isnever used, then the function's own assembler code is never referenced.In this case, GNU CC does not actually output assembler code for thefunction, unless you specify the option @samp{-fkeep-inline-functions}.Some calls cannot be integrated for various reasons (in particular,calls that precede the function's definition cannot be integrated, andneither can recursive calls within the definition).  If there is anonintegrated call, then the function is compiled to assembler code asusual.  The function must also be compiled as usual if the programrefers to its address, because that can't be inlined.@cindex non-static inline functionWhen an inline function is not @code{static}, then the compiler must assumethat there may be calls from other source files; since a global symbol canbe defined only once in any program, the function must not be defined inthe other source files, so the calls therein cannot be integrated.Therefore, a non-@code{static} inline function is always compiled on itsown in the usual fashion.If you specify both @code{inline} and @code{extern} in the functiondefinition, then the definition is used only for inlining.  In no caseis the function compiled on its own, not even if you refer to itsaddress explicitly.  Such an address becomes an external reference, asif you had only declared the function, and had not defined it.This combination of @code{inline} and @code{extern} has almost theeffect of a macro.  The way to use it is to put a function definition ina header file with these keywords, and put another copy of thedefinition (lacking @code{inline} and @code{extern}) in a library file.The definition in the header file will cause most calls to the functionto be inlined.  If any uses of the function remain, they will refer tothe single copy in the library.GNU C does not inline any functions when not optimizing.  It is notclear whether it is better to inline or not, in this case, but we foundthat a correct implementation when not optimizing was difficult.  So wedid the easy thing, and turned it off.@node Extended Asm@section Assembler Instructions with C Expression Operands@cindex extended @code{asm}@cindex @code{asm} expressions@cindex assembler instructions@cindex registersIn an assembler instruction using @code{asm}, you can now specify theoperands of the instruction using C expressions.  This means no moreguessing which registers or memory locations will contain the data you wantto use.You must specify an assembler instruction template much like what appearsin a machine description, plus an operand constraint string for eachoperand.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -