Skip to content

orlof/dcpu-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dcpu-compiler

Optimizing DCPU assembler in python

usage: compiler.py [-h] [--doc] [--debug] [--optimize] [--boot] [--verbose]
                   [--org ORG]
                   input [output]

compiler for DCPU assembler

positional arguments:
  input       source code (.dasm16)
  output      binary file (.bin)

optional arguments:
  -h, --help  show this help message and exit
  --doc       create documentation from doc directives
  --debug     create debugger info *.dbg see http://github.com/orlof/dcpu-
              debugger
  --optimize  optimize local jumps with xor, add and sub - may clear EX
  --boot      create also TC *.boot disk
  --verbose   print every character from lexer
  --org ORG   destination address

Preprocessor directives

#define

  • No multiline or function defines
  • Nested defines ok

#include

supported

#doc

Command line option --doc creates .txt file that collects all doc string and prepends those with address.

set a, 1
#doc HERE WE ARE
set b, 2
#doc ..and again
0x0001: HERE WE ARE
0x0002: ..and again

Strictly speaking doc is not implemented in proprocessor as later compilation passes modify memory locations.

Special directives

DAT

DAT directive supports list of items that can be

  • string literals
  • expression of chars, integers, defined macros and labels
    • operations: +, -, * and /

All of the following are valid values:

  #define ZERO 0

:data_label
  DAT 6, "abcde", ZERO, data_label + 1, 'A' + 20

String literals support following escape sequences:

Escape sequence Hex value Character represented
\\ 0x5c Backslash
\b 0x0010 Backspace
\t 0x0010 Horizontal tab
\r 0x0011 Carriage return
\n 0x0011 Newline (aka linefeed, carriage return in DCPU\r)
\0 0x0000 Zero
\' 0x0027 Single quotation mark
\" 0x0022 Double quotation mark
\xhh 0x0000-0x00ff The character whose numerical value is given by hh interpreted as a hexadecimal number

JMP

JMP is an optimizing instruction for modifying PC with literal value.

set PC, 0xbeef
jmp 0xbeef

JMP can be used with any of the a-operand formats, but it functions as standard set operator.

jmp [a + 5]
set PC, [a + 5]

With literal a-operand type JMP compiles into one of the following instructions (in priority order):

Instruction Range Memory Cycles Notes
SET PC, 0x10 0x0000-0x001e, 0xffff 1 1 Set with short form literal
XOR PC, 0x10 local 5 bit segment in memory 1 1 Cannot jump to 0x1f in local segment
ADD PC, 0x10 0x00-0x1e words forward 1 2 Zeroes EX flag!
SUB PC, 0x10 0x00-0x1e words backward 1 2 Zeroes EX flag!
SET PC, 0x1000 0x0000-0xffff 2 2 Standard set can jump anywhere in memory

Single line IFx

Compiler also supports format where IFx and "then" instructions are written in single line:

  IFE a, 5 SET a, 6

Output Options

Default

Compiler always outputs the compiled binary image as .bin file that. If --org command line option is specified, then all the jump addresses (etc) are modified accordingly, but the binary output contains only the compiled code i.e. No zeros for 0-org addresses.

Boot disk

--boot option outputs Techcompliant boot disk image (floppy).

Boot disk will have filename extension ".boot". It contains the standard BBOS boot code in sector 0 [https://github.com/MadMockers/DCPUBootloader/tree/master/util] and compiled code in the following sectors.

Debug Information

--debug option outputs debug information for [https://github.com/orlof/dcpu-debugger].

Debug information is stored with file extension ".dbg""

Optimizations

Short literals

Short literals are always used instead of long literals. Works also with defined values, address labels and expressions.

IFx swap

If "IFx" statement contains short literal as b-operand then a and b are swapped to be able to use short literal optimization.

Local jumps

With --optimize command line option all "set pc, 0xbeef" -style instructions are optimized as they were "jmp 0xbeef" instructions. This may break applications that trust in EX value preservation during jumps.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages