diff options
author | Kartik Agaram <vc@akkartik.com> | 2020-10-05 10:12:34 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2020-10-05 10:12:34 -0700 |
commit | fe34dc47187ed489b4268a7dbc85ae5b93ca6588 (patch) | |
tree | 8f63635e3b79b7b6ee79af4666f53d71ac008c7c | |
parent | 5c3d495dbe1ffa3141dc09372583d3b1973a3358 (diff) | |
download | mu-fe34dc47187ed489b4268a7dbc85ae5b93ca6588.tar.gz |
6955
-rw-r--r-- | html/mu_instructions.html | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/html/mu_instructions.html b/html/mu_instructions.html index 476b7133..8f21aa08 100644 --- a/html/mu_instructions.html +++ b/html/mu_instructions.html @@ -13,17 +13,16 @@ pre { font-family: monospace; color: #000000; background-color: #c6c6c6; } body { font-family: monospace; color: #000000; background-color: #c6c6c6; } * { font-size: 1em; } -.Comment { color: #005faf; } +.muComment { color: #005faf; } .SpecialChar { color: #d70000; } .Constant { color: #008787; } .PreProc { color: #c000c0; } -.SalientComment { color: #0000af; } --> </style> </head> <body> <pre id='vimCodeElement'> -<span class="SalientComment">## Mu's instructions and their table-driven translation</span> +<span class="muComment">## Mu's instructions and their table-driven translation</span> See <a href="http://akkartik.name/akkartik-convivial-20200607.pdf">http://akkartik.name/akkartik-convivial-20200607.pdf</a> for the complete story. In brief: Mu is a statement-oriented language. Blocks consist of flat @@ -106,6 +105,10 @@ var/reg <span class="SpecialChar"><-</span> xor n => <spa xor-with var, n => <span class="Constant">"81 6/subop/xor *(ebp+"</span> var.stack-offset <span class="Constant">") "</span> n <span class="Constant">"/imm32"</span> xor-with *var/reg, n => <span class="Constant">"81 6/subop/xor *"</span> reg <span class="Constant">" "</span> n <span class="Constant">"/imm32"</span> +var/reg <span class="SpecialChar"><-</span> negate => <span class="Constant">"f7 3/subop/negate %"</span> reg +negate var => <span class="Constant">"f7 3/subop/negate *(ebp+"</span> var.stack-offset <span class="Constant">")"</span> +negate *var/reg => <span class="Constant">"f7 3/subop/negate *"</span> reg + var/reg <span class="SpecialChar"><-</span> shift-left n => <span class="Constant">"c1/shift 4/subop/left %"</span> reg <span class="Constant">" "</span> n <span class="Constant">"/imm32"</span> var/reg <span class="SpecialChar"><-</span> shift-right n => <span class="Constant">"c1/shift 5/subop/right %"</span> reg <span class="Constant">" "</span> n <span class="Constant">"/imm32"</span> var/reg <span class="SpecialChar"><-</span> shift-right-signed n => <span class="Constant">"c1/shift 7/subop/right-signed %"</span> reg <span class="Constant">" "</span> n <span class="Constant">"/imm32"</span> @@ -202,7 +205,7 @@ var/reg <span class="SpecialChar"><-</span> multiply *var2/reg2 => <spa <span class="PreProc">loop-if-addr>=</span> => <span class="Constant">"0f 83/jump-if-addr>= loop/disp32"</span> <span class="PreProc">loop-if-addr>=</span> label => <span class="Constant">"0f 83/jump-if-addr>= "</span> label <span class="Constant">":loop/disp32"</span> -Similar float variants like `<span class="PreProc">break-if-float<</span>` are aliases for the corresponding +Similar float variants like `<span class="PreProc">break-if-float<`</span> are aliases for the corresponding `addr` equivalents. The x86 instruction set stupidly has floating-point operations only update a subset of flags. @@ -211,12 +214,12 @@ operations only update a subset of flags. In the following instructions types are provided for clarity even if they must be provided in an earlier 'var' declaration. -<span class="Comment"># Address operations</span> +<span class="muComment"># Address operations</span> var/reg: (addr T) <span class="SpecialChar"><-</span> address var2: T => <span class="Constant">"8d/copy-address *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> reg <span class="Constant">"/r32"</span> -<span class="Comment"># Array operations</span> +<span class="muComment"># Array operations</span> (TODO: bounds-checking) var/reg <span class="SpecialChar"><-</span> index arr/rega: (addr array T), idx/regi: int @@ -229,9 +232,9 @@ var/reg <span class="SpecialChar"><-</span> index arr/rega: (addr array T), n var/reg <span class="SpecialChar"><-</span> index arr: (array T sz), n => <span class="Constant">"8d/copy-address *(ebp+"</span> (arr.stack-offset+<span class="Constant">4</span>+n*size-of(T)) <span class="Constant">") "</span> reg <span class="Constant">"/r32"</span> -var/reg: (offset T) <span class="SpecialChar"><-</span> compute-offset arr: (addr array T), idx/regi: int <span class="Comment"># arr can be in reg or mem</span> +var/reg: (offset T) <span class="SpecialChar"><-</span> compute-offset arr: (addr array T), idx/regi: int <span class="muComment"># arr can be in reg or mem</span> => <span class="Constant">"69/multiply %"</span> regi <span class="Constant">" "</span> size-of(T) <span class="Constant">"/imm32 "</span> reg <span class="Constant">"/r32"</span> -var/reg: (offset T) <span class="SpecialChar"><-</span> compute-offset arr: (addr array T), idx: int <span class="Comment"># arr can be in reg or mem</span> +var/reg: (offset T) <span class="SpecialChar"><-</span> compute-offset arr: (addr array T), idx: int <span class="muComment"># arr can be in reg or mem</span> => <span class="Constant">"69/multiply *(ebp+"</span> idx.stack-offset <span class="Constant">") "</span> size-of(T) <span class="Constant">"/imm32 "</span> reg <span class="Constant">"/r32"</span> var/reg <span class="SpecialChar"><-</span> index arr/rega: (addr array T), o/rego: offset => <span class="Constant">"8d/copy-address *("</span> rega <span class="Constant">"+"</span> rego <span class="Constant">"+4) "</span> reg <span class="Constant">"/r32"</span> @@ -254,7 +257,7 @@ var/reg <span class="SpecialChar"><-</span> length arr/reg2: (addr array T) if reg is not <span class="Constant">edx</span> <span class="Constant">"52/push-edx"</span> <span class="Constant">"8b/-> *"</span> reg2 <span class="Constant">" eax/r32"</span> - <span class="Constant">"31/xor %edx 2/r32/edx"</span> <span class="Comment"># sign-extend, but array size can't be negative</span> + <span class="Constant">"31/xor %edx 2/r32/edx"</span> <span class="muComment"># sign-extend, but array size can't be negative</span> <span class="Constant">"b9/copy-to-ecx "</span> size-of(T) <span class="Constant">"/imm32"</span> <span class="Constant">"f7 7/subop/idiv-eax-edx-by %ecx"</span> if reg is not <span class="Constant">eax</span> @@ -266,7 +269,7 @@ var/reg <span class="SpecialChar"><-</span> length arr/reg2: (addr array T) if reg is not <span class="Constant">eax</span> <span class="Constant">"58/pop-to-eax"</span> -<span class="Comment"># User-defined types</span> +<span class="muComment"># User-defined types</span> If a record (product) type T was defined to have elements a, b, c, ... of types T_a, T_b, T_c, ..., then accessing one of those elements f of type T_f: @@ -276,15 +279,15 @@ var/reg: (addr T_f) <span class="SpecialChar"><-</span> get var2/reg2: (addr var/reg: (addr T_f) <span class="SpecialChar"><-</span> get var2: T, f => <span class="Constant">"8d/copy-address *(ebp+"</span> var2.stack-offset <span class="Constant">"+"</span> offset(f) <span class="Constant">") "</span> reg <span class="Constant">"/r32"</span> -<span class="Comment"># Allocating memory</span> +<span class="muComment"># Allocating memory</span> allocate in: (addr handle T) => <span class="Constant">"(allocate Heap "</span> size-of(T) <span class="Constant">" "</span> in <span class="Constant">")"</span> -populate in: (addr handle array T), num <span class="Comment"># can be literal or variable on stack or register</span> +populate in: (addr handle array T), num <span class="muComment"># can be literal or variable on stack or register</span> => <span class="Constant">"(allocate-array2 Heap "</span> size-of(T) <span class="Constant">" "</span> num <span class="Constant">" "</span> in <span class="Constant">")"</span> -populate-stream in: (addr handle stream T), num <span class="Comment"># can be literal or variable on stack or register</span> +populate-stream in: (addr handle stream T), num <span class="muComment"># can be literal or variable on stack or register</span> => <span class="Constant">"(new-stream Heap "</span> size-of(T) <span class="Constant">" "</span> num <span class="Constant">" "</span> in <span class="Constant">")"</span> read-from-stream s: (addr stream T), out: (addr T) @@ -293,47 +296,47 @@ read-from-stream s: (addr stream T), out: (addr T) write-to-stream s: (addr stream T), in: (addr T) => <span class="Constant">"(write-to-stream "</span> s <span class="Constant">" "</span> in <span class="Constant">" "</span> size-of(T) <span class="Constant">")"</span> -<span class="Comment"># Floating-point operations</span> +<span class="muComment"># Floating-point operations</span> All the instructions so far use Intel's general-purpose integer registers. However, some of them translate to different SubX if their arguments are in floating-point registers. -var/xreg <span class="SpecialChar"><-</span> add var2/xreg2 => <span class="Constant">"f3 0f 58/add 3/mod "</span> xreg2 <span class="Constant">"/xm32 "</span> xreg1 <span class="Constant">"/x32"</span> +var/xreg <span class="SpecialChar"><-</span> add var2/xreg2 => <span class="Constant">"f3 0f 58/add %"</span> xreg2 <span class="Constant">" "</span> xreg1 <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> add var2 => <span class="Constant">"f3 0f 58/add *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> add *var2/reg2 => <span class="Constant">"f3 0f 58/add *"</span> reg2 <span class="Constant">" "</span> xreg <span class="Constant">"/x32"</span> -var/xreg <span class="SpecialChar"><-</span> subtract var2/xreg2 => <span class="Constant">"f3 0f 5c/subtract 3/mod "</span> xreg2 <span class="Constant">"/xm32 "</span> xreg1 <span class="Constant">"/x32"</span> +var/xreg <span class="SpecialChar"><-</span> subtract var2/xreg2 => <span class="Constant">"f3 0f 5c/subtract %"</span> xreg2 <span class="Constant">" "</span> xreg1 <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> subtract var2 => <span class="Constant">"f3 0f 5c/subtract *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> subtract *var2/reg2 => <span class="Constant">"f3 0f 5c/subtract *"</span> reg2 <span class="Constant">" "</span> xreg <span class="Constant">"/x32"</span> -var/xreg <span class="SpecialChar"><-</span> multiply var2/xreg2 => <span class="Constant">"f3 0f 59/multiply 3/mod "</span> xreg2 <span class="Constant">"/xm32 "</span> xreg1 <span class="Constant">"/x32"</span> +var/xreg <span class="SpecialChar"><-</span> multiply var2/xreg2 => <span class="Constant">"f3 0f 59/multiply %"</span> xreg2 <span class="Constant">" "</span> xreg1 <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> multiply var2 => <span class="Constant">"f3 0f 59/multiply *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> multiply *var2/reg2 => <span class="Constant">"f3 0f 59/multiply *"</span> reg2 <span class="Constant">" "</span> xreg <span class="Constant">"/x32"</span> -var/xreg <span class="SpecialChar"><-</span> divide var2/xreg2 => <span class="Constant">"f3 0f 5e/divide 3/mod "</span> xreg2 <span class="Constant">"/xm32 "</span> xreg1 <span class="Constant">"/x32"</span> +var/xreg <span class="SpecialChar"><-</span> divide var2/xreg2 => <span class="Constant">"f3 0f 5e/divide %"</span> xreg2 <span class="Constant">" "</span> xreg1 <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> divide var2 => <span class="Constant">"f3 0f 5e/divide *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> divide *var2/reg2 => <span class="Constant">"f3 0f 5e/divide *"</span> reg2 <span class="Constant">" "</span> xreg <span class="Constant">"/x32"</span> There are also some exclusively floating-point instructions: -var/xreg <span class="SpecialChar"><-</span> reciprocal var2/xreg2 => <span class="Constant">"f3 0f 53/reciprocal 3/mod "</span> xreg2 <span class="Constant">"/xm32 "</span> xreg1 <span class="Constant">"/x32"</span> +var/xreg <span class="SpecialChar"><-</span> reciprocal var2/xreg2 => <span class="Constant">"f3 0f 53/reciprocal %"</span> xreg2 <span class="Constant">" "</span> xreg1 <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> reciprocal var2 => <span class="Constant">"f3 0f 53/reciprocal *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> reciprocal *var2/reg2 => <span class="Constant">"f3 0f 53/reciprocal *"</span> reg2 <span class="Constant">" "</span> xreg <span class="Constant">"/x32"</span> -var/xreg <span class="SpecialChar"><-</span> square-root var2/xreg2 => <span class="Constant">"f3 0f 51/square-root 3/mod "</span> xreg2 <span class="Constant">"/xm32 "</span> xreg1 <span class="Constant">"/x32"</span> +var/xreg <span class="SpecialChar"><-</span> square-root var2/xreg2 => <span class="Constant">"f3 0f 51/square-root %"</span> xreg2 <span class="Constant">" "</span> xreg1 <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> square-root var2 => <span class="Constant">"f3 0f 51/square-root *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> square-root *var2/reg2 => <span class="Constant">"f3 0f 51/square-root *"</span> reg2 <span class="Constant">" "</span> xreg <span class="Constant">"/x32"</span> -var/xreg <span class="SpecialChar"><-</span> inverse-square-root var2/xreg2 => <span class="Constant">"f3 0f 52/inverse-square-root 3/mod "</span> xreg2 <span class="Constant">"/xm32 "</span> xreg1 <span class="Constant">"/x32"</span> +var/xreg <span class="SpecialChar"><-</span> inverse-square-root var2/xreg2 => <span class="Constant">"f3 0f 52/inverse-square-root %"</span> xreg2 <span class="Constant">" "</span> xreg1 <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> inverse-square-root var2 => <span class="Constant">"f3 0f 52/inverse-square-root *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> inverse-square-root *var2/reg2 => <span class="Constant">"f3 0f 52/inverse-square-root *"</span> reg2 <span class="Constant">" "</span> xreg <span class="Constant">"/x32"</span> -var/xreg <span class="SpecialChar"><-</span> min var2/xreg2 => <span class="Constant">"f3 0f 5d/min 3/mod "</span> xreg2 <span class="Constant">"/xm32 "</span> xreg1 <span class="Constant">"/x32"</span> +var/xreg <span class="SpecialChar"><-</span> min var2/xreg2 => <span class="Constant">"f3 0f 5d/min %"</span> xreg2 <span class="Constant">" "</span> xreg1 <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> min var2 => <span class="Constant">"f3 0f 5d/min *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> min *var2/reg2 => <span class="Constant">"f3 0f 5d/min *"</span> reg2 <span class="Constant">" "</span> xreg <span class="Constant">"/x32"</span> -var/xreg <span class="SpecialChar"><-</span> max var2/xreg2 => <span class="Constant">"f3 0f 5f/max 3/mod "</span> xreg2 <span class="Constant">"/xm32 "</span> xreg1 <span class="Constant">"/x32"</span> +var/xreg <span class="SpecialChar"><-</span> max var2/xreg2 => <span class="Constant">"f3 0f 5f/max %"</span> xreg2 <span class="Constant">" "</span> xreg1 <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> max var2 => <span class="Constant">"f3 0f 5f/max *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> max *var2/reg2 => <span class="Constant">"f3 0f 5f/max *"</span> reg2 <span class="Constant">" "</span> xreg <span class="Constant">"/x32"</span> @@ -348,7 +351,7 @@ var/xreg <span class="SpecialChar"><-</span> convert var2/reg2 => <spa var/xreg <span class="SpecialChar"><-</span> convert var2 => <span class="Constant">"f3 0f 2a/convert-to-float *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> convert *var2/reg2 => <span class="Constant">"f3 0f 2a/convert-to-float *"</span> reg2 <span class="Constant">" "</span> xreg <span class="Constant">"/x32"</span> -var/reg <span class="SpecialChar"><-</span> convert var2/xreg2 => <span class="Constant">"f3 0f 2d/convert-to-int 3/mod "</span> xreg2 <span class="Constant">"/xm32 "</span> reg <span class="Constant">"/r32"</span> +var/reg <span class="SpecialChar"><-</span> convert var2/xreg2 => <span class="Constant">"f3 0f 2d/convert-to-int %"</span> xreg2 <span class="Constant">" "</span> reg <span class="Constant">"/r32"</span> var/reg <span class="SpecialChar"><-</span> convert var2 => <span class="Constant">"f3 0f 2d/convert-to-int *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> reg <span class="Constant">"/r32"</span> var/reg <span class="SpecialChar"><-</span> convert *var2/reg2 => <span class="Constant">"f3 0f 2d/convert-to-int *"</span> reg2 <span class="Constant">" "</span> reg <span class="Constant">"/r32"</span> @@ -360,15 +363,15 @@ One pattern you may have noticed above is that the floating-point instructions above always write to registers. The only exceptions are `copy` instructions, which can write to memory locations. -var/xreg <span class="SpecialChar"><-</span> copy var2/xreg2 => <span class="Constant">"f3 0f 11/<- 3/mod "</span> xreg <span class="Constant">"/xm32 "</span> xreg2 <span class="Constant">"/x32"</span> +var/xreg <span class="SpecialChar"><-</span> copy var2/xreg2 => <span class="Constant">"f3 0f 11/<- %"</span> xreg <span class="Constant">" "</span> xreg2 <span class="Constant">"/x32"</span> copy-to var1, var2/xreg => <span class="Constant">"f3 0f 11/<- *(ebp+"</span> var1.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> copy var2 => <span class="Constant">"f3 0f 10/-> *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg <span class="Constant">"/x32"</span> var/xreg <span class="SpecialChar"><-</span> copy *var2/reg2 => <span class="Constant">"f3 0f 10/-> *"</span> reg2 <span class="Constant">" "</span> xreg <span class="Constant">"/x32"</span> Comparisons must always start with a register: -compare var1/xreg1, var2/xreg2 => <span class="Constant">"0f 2f/compare 3/mod "</span> xreg2 <span class="Constant">"/xm32 "</span> xreg1 <span class="Constant">"/x32"</span> -compare var1/xreg1, var2 => <span class="Constant">"0f 2f/compare 2/mod *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg1 <span class="Constant">"/x32"</span> +compare var1/xreg1, var2/xreg2 => <span class="Constant">"0f 2f/compare %"</span> xreg2 <span class="Constant">" "</span> xreg1 <span class="Constant">"/x32"</span> +compare var1/xreg1, var2 => <span class="Constant">"0f 2f/compare *(ebp+"</span> var2.stack-offset <span class="Constant">") "</span> xreg1 <span class="Constant">"/x32"</span> vim:ft=mu:nowrap:textwidth=<span class="Constant">0</span> </pre> |