Close

Converting the Simple Compiler to a Subleq Compiler

A project log for Simple Compiler

A very simple compiler for minimalist home brew CPUs

agpcooperagp.cooper 06/24/2017 at 17:030 Comments

Compiler Subleq Backend

In order to convert Simple Compiler to a Subleq compiler, I have to replace the Simple Compiler Assembler (OpCode) Lister and Assemble (OpCode) Interpreter with a Subleq OpCode exporter. I will use an external Subleq assembler and interpreter. The Subleq OpCode exporter is called the Subleq backend. The Subleq backend contains all the Subleq macros, but only composite macros (which takes an operand) are accessible. Here is the list of Subleq macros:

And here is th list of compiler OpCode that are supported:The "system" command sets up Subleq with:

The Subleq system also has a point to pointer copy (PPC) routine (as it is assumed the monitor code will be stored in ROM and pointer to pointer copy requires self modifying code).

I will likely add many more constants to support string output later.

I have added the binary operation macros:

These bit operations don't update the flag register.

Integer Minimum Value

The minimum integer value (MIN) for a 16 bit integer is -32768. Subleq has a major problem with MIN. It treats MIN as equal to 0 unless special precautions are taken. This is not usually a problem unless the code uses the sign bit (such as for bit operations etc.). In the end I rewrote all the test macros (i.e. jeqz, jnez, jlez, jltz, jgez and jgtz) to be MIN aware.

I also used MIN as the return value for multiplication overflow and attempt to divide by zero. There is a "jmin" macro if required.

Working Subleq Backend

I have got a working Subleq backend test-bed (TestMacro2Subleq), for example the following Subleq macros:

  compositeMacro("system",0);
  compositeMacro("rdAx",0);
  compositeMacro("pushAx",0);
  compositeMacro("pushAx",0);
  compositeMacro("rdAx",0);
  compositeMacro("movBxAx",0);
  compositeMacro("popAx",0);
  compositeMacro("pushBx",0);
  // iMul
  compositeMacro("mulBx",0);
  compositeMacro("wrtInt",0);
  compositeMacro("wrtLn",0);
  // iDiv
  compositeMacro("popBx",0);
  compositeMacro("popAx",0);
  compositeMacro("divBx",0);
  compositeMacro("wrtInt",0);
  compositeMacro("wrtLn",0);
  compositeMacro("movAxBx",0);
  compositeMacro("wrtInt",0);
  compositeMacro("wrtLn",0);

Produces after assembly and interpretation:

C:\AlanX\TestSubleq>TestMacro2Subleq  1>Test.sasm
C:\AlanX\TestSubleq>subleq_asm -i Test.sasm -o Test.code -l Test.list
C:\AlanX\TestSubleq>Subleq_Int -i Test.code -l Test.list -t Test.trace

SUBLEQ Interpreter

6   <- Entered integer
2   <- Entered integer
12  -> Integer multiplication (=6*2)
3   -> Integer division (=6/2)
0   -> Integer remainder (=6%2)


Interpreter finished

C:\AlanX\TestSubleq>pause
Press any key to continue . . .

The Subleq code produced is too long to list here.

AlanX



Discussions