Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works
Anything · Lazarus or Delphi
Random projectRandom project

sub-pascal

View on GitHub

Member rating

No ratings yet.

Sign in to rate this project.

README

text
Sub-Pascal 32-bit real mode compiler for 80386+ processors v. 2.0
-----------------------------------------------------------------

I. Language

Sub-Pascal is a subset of Pascal programming language. It includes:

1.  If, For, While, Repeat statements.
2.  Compound statements.
3.  Arithmetic and boolean operators.
4.  Procedures and functions with up to 8 parameters. Returned value of a function is assigned to a predefined Result variable.
5.  Static local variables.
6.  Built-in Write, Read, Halt, Intr procedures.
7.  Six primitive data types: Integer, SmallInt, Char, Boolean, Pointer, Real (fixed-point). All types except the Real type are compatible. Pointers are dereferenced as pointers to Integer.
8.  One-dimensional arrays (with zero lower bound) of any primitive type. Arrays are treated as pointers to their origins (like in C) and can be passed to subroutines as parameters.
9.  Predefined type string [N] which is equivalent to array [0..N] of Char.
10. Type aliases.
11. Separate program modules.

It does not include:

1.  Case, Goto statements. Avoided by using If and While statements.
2.  Records and multidimensional arrays. Avoided by using one-dimensional arrays and linear index calculation functions.
3.  Recursion.
4.  File operations.

Language EBNF:

program = block "." .

block = { [ "const" ident "=" constexpression ";" {ident "=" constexpression ";"} ]
          [ "type" ident = type ";" {ident = type ";"} ]
          [ "var" (ident {"," ident}) ":" type ";" 
                { (ident {"," ident}) ":" type ";"} ]
          [ "procedure" ident [ formalparameters ] ";" block ";"] } |
            "function"  ident [ formalparameters ] ":" type ";" block ";" ] }
             statement .

formalparameters = "(" (ident {"," ident}) ":" type {";" (ident {"," ident}) ":" type}  ")" .

type = "Integer" | "SmallInt" | "Char" | "Boolean" | "Pointer" | "Real" |
       "array"  "[" constexpression ".." constexpression "]" "of" type |
       "string" "[" constexpression "]" |
       ident .

statement = [ ident ["^" | "[" expression "]"] ":=" (stringliteral | charliteral | expression) | 
              ident [ "(" expression {"," expression} ")" ]|
              "begin" statement {";" statement } "end" |
              "if" expression "then" statement ["else" statement] |
              "while" expression "do" statement |
              "repeat" statement {";" statement} "until" expression | 
              "for" ident ":=" expression "to" expression "do" statement |
              "Read" "(" ident ")" |
              "Write" "(" (expression | stringliteral) {"," (expression | stringliteral)} ")" |
              "Halt" |
              "Intr" "(" constexpression "," expression ")" ].

constexpression = expression .

expression = simpleexpression [("="|"<>"|"<"|"<="|">"|">=") simpleexpression] .

simpleexpression = ["+"|"-"] term { ("+"|"-"|"or"|"xor") term}.

term = factor {("*"|"/"|"div"|"mod"|"shl"|"shr"|"and") factor}.

factor = ident [ "(" expression {"," expression} ")" ] |
         ident ["^" | "[" expression "]"] |
         "@" ident | 
         number | 
         charliteral | 
         "(" expression ")" | 
         "not" factor |
         "Integer" | "Char" | "Boolean" | "Pointer" | "Real" .

ident = letter | "_" { letter | "_" | digit }.

number = digit { digit } ["."] digit { digit }.

charliteral = "'" character "'".

stringliteral = "'" {character} "'".

II. Compiler 

Compiler builds a COM executable file containing one 64 Kbyte segment organized as follows:

0000h....00FFh - Program Segment Prefix (PSP) generated by DOS / NTVDM (not included into the file);
0100h....0102h - Jump to code area origin (XXXXh + 1);
0103h....XXXXh - Static string data area;
XXXXh+1..7FFFh - Code area;
8000h....EFFFh - Data area;
F000h....YYYYh - Expression stack area; BX register points to the stack top (incremented when the stack is being filled);
YYYYh+1..FFFEh - System stack area used to save procedure return addresses; SP register points to the stack top (decremented when the stack is being filled).

Generated code contains machine instructions with 16-bit and 32-bit operands. It can be executed on a 80386+ processor in real mode under DOS / NTVDM.   
Compiler performs some code optimization by eliminating procedures and functions which are never called. To detect them, two compilation passes are done and a call graph is built.
Source of the compiler can be compiled with any version of Delphi as a 32-bit console application.

III. Standard library

IO.SP       - Console input/output routines for integer and real numbers;
MATH.SP     - Mathematical functions;
GRAPH.SP    - EGA/VGA graphics routines.

IV. Examples

PRIMEDIS.SP - Prime number distribution density calculation;
LINEQ.SP    - Linear algebraic equation systems solver. Should be compiled along with GAUSS.SP;
LIFE.SP     - The game of life;
CANNABIS.SP - Cannabola plot in polar coordinates;
FRACTAL.SP  - Mandelbrot set fragment colored plot.

 

V. Future features
 
1. Data types:
   a. Signed and unsigned data;
   b. Nested data types (multidimensional arrays, arrays of records);
   c. Heap operations. Free memory list.

2. Recursion:
   a. Stack frames for local variables allocation. Reentrant subroutines;
   b. Forward declarations.

3. Input/output:
   a. File input/output.

4. Optimization:
   a. Register allocation.

VI. Disclaimer

The software is provided "as is", without warranties, either expressed or implied, of any kind.

Vasiliy Tereshkov,   
vtereshkov@mail.ru

March 2009