shared libraries can now export variables on MIPS
[voodoo-lang.git] / doc / language.html
blob6090ed5b8a14528a2e7acf7f3ecf5e6a811e185c
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
5 <head>
6 <title>The Voodoo Programming Language</title>
7 <link rel="stylesheet" href="style.css" type="text/css" />
8 </head>
9 <body>
10 <div class="body">
11 <h1>The Voodoo Programming Language</h1>
13 <p class="first">A Voodoo program consists of data, function
14 definitions, and code. All of these are introduced by magic words such
15 as <code>function</code>, <code>call</code>, and
16 <code>string</code>. Any part of the program may be preceded by a
17 label, and labels can be referred to from the code.</p>
19 <h2>Hello World in Voodoo</h2>
21 <p class="first">To quickly get a feeling for a programming language,
22 it is often instructive to look at a simple yet complete program.
23 The following is an implementation of the traditional
24 Hello World program in Voodoo:</p>
26 <pre><code>
27 #### Hello world in Voodoo
29 section data
30 greeting:
31 string "Hello, world!\x00"
33 section functions
34 import puts
35 export main
37 main:
38 function argc argv
39 call puts greeting
40 return 0
41 end function
42 </code></pre>
44 <p class="first">When compiled and linked with a library that provides
45 an appropriate implementation of the <code>puts</code> function, this
46 program will print <q>Hello, world!</q>.</p>
48 <p>What follows is a more formal description of the Voodoo
49 programming language.</p>
51 <h2>Tokens</h2>
53 <h3>Comments</h3>
55 <p class="first">Comments start with a hash mark (#) and run until
56 the end of the line. The following is an example comment:</p>
58 <pre><code>
59 # This is an example comment
60 </code></pre>
62 <h3>Integers</h3>
64 <p class="first">Integers consist of an optional plus or minus sign,
65 followed by one or more digits. Examples of valid integers:</p>
67 <pre><code>
69 +12
71 </code></pre>
73 <h3>Strings</h3>
75 <p class="first">Strings consist of zero or more characters enclosed in double
76 quotes. Examples of valid strings:</p>
78 <pre><code>
80 "Hello, world!"
81 </code></pre>
83 <h3>Symbols</h3>
85 <p class="first">Symbols consist of letters, digits, underscores, and
86 hyphens, although only a letter or an underscore is allowed as the first
87 character of a symbol. Examples of valid symbols:</p>
89 <pre><code>
90 foo
91 some-symbol
92 </code></pre>
94 <h2>Escape Sequences</h2>
96 <p class="first">To facilitate entering special characters and to
97 inhibit the special meaning certain characters normally have, an
98 escape mechanism is provided. Escape sequences start with a backslash,
99 and have the following meanings:</p>
101 <table summary="Special characters"><tr>
102 <td><code>\\</code></td>
103 <td>An actual backslash character</td>
104 </tr><tr>
105 <td><code>\"</code></td>
106 <td>A double quote character</td>
107 </tr><tr>
108 <td><code>\n</code></td>
109 <td>A newline</td>
110 </tr><tr>
111 <td><code>\r</code></td>
112 <td>A carriage return character</td>
113 </tr><tr>
114 <td><code>\t</code></td>
115 <td>A tab character</td>
116 </tr><tr>
117 <td><code>\x<var>XX</var></code></td>
118 <td>The character with hexadecimal value <var>XX</var></td>
119 </tr><tr>
120 <td><code>\&lt;space&gt;</code></td>
121 <td>A space character</td>
122 </tr><tr>
123 <td><code>\&lt;newline&gt;</code></td>
124 <td>Line continuation character. The newline
125 and any leading whitespace on the next line
126 is ignored.</td>
127 </tr></table>
129 <p class="first">Examples of the usage of the escape characters:</p>
131 <table summary="Example Escape Sequences"><tr>
132 <td><code>"Hello&nbsp;world\n"</code></td>
133 <td>A string with a newline in it</td>
134 </tr><tr>
135 <td><code>"\\\""</code></td>
136 <td>A string consisting of a backslash followed by a double quote</td>
137 </tr><tr>
138 <td><code>call foo \<br />
139 bar</code></td>
140 <td>Same as: <code>call&nbsp;foo&nbsp;bar</code></td>
141 </tr></table>
143 <h2>Labels</h2>
145 <p class="first">Places in a program can be marked with labels, so
146 that the place can be referred to from the code by mentioning the
147 label. A label consists of a symbol followed by a colon. When
148 referring to a label, only the symbol is used, without the colon.</p>
150 <p class="first">For example:</p>
152 <pre><code>
153 foo:
154 word 12
155 </code></pre>
157 <p class="first">declares a word with the value 12, and a label 'foo'
158 that refers to the place where this value is stored. For example, if
159 the value happens to be loaded at the address 71234, writing 'foo' in
160 the program will have the same effect as writing 71234 in the same
161 place.</p>
163 <h2>Values</h2>
165 <p class="first">Values are the smallest unit of data in a Voodoo
166 program. Examples of values are:</p>
168 <table summary="Example Values"><tr>
169 <td><code>12</code></td>
170 <td>The integer 12</td>
171 </tr><tr>
172 <td><code>x</code></td>
173 <td>The value of x (may be a label or a local variable)</td>
174 </tr></table>
176 <h2>At-Expressions</h2>
178 <p class="first">The character @ can be used to denote the value at
179 some address. An address prefixed with @ denotes the word stored at
180 that address, rather than the address itself. The address may be an
181 integer, a local variable, or a label. For example:</p>
183 <table summary="Example at-expressions"><tr>
184 <td><code>@12</code></td>
185 <td>The word stored at address 12</td>
186 </tr><tr>
187 <td><code>@x</code></td>
188 <td>The word stored at address x</td>
189 </tr></table>
191 <p class="first">Syntactically, at-expressions are values. Therefore,
192 in any place where a value can be used, an at-expression can also be
193 used.</p>
195 <h2>Data Definitions</h2>
197 <p class="first">Data can be inserted into a Voodoo program by means
198 of the magic words <code>byte</code>, <code>word</code>, and
199 <code>string</code>, followed by the value of the byte, word, or
200 string to be inserted. For example:</p>
202 <pre><code>
203 # A byte with value 42
204 byte 42
206 # A word with value -1
207 word -1
209 # The string "hello"
210 string "hello"
211 </code></pre>
213 <h2>Actions</h2>
215 <p class="first">Voodoo code consists of actions. Actions consist of a
216 magic word, usually followed by a number of values. This section lists
217 all actions supported by Voodoo. In the list below, '&lt;x&gt;',
218 '&lt;y&gt;', and '&lt;z&gt;' denote values, '&lt;symbol&gt;' denotes a
219 symbol, and '&lt;expr&gt;' denotes an expression.
220 (expressions are discussed further on). Ellipsis (&hellip;) is used to
221 denote that more items may follow, and square brackets ([ and ]) are
222 used to denote that an item is optional.</p>
224 <dl>
225 <dt><code>call &lt;x&gt; &lt;y&gt; &lt;z&gt; &hellip;</code></dt>
226 <dd><p class="first">Calls the function &lt;x&gt; with the arguments &lt;y&gt;
227 &lt;z&gt; &hellip;. There may be zero or more arguments.</p></dd>
229 <dt><code>goto &lt;x&gt;</code></dt>
230 <dd><p class="first">Continues the program at location &lt;x&gt;,
231 rather than at the next action after the goto.</p>
233 <p>Any value can be used as the location to go to. However, the
234 consequences of using <code>goto</code> to cross function or block
235 boundaries (e.g. performing a <code>goto</code> to a label inside
236 a different function) are undefined.</p></dd>
238 <dt><code>let &lt;symbol&gt; &lt;expr&gt;</code></dt>
239 <dd><p class="first">Introduces a local variable &lt;symbol&gt;, and
240 initializes it to the result of evaluating &lt;expr&gt;.</p>
242 <p>This action is only allowed inside functions or blocks, that is,
243 between <code>function</code> and the corresponding
244 <code>end function</code>, or between <code>block</code> and the
245 corresponding <code>end block</code>.</p>
247 <p>The scope of a variable introduced by <code>let</code> includes
248 every statement after the <code>let</code> action and before the
249 <code>end function</code> or <code>end block</code> that ends the
250 function or block in which the variable is introduced.</p></dd>
252 <dt><code>return [&lt;expr&gt;]</code></dt>
253 <dd><p class="first">Returns from the current function.
254 If &lt;expr&gt; is specified, it is evaluated and the function returns
255 the result of the evaluation. Otherwise, the return value is
256 unspecified.</p></dd>
258 <dt><code>set &lt;symbol&gt; &lt;expr&gt;</code></dt>
259 <dd><p class="first">Evaluates &lt;expr&gt; and assigns the result to
260 &lt;symbol&gt;. &lt;symbol&gt; may not be a label, because labels cannot be
261 assigned to.</p></dd>
263 <dt><code>set-byte &lt;base&gt; &lt;offset&gt; &lt;x&gt;</code></dt>
264 <dd><p class="first">Sets the byte at &lt;base&gt; + &lt;offset&gt; to &lt;x&gt;.
265 &lt;offset&gt; is given as a number of bytes.</p></dd>
267 <dt><code>set-word &lt;base&gt; &lt;offset&gt; &lt;x&gt;</code></dt>
268 <dd><p class="first">Sets the word at &lt;base&gt; + WORDSIZE *
269 &lt;offset&gt; to &lt;x&gt;.</p>
271 <p>The address computed by &lt;base&gt; + WORDSIZE * &lt;offset&gt;
272 is expected to be a multiple of the word size.
273 The behavior of <code>set-word</code> is undefined if this condition
274 is not satisfied.
275 </p></dd>
277 <dt><code>tail-call &lt;x&gt; &lt;y&gt; &lt;z&gt; ...</code></dt>
278 <dd><p class="first">Performs a tail call to the function &lt;x&gt;
279 with arguments &lt;y&gt; &lt;z&gt; &hellip;. This has an effect
280 similar to 'return call &lt;x&gt; &lt;y&gt; &lt;z&gt; ...', but
281 re-uses the call frame of the current function. This means that if
282 &lt;x&gt; takes fewer or at most as many arguments as the current
283 function, the tail call requires no extra space.</p></dd>
284 </dl>
286 <h2>Expressions</h2>
288 <p class="first">Certain actions have the ability to evaluate
289 expressions. Expressions can be simple values, but expressions can
290 also perform computations on values. The following are valid
291 expressions:</p>
293 <dl>
294 <dt><code>add &lt;x&gt; &lt;y&gt;</code></dt>
295 <dd><p class="first">The result of adding &lt;y&gt; to &lt;x&gt;.</p>
297 <p>If the result of the addition cannot be represented in a single word,
298 the result of <code>add</code> is undefined.</p></dd>
300 <dt><code>and &lt;x&gt; &lt;y&gt;</code></dt>
301 <dd><p class="first">The bitwise and of &lt;x&gt; and &lt;y&gt;.</p></dd>
303 <dt><code>asr &lt;x&gt; &lt;y&gt;</code></dt>
304 <dd><p class="first">Performs an arithmetic right shift. The bits in
305 &lt;x&gt; are shifted &lt;y&gt; positions to the right. The sign of
306 &lt;x&gt; is preserved, so that the result has the same sign as
307 &lt;x&gt;. The result is undefined if &lt;y&gt; is negative.</p></dd>
309 <dt><code>bsr &lt;x&gt; &lt;y&gt;</code></dt>
310 <dd><p class="first">Performs a bitwise right shift. The bits in
311 &lt;x&gt; are shifted &lt;y&gt; positions to the right. &lt;y&gt;
312 zero-valued bits are shifted in from the left. The result does
313 not necessarily have the same sign as &lt;x&gt;. The result is
314 undefined if &lt;y&gt; is negative.</p></dd>
316 <dt><code>call &lt;x&gt; &lt;y&gt; &lt;z&gt; ...</code></dt>
317 <dd><p class="first">Similar to the action call, this calls the
318 function &lt;x&gt; with the arguments &lt;y&gt; &lt;z&gt; ... (there
319 may be zero or more arguments). The result of this expression is the
320 value returned from the function.</p></dd>
322 <dt><code>div &lt;x&gt; &lt;y&gt;</code></dt>
323 <dd><p class="first">The (integer) result of dividing &lt;x&gt; by
324 &lt;y&gt;.</p>
326 <p>If &lt;x&gt; &ge; 0 and &lt;y&gt; &gt; 0, the result is the largest
327 integer equal to or less than the algebraic quotient of &lt;x&gt;
328 and &lt;y&gt;.</p>
330 <p>If either &lt;x&gt; or &lt;y&gt; is negative, the result is
331 implementation-defined.</p>
333 <p>If &lt;y&gt; is zero, or if the quotient cannot be represented in a
334 single machine word, the result is undefined.</p></dd>
336 <dt><code>get-byte &lt;base&gt; &lt;offset&gt;</code></dt>
337 <dd><p class="first">The value of the byte at address &lt;base&gt; + &lt;offset&gt;.</p></dd>
339 <dt><code>get-word &lt;base&gt; &lt;offset&gt;</code></dt>
340 <dd><p class="first">The value of the word at address &lt;base&gt; +
341 (WORDSIZE * &lt;offset&gt;).</p>
343 <p>The address computed as &lt;base&gt; + (WORDSIZE * &lt;offset&gt;) is
344 expected to be a multiple of the word size. If this condition is not met,
345 the behavior of <code>get-word</code> is undefined.</p></dd>
347 <dt><code>mod &lt;x&gt; &lt;y&gt;</code></dt>
348 <dd><p class="first">For &lt;x&gt; &ge; 0 and &lt;y&gt; &gt; 0, returns
349 &lt;x&gt; modulo &lt;y&gt;.</p>
351 <p>If either &lt;x&gt; or &lt;y&gt; is negative, the result is
352 implementation-defined.</p>
354 <p>If &lt;y&gt; is zero, the result is undefined.</p></dd>
356 <dt><code>mul &lt;x&gt; &lt;y&gt;</code></dt>
357 <dd><p class="first">The result of multiplying &lt;x&gt; by &lt;y&gt;.</p>
359 <p>If the algebraic result of &lt;x&gt; * &lt;y&gt; cannot be represented
360 in a single word, the result of <code>mul &lt;x&gt; &lt;y&gt;</code>
361 contains only the low-order bits of the full result.</p></dd>
363 <dt><code>not &lt;x&gt;</code></dt>
364 <dd><p class="first">The ones' complement of &lt;x&gt;; i.e. all the
365 bits in &lt;x&gt; inverted.</p></dd>
367 <dt><code>or &lt;x&gt; &lt;y&gt;</code></dt>
368 <dd><p class="first">The bitwise or of &lt;x&gt; and &lt;y&gt;.</p></dd>
370 <dt><code>rol &lt;x&gt; &lt;y&gt;</code></dt>
371 <dd><p class="first">Rotates the bits in &lt;x&gt; to the left by
372 &lt;y&gt; positions. Bits that are rotated off the left are inserted
373 on the right.
374 The result is undefined if &lt;y&gt; is negative.</p></dd>
376 <dt><code>ror &lt;x&gt; &lt;y&gt;</code></dt>
377 <dd><p class="first">Rotates the bits in &lt;x&gt; to the right by
378 &lt;y&gt; positions. Bits that are rotated off the right are inserted
379 on the left.
380 The result is undefined if &lt;y&gt; is negative.</p></dd>
382 <dt><code>shl &lt;x&gt; &lt;y&gt;</code></dt>
383 <dd><p class="first">Performs a left shift. The bits in
384 &lt;x&gt; are shifted &lt;y&gt; positions to the left.
385 The result is undefined if &lt;y&gt; is negative.</p></dd>
387 <dt><code>shr &lt;x&gt; &lt;y&gt;</code></dt>
388 <dd><p class="first">Performs a right shift. The bits in
389 &lt;x&gt; are shifted &lt;y&gt; positions to the right. It is
390 implementation-defined whether this operation preserves the sign
391 of &lt;x&gt; (for operations with specific sign-preservation
392 properties, use asr or bsr).
393 The result is undefined if &lt;y&gt; is negative.</p></dd>
395 <dt><code>sub &lt;x&gt; &lt;y&gt;</code></dt>
396 <dd><p class="first">The result of subtracting &lt;y&gt; from &lt;x&gt;.</p>
398 <p>If the result of the subtraction cannot be represented in a single
399 word, the result of <code>sub</code> is undefined.</p></dd>
401 <dt><code>xor &lt;x&gt; &lt;y&gt;</code></dt>
402 <dd><p class="first">The bitwise exclusive or of &lt;x&gt; and
403 &lt;y&gt;.</p></dd>
404 </dl>
406 <h2>Conditionals</h2>
408 <p class="first">Conditionals in Voodoo take the following form:</p>
410 <pre><code>if&lt;test&gt;
411 ... some code here ...
412 else if&lt;test&gt;
413 ... other code here ...
414 ... more "else if" parts ...
415 else
416 ... some code ...
417 end if
418 </code></pre>
420 <p class="first">There can be any number of <q>else if</q> parts, and
421 the final <q>else</q> clause is optional. The tests that are provided
422 are the following:</p>
424 <dl>
425 <dt><code>ifeq &lt;x&gt; &lt;y&gt;</code></dt>
426 <dd><p class="first">Tests if &lt;x&gt; is equal to &lt;y&gt;.</p></dd>
428 <dt><code>ifge &lt;x&gt; &lt;y&gt;</code></dt>
429 <dd><p class="first">Tests if &lt;x&gt; is greater than or equal to
430 &lt;y&gt;.</p></dd>
432 <dt><code>ifgt &lt;x&gt; &lt;y&gt;</code></dt>
433 <dd><p class="first">Tests if &lt;x&gt; is strictly greater than
434 &lt;y&gt;.</p></dd>
436 <dt><code>ifle &lt;x&gt; &lt;y&gt;</code></dt>
437 <dd><p class="first">Tests if &lt;x&gt; is less than or equal to
438 &lt;y&gt;.</p></dd>
440 <dt><code>iflt &lt;x&gt; &lt;y&gt;</code></dt>
441 <dd><p class="first">Tests if &lt;x&gt; is strictly less than
442 &lt;y&gt;.</p></dd>
444 <dt><code>ifne &lt;x&gt; &lt;y&gt;</code></dt>
445 <dd><p class="first">Tests if &lt;x&gt; is different from &lt;y&gt;.</p></dd>
446 </dl>
448 <h2>Function Definitions</h2>
450 <p class="first">A function definition looks like:</p>
452 <pre><code>
453 function x y z
454 &lt;code&gt;
455 end function
456 </code></pre>
458 <p class="first">Here, the function being defined takes 3 arguments,
459 which can be referred to as <code>x</code>, <code>y</code>, and
460 <code>z</code> from the code inside the function body. A function may
461 have zero or more arguments and is practically always preceded by a
462 label, so that the function can be referenced from code.</p>
464 <p>A function should only be entered using <code>call</code> or
465 <code>tail-call</code>, and should only be left through a
466 <code>return</code> action. Furthermore, a function should always
467 be called with the same number of arguments it was defined with.
468 Failing to meet any of these requirements results in undefined
469 behavior.</p>
471 <h2>Blocks</h2>
473 <p class="first">Blocks can be used to define a scope in which local
474 variables (introduced with <code>let</code>) can be referred to. A
475 block looks like:</p>
477 <pre><code>
478 block
479 &lt;code&gt;
480 end block
481 </code></pre>
483 <p class="first">Inside a block, variables may be introduced using
484 <code>let</code>. Such a variable is in scope (can be referred to)
485 from the first statement after the <code>let</code> until the
486 <code>end block</code> that terminates the block.</p>
488 <p>Blocks can be placed anywhere an action can be placed: at
489 top-level, inside functions, inside blocks, and inside conditionals.</p>
491 <h2>Sections</h2>
493 <p class="first">A Voodoo program is divided in a number of sections.
494 In source code, these are introduced by a directive of the form
495 <code>section &lt;identifier&gt;</code>, where &lt;identifier&gt; is an
496 identifier for the section.</p>
498 <p>The following section identifiers are valid:</p>
500 <table summary="Section Identifiers"><tr>
501 <th>Identifier</th>
502 <th>Meaning</th>
503 </tr><tr>
504 <td><code>code</code></td>
505 <td>This section contains executable code</td>
506 </tr><tr>
507 <td><code>data</code></td>
508 <td>This section contains data</td>
509 </tr><tr>
510 <td><code>functions</code></td>
511 <td>This section contains function definitions</td>
512 </tr></table>
514 <p class="first">Example usage of the <code>section</code> directive:</p>
516 <pre><code>
517 section data
518 # define data here ...
520 section functions
521 # define functions here ...
522 </code></pre>
524 <h2>Import and Export</h2>
526 <p class="first">To allow Voodoo programs to refer to definitions in
527 other files, and to allow other files to refer to definitions in
528 Voodoo programs, the magic words <code>import</code>
529 and <code>export</code> are used.</p>
531 <p>Using <code>import</code>, definitions from elsewhere are made
532 available to a Voodoo program:</p>
534 <pre><code>
535 section data
536 import stderr
538 section functions
539 import fputs
541 # We can now refer to stderr and fputs
542 </code></pre>
544 <p>Data and functions defined in a Voodoo program can be made
545 available to other programs using <code>export</code>:</p>
547 <pre><code>
548 section data
549 export answer
551 answer:
552 word 42
554 section functions
555 export foo
557 foo:
558 function
559 # some code here
560 end function
561 </code></pre>
563 <h2>Alignment</h2>
565 <p class="first">Many architectures require that data and/or code
566 obey certain alignment restrictions. For example, an architecture may
567 require that a word of data be at an address that is a multiple of the
568 word size. Voodoo provides the <code>align</code> directive, which
569 specifies the alignment for the next program element.</p>
571 <p>Without any arguments, <code>align</code> inserts filler bytes
572 into the current section, so that the next element added to the section
573 will respect the default alignment for the section. The default
574 alignment for each section is implementation-dependent, but must ensure
575 that the alignment restrictions of the target platform are obeyed.</p>
577 <p>When written as <code>align &lt;n&gt;</code>, where &lt;n&gt; is an
578 integer, the directive will insert filler bytes as necessary to align
579 the next element to be added to the section on a multiple of &lt;n&gt;
580 bytes.</p>
582 <p>The filler bytes inserted by <code>align</code> are unspecified.
583 In particular, they are not guaranteed to be valid code.</p>
585 <p>Example uses of the <code>align</code> directive:</p>
587 <pre><code>
588 section data
591 byte 1
593 # Ensure that y is aligned according to
594 # the target platform's alignment restrictions
595 align
597 word 42
601 section functions
603 # Ensure that foo is aligned according to
604 # the target platform's alignment restrictions
605 align
606 foo:
607 function n
608 # some code here
609 end function
610 </code></pre>
611 </div>
612 </body>
613 </html>