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 -> _/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 literal constants (either integers or string literals that get replaced with
26 constant addresses). Outputs are always variables in registers.
28 Inouts in memory can be either inputs or outputs (if they're addresses being
29 written to). Hence the name.
31 Primitives usually require their inouts to be in specific combinations of
32 memory and register. They can't handle more than one inout in memory.
33 User-defined functions are flexible.
35 Primitives can often write to arbitrary output registers. User-defined
36 functions, however, require rigidly specified output registers. Notice how the
37 definition of `foo` above writes to `eax`.
39 A function's header names its inouts, but not its outputs (hence the `_`
42 All Mu programs must define at least one function: `main`. That's where they
43 begin executing instructions.
45 ## Variables, registers and memory
47 Declare local variables in a function using the `var` keyword. As shown above,
48 all variable declarations should specify a type after a `:`.
50 You can declare local variables in either registers or memory (the _stack_
51 segment). So a `var` statement has two forms:
52 - Living in a register, e.g. `var x/eax: int <- copy 0` defines `x` which
54 - Living in memory, e.g. `var x: int` defines `x` on the stack.
56 Variables in registers must be initialized. Variables on the stack are
57 implicitly zeroed out.
59 Variables exist only within the `{}` block they're defined in. Space allocated
60 to them on the stack is reclaimed after execution leaves the block. Registers
61 restore whatever variable was using them in the outer block.
63 It is perfectly ok to reuse a register for a new variable. Even in a single
64 block (though you permanently lose the old variable then).
66 Variables can be in six 32-bit _general-purpose_ registers of the x86 processor.
71 - esi ('s' often a mnemonic for 'source')
72 - edi ('d' often a mnemonic for 'destination')
74 Most functions return results in `eax` by convention. In practice, it ends up
75 churning through variables pretty quickly.
77 You can store several types in these registers:
80 - (addr T) (address into memory)
81 - byte (uses only 8 bits)
82 - code-point (Unicode)
83 - code-point-utf8 (code-point encoded in UTF-8)
85 There's one 32-bit type you _cannot_ store in these registers:
88 It instead uses eight separate 32-bit registers: xmm0, xmm1, ..., xmm7
90 Types that require more than 32 bits (4 bytes) cannot be stored in registers:
95 - any compound types you define using the `type` keyword
97 `T` here can be any type, including combinations of types. For example:
98 - (array int) -- an array of ints
99 - (addr int) -- an address to an int
100 - (addr array byte) -- an address to an array of bytes, useful for Unicode
102 - (handle int) -- a handle to an int
103 - (addr handle int) -- an address to a handle to int
104 - (addr array handle int) -- an address to an array of handles to ints
107 Other miscellaneous restrictions:
108 - `byte` variables must be either in registers or on the heap, never local
109 variables on the stack.
110 - `addr` variables can never "escape" a function either by being returned or
111 by being written to a memory location. When you need that sort of thing,
112 use a `handle` instead.
114 ## Operations on simple types
116 We'll now survey a long list of statement forms that operate on 32-bit types.
117 Most of these are primitives, but some are also implemented as functions
118 (which have slightly different rules as mentioned up top). Most instructions
119 with multiple args require types to match. Various operations have other
120 restrictions which we'll note below, using the following notation:
121 - `var/reg` indicates a variable in some register. Where we require a
122 variable in a specific register, we'll mention it explicitly. E.g.
124 - `var/xreg` indicates a variable in some floating-point register `xmm_`.
125 - `var` without a `reg` indicates either a variable on the stack, or
126 dereferencing a variable in a (non-floating-point) register: `*var/reg`.
127 - `var: type` indicates a variable that must satisfy some type constraint.
128 - `n` indicates a literal integer. There are no floating-point literals.
130 ### Moving values around
132 These instructions work with variables of any 32-bit type except `byte` and
136 var/reg <- copy var2/reg2
137 copy-to var1, var2/reg
143 Byte variables have their own instructions:
146 var/reg <- copy-byte var2/reg2
147 var/reg <- copy-byte *var2/reg2 # var2 must have type (addr byte)
148 copy-byte-to *var1/reg1, var2/reg2 # var1 must have type (addr byte)
151 Floating point variables can be copied as well, but only to or from
152 floating-point registers `xmm_`.
155 var/xreg <- copy var2/xreg2
156 copy-to var1, var2/xreg
157 var/xreg <- copy var2
158 var/xreg <- copy *var2/reg2 # var2 must have type (addr byte) and live in a general-purpose register
161 There's no way to copy a literal to a floating-point register. However,
162 there's a few ways to convert non-float values in general-purpose registers.
165 var/xreg <- convert var2/reg2
166 var/xreg <- convert var2
167 var/xreg <- convert *var2/reg2
170 Correspondingly, there are ways to convert floats into integers, with and
174 var/reg <- convert var2/xreg2
175 var/reg <- convert var2
176 var/reg <- convert *var2/reg2
178 var/reg <- truncate var2/xreg2
179 var/reg <- truncate var2
180 var/reg <- truncate *var2/reg2
183 Still, the absence of fractional literals is an annoyance. Mu provides some
184 helpers to mitigate it somewhat:
187 result/xmm0 <- rational numerator: int, denominator: int
188 fill-in-rational out: (addr float), numerator: int, denominator: int
191 These are functions, so the inouts have fewer restrictions while the outputs
192 have more. The inouts can be registers, or memory, or even literals. The
193 output for `rational` _must_ be in register `xmm0`.
197 Work with variables of any 32-bit type. `addr` variables can only be compared
201 compare var1, var2/reg
202 compare var1/reg, var2
207 Floating-point numbers cannot be compared to literals, and the register must
211 compare var1/xreg1, var2/xreg2
212 compare var1/xreg1, var2
217 Immediately after a `compare` instruction you can branch on its result. For
224 This instruction will jump to after the enclosing `{}` block if the previous
225 `compare` detected equality. Here's the list of conditional and unconditional
226 `break` instructions:
238 Similarly, you can jump back to the start of the enclosing `{}` block with
239 `loop`. Here's the list of `loop` instructions.
251 Additionally, there are special variants for comparing `addr` and `float`
252 values, which results in the following comprehensive list of jumps:
258 break-if-< break-if-addr< break-if-float<
259 break-if-> break-if-addr> break-if-float>
260 break-if-<= break-if-addr<= break-if-float<=
261 break-if->= break-if-addr>= break-if-float>=
266 loop-if-< loop-if-addr< loop-if-float<
267 loop-if-> loop-if-addr> loop-if-float>
268 loop-if-<= loop-if-addr<= loop-if-float<=
269 loop-if->= loop-if-addr>= loop-if-float>=
272 One final property all these jump instructions share: they can take an
273 optional block name to jump to. For example:
294 However, there's no way to jump to a block that doesn't contain the `loop` or
297 ### Integer arithmetic
299 These instructions require variables of non-`addr`, non-`float` types.
303 var1/reg1 <- add var2/reg2
305 add-to var1, var2/reg # var1 += var2
312 var1/reg1 <- subtract var2/reg2
313 var1/reg <- subtract var2
314 subtract-from var1, var2/reg # var1 -= var2
315 var/reg <- subtract n
333 var1/reg <- multiply var2
336 The result of a multiply must be a register.
344 ### Fractional arithmetic
346 Operations on `float` variables include a few we've seen before and some new
347 ones. Notice here that we mostly use floating-point registers `xmm_`, but
348 still use the general-purpose registers when dereferencing variables of type
352 var1/xreg <- add var2/xreg2
353 var1/xreg <- add var2
354 var1/xreg <- add *var2/reg2
356 var1/xreg <- subtract var2/xreg2
357 var1/xreg <- subtract var2
358 var1/xreg <- subtract *var2/reg2
360 var1/xreg <- multiply var2/xreg2
361 var1/xreg <- multiply var2
362 var1/xreg <- multiply *var2/reg2
364 var1/xreg <- divide var2/xreg2
365 var1/xreg <- divide var2
366 var1/xreg <- divide *var2/reg2
368 var1/xreg <- reciprocal var2/xreg2
369 var1/xreg <- reciprocal var2
370 var1/xreg <- reciprocal *var2/reg2
372 var1/xreg <- square-root var2/xreg2
373 var1/xreg <- square-root var2
374 var1/xreg <- square-root *var2/reg2
376 var1/xreg <- inverse-square-root var2/xreg2
377 var1/xreg <- inverse-square-root var2
378 var1/xreg <- inverse-square-root *var2/reg2
380 var1/xreg <- min var2/xreg2
381 var1/xreg <- min var2
382 var1/xreg <- min *var2/reg2
384 var1/xreg <- max var2/xreg2
385 var1/xreg <- max var2
386 var1/xreg <- max *var2/reg2
389 Two instructions in the above list are approximate. According to the Intel
390 manual, `reciprocal` and `inverse-square-root` [go off the rails around the
391 fourth decimal place](linux/x86_approx.md). If you need more precision, use
394 ### Bitwise boolean operations
396 These require variables of non-`addr`, non-`float` types.
400 var1/reg1 <- and var2/reg2
402 and-with var1, var2/reg
409 var1/reg1 <- or var2/reg2
411 or-with var1, var2/reg
424 var1/reg1 <- xor var2/reg2
426 xor-with var1, var2/reg
433 Shifts require variables of non-`addr`, non-`float` types.
436 var/reg <- shift-left n
437 var/reg <- shift-right n
438 var/reg <- shift-right-signed n
441 shift-right-signed var, n
444 Shifting bits left always inserts zeros on the right.
445 Shifting bits right inserts zeros on the left by default.
446 A _signed_ shift right duplicates the leftmost bit, thereby preserving the
449 ## Operations on more complex types
451 These instructions work with any type `T`. As before we use `/reg` here to
452 indicate when a variable must live in a register. We also include type
453 constraints after a `:`.
455 ### Addresses and handles
457 You can compute the address of any variable in memory (never in registers):
460 var1/reg: (addr T) <- address var2: T
463 As mentioned up top, `addr` variables can never escape the function where
464 they're computed. You can't store them on the heap, or in compound types.
465 Think of them as short-lived things.
467 To manage long-lived addresses, _allocate_ them on the heap.
470 allocate var: (addr handle T) # var can be in either register or memory
473 Handles can be copied and stored without restriction. However, they're too
474 large to fit in a register. You also can't access their payload directly, you
475 have to first convert them into a short-lived `addr` using _lookup_.
478 var1/eax: (addr T) <- lookup var2: (handle T) # var2 in either register or memory
481 Since handles are large compound types, there's a special helper for comparing
485 var/eax: boolean <- handle-equal? var1: (handle T), var2: (handle T)
490 Arrays are declared in two ways:
491 1. On the stack with a literal size:
495 2. On the heap with a potentially variable size. For example:
497 var x: (handle array int)
498 var x-ah/eax: (addr handle array int) <- address x
502 The `8` here can also be an int in a register or memory. (The `-ah` is a
503 common variable naming convention and stands for "address of a handle".
504 Essential for allocating long-lived data on the heap.)
506 You can compute the length of an array, though you'll need an `addr` to do so:
509 var/reg: int <- length arr/reg: (addr array T)
512 To read from or write to an array, use `index` to first obtain an address to
516 var/reg: (addr T) <- index arr/reg: (addr array T), n
517 var/reg: (addr T) <- index arr: (array T len), n
520 Like our notation of `n`, `len` here is required to be a literal.
522 The index requested can also be a variable in a register, with one caveat:
525 var/reg: (addr T) <- index arr/reg: (addr array T), idx/reg: int
526 var/reg: (addr T) <- index arr: (array T len), idx/reg: int
529 The caveat: the size of T must be 1, 2, 4 or 8 bytes. For other sizes of T
530 you'll need to split up the work, performing a `compute-offset` before the
534 var/reg: (offset T) <- compute-offset arr: (addr array T), idx/reg: int # arr can be in reg or mem
535 var/reg: (offset T) <- compute-offset arr: (addr array T), idx: int # arr can be in reg or mem
538 The result of a `compute-offset` statement can be passed to `index`:
541 var/reg: (addr T) <- index arr/reg: (addr array T), idx/reg: (offset T)
546 A common use for arrays is as buffers. Save a few items to a scratch space and
547 then process them. This pattern is so common (we use it in files) that there's
548 special support for it with a built-in type: `stream`.
550 Streams are like arrays in many ways. You can initialize them with a length on
554 var x: (stream int 3)
557 You can also populate them on the heap:
559 var x: (handle stream int)
560 var x-ah/eax: (addr handle stream int) <- address x
561 populate-stream x-ah, 8
564 However, streams don't provide random access with an `index` instruction.
565 Instead, you write to them sequentially, and read back what you wrote.
568 read-from-stream s: (addr stream T), out: (addr T)
569 write-to-stream s: (addr stream T), in: (addr T)
572 Streams of bytes are particularly common for managing Unicode text, and there
573 are a few functions to help with them:
576 write s: (addr stream byte), u: (addr array byte) # write u to s, abort if full
577 overflow?/eax: boolean <- try-write s: (addr stream byte), u: (addr array byte)
578 write-stream dest: (addr stream byte), src: (addr stream byte)
580 append-byte s: (addr stream byte), var: int # write lower byte of var
581 var/eax: byte <- read-byte s: (addr stream byte)
582 # 32-bit code-point-utf8s encoded in UTF-8
583 write-code-point-utf8 out: (addr stream byte), g: code-point-utf8
584 g/eax: code-point-utf8 <- read-code-point-utf8 in: (addr stream byte)
587 You can check if a stream is empty or full:
590 var/eax: boolean <- stream-empty? s: (addr stream)
591 var/eax: boolean <- stream-full? s: (addr stream)
594 You can clear streams:
597 clear-stream f: (addr stream T)
600 You can also rewind them to reread their contents:
603 rewind-stream f: (addr stream T)
608 Primitive types can be combined together using the `type` keyword. For
618 Mu programs are sequences of just `fn` and `type` definitions.
620 Compound types can't include `addr` types for safety reasons (use `handle` instead,
621 which is described below). They also can't currently include `array`, `stream`
622 or `byte` types. Since arrays and streams carry their size with them, supporting
623 them in compound types complicates variable initialization. Instead of
624 defining them inline in a type definition, define a `handle` to them. Bytes
625 shouldn't be used for anything but UTF-8 strings.
627 To access within a compound type, use the `get` instruction. There are two
628 forms. You need either a variable of the type itself (say `T`) in memory, or a
629 variable of type `(addr T)` in a register.
632 var/reg: (addr T_f) <- get var/reg: (addr T), f
633 var/reg: (addr T_f) <- get var: T, f
636 The `f` here is the field name from the `type` definition, and its type `T_f`
637 must match the type of `f` in the `type` definition. For example, some legal
638 instructions for the definition of `point` above:
641 var a/eax: (addr int) <- get p, x
642 var a/eax: (addr int) <- get p, y
645 You can clear compound types using the `clear-object` function:
648 clear-object var: (addr T)
651 You can shallow-copy compound types using the `copy-object` function:
654 copy-object src: (addr T), dest: (addr T)