Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alexfru/regal86

Register Allocator for 8086
https://github.com/alexfru/regal86

8086 assembly code-generation compiler compiler-backend compiler-design compiler-optimization dos expression-evaluation register-allocation x86

Last synced: 4 days ago
JSON representation

Register Allocator for 8086

Awesome Lists containing this project

README

        

# Local Register Allocator for 8086

## Table of contents

[What is this?](#what-is-this)

[Motivation](#motivation)

[Scope](#scope)

[Basic algorithm](#basic-algorithm)

[8086-specific approach](#8086-specific-approach)

[Resources](#resources)

## What is this?

This is an implementation of a greedy bottom-up local register allocator for
the intel 8086 CPU. It may be used as part of a simple compiler. In particular,
it can be used in a C compiler whose `int` is 16-bit and `char` is 8-bit.

To make things interesting and interactive it comes with a code generator
capable of generating 8086 assembly code (compilable into DOS .COM programs)
from trees representing expressions with 16-bit integer ALU operations,
constants and memory loads and stores. A few extra instructions generated by
the code check the results of expression evaluation in the output register and
the output memory locations, if any.

Thus you can actually execute the generated code and tweak things around to see
how your changes affect code generation.

Sample assembly output from the register allocator/code generator:

; 7 (vr4)
; mul (vr5)
; 5 (vr3)
; add (vr6)
; 3 (vr1)
; mul (vr2)
; 2 (vr0)
; ----
; Regs needed (approximately): 3
; --------
; ; vr0
mov ax, 2 ; vr0
mov cx, 3 ; vr1
mul cx ; vr2
mov cx, 5 ; vr3
mov dx, 7 ; vr4
xchg ax, cx ; vr5
mul dx ; vr5
add cx, ax ; vr6
; ; vr6
cmp cx, 41 ; vr6
jne failure ; vr6

N.B. This register allocator will not allocate registers perfectly in all
circumstances. It will occasionally generate unnecessary register moves and
exchanges. Bear in mind, optimal register allocation is an NP-complete problem.

## Motivation

The intel 8086 architecture has a number of instructions with fixed input
and/or output registers and it's not entirely trivial to connect the ins and
outs of these instructions. There's obviously a long history of implementing
compilers for this and similar architectures, but it's a bit odd that it's
hard to find a conceptually simple algorithm like this. The "Dragon" book and
many other sources either consider architectures without instructions with such
fixed register operands or solve the problem using graph coloring, which for
certain reasons may be impractical.

The intel 8086 architecture survives to this day in the modern intel Pentium
CPUs that are compatible descendants of the old 8086 and with it survive some
of the instructions with fixed in/out register operands. So, the problem
persists to this day, although there are fewer limitations to deal with when
using newer 32-bit and 64-bit x86 instructions and registers today.

8086 registers by special uses in expression evaluation (pardon the mixture of
C and assembly operators, instructions and syntax and ASCII art):

+-- can be freely used in most ALU instructions as src/dst
| +-- can be used as address to access memory
| | +-- has individual 8-bit subregisters
| | | +-- can be sign-extended with cbw
| | | | +-- can be shifted
| | | | | +-- can be shift count
v v v v v v
+-<&|^~ [r] 8bit cbw <