📄 ref.calculated.html
字号:
<!-- Copyright (C) 2003 Red Hat, Inc. --><!-- This material may be distributed only subject to the terms --><!-- and conditions set forth in the Open Publication License, v1.0 --><!-- or later (the latest version is presently available at --><!-- http://www.opencontent.org/openpub/). --><!-- Distribution of the work or derivative of the work in any --><!-- standard (paper) book form is prohibited unless prior --><!-- permission is obtained from the copyright holder. --><HTML><HEAD><TITLE>calculated</TITLE><meta name="MSSmartTagsPreventParsing" content="TRUE"><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="The eCos Component Writer's Guide"HREF="cdl-guide.html"><LINKREL="UP"TITLE="CDL Language Specification"HREF="reference.html"><LINKREL="PREVIOUS"TITLE="active_if"HREF="ref.active-if.html"><LINKREL="NEXT"TITLE="compile"HREF="ref.compile.html"></HEAD><BODYCLASS="REFENTRY"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">The <SPANCLASS="APPLICATION">eCos</SPAN> Component Writer's Guide</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="ref.active-if.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="ref.compile.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><H1><ANAME="REF.CALCULATED"><SPANCLASS="PROPERTY">calculated</SPAN></H1><DIVCLASS="REFNAMEDIV"><ANAME="AEN3722"></A><H2>Name</H2>Property <SPANCLASS="PROPERTY">calculated</SPAN> -- Used if the current option's value is not user-modifiable,but is calculated using a suitable CDL expression.</DIV><DIVCLASS="REFSYNOPSISDIV"><ANAME="AEN3726"><H2>Synopsis</H2><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="SYNOPSIS">cdl_option <name> { calculated <expression> …}</PRE></TD></TR></TABLE></DIV><DIVCLASS="REFSECT1"><ANAME="AEN3728"></A><H2>Description</H2><P>In some cases it is useful to have a configuration option whose valuecannot be modified directly by the user. This can be achieved using a<SPANCLASS="PROPERTY">calculated</SPAN>, which takes a CDL expression as argument (see <AHREF="language.values.html#LANGUAGE.EXPRESSION">the Section called <I>Ordinary Expressions</I> in Chapter 3</A> for a description of expressionsyntax). The configuration system evaluates the expression when thecurrent package is loaded and whenever there is a change to any otheroption referenced in the expression. The result depends on theoption's flavor:</P><P></P><DIVCLASS="VARIABLELIST"><DL><DT><TTCLASS="LITERAL">flavor none</TT></DT><DD><P>Options with this flavor have no value, so the <SPANCLASS="PROPERTY">calculated</SPAN>property is not applicable.</P></DD><DT><TTCLASS="LITERAL">flavor bool</TT></DT><DD><P>If the expression evaluates to a non-zero result the option isenabled, otherwise it is disabled.</P></DD><DT><TTCLASS="LITERAL">flavor booldata</TT></DT><DD><P>If the result of evaluating the expression is zero then the option isdisabled, otherwise the option is enabled and its value is the result.</P></DD><DT><TTCLASS="LITERAL">flavor data</TT></DT><DD><P>The value of the option is the result of evaluating the expression.</P></DD></DL></DIV><P>There are a number of valid uses for calculated options, and there arealso many cases where some other CDL facility would be moreappropriate. Valid uses of calculated options include the following:</P><P></P><UL><LI><P>On some target hardware a particular feature may be user-configurable,while on other targets it is fixed. For example some processors canoperate in either big-endian or little-endian mode, while otherprocessors do not provide any choice. It is possible to have anoption <TTCLASS="LITERAL">CYGARC_BIGENDIAN</TT> which is calculated insome architectural HAL packages but user-modifiable in others.</P></LI><LI><P>Calculated options can provide an alternative way for one package toaffect the behavior of another one. Suppose a package may provide twopossible implementations, a preferred one involving self-modifyingcode and a slower alternative. If the system involves a ROM bootstrapthen the slower alternative must be used, but it would beinappropriate to modify the startup option in every HAL to imposeconstraints on this package. Instead it is possible to have acalculated option whose value is<TTCLASS="LITERAL">{ CYG_HAL_STARTUP == "ROM" }</TT>,and which has appropriate consequences. Arguably this is a spuriousexample, and it should be a user choice whether or not to useself-modifying code with a <SPANCLASS="PROPERTY">default_value</SPAN> based on<TTCLASS="LITERAL">CYG_HAL_STARTUP</TT>, but that is for the componentwriter to decide.</P></LI><LI><P>Sometimes it should be possible to perform a particular test atcompile-time, for example by using a C preprocessor<TTCLASS="LITERAL">#if</TT> construct. However the preprocessor has onlylimited functionality, for example it cannot perform stringcomparisons. CDL expressions are more powerful.</P></LI><LI><P>Occasionally a particular sub-expression may occur multiple times ina CDL script. If the sub-expression is sufficiently complex then itmay be worthwhile to have a calculated option whose value is thesub-expression, and then reference that calculated option in theappropriate places. </P></LI></UL><P>Alternatives to using calculated options include the following:</P><P></P><UL><LI><P>CDL <AHREF="language.interface.html">interfaces</A> are a form ofcalculated option intended as an abstraction mechanism. An interfacecan be used to express the concept of <SPANCLASS="emphasis"><ICLASS="EMPHASIS">anyscheduler</I></SPAN>, as opposed to a specific one such as the bitmap scheduler.</P></LI><LI><P>If a calculated option would serve only to add additional informationto a configuration header file, it may be possible to achieve the sameeffect using a <AHREF="ref.define-proc.html"><SPANCLASS="PROPERTY">define_proc</SPAN></A>property or one of the other properties related to header filegeneration.</P></LI></UL><DIVCLASS="TIP"><BLOCKQUOTECLASS="TIP"><P><B>Tip: </B>If the first entry in a <SPANCLASS="PROPERTY">calculated</SPAN> expression is a negativenumber, for example <TTCLASS="LITERAL">calculated -1</TT> then thiscan be misinterpreted as an option instead of as part of theexpression. Currently the <SPANCLASS="PROPERTY">calculated</SPAN> property does not take anyoptions, but this may change in future. Option processing halts at thesequence <TTCLASS="LITERAL">--</TT>, so the desired value can be expressedsafely using<TTCLASS="LITERAL">calculated -- -1</TT></P></BLOCKQUOTE></DIV><DIVCLASS="WARNING"><P></P><TABLECLASS="WARNING"BORDER="1"WIDTH="100%"><TR><TDALIGN="CENTER"><B>Warning</B></TD></TR><TR><TDALIGN="LEFT"><P>Some of the CDL scripts in current <SPANCLASS="APPLICATION">eCos</SPAN> releases make excessive useof calculated options. This is partly because the recommendedalternatives were not always available at the time the scripts werewritten. It is also partly because there is still some missingfunctionality, for example <SPANCLASS="PROPERTY">define_proc</SPAN> properties cannot yet accessthe configuration data so it may be necessary to use <SPANCLASS="PROPERTY">calculated</SPAN>properties to access the data and perform the desired manipulation viaa <SPANCLASS="APPLICATION">CDL</SPAN> expression. New scripts should use calculated options only inaccordance with the guidelines.</P></TD></TR></TABLE></DIV><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B>For options with the booldata flavor the current CDL syntax does notallow the enabled flag and the value to be calculated separately.Functionality to permit this may be added in a future release.</P></BLOCKQUOTE></DIV><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B>It has been suggested that having options which are notuser-modifiable is potentially confusing, and that a top-level<TTCLASS="LITERAL">cdl_constant</TT> command should be added to thelanguage instead of or in addition to the <SPANCLASS="PROPERTY">calculated</SPAN> property. Sucha change is under consideration. However because the value of acalculated option can depend on other options, it is not necessarilyconstant.</P></BLOCKQUOTE></DIV></DIV><DIVCLASS="REFSECT1"><ANAME="AEN3799"></A><H2>Example</H2><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING"># A constant on some target hardware, perhaps user-modifiable on other# targets.cdl_option CYGNUM_HAL_RTC_PERIOD { display "Real-time clock period" flavor data calculated 12500}</PRE></TD></TR></TABLE></DIV><DIVCLASS="REFSECT1"><ANAME="AEN3802"></A><H2>See Also</H2><P>Properties <AHREF="ref.default-value.html"><SPANCLASS="PROPERTY">default_value</SPAN></A>,<AHREF="ref.flavor.html"><SPANCLASS="PROPERTY">flavor</SPAN></A> and<AHREF="ref.legal-values.html"><SPANCLASS="PROPERTY">legal_values</SPAN></A>,</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="ref.active-if.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="cdl-guide.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="ref.compile.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><SPANCLASS="PROPERTY">active_if</SPAN></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="reference.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><SPANCLASS="PROPERTY">compile</SPAN></TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -