PageRenderTime 56ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/web/piffle-html-0.1.2/piffle.html

http://piffle.googlecode.com/
HTML | 1306 lines | 1284 code | 21 blank | 1 comment | 0 complexity | e87973a38f0c28f431ab434c9334aef5 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html >
  4. <head><title>Piffle A Packet Filter Language</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <meta name="generator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/mn.html)">
  7. <meta name="originator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/mn.html)">
  8. <!-- html -->
  9. <meta name="src" content="piffle.tex">
  10. <meta name="date" content="2007-06-05 11:59:00">
  11. <link rel="stylesheet" type="text/css" href="piffle.css">
  12. </head><body
  13. >
  14. <div class="maketitle"><div class="titlepage">
  15. <table class="minipage"><tr><td>Vrije Universiteit Amsterdam
  16. <br class="newline" />MPhil Parallel and Distributed Computer Systems
  17. <br class="newline" />Compiler Construction Practical</td></tr></table>
  18. <h2 class="titleHead"><h2 class="titleHead">Piffle<br />
  19. A Packet Filter Language</h2></h2>
  20. <div class="author" ><div class="author" ><span
  21. class="cmr-17">Jaap Weel</span></div></div>
  22. <br />
  23. <div class="date" ><div class="date" ><span
  24. class="cmr-17">Version 0.1.2 </span></div></div>
  25. </div> </div>
  26. <h2 class="likechapterHead"><a
  27. id="x1-1000"></a>Contents</h2> <div class="tableofcontents">
  28. <span class="chapterToc">1&#x00A0;&#x00A0;<a
  29. href="#x1-20001" id="QQ2-1-2">Notices</a></span>
  30. <br /> &#x00A0;<span class="sectionToc">1.1&#x00A0;&#x00A0;<a
  31. href="#x1-30001.1" id="QQ2-1-3">Latest version of this document</a></span>
  32. <br /> &#x00A0;<span class="sectionToc">1.2&#x00A0;&#x00A0;<a
  33. href="#x1-40001.2" id="QQ2-1-4">Copyright notice</a></span>
  34. <br /> <span class="chapterToc">2&#x00A0;&#x00A0;<a
  35. href="#x1-50002" id="QQ2-1-5">Introduction</a></span>
  36. <br /> <span class="chapterToc">3&#x00A0;&#x00A0;<a
  37. href="#x1-60003" id="QQ2-1-6">Bounded packet filters</a></span>
  38. <br /> &#x00A0;<span class="sectionToc">3.1&#x00A0;&#x00A0;<a
  39. href="#x1-70003.1" id="QQ2-1-7">Dynamic bounds on packet filters</a></span>
  40. <br /> &#x00A0;<span class="sectionToc">3.2&#x00A0;&#x00A0;<a
  41. href="#x1-80003.2" id="QQ2-1-8">Static bounds on arbitrary packet filters</a></span>
  42. <br /> &#x00A0;<span class="sectionToc">3.3&#x00A0;&#x00A0;<a
  43. href="#x1-90003.3" id="QQ2-1-9">Proof carrying code</a></span>
  44. <br /> &#x00A0;<span class="sectionToc">3.4&#x00A0;&#x00A0;<a
  45. href="#x1-100003.4" id="QQ2-1-10">Bounds on restricted packet filters</a></span>
  46. <br /> <span class="chapterToc">4&#x00A0;&#x00A0;<a
  47. href="#x1-110004" id="QQ2-1-11">Language definition</a></span>
  48. <br /> &#x00A0;<span class="sectionToc">4.1&#x00A0;&#x00A0;<a
  49. href="#x1-120004.1" id="QQ2-1-12">Vocabulary and representation</a></span>
  50. <br /> &#x00A0;<span class="sectionToc">4.2&#x00A0;&#x00A0;<a
  51. href="#x1-130004.2" id="QQ2-1-13">Declarations</a></span>
  52. <br /> &#x00A0;<span class="sectionToc">4.3&#x00A0;&#x00A0;<a
  53. href="#x1-140004.3" id="QQ2-1-14">Types</a></span>
  54. <br /> &#x00A0;<span class="sectionToc">4.4&#x00A0;&#x00A0;<a
  55. href="#x1-150004.4" id="QQ2-1-15">Expressions</a></span>
  56. <br /> &#x00A0;<span class="sectionToc">4.5&#x00A0;&#x00A0;<a
  57. href="#x1-160004.5" id="QQ2-1-16">Operators</a></span>
  58. <br /> &#x00A0;<span class="sectionToc">4.6&#x00A0;&#x00A0;<a
  59. href="#x1-170004.6" id="QQ2-1-17">The types of expressions</a></span>
  60. <br /> &#x00A0;<span class="sectionToc">4.7&#x00A0;&#x00A0;<a
  61. href="#x1-180004.7" id="QQ2-1-18">Constant expressions</a></span>
  62. <br /> &#x00A0;<span class="sectionToc">4.8&#x00A0;&#x00A0;<a
  63. href="#x1-190004.8" id="QQ2-1-19">Source files</a></span>
  64. <br /> &#x00A0;<span class="sectionToc">4.9&#x00A0;&#x00A0;<a
  65. href="#x1-200004.9" id="QQ2-1-20">The filter function</a></span>
  66. <br /> &#x00A0;<span class="sectionToc">4.10&#x00A0;&#x00A0;<a
  67. href="#x1-210004.10" id="QQ2-1-21">Preprocessor</a></span>
  68. <br /> <span class="chapterToc">5&#x00A0;&#x00A0;<a
  69. href="#x1-220005" id="QQ2-1-22">The Piffle compiler, pfc</a></span>
  70. <br /> &#x00A0;<span class="sectionToc">5.1&#x00A0;&#x00A0;<a
  71. href="#x1-230005.1" id="QQ2-1-23">The pfc(1) manual page</a></span>
  72. <br /> <span class="chapterToc">6&#x00A0;&#x00A0;<a
  73. href="#x1-240006" id="QQ2-1-24">The pcap boilerplate, pcap.c</a></span>
  74. <br /> &#x00A0;<span class="sectionToc">6.1&#x00A0;&#x00A0;<a
  75. href="#x1-250006.1" id="QQ2-1-25">The pfc.pcap(1) manual page</a></span>
  76. <br /> &#x00A0;<span class="sectionToc">6.2&#x00A0;&#x00A0;<a
  77. href="#x1-260006.2" id="QQ2-1-26">Writing programs for the pcap.c boilerplate</a></span>
  78. <br /> <span class="chapterToc">7&#x00A0;&#x00A0;<a
  79. href="#x1-270007" id="QQ2-1-27">The test boilerplate, test.c</a></span>
  80. <br /> &#x00A0;<span class="sectionToc">7.1&#x00A0;&#x00A0;<a
  81. href="#x1-280007.1" id="QQ2-1-28">The pfc.test(1) manual page</a></span>
  82. <br /> <span class="chapterToc">8&#x00A0;&#x00A0;<a
  83. href="#x1-290008" id="QQ2-1-29">An example of a packet filter</a></span>
  84. <br /> &#x00A0;<span class="sectionToc">8.1&#x00A0;&#x00A0;<a
  85. href="#x1-300008.1" id="QQ2-1-30">A packet filter in PFL</a></span>
  86. <br /> &#x00A0;<span class="sectionToc">8.2&#x00A0;&#x00A0;<a
  87. href="#x1-310008.2" id="QQ2-1-31">The packet filter translated to C by pfc</a></span>
  88. </div>
  89. <h2 class="chapterHead"><span class="titlemark">1</span>.&#x00A0;&#x00A0;<a
  90. id="x1-20001"></a>Notices</h2>
  91. <h3 class="sectionHead"><span class="titlemark">1.1</span>.&#x00A0;&#x00A0;<a
  92. id="x1-30001.1"></a>Latest version of this document</h3>
  93. <!--l. 23--><p class="noindent">For up-to-date information on Piffle, and the latest version of this document, be sure to visit
  94. the following web site:
  95. <!--l. 26--><p class="indent"> <span
  96. class="cmti-12">http://code.google.com/p/piffle/wiki/PiffleWiki</span>
  97. <!--l. 28--><p class="noindent">
  98. <h3 class="sectionHead"><span class="titlemark">1.2</span>.&#x00A0;&#x00A0;<a
  99. id="x1-40001.2"></a>Copyright notice</h3>
  100. <!--l. 30--><p class="noindent">Copyright (c) 2007, Jaap Weel. This work is licensed under the Creative Commons
  101. Attribution&#8211;Share-Alike 3.0 License. To view a copy of this license, visit
  102. <!--l. 34--><p class="indent"> <span
  103. class="cmti-12">http://creativecommons.org/licenses/by-sa/3.0/</span>
  104. <!--l. 36--><p class="indent"> or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco,
  105. California, 94105, USA.
  106. <h2 class="chapterHead"><span class="titlemark">2</span>.&#x00A0;&#x00A0;<a
  107. id="x1-50002"></a>Introduction</h2>
  108. <!--l. 44--><p class="noindent">Piffle is a pronounceable form of PFL, that is, Packet Filter Language. (&#8220;Piffle&#8221; is also an
  109. obscure word for &#8220;chatter&#8221;.) Piffle is a very simple programming language for writing network
  110. packet filters, with one special feature: the compiler can enforce limits on the processor time
  111. and memory used for processing each packet of input. This is accomplished by passing
  112. arguments to the &#8211;C and &#8211;M command line options. Memory bounds are in words, more or
  113. less, and CPU bounds are in arbitrary units.
  114. <!--l. 53--><p class="indent"> In this document, I will first consider in general the problem of checking arbitrary programs
  115. for transgression of such resource bounds, and notice that it is very hard. I will conclude that
  116. in Piffle we must punt and restrict the class of programs that can be used as packet filters,
  117. which makes the problem comparatively easy.
  118. <!--l. 59--><p class="indent"> After that, I will give the definition of the Piffle language, and some instructions for how to
  119. use the Piffle compiler.
  120. <h2 class="chapterHead"><span class="titlemark">3</span>.&#x00A0;&#x00A0;<a
  121. id="x1-60003"></a>Bounded packet filters</h2>
  122. <!--l. 65--><p class="noindent">An important requirement in designing network packet processing code is that the amount of
  123. time and memory spent processing each packet be bounded. We do not want the entire
  124. network connection grinding to a halt because of an unforeseen infinite loop in a packet filter.
  125. There are several ways to prevent such havoc. In this chapter, I&#x00A0;will discuss each of them, and
  126. explain which one I chose and why.
  127. <h3 class="sectionHead"><span class="titlemark">3.1</span>.&#x00A0;&#x00A0;<a
  128. id="x1-70003.1"></a>Dynamic bounds on packet filters</h3>
  129. <!--l. 74--><p class="noindent">We can check bounds at runtime. For instance, we can run the packet filter on each packet for
  130. a specified quota of clock ticks and drop the packet if processing has not finished by the time
  131. the quota runs out.
  132. <!--l. 79--><p class="indent"> One problem with this runtime approach is that a bug in the packet filters can
  133. remain hidden for a long time until some unusual packet is received that triggers the
  134. bug.
  135. <!--l. 83--><p class="indent"> Another problem is that it is not clear exactly what to do when the bound is exceeded:
  136. Should we drop the packet? Should we pass it along unaltered? We would like to be able to
  137. give the packet filter author the opportunity to decide what to do with packets that take too
  138. long to analyze. But if we allow that sort of flexibility within the dynamic checking framework,
  139. we will inevitably have to allow a chunk of cleanup code to deal with &#8220;slow&#8221; packets, which
  140. in turn can only run for a restricted amount of time. If <span
  141. class="cmti-12">that </span>time is exceeded, do
  142. we need a third program to deal with slow packet filters that have slow cleanup
  143. code?
  144. <!--l. 94--><p class="noindent">
  145. <h3 class="sectionHead"><span class="titlemark">3.2</span>.&#x00A0;&#x00A0;<a
  146. id="x1-80003.2"></a>Static bounds on arbitrary packet filters</h3>
  147. <!--l. 96--><p class="noindent">Given the problems with checking bounds at run time, we might want to check at compile
  148. time. To do that, we need a language in which we can express exactly those packet filter
  149. programs that execute within given time and space bounds.
  150. <!--l. 101--><p class="indent"> Determining whether a given program runs within these bounds sounds like it ought to be
  151. undecidable, but it is not. After all, we are not trying to evaluate whether a given packet filter
  152. will terminate <span
  153. class="cmti-12">at all, in the end</span>; we merely want it to terminate within a given number of
  154. steps. (The space bound problem is analogous.)
  155. <!--l. 107--><p class="indent"> The problem of determining whether a given Turing machine will terminate within a certain
  156. number of steps is, in fact, decidable. In a limited number <span
  157. class="cmmi-12">N </span>of steps the machine can read
  158. only a limited amount <span
  159. class="cmmi-12">N </span>of its tape, which means we could check whether the time bound by
  160. exhaustively running the machine for at most <span
  161. class="cmmi-12">N </span>steps on all tapes of length at most
  162. <span
  163. class="cmmi-12">N</span>.
  164. <!--l. 114--><p class="indent"> It should be obvious that this procedure, while quite satisfactory as a constructive proof of
  165. decidability, is impractical, because the process of bounds checking itself would take lots of
  166. time (in fact, exponential in <span
  167. class="cmmi-12">N</span>). What we need is a bound on the process of bounds
  168. checking.
  169. <!--l. 120--><p class="noindent">
  170. <h3 class="sectionHead"><span class="titlemark">3.3</span>.&#x00A0;&#x00A0;<a
  171. id="x1-90003.3"></a>Proof carrying code</h3>
  172. <!--l. 122--><p class="noindent">One way to design a language that would let us check bounds at compile time would be to
  173. annotate the program with a proof of the statement that it will terminate within a given
  174. number of steps.
  175. <!--l. 126--><p class="indent"> Proofs could be provided either as annotations or as part of a type
  176. system.<span class="footnote-mark"><a
  177. href="piffle2.html#fn1x5"><sup class="textsuperscript">1</sup></a></span><a
  178. id="x1-9001f1"></a>
  179. The use of proof carrying code would allow all packet filters to be written that can be
  180. proven to execute within bounds, and it would allow for the bounds checking problem
  181. itself to be tractable. Depending on the details of the formal system used, proof
  182. (or type) checking can be polynomial (and often linear) in the length of the proof.
  183. Therefore, bound checking of annotated code complies with the rule of thumb that
  184. compilers should use only algorithms that are at most polynomial in the length of the
  185. program.
  186. <!--l. 137--><p class="indent"> The practical liabilities of proof carrying code are well known. Programmers are unfamiliar
  187. with proof systems, and those who know their way around them could still have trouble
  188. coming up with the (possibly lengthy) proofs in individual cases. Therefore, we
  189. will not pursue proof carrying code any further now, and turn instead to a simpler
  190. solution.
  191. <!--l. 144--><p class="noindent">
  192. <h3 class="sectionHead"><span class="titlemark">3.4</span>.&#x00A0;&#x00A0;<a
  193. id="x1-100003.4"></a>Bounds on restricted packet filters</h3>
  194. <!--l. 146--><p class="noindent">If we do not want to get bogged down in the depths of formal type theory, but still design a
  195. language that would let us check bounds at compile time, we arrive at what is, in fact, the
  196. traditional solution to the problem of bounded packet filters. We will construct a programming
  197. language out of familiar abstractions, and that language will allow us to express a
  198. <span
  199. class="cmti-12">large but arbitrary </span>subset of the set of all packet filters that execute in bounded
  200. time.
  201. <!--l. 154--><p class="indent"> Such a language is not Turing complete, but if it is designed intelligently, it can
  202. cover a large fraction of the problems that people may want to solve using packet
  203. filters.
  204. <!--l. 158--><p class="indent"> An example of a packet filter language based on familiar programming abstractions is the traditional BSD
  205. packet filter <span
  206. class="cmti-12">bpf</span>.<span class="footnote-mark"><a
  207. href="piffle3.html#fn2x5"><sup class="textsuperscript">2</sup></a></span><a
  208. id="x1-10001f2"></a>
  209. Its packet filters are written in a simple machine code with a little twist: the virtual machine
  210. allows jumps forward, but not backward. That way, the time spent executing any given packet
  211. filter program is at most proportional to the length of the program.
  212. <!--l. 167--><p class="indent"> For Piffle, we take a similar approach. Piffle is not based on machine language, but on block
  213. structured, procedural programming languages such as C and Pascal. Just as the <span
  214. class="cmti-12">bpf </span>language
  215. omits backward jumps from machine code, Piffle omits unbounded looping constructs and
  216. recursive procedure calls from block structured, procedural code. Piffle does provide some
  217. bounded looping constructs, and procedure calls are allowed as long as the call graph contains
  218. no cycles.
  219. <h2 class="chapterHead"><span class="titlemark">4</span>.&#x00A0;&#x00A0;<a
  220. id="x1-110004"></a>Language definition</h2>
  221. <!--l. 193--><p class="noindent">The Piffle language is defined as a set of sentences, called source files, well formed according to
  222. the Piffle syntax. To describe the syntax, I will use a modified version of the Backus Normal
  223. Form (also known as Backus-Naur Formalism or BNF), using <span
  224. class="cmmi-12">x</span><sup><span
  225. class="cmr-8">?</span></sup> to indicate that <span
  226. class="cmmi-12">x</span>
  227. may appear either once or not at all, <span
  228. class="cmmi-12">x</span><sup><span
  229. class="cmr-8">+</span></sup> to indicate that <span
  230. class="cmmi-12">x </span>may appear at least
  231. once, and <span
  232. class="cmmi-12">x</span><sup><span
  233. class="cmsy-8">*</span></sup> to indicate that <span
  234. class="cmmi-12">x </span>may appear any number of times, including not at
  235. all.
  236. <!--l. 201--><p class="indent"> I will not provide a formal definition of the semantics of Piffle. In general, when I do not
  237. specify the meaning or type of some Piffle construct, it can be assumed that it is analogous to
  238. the semantics of its closest analog in C.
  239. <h3 class="sectionHead"><span class="titlemark">4.1</span>.&#x00A0;&#x00A0;<a
  240. id="x1-120004.1"></a>Vocabulary and representation</h3>
  241. <!--l. 208--><p class="noindent">A well formed Piffle program is composed of tokens, which are identifiers, literals, and reserved
  242. tokens. Each token is a sequence of characters, and may not contain whitespace unless this is
  243. explicitly specified. Any amount of whitespace may be inserted between tokens, and indeed
  244. must be inserted between any two tokens that would make a valid token when put
  245. together.
  246. <!--l. 215--><p class="indent"> Whitespace is any sequence of blanks, tabs, vertical tabs, form feeds, line feeds, and carriage
  247. returns (in C notation, any character in the string <span class="obeylines-h"><span class="verb"><span
  248. class="cmtt-12">"</span><span
  249. class="cmtt-12">&#x00A0;\v\f\t\r\n"</span></span></span>). Comments may also
  250. appear wherever whitespace can appear. (Unlike in C, they may <span
  251. class="cmti-12">not </span>appear inside of a token.)
  252. In-line comments may be inserted between any two tokens. They are delimited by <span
  253. class="cmtt-12">/* </span>and <span
  254. class="cmtt-12">*/</span>
  255. and may be nested. End-of-line comments may also be inserted between any two tokens,
  256. and they run from <span
  257. class="cmtt-12">// </span>to the next line end, or the end of the file, whichever comes
  258. first.
  259. <!--l. 225--><p class="indent"> Identifiers are sequences of letters, digits, and underscores. The first character may not be a
  260. digit. <div class="eqnarray">
  261. <center class="math-display" >
  262. <img
  263. src="piffle0x.png" alt=" |--| |-| *
  264. ident = (-letter |-x-)|(letter |-x-|-digit)
  265. letter = |A || &#x22C5;&#x22C5;&#x22C5; ||Z|| a || &#x22C5;&#x22C5;&#x22C5; | z|
  266. |--| |--| --- ---
  267. digit = |0 || &#x22C5;&#x22C5;&#x22C5; ||9|
  268. --- ---
  269. " class="math-display" ></center>
  270. </div>
  271. <!--l. 235--><p class="indent"> The literals available in Piffle, are numbers, the <span
  272. class="cmtt-12">unit </span>literal, which is the canonical value of
  273. type <span
  274. class="cmtt-12">void</span>, <span
  275. class="cmtt-12">true</span>, which means the same as <span
  276. class="cmtt-12">1 :</span><span
  277. class="cmtt-12">&#x00A0;bool</span>, and <span
  278. class="cmtt-12">false</span>, which means the same as <span
  279. class="cmtt-12">0</span>
  280. <span
  281. class="cmtt-12">:</span><span
  282. class="cmtt-12">&#x00A0;bool</span>. The only numbers available are integers. They can be represented as decimal, octal,
  283. or hexidecimal, the same way as in Haskell 98. Note that this differs from C syntax in that
  284. leading 0s are simply ignored, and the prefix for octal is 0o or 0O rather than simply
  285. 0.
  286. <!--l. 253--><p class="indent"> <div class="eqnarray">
  287. <center class="math-display" >
  288. <img
  289. src="piffle1x.png" alt=" |----|- |-----| |------|
  290. literal = unit---|-true--|-false---| integer| |--|
  291. integer = digit+ | ( 0o|| |0O ) octit+| ( 0x || |0X|) hexit+
  292. |-| |--| |-|- |-| |-|-- ---|-|
  293. hexit = 0 || &#x22C5;&#x22C5;&#x22C5; ||9|| A || &#x22C5; &#x22C5;&#x22C5; | F|| a|| &#x22C5;&#x22C5;&#x22C5; | f|
  294. |-| |--| --- --- --- ---
  295. octit = 0--| &#x22C5;&#x22C5;&#x22C5; |-7|
  296. " class="math-display" ></center>
  297. </div>
  298. <!--l. 255--><p class="noindent">
  299. <h3 class="sectionHead"><span class="titlemark">4.2</span>.&#x00A0;&#x00A0;<a
  300. id="x1-130004.2"></a>Declarations</h3>
  301. <!--l. 257--><p class="noindent">Piffle allows no recursion, and all functions must be defined before they are used. Therefore,
  302. there is no need for forward declarations. If the return type of a function is omitted, <span
  303. class="cmtt-12">void </span>is
  304. implied. The meaning of duplicate definitions is undefined.
  305. <!--l. 270--><p class="indent"> <div class="eqnarray">
  306. <center class="math-display" >
  307. <img
  308. src="piffle2x.png" alt="declaration = vardec | fundec
  309. |----| |-|
  310. vardec = |var-|ident |:|type |-| |-| |-|
  311. fundec = |fun |ident |(|argl? |)| (|:| type )? = |expression
  312. ----- |-| --- |-| --- |-|- ---
  313. argl = ident :--type ( ,--ident -:- type )*
  314. " class="math-display" ></center>
  315. </div>
  316. <!--l. 272--><p class="noindent">
  317. <h3 class="sectionHead"><span class="titlemark">4.3</span>.&#x00A0;&#x00A0;<a
  318. id="x1-140004.3"></a>Types</h3>
  319. <!--l. 274--><p class="noindent">A type in Piffle is <span
  320. class="cmtt-12">void</span>, an atomic type, or an array. Atomic types come in signed and
  321. unsigned, and have 8, 16, or 32 bits. Each array type encompasses only arrays of a specific
  322. length; you can declare an &#8220;array of 1024 integers&#8221;, but not an &#8220;array of integers&#8221;. This is an
  323. important feature, because it is what enables the calculation of resource limits. It is
  324. at present <span
  325. class="cmti-12">not </span>actually used for anything else; in particular, there are no bounds
  326. checks.
  327. <!--l. 290--><p class="indent"> <div class="eqnarray">
  328. <center class="math-display" >
  329. <img
  330. src="piffle3x.png" alt=" |-----| |--| |-|
  331. type = void---| atomic | atomic-[-|integer ]--
  332. |-----| |---| |---| |----| |--| |----| |----|
  333. atomic = bool---|-u8-|| u16-- |-u32-|| s8--| -s16-||-s32-|
  334. " class="math-display" ></center>
  335. </div>
  336. <!--l. 292--><p class="indent"> Whenever a value of type of void is expected, a value of any other type will do as
  337. well.
  338. <!--l. 295--><p class="noindent">
  339. <h3 class="sectionHead"><span class="titlemark">4.4</span>.&#x00A0;&#x00A0;<a
  340. id="x1-150004.4"></a>Expressions</h3>
  341. <!--l. 297--><p class="noindent">Unlike in C or Pascal, in Piffle there is no syntactic distinction between expressions and
  342. statements; every expression can be used as a statement and vice versa.
  343. <!--l. 327--><p class="indent"> <div class="eqnarray">
  344. <center class="math-display" >
  345. <img
  346. src="piffle4x.png" alt=" *
  347. expression = expression2 (binop expression2 ) |--| |-|
  348. expression2 = unop *expression3 ( [--expression -]-) ? ( :-atomic )?
  349. expression3 = literal |--| |-| |--|
  350. | ident (-(-|(expression ( ,--expression) *)?-)-) ?
  351. |-| |--|
  352. | -(-expression -)-| ---
  353. | | |-| * |-| * | |
  354. | |{--(declaration |;--)-( expression ;--)--}--
  355. | |if |expression then |expression (|else |expression )?
  356. |----| |--|------- |------|----- |---|
  357. | -for-|ident in--expression ( -while--expression) ?-do-|expression
  358. |----| |-----| |---|
  359. | -for-|ident from---integer -to-|integer
  360. |-------| ? |--|
  361. (-while--|expression ) do--expression
  362. " class="math-display" ></center>
  363. </div>
  364. <!--l. 329--><p class="indent"> Cast expressions (like <span
  365. class="cmtt-12">x : u32</span>) allow the use of a value of one atomic type with another
  366. atomic type. The exact meaning of such conversions is undefined but expected to correspond
  367. to the behavior of the C compiler on the platform in use.
  368. <!--l. 334--><p class="indent"> Blocks have the value of the last expression in them (which is why there is no <span
  369. class="cmtt-12">return</span>
  370. statement); if a block contains no expressions, <span
  371. class="cmtt-12">unit </span>is implied.
  372. <!--l. 338--><p class="indent"> If-then-else statements have values, and correspond more closely to the C ternary
  373. operator (<span
  374. class="cmtt-12">?:</span>) than to the C if-else statement. If an <span
  375. class="cmtt-12">else </span>clause is omitted, <span
  376. class="cmtt-12">unit </span>is
  377. implied.
  378. <!--l. 342--><p class="indent"> Loops in Piffle are somewhat peculiar, in order to accommodate resource bound checking.
  379. There are two types of loops: one will iterate with a variable sequentially bound to each
  380. number in a range (which includes both bounds), and the other will iterate with a
  381. variable sequentially bound to each element in an array. Both allow the use of a <span
  382. class="cmtt-12">while</span>
  383. clause, which will be evaluated before each iteration; if it evaluates to 0, the loop is
  384. aborted immediately. The iterator variable is bound within the while clause, so it is
  385. perfectly valid to say something like <span
  386. class="cmtt-12">for i from 0 to 10 while is</span><span
  387. class="cmtt-12">_useful(i) do</span>
  388. <span
  389. class="cmtt-12">...</span>.
  390. <!--l. 354--><p class="noindent">
  391. <h3 class="sectionHead"><span class="titlemark">4.5</span>.&#x00A0;&#x00A0;<a
  392. id="x1-160004.5"></a>Operators</h3>
  393. <!--l. 356--><p class="noindent">The operators are given by <div class="eqnarray">
  394. <center class="math-display" >
  395. <img
  396. src="piffle5x.png" alt=" |--| |-| |-| |-| |-| |---| |---| |-|
  397. binop = |*-|| /--| %|-||+-||----|--&#x003C;&#x003C;-||-&#x003E;&#x003E;-|||&#x003C;--
  398. | |&#x003E; || &#x003C;= || &#x003E;= | ||== |||!= || &amp; || |^|
  399. |--| |--| |--| |--| |---| |--| |--|
  400. | -|-|| &amp;&amp;--| |-|- |-=-||-+=-|| -=--| -*=-
  401. |---| |--| |---| |---| |--| |----| |----|
  402. | -%=-|| /=--| -|=-||-&amp;=-|| ^=--| -&#x003E;&#x003E;=-||-&#x003C;&#x003C;=-|
  403. |--| |-| |-| |-|
  404. unop = -+-|| ---| ~--| !--
  405. " class="math-display" ></center>
  406. </div>
  407. <!--l. 370--><p class="indent"> The grammar as specified so far correctly and completely specifies the language, but it is
  408. misleading and ambiguous, because it does not indicate the meaning of certain compound
  409. expressions. In fact, the actual Piffle parser will respect the standard C precedence rules, as
  410. summarized in the following table.
  411. <div class="tabular"><table class="tabular"
  412. cellspacing="0" cellpadding="0"
  413. ><colgroup id="TBL-2-1g"><col
  414. id="TBL-2-1"><col
  415. id="TBL-2-2"></colgroup><tr
  416. class="hline"><td><hr></td><td><hr></td></tr><tr
  417. class="hline"><td><hr></td><td><hr></td></tr><tr
  418. style="vertical-align:baseline;" id="TBL-2-1-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-1-1"
  419. class="td11">Operators (ordered from tight to loose) </td><td style="white-space:nowrap; text-align:left;" id="TBL-2-1-2"
  420. class="td11">Associativity</td>
  421. </tr><tr
  422. class="hline"><td><hr></td><td><hr></td></tr><tr
  423. style="vertical-align:baseline;" id="TBL-2-2-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-2-1"
  424. class="td11"><span class="obeylines-h"><span class="verb"><span
  425. class="cmtt-12">*</span></span></span> <span class="obeylines-h"><span class="verb"><span
  426. class="cmtt-12">/</span></span></span> <span class="obeylines-h"><span class="verb"><span
  427. class="cmtt-12">%</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-2-2-2"
  428. class="td11">left-to-right </td>
  429. </tr><tr
  430. style="vertical-align:baseline;" id="TBL-2-3-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-3-1"
  431. class="td11"><span class="obeylines-h"><span class="verb"><span
  432. class="cmtt-12">+</span></span></span> <span class="obeylines-h"><span class="verb"><span
  433. class="cmtt-12">-</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-2-3-2"
  434. class="td11">left-to-right </td>
  435. </tr><tr
  436. style="vertical-align:baseline;" id="TBL-2-4-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-4-1"
  437. class="td11"><span class="obeylines-h"><span class="verb"><span
  438. class="cmtt-12">&#x003C;&#x003C;</span></span></span> <span class="obeylines-h"><span class="verb"><span
  439. class="cmtt-12">&#x003E;&#x003E;</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-2-4-2"
  440. class="td11">left-to-right </td>
  441. </tr><tr
  442. style="vertical-align:baseline;" id="TBL-2-5-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-5-1"
  443. class="td11"><span class="obeylines-h"><span class="verb"><span
  444. class="cmtt-12">&#x003C;</span></span></span> <span class="obeylines-h"><span class="verb"><span
  445. class="cmtt-12">&#x003E;</span></span></span> <span class="obeylines-h"><span class="verb"><span
  446. class="cmtt-12">&#x003C;=</span></span></span> <span class="obeylines-h"><span class="verb"><span
  447. class="cmtt-12">&#x003E;=</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-2-5-2"
  448. class="td11">left-to-right </td>
  449. </tr><tr
  450. style="vertical-align:baseline;" id="TBL-2-6-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-6-1"
  451. class="td11"><span class="obeylines-h"><span class="verb"><span
  452. class="cmtt-12">==</span></span></span> <span class="obeylines-h"><span class="verb"><span
  453. class="cmtt-12">!=</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-2-6-2"
  454. class="td11">left-to-right </td>
  455. </tr><tr
  456. style="vertical-align:baseline;" id="TBL-2-7-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-7-1"
  457. class="td11"><span class="obeylines-h"><span class="verb"><span
  458. class="cmtt-12">&amp;</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-2-7-2"
  459. class="td11">left-to-right </td>
  460. </tr><tr
  461. style="vertical-align:baseline;" id="TBL-2-8-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-8-1"
  462. class="td11"><span class="obeylines-h"><span class="verb"><span
  463. class="cmtt-12">^</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-2-8-2"
  464. class="td11">left-to-right </td>
  465. </tr><tr
  466. style="vertical-align:baseline;" id="TBL-2-9-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-9-1"
  467. class="td11"><span class="obeylines-h"><span class="verb"><span
  468. class="cmtt-12">|</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-2-9-2"
  469. class="td11">left-to-right </td>
  470. </tr><tr
  471. style="vertical-align:baseline;" id="TBL-2-10-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-10-1"
  472. class="td11"><span class="obeylines-h"><span class="verb"><span
  473. class="cmtt-12">&amp;&amp;</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-2-10-2"
  474. class="td11">left-to-right </td>
  475. </tr><tr
  476. style="vertical-align:baseline;" id="TBL-2-11-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-11-1"
  477. class="td11"><span class="obeylines-h"><span class="verb"><span
  478. class="cmtt-12">||</span></span></span> </td><td style="white-space:nowrap; text-align:left;" id="TBL-2-11-2"
  479. class="td11">left-to-right </td>
  480. </tr><tr
  481. style="vertical-align:baseline;" id="TBL-2-12-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-12-1"
  482. class="td11"><span class="obeylines-h"><span class="verb"><span
  483. class="cmtt-12">=</span></span></span> <span class="obeylines-h"><span class="verb"><span
  484. class="cmtt-12">+=</span></span></span> <span class="obeylines-h"><span class="verb"><span
  485. class="cmtt-12">-=</span></span></span> <span class="obeylines-h"><span class="verb"><span
  486. class="cmtt-12">*=</span></span></span> <span class="obeylines-h"><span class="verb"><span
  487. class="cmtt-12">%=</span></span></span> <span class="obeylines-h"><span class="verb"><span
  488. class="cmtt-12">/=</span></span></span> <span class="obeylines-h"><span class="verb"><span
  489. class="cmtt-12">|=</span></span></span> <span class="obeylines-h"><span class="verb"><span
  490. class="cmtt-12">&amp;=</span></span></span> <span class="obeylines-h"><span class="verb"><span
  491. class="cmtt-12">^=</span></span></span> <span class="obeylines-h"><span class="verb"><span
  492. class="cmtt-12">&#x003E;&#x003E;=</span></span></span> <span class="obeylines-h"><span class="verb"><span
  493. class="cmtt-12">&#x003C;&#x003C;=</span></span></span></td><td style="white-space:nowrap; text-align:left;" id="TBL-2-12-2"
  494. class="td11">right-to-left </td>
  495. </tr><tr
  496. class="hline"><td><hr></td><td><hr></td></tr><tr
  497. class="hline"><td><hr></td><td><hr></td></tr><tr
  498. style="vertical-align:baseline;" id="TBL-2-13-"><td style="white-space:nowrap; text-align:left;" id="TBL-2-13-1"
  499. class="td11"> </td> </tr></table>
  500. </div>
  501. <!--l. 399--><p class="indent"> The keywords have the loosest possible preference, and thus the expressions at the end of <span
  502. class="cmtt-12">if</span>
  503. and <span
  504. class="cmtt-12">for </span>expressions extend to the right as far as possible.
  505. <!--l. 403--><p class="indent"> Even though any expression may be used, according to the grammar, as the left-hand side
  506. of an assignment, assignment is only a meaningful operation when the left-hand side is an
  507. &#8220;lvalue&#8221; of the form <span
  508. class="cmti-12">ident</span> ( <span class="fbox"><span
  509. class="cmtt-12">[</span></span> <span
  510. class="cmti-12">expression</span> <span class="fbox"><span
  511. class="cmtt-12">]</span></span> ) <sup><span
  512. class="cmr-8">?</span></sup>.
  513. <!--l. 408--><p class="noindent">
  514. <h3 class="sectionHead"><span class="titlemark">4.6</span>.&#x00A0;&#x00A0;<a
  515. id="x1-170004.6"></a>The types of expressions</h3>
  516. <!--l. 410--><p class="noindent">The binary operators <span
  517. class="cmtt-12">*</span>, <span
  518. class="cmtt-12">/</span>, <span
  519. class="cmtt-12">%</span>, <span
  520. class="cmtt-12">+</span>, <span
  521. class="cmtt-12">-</span>, <span
  522. class="cmtt-12">&amp;</span>, <span
  523. class="cmtt-12">|</span>, <span
  524. class="cmtt-12">^</span>, <span
  525. class="cmtt-12">=</span>, <span
  526. class="cmtt-12">*=</span>, <span
  527. class="cmtt-12">/=</span>, <span
  528. class="cmtt-12">%=</span>, <span
  529. class="cmtt-12">+=</span>, <span
  530. class="cmtt-12">-=</span>, <span
  531. class="cmtt-12">&amp;=</span>, <span
  532. class="cmtt-12">|=</span>, <span
  533. class="cmtt-12">^=</span>, all take
  534. two operands of the same, atomic, type, and return a value of that type, that is,
  535. formally,
  536. <center class="math-display" >
  537. <img
  538. src="piffle6x.png" alt="*, /,%,+,-,&amp;,| ,^,=,*=,...,^= : &#x2200;&#x03C4; &#x2208; {bool, u8,u16, u32,s8, s16,s32 }. &#x03C4; &#x00D7; &#x03C4; &#x2192; &#x03C4; .
  539. " class="math-display" ></center>
  540. <!--l. 420--><p class="nopar"> The unary operators <span
  541. class="cmtt-12">+</span>, <span
  542. class="cmtt-12">-</span>, and <span
  543. class="cmtt-12">~ </span>all take one operand of any integer type and return a value
  544. of that type, that is, formally,
  545. <center class="math-display" >
  546. <img
  547. src="piffle7x.png" alt="+,- ,~ : &#x2200;&#x03C4; &#x2208; {bool, u8,u16, u32,s8, s16,s32 }. &#x03C4; &#x2192; &#x03C4; .
  548. " class="math-display" ></center>
  549. <!--l. 426--><p class="nopar"> The binary operators <span
  550. class="cmtt-12">&#x003C;</span>, <span
  551. class="cmtt-12">&#x003E;</span>, <span
  552. class="cmtt-12">&#x003C;=</span>, <span
  553. class="cmtt-12">&#x003E;=</span>, <span
  554. class="cmtt-12">==</span>, <span
  555. class="cmtt-12">!=</span>, <span
  556. class="cmtt-12">&amp;&amp;</span>, and <span
  557. class="cmtt-12">|| </span>all take two operands of arbitrary
  558. atomic types, and return a value of type <span
  559. class="cmtt-12">bool</span>, that is, formally
  560. <center class="math-display" >
  561. <img
  562. src="piffle8x.png" alt="&#x003C;, &#x003E;,&#x003C;=,&#x003E;=, ==,!=,&amp;&amp;, || : &#x2200;&#x03C4;,&#x03C5; &#x2208; {bool, u8, u16,u32, s8,s16,s32 }. &#x03C4; &#x00D7; &#x03C5; &#x2192; bool .
  563. " class="math-display" ></center>
  564. <!--l. 436--><p class="nopar"> The unary operator <span
  565. class="cmtt-12">! </span>takes one operand of any integer types and returns a value of type
  566. <span
  567. class="cmtt-12">bool</span>, that is, formally,
  568. <center class="math-display" >
  569. <img
  570. src="piffle9x.png" alt="! : &#x2200;&#x03C4; &#x2208; {bool, u8,u16, u32,s8, s16,s32 }. &#x03C4; &#x2192; bool .
  571. " class="math-display" ></center>
  572. <!--l. 442--><p class="nopar"> For the binary operators <span
  573. class="cmtt-12">&#x003C;&#x003C;</span>, <span
  574. class="cmtt-12">&#x003E;&#x003E;</span>, <span
  575. class="cmtt-12">&#x003C;&#x003C;=</span>, and <span
  576. class="cmtt-12">&#x003E;&#x003E;=</span>, the value being shifted and the value
  577. indicating the number of bits to shift need not be of the same type, but both types
  578. must be atomic, and the entire expression gets the type of the value being shifted.
  579. Formally,
  580. <center class="math-display" >
  581. <img
  582. src="piffle10x.png" alt="&#x003C;&#x003C;,&#x003E;&#x003E;,&#x003C;&#x003C;=, &#x003E;&#x003E;= : &#x2200;&#x03C3;, &#x03C4; &#x2208; {bool, u8,u16,u32, s8,s16, s32}. &#x03C4; &#x00D7; &#x03C3; &#x2192; &#x03C4; .
  583. " class="math-display" ></center>
  584. <!--l. 451--><p class="nopar">
  585. <!--l. 453--><p class="indent"> A sequence <span
  586. class="cmsy-10x-x-120">{</span><span
  587. class="cmtt-12">...</span><span
  588. class="cmsy-10x-x-120">} </span>of expressions has the value and type of the last expression in the
  589. sequence, or <span
  590. class="cmtt-12">void </span>if there are no expressions. All expressions preceding the last are
  591. expected to have type <span
  592. class="cmtt-12">void</span>, but as stated above, any other type will do when <span
  593. class="cmtt-12">void </span>is
  594. expected.
  595. <!--l. 459--><p class="indent"> An <span
  596. class="cmtt-12">if </span>expression consists of a condition, a consequent, and an optional alternative. If the
  597. alternative is omitted, <span
  598. class="cmtt-12">unit </span>is implied. The condition may be of any atomic type. The types of
  599. the consequent and the alternative must match, but note again that because any type can be
  600. used instead of <span
  601. class="cmtt-12">void</span>, if the type of either branch is <span
  602. class="cmtt-12">void</span>, the other branch can have any type.
  603. The alternative is evaluated only if the condition evaluates to <span
  604. class="cmtt-12">0</span>; otherwise, the consequent is
  605. evaluated. In either case, the entire expression gets the value of the evaluated clause. To
  606. clarify:
  607. <table
  608. class="verbatim"><tr class="verbatim"><td
  609. class="verbatim"><div class="verbatim">
  610. fun&#x00A0;frob&#x00A0;()&#x00A0;:&#x00A0;void&#x00A0;=&#x00A0;{
  611. &#x00A0;<br />&#x00A0;&#x00A0;&#x00A0;&#x00A0;var&#x00A0;x,&#x00A0;y&#x00A0;:&#x00A0;u32;
  612. &#x00A0;<br />&#x00A0;&#x00A0;&#x00A0;&#x00A0;if&#x00A0;3&#x00A0;&#x003C;&#x00A0;5&#x00A0;then&#x00A0;do_something();&#x00A0;&#x00A0;/*&#x00A0;do_something()&#x00A0;is&#x00A0;executed&#x00A0;*/
  613. &#x00A0;<br />&#x00A0;&#x00A0;&#x00A0;&#x00A0;x&#x00A0;=&#x00A0;if&#x00A0;3&#x00A0;&#x003C;&#x00A0;5&#x00A0;then&#x00A0;0&#x00A0;else&#x00A0;1;&#x00A0;&#x00A0;&#x00A0;&#x00A0;/*&#x00A0;x&#x00A0;is&#x00A0;set&#x00A0;to&#x00A0;0&#x00A0;*/
  614. &#x00A0;<br />&#x00A0;&#x00A0;&#x00A0;&#x00A0;x&#x00A0;=&#x00A0;if&#x00A0;3&#x00A0;&#x003E;&#x00A0;5&#x00A0;then&#x00A0;0&#x00A0;else&#x00A0;1;&#x00A0;&#x00A0;&#x00A0;&#x00A0;/*&#x00A0;x&#x00A0;is&#x00A0;set&#x00A0;to&#x00A0;1&#x00A0;*/
  615. &#x00A0;<br />&#x00A0;&#x00A0;&#x00A0;&#x00A0;y&#x00A0;=&#x00A0;if&#x00A0;3&#x00A0;&#x003C;&#x00A0;5&#x00A0;then&#x00A0;0;&#x00A0;&#x00A0;&#x00A0;&#x00A0;&#x00A0;&#x00A0;&#x00A0;&#x00A0;&#x00A0;&#x00A0;&#x00A0;/*&#x00A0;This&#x00A0;won&#8217;t&#x00A0;type&#x00A0;check!!&#x00A0;*/
  616. &#x00A0;<br />};
  617. </div>
  618. </td></tr></table>
  619. <!--l. 476--><p class="nopar">
  620. <!--l. 478--><p class="indent"> There are two iteration constructs in Piffle. Both are different from iteration constructs in
  621. comparable languages because of the need for computing resource bounds.
  622. <!--l. 482--><p class="indent"> An expression of the form
  623. <center class="math-display" >
  624. <img
  625. src="piffle11x.png" alt="|---| |---| |------| ? |--|
  626. for--&#x03B1; -in-&#x03B2; ( -while--&#x03B3;) do--&#x03B4;
  627. " class="math-display" ></center>
  628. <!--l. 483--><p class="nopar"> has type <span
  629. class="cmtt-12">void</span>. The range <span
  630. class="cmmi-12">&#x03B2; </span>must be of type <span
  631. class="cmtt-12">array </span><span
  632. class="cmmi-12">i &#x03C4;</span>, for some integer <span
  633. class="cmmi-12">i </span>and some atomic
  634. type <span
  635. class="cmmi-12">&#x03C4;</span>, and the variable <span
  636. class="cmmi-12">&#x03B1; </span>will be of type <span
  637. class="cmmi-12">&#x03C4; </span>and scope over <span
  638. class="cmmi-12">&#x03B3; </span>and <span
  639. class="cmmi-12">&#x03B4;</span>. The condition <span
  640. class="cmmi-12">&#x03B3; </span>may
  641. have any atomic type, and the body <span
  642. class="cmmi-12">&#x03B4; </span>must be of type <span
  643. class="cmtt-12">void </span>(with any other type
  644. allowed).
  645. <!--l. 491--><p class="indent"> An expression of the form
  646. <center class="math-display" >
  647. <img
  648. src="piffle12x.png" alt="|----| |-----| |---| |------| ? |--|
  649. -for-|&#x03B1; -from--&#x03B2;0-to-|&#x03B2;1 (-while--&#x03B3;) -do-&#x03B4;
  650. " class="math-display" ></center>
  651. <!--l. 492--><p class="nopar"> has type <span
  652. class="cmtt-12">void</span>. The bounds <span
  653. class="cmmi-12">&#x03B2;</span><sub><span
  654. class="cmr-8">0</span><span
  655. class="cmmi-8">,</span><span
  656. class="cmr-8">1</span></sub> must be constant expressions of any atomic type, and <span
  657. class="cmmi-12">&#x03B1; </span>will
  658. be of type <span
  659. class="cmtt-12">u32 </span>and scope over <span
  660. class="cmmi-12">&#x03B3; </span>and <span
  661. class="cmmi-12">&#x03B4;</span>. The condition <span
  662. class="cmmi-12">&#x03B3; </span>may have any atomic type, and the
  663. body <span
  664. class="cmmi-12">&#x03B4; </span>must be of type <span
  665. class="cmtt-12">void </span>(with any other type allowed).
  666. <!--l. 499--><p class="noindent">
  667. <h3 class="sectionHead"><span class="titlemark">4.7</span>.&#x00A0;&#x00A0;<a
  668. id="x1-180004.7"></a>Constant expressions</h3>
  669. <!--l. 501--><p class="noindent">Integer literals are assigned a special type (<span
  670. class="cmtt-12">sliteral</span>) by the compiler. This type is considered
  671. atomic, and values of type <span
  672. class="cmtt-12">sliteral </span>can always be used when any atomic type is
  673. expected.
  674. <!--l. 506--><p class="noindent">
  675. <h3 class="sectionHead"><span class="titlemark">4.8</span>.&#x00A0;&#x00A0;<a
  676. id="x1-190004.8"></a>Source files</h3>
  677. <!--l. 508--><p class="noindent">A source file consists of a number of declarations separated by semicolons.
  678. <!--l. 513--><p class="indent"> <div class="eqnarray">
  679. <center class="math-display" >
  680. <img
  681. src="piffle13x.png" alt=" |--|
  682. file = ( declaration |; ) *
  683. ---
  684. " class="math-display" ></center>
  685. </div>
  686. <!--l. 515--><p class="noindent">
  687. <h3 class="sectionHead"><span class="titlemark">4.9</span>.&#x00A0;&#x00A0;<a
  688. id="x1-200004.9"></a>The filter function</h3>
  689. <!--l. 517--><p class="noindent">The function named <span
  690. class="cmtt-12">filter </span>has a special meaning. It is used as the root of the call graph
  691. (which is actually a tree, in the Piffle case) by the compiler when checking for memory
  692. and CPU usage bounds. It is also the function that gets called on every incoming
  693. packet.
  694. <!--l. 522--><p class="indent"> Strictly speaking, this is dependent on which boilerplate is being used. Boilerplates are a
  695. concept we will get to in the chapter about the compiler, but rest assured that all
  696. currently available boilerplates will cause <span
  697. class="cmtt-12">filter </span>to be called on each incoming
  698. packet.
  699. <!--l. 529--><p class="noindent">
  700. <h3 class="sectionHead"><span class="titlemark">4.10</span>.&#x00A0;&#x00A0;<a
  701. id="x1-210004.10"></a>Preprocessor</h3>
  702. <!--l. 531--><p class="noindent">It should be possible to run a Piffle program through a regular C preprocessor (cpp). This is
  703. not done by default; you would have to do it manually. Also make sure not to use nested
  704. comments if you want to use cpp. The compiler does not interpret cpp line directives, so any
  705. error messages you get about preprocessed code will refer to positions in the preprocessed
  706. source.
  707. <h2 class="chapterHead"><span class="titlemark">5</span>.&#x00A0;&#x00A0;<a
  708. id="x1-220005"></a>The Piffle compiler, pfc</h2>
  709. <!--l. 540--><p class="noindent">The Piffle compiler, pfc, is a fairly simple compiler, written in the Haskell programming language,
  710. that outputs C code. It is documented in the pfc(1) man page, which is reproduced here in its
  711. entirety.<span class="footnote-mark"><a
  712. href="piffle4.html#fn1x7"><sup class="textsuperscript">1</sup></a></span><a
  713. id="x1-22001f1"></a>
  714. <h3 class="sectionHead"><span class="titlemark">5.1</span>.&#x00A0;&#x00A0;<a
  715. id="x1-230005.1"></a>The pfc(1) manual page</h3>
  716. <!--l. 550--><p class="noindent">
  717. <div class="fancyvrb" id="fancyvrb1">
  718. <a
  719. id="x1-23002r1"></a><span
  720. class="cmtt-10">&#x00A0;</span><span
  721. class="cmtt-10">&#x00A0;PFC(1)</span><span
  722. class="cmtt-10">&#x00A0;</span><span
  723. class="cmtt-10">&#x00A0;</span><span
  724. class="cmtt-10">&#x00A0;</span><span
  725. class="cmtt-10">&#x00A0;</span><span
  726. class="cmtt-10">&#x00A0;</span><span
  727. class="cmtt-10">&#x00A0;</span><span
  728. class="cmtt-10">&#x00A0;</span><span
  729. class="cmtt-10">&#x00A0;</span><span
  730. class="cmtt-10">&#x00A0;</span><span
  731. class="cmtt-10">&#x00A0;</span><span
  732. class="cmtt-10">&#x00A0;</span><span
  733. class="cmtt-10">&#x00A0;</span><span
  734. class="cmtt-10">&#x00A0;</span><span
  735. class="cmtt-10">&#x00A0;</span><span
  736. class="cmtt-10">&#x00A0;</span><span
  737. class="cmtt-10">&#x00A0;</span><span
  738. class="cmtt-10">&#x00A0;</span><span
  739. class="cmtt-10">&#x00A0;</span><span
  740. class="cmtt-10">&#x00A0;</span><span
  741. class="cmtt-10">&#x00A0;</span><span
  742. class="cmtt-10">&#x00A0;</span><span
  743. class="cmtt-10">&#x00A0;</span><span
  744. class="cmtt-10">&#x00A0;</span><span
  745. class="cmtt-10">&#x00A0;</span><span
  746. class="cmtt-10">&#x00A0;</span><span
  747. class="cmtt-10">&#x00A0;</span><span
  748. class="cmtt-10">&#x00A0;</span><span
  749. class="cmtt-10">&#x00A0;</span><span
  750. class="cmtt-10">&#x00A0;</span><span
  751. class="cmtt-10">&#x00A0;</span><span
  752. class="cmtt-10">&#x00A0;</span><span
  753. class="cmtt-10">&#x00A0;</span><span
  754. class="cmtt-10">&#x00A0;</span><span
  755. class="cmtt-10">&#x00A0;</span><span
  756. class="cmtt-10">&#x00A0;</span><span
  757. class="cmtt-10">&#x00A0;</span><span
  758. class="cmtt-10">&#x00A0;</span><span
  759. class="cmtt-10">&#x00A0;</span><span
  760. class="cmtt-10">&#x00A0;</span><span
  761. class="cmtt-10">&#x00A0;</span><span
  762. class="cmtt-10">&#x00A0;</span><span
  763. class="cmtt-10">&#x00A0;</span><span
  764. class="cmtt-10">&#x00A0;</span><span
  765. class="cmtt-10">&#x00A0;</span><span
  766. class="cmtt-10">&#x00A0;</span><span
  767. class="cmtt-10">&#x00A0;</span><span
  768. class="cmtt-10">&#x00A0;</span><span
  769. class="cmtt-10">&#x00A0;</span><span
  770. class="cmtt-10">&#x00A0;</span><span
  771. class="cmtt-10">&#x00A0;</span><span
  772. class="cmtt-10">&#x00A0;</span><span
  773. class="cmtt-10">&#x00A0;</span><span
  774. class="cmtt-10">&#x00A0;</span><span
  775. class="cmtt-10">&#x00A0;</span><span
  776. class="cmtt-10">&#x00A0;</span><span
  777. class="cmtt-10">&#x00A0;</span><span
  778. class="cmtt-10">&#x00A0;</span><span
  779. class="cmtt-10">&#x00A0;</span><span
  780. class="cmtt-10">&#x00A0;</span><span
  781. class="cmtt-10">&#x00A0;</span><span
  782. class="cmtt-10">&#x00A0;</span><span
  783. class="cmtt-10">&#x00A0;</span><span
  784. class="cmtt-10">&#x00A0;</span><span
  785. class="cmtt-10">&#x00A0;</span><span
  786. class="cmtt-10">&#x00A0;</span><span
  787. class="cmtt-10">&#x00A0;PFC(1)</span>
  788. <br class="fancyvrb" /><a
  789. id="x1-23004r2"></a><span
  790. class="cmtt-10">&#x00A0;</span><span
  791. class="cmtt-10">&#x00A0;</span>
  792. <br class="fancyvrb" /><a
  793. id="x1-23006r3"></a><span
  794. class="cmtt-10">&#x00A0;</span><span
  795. class="cmtt-10">&#x00A0;</span>
  796. <br class="fancyvrb" /><a
  797. id="x1-23008r4"></a><span
  798. class="cmtt-10">&#x00A0;</span><span
  799. class="cmtt-10">&#x00A0;</span>
  800. <br class="fancyvrb" /><a
  801. id="x1-23010r5"></a><span
  802. class="cmtt-10">&#x00A0;</span><span
  803. class="cmtt-10">&#x00A0;NAME</span>
  804. <br class="fancyvrb" /><a
  805. id="x1-23012r6"></a><span
  806. class="cmtt-10">&#x00A0;</span><span
  807. class="cmtt-10">&#x00A0;</span><span
  808. class="cmtt-10">&#x00A0;</span><span
  809. class="cmtt-10">&#x00A0;</span><span
  810. class="cmtt-10">&#x00A0;</span><span
  811. class="cmtt-10">&#x00A0;</span><span
  812. class="cmtt-10">&#x00A0;</span><span
  813. class="cmtt-10">&#x00A0;</span><span
  814. class="cmtt-10">&#x00A0;pfc</span><span
  815. class="cmtt-10">&#x00A0;-</span><span
  816. class="cmtt-10">&#x00A0;Piffle</span><span
  817. class="cmtt-10">&#x00A0;Compiler</span>
  818. <br class="fancyvrb" /><a
  819. id="x1-23014r7"></a><span
  820. class="cmtt-10">&#x00A0;</span><span
  821. class="cmtt-10">&#x00A0;</span>
  822. <br class="fancyvrb" /><a
  823. id="x1-23016r8"></a><span
  824. class="cmtt-10">&#x00A0;</span><span
  825. class="cmtt-10">&#x00A0;SYNOPSIS</span>
  826. <br class="fancyvrb" /><a
  827. id="x1-23018r9"></a><span
  828. class="cmtt-10">&#x00A0;</span><span
  829. class="cmtt-10">&#x00A0;</span><span
  830. class="cmtt-10">&#x00A0;</span><span
  831. class="cmtt-10">&#x00A0;</span><span
  832. class="cmtt-10">&#x00A0;</span><span
  833. class="cmtt-10">&#x00A0;</span><span
  834. class="cmtt-10">&#x00A0;</span><span
  835. class="cmtt-10">&#x00A0;</span><span
  836. class="cmtt-10">&#x00A0;pfc</span><span
  837. class="cmtt-10">&#x00A0;[OPTIONS]</span><span
  838. class="cmtt-10">&#x00A0;file.pfl</span>
  839. <br class="fancyvrb" /><a
  840. id="x1-23020r10"></a><span
  841. class="cmtt-10">&#x00A0;</span><span
  842. class="cmtt-10">&#x00A0;</span>
  843. <br class="fancyvrb" /><a
  844. id="x1-23022r11"></a><span
  845. class="cmtt-10">&#x00A0;</span><span
  846. class="cmtt-10">&#x00A0;INTRODUCTION</span>
  847. <br class="fancyvrb" /><a
  848. id="x1-23024r12"></a><span
  849. class="cmtt-10">&#x00A0;</span><span
  850. class="cmtt-10">&#x00A0;</span><span
  851. class="cmtt-10">&#x00A0;</span><span
  852. class="cmtt-10">&#x00A0;</span><span
  853. class="cmtt-10">&#x00A0;</span><span
  854. class="cmtt-10">&#x00A0;</span><span
  855. class="cmtt-10">&#x00A0;</span><span
  856. class="cmtt-10">&#x00A0;</span><span
  857. class="cmtt-10">&#x00A0;Piffle</span><span
  858. class="cmtt-10">&#x00A0;is</span><span
  859. class="cmtt-10">&#x00A0;a</span><span
  860. class="cmtt-10">&#x00A0;pronounceable</span><span
  861. class="cmtt-10">&#x00A0;form</span><span
  862. class="cmtt-10">&#x00A0;of</span><span
  863. class="cmtt-10">&#x00A0;PFL,</span><span
  864. class="cmtt-10">&#x00A0;that</span><span
  865. class="cmtt-10">&#x00A0;is,</span><span
  866. class="cmtt-10">&#x00A0;Packet</span><span
  867. class="cmtt-10">&#x00A0;Filter</span><span
  868. class="cmtt-10">&#x00A0;Language.</span>
  869. <br class="fancyvrb" /><a
  870. id="x1-23026r13"></a><span
  871. class="cmtt-10">&#x00A0;</span><span
  872. class="cmtt-10">&#x00A0;</span><span
  873. class="cmtt-10">&#x00A0;</span><span
  874. class="cmtt-10">&#x00A0;</span><span
  875. class="cmtt-10">&#x00A0;</span><span
  876. class=

Large files files are truncated, but you can click here to view the full file