3 Mu programs are sequences of `fn` and `type` definitions.
7 Define functions with the `fn` keyword. For example:
10 fn foo arg1: int, arg2: int -> result/eax: boolean
13 Functions contain `{}` blocks, `var` declarations, primitive statements and
14 calls to other functions. Only `{}` blocks can nest. Primitive statements and
15 function calls look similar:
18 out1, out2, out3, ... <- operation inout1, inout2, inout3, ...
21 They can take any number of inouts and outputs, including 0. Statements
22 with 0 outputs also drop the `<-`.
24 Inouts can be either variables in memory, variables in registers, or
25 constants. Outputs are always variables in registers.
27 Inouts in memory can be either inputs or outputs (if they're addresses being
28 written to). Hence the name.
30 Primitives usually require their inouts to be in specific combinations of
31 memory and register. User-defined functions are flexible.
33 Primitives can often write to arbitrary output registers. User-defined
34 functions, however, require rigidly specified output registers.
36 ## Variables, registers and memory
38 Declare local variables in a function using the `var` keyword.
40 You can declare local variables in either registers or memory (the stack). So
41 a `var` statement has two forms:
42 - Living in a register, e.g. `var x/eax: int <- copy 0` defines `x` which
44 - Living in memory, e.g. `var x: int` defines `x` on the stack.
46 Variables in registers must be initialized. Variables on the stack are
47 implicitly zeroed out.
49 Variables exist only within the `{}` block they're defined in. Space allocated
50 to them on the stack is reclaimed after execution leaves the block. Registers
51 restore whatever variable was using them in the outer block.
53 It is perfectly ok to reuse a register for a new variable. Even in a single
54 block (though you permanently lose the old variable then).
56 Variables can be in six 32-bit _general-purpose_ registers of the x86 processor.
61 - esi ('s' often a mnemonic for 'source')
62 - edi ('d' often a mnemonic for 'destination')
64 Most functions return results in `eax` by convention. In practice, it ends up
65 churning through variables pretty quickly.
67 You can store several types in these registers:
70 - (addr T) (address into memory)
71 - byte (uses only 8 bits)
72 - code-point (Unicode)
73 - grapheme (code-point encoded in UTF-8)
75 There's one 32-bit type you _cannot_ store in these registers:
78 It instead uses eight separate 32-bit registers: xmm0, xmm1, ..., xmm7
80 Types that require more than 32 bits (4 bytes) cannot be stored in registers:
85 - any compound types you define using the `type` keyword
87 `T` here can be any type, including combinations of types. For example:
88 - (array int) -- an array of ints
89 - (addr int) -- an address to an int
90 - (addr array byte) -- an address to an array of bytes, useful for Unicode
92 - (handle int) -- a handle to an int
93 - (addr handle int) -- an address to a handle to int
94 - (addr array handle int) -- an address to an array of handles to ints
97 Other miscellaneous restrictions:
98 - `byte` variables must be either in registers or on the heap, never local
99 variables on the stack.
100 - `addr` variables can never "escape" a function either by being returned or
101 by being written to a memory location. When you need that sort of thing,
102 use a `handle` instead.
104 ## Operations on simple types
106 We'll now survey a long list of statement forms that operate on 32-bit types.
107 Most of these are primitives, but some are also implemented as functions
108 (which have slightly different rules as mentioned up top). Most instructions
109 with multiple args require types to match. Various operations have other
110 restrictions which we'll note below, using the following notation:
111 - `var/reg` indicates a variable in some register. Where we require a
112 variable in a specific register, we'll mention it explicitly. E.g.
114 - `var/xreg` indicates a variable in some floating-point register `xmm_`.
115 - `var` without a `reg` indicates either a variable on the stack, or
116 dereferencing a variable in a (non-floating-point) register: `*var/reg`.
117 - `var: type` indicates a variable that must satisfy some type constraint.
118 - `n` indicates a literal integer. There are no floating-point literals.
120 ### Moving values around
122 These instructions work with variables of any 32-bit type except `byte` and
126 var/reg <- copy var2/reg2
127 copy-to var1, var2/reg
133 Byte variables have their own instructions:
136 var/reg <- copy-byte var2/reg2
137 var/reg <- copy-byte *var2/reg2 # var2 must have type (addr byte)
138 copy-byte-to *var1/reg1, var2/reg2 # var1 must have type (addr byte)
141 Floating point variables can be copied as well, but only to or from
142 floating-point registers `xmm_`.
145 var/xreg <- copy var2/xreg2
146 copy-to var1, var2/xreg
147 var/xreg <- copy var2
148 var/xreg <- copy *var2/reg2 # var2 must have type (addr byte) and live in a general-purpose register
151 There's no way to copy a literal to a floating-point register. However,
152 there's a few ways to convert non-float values in general-purpose registers.
155 var/xreg <- convert var2/reg2
156 var/xreg <- convert var2
157 var/xreg <- convert *var2/reg2
160 Correspondingly, there are ways to convert floats into integers, with and
164 var/reg <- convert var2/xreg2
165 var/reg <- convert var2
166 var/reg <- convert *var2/reg2
168 var/reg <- truncate var2/xreg2
169 var/reg <- truncate var2
170 var/reg <- truncate *var2/reg2
173 Still, the absence of fractional literals is an annoyance. Mu provides some
174 helpers to mitigate it somewhat:
177 result/xmm0 <- rational numerator: int, denominator: int
178 fill-in-rational out: (addr float), numerator: int, denominator: int
181 These are functions, so the inouts have fewer restrictions while the outputs
182 have more. The inouts can be registers, or memory, or even literals. The
183 output for `rational` _must_ be in register `xmm0`.
187 Work with variables of any 32-bit type. `addr` variables can only be compared
191 compare var1, var2/reg
192 compare var1/reg, var2
197 Floating-point numbers cannot be compared to literals, and the register must
201 compare var1/xreg1, var2/xreg2
202 compare var1/xreg1, var2
207 Immediately after a `compare` instruction you can branch on its result. For
214 This instruction will jump to after the enclosing `{}` block if the previous
215 `compare` detected equality. Here's the list of conditional and unconditional
216 `break` instructions:
228 Similarly, you can jump back to the start of the enclosing `{}` block with
229 `loop`. Here's the list of `loop` instructions.
241 Additionally, there are special variants for comparing `addr` and `float`
242 values, which results in the following comprehensive list of jumps:
248 break-if-< break-if-addr< break-if-float<
249 break-if-> break-if-addr> break-if-float>
250 break-if-<= break-if-addr<= break-if-float<=
251 break-if->= break-if-addr>= break-if-float>=
256 loop-if-< loop-if-addr< loop-if-float<
257 loop-if-> loop-if-addr> loop-if-float>
258 loop-if-<= loop-if-addr<= loop-if-float<=
259 loop-if->= loop-if-addr>= loop-if-float>=
262 One final property all these jump instructions share: they can take an
263 optional block name to jump to. For example:
284 However, there's no way to jump to a block that doesn't contain the `loop` or
287 ### Integer arithmetic
289 These instructions require variables of non-`addr`, non-`float` types.
293 var1/reg1 <- add var2/reg2
295 add-to var1, var2/reg # var1 += var2
302 var1/reg1 <- subtract var2/reg2
303 var/reg <- subtract var2
304 subtract-from var1, var2/reg # var1 -= var2
305 var/reg <- subtract n
323 var/reg <- multiply var2
326 The result of a multiply must be a register.
334 ### Fractional arithmetic
336 Operations on `float` variables include a few we've seen before and some new
337 ones. Notice here that we mostly use floating-point registers `xmm_`, but
338 still use the general-purpose registers when dereferencing variables of type
342 var/xreg <- add var2/xreg2
344 var/xreg <- add *var2/reg2
346 var/xreg <- subtract var2/xreg2
347 var/xreg <- subtract var2
348 var/xreg <- subtract *var2/reg2
350 var/xreg <- multiply var2/xreg2
351 var/xreg <- multiply var2
352 var/xreg <- multiply *var2/reg2
354 var/xreg <- divide var2/xreg2
355 var/xreg <- divide var2
356 var/xreg <- divide *var2/reg2
358 var/xreg <- reciprocal var2/xreg2
359 var/xreg <- reciprocal var2
360 var/xreg <- reciprocal *var2/reg2
362 var/xreg <- square-root var2/xreg2
363 var/xreg <- square-root var2
364 var/xreg <- square-root *var2/reg2
366 var/xreg <- inverse-square-root var2/xreg2
367 var/xreg <- inverse-square-root var2
368 var/xreg <- inverse-square-root *var2/reg2
370 var/xreg <- min var2/xreg2
372 var/xreg <- min *var2/reg2
374 var/xreg <- max var2/xreg2
376 var/xreg <- max *var2/reg2
379 Two instructions in the above list are approximate. According to the Intel
380 manual, `reciprocal` and `inverse-square-root` [go off the rails around the
381 fourth decimal place](linux/x86_approx.md). If you need more precision, use
384 ### Bitwise boolean operations
386 These require variables of non-`addr`, non-`float` types.
390 var1/reg1 <- and var2/reg2
392 and-with var1, var2/reg
399 var1/reg1 <- or var2/reg2
401 or-with var1, var2/reg
414 var1/reg1 <- xor var2/reg2
416 xor-with var1, var2/reg
423 Shifts require variables of non-`addr`, non-`float` types.
426 var/reg <- shift-left n
427 var/reg <- shift-right n
428 var/reg <- shift-right-signed n
431 shift-right-signed var, n
434 Shifting bits left always inserts zeros on the right.
435 Shifting bits right inserts zeros on the left by default.
436 A _signed_ shift right duplicates the leftmost bit, thereby preserving the
439 ## Operations on more complex types
441 These instructions work with any type `T`. As before we use `/reg` here to
442 indicate when a variable must live in a register. We also include type
443 constraints after a `:`.
445 ### Addresses and handles
447 You can compute the address of any variable in memory (never in registers):
450 var/reg: (addr T) <- address var2: T
453 As mentioned up top, `addr` variables can never escape the function where
454 they're computed. You can't store them on the heap, or in compound types.
455 Think of them as short-lived things.
457 To manage long-lived addresses, _allocate_ them on the heap.
460 allocate var: (addr handle T) # var can be in either register or memory
463 Handles can be copied and stored without restriction. However, they're too
464 large to fit in a register. You also can't access their payload directly, you
465 have to first convert them into a short-lived `addr` using _lookup_.
468 var y/eax: (addr T) <- lookup x: (handle T)
471 Since handles are large compound types, there's a special helper for comparing
475 var/eax: boolean <- handle-equal? var1: (handle T), var2: (handle T)
480 Arrays are declared in two ways:
481 1. On the stack with a literal size:
485 2. On the heap with a potentially variable size. For example:
487 var x: (handle array int)
488 var x-ah/eax: (addr handle array int) <- address x
492 The `8` here can also be an int in a register or memory. (The `-ah` is a
493 common variable naming convention and stands for "address of a handle".
494 Essential for allocating long-lived data on the heap.)
496 You can compute the length of an array, though you'll need an `addr` to do so:
499 var/reg: int <- length arr/reg: (addr array T)
502 To read from or write to an array, use `index` to first obtain an address to
506 var/reg: (addr T) <- index arr/reg: (addr array T), n
507 var/reg: (addr T) <- index arr: (array T len), n
510 Like our notation of `n`, `len` here is required to be a literal.
512 The index requested can also be a variable in a register, with one caveat:
515 var/reg: (addr T) <- index arr/reg: (addr array T), idx/reg: int
516 var/reg: (addr T) <- index arr: (array T len), idx/reg: int
519 The caveat: the size of T must be 1, 2, 4 or 8 bytes. For other sizes of T
520 you'll need to split up the work, performing a `compute-offset` before the
524 var/reg: (offset T) <- compute-offset arr: (addr array T), idx/reg: int # arr can be in reg or mem
525 var/reg: (offset T) <- compute-offset arr: (addr array T), idx: int # arr can be in reg or mem
528 The result of a `compute-offset` statement can be passed to `index`:
531 var/reg: (addr T) <- index arr/reg: (addr array T), idx/reg: (offset T)
534 ### Stream operations
536 A common use for arrays is as buffers. Save a few items to a scratch space and
537 then process them. This pattern is so common (we use it in files) that there's
538 special support for it with a built-in type: `stream`.
540 Streams are like arrays in many ways. You can initialize them with a length on
544 var x: (stream int 3)
547 You can also populate them on the heap:
549 var x: (handle stream int)
550 var x-ah/eax: (addr handle stream int) <- address x
551 populate-stream x-ah, 8
554 However, streams don't provide random access with an `index` instruction.
555 Instead, you write to them sequentially, and read back what you wrote.
558 read-from-stream s: (addr stream T), out: (addr T)
559 write-to-stream s: (addr stream T), in: (addr T)
562 Streams of bytes are particularly common for managing Unicode text, and there
563 are a few functions to help with them:
566 write s: (addr stream byte), u: (addr array byte) # write u to s, abort if full
567 overflow?/eax: boolean <- try-write s: (addr stream byte), u: (addr array byte)
568 write-stream dest: (addr stream byte), src: (addr stream byte)
570 append-byte s: (addr stream byte), var: int # write lower byte of var
571 var/eax: byte <- read-byte s: (addr stream byte)
572 # 32-bit graphemes encoded in UTF-8
573 write-grapheme out: (addr stream byte), g: grapheme
574 g/eax: grapheme <- read-grapheme in: (addr stream byte)
577 You can check if a stream is empty or full:
580 var/eax: boolean <- stream-empty? s: (addr stream)
581 var/eax: boolean <- stream-full? s: (addr stream)
584 You can clear streams:
587 clear-stream f: (addr stream T)
590 You can also rewind them to reread their contents:
593 rewind-stream f: (addr stream T)
598 Primitive types can be combined together using the `type` keyword. For
608 Mu programs are sequences of just `fn` and `type` definitions.
610 Compound types can't include `addr` types for safety reasons (use `handle` instead,
611 which is described below). They also can't currently include `array`, `stream`
612 or `byte` types. Since arrays and streams carry their size with them, supporting
613 them in compound types complicates variable initialization. Instead of
614 defining them inline in a type definition, define a `handle` to them. Bytes
615 shouldn't be used for anything but UTF-8 strings.
617 To access within a compound type, use the `get` instruction. There are two
618 forms. You need either a variable of the type itself (say `T`) in memory, or a
619 variable of type `(addr T)` in a register.
622 var/reg: (addr T_f) <- get var/reg: (addr T), f
623 var/reg: (addr T_f) <- get var: T, f
626 The `f` here is the field name from the `type` definition, and its type `T_f`
627 must match the type of `f` in the `type` definition. For example, some legal
628 instructions for the definition of `point` above:
631 var a/eax: (addr int) <- get p, x
632 var a/eax: (addr int) <- get p, y
635 You can clear compound types using the `clear-object` function:
638 clear-object var: (addr T)
641 You can shallow-copy compound types using the `copy-object` function:
644 copy-object src: (addr T), dest: (addr T)