## Mu's instructions and their table-driven translation See http://akkartik.name/akkartik-convivial-20200607.pdf for the complete story. In brief: Mu is a statement-oriented language. Blocks consist of flat lists of instructions. Instructions can have inputs after the operation, and outputs to the left of a '<-'. Inputs and outputs must be variables. They can't include nested expressions. Variables can be literals ('n'), or live in a register ('var/reg') or in memory ('var') at some 'stack-offset' from the 'ebp' register. Outputs must be registers. To modify a variable in memory, pass it in by reference as an input. (Inputs are more precisely called 'inouts'.) Conversely, registers that are just read from must not be passed as outputs. The following chart shows all the instruction forms supported by Mu, along with the SubX instruction they're translated to. ## Integer instructions These instructions use the general-purpose registers. var/eax <- increment => "40/increment-eax" var/ecx <- increment => "41/increment-ecx" var/edx <- increment => "42/increment-edx" var/ebx <- increment => "43/increment-ebx" var/esi <- increment => "46/increment-esi" var/edi <- increment => "47/increment-edi" increment var => "ff 0/subop/increment *(ebp+" var.stack-offset ")" increment *var/reg => "ff 0/subop/increment *" reg var/eax <- decrement => "48/decrement-eax" var/ecx <- decrement => "49/decrement-ecx" var/edx <- decrement => "4a/decrement-edx" var/ebx <- decrement => "4b/decrement-ebx" var/esi <- decrement => "4e/decrement-esi" var/edi <- decrement => "4f/decrement-edi" decrement var => "ff 1/subop/decrement *(ebp+" var.stack-offset ")" decrement *var/reg => "ff 1/subop/decrement *" reg var/reg <- add var2/reg2 => "01/add-to %" reg " " reg2 "/r32" var/reg <- add var2 => "03/add *(ebp+" var2.stack-offset ") " reg "/r32" var/reg <- add *var2/reg2 => "03/add *" reg2 " " reg "/r32" add-to var1, var2/reg => "01/add-to *(ebp+" var1.stack-offset ") " reg "/r32" add-to *var1/reg1, var2/reg2 => "01/add-to *" reg1 " " reg2 "/r32" var/eax <- add n => "05/add-to-eax " n "/imm32" var/reg <- add n => "81 0/subop/add %" reg " " n "/imm32" add-to var, n => "81 0/subop/add *(ebp+" var.stack-offset ") " n "/imm32" add-to *var/reg, n => "81 0/subop/add *" reg " " n "/imm32" var/reg <- subtract var2/reg2 => "29/subtract-from %" reg " " reg2 "/r32" var/reg <- subtract var2 => "2b/subtract *(ebp+" var2.stack-offset ") " reg "/r32" var/reg <- subtract *var2/reg2 => "2b/subtract *" reg2 " " reg1 "/r32" subtract-from var1, var2/reg2 => "29/subtract-from *(ebp+" var1.stack-offset ") " reg2 "/r32" subtract-from *var1/reg1, var2/reg2 => "29/subtract-from *" reg1 " " reg2 "/r32" var/eax <- subtract n => "2d/subtract-from-eax " n "/imm32" var/reg <- subtract n => "81 5/subop/subtract %" reg " " n "/imm32" subtract-from var, n => "81 5/subop/subtract *(ebp+" var.stack-offset ") " n "/imm32" subtract-from *var/reg, n => "81 5/subop/subtract *" reg " " n "/imm32" var/reg <- and var2/reg2 => "21/and-with %" reg " " reg2 "/r32" var/reg <- and var2 => "23/and *(ebp+" var2.stack-offset " " reg "/r32" var/reg <- and *var2/reg2 => "23/and *" reg2 " " reg "/r32" and-with var1, var2/reg => "21/and-with *(ebp+" var1.stack-offset ") " reg "/r32" and-with *var1/reg1, var2/reg2 => "21/and-with *" reg1 " " reg2 "/r32" var/eax <- and n => "25/and-with-eax " n "/imm32" var/reg <- and n => "81 4/subop/and %" reg " " n "/imm32" and-with var, n => "81 4/subop/and *(ebp+" var.stack-offset ") " n "/imm32" and-with *var/reg, n => "81 4/subop/and *" reg " " n "/imm32" var/reg <- or var2/reg2 => "09/or-with %" reg " " reg2 "/r32" var/reg <- or var2 => "0b/or *(ebp+" var2.stack-offset ") " reg "/r32" var/reg <- or *var2/reg2 => "0b/or *" reg2 " " reg "/r32" or-with var1, var2/reg2 => "09/or-with *(ebp+" var1.stack-offset " " reg2 "/r32" or-with *var1/reg1, var2/reg2 => "09/or-with *" reg1 " " reg2 "/r32" var/eax <- or n => "0d/or-with-eax " n "/imm32" var/reg <- or n => "81 1/subop/or %" reg " " n "/imm32" or-with var, n => "81 1/subop/or *(ebp+" var.stack-offset ") " n "/imm32" or-with *var/reg, n => "81 1/subop/or *" reg " " n "/imm32" var/reg <- xor var2/reg2 => "31/xor-with %" reg " " reg2 "/r32" var/reg <- xor var2 => "33/xor *(ebp+" var2.stack-offset ") " reg "/r32" var/reg <- xor *var2/reg2 => "33/xor *" reg2 " " reg "/r32" xor-with var1, var2/reg => "31/xor-with *(ebp+" var1.stack-offset ") " reg "/r32" xor-with *var1/reg1, var2/reg2 => "31/xor-with *" reg1 " " reg2 "/r32" var/eax <- xor n => "35/xor-with-eax " n "/imm32" var/reg <- xor n => "81 6/subop/xor %" reg " " n "/i
#!/bin/bash
# Helper to change the numerical prefixes across the repo, say if you want to
# create room between 023 and 024, and so on.
#
# Assumes there's only ever one file with any numeric prefix. If you move
# 003trace.test.cc you might need to do some manual patch-up.
set -e
if [[ $# -eq 0 && `git diff HEAD |wc -l` -gt 0 ]]
then
echo "Uncommitted changes"
exit
fi
if [[ $# -gt 0 ]] # dry run
then
git() {
echo $*
}
fi
#
index=0
ls [0-9]* |grep -v "trace.test" |sort -n |
while read file
do
while [[ $file != `printf "%03d" $index`* ]]
do
echo
index=$(($index+1))
done
echo $file
index=$(($index+1))
done > .layout
vim -c "set nu" .layout
#
root() {
echo $1 |sed 's/^[0-9]*//'
}
index=0
cat .layout |
while read file
do
if [ ! -z $file ]
then
newfile=`printf "%03d" $index``root $file`
if [[ $newfile != $file ]]
then
echo git mv $file $newfile
git mv $file $newfile
fi
fi
index=$(($index+1))
done
rm .layout
# Scenarios considered:
# Don't redo the layout if Vim exits with error.